21 #include "../../SDL_internal.h"
23 #if SDL_VIDEO_DRIVER_UIKIT
28 #include "../../events/SDL_mouse_c.h"
29 #include "../../events/SDL_touch_c.h"
30 #include "../../events/SDL_events_c.h"
43 UITouch * __weak firstFingerDown;
46 - (instancetype)initWithFrame:(CGRect)frame
48 if ((
self = [super initWithFrame:
frame])) {
51 UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
52 swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
53 [
self addGestureRecognizer:swipeUp];
55 UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
56 swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
57 [
self addGestureRecognizer:swipeDown];
59 UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
60 swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
61 [
self addGestureRecognizer:swipeLeft];
63 UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
64 swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
65 [
self addGestureRecognizer:swipeRight];
68 self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
69 self.autoresizesSubviews = YES;
72 self.multipleTouchEnabled = YES;
95 [
data.views removeObject:
self];
97 [
self removeFromSuperview];
100 view =
data.views.lastObject;
102 data.viewcontroller.view = view;
104 data.uiwindow.rootViewController = nil;
105 data.uiwindow.rootViewController =
data.viewcontroller;
107 [
data.uiwindow layoutIfNeeded];
115 [
data.views addObject:
self];
118 [
data.viewcontroller.view removeFromSuperview];
119 data.viewcontroller.view =
self;
126 data.uiwindow.rootViewController = nil;
127 data.uiwindow.rootViewController =
data.viewcontroller;
132 [
data.uiwindow layoutIfNeeded];
138 - (CGPoint)touchLocation:(UITouch *)touch shouldNormalize:(BOOL)normalize
140 CGPoint point = [touch locationInView:self];
143 CGRect bounds =
self.bounds;
144 point.x /= bounds.size.width;
145 point.y /= bounds.size.height;
151 - (float)pressureForTouch:(UITouch *)touch
154 if ([touch respondsToSelector:
@selector(force)]) {
155 return (
float) touch.force;
162 - (
void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
164 for (UITouch *touch
in touches) {
165 float pressure = [
self pressureForTouch:touch];
167 if (!firstFingerDown) {
168 CGPoint locationInView = [
self touchLocation:touch shouldNormalize:NO];
169 int clicks = (int) touch.tapCount;
177 firstFingerDown = touch;
180 CGPoint locationInView = [
self touchLocation:touch shouldNormalize:YES];
182 SDL_TRUE, locationInView.x, locationInView.y, pressure);
186 - (
void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
188 for (UITouch *touch
in touches) {
189 float pressure = [
self pressureForTouch:touch];
191 if (touch == firstFingerDown) {
193 int clicks = (int) touch.tapCount;
195 firstFingerDown = nil;
198 CGPoint locationInView = [
self touchLocation:touch shouldNormalize:YES];
200 SDL_FALSE, locationInView.x, locationInView.y, pressure);
204 - (
void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
206 [
self touchesEnded:touches withEvent:event];
209 - (
void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
211 for (UITouch *touch
in touches) {
212 float pressure = [
self pressureForTouch:touch];
214 if (touch == firstFingerDown) {
215 CGPoint locationInView = [
self touchLocation:touch shouldNormalize:NO];
221 CGPoint locationInView = [
self touchLocation:touch shouldNormalize:YES];
223 locationInView.x, locationInView.y, pressure);
227 #if TARGET_OS_TV || defined(__IPHONE_9_1)
228 - (
SDL_Scancode)scancodeFromPressType:(UIPressType)presstype
231 case UIPressTypeUpArrow:
233 case UIPressTypeDownArrow:
235 case UIPressTypeLeftArrow:
237 case UIPressTypeRightArrow:
239 case UIPressTypeSelect:
242 case UIPressTypeMenu:
245 case UIPressTypePlayPause:
253 - (
void)pressesBegan:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)event
256 for (UIPress *press
in presses) {
257 SDL_Scancode scancode = [
self scancodeFromPressType:press.type];
261 [
super pressesBegan:presses withEvent:event];
264 - (
void)pressesEnded:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)event
267 for (UIPress *press
in presses) {
268 SDL_Scancode scancode = [
self scancodeFromPressType:press.type];
272 [
super pressesEnded:presses withEvent:event];
275 - (
void)pressesCancelled:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)event
278 for (UIPress *press
in presses) {
279 SDL_Scancode scancode = [
self scancodeFromPressType:press.type];
283 [
super pressesCancelled:presses withEvent:event];
286 - (
void)pressesChanged:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)event
289 [
super pressesChanged:presses withEvent:event];
294 -(
void)swipeGesture:(UISwipeGestureRecognizer *)gesture
297 if (gesture.state == UIGestureRecognizerStateEnded) {
301 switch (gesture.direction) {
302 case UISwipeGestureRecognizerDirectionUp:
306 case UISwipeGestureRecognizerDirectionDown:
310 case UISwipeGestureRecognizerDirectionLeft:
314 case UISwipeGestureRecognizerDirectionRight: