| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550 |
- /*
- Copyright (C) 2011 by Ivan Safrin
-
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
-
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
-
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- */
- #import "PolycodeView.h"
- @implementation PolycodeView
- @synthesize viewReady;
- @synthesize mouseX;
- @synthesize mouseY;
- - (id)initWithFrame:(NSRect)frameRect pixelFormat:(NSOpenGLPixelFormat *)format
- {
- self = [super initWithFrame:frameRect pixelFormat:format];
- if(self) {
- glSizeX = 0;
- glSizeY = 0;
- viewReady = NO;
- currentCursor = NULL;
- contextLock = [[NSLock alloc] init];
- }
- return self;
- }
- - (void) awakeFromNib {
- contextLock = [[NSLock alloc] init];
-
- [[self window] setAcceptsMouseMovedEvents:YES];
- [[self window] makeFirstResponder:self];
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowResized:) name:NSWindowDidResizeNotification object:[self window]];
-
- [self setAutoresizesSubviews: YES];
- }
- -(void) viewDidMoveToWindow
- {
- [super viewDidMoveToWindow];
- viewReady = YES;
- }
- - (void) initKeymap {
-
- for(int i=0; i < 512; i++) {
- modifierMap[i] = 0;
- }
-
- keymap[0x00] = KEY_a;
- keymap[0x01] = KEY_s;
- keymap[0x02] = KEY_d;
- keymap[0x03] = KEY_f;
- keymap[0x04] = KEY_g;
- keymap[0x05] = KEY_h;
- keymap[0x06] = KEY_z;
- keymap[0x07] = KEY_x;
- keymap[0x08] = KEY_c;
- keymap[0x09] = KEY_v;
- keymap[0x0B] = KEY_b;
- keymap[0x0C] = KEY_q;
- keymap[0x0D] = KEY_w;
- keymap[0x0E] = KEY_e;
- keymap[0x0F] = KEY_r;
- keymap[0x10] = KEY_y;
- keymap[0x11] = KEY_t;
- keymap[0x12] = KEY_1;
- keymap[0x13] = KEY_2;
- keymap[0x14] = KEY_3;
- keymap[0x15] = KEY_4;
- keymap[0x16] = KEY_6;
- keymap[0x17] = KEY_5;
- keymap[0x18] = KEY_EQUALS;
- keymap[0x19] = KEY_9;
- keymap[0x1A] = KEY_7;
- keymap[0x1B] = KEY_MINUS;
- keymap[0x1C] = KEY_8;
- keymap[0x1D] = KEY_0;
- keymap[0x1E] = KEY_RIGHTBRACKET;
- keymap[0x1F] = KEY_o;
- keymap[0x20] = KEY_u;
- keymap[0x21] = KEY_LEFTBRACKET;
- keymap[0x22] = KEY_i;
- keymap[0x23] = KEY_p;
- keymap[0x25] = KEY_l;
- keymap[0x26] = KEY_j;
- keymap[0x27] = KEY_QUOTE;
- keymap[0x28] = KEY_k;
- keymap[0x29] = KEY_SEMICOLON;
- keymap[0x2A] = KEY_BACKSLASH;
- keymap[0x2B] = KEY_COMMA;
- keymap[0x2C] = KEY_SLASH;
- keymap[0x2D] = KEY_n;
- keymap[0x2E] = KEY_m;
- keymap[0x2F] = KEY_PERIOD;
- // keymap[0x32] = KEY_GRAVE;
- keymap[0x41] = KEY_KP_PERIOD;
- keymap[0x43] = KEY_KP_MULTIPLY;
- keymap[0x45] = KEY_KP_PLUS;
- // keymap[0x47] = KEY_KeypadClear;
- keymap[0x4B] = KEY_KP_DIVIDE;
- keymap[0x4C] = KEY_KP_ENTER;
- keymap[0x4E] = KEY_KP_MINUS;
- keymap[0x51] = KEY_KP_EQUALS;
- keymap[0x52] = KEY_KP0;
- keymap[0x53] = KEY_KP1;
- keymap[0x54] = KEY_KP2;
- keymap[0x55] = KEY_KP3;
- keymap[0x56] = KEY_KP4;
- keymap[0x57] = KEY_KP5;
- keymap[0x58] = KEY_KP6;
- keymap[0x59] = KEY_KP7;
- keymap[0x5B] = KEY_KP8;
- keymap[0x5C] = KEY_KP9;
- keymap[0x24] = KEY_RETURN;
- keymap[0x30] = KEY_TAB;
- keymap[0x31] = KEY_SPACE;
- keymap[0x33] = KEY_BACKSPACE;
- keymap[0x35] = KEY_ESCAPE;
- keymap[0x37] = KEY_LSUPER;
- keymap[0x36] = KEY_RSUPER;
- keymap[0x38] = KEY_LSHIFT;
- keymap[0x39] = KEY_CAPSLOCK;
- keymap[0x3A] = KEY_LALT;
- keymap[0x3D] = KEY_RALT;
- keymap[0x3B] = KEY_LCTRL;
- keymap[0x3C] = KEY_RSHIFT;
- keymap[0x3E] = KEY_RCTRL;
- keymap[0x3F] = KEY_COMPOSE;
- // keymap[0x40] = KEY_F17;
- // keymap[0x48] = KEY_VolumeUp;
- // keymap[0x49] = KEY_VolumeDown;
- // keymap[0x4A] = KEY_Mute;
- // keymap[0x4F] = KEY_F18;
- // keymap[0x50] = KEY_F19;
- // keymap[0x5A] = KEY_F20;
- keymap[0x60] = KEY_F5;
- keymap[0x61] = KEY_F6;
- keymap[0x62] = KEY_F7;
- keymap[0x63] = KEY_F3;
- keymap[0x64] = KEY_F8;
- keymap[0x65] = KEY_F9;
- keymap[0x67] = KEY_F11;
- keymap[0x69] = KEY_F13;
- // keymap[0x6A] = KEY_F16;
- keymap[0x6B] = KEY_F14;
- keymap[0x6D] = KEY_F10;
- keymap[0x6F] = KEY_F12;
- keymap[0x71] = KEY_F15;
- keymap[0x72] = KEY_HELP;
- keymap[0x73] = KEY_HOME;
- keymap[0x74] = KEY_PAGEUP;
- keymap[0x75] = KEY_DELETE;
- keymap[0x76] = KEY_F4;
- keymap[0x77] = KEY_END;
- keymap[0x78] = KEY_F2;
- keymap[0x79] = KEY_PAGEDOWN;
- keymap[0x7A] = KEY_F1;
- keymap[0x7B] = KEY_LEFT;
- keymap[0x7C] = KEY_RIGHT;
- keymap[0x7D] = KEY_DOWN;
- keymap[0x7E] = KEY_UP;
- // keymap[0x0A] = KEY_ISO_Section;
- // keymap[0x5D] = KEY_JIS_Yen;
- // keymap[0x5E] = KEY_JIS_Underscore;
- // keymap[0x5F] = KEY_JIS_KeypadComma;
- // keymap[0x66] = KEY_JIS_Eisu;
- // keymap[0x68] = KEY_JIS_Kana;
- }
- -(void) setCurrentCursor: (NSCursor*) newCursor
- {
- currentCursor = newCursor;
- }
- - (void)resetCursorRects
- {
- if(currentCursor)
- [self addCursorRect:[self bounds] cursor:currentCursor];
- }
- - (void) setCore: (CocoaCore*) newCore {
- [self initKeymap];
- core = newCore;
- }
- - (BOOL) lockFocusIfCanDraw {
- [contextLock lock];
- BOOL retVal = [super lockFocusIfCanDraw];
- [contextLock unlock];
- return retVal;
- }
- - (void) update {
- [contextLock lock];
- [super update];
- [contextLock unlock];
- }
- - (void) lockContext {
- [contextLock lock];
- }
- - (void) unlockContext {
- [contextLock unlock];
- }
- - (void)prepareOpenGL {
- contextReady = YES;
- }
- - (BOOL) isContextReady {
- return contextReady;
- }
- - (void)setOpenGLContext:(NSOpenGLContext *)context {
- contextReady = NO;
- [super setOpenGLContext:context];
- }
- - (BOOL)acceptsFirstResponder
- {
- return YES;
- }
- // ---------------------------------
- - (BOOL)becomeFirstResponder
- {
- return YES;
- }
- // ---------------------------------
- - (BOOL)resignFirstResponder
- {
- return YES;
- }
- - (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
- {
- return YES;
- }
- - (BOOL)mouseDownCanMoveWindow
- {
- return NO;
- }
- - (void) drawRect:(NSRect)rect {
- if(glSizeX != (int)[self frame].size.width || glSizeY != (int)[self frame].size.height) {
- glSizeX = (int)[self frame].size.width;
- glSizeY = (int)[self frame].size.height;
-
- if(core != NULL) {
- core->resizeTo(glSizeX, glSizeY);
- }
-
- }
- }
- -(void) windowResized: (NSNotification *) notification {
- }
- -(void) dealloc {
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- [super dealloc];
-
- }
- - (void) resizeWithOldSuperviewSize: (NSSize) oldSize {
- [super resizeWithOldSuperviewSize: oldSize];
- // if(core == NULL)
- // return;
- // core->setVideoMode(oldSize.width, oldSize.height, core->isFullscreen(), core->getAALevel());
- }
- // INPUT
- - (void) otherMouseDown:(NSEvent *) event
- {
- if(core == NULL)
- return;
-
- core->lockMutex(core->eventMutex);
- NSPoint mouseLoc = [self convertPoint:[event locationInWindow] fromView:self];
- CocoaEvent newEvent;
- newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
- newEvent.eventCode = InputEvent::EVENT_MOUSEDOWN;
- newEvent.mouseButton = CoreInput::MOUSE_BUTTON3;
- newEvent.mouseX = mouseLoc.x;
- newEvent.mouseY = core->getYRes() - mouseLoc.y;
- core->cocoaEvents.push_back(newEvent);
- core->unlockMutex(core->eventMutex);
- }
- - (void) otherMouseUp:(NSEvent *) event
- {
- if(core == NULL)
- return;
-
- core->lockMutex(core->eventMutex);
- NSPoint mouseLoc = [self convertPoint:[event locationInWindow] fromView:self];
- CocoaEvent newEvent;
- newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
- newEvent.eventCode = InputEvent::EVENT_MOUSEUP;
- newEvent.mouseButton = CoreInput::MOUSE_BUTTON3;
- newEvent.mouseX = mouseLoc.x;
- newEvent.mouseY = core->getYRes() - mouseLoc.y;
- core->cocoaEvents.push_back(newEvent);
- core->unlockMutex(core->eventMutex);
- }
- - (void) rightMouseDown:(NSEvent *) event
- {
- if(core == NULL)
- return;
-
- core->lockMutex(core->eventMutex);
- NSPoint mouseLoc = [self convertPoint:[event locationInWindow] fromView:self];
- CocoaEvent newEvent;
- newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
- newEvent.eventCode = InputEvent::EVENT_MOUSEDOWN;
- newEvent.mouseButton = CoreInput::MOUSE_BUTTON2;
- newEvent.mouseX = mouseLoc.x;
- newEvent.mouseY = core->getYRes() - mouseLoc.y;
- core->cocoaEvents.push_back(newEvent);
- core->unlockMutex(core->eventMutex);
- }
- - (void) rightMouseUp:(NSEvent *) event
- {
- if(core == NULL)
- return;
-
- core->lockMutex(core->eventMutex);
- NSPoint mouseLoc = [self convertPoint:[event locationInWindow] fromView:self];
- CocoaEvent newEvent;
- newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
- newEvent.eventCode = InputEvent::EVENT_MOUSEUP;
- newEvent.mouseButton = CoreInput::MOUSE_BUTTON2;
- newEvent.mouseX = mouseLoc.x;
- newEvent.mouseY = core->getYRes() - mouseLoc.y;
- core->cocoaEvents.push_back(newEvent);
- core->unlockMutex(core->eventMutex);
- }
- - (void)scrollWheel:(NSEvent*) event {
-
- if(core == NULL || [event deltaY] == 0)
- return;
-
- core->lockMutex(core->eventMutex);
- NSPoint mouseLoc = [self convertPoint:[event locationInWindow] fromView:self];
- CocoaEvent newEvent;
- newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
-
- if([event deltaY] > 0)
- newEvent.eventCode = InputEvent::EVENT_MOUSEWHEEL_UP;
- else
- newEvent.eventCode = InputEvent::EVENT_MOUSEWHEEL_DOWN;
- newEvent.mouseX = mouseLoc.x;
- newEvent.mouseY = core->getYRes() - mouseLoc.y;
- core->cocoaEvents.push_back(newEvent);
- core->unlockMutex(core->eventMutex);
-
- }
- - (void) mouseDown:(NSEvent *) event
- {
-
- if(core == NULL)
- return;
-
- core->lockMutex(core->eventMutex);
- NSPoint mouseLoc = [self convertPoint:[event locationInWindow] fromView:self];
- CocoaEvent newEvent;
- newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
- newEvent.eventCode = InputEvent::EVENT_MOUSEDOWN;
- newEvent.mouseButton = CoreInput::MOUSE_BUTTON1;
- newEvent.mouseX = mouseLoc.x;
- newEvent.mouseY = core->getYRes() - mouseLoc.y;
-
- mouseX = mouseLoc.x;
- mouseY = mouseLoc.y;
-
-
- core->cocoaEvents.push_back(newEvent);
- core->unlockMutex(core->eventMutex);
- }
- - (void) mouseUp:(NSEvent *) event
- {
- if(core == NULL)
- return;
-
- core->lockMutex(core->eventMutex);
- NSPoint mouseLoc = [self convertPoint:[event locationInWindow] fromView:self];
- CocoaEvent newEvent;
- newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
- newEvent.eventCode = InputEvent::EVENT_MOUSEUP;
- newEvent.mouseButton = CoreInput::MOUSE_BUTTON1;
- newEvent.mouseX = mouseLoc.x;
- newEvent.mouseY = core->getYRes() - mouseLoc.y;
-
- mouseX = mouseLoc.x;
- mouseY = mouseLoc.y;
-
-
- core->cocoaEvents.push_back(newEvent);
- core->unlockMutex(core->eventMutex);
- }
- - (void)flagsChanged:(NSEvent *)theEvent
- {
- if(core == NULL)
- return;
-
- // NSLog(@"KEY: %x\n", [theEvent keyCode]);
-
- core->lockMutex(core->eventMutex);
- CocoaEvent newEvent;
- newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
- newEvent.keyCode = keymap[[theEvent keyCode]];
-
- if(modifierMap[[theEvent keyCode]] == 0) {
- modifierMap[[theEvent keyCode]] = 1;
- newEvent.eventCode = InputEvent::EVENT_KEYDOWN;
- } else {
- modifierMap[[theEvent keyCode]] = 0;
- newEvent.eventCode = InputEvent::EVENT_KEYUP;
- }
- newEvent.unicodeChar = 0;
-
-
- core->cocoaEvents.push_back(newEvent);
- core->unlockMutex(core->eventMutex);
- }
- - (void)keyDown: (NSEvent*) theEvent
- {
- if(core == NULL)
- return;
-
- core->lockMutex(core->eventMutex);
- CocoaEvent newEvent;
- newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
- newEvent.eventCode = InputEvent::EVENT_KEYDOWN;
- newEvent.keyCode = keymap[[theEvent keyCode]];
-
- NSString *chars = [theEvent characters];
- unsigned int numChars = [chars length];
-
- // NSLog(@"CHARS: %@", [chars characterAtIndex:0]);
- if(numChars > 0) {
- newEvent.unicodeChar = [chars characterAtIndex:0];
- } else {
- newEvent.unicodeChar = 0;
- }
-
- core->cocoaEvents.push_back(newEvent);
- core->unlockMutex(core->eventMutex);
-
- }
- - (void)keyUp: (NSEvent*) theEvent
- {
- if(core == NULL)
- return;
-
- core->lockMutex(core->eventMutex);
- NSPoint mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:self];
- CocoaEvent newEvent;
- newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
- newEvent.eventCode = InputEvent::EVENT_KEYUP;
- newEvent.keyCode = keymap[[theEvent keyCode]];
-
- NSString *chars = [theEvent characters];
- unsigned int numChars = [chars length];
-
- if(numChars > 0)
- newEvent.unicodeChar = [chars characterAtIndex:0];
- else
- newEvent.unicodeChar = 0;
-
- core->cocoaEvents.push_back(newEvent);
- core->unlockMutex(core->eventMutex);
- }
- - (void)mouseDragged:(NSEvent *)theEvent
- {
- if(core == NULL)
- return;
-
- core->lockMutex(core->eventMutex);
- NSPoint mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:self];
- CocoaEvent newEvent;
- newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
- newEvent.eventCode = InputEvent::EVENT_MOUSEMOVE;
- newEvent.mouseX = mouseLoc.x;
- newEvent.mouseY = core->getYRes() - mouseLoc.y;
- core->cocoaEvents.push_back(newEvent);
- core->unlockMutex(core->eventMutex);
- }
- - (void)mouseMoved:(NSEvent *)theEvent
- {
- if(core == NULL)
- return;
-
- core->lockMutex(core->eventMutex);
- NSPoint mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:self];
- CocoaEvent newEvent;
- newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
- newEvent.eventCode = InputEvent::EVENT_MOUSEMOVE;
- newEvent.mouseX = mouseLoc.x;
- newEvent.mouseY = core->getYRes() - mouseLoc.y;
-
- mouseX = mouseLoc.x;
- mouseY = mouseLoc.y;
-
- core->cocoaEvents.push_back(newEvent);
- core->unlockMutex(core->eventMutex);
- }
- @end
|