2021-03-16 22:32:07 +02:00
|
|
|
/*
|
2022-08-05 13:32:29 +02:00
|
|
|
* startSDL.mm, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
2022-08-07 07:58:04 +02:00
|
|
|
#import "startSDL.h"
|
|
|
|
#import "GameChatKeyboardHanlder.h"
|
2021-03-16 22:32:07 +02:00
|
|
|
|
2021-03-21 16:30:34 +02:00
|
|
|
#include "../Global.h"
|
|
|
|
#include "CMT.h"
|
2021-03-22 21:40:19 +02:00
|
|
|
#include "CServerHandler.h"
|
2021-03-21 16:30:34 +02:00
|
|
|
#include "CFocusableHelper.h"
|
2021-03-16 22:32:07 +02:00
|
|
|
|
2022-08-07 07:58:04 +02:00
|
|
|
#include <SDL_main.h>
|
|
|
|
#include <SDL_events.h>
|
|
|
|
#include <SDL_render.h>
|
|
|
|
#include <SDL_system.h>
|
2021-04-19 01:28:29 +02:00
|
|
|
|
2021-03-21 16:30:34 +02:00
|
|
|
#import <UIKit/UIKit.h>
|
2021-03-21 12:15:43 +02:00
|
|
|
|
2021-03-22 21:40:19 +02:00
|
|
|
@interface SDLViewObserver : NSObject <UIGestureRecognizerDelegate>
|
2021-04-19 01:28:29 +02:00
|
|
|
@property (nonatomic, strong) GameChatKeyboardHanlder * gameChatHandler;
|
2021-03-16 22:32:07 +02:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation SDLViewObserver
|
|
|
|
|
|
|
|
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
|
2022-08-07 08:31:56 +02:00
|
|
|
[object removeObserver:self forKeyPath:keyPath];
|
|
|
|
|
2021-04-19 01:28:29 +02:00
|
|
|
UIView * view = [object valueForKeyPath:keyPath];
|
|
|
|
|
|
|
|
UITextField * textField;
|
|
|
|
for (UIView * v in view.subviews) {
|
|
|
|
if ([v isKindOfClass:[UITextField class]]) {
|
|
|
|
textField = (UITextField *)v;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto r = textField.frame;
|
|
|
|
r.size.height = 40;
|
|
|
|
textField.frame = r;
|
|
|
|
self.gameChatHandler.textFieldSDL = textField;
|
2021-03-22 21:40:19 +02:00
|
|
|
|
2022-09-22 13:31:57 +02:00
|
|
|
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
|
2022-09-22 13:07:41 +02:00
|
|
|
if(@available(iOS 13.0, *))
|
|
|
|
textField.backgroundColor = UIColor.systemBackgroundColor;
|
|
|
|
else
|
2022-09-22 13:31:57 +02:00
|
|
|
#endif
|
2022-09-22 13:07:41 +02:00
|
|
|
textField.backgroundColor = UIColor.whiteColor;
|
|
|
|
|
2021-03-22 21:40:19 +02:00
|
|
|
auto longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
|
2021-04-19 01:28:29 +02:00
|
|
|
longPress.minimumPressDuration = 0.2;
|
2021-03-22 21:40:19 +02:00
|
|
|
[view addGestureRecognizer:longPress];
|
|
|
|
|
|
|
|
auto pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
|
|
|
|
[view addGestureRecognizer:pinch];
|
2021-03-16 22:32:07 +02:00
|
|
|
}
|
|
|
|
|
2021-03-22 21:40:19 +02:00
|
|
|
#pragma mark - Gestures
|
|
|
|
|
2021-03-16 22:32:07 +02:00
|
|
|
- (void)handleLongPress:(UIGestureRecognizer *)gesture {
|
2021-03-22 21:40:19 +02:00
|
|
|
// send RMB click
|
2021-03-16 22:32:07 +02:00
|
|
|
SDL_EventType mouseButtonType;
|
|
|
|
switch (gesture.state)
|
|
|
|
{
|
|
|
|
case UIGestureRecognizerStateBegan:
|
|
|
|
mouseButtonType = SDL_MOUSEBUTTONDOWN;
|
|
|
|
break;
|
|
|
|
case UIGestureRecognizerStateEnded:
|
|
|
|
mouseButtonType = SDL_MOUSEBUTTONUP;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-22 21:40:19 +02:00
|
|
|
auto renderer = SDL_GetRenderer(mainWindow);
|
2021-03-21 12:15:43 +02:00
|
|
|
float scaleX, scaleY;
|
|
|
|
SDL_Rect viewport;
|
|
|
|
SDL_RenderGetScale(renderer, &scaleX, &scaleY);
|
|
|
|
SDL_RenderGetViewport(renderer, &viewport);
|
|
|
|
|
2021-03-22 21:40:19 +02:00
|
|
|
auto touchedPoint = [gesture locationInView:gesture.view];
|
|
|
|
auto screenScale = UIScreen.mainScreen.nativeScale;
|
2021-03-21 12:15:43 +02:00
|
|
|
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){
|
2021-03-16 22:32:07 +02:00
|
|
|
.type = mouseButtonType,
|
|
|
|
.button = SDL_BUTTON_RIGHT,
|
|
|
|
.clicks = 1,
|
2021-03-21 12:15:43 +02:00
|
|
|
.x = x,
|
|
|
|
.y = y,
|
2021-03-16 22:32:07 +02:00
|
|
|
};
|
2021-03-21 12:15:43 +02:00
|
|
|
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);
|
|
|
|
});
|
2021-03-16 22:32:07 +02:00
|
|
|
}
|
|
|
|
|
2021-03-22 21:40:19 +02:00
|
|
|
- (void)handlePinch:(UIGestureRecognizer *)gesture {
|
|
|
|
if(gesture.state != UIGestureRecognizerStateBegan || CSH->state != EClientState::GAMEPLAY)
|
|
|
|
return;
|
2021-04-19 01:28:29 +02:00
|
|
|
[self.gameChatHandler triggerInput];
|
2021-03-22 21:40:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - UIGestureRecognizerDelegate
|
|
|
|
|
|
|
|
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
|
|
|
|
return [gestureRecognizer isKindOfClass:[UIPinchGestureRecognizer class]];
|
|
|
|
}
|
|
|
|
|
2021-03-16 22:32:07 +02:00
|
|
|
@end
|
|
|
|
|
|
|
|
|
2022-08-07 07:58:04 +02:00
|
|
|
int startSDL(int argc, char * argv[], BOOL startManually)
|
2021-03-16 22:32:07 +02:00
|
|
|
{
|
2022-08-05 13:32:29 +02:00
|
|
|
@autoreleasepool {
|
|
|
|
auto observer = [SDLViewObserver new];
|
2021-04-19 01:28:29 +02:00
|
|
|
observer.gameChatHandler = [GameChatKeyboardHanlder new];
|
2022-08-07 08:31:56 +02:00
|
|
|
|
|
|
|
id __block sdlWindowCreationObserver = [NSNotificationCenter.defaultCenter addObserverForName:UIWindowDidBecomeKeyNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
|
|
|
|
[NSNotificationCenter.defaultCenter removeObserver:sdlWindowCreationObserver];
|
|
|
|
sdlWindowCreationObserver = nil;
|
|
|
|
|
|
|
|
UIWindow * sdlWindow = note.object;
|
|
|
|
[sdlWindow.rootViewController addObserver:observer forKeyPath:NSStringFromSelector(@selector(view)) options:NSKeyValueObservingOptionNew context:NULL];
|
2022-08-05 13:32:29 +02:00
|
|
|
}];
|
2022-08-07 08:31:56 +02:00
|
|
|
id textFieldObserver = [NSNotificationCenter.defaultCenter addObserverForName:UITextFieldTextDidEndEditingNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
|
2022-08-05 13:32:29 +02:00
|
|
|
removeFocusFromActiveInput();
|
|
|
|
}];
|
|
|
|
|
2022-08-07 08:31:56 +02:00
|
|
|
int result;
|
|
|
|
if (startManually)
|
|
|
|
{
|
|
|
|
// copied from -[SDLUIKitDelegate postFinishLaunch]
|
|
|
|
SDL_SetMainReady();
|
|
|
|
SDL_iOSSetEventPump(SDL_TRUE);
|
|
|
|
result = SDL_main(argc, argv);
|
|
|
|
SDL_iOSSetEventPump(SDL_FALSE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
result = SDL_UIKitRunApp(argc, argv, SDL_main);
|
|
|
|
|
|
|
|
[NSNotificationCenter.defaultCenter removeObserver:textFieldObserver];
|
2022-08-05 13:32:29 +02:00
|
|
|
return result;
|
|
|
|
}
|
2021-03-16 22:32:07 +02:00
|
|
|
}
|