src/video/uikit/SDL_uikitview.m
changeset 12404 eb60e952b13f
parent 12201 8bdc4d340419
child 12503 806492103856
equal deleted inserted replaced
12403:a4d730631698 12404:eb60e952b13f
    37 extern int SDL_AppleTVRemoteOpenedAsJoystick;
    37 extern int SDL_AppleTVRemoteOpenedAsJoystick;
    38 
    38 
    39 @implementation SDL_uikitview {
    39 @implementation SDL_uikitview {
    40     SDL_Window *sdlwindow;
    40     SDL_Window *sdlwindow;
    41 
    41 
    42     SDL_TouchID touchId;
    42     SDL_TouchID directTouchId;
       
    43     SDL_TouchID indirectTouchId;
       
    44 
    43     UITouch * __weak firstFingerDown;
    45     UITouch * __weak firstFingerDown;
    44 }
    46 }
    45 
    47 
    46 - (instancetype)initWithFrame:(CGRect)frame
    48 - (instancetype)initWithFrame:(CGRect)frame
    47 {
    49 {
    66 #endif
    68 #endif
    67 
    69 
    68         self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    70         self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    69         self.autoresizesSubviews = YES;
    71         self.autoresizesSubviews = YES;
    70 
    72 
       
    73         directTouchId = 1;
       
    74         indirectTouchId = 2;
       
    75 
    71 #if !TARGET_OS_TV
    76 #if !TARGET_OS_TV
    72         self.multipleTouchEnabled = YES;
    77         self.multipleTouchEnabled = YES;
       
    78         SDL_AddTouch(directTouchId, SDL_TOUCH_DEVICE_DIRECT, "");
    73 #endif
    79 #endif
    74 
       
    75         touchId = 1;
       
    76         SDL_AddTouch(touchId, "");
       
    77     }
    80     }
    78 
    81 
    79     return self;
    82     return self;
    80 }
    83 }
    81 
    84 
   133     }
   136     }
   134 
   137 
   135     sdlwindow = window;
   138     sdlwindow = window;
   136 }
   139 }
   137 
   140 
       
   141 - (SDL_TouchDeviceType)touchTypeForTouch:(UITouch *)touch
       
   142 {
       
   143 #ifdef __IPHONE_9_0
       
   144     if ([touch respondsToSelector:@selector((type))]) {
       
   145         if (touch.type == UITouchTypeIndirect) {
       
   146             return SDL_TOUCH_DEVICE_INDIRECT_RELATIVE;
       
   147         }
       
   148     }
       
   149 #endif
       
   150 
       
   151     return SDL_TOUCH_DEVICE_DIRECT;
       
   152 }
       
   153 
       
   154 - (SDL_TouchID)touchIdForType:(SDL_TouchDeviceType)type
       
   155 {
       
   156     switch (type) {
       
   157         case SDL_TOUCH_DEVICE_DIRECT:
       
   158         default:
       
   159             return directTouchId;
       
   160         case SDL_TOUCH_DEVICE_INDIRECT_RELATIVE:
       
   161             return indirectTouchId;
       
   162     }
       
   163 }
       
   164 
   138 - (CGPoint)touchLocation:(UITouch *)touch shouldNormalize:(BOOL)normalize
   165 - (CGPoint)touchLocation:(UITouch *)touch shouldNormalize:(BOOL)normalize
   139 {
   166 {
   140     CGPoint point = [touch locationInView:self];
   167     CGPoint point = [touch locationInView:self];
   141 
   168 
   142     if (normalize) {
   169     if (normalize) {
   160 }
   187 }
   161 
   188 
   162 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
   189 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
   163 {
   190 {
   164     for (UITouch *touch in touches) {
   191     for (UITouch *touch in touches) {
       
   192         SDL_TouchDeviceType touchType = [self touchTypeForTouch:touch];
       
   193         SDL_TouchID touchId = [self touchIdForType:touchType];
   165         float pressure = [self pressureForTouch:touch];
   194         float pressure = [self pressureForTouch:touch];
       
   195 
       
   196         if (SDL_AddTouch(touchId, touchType, "") < 0) {
       
   197             continue;
       
   198         }
   166 
   199 
   167         if (!firstFingerDown) {
   200         if (!firstFingerDown) {
   168             CGPoint locationInView = [self touchLocation:touch shouldNormalize:NO];
   201             CGPoint locationInView = [self touchLocation:touch shouldNormalize:NO];
   169             int clicks = (int) touch.tapCount;
   202             int clicks = (int) touch.tapCount;
   170 
   203 
   184 }
   217 }
   185 
   218 
   186 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
   219 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
   187 {
   220 {
   188     for (UITouch *touch in touches) {
   221     for (UITouch *touch in touches) {
       
   222         SDL_TouchDeviceType touchType = [self touchTypeForTouch:touch];
       
   223         SDL_TouchID touchId = [self touchIdForType:touchType];
   189         float pressure = [self pressureForTouch:touch];
   224         float pressure = [self pressureForTouch:touch];
       
   225 
       
   226         if (SDL_AddTouch(touchId, touchType, "") < 0) {
       
   227             continue;
       
   228         }
   190 
   229 
   191         if (touch == firstFingerDown) {
   230         if (touch == firstFingerDown) {
   192             /* send mouse up */
   231             /* send mouse up */
   193             int clicks = (int) touch.tapCount;
   232             int clicks = (int) touch.tapCount;
   194             SDL_SendMouseButtonClicks(sdlwindow, SDL_TOUCH_MOUSEID, SDL_RELEASED, SDL_BUTTON_LEFT, clicks);
   233             SDL_SendMouseButtonClicks(sdlwindow, SDL_TOUCH_MOUSEID, SDL_RELEASED, SDL_BUTTON_LEFT, clicks);
   207 }
   246 }
   208 
   247 
   209 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
   248 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
   210 {
   249 {
   211     for (UITouch *touch in touches) {
   250     for (UITouch *touch in touches) {
       
   251         SDL_TouchDeviceType touchType = [self touchTypeForTouch:touch];
       
   252         SDL_TouchID touchId = [self touchIdForType:touchType];
   212         float pressure = [self pressureForTouch:touch];
   253         float pressure = [self pressureForTouch:touch];
       
   254 
       
   255         if (SDL_AddTouch(touchId, touchType, "") < 0) {
       
   256             continue;
       
   257         }
   213 
   258 
   214         if (touch == firstFingerDown) {
   259         if (touch == firstFingerDown) {
   215             CGPoint locationInView = [self touchLocation:touch shouldNormalize:NO];
   260             CGPoint locationInView = [self touchLocation:touch shouldNormalize:NO];
   216 
   261 
   217             /* send moved event */
   262             /* send moved event */