1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
#import "CustomView.h"
#import "CustomLayer.h"
#import "vtkCocoaRenderWindow.h"
#import "vtkCocoaRenderWindowInteractor.h"
#import "vtkRenderer.h"
@implementation CustomView
// ----------------------------------------------------------------------------
// Designated initializer
- (instancetype)initWithFrame:(NSRect)frameRect
{
self = [super initWithFrame:frameRect];
if (self)
{
// nothing to do... add something if you need to
}
return self;
}
// ----------------------------------------------------------------------------
// Designated initializer
- (nullable instancetype)initWithCoder:(NSCoder*)coder
{
self = [super initWithCoder:coder];
if (self)
{
// nothing to do... add something if you need to
}
return self;
}
#pragma mark -
// ----------------------------------------------------------------------------
- (void)initializeVTKSupport
{
// The usual vtk object creation.
vtkRenderer* ren = vtkRenderer::New();
vtkRenderWindow* renWin = vtkRenderWindow::New();
vtkRenderWindowInteractor* renWinInt = vtkRenderWindowInteractor::New();
// The cast should never fail, but we do it anyway, as
// it's more correct to do so.
vtkCocoaRenderWindow* cocoaRenWin = vtkCocoaRenderWindow::SafeDownCast(renWin);
if (ren && cocoaRenWin && renWinInt)
{
//
[self setRenderer:ren];
[self setRenderWindow:cocoaRenWin];
[self setRenderWindowInteractor:renWinInt];
// This is special to our usage of vtk. To prevent vtk
// from creating an NSWindow and NSView automatically (its
// default behaviour) we tell vtk that they exist already.
// The APIs names are a bit misleading, due to the cross
// platform nature of vtk, but this usage is correct.
NSWindow* parentWindow = [self window];
assert(parentWindow);
cocoaRenWin->SetRootWindow((__bridge void*)parentWindow);
cocoaRenWin->SetWindowId((__bridge void*)self);
// Because we want our rendering to happen in our CAOpenGLLayer subclass (CustomLayer),
// instruct vtk to not associate the NSOpenGLContext it creates with our NSView.
cocoaRenWin->SetConnectContextToNSView(false);
// The usual vtk connections.
cocoaRenWin->AddRenderer(ren);
renWinInt->SetRenderWindow(cocoaRenWin);
// Initialize now so we are ready when the OS asks us to draw.
if (!renWinInt->GetInitialized())
{
renWinInt->Initialize();
}
}
}
// ----------------------------------------------------------------------------
- (void)cleanUpVTKSupport
{
vtkRenderer* ren = [self renderer];
vtkRenderWindow* renWin = [self renderWindow];
vtkRenderWindowInteractor* renWinInt = [self renderWindowInteractor];
if (ren)
{
ren->Delete();
}
if (renWin)
{
renWin->Delete();
}
if (renWinInt)
{
renWinInt->Delete();
}
[self setRenderer:nil];
[self setRenderWindow:nil];
[self setRenderWindowInteractor:nil];
}
#pragma mark -
// ----------------------------------------------------------------------------
- (void)initializeLayerSupport
{
// Make sure we don't do this twice.
assert([[self layer] isKindOfClass:[CustomLayer class]] == NO);
NSRect viewBounds = [self bounds];
// Create the CustomLayer.
CustomLayer* layer = [CustomLayer layer];
[layer setName:@"CustomLayer"];
[layer setAnchorPoint:CGPointZero];
[layer setPosition:CGPointZero];
[layer setBounds:viewBounds];
[layer setMasksToBounds:YES];
[layer setNeedsDisplayOnBoundsChange:YES];
[layer setOpaque:YES];
[layer setCustomView:self];
// If the view has been connected to a window use the window's backing scale factor as its content
// scale.
NSWindow* window = [self window];
if (window)
{
double backingScaleFactor = [window backingScaleFactor];
[layer setContentsScale:backingScaleFactor];
[self modifyDPIForBackingScaleFactor:backingScaleFactor];
}
// Newly created layers are generally invalidated after creation, and so must be told to redraw.
[layer setNeedsDisplay];
// Make us a layer-hosting (not layer-backed) view.
[self setLayer:layer];
[self setWantsLayer:YES];
}
#pragma mark - Backing Property Change
// ---------------------------------------------------------------------------------------------------------------
- (void)modifyDPIForBackingScaleFactor:(double)backingScaleFactor
{
vtkCocoaRenderWindow* renderWindow = [self renderWindow];
if (renderWindow)
{
// DPI has been hardcoded to 72 but in order to cater for text rendering discrepancies with
// vtkTextActors for instance we must adjust the scaling factor per NSWindow.
renderWindow->SetDPI((int)lround(72.0 * backingScaleFactor));
}
}
// ---------------------------------------------------------------------------------------------------------------
- (void)viewWillMoveToWindow:(nullable NSWindow*)inNewWindow
{
[super viewWillMoveToWindow:inNewWindow];
if (inNewWindow)
{
CGFloat backingScaleFactor = [inNewWindow backingScaleFactor];
assert(backingScaleFactor >= 1.0);
// If the layer exists set its content scale to the window's backing scale factor otherwise it
// will eventually get set when the layer is created (see 'initializeLayerSupport').
[[self layer] setContentsScale:backingScaleFactor];
// Update the rendering DPI per window.
[self modifyDPIForBackingScaleFactor:backingScaleFactor];
}
}
// ---------------------------------------------------------------------------------------------------------------
- (void)viewDidChangeBackingProperties
{
[super viewDidChangeBackingProperties];
NSWindow* window = [self window];
if (window)
{
CGFloat backingScaleFactor = [[self window] backingScaleFactor];
assert(backingScaleFactor >= 1.0);
// If the layer exists set its content scale to the window's backing scale factor otherwise when
// it's ready and the backing property changes (i.e moving between screens that differ in
// resolution) it will be set.
[[self layer] setContentsScale:backingScaleFactor];
// Update the rendering DPI per window.
[self modifyDPIForBackingScaleFactor:backingScaleFactor];
}
}
@end
|