-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathOcctAisHello.objc.mm
More file actions
293 lines (238 loc) · 8.87 KB
/
OcctAisHello.objc.mm
File metadata and controls
293 lines (238 loc) · 8.87 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
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
#import <Cocoa/Cocoa.h>
#include <AIS_ViewController.hxx>
#include <Cocoa_LocalPool.hxx>
#include <Cocoa_Window.hxx>
#include <Message.hxx>
//! Main Cocoa application responder - minimal implementation for offscreen viewer.
@interface OcctTestNSResponder : NSObject <NSApplicationDelegate>
{
}
//! Default constructor.
- (id ) init;
//! Access singleton.
+ (OcctTestNSResponder* ) sharedInstance;
//! Dummy method for thread-safety Cocoa initialization.
+ (void ) doDummyThread: (id )theParam;
@end
//! Custom Cocoa view to handle events
@interface OcctAisCocoaEventManagerView : NSView
@end
//! Custom Cocoa window delegate to handle window events
@interface OcctAisCocoaWindowController : NSObject <NSWindowDelegate>
@end
@implementation OcctTestNSResponder
- (id )init
{
self = [super init];
return self;
}
// Singletone implementation
+ (OcctTestNSResponder* ) sharedInstance
{
static OcctTestNSResponder* TheAppResponder = [[super allocWithZone: nullptr] init];
return TheAppResponder;
}
+ (id ) allocWithZone: (NSZone* )theZone { return [[self sharedInstance] retain]; }
- (id ) copyWithZone: (NSZone* )theZone { return self; }
- (id ) retain { return self; }
- (NSUInteger ) retainCount { return NSUIntegerMax; }
- (oneway void ) release {}
- (id ) autorelease { return self; }
- (BOOL ) application: (NSApplication* )theApplication openFile: (NSString* )theFilename { return YES; }
- (void ) applicationWillFinishLaunching: (NSNotification* )theNotif { [self createMenu]; }
- (void ) applicationDidFinishLaunching: (NSNotification* )theNotif {}
- (void ) applicationWillTerminate: (NSNotification* )theNotif {}
+ (void ) doDummyThread: (id )theParam {}
- (void ) createMenu
{
NSMenu* aMenubar = [[NSMenu alloc] init];
NSMenuItem* aMenuBarItem = [[NSMenuItem alloc] init];
[aMenubar addItem: aMenuBarItem];
[NSApp setMainMenu: aMenubar];
NSMenu* aMenu = [[NSMenu alloc] init];
NSString* aQuitTitle = @"Quit";
NSMenuItem* aQuitMenuItem = [[NSMenuItem alloc] initWithTitle: aQuitTitle action: @selector(terminate:) keyEquivalent: @"q"];
[aMenu addItem: aQuitMenuItem];
[aMenuBarItem setSubmenu: aMenu];
}
@end
@implementation OcctAisCocoaWindowController
- (void )windowWillClose: (NSNotification* )theNotif
{
(void )theNotif;
[[NSApplication sharedApplication] terminate: nil];
}
- (void )windowDidBecomeKey: (NSNotification* )theNotif
{
//NSWindow* aWindow = [theNotif object];
//ActivateView(aWindow);
}
@end
//! Retrieve cursor position
static Graphic3d_Vec2i getMouseCoords(NSView* theView, NSEvent* theEvent)
{
NSPoint aMouseLoc = [theView convertPoint: [theEvent locationInWindow] fromView: nil];
NSRect aBounds = [theView bounds];
return Graphic3d_Vec2i(int(aMouseLoc.x), int(aBounds.size.height - aMouseLoc.y));
}
//! Convert key flags from mouse event.
static Aspect_VKeyFlags getMouseKeyFlags (NSEvent* theEvent)
{
Aspect_VKeyFlags aFlags = Aspect_VKeyFlags_NONE;
if (([theEvent modifierFlags] & NSEventModifierFlagShift) != 0)
aFlags |= Aspect_VKeyFlags_SHIFT;
if (([theEvent modifierFlags] & NSEventModifierFlagControl) != 0)
aFlags |= Aspect_VKeyFlags_CTRL;
if (([theEvent modifierFlags] & NSEventModifierFlagOption) != 0)
aFlags |= Aspect_VKeyFlags_ALT;
return aFlags;
}
@implementation OcctAisCocoaEventManagerView
AIS_ViewController* myViewCtrl = nullptr;
- (void )setViewController: (AIS_ViewController* )theCtrl
{
myViewCtrl = theCtrl;
}
- (void )updateTrackingAreas
{
// to track mouse movements for dynamic highlight
NSTrackingAreaOptions aFlags = (NSTrackingActiveAlways | NSTrackingInVisibleRect | NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved);
NSTrackingArea* anArea = [[NSTrackingArea alloc] initWithRect: [self bounds] options: aFlags owner: self userInfo: nil];
[self addTrackingArea: anArea];
}
- (void )setFrameSize: (NSSize )theNewSize
{
[super setFrameSize: theNewSize];
myViewCtrl->ProcessConfigure(true);
}
- (void )drawRect: (NSRect )theDirtyRect
{
(void )theDirtyRect;
if (myViewCtrl != nullptr)
myViewCtrl->ProcessExpose();
}
- (void )mouseMoved: (NSEvent* )theEvent
{
const Graphic3d_Vec2i aPos = getMouseCoords(self, theEvent);
const Aspect_VKeyFlags aFlags = getMouseKeyFlags(theEvent);
const Aspect_VKeyMouse aButtons = myViewCtrl->PressedMouseButtons();
myViewCtrl->UpdateMousePosition(aPos, aButtons, aFlags, false);
myViewCtrl->ProcessInput();
}
- (BOOL )acceptsFirstResponder
{
return YES;
}
- (void )mouseDown: (NSEvent* )theEvent
{
const Graphic3d_Vec2i aPos = getMouseCoords(self, theEvent);
const Aspect_VKeyFlags aFlags = getMouseKeyFlags(theEvent);
myViewCtrl->PressMouseButton(aPos, Aspect_VKeyMouse_LeftButton, aFlags, false);
myViewCtrl->ProcessInput();
}
- (void )mouseUp: (NSEvent* )theEvent
{
const Graphic3d_Vec2i aPos = getMouseCoords(self, theEvent);
const Aspect_VKeyFlags aFlags = getMouseKeyFlags(theEvent);
myViewCtrl->ReleaseMouseButton(aPos, Aspect_VKeyMouse_LeftButton, aFlags, false);
myViewCtrl->ProcessInput();
}
- (void )mouseDragged: (NSEvent* )theEvent
{
const Graphic3d_Vec2i aPos = getMouseCoords(self, theEvent);
const Aspect_VKeyFlags aFlags = getMouseKeyFlags(theEvent);
const Aspect_VKeyMouse aButtons = myViewCtrl->PressedMouseButtons();
myViewCtrl->UpdateMousePosition(aPos, aButtons, aFlags, false);
myViewCtrl->ProcessInput();
}
- (void )rightMouseDown: (NSEvent* )theEvent
{
const Graphic3d_Vec2i aPos = getMouseCoords(self, theEvent);
const Aspect_VKeyFlags aFlags = getMouseKeyFlags(theEvent);
myViewCtrl->PressMouseButton(aPos, Aspect_VKeyMouse_RightButton, aFlags, false);
myViewCtrl->ProcessInput();
}
- (void )rightMouseUp: (NSEvent* )theEvent
{
const Graphic3d_Vec2i aPos = getMouseCoords(self, theEvent);
const Aspect_VKeyFlags aFlags = getMouseKeyFlags(theEvent);
myViewCtrl->ReleaseMouseButton(aPos, Aspect_VKeyMouse_RightButton, aFlags, false);
myViewCtrl->ProcessInput();
}
- (void )rightMouseDragged: (NSEvent* )theEvent
{
const Graphic3d_Vec2i aPos = getMouseCoords(self, theEvent);
const Aspect_VKeyFlags aFlags = getMouseKeyFlags(theEvent);
const Aspect_VKeyMouse aButtons = myViewCtrl->PressedMouseButtons();
myViewCtrl->UpdateMousePosition (aPos, aButtons, aFlags, false);
myViewCtrl->ProcessInput();
}
- (void )scrollWheel: (NSEvent* )theEvent
{
const Graphic3d_Vec2i aPos = getMouseCoords(self, theEvent);
const Aspect_VKeyFlags aFlags = getMouseKeyFlags(theEvent);
const double aDelta = [theEvent deltaY];
if (Abs (aDelta) < 0.001) // a lot of values near zero can be generated by touchpad
return;
myViewCtrl->UpdateMouseScroll(Aspect_ScrollDelta(aPos, aDelta, aFlags));
myViewCtrl->ProcessInput();
}
- (void )keyDown: (NSEvent* )theEvent
{
unsigned int aKeyCode = [theEvent keyCode];
const Aspect_VKey aVKey = Cocoa_Window::VirtualKeyFromNative(aKeyCode);
if (aVKey != Aspect_VKey_UNKNOWN)
{
const double aTimeStamp = [theEvent timestamp];
myViewCtrl->KeyDown(aVKey, aTimeStamp);
myViewCtrl->ProcessInput();
}
}
- (void )keyUp: (NSEvent* )theEvent
{
unsigned int aKeyCode = [theEvent keyCode];
const Aspect_VKey aVKey = Cocoa_Window::VirtualKeyFromNative(aKeyCode);
if (aVKey != Aspect_VKey_UNKNOWN)
{
const double aTimeStamp = [theEvent timestamp];
myViewCtrl->KeyUp(aVKey, aTimeStamp);
myViewCtrl->ProcessInput();
}
}
@end
void occtNSAppCreate()
{
// create dummy NSThread to ensure Cocoa thread-safety
[NSThread detachNewThreadSelector: @selector(doDummyThread: ) toTarget: [OcctTestNSResponder class] withObject: nullptr];
NSApplication* anAppNs = [NSApplication sharedApplication];
OcctTestNSResponder* anAppResp = [OcctTestNSResponder sharedInstance];
[anAppNs setDelegate: anAppResp];
//[NSBundle loadNibNamed: @"MainMenu" owner: anAppResp];
// allow our application to steal input focus (when needed)
[anAppNs activateIgnoringOtherApps: YES];
}
void occtNSAppRun()
{
NSApplication* anAppNs = [NSApplication sharedApplication];
//[[NSApp mainWindow] makeKeyAndOrderFront: anAppNs];
[anAppNs run]; // Cocoa event loop
}
void occtNSAppSetEventView(const Handle(Cocoa_Window)& theWindow, AIS_ViewController* theCtrl)
{
if (theWindow.IsNull())
return;
NSWindow* aWin = [theWindow->HView() window];
NSRect aBounds = [[aWin contentView] bounds];
OcctAisCocoaEventManagerView* aView = [[OcctAisCocoaEventManagerView alloc] initWithFrame: aBounds];
[aView setViewController: theCtrl];
// replace content view in the window
theWindow->SetHView(aView);
// set delegate for window
OcctAisCocoaWindowController* aWindowController = [[[OcctAisCocoaWindowController alloc] init] autorelease];
[aWin setDelegate: aWindowController];
// make view as first responder in winow to capture all useful events
[aWin makeFirstResponder: aView];
[aWin setAcceptsMouseMovedEvents: YES];
// should be retained by parent NSWindow
[aView release];
}