PolySubstanceView.m 12 KB

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