2015-07-17 00:40:46 +00:00
|
|
|
//
|
2016-03-05 19:45:09 +00:00
|
|
|
// CSOpenGLView
|
2015-07-26 19:25:11 +00:00
|
|
|
// CLK
|
2015-07-17 00:40:46 +00:00
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 16/07/2015.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2015 Thomas Harte. All rights reserved.
|
2015-07-17 00:40:46 +00:00
|
|
|
//
|
|
|
|
|
2016-03-05 19:45:09 +00:00
|
|
|
#import "CSOpenGLView.h"
|
2015-07-17 00:40:46 +00:00
|
|
|
@import CoreVideo;
|
2015-09-25 01:23:16 +00:00
|
|
|
@import GLKit;
|
2015-07-17 00:40:46 +00:00
|
|
|
|
2017-08-17 14:48:29 +00:00
|
|
|
@interface CSOpenGLView () <NSDraggingDestination>
|
|
|
|
@end
|
|
|
|
|
2016-03-05 19:45:09 +00:00
|
|
|
@implementation CSOpenGLView {
|
2016-02-05 03:28:50 +00:00
|
|
|
CVDisplayLinkRef _displayLink;
|
2017-09-20 23:59:34 +00:00
|
|
|
CGSize _backingSize;
|
2019-03-07 02:49:50 +00:00
|
|
|
|
|
|
|
NSTrackingArea *_mouseTrackingArea;
|
|
|
|
NSTimer *_mouseHideTimer;
|
2019-06-11 20:30:53 +00:00
|
|
|
BOOL _mouseIsCaptured;
|
2015-07-17 00:40:46 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 02:54:21 +00:00
|
|
|
- (void)prepareOpenGL {
|
2019-03-26 23:47:41 +00:00
|
|
|
[super prepareOpenGL];
|
|
|
|
|
2015-07-17 01:16:21 +00:00
|
|
|
// Synchronize buffer swaps with vertical refresh rate
|
|
|
|
GLint swapInt = 1;
|
|
|
|
[[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval];
|
2015-07-17 00:40:46 +00:00
|
|
|
|
2015-07-17 01:16:21 +00:00
|
|
|
// Create a display link capable of being used with all active displays
|
2016-02-05 03:28:50 +00:00
|
|
|
CVDisplayLinkCreateWithActiveCGDisplays(&_displayLink);
|
2016-04-21 02:35:46 +00:00
|
|
|
|
2015-07-17 01:16:21 +00:00
|
|
|
// Set the renderer output callback function
|
2016-02-05 03:28:50 +00:00
|
|
|
CVDisplayLinkSetOutputCallback(_displayLink, DisplayLinkCallback, (__bridge void * __nullable)(self));
|
2016-03-04 03:12:31 +00:00
|
|
|
|
2015-07-17 01:16:21 +00:00
|
|
|
// Set the display link for the current renderer
|
|
|
|
CGLContextObj cglContext = [[self openGLContext] CGLContextObj];
|
|
|
|
CGLPixelFormatObj cglPixelFormat = [[self pixelFormat] CGLPixelFormatObj];
|
2016-02-05 03:28:50 +00:00
|
|
|
CVDisplayLinkSetCurrentCGDisplayFromOpenGLContext(_displayLink, cglContext, cglPixelFormat);
|
2015-07-26 19:13:46 +00:00
|
|
|
|
2016-03-04 03:12:31 +00:00
|
|
|
// set the clear colour
|
2015-07-26 19:25:11 +00:00
|
|
|
[self.openGLContext makeCurrentContext];
|
2015-07-26 19:55:19 +00:00
|
|
|
glClearColor(0.0, 0.0, 0.0, 1.0);
|
2015-07-26 19:13:46 +00:00
|
|
|
|
2015-07-17 01:16:21 +00:00
|
|
|
// Activate the display link
|
2016-02-05 03:28:50 +00:00
|
|
|
CVDisplayLinkStart(_displayLink);
|
2015-07-17 01:16:21 +00:00
|
|
|
}
|
2015-07-17 00:40:46 +00:00
|
|
|
|
2019-03-07 02:54:21 +00:00
|
|
|
static CVReturn DisplayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeStamp *now, const CVTimeStamp *outputTime, CVOptionFlags flagsIn, CVOptionFlags *flagsOut, void *displayLinkContext) {
|
2016-04-21 02:35:46 +00:00
|
|
|
CSOpenGLView *const view = (__bridge CSOpenGLView *)displayLinkContext;
|
|
|
|
[view drawAtTime:now frequency:CVDisplayLinkGetActualOutputVideoRefreshPeriod(displayLink)];
|
2015-07-17 01:16:21 +00:00
|
|
|
return kCVReturnSuccess;
|
|
|
|
}
|
2015-07-28 01:15:10 +00:00
|
|
|
|
2019-03-07 02:54:21 +00:00
|
|
|
- (void)drawAtTime:(const CVTimeStamp *)now frequency:(double)frequency {
|
2019-03-03 19:49:20 +00:00
|
|
|
[self redrawWithEvent:CSOpenGLViewRedrawEventTimer];
|
2019-03-03 00:33:28 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 02:54:21 +00:00
|
|
|
- (void)drawRect:(NSRect)dirtyRect {
|
2019-03-03 19:49:20 +00:00
|
|
|
[self redrawWithEvent:CSOpenGLViewRedrawEventAppKit];
|
|
|
|
}
|
|
|
|
|
2019-03-07 02:54:21 +00:00
|
|
|
- (void)redrawWithEvent:(CSOpenGLViewRedrawEvent)event {
|
2019-03-03 19:49:20 +00:00
|
|
|
[self performWithGLContext:^{
|
|
|
|
[self.delegate openGLViewRedraw:self event:event];
|
|
|
|
CGLFlushDrawable([[self openGLContext] CGLContextObj]);
|
|
|
|
}];
|
2016-03-04 03:12:31 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 02:54:21 +00:00
|
|
|
- (void)invalidate {
|
2016-02-05 03:28:50 +00:00
|
|
|
CVDisplayLinkStop(_displayLink);
|
2015-07-28 01:15:10 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 02:54:21 +00:00
|
|
|
- (void)dealloc {
|
2015-07-17 01:16:21 +00:00
|
|
|
// Release the display link
|
2016-02-05 03:28:50 +00:00
|
|
|
CVDisplayLinkRelease(_displayLink);
|
2015-07-17 00:40:46 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 02:54:21 +00:00
|
|
|
- (CGSize)backingSize {
|
2017-09-20 23:59:34 +00:00
|
|
|
@synchronized(self) {
|
|
|
|
return _backingSize;
|
|
|
|
}
|
2016-01-07 04:14:36 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 02:54:21 +00:00
|
|
|
- (void)reshape {
|
2015-07-24 02:51:53 +00:00
|
|
|
[super reshape];
|
2017-09-20 23:59:34 +00:00
|
|
|
@synchronized(self) {
|
|
|
|
_backingSize = [self convertSizeToBacking:self.bounds.size];
|
|
|
|
}
|
2015-07-24 02:51:53 +00:00
|
|
|
|
2016-03-20 02:46:17 +00:00
|
|
|
[self performWithGLContext:^{
|
|
|
|
CGSize viewSize = [self backingSize];
|
|
|
|
glViewport(0, 0, (GLsizei)viewSize.width, (GLsizei)viewSize.height);
|
|
|
|
}];
|
2015-07-24 02:51:53 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 02:54:21 +00:00
|
|
|
- (void)awakeFromNib {
|
|
|
|
NSOpenGLPixelFormatAttribute attributes[] = {
|
2015-07-24 02:51:53 +00:00
|
|
|
NSOpenGLPFADoubleBuffer,
|
|
|
|
NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
|
2018-11-20 04:36:29 +00:00
|
|
|
// NSOpenGLPFAMultisample,
|
|
|
|
// NSOpenGLPFASampleBuffers, 1,
|
|
|
|
// NSOpenGLPFASamples, 2,
|
2015-07-24 02:51:53 +00:00
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
NSOpenGLPixelFormat *pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
|
|
|
|
NSOpenGLContext *context = [[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:nil];
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
// When we're using a CoreProfile context, crash if we call a legacy OpenGL function
|
|
|
|
// This will make it much more obvious where and when such a function call is made so
|
|
|
|
// that we can remove such calls.
|
|
|
|
// Without this we'd simply get GL_INVALID_OPERATION error for calling legacy functions
|
|
|
|
// but it would be more difficult to see where that function was called.
|
|
|
|
CGLEnable([context CGLContextObj], kCGLCECrashOnRemovedFunctions);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
self.pixelFormat = pixelFormat;
|
|
|
|
self.openGLContext = context;
|
|
|
|
self.wantsBestResolutionOpenGLSurface = YES;
|
2017-08-17 14:48:29 +00:00
|
|
|
|
|
|
|
// Register to receive dragged and dropped file URLs.
|
|
|
|
[self registerForDraggedTypes:@[(__bridge NSString *)kUTTypeFileURL]];
|
2015-07-24 02:51:53 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 02:54:21 +00:00
|
|
|
- (void)performWithGLContext:(dispatch_block_t)action {
|
2015-08-13 21:01:25 +00:00
|
|
|
CGLLockContext([[self openGLContext] CGLContextObj]);
|
2016-03-20 02:54:56 +00:00
|
|
|
[self.openGLContext makeCurrentContext];
|
2016-03-20 02:46:17 +00:00
|
|
|
action();
|
2015-08-13 21:01:25 +00:00
|
|
|
CGLUnlockContext([[self openGLContext] CGLContextObj]);
|
2015-07-26 19:13:46 +00:00
|
|
|
}
|
|
|
|
|
2015-08-19 00:33:24 +00:00
|
|
|
#pragma mark - NSResponder
|
|
|
|
|
2019-03-07 02:54:21 +00:00
|
|
|
- (BOOL)acceptsFirstResponder {
|
2015-08-19 00:33:24 +00:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2019-03-07 02:54:21 +00:00
|
|
|
- (void)keyDown:(NSEvent *)theEvent {
|
2016-03-20 02:46:17 +00:00
|
|
|
[self.responderDelegate keyDown:theEvent];
|
2015-08-19 00:33:24 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 02:54:21 +00:00
|
|
|
- (void)keyUp:(NSEvent *)theEvent {
|
2016-03-20 02:46:17 +00:00
|
|
|
[self.responderDelegate keyUp:theEvent];
|
2015-08-19 00:33:24 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 02:54:21 +00:00
|
|
|
- (void)flagsChanged:(NSEvent *)theEvent {
|
2016-03-20 02:46:17 +00:00
|
|
|
[self.responderDelegate flagsChanged:theEvent];
|
2019-06-11 20:30:53 +00:00
|
|
|
|
|
|
|
// Release the mouse upon a control + command.
|
|
|
|
if(_mouseIsCaptured &&
|
|
|
|
theEvent.modifierFlags & NSEventModifierFlagControl &&
|
|
|
|
theEvent.modifierFlags & NSEventModifierFlagCommand) {
|
|
|
|
[self releaseMouse];
|
|
|
|
}
|
2015-08-19 00:33:24 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 02:54:21 +00:00
|
|
|
- (void)paste:(id)sender {
|
2018-05-13 15:12:03 +00:00
|
|
|
[self.responderDelegate paste:sender];
|
|
|
|
}
|
|
|
|
|
2017-08-17 14:48:29 +00:00
|
|
|
#pragma mark - NSDraggingDestination
|
|
|
|
|
2019-03-07 02:54:21 +00:00
|
|
|
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender {
|
|
|
|
for(NSPasteboardItem *item in [[sender draggingPasteboard] pasteboardItems]) {
|
2017-08-17 14:48:29 +00:00
|
|
|
NSURL *URL = [NSURL URLWithString:[item stringForType:(__bridge NSString *)kUTTypeFileURL]];
|
|
|
|
[self.delegate openGLView:self didReceiveFileAtURL:URL];
|
|
|
|
}
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2019-03-07 02:54:21 +00:00
|
|
|
- (NSDragOperation)draggingEntered:(id < NSDraggingInfo >)sender {
|
2017-08-17 14:48:29 +00:00
|
|
|
return NSDragOperationLink;
|
|
|
|
}
|
|
|
|
|
2019-03-07 02:49:50 +00:00
|
|
|
#pragma mark - Mouse hiding
|
|
|
|
|
2019-06-11 20:30:53 +00:00
|
|
|
- (void)setShouldCaptureMouse:(BOOL)shouldCaptureMouse {
|
|
|
|
_shouldCaptureMouse = shouldCaptureMouse;
|
|
|
|
}
|
|
|
|
|
2019-03-07 02:49:50 +00:00
|
|
|
- (void)updateTrackingAreas {
|
|
|
|
[super updateTrackingAreas];
|
|
|
|
|
|
|
|
if(_mouseTrackingArea) {
|
|
|
|
[self removeTrackingArea:_mouseTrackingArea];
|
|
|
|
}
|
|
|
|
_mouseTrackingArea =
|
|
|
|
[[NSTrackingArea alloc]
|
|
|
|
initWithRect:self.bounds
|
|
|
|
options:NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved | NSTrackingActiveWhenFirstResponder
|
|
|
|
owner:self
|
|
|
|
userInfo:nil];
|
|
|
|
[self addTrackingArea:_mouseTrackingArea];
|
|
|
|
}
|
|
|
|
|
2019-06-11 20:30:53 +00:00
|
|
|
- (void)scheduleMouseHide {
|
|
|
|
if(!self.shouldCaptureMouse) {
|
|
|
|
[_mouseHideTimer invalidate];
|
|
|
|
|
|
|
|
_mouseHideTimer = [NSTimer scheduledTimerWithTimeInterval:3.0 repeats:NO block:^(NSTimer * _Nonnull timer) {
|
|
|
|
[NSCursor setHiddenUntilMouseMoves:YES];
|
|
|
|
}];
|
|
|
|
}
|
2019-03-07 02:49:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)mouseEntered:(NSEvent *)event {
|
|
|
|
[super mouseEntered:event];
|
|
|
|
[self scheduleMouseHide];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)mouseExited:(NSEvent *)event {
|
|
|
|
[super mouseExited:event];
|
|
|
|
[_mouseHideTimer invalidate];
|
|
|
|
_mouseHideTimer = nil;
|
|
|
|
}
|
|
|
|
|
2019-06-11 20:30:53 +00:00
|
|
|
- (void)releaseMouse {
|
2019-06-13 17:39:35 +00:00
|
|
|
if(_mouseIsCaptured) {
|
|
|
|
_mouseIsCaptured = NO;
|
|
|
|
CGAssociateMouseAndMouseCursorPosition(true);
|
|
|
|
[NSCursor unhide];
|
|
|
|
}
|
2019-06-11 20:30:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Mouse motion
|
|
|
|
|
|
|
|
- (void)applyMouseMotion:(NSEvent *)event {
|
|
|
|
if(!self.shouldCaptureMouse) {
|
|
|
|
// Mouse capture is off, so don't play games with the cursor, just schedule it to
|
|
|
|
// hide in the near future.
|
|
|
|
[self scheduleMouseHide];
|
|
|
|
} else {
|
|
|
|
if(_mouseIsCaptured) {
|
|
|
|
// Mouse capture is on, so move the cursor back to the middle of the window, and
|
|
|
|
// forward the deltas to the listener.
|
|
|
|
//
|
|
|
|
// TODO: should I really need to invert the y coordinate myself? It suggests I
|
|
|
|
// might have an error in mapping here.
|
|
|
|
const NSPoint windowCentre = [self convertPoint:CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height * 0.5) toView:nil];
|
|
|
|
const NSPoint screenCentre = [self.window convertPointToScreen:windowCentre];
|
|
|
|
const CGRect screenFrame = self.window.screen.frame;
|
|
|
|
CGWarpMouseCursorPosition(NSMakePoint(
|
|
|
|
screenFrame.origin.x + screenCentre.x,
|
|
|
|
screenFrame.origin.y + screenFrame.size.height - screenCentre.y
|
|
|
|
));
|
|
|
|
|
|
|
|
[self.responderDelegate mouseMoved:event];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)mouseDragged:(NSEvent *)event {
|
|
|
|
[self applyMouseMotion:event];
|
|
|
|
[super mouseDragged:event];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)rightMouseDragged:(NSEvent *)event {
|
|
|
|
[self applyMouseMotion:event];
|
|
|
|
[super rightMouseDragged:event];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)otherMouseDragged:(NSEvent *)event {
|
|
|
|
[self applyMouseMotion:event];
|
|
|
|
[super otherMouseDragged:event];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)mouseMoved:(NSEvent *)event {
|
|
|
|
[self applyMouseMotion:event];
|
|
|
|
[super mouseMoved:event];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Mouse buttons
|
|
|
|
|
|
|
|
- (void)applyButtonDown:(NSEvent *)event {
|
2019-06-13 17:39:35 +00:00
|
|
|
if(self.shouldCaptureMouse && !_mouseIsCaptured) {
|
2019-06-11 20:30:53 +00:00
|
|
|
_mouseIsCaptured = YES;
|
|
|
|
[NSCursor hide];
|
|
|
|
CGAssociateMouseAndMouseCursorPosition(false);
|
|
|
|
|
|
|
|
[self.responderDelegate mouseDown:event];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)applyButtonUp:(NSEvent *)event {
|
|
|
|
if(self.shouldCaptureMouse) {
|
|
|
|
[self.responderDelegate mouseUp:event];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)mouseDown:(NSEvent *)event {
|
|
|
|
[self applyButtonDown:event];
|
|
|
|
[super mouseDown:event];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)rightMouseDown:(NSEvent *)event {
|
|
|
|
[self applyButtonDown:event];
|
|
|
|
[super rightMouseDown:event];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)otherMouseDown:(NSEvent *)event {
|
|
|
|
[self applyButtonDown:event];
|
|
|
|
[super otherMouseDown:event];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)mouseUp:(NSEvent *)event {
|
|
|
|
[self applyButtonUp:event];
|
|
|
|
[super mouseUp:event];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)rightMouseUp:(NSEvent *)event {
|
|
|
|
[self applyButtonUp:event];
|
|
|
|
[super rightMouseUp:event];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)otherMouseUp:(NSEvent *)event {
|
|
|
|
[self applyButtonUp:event];
|
|
|
|
[super otherMouseUp:event];
|
|
|
|
}
|
|
|
|
|
2015-07-17 00:40:46 +00:00
|
|
|
@end
|