tvOS: Expose remote swipe gestures as arrow key presses (thanks oviano!)
authorAlex Szpakowski <slime73@gmail.com>
Sun, 18 Dec 2016 13:05:14 -0400
changeset 10710a6babd973955
parent 10709 5c8870c092ed
child 10711 838b7f845f84
tvOS: Expose remote swipe gestures as arrow key presses (thanks oviano!)
src/video/uikit/SDL_uikitview.m
     1.1 --- a/src/video/uikit/SDL_uikitview.m	Sun Dec 18 12:28:28 2016 -0400
     1.2 +++ b/src/video/uikit/SDL_uikitview.m	Sun Dec 18 13:05:14 2016 -0400
     1.3 @@ -42,6 +42,25 @@
     1.4  - (instancetype)initWithFrame:(CGRect)frame
     1.5  {
     1.6      if ((self = [super initWithFrame:frame])) {
     1.7 +        /* Apple TV Remote touchpad swipe gestures. */
     1.8 +#if TARGET_OS_TV
     1.9 +        UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
    1.10 +        swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
    1.11 +        [self addGestureRecognizer:swipeUp];
    1.12 +
    1.13 +        UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
    1.14 +        swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
    1.15 +        [self addGestureRecognizer:swipeDown];
    1.16 +
    1.17 +        UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
    1.18 +        swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    1.19 +        [self addGestureRecognizer:swipeLeft];
    1.20 +
    1.21 +        UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
    1.22 +        swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
    1.23 +        [self addGestureRecognizer:swipeRight];
    1.24 +#endif
    1.25 +
    1.26          self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    1.27          self.autoresizesSubviews = YES;
    1.28  
    1.29 @@ -264,6 +283,35 @@
    1.30  }
    1.31  #endif /* TARGET_OS_TV || defined(__IPHONE_9_1) */
    1.32  
    1.33 +#if TARGET_OS_TV
    1.34 +-(void)swipeGesture:(UISwipeGestureRecognizer *)gesture
    1.35 +{
    1.36 +    /* Swipe gestures don't trigger begin states. */
    1.37 +    if (gesture.state == UIGestureRecognizerStateEnded) {
    1.38 +        /* Send arrow key presses for now, as we don't have an external API
    1.39 +         * which better maps to swipe gestures. */
    1.40 +        switch (gesture.direction) {
    1.41 +        case UISwipeGestureRecognizerDirectionUp:
    1.42 +            SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_UP);
    1.43 +            SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_UP);
    1.44 +            break;
    1.45 +        case UISwipeGestureRecognizerDirectionDown:
    1.46 +            SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_DOWN);
    1.47 +            SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_DOWN);
    1.48 +            break;
    1.49 +        case UISwipeGestureRecognizerDirectionLeft:
    1.50 +            SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_LEFT);
    1.51 +            SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_LEFT);
    1.52 +            break;
    1.53 +        case UISwipeGestureRecognizerDirectionRight:
    1.54 +            SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_RIGHT);
    1.55 +            SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_RIGHT);
    1.56 +            break;
    1.57 +        }
    1.58 +    }
    1.59 +}
    1.60 +#endif /* TARGET_OS_TV */
    1.61 +
    1.62  @end
    1.63  
    1.64  #endif /* SDL_VIDEO_DRIVER_UIKIT */