Просмотр исходного кода

Added native touch support on Mac

Ivan Safrin 10 лет назад
Родитель
Сommit
94bfec4af7

+ 3 - 0
Core/Contents/Include/PolyCocoaCore.h

@@ -59,6 +59,9 @@ namespace Polycode {
 		int mouseX;
 		int mouseY;
 		
+        std::vector<TouchInfo> touches;
+        TouchInfo touch;
+        
 		PolyKEY keyCode;
 		wchar_t unicodeChar;
 		

+ 151 - 1
Core/Contents/PolycodeView/Mac OS X/PolycodeView.mm

@@ -46,7 +46,9 @@
 	contextLock = [[NSLock alloc] init];
 	
 	[[self window] setAcceptsMouseMovedEvents:YES];
-	[[self window] makeFirstResponder:self];	
+	[[self window] makeFirstResponder:self];
+    
+    [self setAcceptsTouchEvents:YES];
 
 	[[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(windowResized:) name:NSWindowDidResizeNotification object:[self window]];
 	
@@ -417,6 +419,154 @@
 	
 }
 
+- (void)touchesBeganWithEvent:(NSEvent *)event
+{
+    if(core == NULL)
+        return;
+    
+    core->lockMutex(core->eventMutex);
+
+    NSSet *touches = [event touchesMatchingPhase:NSTouchPhaseAny inView:self];
+    NSArray *touchArray = [touches allObjects];
+    
+    std::vector<TouchInfo> touchInfos;
+    
+    for(NSTouch *touch in touchArray) {
+        TouchInfo info;
+        long touchID = (long)[touch identity];
+        info.id = touchID;
+        info.position.x = [touch normalizedPosition].x * core->getXRes();
+        info.position.y = (1.0-[touch normalizedPosition].y) * core->getYRes();
+        touchInfos.push_back(info);
+    }
+
+    unsigned int index = 0;
+    for(NSTouch *touch in touchArray) {
+        if([touch phase] == NSTouchPhaseBegan) {
+            CocoaEvent newEvent;
+            newEvent.eventCode = InputEvent::EVENT_TOUCHES_BEGAN;
+            newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
+            newEvent.touches = touchInfos;
+            newEvent.touch = touchInfos[index];
+            core->cocoaEvents.push_back(newEvent);
+        }
+        index++;
+    }
+    core->unlockMutex(core->eventMutex);	
+
+}
+
+- (void)touchesMovedWithEvent:(NSEvent *)event
+{
+    
+    if(core == NULL)
+        return;
+    
+    core->lockMutex(core->eventMutex);
+    
+    NSSet *touches = [event touchesMatchingPhase:NSTouchPhaseAny inView:self];
+    NSArray *touchArray = [touches allObjects];
+    
+    std::vector<TouchInfo> touchInfos;
+    
+    for(NSTouch *touch in touchArray) {
+        TouchInfo info;
+        long touchID = (long)[touch identity];
+        info.id = touchID;
+        info.position.x = [touch normalizedPosition].x * core->getXRes();
+        info.position.y = (1.0-[touch normalizedPosition].y) * core->getYRes();
+        touchInfos.push_back(info);
+    }
+    
+    unsigned int index = 0;
+    for(NSTouch *touch in touchArray) {
+        if([touch phase] == NSTouchPhaseMoved) {
+            CocoaEvent newEvent;
+            newEvent.eventCode = InputEvent::EVENT_TOUCHES_MOVED;
+            newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
+            newEvent.touches = touchInfos;
+            newEvent.touch = touchInfos[index];
+            core->cocoaEvents.push_back(newEvent);
+        }
+        index++;
+    }
+    core->unlockMutex(core->eventMutex);
+    
+}
+
+- (void)touchesEndedWithEvent:(NSEvent *)event
+{
+    if(core == NULL)
+        return;
+    
+    core->lockMutex(core->eventMutex);
+    
+    NSSet *touches = [event touchesMatchingPhase:NSTouchPhaseAny inView:self];
+    NSArray *touchArray = [touches allObjects];
+    
+    std::vector<TouchInfo> touchInfos;
+    
+    for(NSTouch *touch in touchArray) {
+        TouchInfo info;
+        long touchID = (long)[touch identity];
+        info.id = touchID;
+        info.position.x = [touch normalizedPosition].x * core->getXRes();
+        info.position.y = (1.0-[touch normalizedPosition].y) * core->getYRes();
+        touchInfos.push_back(info);
+    }
+    
+    unsigned int index = 0;
+    for(NSTouch *touch in touchArray) {
+        if([touch phase] == NSTouchPhaseEnded) {
+            CocoaEvent newEvent;
+            newEvent.eventCode = InputEvent::EVENT_TOUCHES_ENDED;
+            newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
+            newEvent.touches = touchInfos;
+            newEvent.touch = touchInfos[index];
+            core->cocoaEvents.push_back(newEvent);
+        }
+        index++;
+    }
+    core->unlockMutex(core->eventMutex);
+    
+}
+
+- (void)touchesCancelledWithEvent:(NSEvent *)event
+{
+    if(core == NULL)
+        return;
+    
+    core->lockMutex(core->eventMutex);
+    
+    NSSet *touches = [event touchesMatchingPhase:NSTouchPhaseAny inView:self];
+    NSArray *touchArray = [touches allObjects];
+    
+    std::vector<TouchInfo> touchInfos;
+    
+    for(NSTouch *touch in touchArray) {
+        TouchInfo info;
+        long touchID = (long)[touch identity];
+        info.id = touchID;
+        info.position.x = [touch normalizedPosition].x * core->getXRes();
+        info.position.y = (1.0-[touch normalizedPosition].y) * core->getYRes();
+        touchInfos.push_back(info);
+    }
+    
+    unsigned int index = 0;
+    for(NSTouch *touch in touchArray) {
+        if([touch phase] == NSTouchPhaseCancelled) {
+            CocoaEvent newEvent;
+            newEvent.eventCode = InputEvent::EVENT_TOUCHES_ENDED;
+            newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
+            newEvent.touches = touchInfos;
+            newEvent.touch = touchInfos[index];
+            core->cocoaEvents.push_back(newEvent);
+        }
+        index++;
+    }
+    core->unlockMutex(core->eventMutex);
+}
+
 - (void) mouseDown:(NSEvent *) event
 {
 	

+ 10 - 1
Core/Contents/Source/PolyCocoaCore.mm

@@ -509,7 +509,16 @@ void CocoaCore::checkEvents() {
 						break;
 					case InputEvent::EVENT_KEYUP:
 						input->setKeyState(event.keyCode, event.unicodeChar, false, getTicks());
-						break;						
+                    break;
+                    case InputEvent::EVENT_TOUCHES_BEGAN:
+                        input->touchesBegan(event.touch, event.touches, getTicks());
+                        break;
+                    case InputEvent::EVENT_TOUCHES_ENDED:
+                        input->touchesEnded(event.touch, event.touches, getTicks());
+                        break;
+                    case InputEvent::EVENT_TOUCHES_MOVED:
+                        input->touchesMoved(event.touch, event.touches, getTicks());
+                    break;
 				}
 				break;
 				case CocoaEvent::FOCUS_EVENT: