PolySubstanceView.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. //
  2. // SubstanceView.m
  3. // Substance
  4. //
  5. // Created by Ivan Safrin on 2/21/10.
  6. // Copyright 2010 Ivan Safrin. All rights reserved.
  7. //
  8. #import "PolySubstanceView.h"
  9. @implementation SubstanceView
  10. @synthesize viewReady;
  11. @synthesize mouseX;
  12. @synthesize mouseY;
  13. - (id)initWithFrame:(NSRect)frameRect pixelFormat:(NSOpenGLPixelFormat *)format
  14. {
  15. self = [super initWithFrame:frameRect pixelFormat:format];
  16. if(self) {
  17. viewReady = NO;
  18. currentCursor = NULL;
  19. contextLock = [[NSLock alloc] init];
  20. }
  21. return self;
  22. }
  23. - (void) awakeFromNib {
  24. contextLock = [[NSLock alloc] init];
  25. [[self window] setAcceptsMouseMovedEvents:YES];
  26. [[self window] makeFirstResponder:self];
  27. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowResized:) name:NSWindowDidResizeNotification object:[self window]];
  28. [self setAutoresizesSubviews: YES];
  29. }
  30. -(void) viewDidMoveToWindow
  31. {
  32. [super viewDidMoveToWindow];
  33. viewReady = YES;
  34. NSLog(@"VIEW READY!");
  35. }
  36. - (void) initKeymap {
  37. for(int i=0; i < 512; i++) {
  38. modifierMap[i] = 0;
  39. }
  40. keymap[0x00] = TAUK_a;
  41. keymap[0x01] = TAUK_s;
  42. keymap[0x02] = TAUK_d;
  43. keymap[0x03] = TAUK_f;
  44. keymap[0x04] = TAUK_g;
  45. keymap[0x05] = TAUK_h;
  46. keymap[0x06] = TAUK_z;
  47. keymap[0x07] = TAUK_x;
  48. keymap[0x08] = TAUK_c;
  49. keymap[0x09] = TAUK_v;
  50. keymap[0x0B] = TAUK_b;
  51. keymap[0x0C] = TAUK_q;
  52. keymap[0x0D] = TAUK_w;
  53. keymap[0x0E] = TAUK_e;
  54. keymap[0x0F] = TAUK_r;
  55. keymap[0x10] = TAUK_y;
  56. keymap[0x11] = TAUK_t;
  57. keymap[0x12] = TAUK_1;
  58. keymap[0x13] = TAUK_2;
  59. keymap[0x14] = TAUK_3;
  60. keymap[0x15] = TAUK_4;
  61. keymap[0x16] = TAUK_6;
  62. keymap[0x17] = TAUK_5;
  63. keymap[0x18] = TAUK_EQUALS;
  64. keymap[0x19] = TAUK_9;
  65. keymap[0x1A] = TAUK_7;
  66. keymap[0x1B] = TAUK_MINUS;
  67. keymap[0x1C] = TAUK_8;
  68. keymap[0x1D] = TAUK_0;
  69. keymap[0x1E] = TAUK_RIGHTBRACKET;
  70. keymap[0x1F] = TAUK_o;
  71. keymap[0x20] = TAUK_u;
  72. keymap[0x21] = TAUK_LEFTBRACKET;
  73. keymap[0x22] = TAUK_i;
  74. keymap[0x23] = TAUK_p;
  75. keymap[0x25] = TAUK_l;
  76. keymap[0x26] = TAUK_j;
  77. keymap[0x27] = TAUK_QUOTE;
  78. keymap[0x28] = TAUK_k;
  79. keymap[0x29] = TAUK_SEMICOLON;
  80. keymap[0x2A] = TAUK_BACKSLASH;
  81. keymap[0x2B] = TAUK_COMMA;
  82. keymap[0x2C] = TAUK_SLASH;
  83. keymap[0x2D] = TAUK_n;
  84. keymap[0x2E] = TAUK_m;
  85. keymap[0x2F] = TAUK_PERIOD;
  86. // keymap[0x32] = TAUK_GRAVE;
  87. keymap[0x41] = TAUK_KP_PERIOD;
  88. keymap[0x43] = TAUK_KP_MULTIPLY;
  89. keymap[0x45] = TAUK_KP_PLUS;
  90. // keymap[0x47] = TAUK_KeypadClear;
  91. keymap[0x4B] = TAUK_KP_DIVIDE;
  92. keymap[0x4C] = TAUK_KP_ENTER;
  93. keymap[0x4E] = TAUK_KP_MINUS;
  94. keymap[0x51] = TAUK_KP_EQUALS;
  95. keymap[0x52] = TAUK_KP0;
  96. keymap[0x53] = TAUK_KP1;
  97. keymap[0x54] = TAUK_KP2;
  98. keymap[0x55] = TAUK_KP3;
  99. keymap[0x56] = TAUK_KP4;
  100. keymap[0x57] = TAUK_KP5;
  101. keymap[0x58] = TAUK_KP6;
  102. keymap[0x59] = TAUK_KP7;
  103. keymap[0x5B] = TAUK_KP8;
  104. keymap[0x5C] = TAUK_KP9;
  105. keymap[0x24] = TAUK_RETURN;
  106. keymap[0x30] = TAUK_TAB;
  107. keymap[0x31] = TAUK_SPACE;
  108. keymap[0x33] = TAUK_BACKSPACE;
  109. keymap[0x35] = TAUK_ESCAPE;
  110. keymap[0x37] = TAUK_LSUPER;
  111. keymap[0x36] = TAUK_RSUPER;
  112. keymap[0x38] = TAUK_LSHIFT;
  113. keymap[0x39] = TAUK_CAPSLOCK;
  114. keymap[0x3A] = TAUK_LALT;
  115. keymap[0x3D] = TAUK_RALT;
  116. keymap[0x3B] = TAUK_LCTRL;
  117. keymap[0x3C] = TAUK_RSHIFT;
  118. keymap[0x3E] = TAUK_RCTRL;
  119. keymap[0x3F] = TAUK_COMPOSE;
  120. // keymap[0x40] = TAUK_F17;
  121. // keymap[0x48] = TAUK_VolumeUp;
  122. // keymap[0x49] = TAUK_VolumeDown;
  123. // keymap[0x4A] = TAUK_Mute;
  124. // keymap[0x4F] = TAUK_F18;
  125. // keymap[0x50] = TAUK_F19;
  126. // keymap[0x5A] = TAUK_F20;
  127. keymap[0x60] = TAUK_F5;
  128. keymap[0x61] = TAUK_F6;
  129. keymap[0x62] = TAUK_F7;
  130. keymap[0x63] = TAUK_F3;
  131. keymap[0x64] = TAUK_F8;
  132. keymap[0x65] = TAUK_F9;
  133. keymap[0x67] = TAUK_F11;
  134. keymap[0x69] = TAUK_F13;
  135. // keymap[0x6A] = TAUK_F16;
  136. keymap[0x6B] = TAUK_F14;
  137. keymap[0x6D] = TAUK_F10;
  138. keymap[0x6F] = TAUK_F12;
  139. keymap[0x71] = TAUK_F15;
  140. keymap[0x72] = TAUK_HELP;
  141. keymap[0x73] = TAUK_HOME;
  142. keymap[0x74] = TAUK_PAGEUP;
  143. keymap[0x75] = TAUK_DELETE;
  144. keymap[0x76] = TAUK_F4;
  145. keymap[0x77] = TAUK_END;
  146. keymap[0x78] = TAUK_F2;
  147. keymap[0x79] = TAUK_PAGEDOWN;
  148. keymap[0x7A] = TAUK_F1;
  149. keymap[0x7B] = TAUK_LEFT;
  150. keymap[0x7C] = TAUK_RIGHT;
  151. keymap[0x7D] = TAUK_DOWN;
  152. keymap[0x7E] = TAUK_UP;
  153. // keymap[0x0A] = TAUK_ISO_Section;
  154. // keymap[0x5D] = TAUK_JIS_Yen;
  155. // keymap[0x5E] = TAUK_JIS_Underscore;
  156. // keymap[0x5F] = TAUK_JIS_KeypadComma;
  157. // keymap[0x66] = TAUK_JIS_Eisu;
  158. // keymap[0x68] = TAUK_JIS_Kana;
  159. }
  160. -(void) setCurrentCursor: (NSCursor*) newCursor
  161. {
  162. currentCursor = newCursor;
  163. }
  164. - (void)resetCursorRects
  165. {
  166. if(currentCursor)
  167. [self addCursorRect:[self bounds] cursor:currentCursor];
  168. }
  169. - (void) setCore: (CocoaCore*) newCore {
  170. [self initKeymap];
  171. core = newCore;
  172. }
  173. - (BOOL) lockFocusIfCanDraw {
  174. [contextLock lock];
  175. BOOL retVal = [super lockFocusIfCanDraw];
  176. [contextLock unlock];
  177. return retVal;
  178. }
  179. - (void) update {
  180. [contextLock lock];
  181. [super update];
  182. [contextLock unlock];
  183. }
  184. - (void) lockContext {
  185. [contextLock lock];
  186. }
  187. - (void) unlockContext {
  188. [contextLock unlock];
  189. }
  190. - (void)prepareOpenGL {
  191. contextReady = YES;
  192. }
  193. - (BOOL) isContextReady {
  194. return contextReady;
  195. }
  196. - (void)setOpenGLContext:(NSOpenGLContext *)context {
  197. contextReady = NO;
  198. [super setOpenGLContext:context];
  199. }
  200. - (BOOL)acceptsFirstResponder
  201. {
  202. return YES;
  203. }
  204. // ---------------------------------
  205. - (BOOL)becomeFirstResponder
  206. {
  207. return YES;
  208. }
  209. // ---------------------------------
  210. - (BOOL)resignFirstResponder
  211. {
  212. return YES;
  213. }
  214. - (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
  215. {
  216. return YES;
  217. }
  218. - (BOOL)mouseDownCanMoveWindow
  219. {
  220. return NO;
  221. }
  222. - (void) drawRect:(NSRect)rect {
  223. }
  224. -(void) windowResized: (NSNotification *) notification {
  225. NSSize size = [self frame].size;
  226. if(core == NULL)
  227. return;
  228. core->resizeTo(size.width, size.height);
  229. }
  230. -(void) dealloc {
  231. [[NSNotificationCenter defaultCenter] removeObserver:self];
  232. [super dealloc];
  233. }
  234. - (void) resizeWithOldSuperviewSize: (NSSize) oldSize {
  235. [super resizeWithOldSuperviewSize: oldSize];
  236. // if(core == NULL)
  237. // return;
  238. // core->setVideoMode(oldSize.width, oldSize.height, core->isFullscreen(), core->getAALevel());
  239. }
  240. // INPUT
  241. - (void) otherMouseDown:(NSEvent *) event
  242. {
  243. if(core == NULL)
  244. return;
  245. core->lockMutex(core->eventMutex);
  246. NSPoint mouseLoc = [self convertPoint:[event locationInWindow] fromView:self];
  247. CocoaEvent newEvent;
  248. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  249. newEvent.eventCode = InputEvent::EVENT_MOUSEDOWN;
  250. newEvent.mouseButton = CoreInput::MOUSE_BUTTON3;
  251. newEvent.mouseX = mouseLoc.x;
  252. newEvent.mouseY = core->getYRes() - mouseLoc.y;
  253. core->cocoaEvents.push_back(newEvent);
  254. core->unlockMutex(core->eventMutex);
  255. }
  256. - (void) otherMouseUp:(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_MOUSEUP;
  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) rightMouseDown:(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_MOUSEDOWN;
  280. newEvent.mouseButton = CoreInput::MOUSE_BUTTON2;
  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) rightMouseUp:(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_MOUSEUP;
  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)scrollWheel:(NSEvent*) event {
  302. if(core == NULL || [event deltaY] == 0)
  303. return;
  304. core->lockMutex(core->eventMutex);
  305. NSPoint mouseLoc = [self convertPoint:[event locationInWindow] fromView:self];
  306. CocoaEvent newEvent;
  307. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  308. if([event deltaY] > 0)
  309. newEvent.eventCode = InputEvent::EVENT_MOUSEWHEEL_UP;
  310. else
  311. newEvent.eventCode = InputEvent::EVENT_MOUSEWHEEL_DOWN;
  312. newEvent.mouseX = mouseLoc.x;
  313. newEvent.mouseY = core->getYRes() - mouseLoc.y;
  314. core->cocoaEvents.push_back(newEvent);
  315. core->unlockMutex(core->eventMutex);
  316. }
  317. - (void) mouseDown:(NSEvent *) event
  318. {
  319. if(core == NULL)
  320. return;
  321. core->lockMutex(core->eventMutex);
  322. NSPoint mouseLoc = [self convertPoint:[event locationInWindow] fromView:self];
  323. CocoaEvent newEvent;
  324. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  325. newEvent.eventCode = InputEvent::EVENT_MOUSEDOWN;
  326. newEvent.mouseButton = CoreInput::MOUSE_BUTTON1;
  327. newEvent.mouseX = mouseLoc.x;
  328. newEvent.mouseY = core->getYRes() - mouseLoc.y;
  329. mouseX = mouseLoc.x;
  330. mouseY = mouseLoc.y;
  331. core->cocoaEvents.push_back(newEvent);
  332. core->unlockMutex(core->eventMutex);
  333. }
  334. - (void) mouseUp:(NSEvent *) event
  335. {
  336. if(core == NULL)
  337. return;
  338. core->lockMutex(core->eventMutex);
  339. NSPoint mouseLoc = [self convertPoint:[event locationInWindow] fromView:self];
  340. CocoaEvent newEvent;
  341. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  342. newEvent.eventCode = InputEvent::EVENT_MOUSEUP;
  343. newEvent.mouseButton = CoreInput::MOUSE_BUTTON1;
  344. newEvent.mouseX = mouseLoc.x;
  345. newEvent.mouseY = core->getYRes() - mouseLoc.y;
  346. mouseX = mouseLoc.x;
  347. mouseY = mouseLoc.y;
  348. core->cocoaEvents.push_back(newEvent);
  349. core->unlockMutex(core->eventMutex);
  350. }
  351. - (void)flagsChanged:(NSEvent *)theEvent
  352. {
  353. if(core == NULL)
  354. return;
  355. // NSLog(@"KEY: %x\n", [theEvent keyCode]);
  356. core->lockMutex(core->eventMutex);
  357. CocoaEvent newEvent;
  358. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  359. newEvent.keyCode = keymap[[theEvent keyCode]];
  360. if(modifierMap[[theEvent keyCode]] == 0) {
  361. modifierMap[[theEvent keyCode]] = 1;
  362. newEvent.eventCode = InputEvent::EVENT_KEYDOWN;
  363. } else {
  364. modifierMap[[theEvent keyCode]] = 0;
  365. newEvent.eventCode = InputEvent::EVENT_KEYUP;
  366. }
  367. newEvent.unicodeChar = 0;
  368. core->cocoaEvents.push_back(newEvent);
  369. core->unlockMutex(core->eventMutex);
  370. }
  371. - (void)keyDown: (NSEvent*) theEvent
  372. {
  373. if(core == NULL)
  374. return;
  375. core->lockMutex(core->eventMutex);
  376. CocoaEvent newEvent;
  377. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  378. newEvent.eventCode = InputEvent::EVENT_KEYDOWN;
  379. newEvent.keyCode = keymap[[theEvent keyCode]];
  380. NSString *chars = [theEvent characters];
  381. unsigned int numChars = [chars length];
  382. // NSLog(@"CHARS: %@", [chars characterAtIndex:0]);
  383. if(numChars > 0) {
  384. newEvent.unicodeChar = [chars characterAtIndex:0];
  385. } else {
  386. newEvent.unicodeChar = 0;
  387. }
  388. core->cocoaEvents.push_back(newEvent);
  389. core->unlockMutex(core->eventMutex);
  390. }
  391. - (void)keyUp: (NSEvent*) theEvent
  392. {
  393. if(core == NULL)
  394. return;
  395. core->lockMutex(core->eventMutex);
  396. NSPoint mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:self];
  397. CocoaEvent newEvent;
  398. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  399. newEvent.eventCode = InputEvent::EVENT_KEYUP;
  400. newEvent.keyCode = keymap[[theEvent keyCode]];
  401. NSString *chars = [theEvent characters];
  402. unsigned int numChars = [chars length];
  403. if(numChars > 0)
  404. newEvent.unicodeChar = [chars characterAtIndex:0];
  405. else
  406. newEvent.unicodeChar = 0;
  407. core->cocoaEvents.push_back(newEvent);
  408. core->unlockMutex(core->eventMutex);
  409. }
  410. - (void)mouseDragged:(NSEvent *)theEvent
  411. {
  412. if(core == NULL)
  413. return;
  414. core->lockMutex(core->eventMutex);
  415. NSPoint mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:self];
  416. CocoaEvent newEvent;
  417. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  418. newEvent.eventCode = InputEvent::EVENT_MOUSEMOVE;
  419. newEvent.mouseX = mouseLoc.x;
  420. newEvent.mouseY = core->getYRes() - mouseLoc.y;
  421. core->cocoaEvents.push_back(newEvent);
  422. core->unlockMutex(core->eventMutex);
  423. }
  424. - (void)mouseMoved:(NSEvent *)theEvent
  425. {
  426. if(core == NULL)
  427. return;
  428. core->lockMutex(core->eventMutex);
  429. NSPoint mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:self];
  430. CocoaEvent newEvent;
  431. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  432. newEvent.eventCode = InputEvent::EVENT_MOUSEMOVE;
  433. newEvent.mouseX = mouseLoc.x;
  434. newEvent.mouseY = core->getYRes() - mouseLoc.y;
  435. mouseX = mouseLoc.x;
  436. mouseY = mouseLoc.y;
  437. core->cocoaEvents.push_back(newEvent);
  438. core->unlockMutex(core->eventMutex);
  439. }
  440. @end