startSDL.mm 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 "GameChatKeyboardHanlder.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) GameChatKeyboardHanlder * 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. UITextField * textField;
  29. for (UIView * v in view.subviews) {
  30. if ([v isKindOfClass:[UITextField class]]) {
  31. textField = (UITextField *)v;
  32. break;
  33. }
  34. }
  35. auto r = textField.frame;
  36. r.size.height = 40;
  37. textField.frame = r;
  38. textField.backgroundColor = UIColor.whiteColor;
  39. self.gameChatHandler.textFieldSDL = textField;
  40. auto longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
  41. longPress.minimumPressDuration = 0.2;
  42. [view addGestureRecognizer:longPress];
  43. auto pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
  44. [view addGestureRecognizer:pinch];
  45. }
  46. #pragma mark - Gestures
  47. - (void)handleLongPress:(UIGestureRecognizer *)gesture {
  48. // send RMB click
  49. SDL_EventType mouseButtonType;
  50. switch (gesture.state)
  51. {
  52. case UIGestureRecognizerStateBegan:
  53. mouseButtonType = SDL_MOUSEBUTTONDOWN;
  54. break;
  55. case UIGestureRecognizerStateEnded:
  56. mouseButtonType = SDL_MOUSEBUTTONUP;
  57. break;
  58. default:
  59. return;
  60. }
  61. auto renderer = SDL_GetRenderer(mainWindow);
  62. float scaleX, scaleY;
  63. SDL_Rect viewport;
  64. SDL_RenderGetScale(renderer, &scaleX, &scaleY);
  65. SDL_RenderGetViewport(renderer, &viewport);
  66. auto touchedPoint = [gesture locationInView:gesture.view];
  67. auto screenScale = UIScreen.mainScreen.nativeScale;
  68. Sint32 x = (int)touchedPoint.x * screenScale / scaleX - viewport.x;
  69. Sint32 y = (int)touchedPoint.y * screenScale / scaleY - viewport.y;
  70. SDL_Event rmbEvent;
  71. rmbEvent.button = (SDL_MouseButtonEvent){
  72. .type = mouseButtonType,
  73. .button = SDL_BUTTON_RIGHT,
  74. .clicks = 1,
  75. .x = x,
  76. .y = y,
  77. };
  78. SDL_PushEvent(&rmbEvent);
  79. // small hack to prevent cursor jumping
  80. if (mouseButtonType == SDL_MOUSEBUTTONUP)
  81. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.025 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  82. SDL_Event motionEvent;
  83. motionEvent.motion = (SDL_MouseMotionEvent){
  84. .type = SDL_MOUSEMOTION,
  85. .x = x,
  86. .y = y,
  87. };
  88. SDL_PushEvent(&motionEvent);
  89. });
  90. }
  91. - (void)handlePinch:(UIGestureRecognizer *)gesture {
  92. if(gesture.state != UIGestureRecognizerStateBegan || CSH->state != EClientState::GAMEPLAY)
  93. return;
  94. [self.gameChatHandler triggerInput];
  95. }
  96. #pragma mark - UIGestureRecognizerDelegate
  97. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
  98. return [gestureRecognizer isKindOfClass:[UIPinchGestureRecognizer class]];
  99. }
  100. @end
  101. int startSDL(int argc, char * argv[], BOOL startManually)
  102. {
  103. @autoreleasepool {
  104. auto observer = [SDLViewObserver new];
  105. observer.gameChatHandler = [GameChatKeyboardHanlder new];
  106. id __block sdlWindowCreationObserver = [NSNotificationCenter.defaultCenter addObserverForName:UIWindowDidBecomeKeyNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
  107. [NSNotificationCenter.defaultCenter removeObserver:sdlWindowCreationObserver];
  108. sdlWindowCreationObserver = nil;
  109. UIWindow * sdlWindow = note.object;
  110. [sdlWindow.rootViewController addObserver:observer forKeyPath:NSStringFromSelector(@selector(view)) options:NSKeyValueObservingOptionNew context:NULL];
  111. }];
  112. id textFieldObserver = [NSNotificationCenter.defaultCenter addObserverForName:UITextFieldTextDidEndEditingNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
  113. removeFocusFromActiveInput();
  114. }];
  115. int result;
  116. if (startManually)
  117. {
  118. // copied from -[SDLUIKitDelegate postFinishLaunch]
  119. SDL_SetMainReady();
  120. SDL_iOSSetEventPump(SDL_TRUE);
  121. result = SDL_main(argc, argv);
  122. SDL_iOSSetEventPump(SDL_FALSE);
  123. }
  124. else
  125. result = SDL_UIKitRunApp(argc, argv, SDL_main);
  126. [NSNotificationCenter.defaultCenter removeObserver:textFieldObserver];
  127. return result;
  128. }
  129. }