From 783c0eab26312942415156c7e332c81433b0d93b Mon Sep 17 00:00:00 2001 From: Andrey Filipenkov Date: Sun, 21 Mar 2021 13:15:43 +0300 Subject: [PATCH] fix coordinates of long press (right click) kambala-decapitator/vcmi#1 kambala-decapitator/vcmi#26 --- client/SDL_uikit_main.m | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/client/SDL_uikit_main.m b/client/SDL_uikit_main.m index 6969a2012..cb3eb0e99 100644 --- a/client/SDL_uikit_main.m +++ b/client/SDL_uikit_main.m @@ -3,12 +3,15 @@ */ /* Include the SDL main definition header */ -#include "SDL_main.h" +#include -#include "SDL_events.h" +#include +#include @import UIKit; +extern SDL_Window * mainWindow; + @interface SDLViewObserver : NSObject @end @@ -18,6 +21,7 @@ - (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; + longPress.delaysTouchesBegan = YES; // prevent normal clicks by SDL [(UIView *)[object valueForKey:keyPath] addGestureRecognizer:longPress]; } @@ -35,16 +39,38 @@ return; } + __auto_type renderer = SDL_GetRenderer(mainWindow); + float scaleX, scaleY; + SDL_Rect viewport; + SDL_RenderGetScale(renderer, &scaleX, &scaleY); + SDL_RenderGetViewport(renderer, &viewport); + __auto_type touchedPoint = [gesture locationInView:gesture.view]; - SDL_Event event; - event.button = (SDL_MouseButtonEvent){ + __auto_type screenScale = UIScreen.mainScreen.nativeScale; + Sint32 x = (int)touchedPoint.x * screenScale / scaleX - viewport.x; + Sint32 y = (int)touchedPoint.y * screenScale / scaleY - viewport.y; + + SDL_Event rmbEvent; + rmbEvent.button = (SDL_MouseButtonEvent){ .type = mouseButtonType, .button = SDL_BUTTON_RIGHT, .clicks = 1, - .x = touchedPoint.x, - .y = touchedPoint.y, + .x = x, + .y = y, }; - SDL_PushEvent(&event); + SDL_PushEvent(&rmbEvent); + + // small hack to prevent cursor jumping + if (mouseButtonType == SDL_MOUSEBUTTONUP) + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.025 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + SDL_Event motionEvent; + motionEvent.motion = (SDL_MouseMotionEvent){ + .type = SDL_MOUSEMOTION, + .x = x, + .y = y, + }; + SDL_PushEvent(&motionEvent); + }); } @end