osxTorqueView.mm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #import "platformOSX/platformOSX.h"
  23. #import "platformOSX/osxTorqueView.h"
  24. #import "game/gameInterface.h"
  25. #import "gui/guiCanvas.h"
  26. #pragma mark ---- OSXTorqueView Implementation ----
  27. @interface OSXTorqueView (PrivateMethods)
  28. - (void)windowFinishedLiveResize:(NSNotification *)notification;
  29. - (void)getModifierKey:(U32&)modifiers event:(NSEvent *)event;
  30. - (void)processMouseButton:(NSEvent *)event button:(KeyCodes)button action:(U8)action;
  31. - (void)processKeyEvent:(NSEvent *)event make:(BOOL)make;
  32. @end
  33. @implementation OSXTorqueView
  34. @synthesize contextInitialized = _contextInitialized;
  35. //-----------------------------------------------------------------------------
  36. // Custom initialization method for OSXTorqueView
  37. - (void)initialize
  38. {
  39. if (self)
  40. {
  41. // Make absolutely sure _openGLContext is nil
  42. _openGLContext = nil;
  43. NSTrackingAreaOptions trackingOptions = NSTrackingCursorUpdate |
  44. NSTrackingMouseMoved |
  45. NSTrackingMouseEnteredAndExited |
  46. NSTrackingInVisibleRect |
  47. NSTrackingActiveInActiveApp;
  48. _trackingArea = [[NSTrackingArea alloc] initWithRect:[self bounds] options:trackingOptions owner:self userInfo:nil];
  49. [self addTrackingArea:_trackingArea];
  50. inputManager = (osxInputManager *) Input::getManager();
  51. }
  52. }
  53. //-----------------------------------------------------------------------------
  54. // Default dealloc override
  55. - (void)dealloc
  56. {
  57. // End notifications
  58. [[NSNotificationCenter defaultCenter] removeObserver:self];
  59. // Drop the tracking rectangle for mouse events
  60. if (_trackingArea != nil)
  61. {
  62. [self removeTrackingArea:_trackingArea];
  63. [_trackingArea release];
  64. }
  65. // Custom memory cleanup
  66. if (_openGLContext != nil)
  67. {
  68. [_openGLContext release];
  69. _openGLContext = nil;
  70. }
  71. // "Parent" cleanup
  72. [super dealloc];
  73. }
  74. //-----------------------------------------------------------------------------
  75. // This view an always be a first responder
  76. - (BOOL)acceptsFirstResponder
  77. {
  78. return YES;
  79. }
  80. //-----------------------------------------------------------------------------
  81. // Called whent the parent finishes its live resizing
  82. - (void)windowFinishedLiveResize:(NSNotification *)notification
  83. {
  84. NSSize size = [[self window] frame].size;
  85. [[osxPlatState sharedPlatState] setWindowSize:(S32)size.width height:(S32)size.height];
  86. NSRect frame = NSMakeRect(0, 0, size.width, size.height);
  87. S32 barHeight = frame.size.height;
  88. frame = [NSWindow frameRectForContentRect:frame styleMask:NSTitledWindowMask];
  89. barHeight -= frame.size.height;
  90. NSRect viewFrame = NSMakeRect(0, barHeight, frame.size.width, frame.size.height);
  91. [self setFrame:viewFrame];
  92. [self updateContext];
  93. }
  94. #pragma mark ---- OSXTorqueView OpenGL Handling ----
  95. //-----------------------------------------------------------------------------
  96. // Allocates a new NSOpenGLContext with the specified pixel format and makes
  97. // it the current OpenGL context automatically
  98. - (void)createContextWithPixelFormat:(NSOpenGLPixelFormat *)pixelFormat
  99. {
  100. _openGLContext = [[[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:nil] retain];
  101. AssertFatal(_openGLContext, "We could not create a valid NSOpenGL rendering context.");
  102. [_openGLContext setView:self];
  103. [_openGLContext makeCurrentContext];
  104. _contextInitialized = YES;
  105. }
  106. //-----------------------------------------------------------------------------
  107. // Clears the current context, releases control from this view, and deallocates
  108. // the NSOpenGLContext
  109. - (void)clearContext
  110. {
  111. if (_openGLContext != nil)
  112. {
  113. [NSOpenGLContext clearCurrentContext];
  114. [_openGLContext clearDrawable];
  115. [_openGLContext release];
  116. _openGLContext = nil;
  117. _contextInitialized = NO;
  118. }
  119. }
  120. //-----------------------------------------------------------------------------
  121. // Perform an update on the NSOpenGLContext, which will match the surface
  122. // size to the view's frame
  123. - (void)updateContext
  124. {
  125. if (_openGLContext != nil)
  126. [_openGLContext update];
  127. }
  128. //-----------------------------------------------------------------------------
  129. // Perform a swap buffer if the NSOpenGLContext is initialized
  130. - (void)flushBuffer
  131. {
  132. if (_openGLContext != nil)
  133. [_openGLContext flushBuffer];
  134. }
  135. //-----------------------------------------------------------------------------
  136. - (int)getVerticalSync
  137. {
  138. if (_openGLContext != nil)
  139. {
  140. GLint swapInterval = 0;
  141. [_openGLContext getValues:&swapInterval forParameter:NSOpenGLCPSwapInterval];
  142. return swapInterval;
  143. }
  144. else
  145. {
  146. return 0;
  147. }
  148. }
  149. //-----------------------------------------------------------------------------
  150. - (void)setVerticalSync:(bool)sync
  151. {
  152. if (_openGLContext != nil)
  153. {
  154. GLint swapInterval = sync ? 1 : 0;
  155. [_openGLContext setValues:&swapInterval forParameter:NSOpenGLCPSwapInterval];
  156. }
  157. }
  158. #pragma mark ---- OSXTorqueView Input Handling ----
  159. //-----------------------------------------------------------------------------
  160. // Fills out the modifiers based on key presses such as shift, alt, etc
  161. - (void)getModifierKey:(U32&)modifiers event:(NSEvent *)event;
  162. {
  163. /*
  164. NSAlphaShiftKeyMask = 1 << 16,
  165. NSShiftKeyMask = 1 << 17,
  166. NSControlKeyMask = 1 << 18,
  167. NSAlternateKeyMask = 1 << 19,
  168. NSCommandKeyMask = 1 << 20,
  169. NSNumericPadKeyMask = 1 << 21,
  170. NSHelpKeyMask = 1 << 22,
  171. NSFunctionKeyMask = 1 << 23,
  172. NSDeviceIndependentModifierFlagsMask = 0xffff0000U
  173. */
  174. U32 keyMods = (U32)[event modifierFlags];
  175. if (keyMods & NSShiftKeyMask)
  176. modifiers |= SI_SHIFT;
  177. if (keyMods & NSCommandKeyMask)
  178. modifiers |= SI_ALT;
  179. if (keyMods & NSAlternateKeyMask)
  180. modifiers |= SI_MAC_OPT;
  181. if (keyMods & NSControlKeyMask)
  182. modifiers |= SI_CTRL;
  183. }
  184. //-----------------------------------------------------------------------------
  185. // Processes mouse up and down events, posts to the event system
  186. - (void)processMouseButton:(NSEvent *)event button:(KeyCodes)button action:(U8)action
  187. {
  188. // Get the click location
  189. NSPoint clickLocation = [self convertPoint:[event locationInWindow] fromView:nil];
  190. NSRect bounds = [self bounds];
  191. clickLocation.y = bounds.size.height - clickLocation.y;
  192. // Move the cursor
  193. Canvas->setCursorPos(Point2I((S32) clickLocation.x, (S32) clickLocation.y));
  194. // Grab any modifiers
  195. U32 modifiers = 0;
  196. [self getModifierKey:modifiers event:event];
  197. // Build the input event
  198. InputEvent torqueEvent;
  199. torqueEvent.deviceType = MouseDeviceType;
  200. torqueEvent.deviceInst = 0;
  201. torqueEvent.objType = SI_BUTTON;
  202. torqueEvent.objInst = button;
  203. torqueEvent.modifier = modifiers;
  204. torqueEvent.ascii = 0;
  205. torqueEvent.action = action;
  206. if (action == SI_BREAK)
  207. torqueEvent.fValues[0] = 0.0;
  208. else
  209. torqueEvent.fValues[0] = 1.0;
  210. // Post the input event
  211. Game->postEvent(torqueEvent);
  212. }
  213. //-----------------------------------------------------------------------------
  214. // Processes keyboard up and down events, posts to the event system
  215. - (void)processKeyEvent:(NSEvent *)event make:(BOOL)make
  216. {
  217. // If input and keyboard are enabled
  218. if (!Input::isEnabled() && !Input::isKeyboardEnabled())
  219. return;
  220. unichar chars = [[event charactersIgnoringModifiers] characterAtIndex:0];
  221. // Get the key code for the event
  222. U32 keyCode = [event keyCode];
  223. U16 objInst = TranslateOSKeyCode(keyCode);
  224. // Grab any modifiers
  225. U32 modifiers = 0;
  226. [self getModifierKey:modifiers event:event];
  227. // Build the input event
  228. InputEvent torqueEvent;
  229. F32 fValue = 1.0f;
  230. U8 action = SI_MAKE;
  231. if (!make)
  232. {
  233. action = SI_BREAK;
  234. fValue = 0.0f;
  235. }
  236. else if(make && [event isARepeat])
  237. {
  238. action = SI_REPEAT;
  239. }
  240. torqueEvent.deviceType = KeyboardDeviceType;
  241. torqueEvent.deviceInst = 0;
  242. torqueEvent.objType = SI_KEY;
  243. torqueEvent.objInst = objInst;
  244. torqueEvent.modifier = modifiers;
  245. torqueEvent.ascii = 0;
  246. torqueEvent.action = action;
  247. torqueEvent.fValues[0] = fValue;
  248. torqueEvent.ascii = chars;
  249. // Post the input event
  250. Game->postEvent(torqueEvent);
  251. }
  252. //-----------------------------------------------------------------------------
  253. // Default mouseDown override
  254. - (void)mouseDown:(NSEvent *)event
  255. {
  256. if (!Input::isEnabled() && !Input::isMouseEnabled())
  257. return;
  258. [self processMouseButton:event button:KEY_BUTTON0 action:SI_MAKE];
  259. }
  260. //-----------------------------------------------------------------------------
  261. // Default rightMouseDown override
  262. - (void)rightMouseDown:(NSEvent *)event
  263. {
  264. if (!Input::isEnabled() && !Input::isMouseEnabled())
  265. return;
  266. [self processMouseButton:event button:KEY_BUTTON1 action:SI_MAKE];
  267. }
  268. //-----------------------------------------------------------------------------
  269. // Default otherMouseDown override
  270. - (void)otherMouseDown:(NSEvent *)event
  271. {
  272. if (!Input::isEnabled() && !Input::isMouseEnabled())
  273. return;
  274. [self processMouseButton:event button:KEY_BUTTON2 action:SI_MAKE];
  275. }
  276. //-----------------------------------------------------------------------------
  277. // Default mouseUp override
  278. - (void)mouseUp:(NSEvent *)event
  279. {
  280. if (!Input::isEnabled() && !Input::isMouseEnabled())
  281. return;
  282. [self processMouseButton:event button:KEY_BUTTON0 action:SI_BREAK];
  283. }
  284. //-----------------------------------------------------------------------------
  285. // Default rightMouseUp override
  286. - (void)rightMouseUp:(NSEvent *)event
  287. {
  288. if (!Input::isEnabled() && !Input::isMouseEnabled())
  289. return;
  290. [self processMouseButton:event button:KEY_BUTTON1 action:SI_BREAK];
  291. }
  292. //-----------------------------------------------------------------------------
  293. // Default otherMouseUp override
  294. - (void)otherMouseUp:(NSEvent *)event
  295. {
  296. if (!Input::isEnabled() && !Input::isMouseEnabled())
  297. return;
  298. [self processMouseButton:event button:KEY_BUTTON2 action:SI_BREAK];
  299. }
  300. //-----------------------------------------------------------------------------
  301. - (void)mouseEntered:(NSEvent *)event
  302. {
  303. if (!Canvas->getUseNativeCursor())
  304. {
  305. [NSCursor hide];
  306. }
  307. }
  308. -(void)mouseExited:(NSEvent *)event
  309. {
  310. [NSCursor unhide];
  311. }
  312. // Default otherMouseDown override
  313. - (void)mouseMoved:(NSEvent *)event
  314. {
  315. if (!Input::isEnabled() && !Input::isMouseEnabled())
  316. return;
  317. // Get the mouse location
  318. NSPoint location = [self convertPoint:[event locationInWindow] fromView:nil];
  319. // NSViews increase the Y the higher the cursor
  320. // Torque needs that to be inverted
  321. NSRect bounds = [self bounds];
  322. location.y = bounds.size.height - location.y;
  323. // Grab any modifiers
  324. U32 modifiers = 0;
  325. [self getModifierKey:modifiers event:event];
  326. // Move the cursor
  327. Canvas->setCursorPos(Point2I((S32) location.x, (S32) location.y));
  328. // Build the mouse event
  329. MouseMoveEvent TorqueEvent;
  330. TorqueEvent.xPos = (S32) location.x;
  331. TorqueEvent.yPos = (S32) location.y;
  332. TorqueEvent.modifier = modifiers;
  333. // Post the event
  334. Game->postEvent(TorqueEvent);
  335. }
  336. //-----------------------------------------------------------------------------
  337. // Default mouseDragged override
  338. - (void)mouseDragged:(NSEvent *)event
  339. {
  340. if (!Input::isEnabled() && !Input::isMouseEnabled())
  341. return;
  342. [self mouseMoved:event];
  343. }
  344. //-----------------------------------------------------------------------------
  345. // Default rightMouseDragged override
  346. - (void)rightMouseDragged:(NSEvent *)event
  347. {
  348. if (!Input::isEnabled() && !Input::isMouseEnabled())
  349. return;
  350. [self mouseMoved:event];
  351. }
  352. //-----------------------------------------------------------------------------
  353. // Default otherMouseDragged override
  354. - (void)otherMouseDragged:(NSEvent *)event
  355. {
  356. if (!Input::isEnabled() && !Input::isMouseEnabled())
  357. return;
  358. [self mouseMoved:event];
  359. }
  360. //-----------------------------------------------------------------------------
  361. // Default scrollWheel override
  362. - (void)scrollWheel:(NSEvent *)event
  363. {
  364. if (!Input::isEnabled() && !Input::isMouseEnabled())
  365. return;
  366. F32 deltaY = [event deltaY];
  367. if (deltaY == 0)
  368. return;
  369. // Grab any modifiers
  370. U32 modifiers = 0;
  371. [self getModifierKey:modifiers event:event];
  372. InputEvent torqueEvent;
  373. torqueEvent.deviceType = MouseDeviceType;
  374. torqueEvent.deviceInst = 0;
  375. torqueEvent.objType = SI_ZAXIS;
  376. torqueEvent.objInst = 0;
  377. torqueEvent.modifier = modifiers;
  378. torqueEvent.ascii = 0;
  379. torqueEvent.action = SI_MOVE;
  380. torqueEvent.fValues[0] = deltaY;
  381. Game->postEvent(torqueEvent);
  382. }
  383. //-----------------------------------------------------------------------------
  384. // Default keyDown override
  385. - (void)keyDown:(NSEvent *)event
  386. {
  387. // If input and keyboard are enabled
  388. if (!Input::isEnabled() && !Input::isKeyboardEnabled())
  389. return;
  390. [self processKeyEvent:event make:YES];
  391. }
  392. //-----------------------------------------------------------------------------
  393. // Default keyUp override
  394. - (void)keyUp:(NSEvent *)event
  395. {
  396. // If input and keyboard are enabled
  397. if (!Input::isEnabled() && !Input::isKeyboardEnabled())
  398. return;
  399. [self processKeyEvent:event make:NO];
  400. }
  401. @end