Debian Codegraph @

drivers/apple_embedded/godot_view_controller.mm

raw
1 /**************************************************************************/
2 /* godot_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
31 #import "godot_view_controller.h"
32
33 #import "display_server_apple_embedded.h"
34 #import "godot_keyboard_input_view.h"
35 #import "godot_view_apple_embedded.h"
36 #import "godot_view_renderer.h"
37 #import "key_mapping_apple_embedded.h"
38 #import "os_apple_embedded.h"
39
40 #include "core/config/project_settings.h"
41 #include "servers/camera/camera_server.h"
42
43 #import <AVFoundation/AVFoundation.h>
44 #import <GameController/GameController.h>
45 #import <objc/runtime.h>
46
47 @interface GDTViewController () <GDTViewDelegate>
48
49 @property(strong, nonatomic) GDTViewRenderer *renderer;
50 @property(strong, nonatomic) GDTKeyboardInputView *keyboardView;
51
52 @property(strong, nonatomic) UIView *godotLoadingOverlay;
53
54 @end
55
56 @implementation GDTViewController
57
58 - (GDTView *)godotView {
59 return (GDTView *)self.view;
60 }
61
62 - (void)pressesBegan:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)event {
63 [super pressesBegan:presses withEvent:event];
64
65 if (!DisplayServerAppleEmbedded::get_singleton() || DisplayServerAppleEmbedded::get_singleton()->is_keyboard_active()) {
66 return;
67 }
68 if (@available(iOS 13.4, *)) {
69 for (UIPress *press in presses) {
70 String u32lbl = String::utf8([press.key.charactersIgnoringModifiers UTF8String]);
71 String u32text = String::utf8([press.key.characters UTF8String]);
72 Key key = KeyMappingAppleEmbedded::remap_key(press.key.keyCode);
73
74 if (press.key.keyCode == 0 && u32text.is_empty() && u32lbl.is_empty()) {
75 continue;
76 }
77
78 char32_t us = 0;
79 if (!u32lbl.is_empty() && !u32lbl.begins_with("UIKey")) {
80 us = u32lbl[0];
81 }
82
83 KeyLocation location = KeyMappingAppleEmbedded::key_location(press.key.keyCode);
84
85 if (!u32text.is_empty() && !u32text.begins_with("UIKey")) {
86 for (int i = 0; i < u32text.length(); i++) {
87 const char32_t c = u32text[i];
88 DisplayServerAppleEmbedded::get_singleton()->key(fix_keycode(us, key), c, fix_key_label(us, key), key, press.key.modifierFlags, true, location);
89 }
90 } else {
91 DisplayServerAppleEmbedded::get_singleton()->key(fix_keycode(us, key), 0, fix_key_label(us, key), key, press.key.modifierFlags, true, location);
92 }
93 }
94 }
95 }
96
97 - (void)pressesEnded:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)event {
98 [super pressesEnded:presses withEvent:event];
99
100 if (!DisplayServerAppleEmbedded::get_singleton() || DisplayServerAppleEmbedded::get_singleton()->is_keyboard_active()) {
101 return;
102 }
103 if (@available(iOS 13.4, *)) {
104 for (UIPress *press in presses) {
105 String u32lbl = String::utf8([press.key.charactersIgnoringModifiers UTF8String]);
106 Key key = KeyMappingAppleEmbedded::remap_key(press.key.keyCode);
107
108 if (press.key.keyCode == 0 && u32lbl.is_empty()) {
109 continue;
110 }
111
112 char32_t us = 0;
113 if (!u32lbl.is_empty() && !u32lbl.begins_with("UIKey")) {
114 us = u32lbl[0];
115 }
116
117 KeyLocation location = KeyMappingAppleEmbedded::key_location(press.key.keyCode);
118
119 DisplayServerAppleEmbedded::get_singleton()->key(fix_keycode(us, key), 0, fix_key_label(us, key), key, press.key.modifierFlags, false, location);
120 }
121 }
122 }
123
124 - (void)loadView {
125 GDTView *view = GDTViewCreate();
126 GDTViewRenderer *renderer = [[GDTViewRenderer alloc] init];
127
128 self.renderer = renderer;
129 self.view = view;
130
131 view.renderer = self.renderer;
132 view.delegate = self;
133 }
134
135 - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
136 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
137
138 if (self) {
139 [self godot_commonInit];
140 }
141
142 return self;
143 }
144
145 - (instancetype)initWithCoder:(NSCoder *)coder {
146 self = [super initWithCoder:coder];
147
148 if (self) {
149 [self godot_commonInit];
150 }
151
152 return self;
153 }
154
155 - (void)godot_commonInit {
156 // Initialize view controller values.
157 }
158
159 - (void)didReceiveMemoryWarning {
160 [super didReceiveMemoryWarning];
161 print_verbose("Did receive memory warning!");
162 }
163
164 - (void)viewDidLoad {
165 [super viewDidLoad];
166
167 [self observeKeyboard];
168 [self displayLoadingOverlay];
169
170 [self setNeedsUpdateOfScreenEdgesDeferringSystemGestures];
171 }
172
173 - (void)viewDidAppear:(BOOL)animated {
174 [super viewDidAppear:animated];
175 [self.godotView startRendering];
176 #ifdef IOS_ENABLED
177 [self propagateUIPreferencesToRootViewController];
178 #endif
179 }
180
181 - (void)viewDidDisappear:(BOOL)animated {
182 [self.godotView stopRendering];
183 [super viewDidDisappear:animated];
184 }
185
186 - (void)observeKeyboard {
187 print_verbose("Setting up keyboard input view.");
188 self.keyboardView = [GDTKeyboardInputView new];
189 [self.view addSubview:self.keyboardView];
190
191 print_verbose("Adding observer for keyboard show/hide.");
192 [[NSNotificationCenter defaultCenter]
193 addObserver:self
194 selector:@selector(keyboardOnScreen:)
195 name:UIKeyboardDidShowNotification
196 object:nil];
197 [[NSNotificationCenter defaultCenter]
198 addObserver:self
199 selector:@selector(keyboardHidden:)
200 name:UIKeyboardDidHideNotification
201 object:nil];
202 }
203
204 - (void)displayLoadingOverlay {
205 #if !defined(VISIONOS_ENABLED)
206 NSBundle *bundle = [NSBundle mainBundle];
207 NSString *storyboardName = @"Launch Screen";
208
209 if ([bundle pathForResource:storyboardName ofType:@"storyboardc"] == nil) {
210 return;
211 }
212
213 UIStoryboard *launchStoryboard = [UIStoryboard storyboardWithName:storyboardName bundle:bundle];
214
215 UIViewController *controller = [launchStoryboard instantiateInitialViewController];
216 self.godotLoadingOverlay = controller.view;
217 self.godotLoadingOverlay.frame = self.view.bounds;
218 self.godotLoadingOverlay.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
219
220 [self.view addSubview:self.godotLoadingOverlay];
221 #endif
222 }
223
224 - (BOOL)godotViewFinishedSetup:(GDTView *)view {
225 [self.godotLoadingOverlay removeFromSuperview];
226 self.godotLoadingOverlay = nil;
227
228 return YES;
229 }
230
231 - (void)dealloc {
232 self.keyboardView = nil;
233
234 self.renderer = nil;
235
236 if (self.godotLoadingOverlay) {
237 [self.godotLoadingOverlay removeFromSuperview];
238 self.godotLoadingOverlay = nil;
239 }
240
241 [[NSNotificationCenter defaultCenter] removeObserver:self];
242 }
243
244 // Propagate UI preferences to the root VC (the SwiftUI hosting controller doesn't delegate these).
245 #ifdef IOS_ENABLED
246 - (void)propagateUIPreferencesToRootViewController {
247 UIViewController *rootVC = self.view.window.rootViewController;
248 if (!rootVC || rootVC == self) {
249 return;
250 }
251
252 Class rootClass = [rootVC class];
253
254 SEL selectors[] = {
255 @selector(preferredScreenEdgesDeferringSystemGestures),
256 @selector(prefersHomeIndicatorAutoHidden),
257 @selector(prefersStatusBarHidden),
258 };
259
260 for (SEL sel : selectors) {
261 Method method = class_getInstanceMethod([GDTViewController class], sel);
262 if (!class_addMethod(rootClass, sel, method_getImplementation(method), method_getTypeEncoding(method))) {
263 method_setImplementation(class_getInstanceMethod(rootClass, sel), method_getImplementation(method));
264 }
265 }
266
267 [rootVC setNeedsUpdateOfScreenEdgesDeferringSystemGestures];
268 [rootVC setNeedsUpdateOfHomeIndicatorAutoHidden];
269 [rootVC setNeedsStatusBarAppearanceUpdate];
270 }
271 #endif
272
273 // MARK: Orientation
274
275 #ifdef IOS_ENABLED
276 - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
277 [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
278
279 [coordinator animateAlongsideTransition:nil
280 completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
281 // Get the new interface orientation after rotation completes (iOS only)
282 UIInterfaceOrientation orientation = self.view.window.windowScene.interfaceOrientation;
283
284 // Notify camera server of orientation change
285 CameraServer *camera_server = CameraServer::get_singleton();
286 if (camera_server) {
287 camera_server->handle_display_rotation_change((int)orientation);
288 }
289 }];
290 }
291 #endif
292
293 - (UIRectEdge)preferredScreenEdgesDeferringSystemGestures {
294 if (GLOBAL_GET("display/window/ios/suppress_ui_gesture")) {
295 return UIRectEdgeAll;
296 } else {
297 return UIRectEdgeNone;
298 }
299 }
300
301 - (BOOL)shouldAutorotate {
302 if (!DisplayServerAppleEmbedded::get_singleton()) {
303 return NO;
304 }
305
306 switch (DisplayServerAppleEmbedded::get_singleton()->screen_get_orientation(DisplayServer::SCREEN_OF_MAIN_WINDOW)) {
307 case DisplayServer::SCREEN_SENSOR:
308 case DisplayServer::SCREEN_SENSOR_LANDSCAPE:
309 case DisplayServer::SCREEN_SENSOR_PORTRAIT:
310 return YES;
311 default:
312 return NO;
313 }
314 }
315
316 - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
317 if (!DisplayServerAppleEmbedded::get_singleton()) {
318 return UIInterfaceOrientationMaskAll;
319 }
320
321 switch (DisplayServerAppleEmbedded::get_singleton()->screen_get_orientation(DisplayServer::SCREEN_OF_MAIN_WINDOW)) {
322 case DisplayServer::SCREEN_PORTRAIT:
323 return UIInterfaceOrientationMaskPortrait;
324 case DisplayServer::SCREEN_REVERSE_LANDSCAPE:
325 if (UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad) {
326 return UIInterfaceOrientationMaskLandscapeLeft;
327 } else {
328 return UIInterfaceOrientationMaskLandscapeRight;
329 }
330 case DisplayServer::SCREEN_REVERSE_PORTRAIT:
331 return UIInterfaceOrientationMaskPortraitUpsideDown;
332 case DisplayServer::SCREEN_SENSOR_LANDSCAPE:
333 return UIInterfaceOrientationMaskLandscape;
334 case DisplayServer::SCREEN_SENSOR_PORTRAIT:
335 return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
336 case DisplayServer::SCREEN_SENSOR:
337 return UIInterfaceOrientationMaskAll;
338 case DisplayServer::SCREEN_LANDSCAPE:
339 if (UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad) {
340 return UIInterfaceOrientationMaskLandscapeRight;
341 } else {
342 return UIInterfaceOrientationMaskLandscapeLeft;
343 }
344 }
345 }
346
347 - (BOOL)prefersStatusBarHidden {
348 if (GLOBAL_GET("display/window/ios/hide_status_bar")) {
349 return YES;
350 } else {
351 return NO;
352 }
353 }
354
355 - (BOOL)prefersHomeIndicatorAutoHidden {
356 if (GLOBAL_GET("display/window/ios/hide_home_indicator")) {
357 return YES;
358 } else {
359 return NO;
360 }
361 }
362
363 // MARK: Keyboard
364
365 - (void)keyboardOnScreen:(NSNotification *)notification {
366 NSDictionary *info = notification.userInfo;
367 NSValue *value = info[UIKeyboardFrameEndUserInfoKey];
368
369 CGRect rawFrame = [value CGRectValue];
370 CGRect keyboardFrame = [self.view convertRect:rawFrame fromView:nil];
371
372 if (DisplayServerAppleEmbedded::get_singleton()) {
373 DisplayServerAppleEmbedded::get_singleton()->virtual_keyboard_set_height(keyboardFrame.size.height);
374 }
375 }
376
377 - (void)keyboardHidden:(NSNotification *)notification {
378 if (DisplayServerAppleEmbedded::get_singleton()) {
379 DisplayServerAppleEmbedded::get_singleton()->virtual_keyboard_set_height(0);
380 }
381 }
382
383 @end