summaryrefslogtreecommitdiff
path: root/indra/llwindow/llopenglview-objc.mm
blob: 7c148f4acdd01987c2a42b537760181b7ca27679 (plain)
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
//
//  LLOpenGLView.m
//  SecondLife
//
//  Created by Geenz on 10/2/12.
//
//

#import "llopenglview-objc.h"

@implementation LLOpenGLView

- (void)viewDidMoveToWindow
{
	[[NSNotificationCenter defaultCenter] addObserver:self
											 selector:@selector(windowResized:) name:NSWindowDidResizeNotification
											   object:[self window]];
}

- (void)windowResized:(NSNotification *)notification;
{
	if (mResizeCallback != nil)
	{
		NSSize size = [[self window] frame].size;
		
		mResizeCallback(size.width, size.height);
	}
}

- (void)dealloc
{
	[[NSNotificationCenter defaultCenter] removeObserver:self];
	[super dealloc];
}

- (id) initWithFrame:(NSRect)frame withSamples:(NSUInteger)samples andVsync:(BOOL)vsync
{
	
	[self initWithFrame:frame];
	
	// Initialize with a default "safe" pixel format that will work with versions dating back to OS X 10.6.
	// Any specialized pixel formats, i.e. a core profile pixel format, should be initialized through rebuildContextWithFormat.
	// 10.7 and 10.8 don't really care if we're defining a profile or not.  If we don't explicitly request a core or legacy profile, it'll always assume a legacy profile (for compatibility reasons).
	NSOpenGLPixelFormatAttribute attrs[] = {
        NSOpenGLPFANoRecovery,
		NSOpenGLPFADoubleBuffer,
		NSOpenGLPFAClosestPolicy,
		NSOpenGLPFAAccelerated,
		NSOpenGLPFASampleBuffers, (samples > 0 ? 1 : 0),
		NSOpenGLPFASamples, samples,
		NSOpenGLPFAStencilSize, 8,
		NSOpenGLPFADepthSize, 24,
		NSOpenGLPFAAlphaSize, 8,
		NSOpenGLPFAColorSize, 24,
		0
    };
	
	NSOpenGLPixelFormat *pixelFormat = [[[NSOpenGLPixelFormat alloc] initWithAttributes:attrs] autorelease];
	
	if (pixelFormat == nil)
	{
		NSLog(@"Failed to create pixel format!", nil);
		return nil;
	}
	
	NSOpenGLContext *glContext = [[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:nil];
	
	if (glContext == nil)
	{
		NSLog(@"Failed to create OpenGL context!", nil);
		return nil;
	}
	
	[self setPixelFormat:pixelFormat];
	
	[self setOpenGLContext:glContext];
	
	[glContext setView:self];
	
	[glContext makeCurrentContext];
	
	if (vsync)
	{
		[glContext setValues:(const GLint*)1 forParameter:NSOpenGLCPSwapInterval];
	} else {
		[glContext setValues:(const GLint*)0 forParameter:NSOpenGLCPSwapInterval];
	}
	
	return self;
}

- (BOOL) rebuildContext
{
	return [self rebuildContextWithFormat:[self pixelFormat]];
}

- (BOOL) rebuildContextWithFormat:(NSOpenGLPixelFormat *)format
{
	NSOpenGLContext *ctx = [self openGLContext];
	
	[ctx clearDrawable];
	[ctx initWithFormat:format shareContext:nil];
	
	if (ctx == nil)
	{
		NSLog(@"Failed to create OpenGL context!", nil);
		return false;
	}
	
	[self setOpenGLContext:ctx];
	[ctx setView:self];
	[ctx makeCurrentContext];
	return true;
}

- (CGLContextObj)getCGLContextObj
{
	NSOpenGLContext *ctx = [self openGLContext];
	return (CGLContextObj)[ctx CGLContextObj];
}

- (CGLPixelFormatObj*)getCGLPixelFormatObj
{
	NSOpenGLPixelFormat *fmt = [self pixelFormat];
	return (CGLPixelFormatObj*)[fmt	CGLPixelFormatObj];
}

- (void) registerResizeCallback:(ResizeCallback)callback
{
	mResizeCallback = callback;
}

// Various events can be intercepted by our view, thus not reaching our window.
// Intercept these events, and pass them to the window as needed. - Geenz

- (void) mouseDragged:(NSEvent *)theEvent
{
	[super mouseDragged:theEvent];
}

- (void) scrollWheel:(NSEvent *)theEvent
{
	[super scrollWheel:theEvent];
}

- (void) mouseDown:(NSEvent *)theEvent
{
	[super mouseDown:theEvent];
}

- (void) mouseUp:(NSEvent *)theEvent
{
	[super mouseUp:theEvent];
}

- (void) rightMouseDown:(NSEvent *)theEvent
{
	[super rightMouseDown:theEvent];
}

- (void) rightMouseUp:(NSEvent *)theEvent
{
	[super rightMouseUp:theEvent];
}

- (void) keyUp:(NSEvent *)theEvent
{
	[super keyUp:theEvent];
}

- (void) keyDown:(NSEvent *)theEvent
{
	[super keyDown:theEvent];
}

- (void) mouseMoved:(NSEvent *)theEvent
{
	[super mouseMoved:theEvent];
}

- (void) flagsChanged:(NSEvent *)theEvent
{
	[super flagsChanged:theEvent];
}

@end

// We use a custom NSWindow for our event handling.
// Why not an NSWindowController you may ask?
// Answer: this is easier.

@implementation LLNSWindow

- (id) init
{
	return self;
}

- (void) keyDown:(NSEvent *)theEvent {
	if (mKeyDownCallback != nil && mUnicodeCallback != nil)
	{
		mKeyDownCallback([theEvent keyCode], [theEvent modifierFlags]);
		
		NSString *chars = [theEvent charactersIgnoringModifiers];
		for (uint i = 0; i < [chars length]; i++)
		{
			mUnicodeCallback([chars characterAtIndex:i], [theEvent modifierFlags]);
		}
		
		// The viewer expects return to be submitted separately as a unicode character.
		if ([theEvent keyCode] == 3 || [theEvent keyCode] == 13)
		{
			mUnicodeCallback([theEvent keyCode], [theEvent modifierFlags]);
		}
	}
}

- (void) keyUp:(NSEvent *)theEvent {
	if (mKeyUpCallback != nil)
	{
		mKeyUpCallback([theEvent keyCode], [theEvent modifierFlags]);
	}
}

- (void)flagsChanged:(NSEvent *)theEvent {
	mModifiers = [theEvent modifierFlags];
}

- (void) mouseDown:(NSEvent *)theEvent
{
	if (mMouseDoubleClickCallback != nil && mMouseDownCallback != nil)
	{
		if ([theEvent clickCount] == 2)
		{
			mMouseDoubleClickCallback(mMousePos, [theEvent modifierFlags]);
		} else if ([theEvent clickCount] == 1) {
			mMouseDownCallback(mMousePos, [theEvent modifierFlags]);
		}
	}
}

- (void) mouseUp:(NSEvent *)theEvent
{
	if (mMouseUpCallback != nil)
	{
		mMouseUpCallback(mMousePos, [theEvent modifierFlags]);
	}
}

- (void) rightMouseDown:(NSEvent *)theEvent
{
	if (mRightMouseDownCallback != nil)
	{
		mRightMouseDownCallback(mMousePos, [theEvent modifierFlags]);
	}
}

- (void) rightMouseUp:(NSEvent *)theEvent
{
	if (mRightMouseUpCallback != nil)
	{
		mRightMouseUpCallback(mMousePos, [theEvent modifierFlags]);
	}
}

- (void)mouseMoved:(NSEvent *)theEvent {
	if (mDeltaUpdateCallback != nil && mMouseMovedCallback != nil)
	{
		float mouseDeltas[2] = {
			[theEvent deltaX],
			[theEvent deltaZ]
		};
		
		mDeltaUpdateCallback(mouseDeltas, 0);
		
		NSPoint mPoint = [theEvent locationInWindow];
		mMousePos[0] = mPoint.x;
		mMousePos[1] = mPoint.y;
		if (mMouseMovedCallback != nil)
		{
			mMouseMovedCallback(mMousePos, 0);
		}
	}
}

// NSWindow doesn't trigger mouseMoved when the mouse is being clicked and dragged.
// Use mouseDragged for situations like this to trigger our movement callback instead.

- (void) mouseDragged:(NSEvent *)theEvent
{
	if (mDeltaUpdateCallback != nil && mMouseMovedCallback != nil)
	{
		float mouseDeltas[2] = {
			[theEvent deltaX],
			[theEvent deltaZ]
		};
		
		mDeltaUpdateCallback(mouseDeltas, 0);
		
		NSPoint mPoint = [theEvent locationInWindow];
		mMousePos[0] = mPoint.x;
		mMousePos[1] = mPoint.y;
		mMouseMovedCallback(mMousePos, 0);
	}
}

- (void) scrollWheel:(NSEvent *)theEvent
{
	if (mScrollWhellCallback != nil)
	{
		mScrollWhellCallback(-[theEvent deltaY]);
	}
}

- (void) mouseExited:(NSEvent *)theEvent
{
	if (mMouseExitCallback != nil)
	{
		mMouseExitCallback();
	}
}

- (void) registerKeyDownCallback:(KeyCallback)callback
{
	mKeyDownCallback = callback;
}

- (void) registerKeyUpCallback:(KeyCallback)callback
{
	mKeyUpCallback = callback;
}

- (void) registerUnicodeCallback:(UnicodeCallback)callback
{
	mUnicodeCallback = callback;
}

- (void) registerModifierCallback:(ModifierCallback)callback
{
	mModifierCallback = callback;
}

- (void) registerMouseDownCallback:(MouseCallback)callback
{
	mMouseDownCallback = callback;
}

- (void) registerMouseUpCallback:(MouseCallback)callback
{
	mMouseUpCallback = callback;
}

- (void) registerRightMouseDownCallback:(MouseCallback)callback
{
	mRightMouseDownCallback = callback;
}

- (void) registerRightMouseUpCallback:(MouseCallback)callback
{
	mRightMouseUpCallback = callback;
}

- (void) registerDoubleClickCallback:(MouseCallback)callback
{
	mMouseDoubleClickCallback = callback;
}

- (void) registerMouseMovedCallback:(MouseCallback)callback
{
	mMouseMovedCallback = callback;
}

- (void) registerScrollCallback:(ScrollWheelCallback)callback
{
	mScrollWhellCallback = callback;
}

- (void) registerMouseExitCallback:(VoidCallback)callback
{
	mMouseExitCallback = callback;
}

- (void) registerDeltaUpdateCallback:(MouseCallback)callback
{
	mDeltaUpdateCallback = callback;
}

@end

void setLLNSWindowRef(LLNSWindow* window)
{
	winRef = window;
}

void setLLOpenGLViewRef(LLOpenGLView* view)
{
	glviewRef = view;
}