From e3539049cdc142315d01d3a1859b555eeeee592b Mon Sep 17 00:00:00 2001 From: Andrey Filipenkov Date: Tue, 16 Mar 2021 23:32:07 +0300 Subject: [PATCH] implement RMB click as long press kambala-decapitator/vcmi#1 --- client/CMakeLists.txt | 2 +- client/SDL_uikit_main.c | 19 ------------ client/SDL_uikit_main.m | 68 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 20 deletions(-) delete mode 100644 client/SDL_uikit_main.c create mode 100644 client/SDL_uikit_main.m diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index 2324cd88d..f826605d2 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -154,7 +154,7 @@ endif() if(WIN32) set(client_ICON "VCMI_client.rc") elseif(APPLE_IOS) - set(client_SRCS ${client_SRCS} SDL_uikit_main.c) + set(client_SRCS ${client_SRCS} SDL_uikit_main.m) endif() if(ENABLE_DEBUG_CONSOLE) diff --git a/client/SDL_uikit_main.c b/client/SDL_uikit_main.c deleted file mode 100644 index 966009afe..000000000 --- a/client/SDL_uikit_main.c +++ /dev/null @@ -1,19 +0,0 @@ -/* - SDL_uikit_main.c, placed in the public domain by Sam Lantinga 3/18/2019 -*/ -// #include "../../SDL_internal.h" - -/* Include the SDL main definition header */ -#include "SDL_main.h" - -#ifdef main -#undef main -#endif - -int -main(int argc, char *argv[]) -{ - return SDL_UIKitRunApp(argc, argv, SDL_main); -} - -/* vi: set ts=4 sw=4 expandtab: */ diff --git a/client/SDL_uikit_main.m b/client/SDL_uikit_main.m new file mode 100644 index 000000000..6969a2012 --- /dev/null +++ b/client/SDL_uikit_main.m @@ -0,0 +1,68 @@ +/* + SDL_uikit_main.c, placed in the public domain by Sam Lantinga 3/18/2019 +*/ + +/* Include the SDL main definition header */ +#include "SDL_main.h" + +#include "SDL_events.h" + +@import UIKit; + + +@interface SDLViewObserver : NSObject +@end + +@implementation SDLViewObserver + +- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { + __auto_type longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)]; + longPress.minimumPressDuration = 0.1; + [(UIView *)[object valueForKey:keyPath] addGestureRecognizer:longPress]; +} + +- (void)handleLongPress:(UIGestureRecognizer *)gesture { + SDL_EventType mouseButtonType; + switch (gesture.state) + { + case UIGestureRecognizerStateBegan: + mouseButtonType = SDL_MOUSEBUTTONDOWN; + break; + case UIGestureRecognizerStateEnded: + mouseButtonType = SDL_MOUSEBUTTONUP; + break; + default: + return; + } + + __auto_type touchedPoint = [gesture locationInView:gesture.view]; + SDL_Event event; + event.button = (SDL_MouseButtonEvent){ + .type = mouseButtonType, + .button = SDL_BUTTON_RIGHT, + .clicks = 1, + .x = touchedPoint.x, + .y = touchedPoint.y, + }; + SDL_PushEvent(&event); +} + +@end + + +#ifdef main +#undef main +#endif + +int +main(int argc, char *argv[]) +{ + @autoreleasepool + { + __auto_type observer = [SDLViewObserver new]; + [NSNotificationCenter.defaultCenter addObserverForName:UIWindowDidBecomeKeyNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) { + [UIApplication.sharedApplication.keyWindow.rootViewController addObserver:observer forKeyPath:NSStringFromSelector(@selector(view)) options:NSKeyValueObservingOptionNew context:NULL]; + }]; + return SDL_UIKitRunApp(argc, argv, SDL_main); + } +}