startSDL.mm 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * startSDL.mm, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #import "startSDL.h"
  11. #import "GameChatKeyboardHandler.h"
  12. #include "../Global.h"
  13. #include "CMT.h"
  14. #include "CServerHandler.h"
  15. #include "CFocusableHelper.h"
  16. #include <SDL_main.h>
  17. #include <SDL_events.h>
  18. #include <SDL_render.h>
  19. #include <SDL_system.h>
  20. #import <UIKit/UIKit.h>
  21. @interface SDLViewObserver : NSObject <UIGestureRecognizerDelegate>
  22. @property (nonatomic, strong) GameChatKeyboardHandler * gameChatHandler;
  23. @end
  24. @implementation SDLViewObserver
  25. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
  26. [object removeObserver:self forKeyPath:keyPath];
  27. UIView * view = [object valueForKeyPath:keyPath];
  28. auto longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
  29. longPress.minimumPressDuration = 0.2;
  30. [view addGestureRecognizer:longPress];
  31. auto pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
  32. [view addGestureRecognizer:pinch];
  33. }
  34. #pragma mark - Gestures
  35. - (void)handleLongPress:(UIGestureRecognizer *)gesture {
  36. // send RMB click
  37. SDL_EventType mouseButtonType;
  38. switch (gesture.state)
  39. {
  40. case UIGestureRecognizerStateBegan:
  41. mouseButtonType = SDL_MOUSEBUTTONDOWN;
  42. break;
  43. case UIGestureRecognizerStateEnded:
  44. mouseButtonType = SDL_MOUSEBUTTONUP;
  45. break;
  46. default:
  47. return;
  48. }
  49. auto renderer = SDL_GetRenderer(mainWindow);
  50. float scaleX, scaleY;
  51. SDL_Rect viewport;
  52. SDL_RenderGetScale(renderer, &scaleX, &scaleY);
  53. SDL_RenderGetViewport(renderer, &viewport);
  54. auto touchedPoint = [gesture locationInView:gesture.view];
  55. auto screenScale = UIScreen.mainScreen.nativeScale;
  56. Sint32 x = (int)touchedPoint.x * screenScale / scaleX - viewport.x;
  57. Sint32 y = (int)touchedPoint.y * screenScale / scaleY - viewport.y;
  58. SDL_Event rmbEvent;
  59. rmbEvent.button = (SDL_MouseButtonEvent){
  60. .type = mouseButtonType,
  61. .button = SDL_BUTTON_RIGHT,
  62. .clicks = 1,
  63. .x = x,
  64. .y = y,
  65. };
  66. SDL_PushEvent(&rmbEvent);
  67. // small hack to prevent cursor jumping
  68. if (mouseButtonType == SDL_MOUSEBUTTONUP)
  69. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.025 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  70. SDL_Event motionEvent;
  71. motionEvent.motion = (SDL_MouseMotionEvent){
  72. .type = SDL_MOUSEMOTION,
  73. .x = x,
  74. .y = y,
  75. };
  76. SDL_PushEvent(&motionEvent);
  77. });
  78. }
  79. - (void)handlePinch:(UIGestureRecognizer *)gesture {
  80. if(gesture.state != UIGestureRecognizerStateBegan || CSH->state != EClientState::GAMEPLAY)
  81. return;
  82. [self.gameChatHandler triggerInput];
  83. }
  84. #pragma mark - UIGestureRecognizerDelegate
  85. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
  86. return [gestureRecognizer isKindOfClass:[UIPinchGestureRecognizer class]];
  87. }
  88. @end
  89. int startSDL(int argc, char * argv[], BOOL startManually)
  90. {
  91. @autoreleasepool {
  92. auto observer = [SDLViewObserver new];
  93. observer.gameChatHandler = [GameChatKeyboardHandler new];
  94. id __block sdlWindowCreationObserver = [NSNotificationCenter.defaultCenter addObserverForName:UIWindowDidBecomeKeyNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
  95. [NSNotificationCenter.defaultCenter removeObserver:sdlWindowCreationObserver];
  96. sdlWindowCreationObserver = nil;
  97. UIWindow * sdlWindow = note.object;
  98. [sdlWindow.rootViewController addObserver:observer forKeyPath:NSStringFromSelector(@selector(view)) options:NSKeyValueObservingOptionNew context:NULL];
  99. }];
  100. id textFieldObserver = [NSNotificationCenter.defaultCenter addObserverForName:UITextFieldTextDidEndEditingNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
  101. removeFocusFromActiveInput();
  102. }];
  103. int result;
  104. if (startManually)
  105. {
  106. // copied from -[SDLUIKitDelegate postFinishLaunch]
  107. SDL_SetMainReady();
  108. SDL_iOSSetEventPump(SDL_TRUE);
  109. result = SDL_main(argc, argv);
  110. SDL_iOSSetEventPump(SDL_FALSE);
  111. }
  112. else
  113. result = SDL_UIKitRunApp(argc, argv, SDL_main);
  114. [NSNotificationCenter.defaultCenter removeObserver:textFieldObserver];
  115. return result;
  116. }
  117. }