view_controller.mm 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*************************************************************************/
  2. /* view_controller.mm */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. #include "core/config/project_settings.h"
  32. #include "display_server_ios.h"
  33. #import "godot_view.h"
  34. #import "godot_view_renderer.h"
  35. #import "keyboard_input_view.h"
  36. #include "os_ios.h"
  37. #import <AVFoundation/AVFoundation.h>
  38. #import <GameController/GameController.h>
  39. @interface ViewController () <GodotViewDelegate>
  40. @property(strong, nonatomic) GodotViewRenderer *renderer;
  41. @property(strong, nonatomic) GodotKeyboardInputView *keyboardView;
  42. @property(strong, nonatomic) UIView *godotLoadingOverlay;
  43. @end
  44. @implementation ViewController
  45. - (GodotView *)godotView {
  46. return (GodotView *)self.view;
  47. }
  48. - (void)loadView {
  49. GodotView *view = [[GodotView alloc] init];
  50. GodotViewRenderer *renderer = [[GodotViewRenderer alloc] init];
  51. self.renderer = renderer;
  52. self.view = view;
  53. view.renderer = self.renderer;
  54. view.delegate = self;
  55. }
  56. - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
  57. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  58. if (self) {
  59. [self godot_commonInit];
  60. }
  61. return self;
  62. }
  63. - (instancetype)initWithCoder:(NSCoder *)coder {
  64. self = [super initWithCoder:coder];
  65. if (self) {
  66. [self godot_commonInit];
  67. }
  68. return self;
  69. }
  70. - (void)godot_commonInit {
  71. // Initialize view controller values.
  72. }
  73. - (void)didReceiveMemoryWarning {
  74. [super didReceiveMemoryWarning];
  75. printf("*********** did receive memory warning!\n");
  76. }
  77. - (void)viewDidLoad {
  78. [super viewDidLoad];
  79. [self observeKeyboard];
  80. [self displayLoadingOverlay];
  81. if (@available(iOS 11.0, *)) {
  82. [self setNeedsUpdateOfScreenEdgesDeferringSystemGestures];
  83. }
  84. }
  85. - (void)observeKeyboard {
  86. printf("******** setting up keyboard input view\n");
  87. self.keyboardView = [GodotKeyboardInputView new];
  88. [self.view addSubview:self.keyboardView];
  89. printf("******** adding observer for keyboard show/hide\n");
  90. [[NSNotificationCenter defaultCenter]
  91. addObserver:self
  92. selector:@selector(keyboardOnScreen:)
  93. name:UIKeyboardDidShowNotification
  94. object:nil];
  95. [[NSNotificationCenter defaultCenter]
  96. addObserver:self
  97. selector:@selector(keyboardHidden:)
  98. name:UIKeyboardDidHideNotification
  99. object:nil];
  100. }
  101. - (void)displayLoadingOverlay {
  102. NSBundle *bundle = [NSBundle mainBundle];
  103. NSString *storyboardName = @"Launch Screen";
  104. if ([bundle pathForResource:storyboardName ofType:@"storyboardc"] == nil) {
  105. return;
  106. }
  107. UIStoryboard *launchStoryboard = [UIStoryboard storyboardWithName:storyboardName bundle:bundle];
  108. UIViewController *controller = [launchStoryboard instantiateInitialViewController];
  109. self.godotLoadingOverlay = controller.view;
  110. self.godotLoadingOverlay.frame = self.view.bounds;
  111. self.godotLoadingOverlay.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  112. [self.view addSubview:self.godotLoadingOverlay];
  113. }
  114. - (BOOL)godotViewFinishedSetup:(GodotView *)view {
  115. [self.godotLoadingOverlay removeFromSuperview];
  116. self.godotLoadingOverlay = nil;
  117. return YES;
  118. }
  119. - (void)dealloc {
  120. self.keyboardView = nil;
  121. self.renderer = nil;
  122. if (self.godotLoadingOverlay) {
  123. [self.godotLoadingOverlay removeFromSuperview];
  124. self.godotLoadingOverlay = nil;
  125. }
  126. [[NSNotificationCenter defaultCenter] removeObserver:self];
  127. }
  128. // MARK: Orientation
  129. - (UIRectEdge)preferredScreenEdgesDeferringSystemGestures {
  130. if (GLOBAL_GET("display/window/ios/suppress_ui_gesture")) {
  131. return UIRectEdgeAll;
  132. } else {
  133. return UIRectEdgeNone;
  134. }
  135. }
  136. - (BOOL)shouldAutorotate {
  137. if (!DisplayServerIOS::get_singleton()) {
  138. return NO;
  139. }
  140. switch (DisplayServerIOS::get_singleton()->screen_get_orientation(DisplayServer::SCREEN_OF_MAIN_WINDOW)) {
  141. case DisplayServer::SCREEN_SENSOR:
  142. case DisplayServer::SCREEN_SENSOR_LANDSCAPE:
  143. case DisplayServer::SCREEN_SENSOR_PORTRAIT:
  144. return YES;
  145. default:
  146. return NO;
  147. }
  148. }
  149. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  150. if (!DisplayServerIOS::get_singleton()) {
  151. return UIInterfaceOrientationMaskAll;
  152. }
  153. switch (DisplayServerIOS::get_singleton()->screen_get_orientation(DisplayServer::SCREEN_OF_MAIN_WINDOW)) {
  154. case DisplayServer::SCREEN_PORTRAIT:
  155. return UIInterfaceOrientationMaskPortrait;
  156. case DisplayServer::SCREEN_REVERSE_LANDSCAPE:
  157. return UIInterfaceOrientationMaskLandscapeRight;
  158. case DisplayServer::SCREEN_REVERSE_PORTRAIT:
  159. return UIInterfaceOrientationMaskPortraitUpsideDown;
  160. case DisplayServer::SCREEN_SENSOR_LANDSCAPE:
  161. return UIInterfaceOrientationMaskLandscape;
  162. case DisplayServer::SCREEN_SENSOR_PORTRAIT:
  163. return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
  164. case DisplayServer::SCREEN_SENSOR:
  165. return UIInterfaceOrientationMaskAll;
  166. case DisplayServer::SCREEN_LANDSCAPE:
  167. return UIInterfaceOrientationMaskLandscapeLeft;
  168. }
  169. }
  170. - (BOOL)prefersStatusBarHidden {
  171. if (GLOBAL_GET("display/window/ios/hide_status_bar")) {
  172. return YES;
  173. } else {
  174. return NO;
  175. }
  176. }
  177. - (BOOL)prefersHomeIndicatorAutoHidden {
  178. if (GLOBAL_GET("display/window/ios/hide_home_indicator")) {
  179. return YES;
  180. } else {
  181. return NO;
  182. }
  183. }
  184. // MARK: Keyboard
  185. - (void)keyboardOnScreen:(NSNotification *)notification {
  186. NSDictionary *info = notification.userInfo;
  187. NSValue *value = info[UIKeyboardFrameEndUserInfoKey];
  188. CGRect rawFrame = [value CGRectValue];
  189. CGRect keyboardFrame = [self.view convertRect:rawFrame fromView:nil];
  190. if (DisplayServerIOS::get_singleton()) {
  191. DisplayServerIOS::get_singleton()->virtual_keyboard_set_height(keyboardFrame.size.height);
  192. }
  193. }
  194. - (void)keyboardHidden:(NSNotification *)notification {
  195. if (DisplayServerIOS::get_singleton()) {
  196. DisplayServerIOS::get_singleton()->virtual_keyboard_set_height(0);
  197. }
  198. }
  199. @end