view_controller.mm 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /**************************************************************************/
  2. /* view_controller.mm */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #import "view_controller.h"
  31. #import "display_server_ios.h"
  32. #import "godot_view.h"
  33. #import "godot_view_renderer.h"
  34. #import "key_mapping_ios.h"
  35. #import "keyboard_input_view.h"
  36. #import "os_ios.h"
  37. #include "core/config/project_settings.h"
  38. #import <AVFoundation/AVFoundation.h>
  39. #import <GameController/GameController.h>
  40. @interface ViewController () <GodotViewDelegate>
  41. @property(strong, nonatomic) GodotViewRenderer *renderer;
  42. @property(strong, nonatomic) GodotKeyboardInputView *keyboardView;
  43. @property(strong, nonatomic) UIView *godotLoadingOverlay;
  44. @end
  45. @implementation ViewController
  46. - (GodotView *)godotView {
  47. return (GodotView *)self.view;
  48. }
  49. - (void)pressesBegan:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)event {
  50. [super pressesBegan:presses withEvent:event];
  51. if (!DisplayServerIOS::get_singleton() || DisplayServerIOS::get_singleton()->is_keyboard_active()) {
  52. return;
  53. }
  54. if (@available(iOS 13.4, *)) {
  55. for (UIPress *press in presses) {
  56. String u32lbl = String::utf8([press.key.charactersIgnoringModifiers UTF8String]);
  57. String u32text = String::utf8([press.key.characters UTF8String]);
  58. Key key = KeyMappingIOS::remap_key(press.key.keyCode);
  59. if (press.key.keyCode == 0 && u32text.is_empty() && u32lbl.is_empty()) {
  60. continue;
  61. }
  62. char32_t us = 0;
  63. if (!u32lbl.is_empty() && !u32lbl.begins_with("UIKey")) {
  64. us = u32lbl[0];
  65. }
  66. if (!u32text.is_empty() && !u32text.begins_with("UIKey")) {
  67. for (int i = 0; i < u32text.length(); i++) {
  68. const char32_t c = u32text[i];
  69. DisplayServerIOS::get_singleton()->key(fix_keycode(us, key), c, fix_key_label(us, key), key, press.key.modifierFlags, true);
  70. }
  71. } else {
  72. DisplayServerIOS::get_singleton()->key(fix_keycode(us, key), 0, fix_key_label(us, key), key, press.key.modifierFlags, true);
  73. }
  74. }
  75. }
  76. }
  77. - (void)pressesEnded:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)event {
  78. [super pressesEnded:presses withEvent:event];
  79. if (!DisplayServerIOS::get_singleton() || DisplayServerIOS::get_singleton()->is_keyboard_active()) {
  80. return;
  81. }
  82. if (@available(iOS 13.4, *)) {
  83. for (UIPress *press in presses) {
  84. String u32lbl = String::utf8([press.key.charactersIgnoringModifiers UTF8String]);
  85. Key key = KeyMappingIOS::remap_key(press.key.keyCode);
  86. if (press.key.keyCode == 0 && u32lbl.is_empty()) {
  87. continue;
  88. }
  89. char32_t us = 0;
  90. if (!u32lbl.is_empty() && !u32lbl.begins_with("UIKey")) {
  91. us = u32lbl[0];
  92. }
  93. DisplayServerIOS::get_singleton()->key(fix_keycode(us, key), 0, fix_key_label(us, key), key, press.key.modifierFlags, false);
  94. }
  95. }
  96. }
  97. - (void)loadView {
  98. GodotView *view = [[GodotView alloc] init];
  99. GodotViewRenderer *renderer = [[GodotViewRenderer alloc] init];
  100. self.renderer = renderer;
  101. self.view = view;
  102. view.renderer = self.renderer;
  103. view.delegate = self;
  104. }
  105. - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
  106. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  107. if (self) {
  108. [self godot_commonInit];
  109. }
  110. return self;
  111. }
  112. - (instancetype)initWithCoder:(NSCoder *)coder {
  113. self = [super initWithCoder:coder];
  114. if (self) {
  115. [self godot_commonInit];
  116. }
  117. return self;
  118. }
  119. - (void)godot_commonInit {
  120. // Initialize view controller values.
  121. }
  122. - (void)didReceiveMemoryWarning {
  123. [super didReceiveMemoryWarning];
  124. print_verbose("Did receive memory warning!");
  125. }
  126. - (void)viewDidLoad {
  127. [super viewDidLoad];
  128. [self observeKeyboard];
  129. [self displayLoadingOverlay];
  130. [self setNeedsUpdateOfScreenEdgesDeferringSystemGestures];
  131. }
  132. - (void)observeKeyboard {
  133. print_verbose("Setting up keyboard input view.");
  134. self.keyboardView = [GodotKeyboardInputView new];
  135. [self.view addSubview:self.keyboardView];
  136. print_verbose("Adding observer for keyboard show/hide.");
  137. [[NSNotificationCenter defaultCenter]
  138. addObserver:self
  139. selector:@selector(keyboardOnScreen:)
  140. name:UIKeyboardDidShowNotification
  141. object:nil];
  142. [[NSNotificationCenter defaultCenter]
  143. addObserver:self
  144. selector:@selector(keyboardHidden:)
  145. name:UIKeyboardDidHideNotification
  146. object:nil];
  147. }
  148. - (void)displayLoadingOverlay {
  149. NSBundle *bundle = [NSBundle mainBundle];
  150. NSString *storyboardName = @"Launch Screen";
  151. if ([bundle pathForResource:storyboardName ofType:@"storyboardc"] == nil) {
  152. return;
  153. }
  154. UIStoryboard *launchStoryboard = [UIStoryboard storyboardWithName:storyboardName bundle:bundle];
  155. UIViewController *controller = [launchStoryboard instantiateInitialViewController];
  156. self.godotLoadingOverlay = controller.view;
  157. self.godotLoadingOverlay.frame = self.view.bounds;
  158. self.godotLoadingOverlay.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  159. [self.view addSubview:self.godotLoadingOverlay];
  160. }
  161. - (BOOL)godotViewFinishedSetup:(GodotView *)view {
  162. [self.godotLoadingOverlay removeFromSuperview];
  163. self.godotLoadingOverlay = nil;
  164. return YES;
  165. }
  166. - (void)dealloc {
  167. self.keyboardView = nil;
  168. self.renderer = nil;
  169. if (self.godotLoadingOverlay) {
  170. [self.godotLoadingOverlay removeFromSuperview];
  171. self.godotLoadingOverlay = nil;
  172. }
  173. [[NSNotificationCenter defaultCenter] removeObserver:self];
  174. }
  175. // MARK: Orientation
  176. - (UIRectEdge)preferredScreenEdgesDeferringSystemGestures {
  177. if (GLOBAL_GET("display/window/ios/suppress_ui_gesture")) {
  178. return UIRectEdgeAll;
  179. } else {
  180. return UIRectEdgeNone;
  181. }
  182. }
  183. - (BOOL)shouldAutorotate {
  184. if (!DisplayServerIOS::get_singleton()) {
  185. return NO;
  186. }
  187. switch (DisplayServerIOS::get_singleton()->screen_get_orientation(DisplayServer::SCREEN_OF_MAIN_WINDOW)) {
  188. case DisplayServer::SCREEN_SENSOR:
  189. case DisplayServer::SCREEN_SENSOR_LANDSCAPE:
  190. case DisplayServer::SCREEN_SENSOR_PORTRAIT:
  191. return YES;
  192. default:
  193. return NO;
  194. }
  195. }
  196. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  197. if (!DisplayServerIOS::get_singleton()) {
  198. return UIInterfaceOrientationMaskAll;
  199. }
  200. switch (DisplayServerIOS::get_singleton()->screen_get_orientation(DisplayServer::SCREEN_OF_MAIN_WINDOW)) {
  201. case DisplayServer::SCREEN_PORTRAIT:
  202. return UIInterfaceOrientationMaskPortrait;
  203. case DisplayServer::SCREEN_REVERSE_LANDSCAPE:
  204. return UIInterfaceOrientationMaskLandscapeRight;
  205. case DisplayServer::SCREEN_REVERSE_PORTRAIT:
  206. return UIInterfaceOrientationMaskPortraitUpsideDown;
  207. case DisplayServer::SCREEN_SENSOR_LANDSCAPE:
  208. return UIInterfaceOrientationMaskLandscape;
  209. case DisplayServer::SCREEN_SENSOR_PORTRAIT:
  210. return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
  211. case DisplayServer::SCREEN_SENSOR:
  212. return UIInterfaceOrientationMaskAll;
  213. case DisplayServer::SCREEN_LANDSCAPE:
  214. return UIInterfaceOrientationMaskLandscapeLeft;
  215. }
  216. }
  217. - (BOOL)prefersStatusBarHidden {
  218. if (GLOBAL_GET("display/window/ios/hide_status_bar")) {
  219. return YES;
  220. } else {
  221. return NO;
  222. }
  223. }
  224. - (BOOL)prefersHomeIndicatorAutoHidden {
  225. if (GLOBAL_GET("display/window/ios/hide_home_indicator")) {
  226. return YES;
  227. } else {
  228. return NO;
  229. }
  230. }
  231. // MARK: Keyboard
  232. - (void)keyboardOnScreen:(NSNotification *)notification {
  233. NSDictionary *info = notification.userInfo;
  234. NSValue *value = info[UIKeyboardFrameEndUserInfoKey];
  235. CGRect rawFrame = [value CGRectValue];
  236. CGRect keyboardFrame = [self.view convertRect:rawFrame fromView:nil];
  237. if (DisplayServerIOS::get_singleton()) {
  238. DisplayServerIOS::get_singleton()->virtual_keyboard_set_height(keyboardFrame.size.height);
  239. }
  240. }
  241. - (void)keyboardHidden:(NSNotification *)notification {
  242. if (DisplayServerIOS::get_singleton()) {
  243. DisplayServerIOS::get_singleton()->virtual_keyboard_set_height(0);
  244. }
  245. }
  246. @end