2
0

PolycodeView.mm 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #import "PolycodeView.h"
  20. @implementation PolycodeView
  21. @synthesize viewReady;
  22. @synthesize mouseX;
  23. @synthesize mouseY;
  24. - (id)initWithFrame:(NSRect)frameRect pixelFormat:(NSOpenGLPixelFormat *)format
  25. {
  26. self = [super initWithFrame:frameRect pixelFormat:format];
  27. if(self) {
  28. glSizeX = 0;
  29. glSizeY = 0;
  30. viewReady = NO;
  31. currentCursor = NULL;
  32. contextLock = [[NSLock alloc] init];
  33. }
  34. return self;
  35. }
  36. - (void) awakeFromNib {
  37. contextLock = [[NSLock alloc] init];
  38. [[self window] setAcceptsMouseMovedEvents:YES];
  39. [[self window] makeFirstResponder:self];
  40. [self setAcceptsTouchEvents:YES];
  41. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowResized:) name:NSWindowDidResizeNotification object:[self window]];
  42. [self setAutoresizesSubviews: YES];
  43. }
  44. -(void) viewDidMoveToWindow
  45. {
  46. [super viewDidMoveToWindow];
  47. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidResignMain:) name:NSWindowDidResignKeyNotification object:[self window]];
  48. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidResignMain:) name:NSWindowDidResignMainNotification object:[self window]];
  49. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidBecomeMain:) name:NSWindowDidBecomeKeyNotification object:[self window]];
  50. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidBecomeMain:) name:NSWindowDidBecomeMainNotification object:[self window]];
  51. viewReady = YES;
  52. }
  53. - (void)windowDidResignMain:(NSNotification *)notification
  54. {
  55. if(core == NULL)
  56. return;
  57. core->lockMutex(core->eventMutex);
  58. CocoaEvent newEvent;
  59. newEvent.eventGroup = CocoaEvent::FOCUS_EVENT;
  60. newEvent.eventCode = Core::EVENT_LOST_FOCUS;
  61. core->cocoaEvents.push_back(newEvent);
  62. core->unlockMutex(core->eventMutex);
  63. }
  64. - (void)windowDidBecomeMain:(NSNotification *)notification
  65. {
  66. if(core == NULL)
  67. return;
  68. core->lockMutex(core->eventMutex);
  69. CocoaEvent newEvent;
  70. newEvent.eventGroup = CocoaEvent::FOCUS_EVENT;
  71. newEvent.eventCode = Core::EVENT_GAINED_FOCUS;
  72. core->cocoaEvents.push_back(newEvent);
  73. core->unlockMutex(core->eventMutex);
  74. }
  75. - (void) initKeymap {
  76. keymap[0x00] = KEY_a;
  77. keymap[0x01] = KEY_s;
  78. keymap[0x02] = KEY_d;
  79. keymap[0x03] = KEY_f;
  80. keymap[0x05] = KEY_g;
  81. keymap[0x04] = KEY_h;
  82. keymap[0x06] = KEY_z;
  83. keymap[0x07] = KEY_x;
  84. keymap[0x08] = KEY_c;
  85. keymap[0x09] = KEY_v;
  86. keymap[0x0B] = KEY_b;
  87. keymap[0x0C] = KEY_q;
  88. keymap[0x0D] = KEY_w;
  89. keymap[0x0E] = KEY_e;
  90. keymap[0x0F] = KEY_r;
  91. keymap[0x10] = KEY_y;
  92. keymap[0x11] = KEY_t;
  93. keymap[0x12] = KEY_1;
  94. keymap[0x13] = KEY_2;
  95. keymap[0x14] = KEY_3;
  96. keymap[0x15] = KEY_4;
  97. keymap[0x16] = KEY_6;
  98. keymap[0x17] = KEY_5;
  99. keymap[0x18] = KEY_EQUALS;
  100. keymap[0x19] = KEY_9;
  101. keymap[0x1A] = KEY_7;
  102. keymap[0x1B] = KEY_MINUS;
  103. keymap[0x1C] = KEY_8;
  104. keymap[0x1D] = KEY_0;
  105. keymap[0x1E] = KEY_RIGHTBRACKET;
  106. keymap[0x1F] = KEY_o;
  107. keymap[0x20] = KEY_u;
  108. keymap[0x21] = KEY_LEFTBRACKET;
  109. keymap[0x22] = KEY_i;
  110. keymap[0x23] = KEY_p;
  111. keymap[0x25] = KEY_l;
  112. keymap[0x26] = KEY_j;
  113. keymap[0x27] = KEY_QUOTE;
  114. keymap[0x28] = KEY_k;
  115. keymap[0x29] = KEY_SEMICOLON;
  116. keymap[0x2A] = KEY_BACKSLASH;
  117. keymap[0x2B] = KEY_COMMA;
  118. keymap[0x2C] = KEY_SLASH;
  119. keymap[0x2D] = KEY_n;
  120. keymap[0x2E] = KEY_m;
  121. keymap[0x2F] = KEY_PERIOD;
  122. // keymap[0x32] = KEY_GRAVE;
  123. keymap[0x41] = KEY_KP_PERIOD;
  124. keymap[0x43] = KEY_KP_MULTIPLY;
  125. keymap[0x45] = KEY_KP_PLUS;
  126. // keymap[0x47] = KEY_KeypadClear;
  127. keymap[0x4B] = KEY_KP_DIVIDE;
  128. keymap[0x4C] = KEY_KP_ENTER;
  129. keymap[0x4E] = KEY_KP_MINUS;
  130. keymap[0x51] = KEY_KP_EQUALS;
  131. keymap[0x52] = KEY_KP0;
  132. keymap[0x53] = KEY_KP1;
  133. keymap[0x54] = KEY_KP2;
  134. keymap[0x55] = KEY_KP3;
  135. keymap[0x56] = KEY_KP4;
  136. keymap[0x57] = KEY_KP5;
  137. keymap[0x58] = KEY_KP6;
  138. keymap[0x59] = KEY_KP7;
  139. keymap[0x5B] = KEY_KP8;
  140. keymap[0x5C] = KEY_KP9;
  141. keymap[0x24] = KEY_RETURN;
  142. keymap[0x30] = KEY_TAB;
  143. keymap[0x31] = KEY_SPACE;
  144. keymap[0x33] = KEY_BACKSPACE;
  145. keymap[0x35] = KEY_ESCAPE;
  146. keymap[0x37] = KEY_LSUPER;
  147. keymap[0x36] = KEY_RSUPER;
  148. keymap[0x38] = KEY_LSHIFT;
  149. keymap[0x39] = KEY_CAPSLOCK;
  150. keymap[0x3A] = KEY_LALT;
  151. keymap[0x3D] = KEY_RALT;
  152. keymap[0x3B] = KEY_LCTRL;
  153. keymap[0x3C] = KEY_RSHIFT;
  154. keymap[0x3E] = KEY_RCTRL;
  155. keymap[0x3F] = KEY_COMPOSE;
  156. // keymap[0x40] = KEY_F17;
  157. // keymap[0x48] = KEY_VolumeUp;
  158. // keymap[0x49] = KEY_VolumeDown;
  159. // keymap[0x4A] = KEY_Mute;
  160. // keymap[0x4F] = KEY_F18;
  161. // keymap[0x50] = KEY_F19;
  162. // keymap[0x5A] = KEY_F20;
  163. keymap[0x60] = KEY_F5;
  164. keymap[0x61] = KEY_F6;
  165. keymap[0x62] = KEY_F7;
  166. keymap[0x63] = KEY_F3;
  167. keymap[0x64] = KEY_F8;
  168. keymap[0x65] = KEY_F9;
  169. keymap[0x67] = KEY_F11;
  170. keymap[0x69] = KEY_F13;
  171. // keymap[0x6A] = KEY_F16;
  172. keymap[0x6B] = KEY_F14;
  173. keymap[0x6D] = KEY_F10;
  174. keymap[0x6F] = KEY_F12;
  175. keymap[0x71] = KEY_F15;
  176. keymap[0x72] = KEY_HELP;
  177. keymap[0x73] = KEY_HOME;
  178. keymap[0x74] = KEY_PAGEUP;
  179. keymap[0x75] = KEY_DELETE;
  180. keymap[0x76] = KEY_F4;
  181. keymap[0x77] = KEY_END;
  182. keymap[0x78] = KEY_F2;
  183. keymap[0x79] = KEY_PAGEDOWN;
  184. keymap[0x7A] = KEY_F1;
  185. keymap[0x7B] = KEY_LEFT;
  186. keymap[0x7C] = KEY_RIGHT;
  187. keymap[0x7D] = KEY_DOWN;
  188. keymap[0x7E] = KEY_UP;
  189. // keymap[0x0A] = KEY_ISO_Section;
  190. // keymap[0x5D] = KEY_JIS_Yen;
  191. // keymap[0x5E] = KEY_JIS_Underscore;
  192. // keymap[0x5F] = KEY_JIS_KeypadComma;
  193. // keymap[0x66] = KEY_JIS_Eisu;
  194. // keymap[0x68] = KEY_JIS_Kana;
  195. }
  196. -(void) setCurrentCursor: (NSCursor*) newCursor
  197. {
  198. currentCursor = newCursor;
  199. }
  200. - (void)resetCursorRects
  201. {
  202. if(currentCursor)
  203. [self addCursorRect:[self bounds] cursor:currentCursor];
  204. }
  205. - (void) setCore: (CocoaCore*) newCore {
  206. [self initKeymap];
  207. core = newCore;
  208. }
  209. - (BOOL) lockFocusIfCanDraw {
  210. [contextLock lock];
  211. BOOL retVal = [super lockFocusIfCanDraw];
  212. [contextLock unlock];
  213. return retVal;
  214. }
  215. - (void) update {
  216. [contextLock lock];
  217. [super update];
  218. [contextLock unlock];
  219. }
  220. - (void) lockContext {
  221. [contextLock lock];
  222. }
  223. - (void) unlockContext {
  224. [contextLock unlock];
  225. }
  226. - (void)prepareOpenGL {
  227. contextReady = YES;
  228. }
  229. - (BOOL) isContextReady {
  230. return contextReady;
  231. }
  232. - (void)setOpenGLContext:(NSOpenGLContext *)context {
  233. contextReady = NO;
  234. [super setOpenGLContext:context];
  235. }
  236. - (BOOL)acceptsFirstResponder
  237. {
  238. return YES;
  239. }
  240. // ---------------------------------
  241. - (BOOL)becomeFirstResponder
  242. {
  243. return YES;
  244. }
  245. // ---------------------------------
  246. - (BOOL)resignFirstResponder
  247. {
  248. return YES;
  249. }
  250. - (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
  251. {
  252. return YES;
  253. }
  254. - (BOOL)mouseDownCanMoveWindow
  255. {
  256. return NO;
  257. }
  258. - (void) drawRect:(NSRect)rect {
  259. if(glSizeX != (int)[self frame].size.width || glSizeY != (int)[self frame].size.height) {
  260. glSizeX = (int)[self frame].size.width;
  261. glSizeY = (int)[self frame].size.height;
  262. if(core != NULL) {
  263. core->resizeTo(glSizeX, glSizeY);
  264. }
  265. }
  266. }
  267. -(void) windowResized: (NSNotification *) notification {
  268. }
  269. -(void) dealloc {
  270. [[NSNotificationCenter defaultCenter] removeObserver:self];
  271. [super dealloc];
  272. }
  273. - (void) resizeWithOldSuperviewSize: (NSSize) oldSize {
  274. [super resizeWithOldSuperviewSize: oldSize];
  275. // if(core == NULL)
  276. // return;
  277. // core->setVideoMode(oldSize.width, oldSize.height, core->isFullscreen(), core->getAALevel());
  278. }
  279. // INPUT
  280. - (void) otherMouseDown:(NSEvent *) event
  281. {
  282. if(core == NULL)
  283. return;
  284. core->lockMutex(core->eventMutex);
  285. NSPoint mouseLoc = [self convertPoint:[event locationInWindow] fromView:self];
  286. CocoaEvent newEvent;
  287. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  288. newEvent.eventCode = InputEvent::EVENT_MOUSEDOWN;
  289. newEvent.mouseButton = CoreInput::MOUSE_BUTTON3;
  290. newEvent.mouseX = mouseLoc.x;
  291. newEvent.mouseY = core->getYRes() - mouseLoc.y;
  292. core->cocoaEvents.push_back(newEvent);
  293. core->unlockMutex(core->eventMutex);
  294. }
  295. - (void) otherMouseUp:(NSEvent *) event
  296. {
  297. if(core == NULL)
  298. return;
  299. core->lockMutex(core->eventMutex);
  300. NSPoint mouseLoc = [self convertPoint:[event locationInWindow] fromView:self];
  301. CocoaEvent newEvent;
  302. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  303. newEvent.eventCode = InputEvent::EVENT_MOUSEUP;
  304. newEvent.mouseButton = CoreInput::MOUSE_BUTTON3;
  305. newEvent.mouseX = mouseLoc.x;
  306. newEvent.mouseY = core->getYRes() - mouseLoc.y;
  307. core->cocoaEvents.push_back(newEvent);
  308. core->unlockMutex(core->eventMutex);
  309. }
  310. - (void) rightMouseDown:(NSEvent *) event
  311. {
  312. if(core == NULL)
  313. return;
  314. core->lockMutex(core->eventMutex);
  315. NSPoint mouseLoc = [self convertPoint:[event locationInWindow] fromView:self];
  316. CocoaEvent newEvent;
  317. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  318. newEvent.eventCode = InputEvent::EVENT_MOUSEDOWN;
  319. newEvent.mouseButton = CoreInput::MOUSE_BUTTON2;
  320. newEvent.mouseX = mouseLoc.x;
  321. newEvent.mouseY = core->getYRes() - mouseLoc.y;
  322. core->cocoaEvents.push_back(newEvent);
  323. core->unlockMutex(core->eventMutex);
  324. }
  325. - (void) rightMouseUp:(NSEvent *) event
  326. {
  327. if(core == NULL)
  328. return;
  329. core->lockMutex(core->eventMutex);
  330. NSPoint mouseLoc = [self convertPoint:[event locationInWindow] fromView:self];
  331. CocoaEvent newEvent;
  332. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  333. newEvent.eventCode = InputEvent::EVENT_MOUSEUP;
  334. newEvent.mouseButton = CoreInput::MOUSE_BUTTON2;
  335. newEvent.mouseX = mouseLoc.x;
  336. newEvent.mouseY = core->getYRes() - mouseLoc.y;
  337. core->cocoaEvents.push_back(newEvent);
  338. core->unlockMutex(core->eventMutex);
  339. }
  340. - (void)scrollWheel:(NSEvent*) event {
  341. if(core == NULL || [event deltaY] == 0)
  342. return;
  343. core->lockMutex(core->eventMutex);
  344. NSPoint mouseLoc = [self convertPoint:[event locationInWindow] fromView:self];
  345. CocoaEvent newEvent;
  346. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  347. if([event deltaY] > 0)
  348. newEvent.eventCode = InputEvent::EVENT_MOUSEWHEEL_UP;
  349. else
  350. newEvent.eventCode = InputEvent::EVENT_MOUSEWHEEL_DOWN;
  351. newEvent.mouseX = mouseLoc.x;
  352. newEvent.mouseY = core->getYRes() - mouseLoc.y;
  353. core->cocoaEvents.push_back(newEvent);
  354. core->unlockMutex(core->eventMutex);
  355. }
  356. - (void)touchesBeganWithEvent:(NSEvent *)event
  357. {
  358. if(core == NULL)
  359. return;
  360. core->lockMutex(core->eventMutex);
  361. NSSet *touches = [event touchesMatchingPhase:NSTouchPhaseAny inView:self];
  362. NSArray *touchArray = [touches allObjects];
  363. std::vector<TouchInfo> touchInfos;
  364. for(NSTouch *touch in touchArray) {
  365. TouchInfo info;
  366. long touchID = (long)[touch identity];
  367. info.id = touchID;
  368. info.position.x = [touch normalizedPosition].x * core->getXRes();
  369. info.position.y = (1.0-[touch normalizedPosition].y) * core->getYRes();
  370. touchInfos.push_back(info);
  371. }
  372. unsigned int index = 0;
  373. for(NSTouch *touch in touchArray) {
  374. if([touch phase] == NSTouchPhaseBegan) {
  375. CocoaEvent newEvent;
  376. newEvent.eventCode = InputEvent::EVENT_TOUCHES_BEGAN;
  377. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  378. newEvent.touches = touchInfos;
  379. newEvent.touch = touchInfos[index];
  380. core->cocoaEvents.push_back(newEvent);
  381. }
  382. index++;
  383. }
  384. core->unlockMutex(core->eventMutex);
  385. }
  386. - (void)touchesMovedWithEvent:(NSEvent *)event
  387. {
  388. if(core == NULL)
  389. return;
  390. core->lockMutex(core->eventMutex);
  391. NSSet *touches = [event touchesMatchingPhase:NSTouchPhaseAny inView:self];
  392. NSArray *touchArray = [touches allObjects];
  393. std::vector<TouchInfo> touchInfos;
  394. for(NSTouch *touch in touchArray) {
  395. TouchInfo info;
  396. long touchID = (long)[touch identity];
  397. info.id = touchID;
  398. info.position.x = [touch normalizedPosition].x * core->getXRes();
  399. info.position.y = (1.0-[touch normalizedPosition].y) * core->getYRes();
  400. touchInfos.push_back(info);
  401. }
  402. unsigned int index = 0;
  403. for(NSTouch *touch in touchArray) {
  404. if([touch phase] == NSTouchPhaseMoved) {
  405. CocoaEvent newEvent;
  406. newEvent.eventCode = InputEvent::EVENT_TOUCHES_MOVED;
  407. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  408. newEvent.touches = touchInfos;
  409. newEvent.touch = touchInfos[index];
  410. core->cocoaEvents.push_back(newEvent);
  411. }
  412. index++;
  413. }
  414. core->unlockMutex(core->eventMutex);
  415. }
  416. - (void)touchesEndedWithEvent:(NSEvent *)event
  417. {
  418. if(core == NULL)
  419. return;
  420. core->lockMutex(core->eventMutex);
  421. NSSet *touches = [event touchesMatchingPhase:NSTouchPhaseAny inView:self];
  422. NSArray *touchArray = [touches allObjects];
  423. std::vector<TouchInfo> touchInfos;
  424. for(NSTouch *touch in touchArray) {
  425. TouchInfo info;
  426. long touchID = (long)[touch identity];
  427. info.id = touchID;
  428. info.position.x = [touch normalizedPosition].x * core->getXRes();
  429. info.position.y = (1.0-[touch normalizedPosition].y) * core->getYRes();
  430. touchInfos.push_back(info);
  431. }
  432. unsigned int index = 0;
  433. for(NSTouch *touch in touchArray) {
  434. if([touch phase] == NSTouchPhaseEnded) {
  435. CocoaEvent newEvent;
  436. newEvent.eventCode = InputEvent::EVENT_TOUCHES_ENDED;
  437. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  438. newEvent.touches = touchInfos;
  439. newEvent.touch = touchInfos[index];
  440. core->cocoaEvents.push_back(newEvent);
  441. }
  442. index++;
  443. }
  444. core->unlockMutex(core->eventMutex);
  445. }
  446. - (void)touchesCancelledWithEvent:(NSEvent *)event
  447. {
  448. if(core == NULL)
  449. return;
  450. core->lockMutex(core->eventMutex);
  451. NSSet *touches = [event touchesMatchingPhase:NSTouchPhaseAny inView:self];
  452. NSArray *touchArray = [touches allObjects];
  453. std::vector<TouchInfo> touchInfos;
  454. for(NSTouch *touch in touchArray) {
  455. TouchInfo info;
  456. long touchID = (long)[touch identity];
  457. info.id = touchID;
  458. info.position.x = [touch normalizedPosition].x * core->getXRes();
  459. info.position.y = (1.0-[touch normalizedPosition].y) * core->getYRes();
  460. touchInfos.push_back(info);
  461. }
  462. unsigned int index = 0;
  463. for(NSTouch *touch in touchArray) {
  464. if([touch phase] == NSTouchPhaseCancelled) {
  465. CocoaEvent newEvent;
  466. newEvent.eventCode = InputEvent::EVENT_TOUCHES_ENDED;
  467. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  468. newEvent.touches = touchInfos;
  469. newEvent.touch = touchInfos[index];
  470. core->cocoaEvents.push_back(newEvent);
  471. }
  472. index++;
  473. }
  474. core->unlockMutex(core->eventMutex);
  475. }
  476. - (void) mouseDown:(NSEvent *) event
  477. {
  478. if(core == NULL)
  479. return;
  480. core->lockMutex(core->eventMutex);
  481. NSPoint mouseLoc = [self convertPoint:[event locationInWindow] fromView:self];
  482. CocoaEvent newEvent;
  483. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  484. newEvent.eventCode = InputEvent::EVENT_MOUSEDOWN;
  485. newEvent.mouseButton = CoreInput::MOUSE_BUTTON1;
  486. newEvent.mouseX = mouseLoc.x;
  487. newEvent.mouseY = core->getYRes() - mouseLoc.y;
  488. mouseX = mouseLoc.x;
  489. mouseY = mouseLoc.y;
  490. core->cocoaEvents.push_back(newEvent);
  491. core->unlockMutex(core->eventMutex);
  492. }
  493. - (void) mouseUp:(NSEvent *) event
  494. {
  495. if(core == NULL)
  496. return;
  497. core->lockMutex(core->eventMutex);
  498. NSPoint mouseLoc = [self convertPoint:[event locationInWindow] fromView:self];
  499. CocoaEvent newEvent;
  500. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  501. newEvent.eventCode = InputEvent::EVENT_MOUSEUP;
  502. newEvent.mouseButton = CoreInput::MOUSE_BUTTON1;
  503. newEvent.mouseX = mouseLoc.x;
  504. newEvent.mouseY = core->getYRes() - mouseLoc.y;
  505. mouseX = mouseLoc.x;
  506. mouseY = mouseLoc.y;
  507. core->cocoaEvents.push_back(newEvent);
  508. core->unlockMutex(core->eventMutex);
  509. }
  510. - (void)flagsChanged:(NSEvent *)theEvent
  511. {
  512. if(core == NULL)
  513. return;
  514. if([theEvent keyCode] == 0) {
  515. return;
  516. }
  517. core->lockMutex(core->eventMutex);
  518. CocoaEvent newEvent;
  519. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  520. newEvent.keyCode = keymap[[theEvent keyCode]];
  521. switch(newEvent.keyCode) {
  522. case Polycode::KEY_LALT:
  523. if (([theEvent modifierFlags] & 0x80120) == 0x80120) {
  524. newEvent.eventCode = InputEvent::EVENT_KEYDOWN;
  525. } else {
  526. newEvent.eventCode = InputEvent::EVENT_KEYUP;
  527. }
  528. break;
  529. case Polycode::KEY_RALT:
  530. if (([theEvent modifierFlags] & 0x80140) == 0x80140) {
  531. newEvent.eventCode = InputEvent::EVENT_KEYDOWN;
  532. } else {
  533. newEvent.eventCode = InputEvent::EVENT_KEYUP;
  534. }
  535. break;
  536. case Polycode::KEY_LSHIFT:
  537. if (([theEvent modifierFlags] & 0x20102) == 0x20102) {
  538. newEvent.eventCode = InputEvent::EVENT_KEYDOWN;
  539. } else {
  540. newEvent.eventCode = InputEvent::EVENT_KEYUP;
  541. }
  542. break;
  543. case Polycode::KEY_RSHIFT:
  544. if (([theEvent modifierFlags] & 0x20104) == 0x20104) {
  545. newEvent.eventCode = InputEvent::EVENT_KEYDOWN;
  546. } else {
  547. newEvent.eventCode = InputEvent::EVENT_KEYUP;
  548. }
  549. break;
  550. case Polycode::KEY_LCTRL:
  551. if (([theEvent modifierFlags] & 0x40101) == 0x40101) {
  552. newEvent.eventCode = InputEvent::EVENT_KEYDOWN;
  553. } else {
  554. newEvent.eventCode = InputEvent::EVENT_KEYUP;
  555. }
  556. break;
  557. case Polycode::KEY_RCTRL:
  558. // no right control on my mac :)
  559. break;
  560. case Polycode::KEY_LSUPER:
  561. if (([theEvent modifierFlags] & 0x100108) == 0x100108) {
  562. newEvent.eventCode = InputEvent::EVENT_KEYDOWN;
  563. } else {
  564. newEvent.eventCode = InputEvent::EVENT_KEYUP;
  565. }
  566. break;
  567. case Polycode::KEY_RSUPER:
  568. if (([theEvent modifierFlags] & 0x100110) == 0x100110) {
  569. newEvent.eventCode = InputEvent::EVENT_KEYDOWN;
  570. } else {
  571. newEvent.eventCode = InputEvent::EVENT_KEYUP;
  572. }
  573. break;
  574. default:
  575. // Don't care otherwise
  576. break;
  577. }
  578. newEvent.unicodeChar = 0;
  579. core->cocoaEvents.push_back(newEvent);
  580. core->unlockMutex(core->eventMutex);
  581. }
  582. - (void)keyDown: (NSEvent*) theEvent
  583. {
  584. if(core == NULL)
  585. return;
  586. core->lockMutex(core->eventMutex);
  587. CocoaEvent newEvent;
  588. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  589. newEvent.eventCode = InputEvent::EVENT_KEYDOWN;
  590. newEvent.keyCode = keymap[[theEvent keyCode]];
  591. NSString *chars = [theEvent characters];
  592. NSUInteger numChars = [chars length];
  593. // NSLog(@"CHARS: %@", [chars characterAtIndex:0]);
  594. if(numChars > 0) {
  595. newEvent.unicodeChar = [chars characterAtIndex:0];
  596. } else {
  597. newEvent.unicodeChar = 0;
  598. }
  599. core->cocoaEvents.push_back(newEvent);
  600. core->unlockMutex(core->eventMutex);
  601. }
  602. - (void)keyUp: (NSEvent*) theEvent
  603. {
  604. if(core == NULL)
  605. return;
  606. core->lockMutex(core->eventMutex);
  607. CocoaEvent newEvent;
  608. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  609. newEvent.eventCode = InputEvent::EVENT_KEYUP;
  610. newEvent.keyCode = keymap[[theEvent keyCode]];
  611. NSString *chars = [theEvent characters];
  612. NSUInteger numChars = [chars length];
  613. if(numChars > 0)
  614. newEvent.unicodeChar = [chars characterAtIndex:0];
  615. else
  616. newEvent.unicodeChar = 0;
  617. core->cocoaEvents.push_back(newEvent);
  618. core->unlockMutex(core->eventMutex);
  619. }
  620. - (void)mouseDragged:(NSEvent *)theEvent
  621. {
  622. if(core == NULL)
  623. return;
  624. core->lockMutex(core->eventMutex);
  625. NSPoint mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:self];
  626. CocoaEvent newEvent;
  627. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  628. newEvent.eventCode = InputEvent::EVENT_MOUSEMOVE;
  629. newEvent.mouseX = mouseLoc.x;
  630. newEvent.mouseY = core->getYRes() - mouseLoc.y;
  631. core->cocoaEvents.push_back(newEvent);
  632. core->unlockMutex(core->eventMutex);
  633. }
  634. - (void)mouseMoved:(NSEvent *)theEvent
  635. {
  636. if(core == NULL)
  637. return;
  638. core->lockMutex(core->eventMutex);
  639. NSPoint mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:self];
  640. CocoaEvent newEvent;
  641. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  642. newEvent.eventCode = InputEvent::EVENT_MOUSEMOVE;
  643. newEvent.mouseX = mouseLoc.x;
  644. newEvent.mouseY = core->getYRes() - mouseLoc.y;
  645. mouseX = mouseLoc.x;
  646. mouseY = mouseLoc.y;
  647. core->cocoaEvents.push_back(newEvent);
  648. core->unlockMutex(core->eventMutex);
  649. }
  650. @end