diff src/video/cocoa/SDL_cocoawindow.m @ 4673:c17ac64abb70

Fixed the code so we receive Cocoa touch events
author Sam Lantinga <slouken@libsdl.org>
date Wed, 28 Jul 2010 23:32:13 -0700
parents 524dfefd554c
children 89d5e2201b00
line wrap: on
line diff
--- a/src/video/cocoa/SDL_cocoawindow.m	Tue Jul 27 18:14:09 2010 +0400
+++ b/src/video/cocoa/SDL_cocoawindow.m	Wed Jul 28 23:32:13 2010 -0700
@@ -25,6 +25,7 @@
 #include "../SDL_sysvideo.h"
 #include "../../events/SDL_keyboard_c.h"
 #include "../../events/SDL_mouse_c.h"
+#include "../../events/SDL_touch_c.h"
 #include "../../events/SDL_windowevents_c.h"
 
 #include "SDL_cocoavideo.h"
@@ -60,6 +61,7 @@
     [center addObserver:self selector:@selector(windowDidUnhide:) name:NSApplicationDidUnhideNotification object:NSApp];
 
     [_data->nswindow setAcceptsMouseMovedEvents:YES];
+    [[_data->nswindow contentView] setAcceptsTouchEvents:YES];
 }
 
 - (void)close
@@ -268,6 +270,72 @@
     SDL_SendMouseWheel(_data->window, (int)x, (int)y);
 }
 
+- (void)touchesBeganWithEvent:(NSEvent *) theEvent
+{
+    [self handleTouches:COCOA_TOUCH_DOWN withEvent:theEvent];
+}
+
+- (void)touchesMovedWithEvent:(NSEvent *) theEvent
+{
+    [self handleTouches:COCOA_TOUCH_MOVE withEvent:theEvent];
+}
+
+- (void)touchesEndedWithEvent:(NSEvent *) theEvent
+{
+    [self handleTouches:COCOA_TOUCH_UP withEvent:theEvent];
+}
+
+- (void)touchesCancelledWithEvent:(NSEvent *) theEvent
+{
+    [self handleTouches:COCOA_TOUCH_CANCELLED withEvent:theEvent];
+}
+
+- (void)handleTouches:(cocoaTouchType)type withEvent:(NSEvent *)event
+{
+    NSSet *touches = [event touchesMatchingPhase:NSTouchPhaseBegan inView:nil];
+
+    NSEnumerator *enumerator = [touches objectEnumerator];
+    NSTouch *touch = (NSTouch*)[enumerator nextObject];
+    while (touch) {
+        long touchId = (long)[touch device];
+        if (!SDL_GetTouch(touchId)) {
+            SDL_Touch touch;
+
+            touch.id = touchId;
+            touch.x_min = 0;
+            touch.x_max = 1;
+            touch.xres = touch.x_max - touch.x_min;
+            touch.y_min = 0;
+            touch.y_max = 1;
+            touch.yres = touch.y_max - touch.y_min;
+            touch.pressure_min = 0;
+            touch.pressure_max = 1;
+            touch.pressureres = touch.pressure_max - touch.pressure_min;
+            
+            if (SDL_AddTouch(&touch, "") < 0) {
+                return; 
+            }
+        } 
+        float x = [touch normalizedPosition].x;
+        float y = [touch normalizedPosition].y;
+        long fingerId = (long)[touch identity];
+        switch (type) {
+        case COCOA_TOUCH_DOWN:
+            SDL_SendFingerDown(touchId, fingerId, SDL_TRUE, x, y, 1);
+            break;
+        case COCOA_TOUCH_UP:
+        case COCOA_TOUCH_CANCELLED:
+            SDL_SendFingerDown(touchId, fingerId, SDL_FALSE, x, y, 1);
+            break;
+        case COCOA_TOUCH_MOVE:
+            SDL_SendTouchMotion(touchId, fingerId, SDL_FALSE, x, y, 1);
+            break;
+        }
+        
+        touch = (NSTouch*)[enumerator nextObject];
+    }
+}
+
 @end
 
 @interface SDLWindow : NSWindow