osxTorqueView.mm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. - (void)setVerticalSync:(bool)sync
  137. {
  138. if (_openGLContext != nil)
  139. {
  140. GLint swapInterval = sync ? 1 : 0;
  141. [_openGLContext setValues:&swapInterval forParameter:NSOpenGLCPSwapInterval];
  142. }
  143. }
  144. #pragma mark ---- OSXTorqueView Input Handling ----
  145. //-----------------------------------------------------------------------------
  146. // Fills out the modifiers based on key presses such as shift, alt, etc
  147. - (void)getModifierKey:(U32&)modifiers event:(NSEvent *)event;
  148. {
  149. /*
  150. NSAlphaShiftKeyMask = 1 << 16,
  151. NSShiftKeyMask = 1 << 17,
  152. NSControlKeyMask = 1 << 18,
  153. NSAlternateKeyMask = 1 << 19,
  154. NSCommandKeyMask = 1 << 20,
  155. NSNumericPadKeyMask = 1 << 21,
  156. NSHelpKeyMask = 1 << 22,
  157. NSFunctionKeyMask = 1 << 23,
  158. NSDeviceIndependentModifierFlagsMask = 0xffff0000U
  159. */
  160. U32 keyMods = [event modifierFlags];
  161. if (keyMods & NSShiftKeyMask)
  162. modifiers |= SI_SHIFT;
  163. if (keyMods & NSCommandKeyMask)
  164. modifiers |= SI_ALT;
  165. if (keyMods & NSAlternateKeyMask)
  166. modifiers |= SI_MAC_OPT;
  167. if (keyMods & NSControlKeyMask)
  168. modifiers |= SI_CTRL;
  169. }
  170. //-----------------------------------------------------------------------------
  171. // Processes mouse up and down events, posts to the event system
  172. - (void)processMouseButton:(NSEvent *)event button:(KeyCodes)button action:(U8)action
  173. {
  174. // Get the click location
  175. NSPoint clickLocation = [self convertPoint:[event locationInWindow] fromView:nil];
  176. NSRect bounds = [self bounds];
  177. clickLocation.y = bounds.size.height - clickLocation.y;
  178. // Move the cursor
  179. Canvas->setCursorPos(Point2I((S32) clickLocation.x, (S32) clickLocation.y));
  180. // Grab any modifiers
  181. U32 modifiers = 0;
  182. [self getModifierKey:modifiers event:event];
  183. // Build the input event
  184. InputEvent torqueEvent;
  185. torqueEvent.deviceType = MouseDeviceType;
  186. torqueEvent.deviceInst = 0;
  187. torqueEvent.objType = SI_BUTTON;
  188. torqueEvent.objInst = button;
  189. torqueEvent.modifier = modifiers;
  190. torqueEvent.ascii = 0;
  191. torqueEvent.action = action;
  192. if (action == SI_BREAK)
  193. torqueEvent.fValues[0] = 0.0;
  194. else
  195. torqueEvent.fValues[0] = 1.0;
  196. // Post the input event
  197. Game->postEvent(torqueEvent);
  198. }
  199. //-----------------------------------------------------------------------------
  200. // Processes keyboard up and down events, posts to the event system
  201. - (void)processKeyEvent:(NSEvent *)event make:(BOOL)make
  202. {
  203. // If input and keyboard are enabled
  204. if (!Input::isEnabled() && !Input::isKeyboardEnabled())
  205. return;
  206. unichar chars = [[event charactersIgnoringModifiers] characterAtIndex:0];
  207. // Get the key code for the event
  208. U32 keyCode = [event keyCode];
  209. U16 objInst = TranslateOSKeyCode(keyCode);
  210. // Grab any modifiers
  211. U32 modifiers = 0;
  212. [self getModifierKey:modifiers event:event];
  213. // Build the input event
  214. InputEvent torqueEvent;
  215. F32 fValue = 1.0f;
  216. U8 action = SI_MAKE;
  217. if (!make)
  218. {
  219. action = SI_BREAK;
  220. fValue = 0.0f;
  221. }
  222. else if(make && [event isARepeat])
  223. {
  224. action = SI_REPEAT;
  225. }
  226. torqueEvent.deviceType = KeyboardDeviceType;
  227. torqueEvent.deviceInst = 0;
  228. torqueEvent.objType = SI_KEY;
  229. torqueEvent.objInst = objInst;
  230. torqueEvent.modifier = modifiers;
  231. torqueEvent.ascii = 0;
  232. torqueEvent.action = action;
  233. torqueEvent.fValues[0] = fValue;
  234. torqueEvent.ascii = chars;
  235. // Post the input event
  236. Game->postEvent(torqueEvent);
  237. }
  238. //-----------------------------------------------------------------------------
  239. // Default mouseDown override
  240. - (void)mouseDown:(NSEvent *)event
  241. {
  242. if (!Input::isEnabled() && !Input::isMouseEnabled())
  243. return;
  244. [self processMouseButton:event button:KEY_BUTTON0 action:SI_MAKE];
  245. }
  246. //-----------------------------------------------------------------------------
  247. // Default rightMouseDown override
  248. - (void)rightMouseDown:(NSEvent *)event
  249. {
  250. if (!Input::isEnabled() && !Input::isMouseEnabled())
  251. return;
  252. [self processMouseButton:event button:KEY_BUTTON1 action:SI_MAKE];
  253. }
  254. //-----------------------------------------------------------------------------
  255. // Default otherMouseDown override
  256. - (void)otherMouseDown:(NSEvent *)event
  257. {
  258. if (!Input::isEnabled() && !Input::isMouseEnabled())
  259. return;
  260. [self processMouseButton:event button:KEY_BUTTON2 action:SI_MAKE];
  261. }
  262. //-----------------------------------------------------------------------------
  263. // Default mouseUp override
  264. - (void)mouseUp:(NSEvent *)event
  265. {
  266. if (!Input::isEnabled() && !Input::isMouseEnabled())
  267. return;
  268. [self processMouseButton:event button:KEY_BUTTON0 action:SI_BREAK];
  269. }
  270. //-----------------------------------------------------------------------------
  271. // Default rightMouseUp override
  272. - (void)rightMouseUp:(NSEvent *)event
  273. {
  274. if (!Input::isEnabled() && !Input::isMouseEnabled())
  275. return;
  276. [self processMouseButton:event button:KEY_BUTTON1 action:SI_BREAK];
  277. }
  278. //-----------------------------------------------------------------------------
  279. // Default otherMouseUp override
  280. - (void)otherMouseUp:(NSEvent *)event
  281. {
  282. if (!Input::isEnabled() && !Input::isMouseEnabled())
  283. return;
  284. [self processMouseButton:event button:KEY_BUTTON2 action:SI_BREAK];
  285. }
  286. //-----------------------------------------------------------------------------
  287. // Default otherMouseDown override
  288. - (void)mouseMoved:(NSEvent *)event
  289. {
  290. if (!Input::isEnabled() && !Input::isMouseEnabled())
  291. return;
  292. // Get the mouse location
  293. NSPoint location = [self convertPoint:[event locationInWindow] fromView:nil];
  294. // NSViews increase the Y the higher the cursor
  295. // Torque needs that to be inverted
  296. NSRect bounds = [self bounds];
  297. location.y = bounds.size.height - location.y;
  298. // Grab any modifiers
  299. U32 modifiers = 0;
  300. [self getModifierKey:modifiers event:event];
  301. // Move the cursor
  302. Canvas->setCursorPos(Point2I((S32) location.x, (S32) location.y));
  303. // Build the mouse event
  304. MouseMoveEvent TorqueEvent;
  305. TorqueEvent.xPos = (S32) location.x;
  306. TorqueEvent.yPos = (S32) location.y;
  307. TorqueEvent.modifier = modifiers;
  308. // Post the event
  309. Game->postEvent(TorqueEvent);
  310. }
  311. //-----------------------------------------------------------------------------
  312. // Default mouseDragged override
  313. - (void)mouseDragged:(NSEvent *)event
  314. {
  315. if (!Input::isEnabled() && !Input::isMouseEnabled())
  316. return;
  317. [self mouseMoved:event];
  318. }
  319. //-----------------------------------------------------------------------------
  320. // Default rightMouseDragged override
  321. - (void)rightMouseDragged:(NSEvent *)event
  322. {
  323. if (!Input::isEnabled() && !Input::isMouseEnabled())
  324. return;
  325. [self mouseMoved:event];
  326. }
  327. //-----------------------------------------------------------------------------
  328. // Default otherMouseDragged override
  329. - (void)otherMouseDragged:(NSEvent *)event
  330. {
  331. if (!Input::isEnabled() && !Input::isMouseEnabled())
  332. return;
  333. [self mouseMoved:event];
  334. }
  335. //-----------------------------------------------------------------------------
  336. // Default scrollWheel override
  337. - (void)scrollWheel:(NSEvent *)event
  338. {
  339. if (!Input::isEnabled() && !Input::isMouseEnabled())
  340. return;
  341. F32 deltaY = [event deltaY];
  342. if (deltaY == 0)
  343. return;
  344. // Grab any modifiers
  345. U32 modifiers = 0;
  346. [self getModifierKey:modifiers event:event];
  347. InputEvent torqueEvent;
  348. torqueEvent.deviceType = MouseDeviceType;
  349. torqueEvent.deviceInst = 0;
  350. torqueEvent.objType = SI_ZAXIS;
  351. torqueEvent.objInst = 0;
  352. torqueEvent.modifier = modifiers;
  353. torqueEvent.ascii = 0;
  354. torqueEvent.action = SI_MOVE;
  355. torqueEvent.fValues[0] = deltaY;
  356. Game->postEvent(torqueEvent);
  357. }
  358. //-----------------------------------------------------------------------------
  359. // Default keyDown override
  360. - (void)keyDown:(NSEvent *)event
  361. {
  362. // If input and keyboard are enabled
  363. if (!Input::isEnabled() && !Input::isKeyboardEnabled())
  364. return;
  365. [self processKeyEvent:event make:YES];
  366. }
  367. //-----------------------------------------------------------------------------
  368. // Default keyUp override
  369. - (void)keyUp:(NSEvent *)event
  370. {
  371. // If input and keyboard are enabled
  372. if (!Input::isEnabled() && !Input::isKeyboardEnabled())
  373. return;
  374. [self processKeyEvent:event make:NO];
  375. }
  376. @end