PolySubstanceView.m 12 KB

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