Finished (untested) OS X code.
authorjimtla
Tue, 27 Jul 2010 05:21:24 +0400
changeset 46711f0e8f6417d9
parent 4670 ad4f32e874ee
child 4672 013b0ea263dd
Finished (untested) OS X code.
src/video/cocoa/SDL_cocoakeyboard.m
     1.1 --- a/src/video/cocoa/SDL_cocoakeyboard.m	Sat Jul 24 22:28:38 2010 +0400
     1.2 +++ b/src/video/cocoa/SDL_cocoakeyboard.m	Tue Jul 27 05:21:24 2010 +0400
     1.3 @@ -26,6 +26,10 @@
     1.4  #include "../../events/SDL_keyboard_c.h"
     1.5  #include "../../events/scancodes_darwin.h"
     1.6  
     1.7 +//Touch Code
     1.8 +#include "../../events/SDL_touch_c.h"
     1.9 +#include "SDL_cocoatouch.h"
    1.10 +
    1.11  #include <Carbon/Carbon.h>
    1.12  
    1.13  //#define DEBUG_IME NSLog
    1.14 @@ -67,6 +71,7 @@
    1.15  }
    1.16  - (void) doCommandBySelector:(SEL)myselector;
    1.17  - (void) setInputRect:(SDL_Rect *) rect;
    1.18 +- (void) handleTouches:(cocoaTouchType)type WithEvent:(NSEvent*) event;
    1.19  @end
    1.20  
    1.21  @implementation SDLTranslatorResponder
    1.22 @@ -200,22 +205,71 @@
    1.23      [self setAcceptsTouchEvents:YES];
    1.24      [self setWantsRestingTouches:YES];
    1.25      DEBUG_TOUCH(@"Initializing Cocoa Touch System....");
    1.26 -    //DEBUG_TOUCH(@"Accepts Touch events? %@",[self acceptsTouchEvents]);
    1.27 +    
    1.28    }
    1.29    return self;
    1.30  }
    1.31  
    1.32 +//Not an API function
    1.33 +- (void)handleTouches:(cocoaTouchType)type WithEvent:(NSEvent *)event {
    1.34 +  NSSet *touches = [event touchesMatchingPhase:NSTouchPhaseBegan inView:self];
    1.35 +
    1.36 +  NSEnumerator *enumerator = [touches objectEnumerator];
    1.37 +  NSTouch *touch = (NSTouch*)[enumerator nextObject];
    1.38 +  while (touch) {
    1.39 +    long touchId = (long)[touch device];
    1.40 +    if(!SDL_GetTouchIndex(touchId)) {
    1.41 +      if(Cocoa_AddTouch(touch) < 0) continue;
    1.42 +    } 
    1.43 +    float x = [touch normalizedPosition].x;
    1.44 +    float y = [touch normalizedPosition].y;
    1.45 +    long fingerId = (long)[touch identity];
    1.46 +    switch (type) {
    1.47 +    case COCOA_TOUCH_DOWN:
    1.48 +      SDL_SendFingerDown(touchId,fingerId,
    1.49 +			 SDL_TRUE,x,y,1);
    1.50 +      break;
    1.51 +    case COCOA_TOUCH_UP:
    1.52 +    case COCOA_TOUCH_CANCELLED:
    1.53 +      SDL_SendFingerDown(touchId,fingerId,
    1.54 +			 SDL_FALSE,x,y,1);
    1.55 +    case COCOA_TOUCH_MOVE:
    1.56 +      SDL_SendTouchMotion(touchId,fingerId,
    1.57 +			  SDL_FALSE,x,y,1);
    1.58 +    }
    1.59 +    
    1.60 +    touch = (NSTouch*)[enumerator nextObject];
    1.61 +  }
    1.62 +}
    1.63 +
    1.64  - (void)touchesBeganWithEvent:(NSEvent *)event {
    1.65    DEBUG_TOUCH(@"Finger Down");
    1.66 +  
    1.67 +  [self handleTouches: COCOA_TOUCH_DOWN WithEvent: event];
    1.68 +
    1.69 +  //Documentation said to call super, but examples do not
    1.70 +  //[super touchesBeganWithEvent:event]
    1.71  }
    1.72  - (void)touchesMovedWithEvent:(NSEvent *)event {
    1.73    DEBUG_TOUCH(@"Finger Moved");
    1.74 +
    1.75 +  [self handleTouches: COCOA_TOUCH_MOVE WithEvent: event];
    1.76 +
    1.77 +  //[super touchesMovedWithEvent:event]
    1.78  }
    1.79  - (void)touchesEndedWithEvent:(NSEvent *)event {
    1.80    DEBUG_TOUCH(@"Finger Up");
    1.81 +
    1.82 +  [self handleTouches: COCOA_TOUCH_UP WithEvent: event];
    1.83 +
    1.84 +  //[super touchesEndedWithEvent:event]
    1.85  }
    1.86  - (void)touchesCancelledWithEvent:(NSEvent *)event {
    1.87    DEBUG_TOUCH(@"Finger Cancelled");
    1.88 +
    1.89 +  [self handleTouches: COCOA_TOUCH_CANCELLED WithEvent: event];
    1.90 +
    1.91 +  //[super touchesCancelledWithEvent:event]
    1.92  }
    1.93  
    1.94  //Touch Code Ends --------------
    1.95 @@ -658,13 +712,14 @@
    1.96       * it to the front most window's content view */
    1.97      if (!data->fieldEdit) {
    1.98          data->fieldEdit =
    1.99 -            [[SDLTranslatorResponder alloc] initWithFrame: NSMakeRect(0.0, 0.0, 0.0, 0.0)];
   1.100 +            [[SDLTranslatorResponder alloc] initWithFrame: NSMakeRect(0.0, 0.0, 0.0, 0.0)];	
   1.101 +	DEBUG_TOUCH(@"Accepts Touch events? %i",[data->fieldEdit acceptsTouchEvents]);
   1.102      }
   1.103  
   1.104      if (![[data->fieldEdit superview] isEqual: parentView])
   1.105      {
   1.106          // DEBUG_IME(@"add fieldEdit to window contentView");
   1.107 -        [data->fieldEdit removeFromSuperview];
   1.108 +       [data->fieldEdit removeFromSuperview];
   1.109          [parentView addSubview: data->fieldEdit];
   1.110          [[NSApp keyWindow] makeFirstResponder: data->fieldEdit];
   1.111      }
   1.112 @@ -753,6 +808,29 @@
   1.113      }
   1.114  }
   1.115  
   1.116 +Cocoa_AddTouch(NSTouch* finger) {  
   1.117 +  SDL_Touch touch;
   1.118 +  touch.id = (long)[finger device]; 
   1.119 +  //NSSize size = [finger deviceSize];
   1.120 +  //touch.driverdata = SDL_malloc(sizeof(EventTouchData));
   1.121 +  //EventTouchData* data = (EventTouchData*)(touch.driverdata);
   1.122 +  
   1.123 +  touch.x_min = 0;
   1.124 +  touch.x_max = 1;
   1.125 +  touch.xres = touch.x_max - touch.x_min;
   1.126 +  touch.y_min = 0;
   1.127 +  touch.y_max = 1;
   1.128 +  touch.yres = touch.y_max - touch.y_min;
   1.129 +  touch.pressure_min = 0;
   1.130 +  touch.pressure_max = 1;
   1.131 +  touch.pressureres = touch.pressure_max - touch.pressure_min;
   1.132 +  
   1.133 +  
   1.134 +  return SDL_AddTouch(&touch, ""); 
   1.135 +
   1.136 +}
   1.137 +
   1.138 +
   1.139  void
   1.140  Cocoa_QuitKeyboard(_THIS)
   1.141  {