startSDL.mm 4.6 KB

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