src/video/cocoa/SDL_cocoawindow.m
changeset 4673 c17ac64abb70
parent 4503 524dfefd554c
child 4674 89d5e2201b00
     1.1 --- a/src/video/cocoa/SDL_cocoawindow.m	Tue Jul 27 18:14:09 2010 +0400
     1.2 +++ b/src/video/cocoa/SDL_cocoawindow.m	Wed Jul 28 23:32:13 2010 -0700
     1.3 @@ -25,6 +25,7 @@
     1.4  #include "../SDL_sysvideo.h"
     1.5  #include "../../events/SDL_keyboard_c.h"
     1.6  #include "../../events/SDL_mouse_c.h"
     1.7 +#include "../../events/SDL_touch_c.h"
     1.8  #include "../../events/SDL_windowevents_c.h"
     1.9  
    1.10  #include "SDL_cocoavideo.h"
    1.11 @@ -60,6 +61,7 @@
    1.12      [center addObserver:self selector:@selector(windowDidUnhide:) name:NSApplicationDidUnhideNotification object:NSApp];
    1.13  
    1.14      [_data->nswindow setAcceptsMouseMovedEvents:YES];
    1.15 +    [[_data->nswindow contentView] setAcceptsTouchEvents:YES];
    1.16  }
    1.17  
    1.18  - (void)close
    1.19 @@ -268,6 +270,72 @@
    1.20      SDL_SendMouseWheel(_data->window, (int)x, (int)y);
    1.21  }
    1.22  
    1.23 +- (void)touchesBeganWithEvent:(NSEvent *) theEvent
    1.24 +{
    1.25 +    [self handleTouches:COCOA_TOUCH_DOWN withEvent:theEvent];
    1.26 +}
    1.27 +
    1.28 +- (void)touchesMovedWithEvent:(NSEvent *) theEvent
    1.29 +{
    1.30 +    [self handleTouches:COCOA_TOUCH_MOVE withEvent:theEvent];
    1.31 +}
    1.32 +
    1.33 +- (void)touchesEndedWithEvent:(NSEvent *) theEvent
    1.34 +{
    1.35 +    [self handleTouches:COCOA_TOUCH_UP withEvent:theEvent];
    1.36 +}
    1.37 +
    1.38 +- (void)touchesCancelledWithEvent:(NSEvent *) theEvent
    1.39 +{
    1.40 +    [self handleTouches:COCOA_TOUCH_CANCELLED withEvent:theEvent];
    1.41 +}
    1.42 +
    1.43 +- (void)handleTouches:(cocoaTouchType)type withEvent:(NSEvent *)event
    1.44 +{
    1.45 +    NSSet *touches = [event touchesMatchingPhase:NSTouchPhaseBegan inView:nil];
    1.46 +
    1.47 +    NSEnumerator *enumerator = [touches objectEnumerator];
    1.48 +    NSTouch *touch = (NSTouch*)[enumerator nextObject];
    1.49 +    while (touch) {
    1.50 +        long touchId = (long)[touch device];
    1.51 +        if (!SDL_GetTouch(touchId)) {
    1.52 +            SDL_Touch touch;
    1.53 +
    1.54 +            touch.id = touchId;
    1.55 +            touch.x_min = 0;
    1.56 +            touch.x_max = 1;
    1.57 +            touch.xres = touch.x_max - touch.x_min;
    1.58 +            touch.y_min = 0;
    1.59 +            touch.y_max = 1;
    1.60 +            touch.yres = touch.y_max - touch.y_min;
    1.61 +            touch.pressure_min = 0;
    1.62 +            touch.pressure_max = 1;
    1.63 +            touch.pressureres = touch.pressure_max - touch.pressure_min;
    1.64 +            
    1.65 +            if (SDL_AddTouch(&touch, "") < 0) {
    1.66 +                return; 
    1.67 +            }
    1.68 +        } 
    1.69 +        float x = [touch normalizedPosition].x;
    1.70 +        float y = [touch normalizedPosition].y;
    1.71 +        long fingerId = (long)[touch identity];
    1.72 +        switch (type) {
    1.73 +        case COCOA_TOUCH_DOWN:
    1.74 +            SDL_SendFingerDown(touchId, fingerId, SDL_TRUE, x, y, 1);
    1.75 +            break;
    1.76 +        case COCOA_TOUCH_UP:
    1.77 +        case COCOA_TOUCH_CANCELLED:
    1.78 +            SDL_SendFingerDown(touchId, fingerId, SDL_FALSE, x, y, 1);
    1.79 +            break;
    1.80 +        case COCOA_TOUCH_MOVE:
    1.81 +            SDL_SendTouchMotion(touchId, fingerId, SDL_FALSE, x, y, 1);
    1.82 +            break;
    1.83 +        }
    1.84 +        
    1.85 +        touch = (NSTouch*)[enumerator nextObject];
    1.86 +    }
    1.87 +}
    1.88 +
    1.89  @end
    1.90  
    1.91  @interface SDLWindow : NSWindow