SDL_uikit_main.mm 4.4 KB

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