@preeternal/react-native-godot 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (109) hide show
  1. package/LICENSE +22 -0
  2. package/Package.swift +106 -0
  3. package/README.md +590 -2
  4. package/android/build.gradle +291 -0
  5. package/android/consumer-rules.pro +20 -0
  6. package/android/fix-prefab.gradle +54 -0
  7. package/android/src/main/AndroidManifest.xml +2 -0
  8. package/android/src/main/cpp/CMakeLists.txt +78 -0
  9. package/android/src/main/cpp/GodotModule.cpp +764 -0
  10. package/android/src/main/cpp/OnLoad.cpp +66 -0
  11. package/android/src/main/cpp/libgodot_android.h +73 -0
  12. package/android/src/main/cpp/libgodot_jni.cpp +658 -0
  13. package/android/src/main/cpp/libgodot_jni.h +116 -0
  14. package/android/src/main/cpp/native_godot_module_jni.cpp +86 -0
  15. package/android/src/main/cpp/native_godot_module_jni.h +58 -0
  16. package/android/src/main/java/com/preeternal/reactnativegodot/NativeGodotModuleModule.kt +83 -0
  17. package/android/src/main/java/com/preeternal/reactnativegodot/NativeGodotModulePackage.kt +41 -0
  18. package/android/src/main/java/com/preeternal/reactnativegodot/NativeLibraryLoader.kt +5 -0
  19. package/android/src/main/java/com/preeternal/reactnativegodot/RTNGodotIO.kt +123 -0
  20. package/android/src/main/java/com/preeternal/reactnativegodot/RTNGodotView.kt +92 -0
  21. package/android/src/main/java/com/preeternal/reactnativegodot/RTNGodotViewManager.kt +34 -0
  22. package/android/src/main/java/com/preeternal/reactnativegodot/RTNLibGodot.kt +215 -0
  23. package/android/src/main/res/layout/downloading_expansion.xml +165 -0
  24. package/android/src/main/res/layout/godot_app_layout.xml +5 -0
  25. package/android/src/main/res/layout/snackbar.xml +27 -0
  26. package/android/src/main/res/layout/status_bar_ongoing_event_progress_bar.xml +108 -0
  27. package/android/src/main/res/mipmap/icon.png +0 -0
  28. package/android/src/main/res/mipmap/icon_background.png +0 -0
  29. package/android/src/main/res/mipmap/icon_foreground.png +0 -0
  30. package/android/src/main/res/mipmap/icon_monochrome.png +0 -0
  31. package/android/src/main/res/mipmap-anydpi-v26/icon.xml +8 -0
  32. package/android/src/main/res/mipmap-anydpi-v26/themed_icon.xml +11 -0
  33. package/android/src/main/res/mipmap-hdpi/icon.png +0 -0
  34. package/android/src/main/res/mipmap-hdpi/icon_background.png +0 -0
  35. package/android/src/main/res/mipmap-hdpi/icon_foreground.png +0 -0
  36. package/android/src/main/res/mipmap-hdpi/icon_monochrome.png +0 -0
  37. package/android/src/main/res/mipmap-mdpi/icon.png +0 -0
  38. package/android/src/main/res/mipmap-mdpi/icon_background.png +0 -0
  39. package/android/src/main/res/mipmap-mdpi/icon_foreground.png +0 -0
  40. package/android/src/main/res/mipmap-mdpi/icon_monochrome.png +0 -0
  41. package/android/src/main/res/mipmap-xhdpi/icon.png +0 -0
  42. package/android/src/main/res/mipmap-xhdpi/icon_background.png +0 -0
  43. package/android/src/main/res/mipmap-xhdpi/icon_foreground.png +0 -0
  44. package/android/src/main/res/mipmap-xhdpi/icon_monochrome.png +0 -0
  45. package/android/src/main/res/mipmap-xxhdpi/icon.png +0 -0
  46. package/android/src/main/res/mipmap-xxhdpi/icon_background.png +0 -0
  47. package/android/src/main/res/mipmap-xxhdpi/icon_foreground.png +0 -0
  48. package/android/src/main/res/mipmap-xxhdpi/icon_monochrome.png +0 -0
  49. package/android/src/main/res/mipmap-xxxhdpi/icon.png +0 -0
  50. package/android/src/main/res/mipmap-xxxhdpi/icon_background.png +0 -0
  51. package/android/src/main/res/mipmap-xxxhdpi/icon_foreground.png +0 -0
  52. package/android/src/main/res/mipmap-xxxhdpi/icon_monochrome.png +0 -0
  53. package/android/src/main/res/values/dimens.xml +9 -0
  54. package/android/src/main/res/values/strings.xml +61 -0
  55. package/android/src/main/res/values/styles.xml +25 -0
  56. package/android/src/main/res/values-fa/strings.xml +14 -0
  57. package/android/src/main/res/values-ko/strings.xml +54 -0
  58. package/android/src/main/res/xml/godot_provider_paths.xml +15 -0
  59. package/android/src/test/java/com/preeternal/reactnativegodot/NativeGodotModulePackageTest.kt +26 -0
  60. package/app.plugin.js +169 -0
  61. package/cpp/AppleRendererPolicy.h +95 -0
  62. package/cpp/GodotModule.h +126 -0
  63. package/cpp/LibGodotABI.h +52 -0
  64. package/cpp/NativeGodotModule.cpp +825 -0
  65. package/cpp/NativeGodotModule.h +45 -0
  66. package/cpp/godot-log.h +64 -0
  67. package/ios/GodotModule.mm +695 -0
  68. package/ios/NativeGodotModuleProvider.h +35 -0
  69. package/ios/NativeGodotModuleProvider.mm +42 -0
  70. package/ios/RTNGodotView.h +42 -0
  71. package/ios/RTNGodotView.mm +671 -0
  72. package/ios/log.mm +38 -0
  73. package/lib/module/NativeNativeGodotModule.js +59 -0
  74. package/lib/module/NativeNativeGodotModule.js.map +1 -0
  75. package/lib/module/RTNGodotView.js +6 -0
  76. package/lib/module/RTNGodotView.js.map +1 -0
  77. package/lib/module/RTNGodotView.native.js +5 -0
  78. package/lib/module/RTNGodotView.native.js.map +1 -0
  79. package/lib/module/RTNGodotViewNativeComponent.ts +37 -0
  80. package/lib/module/index.js +30 -0
  81. package/lib/module/index.js.map +1 -0
  82. package/lib/module/package.json +1 -0
  83. package/lib/typescript/package.json +1 -0
  84. package/lib/typescript/src/NativeNativeGodotModule.d.ts +26 -0
  85. package/lib/typescript/src/NativeNativeGodotModule.d.ts.map +1 -0
  86. package/lib/typescript/src/RTNGodotView.d.ts +7 -0
  87. package/lib/typescript/src/RTNGodotView.d.ts.map +1 -0
  88. package/lib/typescript/src/RTNGodotView.native.d.ts +3 -0
  89. package/lib/typescript/src/RTNGodotView.native.d.ts.map +1 -0
  90. package/lib/typescript/src/RTNGodotViewNativeComponent.d.ts +11 -0
  91. package/lib/typescript/src/RTNGodotViewNativeComponent.d.ts.map +1 -0
  92. package/lib/typescript/src/index.d.ts +6 -0
  93. package/lib/typescript/src/index.d.ts.map +1 -0
  94. package/package.json +282 -4
  95. package/prebuilt-utils.rb +46 -0
  96. package/react-native-godot.podspec +41 -0
  97. package/react-native.config.js +16 -0
  98. package/scripts/check-16kb.sh +136 -0
  99. package/scripts/check-packaged-artifacts.sh +156 -0
  100. package/scripts/ci/verify-ios-simulator.py +48 -0
  101. package/scripts/ci/verify-prebuilt-install.cjs +41 -0
  102. package/scripts/download-prebuilt.js +283 -0
  103. package/scripts/prebuilt-config.js +80 -0
  104. package/scripts/spm-autolinking-plugin.js +87 -0
  105. package/src/NativeNativeGodotModule.ts +90 -0
  106. package/src/RTNGodotView.native.tsx +2 -0
  107. package/src/RTNGodotView.tsx +12 -0
  108. package/src/RTNGodotViewNativeComponent.ts +37 -0
  109. package/src/index.tsx +28 -0
