forked from PixarAnimationStudios/OpenUSD
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathglPlatformDebugWindowDarwin.mm
More file actions
255 lines (197 loc) · 6.28 KB
/
glPlatformDebugWindowDarwin.mm
File metadata and controls
255 lines (197 loc) · 6.28 KB
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
//
// Copyright 2016 Pixar
//
// Licensed under the terms set forth in the LICENSE.txt file available at
// https://openusd.org/license.
//
#include "pxr/pxr.h"
#include "pxr/imaging/garch/glDebugWindow.h"
#include "pxr/imaging/garch/glPlatformDebugWindowDarwin.h"
#if defined(ARCH_OS_OSX)
#import <Cocoa/Cocoa.h>
#import <OpenGL/OpenGL.h>
#import <OpenGL/gl.h>
#endif
PXR_NAMESPACE_USING_DIRECTIVE
#if defined(ARCH_OS_OSX)
static int
Garch_GetModifierKeys(NSUInteger flags)
{
int keys = 0;
// The 10.12 SDK has new symbols.
#if MAC_OS_X_VERSION_MAX_ALLOWED < 101200
#define NSEventModifierFlagShift NSShiftKeyMask
#define NSEventModifierFlagControl NSControlKeyMask
#define NSEventModifierFlagOption NSAlternateKeyMask
#define NSEventModifierFlagCommand NSCommandKeyMask
#endif
if (flags & NSEventModifierFlagShift) keys |= GarchGLDebugWindow::Shift;
if (flags & NSEventModifierFlagControl) keys |= GarchGLDebugWindow::Ctrl;
if (flags & NSEventModifierFlagOption) keys |= GarchGLDebugWindow::Alt;
if (flags & NSEventModifierFlagCommand) keys |= GarchGLDebugWindow::Alt;
return keys;
}
@class Garch_GLPlatformView;
@interface Garch_GLPlatformView : NSOpenGLView <NSWindowDelegate>
{
GarchGLDebugWindow *_callback;
NSOpenGLContext *_ctx;
}
@end
@implementation Garch_GLPlatformView
-(id)initGL:(NSRect)frame callback:(GarchGLDebugWindow*)cb
{
_callback = cb;
int attribs[] = {
NSOpenGLPFAAccelerated,
NSOpenGLPFADoubleBuffer,
NSOpenGLPFADepthSize, 24,
NSOpenGLPFAAlphaSize, 8,
NSOpenGLPFAColorSize, 32,
NSOpenGLPFANoRecovery,
NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
0
};
NSOpenGLPixelFormat *pf
= [[NSOpenGLPixelFormat alloc]
initWithAttributes:(NSOpenGLPixelFormatAttribute*)attribs];
self = [self initWithFrame:frame pixelFormat:pf];
_ctx = [[NSOpenGLContext alloc] initWithFormat:pf shareContext:nil];
[self setOpenGLContext:_ctx];
[_ctx makeCurrentContext];
_callback->OnInitializeGL();
[pf release];
return self;
}
-(BOOL)acceptsFirstResponder
{
return YES;
}
-(void)drawRect:(NSRect)theRect
{
[_ctx makeCurrentContext];
_callback->OnPaintGL();
[[self openGLContext] flushBuffer];
}
-(void)windowWillClose:(NSNotification*)notification
{
[[NSApplication sharedApplication] terminate:self];
}
-(void)windowDidResize:(NSNotification *)notification
{
NSRect r = [self frame];
_callback->OnResize(r.size.width, r.size.height);
}
-(void)mouseDown:(NSEvent*)event
{
NSPoint p = [event locationInWindow];
NSRect r = [self frame];
NSUInteger modflags = [event modifierFlags];
_callback->OnMousePress(GarchGLDebugWindow::MyButton1,
p.x, r.size.height - 1 - p.y,
Garch_GetModifierKeys(modflags));
[self setNeedsDisplay:YES];
}
-(void)mouseUp:(NSEvent*)event
{
NSPoint p = [event locationInWindow];
NSRect r = [self frame];
NSUInteger modflags = [event modifierFlags];
_callback->OnMouseRelease(GarchGLDebugWindow::MyButton1,
p.x, r.size.height - 1 - p.y,
Garch_GetModifierKeys(modflags));
[self setNeedsDisplay:YES];
}
-(void)mouseDragged:(NSEvent*)event
{
NSPoint p = [event locationInWindow];
NSRect r = [self frame];
NSUInteger modflags = [event modifierFlags];
_callback->OnMouseMove(p.x, r.size.height - 1 - p.y,
Garch_GetModifierKeys(modflags));
[self setNeedsDisplay:YES];
}
- (void)keyDown:(NSEvent *)event
{
int keyCode = [event keyCode];
int key = 0;
// XXX shoud call UCKeyTranslate() for non-us keyboard
const int keyMap[] = { 0x00, 'a', 0x0b, 'b', 0x08, 'c', 0x02, 'd',
0x0e, 'e', 0x03, 'f', 0x05, 'g', 0x04, 'h',
0x22, 'i', 0x26, 'j', 0x28, 'k', 0x25, 'l',
0x2e, 'm', 0x2d, 'n', 0x1f, 'o', 0x23, 'p',
0x0c, 'q', 0x0f, 'r', 0x01, 's', 0x11, 't',
0x20, 'u', 0x09, 'v', 0x0d, 'w', 0x07, 'x',
0x10, 'y', 0x06, 'z', 0x31, ' ', -1, -1};
for (int i = 0; keyMap[i] >=0; i += 2) {
if (keyMap[i] == keyCode) {
key = keyMap[i+1];
break;
}
}
if (key) {
_callback->OnKeyRelease(key);
}
[self setNeedsDisplay:YES];
}
@end
// ---------------------------------------------------------------------------
PXR_NAMESPACE_OPEN_SCOPE
Garch_GLPlatformDebugWindow::Garch_GLPlatformDebugWindow(GarchGLDebugWindow *w)
: _callback(w)
{
}
void
Garch_GLPlatformDebugWindow::Init(const char *title,
int width, int height, int nSamples)
{
[NSApplication sharedApplication];
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
id applicationName = [[NSProcessInfo processInfo] processName];
NSRect frame = NSMakeRect(0, 0, width, height);
NSRect viewBounds = NSMakeRect(0, 0, width, height);
Garch_GLPlatformView *view =
[[Garch_GLPlatformView alloc] initGL:viewBounds callback:_callback];
NSWindow *window = [[NSWindow alloc]
initWithContentRect:frame
styleMask:NSTitledWindowMask
|NSClosableWindowMask
|NSMiniaturizableWindowMask
|NSResizableWindowMask
backing:NSBackingStoreBuffered
defer:NO];
[window cascadeTopLeftFromPoint:NSMakePoint(20,20)];
[window setTitle: applicationName];
[window makeKeyAndOrderFront:nil];
[NSApp activateIgnoringOtherApps:YES];
[window setContentView:view];
[window setDelegate:view];
}
void
Garch_GLPlatformDebugWindow::Run()
{
[NSApp run];
}
void
Garch_GLPlatformDebugWindow::ExitApp()
{
[NSApp stop:nil];
}
#else // IPHONE Derivatives
PXR_NAMESPACE_OPEN_SCOPE
Garch_GLPlatformDebugWindow::Garch_GLPlatformDebugWindow(GarchGLDebugWindow *w)
{
}
void Garch_GLPlatformDebugWindow::Init(const char *title, int width, int height, int nSamples)
{
}
void
Garch_GLPlatformDebugWindow::Run()
{
}
void
Garch_GLPlatformDebugWindow::ExitApp()
{
}
#endif
PXR_NAMESPACE_CLOSE_SCOPE