PolycodeView.mm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowResized:) name:NSWindowDidResizeNotification object:[self window]];
  41. [self setAutoresizesSubviews: YES];
  42. }
  43. -(void) viewDidMoveToWindow
  44. {
  45. [super viewDidMoveToWindow];
  46. viewReady = YES;
  47. }
  48. - (void) initKeymap {
  49. for(int i=0; i < 512; i++) {
  50. modifierMap[i] = 0;
  51. }
  52. keymap[0x00] = KEY_a;
  53. keymap[0x01] = KEY_s;
  54. keymap[0x02] = KEY_d;
  55. keymap[0x03] = KEY_f;
  56. keymap[0x04] = KEY_g;
  57. keymap[0x05] = KEY_h;
  58. keymap[0x06] = KEY_z;
  59. keymap[0x07] = KEY_x;
  60. keymap[0x08] = KEY_c;
  61. keymap[0x09] = KEY_v;
  62. keymap[0x0B] = KEY_b;
  63. keymap[0x0C] = KEY_q;
  64. keymap[0x0D] = KEY_w;
  65. keymap[0x0E] = KEY_e;
  66. keymap[0x0F] = KEY_r;
  67. keymap[0x10] = KEY_y;
  68. keymap[0x11] = KEY_t;
  69. keymap[0x12] = KEY_1;
  70. keymap[0x13] = KEY_2;
  71. keymap[0x14] = KEY_3;
  72. keymap[0x15] = KEY_4;
  73. keymap[0x16] = KEY_6;
  74. keymap[0x17] = KEY_5;
  75. keymap[0x18] = KEY_EQUALS;
  76. keymap[0x19] = KEY_9;
  77. keymap[0x1A] = KEY_7;
  78. keymap[0x1B] = KEY_MINUS;
  79. keymap[0x1C] = KEY_8;
  80. keymap[0x1D] = KEY_0;
  81. keymap[0x1E] = KEY_RIGHTBRACKET;
  82. keymap[0x1F] = KEY_o;
  83. keymap[0x20] = KEY_u;
  84. keymap[0x21] = KEY_LEFTBRACKET;
  85. keymap[0x22] = KEY_i;
  86. keymap[0x23] = KEY_p;
  87. keymap[0x25] = KEY_l;
  88. keymap[0x26] = KEY_j;
  89. keymap[0x27] = KEY_QUOTE;
  90. keymap[0x28] = KEY_k;
  91. keymap[0x29] = KEY_SEMICOLON;
  92. keymap[0x2A] = KEY_BACKSLASH;
  93. keymap[0x2B] = KEY_COMMA;
  94. keymap[0x2C] = KEY_SLASH;
  95. keymap[0x2D] = KEY_n;
  96. keymap[0x2E] = KEY_m;
  97. keymap[0x2F] = KEY_PERIOD;
  98. // keymap[0x32] = KEY_GRAVE;
  99. keymap[0x41] = KEY_KP_PERIOD;
  100. keymap[0x43] = KEY_KP_MULTIPLY;
  101. keymap[0x45] = KEY_KP_PLUS;
  102. // keymap[0x47] = KEY_KeypadClear;
  103. keymap[0x4B] = KEY_KP_DIVIDE;
  104. keymap[0x4C] = KEY_KP_ENTER;
  105. keymap[0x4E] = KEY_KP_MINUS;
  106. keymap[0x51] = KEY_KP_EQUALS;
  107. keymap[0x52] = KEY_KP0;
  108. keymap[0x53] = KEY_KP1;
  109. keymap[0x54] = KEY_KP2;
  110. keymap[0x55] = KEY_KP3;
  111. keymap[0x56] = KEY_KP4;
  112. keymap[0x57] = KEY_KP5;
  113. keymap[0x58] = KEY_KP6;
  114. keymap[0x59] = KEY_KP7;
  115. keymap[0x5B] = KEY_KP8;
  116. keymap[0x5C] = KEY_KP9;
  117. keymap[0x24] = KEY_RETURN;
  118. keymap[0x30] = KEY_TAB;
  119. keymap[0x31] = KEY_SPACE;
  120. keymap[0x33] = KEY_BACKSPACE;
  121. keymap[0x35] = KEY_ESCAPE;
  122. keymap[0x37] = KEY_LSUPER;
  123. keymap[0x36] = KEY_RSUPER;
  124. keymap[0x38] = KEY_LSHIFT;
  125. keymap[0x39] = KEY_CAPSLOCK;
  126. keymap[0x3A] = KEY_LALT;
  127. keymap[0x3D] = KEY_RALT;
  128. keymap[0x3B] = KEY_LCTRL;
  129. keymap[0x3C] = KEY_RSHIFT;
  130. keymap[0x3E] = KEY_RCTRL;
  131. keymap[0x3F] = KEY_COMPOSE;
  132. // keymap[0x40] = KEY_F17;
  133. // keymap[0x48] = KEY_VolumeUp;
  134. // keymap[0x49] = KEY_VolumeDown;
  135. // keymap[0x4A] = KEY_Mute;
  136. // keymap[0x4F] = KEY_F18;
  137. // keymap[0x50] = KEY_F19;
  138. // keymap[0x5A] = KEY_F20;
  139. keymap[0x60] = KEY_F5;
  140. keymap[0x61] = KEY_F6;
  141. keymap[0x62] = KEY_F7;
  142. keymap[0x63] = KEY_F3;
  143. keymap[0x64] = KEY_F8;
  144. keymap[0x65] = KEY_F9;
  145. keymap[0x67] = KEY_F11;
  146. keymap[0x69] = KEY_F13;
  147. // keymap[0x6A] = KEY_F16;
  148. keymap[0x6B] = KEY_F14;
  149. keymap[0x6D] = KEY_F10;
  150. keymap[0x6F] = KEY_F12;
  151. keymap[0x71] = KEY_F15;
  152. keymap[0x72] = KEY_HELP;
  153. keymap[0x73] = KEY_HOME;
  154. keymap[0x74] = KEY_PAGEUP;
  155. keymap[0x75] = KEY_DELETE;
  156. keymap[0x76] = KEY_F4;
  157. keymap[0x77] = KEY_END;
  158. keymap[0x78] = KEY_F2;
  159. keymap[0x79] = KEY_PAGEDOWN;
  160. keymap[0x7A] = KEY_F1;
  161. keymap[0x7B] = KEY_LEFT;
  162. keymap[0x7C] = KEY_RIGHT;
  163. keymap[0x7D] = KEY_DOWN;
  164. keymap[0x7E] = KEY_UP;
  165. // keymap[0x0A] = KEY_ISO_Section;
  166. // keymap[0x5D] = KEY_JIS_Yen;
  167. // keymap[0x5E] = KEY_JIS_Underscore;
  168. // keymap[0x5F] = KEY_JIS_KeypadComma;
  169. // keymap[0x66] = KEY_JIS_Eisu;
  170. // keymap[0x68] = KEY_JIS_Kana;
  171. }
  172. -(void) setCurrentCursor: (NSCursor*) newCursor
  173. {
  174. currentCursor = newCursor;
  175. }
  176. - (void)resetCursorRects
  177. {
  178. if(currentCursor)
  179. [self addCursorRect:[self bounds] cursor:currentCursor];
  180. }
  181. - (void) setCore: (CocoaCore*) newCore {
  182. [self initKeymap];
  183. core = newCore;
  184. }
  185. - (BOOL) lockFocusIfCanDraw {
  186. [contextLock lock];
  187. BOOL retVal = [super lockFocusIfCanDraw];
  188. [contextLock unlock];
  189. return retVal;
  190. }
  191. - (void) update {
  192. [contextLock lock];
  193. [super update];
  194. [contextLock unlock];
  195. }
  196. - (void) lockContext {
  197. [contextLock lock];
  198. }
  199. - (void) unlockContext {
  200. [contextLock unlock];
  201. }
  202. - (void)prepareOpenGL {
  203. contextReady = YES;
  204. }
  205. - (BOOL) isContextReady {
  206. return contextReady;
  207. }
  208. - (void)setOpenGLContext:(NSOpenGLContext *)context {
  209. contextReady = NO;
  210. [super setOpenGLContext:context];
  211. }
  212. - (BOOL)acceptsFirstResponder
  213. {
  214. return YES;
  215. }
  216. // ---------------------------------
  217. - (BOOL)becomeFirstResponder
  218. {
  219. return YES;
  220. }
  221. // ---------------------------------
  222. - (BOOL)resignFirstResponder
  223. {
  224. return YES;
  225. }
  226. - (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
  227. {
  228. return YES;
  229. }
  230. - (BOOL)mouseDownCanMoveWindow
  231. {
  232. return NO;
  233. }
  234. - (void) drawRect:(NSRect)rect {
  235. if(glSizeX != (int)[self frame].size.width || glSizeY != (int)[self frame].size.height) {
  236. glSizeX = (int)[self frame].size.width;
  237. glSizeY = (int)[self frame].size.height;
  238. if(core != NULL) {
  239. core->resizeTo(glSizeX, glSizeY);
  240. }
  241. }
  242. }
  243. -(void) windowResized: (NSNotification *) notification {
  244. }
  245. -(void) dealloc {
  246. [[NSNotificationCenter defaultCenter] removeObserver:self];
  247. [super dealloc];
  248. }
  249. - (void) resizeWithOldSuperviewSize: (NSSize) oldSize {
  250. [super resizeWithOldSuperviewSize: oldSize];
  251. // if(core == NULL)
  252. // return;
  253. // core->setVideoMode(oldSize.width, oldSize.height, core->isFullscreen(), core->getAALevel());
  254. }
  255. // INPUT
  256. - (void) otherMouseDown:(NSEvent *) event
  257. {
  258. if(core == NULL)
  259. return;
  260. core->lockMutex(core->eventMutex);
  261. NSPoint mouseLoc = [self convertPoint:[event locationInWindow] fromView:self];
  262. CocoaEvent newEvent;
  263. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  264. newEvent.eventCode = InputEvent::EVENT_MOUSEDOWN;
  265. newEvent.mouseButton = CoreInput::MOUSE_BUTTON3;
  266. newEvent.mouseX = mouseLoc.x;
  267. newEvent.mouseY = core->getYRes() - mouseLoc.y;
  268. core->cocoaEvents.push_back(newEvent);
  269. core->unlockMutex(core->eventMutex);
  270. }
  271. - (void) otherMouseUp:(NSEvent *) event
  272. {
  273. if(core == NULL)
  274. return;
  275. core->lockMutex(core->eventMutex);
  276. NSPoint mouseLoc = [self convertPoint:[event locationInWindow] fromView:self];
  277. CocoaEvent newEvent;
  278. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  279. newEvent.eventCode = InputEvent::EVENT_MOUSEUP;
  280. newEvent.mouseButton = CoreInput::MOUSE_BUTTON3;
  281. newEvent.mouseX = mouseLoc.x;
  282. newEvent.mouseY = core->getYRes() - mouseLoc.y;
  283. core->cocoaEvents.push_back(newEvent);
  284. core->unlockMutex(core->eventMutex);
  285. }
  286. - (void) rightMouseDown:(NSEvent *) event
  287. {
  288. if(core == NULL)
  289. return;
  290. core->lockMutex(core->eventMutex);
  291. NSPoint mouseLoc = [self convertPoint:[event locationInWindow] fromView:self];
  292. CocoaEvent newEvent;
  293. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  294. newEvent.eventCode = InputEvent::EVENT_MOUSEDOWN;
  295. newEvent.mouseButton = CoreInput::MOUSE_BUTTON2;
  296. newEvent.mouseX = mouseLoc.x;
  297. newEvent.mouseY = core->getYRes() - mouseLoc.y;
  298. core->cocoaEvents.push_back(newEvent);
  299. core->unlockMutex(core->eventMutex);
  300. }
  301. - (void) rightMouseUp:(NSEvent *) event
  302. {
  303. if(core == NULL)
  304. return;
  305. core->lockMutex(core->eventMutex);
  306. NSPoint mouseLoc = [self convertPoint:[event locationInWindow] fromView:self];
  307. CocoaEvent newEvent;
  308. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  309. newEvent.eventCode = InputEvent::EVENT_MOUSEUP;
  310. newEvent.mouseButton = CoreInput::MOUSE_BUTTON2;
  311. newEvent.mouseX = mouseLoc.x;
  312. newEvent.mouseY = core->getYRes() - mouseLoc.y;
  313. core->cocoaEvents.push_back(newEvent);
  314. core->unlockMutex(core->eventMutex);
  315. }
  316. - (void)scrollWheel:(NSEvent*) event {
  317. if(core == NULL || [event deltaY] == 0)
  318. return;
  319. core->lockMutex(core->eventMutex);
  320. NSPoint mouseLoc = [self convertPoint:[event locationInWindow] fromView:self];
  321. CocoaEvent newEvent;
  322. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  323. if([event deltaY] > 0)
  324. newEvent.eventCode = InputEvent::EVENT_MOUSEWHEEL_UP;
  325. else
  326. newEvent.eventCode = InputEvent::EVENT_MOUSEWHEEL_DOWN;
  327. newEvent.mouseX = mouseLoc.x;
  328. newEvent.mouseY = core->getYRes() - mouseLoc.y;
  329. core->cocoaEvents.push_back(newEvent);
  330. core->unlockMutex(core->eventMutex);
  331. }
  332. - (void) mouseDown:(NSEvent *) event
  333. {
  334. if(core == NULL)
  335. return;
  336. core->lockMutex(core->eventMutex);
  337. NSPoint mouseLoc = [self convertPoint:[event locationInWindow] fromView:self];
  338. CocoaEvent newEvent;
  339. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  340. newEvent.eventCode = InputEvent::EVENT_MOUSEDOWN;
  341. newEvent.mouseButton = CoreInput::MOUSE_BUTTON1;
  342. newEvent.mouseX = mouseLoc.x;
  343. newEvent.mouseY = core->getYRes() - mouseLoc.y;
  344. mouseX = mouseLoc.x;
  345. mouseY = mouseLoc.y;
  346. core->cocoaEvents.push_back(newEvent);
  347. core->unlockMutex(core->eventMutex);
  348. }
  349. - (void) mouseUp:(NSEvent *) event
  350. {
  351. if(core == NULL)
  352. return;
  353. core->lockMutex(core->eventMutex);
  354. NSPoint mouseLoc = [self convertPoint:[event locationInWindow] fromView:self];
  355. CocoaEvent newEvent;
  356. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  357. newEvent.eventCode = InputEvent::EVENT_MOUSEUP;
  358. newEvent.mouseButton = CoreInput::MOUSE_BUTTON1;
  359. newEvent.mouseX = mouseLoc.x;
  360. newEvent.mouseY = core->getYRes() - mouseLoc.y;
  361. mouseX = mouseLoc.x;
  362. mouseY = mouseLoc.y;
  363. core->cocoaEvents.push_back(newEvent);
  364. core->unlockMutex(core->eventMutex);
  365. }
  366. - (void)flagsChanged:(NSEvent *)theEvent
  367. {
  368. if(core == NULL)
  369. return;
  370. // NSLog(@"KEY: %x\n", [theEvent keyCode]);
  371. core->lockMutex(core->eventMutex);
  372. CocoaEvent newEvent;
  373. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  374. newEvent.keyCode = keymap[[theEvent keyCode]];
  375. if(modifierMap[[theEvent keyCode]] == 0) {
  376. modifierMap[[theEvent keyCode]] = 1;
  377. newEvent.eventCode = InputEvent::EVENT_KEYDOWN;
  378. } else {
  379. modifierMap[[theEvent keyCode]] = 0;
  380. newEvent.eventCode = InputEvent::EVENT_KEYUP;
  381. }
  382. newEvent.unicodeChar = 0;
  383. core->cocoaEvents.push_back(newEvent);
  384. core->unlockMutex(core->eventMutex);
  385. }
  386. - (void)keyDown: (NSEvent*) theEvent
  387. {
  388. if(core == NULL)
  389. return;
  390. core->lockMutex(core->eventMutex);
  391. CocoaEvent newEvent;
  392. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  393. newEvent.eventCode = InputEvent::EVENT_KEYDOWN;
  394. newEvent.keyCode = keymap[[theEvent keyCode]];
  395. NSString *chars = [theEvent characters];
  396. unsigned int numChars = [chars length];
  397. // NSLog(@"CHARS: %@", [chars characterAtIndex:0]);
  398. if(numChars > 0) {
  399. newEvent.unicodeChar = [chars characterAtIndex:0];
  400. } else {
  401. newEvent.unicodeChar = 0;
  402. }
  403. core->cocoaEvents.push_back(newEvent);
  404. core->unlockMutex(core->eventMutex);
  405. }
  406. - (void)keyUp: (NSEvent*) theEvent
  407. {
  408. if(core == NULL)
  409. return;
  410. core->lockMutex(core->eventMutex);
  411. NSPoint mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:self];
  412. CocoaEvent newEvent;
  413. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  414. newEvent.eventCode = InputEvent::EVENT_KEYUP;
  415. newEvent.keyCode = keymap[[theEvent keyCode]];
  416. NSString *chars = [theEvent characters];
  417. unsigned int numChars = [chars length];
  418. if(numChars > 0)
  419. newEvent.unicodeChar = [chars characterAtIndex:0];
  420. else
  421. newEvent.unicodeChar = 0;
  422. core->cocoaEvents.push_back(newEvent);
  423. core->unlockMutex(core->eventMutex);
  424. }
  425. - (void)mouseDragged:(NSEvent *)theEvent
  426. {
  427. if(core == NULL)
  428. return;
  429. core->lockMutex(core->eventMutex);
  430. NSPoint mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:self];
  431. CocoaEvent newEvent;
  432. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  433. newEvent.eventCode = InputEvent::EVENT_MOUSEMOVE;
  434. newEvent.mouseX = mouseLoc.x;
  435. newEvent.mouseY = core->getYRes() - mouseLoc.y;
  436. core->cocoaEvents.push_back(newEvent);
  437. core->unlockMutex(core->eventMutex);
  438. }
  439. - (void)mouseMoved:(NSEvent *)theEvent
  440. {
  441. if(core == NULL)
  442. return;
  443. core->lockMutex(core->eventMutex);
  444. NSPoint mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:self];
  445. CocoaEvent newEvent;
  446. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  447. newEvent.eventCode = InputEvent::EVENT_MOUSEMOVE;
  448. newEvent.mouseX = mouseLoc.x;
  449. newEvent.mouseY = core->getYRes() - mouseLoc.y;
  450. mouseX = mouseLoc.x;
  451. mouseY = mouseLoc.y;
  452. core->cocoaEvents.push_back(newEvent);
  453. core->unlockMutex(core->eventMutex);
  454. }
  455. @end