@@ -0,0 +1,671 @@
1
+ /**************************************************************************/
2
+ /* RTNGodotView.mm */
3
+ /**************************************************************************/
4
+ /* Copyright (c) 2024-2025 Slay GmbH */
5
+ /* */
6
+ /* Permission is hereby granted, free of charge, to any person obtaining */
7
+ /* a copy of this software and associated documentation files (the */
8
+ /* "Software"), to deal in the Software without restriction, including */
9
+ /* without limitation the rights to use, copy, modify, merge, publish, */
10
+ /* distribute, sublicense, and/or sell copies of the Software, and to */
11
+ /* permit persons to whom the Software is furnished to do so, subject to */
12
+ /* the following conditions: */
13
+ /* */
14
+ /* The above copyright notice and this permission notice shall be */
15
+ /* included in all copies or substantial portions of the Software. */
16
+ /* */
17
+ /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
18
+ /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
19
+ /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
20
+ /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
21
+ /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
22
+ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
23
+ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
24
+ /**************************************************************************/
25
+
26
+ #import <React/RCTLog.h>
27
+ #import <React/RCTUIManager.h>
28
+
29
+ #include <cmath>
30
+
31
+ #include <godot_cpp/classes/display_server_embedded.hpp>
32
+ #include <godot_cpp/classes/engine.hpp>
33
+ #include <godot_cpp/classes/godot_instance.hpp>
34
+ #include <godot_cpp/classes/main_loop.hpp>
35
+ #include <godot_cpp/classes/rendering_native_surface.hpp>
36
+ #include <godot_cpp/classes/rendering_native_surface_apple.hpp>
37
+ #include <godot_cpp/classes/scene_tree.hpp>
38
+ #include <godot_cpp/classes/window.hpp>
39
+ #include <godot_cpp/core/math.hpp>
40
+ #include <godot_cpp/godot.hpp>
41
+ #include <godot_cpp/variant/utility_functions.hpp>
42
+
43
+ #include "GodotModule.h"
44
+
45
+ #import "RTNGodotView.h"
46
+
47
+ #import <react/renderer/components/RTNGodotSpec/ComponentDescriptors.h>
48
+ #import <react/renderer/components/RTNGodotSpec/EventEmitters.h>
49
+ #import <react/renderer/components/RTNGodotSpec/Props.h>
50
+ #import <react/renderer/components/RTNGodotSpec/RCTComponentViewHelpers.h>
51
+
52
+ using namespace facebook::react;
53
+
54
+ static const int MAX_TOUCH_COUNT = 32;
55
+
56
+ static NSHashTable<UIView *> *_views = [NSHashTable weakObjectsHashTable];
57
+ static __weak UIView *_currentView = nil;
58
+
59
+ @interface RTNGodotView () <RCTRTNGodotViewViewProtocol>
60
+ - (void)invalidateAttachment;
61
+ - (bool)ownsRenderingLayer;
62
+ - (void)addToGodotView;
63
+ - (void)removeFromGodotView:(bool)addAfter unregister:(bool)unregister;
64
+ @end
65
+ @implementation RTNGodotView {
66
+ NSString *_windowName;
67
+ CALayer *_renderingLayer;
68
+ uint64_t _windowId;
69
+ uint64_t _engineGeneration;
70
+ double _contentScaleFactor;
71
+ std::vector<UITouch *> _touches;
72
+ bool _propsUpdated;
73
+ bool _instanceCallbackRegistered;
74
+ bool _isMounted;
75
+ std::atomic<uint64_t> _attachmentGeneration;
76
+ std::atomic<uint64_t> _resizeGeneration;
77
+ godot::Vector2i _lastRequestedPixelSize;
78
+ }
79
+
80
+ + (BOOL)shouldBeRecycled {
81
+ return NO;
82
+ }
83
+
84
+ + (void)removeMainLayerFromGodotView:(RTNGodotView *)view promote:(bool)promote {
85
+ // The module has already cleared its layer on Stop. The view owns this reference.
86
+ CALayer *mainLayer = view->_renderingLayer;
87
+ if (promote) {
88
+ [_views removeObject:view];
89
+ }
90
+ if (_currentView != view) {
91
+ return;
92
+ }
93
+ if (mainLayer.superlayer == view.layer) {
94
+ [mainLayer removeFromSuperlayer];
95
+ }
96
+ // Keep the preferred host across Stop/Start, regardless of callback order.
97
+ if (!promote) {
98
+ return;
99
+ }
100
+ _currentView = nil;
101
+ const uint64_t generation = GodotModule::get_singleton()->get_instance_generation();
102
+ for (RTNGodotView *nextView in _views.allObjects) {
103
+ if (!nextView->_isMounted || nextView->_engineGeneration != generation ||
104
+ generation == 0 || !nextView->_renderingLayer) {
105
+ continue;
106
+ }
107
+ [nextView invalidateAttachment];
108
+ _currentView = nextView;
109
+ // The departing host may not have received the new engine's layer yet.
110
+ CALayer *nextLayer = nextView->_renderingLayer;
111
+ [CATransaction begin];
112
+ [CATransaction setDisableActions:YES];
113
+ nextLayer.hidden = YES;
114
+ [nextView.layer addSublayer:nextLayer];
115
+ [CATransaction commit];
116
+ nextView->_lastRequestedPixelSize = godot::Vector2i();
117
+ [nextView setNeedsLayout];
118
+ break;
119
+ }
120
+ }
121
+
122
+ + (void)addMainLayer:(CALayer *)mainLayer toGodotView:(RTNGodotView *)view {
123
+ const bool newHost = ![_views containsObject:view];
124
+ [_views addObject:view];
125
+ if (_currentView && _currentView != view && !newHost) {
126
+ return;
127
+ }
128
+ if (_currentView && _currentView != view) {
129
+ [(RTNGodotView *)_currentView invalidateAttachment];
130
+ }
131
+ [CATransaction begin];
132
+ [CATransaction setDisableActions:YES];
133
+ [mainLayer removeFromSuperlayer];
134
+ [view.layer addSublayer:mainLayer];
135
+ mainLayer.hidden = YES;
136
+ [CATransaction commit];
137
+ _currentView = view;
138
+ }
139
+
140
+ - (instancetype)init {
141
+ if (self = [super init]) {
142
+ // Fabric requires the component's own default props before the first update.
143
+ static const auto defaultProps = std::make_shared<const RTNGodotViewProps>();
144
+ _props = defaultProps;
145
+ [self setDefaultValues];
146
+ }
147
+ return self;
148
+ }
149
+
150
+ - (void)setDefaultValues {
151
+ while (self.subviews.firstObject) {
152
+ [self.subviews.firstObject removeFromSuperview];
153
+ }
154
+ self.multipleTouchEnabled = YES;
155
+
156
+ _renderingLayer = nil;
157
+ _windowId = 0;
158
+ _engineGeneration = 0;
159
+ _contentScaleFactor = 1.0;
160
+ _touches.reserve(MAX_TOUCH_COUNT);
161
+ for (int i = 0; i < MAX_TOUCH_COUNT; ++i) {
162
+ _touches.push_back(nil);
163
+ }
164
+ _propsUpdated = false;
165
+ _instanceCallbackRegistered = false;
166
+ _windowName = @"";
167
+ _isMounted = false;
168
+ _attachmentGeneration = 0;
169
+ _resizeGeneration = 0;
170
+ _lastRequestedPixelSize = godot::Vector2i();
171
+ }
172
+
173
+ //Setter method
174
+ - (void)setWindowName:(NSString *)n {
175
+ if (n == nil) {
176
+ n = @"";
177
+ }
178
+ if ([_windowName isEqualToString:n]) {
179
+ return;
180
+ }
181
+
182
+ NSLog(@"Setting windowName to: %@", n);
183
+ if (_isMounted) {
184
+ [self removeFromGodotView:false unregister:true];
185
+ }
186
+ _windowName = n;
187
+ if (_isMounted) {
188
+ [self addToGodotView];
189
+ }
190
+ }
191
+
192
+ //Getter method
193
+ - (NSString *)windowName {
194
+ NSLog(@"Returning windowName: %@", _windowName);
195
+ return _windowName;
196
+ }
197
+
198
+ - (void)setNeedsLayout {
199
+ [super setNeedsLayout];
200
+ [self invalidateIntrinsicContentSize];
201
+ }
202
+
203
+ - (CGSize)intrinsicContentSize {
204
+ return [self sizeThatFits:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)];
205
+ }
206
+
207
+ - (void)dealloc {
208
+ if (_instanceCallbackRegistered) {
209
+ GodotModule::get_singleton()->unregisterWindowUpdateCallback((__bridge void *)self);
210
+ }
211
+ if ([NSThread isMainThread]) {
212
+ [RTNGodotView removeMainLayerFromGodotView:self promote:true];
213
+ }
214
+ }
215
+
216
+ - (void)invalidateAttachment {
217
+ ++_attachmentGeneration;
218
+ ++_resizeGeneration;
219
+ std::vector<int> ids;
220
+ for (int i = 0; i < MAX_TOUCH_COUNT; ++i) {
221
+ if (_touches[i]) {
222
+ ids.push_back(i);
223
+ }
224
+ _touches[i] = nil;
225
+ }
226
+ const uint64_t windowId = _windowId;
227
+ if (ids.empty() || !windowId) {
228
+ return;
229
+ }
230
+ GodotModule::get_singleton()->runWithInstance(_engineGeneration, [windowId, ids]() {
231
+ godot::Window *window = godot::Object::cast_to<godot::Window>(godot::UtilityFunctions::instance_from_id(windowId));
232
+ auto *dse = godot::DisplayServerEmbedded::get_singleton();
233
+ if (window && dse) {
234
+ for (int id : ids) {
235
+ dse->touches_canceled(id, window->get_window_id());
236
+ }
237
+ }
238
+ });
239
+ }
240
+
241
+ - (bool)ownsRenderingLayer {
242
+ return _isMounted && _windowId && _renderingLayer &&
243
+ _engineGeneration != 0 &&
244
+ _engineGeneration == GodotModule::get_singleton()->get_instance_generation() &&
245
+ _renderingLayer.superlayer == self.layer &&
246
+ (![@"" isEqualToString:_windowName] || _currentView == self);
247
+ }
248
+
249
+ - (void)addToGodotView {
250
+ if (!_isMounted) {
251
+ return;
252
+ }
253
+ auto *module = GodotModule::get_singleton();
254
+ if (!_instanceCallbackRegistered) {
255
+ __weak RTNGodotView *weakSelf = self;
256
+ module->registerWindowUpdateCallback([_windowName UTF8String], (__bridge void *)self, [weakSelf](bool adding) {
257
+ const uint64_t generation = GodotModule::get_singleton()->get_instance_generation();
258
+ dispatch_async(dispatch_get_main_queue(), ^{
259
+ RTNGodotView *view = weakSelf;
260
+ if (!view){ return;
261
+ }
262
+ if (adding) {
263
+ if (generation == GodotModule::get_singleton()->get_instance_generation()){ [view addToGodotView];
264
+ }
265
+ } else if (view->_engineGeneration == generation) {
266
+ [view removeFromGodotView:false unregister:false];
267
+ }
268
+ }); }, nullptr);
269
+ _instanceCallbackRegistered = true;
270
+ }
271
+ const uint64_t engineGeneration = module->get_instance_generation();
272
+ if (_engineGeneration && _engineGeneration != engineGeneration) {
273
+ [self removeFromGodotView:false unregister:false];
274
+ }
275
+ if (!engineGeneration || (_windowId && _engineGeneration == engineGeneration)) {
276
+ return;
277
+ }
278
+ const uint64_t attachmentGeneration = ++_attachmentGeneration;
279
+ const std::string windowName = [_windowName UTF8String];
280
+ const CGSize initialSize = self.bounds.size;
281
+ module->runWithInstance(engineGeneration, [=]() {
282
+ if (self->_attachmentGeneration.load() != attachmentGeneration) {
283
+ return;
284
+ }
285
+ auto *tree = godot::Object::cast_to<godot::SceneTree>(godot::Engine::get_singleton()->get_main_loop());
286
+ if (!tree) {
287
+ return;
288
+ }
289
+ auto *window = windowName.empty() ? tree->get_root() : godot::Object::cast_to<godot::Window>(tree->get_root()->find_child(godot::String::utf8(windowName.c_str()), true, false));
290
+ if (!window) {
291
+ return;
292
+ }
293
+ const uint64_t windowId = window->get_instance_id();
294
+ const double scale = module->get_content_scale_factor();
295
+ CALayer *layer = nil;
296
+ if (windowName.empty()) {
297
+ layer = (__bridge CALayer *)module->get_main_rendering_layer();
298
+ } else {
299
+ godot::Ref<godot::RenderingNativeSurfaceApple> surface = window->get_native_surface();
300
+ if (surface.is_null()) {
301
+ surface = godot::RenderingNativeSurfaceApple::create(0);
302
+ if (surface.is_null()) {
303
+ return;
304
+ }
305
+ layer = (__bridge CALayer *)(void *)surface->get_layer();
306
+ layer.bounds = CGRectMake(0, 0, godot::MAX(1, initialSize.width), godot::MAX(1, initialSize.height));
307
+ layer.anchorPoint = CGPointZero;
308
+ layer.position = CGPointZero;
309
+ layer.contentsScale = scale;
310
+ window->set_native_surface(surface);
311
+ } else {
312
+ layer = (__bridge CALayer *)(void *)surface->get_layer();
313
+ }
314
+ window->set_visible(true);
315
+ __weak RTNGodotView *weakSelf = self;
316
+ godot::Callable exited = module->create_callable([=](const godot::Variant **, int, godot::Variant &, GDExtensionCallError &) {
317
+ dispatch_async(dispatch_get_main_queue(), ^{
318
+ RTNGodotView *view = weakSelf;
319
+ if (view && view->_engineGeneration == engineGeneration &&
320
+ view->_attachmentGeneration.load() == attachmentGeneration && view->_windowId == windowId) {
321
+ [view removeFromGodotView:false unregister:false];
322
+ }
323
+ });
324
+ });
325
+ window->connect("tree_exited", exited);
326
+ }
327
+ if (!layer) {
328
+ return;
329
+ }
330
+ // Only retain a CALayer across the UI handoff, never a Godot binding/Ref.
331
+ dispatch_async(dispatch_get_main_queue(), ^{
332
+ if (!self->_isMounted || self->_attachmentGeneration.load() != attachmentGeneration ||
333
+ engineGeneration != module->get_instance_generation()) {
334
+ return;
335
+ }
336
+ self->_windowId = windowId;
337
+ self->_engineGeneration = engineGeneration;
338
+ self->_contentScaleFactor = scale;
339
+ self->_renderingLayer = layer;
340
+ self->_lastRequestedPixelSize = godot::Vector2i();
341
+ if (windowName.empty()) {
342
+ [RTNGodotView addMainLayer:layer toGodotView:self];
343
+ } else {
344
+ [self.layer addSublayer:layer];
345
+ layer.hidden = YES;
346
+ }
347
+ [self setNeedsLayout];
348
+ });
349
+ });
350
+ }
351
+
352
+ - (void)removeFromGodotView:(bool)addAfter unregister:(bool)unregister {
353
+ // All callers are on the main queue; teardown callbacks carry their old engine generation.
354
+ [self invalidateAttachment];
355
+ if (unregister) {
356
+ GodotModule::get_singleton()->unregisterWindowUpdateCallback((__bridge void *)self);
357
+ _instanceCallbackRegistered = false;
358
+ }
359
+ if ([@"" isEqualToString:_windowName]) {
360
+ [RTNGodotView removeMainLayerFromGodotView:self promote:unregister];
361
+ } else if (_renderingLayer.superlayer == self.layer) {
362
+ [_renderingLayer removeFromSuperlayer];
363
+ }
364
+ _windowId = 0;
365
+ _engineGeneration = 0;
366
+ _renderingLayer = nil;
367
+ _lastRequestedPixelSize = godot::Vector2i();
368
+ if (addAfter) {
369
+ [self addToGodotView];
370
+ }
371
+ }
372
+
373
+ - (void)layoutSubviews {
374
+ [super layoutSubviews];
375
+
376
+ if (![self ownsRenderingLayer]) {
377
+ return;
378
+ }
379
+
380
+ CGSize renderingSize = self.bounds.size;
381
+ if (renderingSize.width <= 0 || renderingSize.height <= 0) {
382
+ return;
383
+ }
384
+
385
+ double contentScaleFactor = _contentScaleFactor;
386
+ CGRect renderingFrame = CGRectMake(0, 0, renderingSize.width, renderingSize.height);
387
+ uint64_t windowId = _windowId;
388
+ godot::Vector2i pixelSize(
389
+ godot::MAX(1, (int32_t)std::lround(renderingSize.width * contentScaleFactor)),
390
+ godot::MAX(1, (int32_t)std::lround(renderingSize.height * contentScaleFactor)));
391
+
392
+ [CATransaction begin];
393
+ [CATransaction setDisableActions:YES];
394
+ _renderingLayer.frame = renderingFrame;
395
+ [CATransaction commit];
396
+
397
+ if (_lastRequestedPixelSize == pixelSize) {
398
+ return;
399
+ }
400
+
401
+ _lastRequestedPixelSize = pixelSize;
402
+ const uint64_t attachmentGeneration = _attachmentGeneration.load();
403
+ const uint64_t resizeGeneration = ++_resizeGeneration;
404
+ CALayer *renderingLayer = _renderingLayer;
405
+
406
+ [CATransaction begin];
407
+ [CATransaction setDisableActions:YES];
408
+ renderingLayer.hidden = YES;
409
+ [CATransaction commit];
410
+
411
+ const uint64_t engineGeneration = _engineGeneration;
412
+ GodotModule::get_singleton()->runWithInstance(engineGeneration, [=]() {
413
+ if (self->_attachmentGeneration.load() != attachmentGeneration || self->_resizeGeneration.load() != resizeGeneration) {
414
+ return;
415
+ }
416
+ godot::DisplayServerEmbedded *dse = godot::DisplayServerEmbedded::get_singleton();
417
+ if (dse && godot::UtilityFunctions::is_instance_id_valid(windowId)) {
418
+ godot::Object *obj = godot::UtilityFunctions::instance_from_id(windowId);
419
+ godot::Window *window = godot::Object::cast_to<godot::Window>(obj);
420
+ if (window) {
421
+ dse->resize_window(pixelSize, window->get_window_id());
422
+ // Submit a frame at the new size before exposing the layer. This
423
+ // orders CPU work; it is not a GPU presentation-completion signal.
424
+ GodotModule::get_singleton()->iterate();
425
+ dispatch_async(dispatch_get_main_queue(), ^{
426
+ if (![self ownsRenderingLayer] ||
427
+ self->_attachmentGeneration.load() != attachmentGeneration ||
428
+ self->_resizeGeneration.load() != resizeGeneration ||
429
+ self->_windowId != windowId ||
430
+ self->_renderingLayer != renderingLayer) {
431
+ return;
432
+ }
433
+
434
+ [CATransaction begin];
435
+ [CATransaction setDisableActions:YES];
436
+ renderingLayer.hidden = NO;
437
+ [CATransaction commit];
438
+
439
+ if (self->_eventEmitter) {
440
+ static_cast<const RTNGodotViewEventEmitter &>(*self->_eventEmitter).onSurfaceReady({});
441
+ }
442
+ });
443
+ }
444
+ }
445
+ });
446
+ }
447
+
448
+ - (int)getTouchId:(UITouch *)touch {
449
+ int first = -1;
450
+ for (int i = 0; i < MAX_TOUCH_COUNT; ++i) {
451
+ if (first == -1 && (i >= _touches.size() || _touches[i] == nil)) {
452
+ first = i;
453
+ continue;
454
+ }
455
+ if (_touches[i] == touch) {
456
+ return i;
457
+ }
458
+ }
459
+
460
+ if (first != -1) {
461
+ _touches[first] = touch;
462
+ return first;
463
+ }
464
+
465
+ return -1;
466
+ }
467
+
468
+ - (void)removeTouchId:(int)touchId {
469
+ _touches[touchId] = nil;
470
+ }
471
+
472
+ - (void)touchesBegan:(NSSet<UITouch *> *)touches
473
+ withEvent:(UIEvent *)event {
474
+ if (![self ownsRenderingLayer]) {
475
+ return;
476
+ }
477
+ const uint64_t windowId = _windowId;
478
+ const uint64_t engineGeneration = _engineGeneration;
479
+ const uint64_t attachmentGeneration = _attachmentGeneration.load();
480
+
481
+ for (UITouch *touch in touches) {
482
+ int touchId = [self getTouchId:touch];
483
+ if (touchId == -1) {
484
+ continue;
485
+ }
486
+ CGPoint location = [touch locationInView:self];
487
+ if (!CGRectContainsPoint(_renderingLayer.frame, location)) {
488
+ continue;
489
+ }
490
+ location.x -= _renderingLayer.frame.origin.x;
491
+ location.y -= _renderingLayer.frame.origin.y;
492
+ NSUInteger tapCount = touch.tapCount;
493
+ double contentScaleFactor = _contentScaleFactor;
494
+ GodotModule::get_singleton()->runWithInstance(engineGeneration, [=]() {
495
+ if (self->_attachmentGeneration.load() != attachmentGeneration) {
496
+ return;
497
+ }
498
+ auto *dse = godot::DisplayServerEmbedded::get_singleton();
499
+ if (dse && godot::UtilityFunctions::is_instance_id_valid(windowId)) {
500
+ godot::Object *obj = godot::UtilityFunctions::instance_from_id(windowId);
501
+ godot::Window *window = godot::Object::cast_to<godot::Window>(obj);
502
+ if (!window) {
503
+ return;
504
+ }
505
+ dse->touch_press(touchId, location.x * contentScaleFactor, location.y * contentScaleFactor, true, tapCount > 1, window->get_window_id());
506
+ }
507
+ });
508
+ }
509
+ }
510
+
511
+ - (void)touchesMoved:(NSSet<UITouch *> *)touches
512
+ withEvent:(UIEvent *)event {
513
+ if (![self ownsRenderingLayer]) {
514
+ return;
515
+ }
516
+ const uint64_t windowId = _windowId;
517
+ const uint64_t engineGeneration = _engineGeneration;
518
+ const uint64_t attachmentGeneration = _attachmentGeneration.load();
519
+
520
+ for (UITouch *touch in touches) {
521
+ int touchId = [self getTouchId:touch];
522
+ if (touchId == -1) {
523
+ continue;
524
+ }
525
+ CGPoint location = [touch locationInView:self];
526
+ if (!CGRectContainsPoint(_renderingLayer.frame, location)) {
527
+ continue;
528
+ }
529
+ location.x -= _renderingLayer.frame.origin.x;
530
+ location.y -= _renderingLayer.frame.origin.y;
531
+ CGPoint prevLocation = [touch previousLocationInView:self];
532
+ if (!CGRectContainsPoint(_renderingLayer.frame, prevLocation)) {
533
+ continue;
534
+ }
535
+ prevLocation.x -= _renderingLayer.frame.origin.x;
536
+ prevLocation.y -= _renderingLayer.frame.origin.y;
537
+ CGFloat alt = touch.altitudeAngle;
538
+ CGVector azim = [touch azimuthUnitVectorInView:self];
539
+ CGFloat force = touch.force;
540
+ CGFloat maximumPossibleForce = touch.maximumPossibleForce;
541
+ double contentScaleFactor = _contentScaleFactor;
542
+ GodotModule::get_singleton()->runWithInstance(engineGeneration, [=]() {
543
+ if (self->_attachmentGeneration.load() != attachmentGeneration) {
544
+ return;
545
+ }
546
+ auto *dse = godot::DisplayServerEmbedded::get_singleton();
547
+ if (dse && godot::UtilityFunctions::is_instance_id_valid(windowId)) {
548
+ godot::Object *obj = godot::UtilityFunctions::instance_from_id(windowId);
549
+ godot::Window *window = godot::Object::cast_to<godot::Window>(obj);
550
+ if (!window) {
551
+ return;
552
+ }
553
+ dse->touch_drag(
554
+ touchId,
555
+ prevLocation.x * contentScaleFactor,
556
+ prevLocation.y * contentScaleFactor,
557
+ location.x * contentScaleFactor,
558
+ location.y * contentScaleFactor,
559
+ force / maximumPossibleForce,
560
+ godot::Vector2(azim.dx * cos(alt), azim.dy * cos(alt)),
561
+ window->get_window_id());
562
+ }
563
+ });
564
+ }
565
+ }
566
+
567
+ - (void)touchesEnded:(NSSet<UITouch *> *)touches
568
+ withEvent:(UIEvent *)event {
569
+ if (![self ownsRenderingLayer]) {
570
+ return;
571
+ }
572
+ const uint64_t windowId = _windowId;
573
+ const uint64_t engineGeneration = _engineGeneration;
574
+ const uint64_t attachmentGeneration = _attachmentGeneration.load();
575
+
576
+ for (UITouch *touch in touches) {
577
+ int touchId = [self getTouchId:touch];
578
+ if (touchId == -1) {
579
+ continue;
580
+ }
581
+ [self removeTouchId:touchId];
582
+ CGPoint location = [touch locationInView:self];
583
+ if (!CGRectContainsPoint(_renderingLayer.frame, location)) {
584
+ continue;
585
+ }
586
+ location.x -= _renderingLayer.frame.origin.x;
587
+ location.y -= _renderingLayer.frame.origin.y;
588
+ double contentScaleFactor = _contentScaleFactor;
589
+ GodotModule::get_singleton()->runWithInstance(engineGeneration, [=]() {
590
+ if (self->_attachmentGeneration.load() != attachmentGeneration) {
591
+ return;
592
+ }
593
+ auto *dse = godot::DisplayServerEmbedded::get_singleton();
594
+ if (dse && godot::UtilityFunctions::is_instance_id_valid(windowId)) {
595
+ godot::Object *obj = godot::UtilityFunctions::instance_from_id(windowId);
596
+ godot::Window *window = godot::Object::cast_to<godot::Window>(obj);
597
+ if (!window) {
598
+ return;
599
+ }
600
+ dse->touch_press(touchId, location.x * contentScaleFactor, location.y * contentScaleFactor, false, false, window->get_window_id());
601
+ }
602
+ });
603
+ }
604
+ }
605
+
606
+ - (void)touchesCancelled:(NSSet<UITouch *> *)touches
607
+ withEvent:(UIEvent *)event {
608
+ if (![self ownsRenderingLayer]) {
609
+ return;
610
+ }
611
+ const uint64_t windowId = _windowId;
612
+ const uint64_t engineGeneration = _engineGeneration;
613
+ const uint64_t attachmentGeneration = _attachmentGeneration.load();
614
+
615
+ for (UITouch *touch in touches) {
616
+ int touchId = [self getTouchId:touch];
617
+ if (touchId == -1) {
618
+ continue;
619
+ }
620
+ [self removeTouchId:touchId];
621
+ GodotModule::get_singleton()->runWithInstance(engineGeneration, [=]() {
622
+ if (self->_attachmentGeneration.load() != attachmentGeneration) {
623
+ return;
624
+ }
625
+ auto *dse = godot::DisplayServerEmbedded::get_singleton();
626
+ if (dse && godot::UtilityFunctions::is_instance_id_valid(windowId)) {
627
+ godot::Object *obj = godot::UtilityFunctions::instance_from_id(windowId);
628
+ godot::Window *window = godot::Object::cast_to<godot::Window>(obj);
629
+ if (!window) {
630
+ return;
631
+ }
632
+ dse->touches_canceled(touchId, window->get_window_id());
633
+ }
634
+ });
635
+ }
636
+ }
637
+
638
+ - (void)willMoveToSuperview:(UIView *)newSuperview {
639
+ [super willMoveToSuperview:newSuperview];
640
+ NSLog(@"RTNGodotView: %@ will move to superview %@", self, newSuperview);
641
+ _isMounted = newSuperview != nil;
642
+ if (newSuperview == nil) {
643
+ [self removeFromGodotView:false unregister:true];
644
+ } else {
645
+ [self addToGodotView];
646
+ }
647
+ }
648
+
649
+ - (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &)oldProps {
650
+ [super updateProps:props oldProps:oldProps];
651
+
652
+ // Handle your props here
653
+ if (props != nullptr &&
654
+ (oldProps == nullptr ||
655
+ std::static_pointer_cast<RTNGodotViewProps const>(oldProps)->windowName !=
656
+ std::static_pointer_cast<RTNGodotViewProps const>(props)->windowName)) {
657
+ const auto &newViewProps = *std::static_pointer_cast<RTNGodotViewProps const>(props);
658
+ [self setWindowName:[NSString stringWithUTF8String:newViewProps.windowName.c_str()]];
659
+ }
660
+ }
661
+
662
+ - (void)didMoveToSuperview {
663
+ [super didMoveToSuperview];
664
+ NSLog(@"RTNGodotView: %@ did move to superview %@", self, self.superview);
665
+ }
666
+
667
+ + (ComponentDescriptorProvider)componentDescriptorProvider {
668
+ return concreteComponentDescriptorProvider<RTNGodotViewComponentDescriptor>();
669
+ }
670
+
671
+ @end
package/ios/log.mm ADDED
@@ -0,0 +1,38 @@
1
+ /**************************************************************************/
2
+ /* log.mm */
3
+ /**************************************************************************/
4
+ /* Copyright (c) 2024-2025 Slay GmbH */
5
+ /* */
6
+ /* Permission is hereby granted, free of charge, to any person obtaining */
7
+ /* a copy of this software and associated documentation files (the */
8
+ /* "Software"), to deal in the Software without restriction, including */
9
+ /* without limitation the rights to use, copy, modify, merge, publish, */
10
+ /* distribute, sublicense, and/or sell copies of the Software, and to */
11
+ /* permit persons to whom the Software is furnished to do so, subject to */
12
+ /* the following conditions: */
13
+ /* */
14
+ /* The above copyright notice and this permission notice shall be */
15
+ /* included in all copies or substantial portions of the Software. */
16
+ /* */
17
+ /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
18
+ /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
19
+ /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
20
+ /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
21
+ /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
22
+ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
23
+ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
24
+ /**************************************************************************/
25
+
26
+ #define LOG_TAG "IOSLog"
27
+ #include "godot-log.h"
28
+
29
+ #include <Foundation/Foundation.h>
30
+ #include <stdarg.h>
31
+
32
+ void __ios_log_print(ios_LogPriority prio, const char *tag, ...) {
33
+ va_list ap;
34
+ va_start(ap, tag);
35
+ NSString *format = [NSString stringWithUTF8String:va_arg(ap, const char *)];
36
+ NSLogv(format, ap);
37
+ va_end(ap);
38
+ }