@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,658 @@
1
+ /**************************************************************************/
2
+ /* libgodot_jni.cpp */
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
+ #include "libgodot_jni.h"
27
+ #define LOG_TAG "LibGodotJNI"
28
+ #include "godot-log.h"
29
+
30
+ #include <math.h>
31
+
32
+ #include <GodotModule.h>
33
+ #include <android/input.h>
34
+ #include <android/native_window_jni.h>
35
+ #include <godot_cpp/classes/display_server.hpp>
36
+ #include <godot_cpp/classes/display_server_embedded.hpp>
37
+ #include <godot_cpp/classes/engine.hpp>
38
+ #include <godot_cpp/classes/input.hpp>
39
+ #include <godot_cpp/classes/input_event_screen_drag.hpp>
40
+ #include <godot_cpp/classes/input_event_screen_touch.hpp>
41
+ #include <godot_cpp/classes/main_loop.hpp>
42
+ #include <godot_cpp/classes/rendering_native_surface_android.hpp>
43
+ #include <godot_cpp/classes/scene_tree.hpp>
44
+ #include <godot_cpp/classes/window.hpp>
45
+
46
+ #include <godot_cpp/classes/project_settings.hpp>
47
+ #include <godot_cpp/classes/rendering_server.hpp>
48
+ #include <map>
49
+ #include <memory>
50
+ #include <mutex>
51
+ #include <string>
52
+ #include <vector>
53
+
54
+ using NativeWindowRef = std::shared_ptr<ANativeWindow>;
55
+
56
+ static NativeWindowRef adoptWindow(ANativeWindow *window) {
57
+ return NativeWindowRef(window, [](ANativeWindow *value) {
58
+ if (value) {
59
+ ANativeWindow_release(value);
60
+ }
61
+ });
62
+ }
63
+
64
+ struct WindowData {
65
+ int width;
66
+ int height;
67
+ int32_t id;
68
+ NativeWindowRef surface;
69
+ // Keep the engine's borrowed ANativeWindow alive until replacement or detach.
70
+ NativeWindowRef attachedSurface;
71
+ bool active = true;
72
+ bool updatePending = false;
73
+ uint64_t attachmentGeneration = 1;
74
+ WindowData(NativeWindowRef p_surface, int p_width, int p_height, int32_t p_id = -1) :
75
+ width(p_width), height(p_height), id(p_id), surface(std::move(p_surface)) {}
76
+ };
77
+
78
+ static std::map<std::string, std::shared_ptr<WindowData>> windowMap;
79
+ static std::mutex windowMapMutex;
80
+
81
+ void LibGodot::initialize(JNIEnv *env, jobject p_asset_manager, jobject p_net_utils, jobject p_dir_access_handler, jobject p_file_access_handler, jobject p_godot_io, jobject p_main_surface, jint p_width, jint p_height) {
82
+ LOGI("LibGodot::initialize");
83
+ env->GetJavaVM(&java_vm);
84
+ asset_manager = env->NewGlobalRef(p_asset_manager);
85
+ net_utils = env->NewGlobalRef(p_net_utils);
86
+ dir_access_handler = env->NewGlobalRef(p_dir_access_handler);
87
+ file_access_handler = env->NewGlobalRef(p_file_access_handler);
88
+ godot_io = env->NewGlobalRef(p_godot_io);
89
+ ANativeWindow *mainSurface = ANativeWindow_fromSurface(env, p_main_surface);
90
+ std::lock_guard<std::mutex> lock(windowMapMutex);
91
+ windowMap[""] = std::make_shared<WindowData>(adoptWindow(mainSurface), p_width, p_height, 0);
92
+ }
93
+
94
+ ANativeWindow *LibGodot::get_main_surface() {
95
+ std::lock_guard<std::mutex> lock(windowMapMutex);
96
+ auto it = windowMap.find("");
97
+ if (it == windowMap.end()) {
98
+ return nullptr;
99
+ }
100
+ it->second->attachedSurface = it->second->surface;
101
+ return it->second->surface.get();
102
+ }
103
+
104
+ int LibGodot::get_main_width() {
105
+ std::lock_guard<std::mutex> lock(windowMapMutex);
106
+ auto it = windowMap.find("");
107
+ return it == windowMap.end() ? 0 : it->second->width;
108
+ }
109
+
110
+ int LibGodot::get_main_height() {
111
+ std::lock_guard<std::mutex> lock(windowMapMutex);
112
+ auto it = windowMap.find("");
113
+ return it == windowMap.end() ? 0 : it->second->height;
114
+ }
115
+
116
+ void LibGodot::cleanup(JNIEnv *env) {
117
+ if (asset_manager) {
118
+ env->DeleteGlobalRef(asset_manager);
119
+ asset_manager = nullptr;
120
+ }
121
+ if (net_utils) {
122
+ env->DeleteGlobalRef(net_utils);
123
+ net_utils = nullptr;
124
+ }
125
+ if (dir_access_handler) {
126
+ env->DeleteGlobalRef(dir_access_handler);
127
+ dir_access_handler = nullptr;
128
+ }
129
+ if (file_access_handler) {
130
+ env->DeleteGlobalRef(file_access_handler);
131
+ file_access_handler = nullptr;
132
+ }
133
+ if (godot_io) {
134
+ env->DeleteGlobalRef(godot_io);
135
+ godot_io = nullptr;
136
+ }
137
+ {
138
+ std::lock_guard<std::mutex> lock(windowMapMutex);
139
+ for (const auto &[name, state] : windowMap) {
140
+ state->active = false;
141
+ ++state->attachmentGeneration;
142
+ }
143
+ windowMap.clear();
144
+ }
145
+
146
+ java_vm = nullptr;
147
+ }
148
+
149
+ JNIEnv *LibGodot::get_jni_env() {
150
+ if (!java_vm) {
151
+ LOGE("LibGodot::get_jni_env() called before JavaVM initialization");
152
+ return nullptr;
153
+ }
154
+ JNIEnv *env;
155
+ java_vm->AttachCurrentThread(&env, nullptr);
156
+ return env;
157
+ }
158
+
159
+ void LibGodot::set_java_vm(JavaVM *vm) {
160
+ java_vm = vm;
161
+ }
162
+
163
+ static void applyWindowUpdate(const std::string &p_window_name, const std::shared_ptr<WindowData> &state) {
164
+ int p_width, p_height;
165
+ NativeWindowRef surface;
166
+ {
167
+ std::lock_guard<std::mutex> lock(windowMapMutex);
168
+ state->updatePending = false;
169
+ if (!state->active) {
170
+ return;
171
+ }
172
+ p_width = state->width;
173
+ p_height = state->height;
174
+ surface = state->surface;
175
+ }
176
+ // This function and engine teardown are serialized on the Godot thread.
177
+ auto *instance = GodotModule::get_singleton()->get_instance();
178
+ if (!instance || !instance->is_started() || !surface || p_width <= 0 || p_height <= 0) {
179
+ return;
180
+ }
181
+ ANativeWindow *p_window_surface = surface.get();
182
+ godot::DisplayServerEmbedded *dse = godot::DisplayServerEmbedded::get_singleton();
183
+ if (!dse) {
184
+ return;
185
+ }
186
+ int32_t windowId = -1;
187
+ if (p_window_name == "") {
188
+ // Default id
189
+ windowId = 0;
190
+ } else {
191
+ // Find window
192
+ godot::MainLoop *mainLoop = godot::Engine::get_singleton()->get_main_loop();
193
+ godot::SceneTree *sceneTree = godot::Object::cast_to<godot::SceneTree>(mainLoop);
194
+ if (!sceneTree) {
195
+ LOGE("Unable to get SceneTree from Godot!");
196
+ return;
197
+ }
198
+ godot::Node *node = sceneTree->get_root()->find_child(
199
+ godot::String::utf8(p_window_name.c_str()), true, false);
200
+ godot::Window *newWindow = godot::Object::cast_to<godot::Window>(node);
201
+
202
+ if (newWindow) {
203
+ bool change_surface = true;
204
+ godot::Ref<godot::RenderingNativeSurfaceAndroid> ns = newWindow->get_native_surface();
205
+ if (ns.is_valid()) {
206
+ ANativeWindow *current_window = (ANativeWindow *)ns->get_window();
207
+ if (current_window == p_window_surface) {
208
+ change_surface = false;
209
+ }
210
+ }
211
+
212
+ if (change_surface) {
213
+ LOGI("Changing surface");
214
+ godot::Ref<godot::RenderingNativeSurfaceAndroid> androidSurface = godot::RenderingNativeSurfaceAndroid::create(
215
+ (uint64_t)p_window_surface, p_width, p_height);
216
+ if (androidSurface.is_null()) {
217
+ return;
218
+ }
219
+
220
+ newWindow->set_visible(true);
221
+ newWindow->set_native_surface(androidSurface);
222
+ }
223
+
224
+ windowId = newWindow->get_window_id();
225
+ }
226
+ }
227
+ if (windowId >= 0) {
228
+ LOGI("Resizing Window: %d %d %d", windowId, p_width, p_height);
229
+ dse->resize_window(godot::Vector2i(p_width, p_height), windowId);
230
+ std::lock_guard<std::mutex> lock(windowMapMutex);
231
+ state->id = windowId;
232
+ state->attachedSurface = surface;
233
+ }
234
+ }
235
+
236
+ static void scheduleWindowUpdate(const std::string &name, const std::shared_ptr<WindowData> &state) {
237
+ {
238
+ std::lock_guard<std::mutex> lock(windowMapMutex);
239
+ if (!state->active || state->updatePending) {
240
+ return;
241
+ }
242
+ state->updatePending = true;
243
+ }
244
+ GodotModule::get_singleton()->runOnGodotThread([name, state]() {
245
+ applyWindowUpdate(name, state);
246
+ });
247
+ }
248
+
249
+ void LibGodot::updateWindowNative(JNIEnv *env, jstring p_name, jobject p_surface, jint p_width, jint p_height) {
250
+ std::string windowName;
251
+ {
252
+ jboolean isCopy;
253
+ const char *val = env->GetStringUTFChars(p_name, &isCopy);
254
+ windowName = val;
255
+ env->ReleaseStringUTFChars(p_name, val);
256
+ }
257
+
258
+ NativeWindowRef surface = adoptWindow(ANativeWindow_fromSurface(env, p_surface));
259
+ if (!surface || p_width <= 0 || p_height <= 0) {
260
+ return;
261
+ }
262
+ std::shared_ptr<WindowData> state;
263
+ {
264
+ std::lock_guard<std::mutex> lock(windowMapMutex);
265
+ auto &entry = windowMap[windowName];
266
+ if (!entry) {
267
+ entry = std::make_shared<WindowData>(surface, p_width, p_height, windowName.empty() ? 0 : -1);
268
+ }
269
+ state = entry;
270
+ if (!state->active || state->surface.get() != surface.get()) {
271
+ ++state->attachmentGeneration;
272
+ }
273
+ state->surface = std::move(surface);
274
+ state->active = true;
275
+ state->width = p_width;
276
+ state->height = p_height;
277
+ }
278
+ // Only one pending job per window; it reads the latest requested size.
279
+ scheduleWindowUpdate(windowName, state);
280
+ }
281
+
282
+ void LibGodot::removeWindowNative(JNIEnv *env, jstring p_name) {
283
+ const uint64_t engineGeneration = GodotModule::get_singleton()->get_instance_generation();
284
+ const char *name = env->GetStringUTFChars(p_name, nullptr);
285
+ std::string windowName(name);
286
+ env->ReleaseStringUTFChars(p_name, name);
287
+ std::shared_ptr<WindowData> state;
288
+ {
289
+ std::lock_guard<std::mutex> lock(windowMapMutex);
290
+ auto it = windowMap.find(windowName);
291
+ if (it == windowMap.end()) {
292
+ return;
293
+ }
294
+ state = it->second;
295
+ state->active = false;
296
+ ++state->attachmentGeneration;
297
+ if (!windowName.empty()) {
298
+ windowMap.erase(it);
299
+ }
300
+ }
301
+ // The default child SurfaceControl is persistent even while its host is hidden.
302
+ if (windowName.empty()) {
303
+ return;
304
+ }
305
+ // SurfaceHolder requires rendering to stop before a named Surface is destroyed.
306
+ GodotModule::get_singleton()->runOnGodotThread([windowName, state, engineGeneration]() {
307
+ auto *instance = GodotModule::get_singleton()->get_instance();
308
+ if (engineGeneration != 0 && engineGeneration == GodotModule::get_singleton()->get_instance_generation() &&
309
+ instance && instance->is_started()) {
310
+ auto *tree = godot::Object::cast_to<godot::SceneTree>(godot::Engine::get_singleton()->get_main_loop());
311
+ if (tree) {
312
+ auto *window = godot::Object::cast_to<godot::Window>(tree->get_root()->find_child(
313
+ godot::String::utf8(windowName.c_str()), true, false));
314
+ if (window) {
315
+ window->set_native_surface(godot::Ref<godot::RenderingNativeSurface>());
316
+ }
317
+ }
318
+ }
319
+ std::lock_guard<std::mutex> lock(windowMapMutex);
320
+ state->attachedSurface.reset();
321
+ state->surface.reset();
322
+ },
323
+ true);
324
+ }
325
+
326
+ void LibGodot::updateWindow(std::string windowName) {
327
+ std::shared_ptr<WindowData> state;
328
+ {
329
+ std::lock_guard<std::mutex> lock(windowMapMutex);
330
+ auto it = windowMap.find(windowName);
331
+ if (it != windowMap.end()) {
332
+ state = it->second;
333
+ }
334
+ }
335
+ if (state) {
336
+ scheduleWindowUpdate(windowName, state);
337
+ }
338
+ }
339
+
340
+ void LibGodot::updateWindows() {
341
+ std::vector<std::pair<std::string, std::shared_ptr<WindowData>>> windows;
342
+ {
343
+ std::lock_guard<std::mutex> lock(windowMapMutex);
344
+ for (const auto &entry : windowMap) {
345
+ windows.push_back(entry);
346
+ }
347
+ }
348
+ for (const auto &[name, state] : windows) {
349
+ scheduleWindowUpdate(name, state);
350
+ }
351
+ }
352
+
353
+ void LibGodot::registerWindowUpdateCallbackNative(JNIEnv *env, jstring name, jlong handle, jobject r) {
354
+ GodotModule *mod = GodotModule::get_singleton();
355
+ if (!mod) {
356
+ LOGE("GodotModule not available");
357
+ return;
358
+ }
359
+
360
+ std::string std_name = jstring_to_std_string(name);
361
+
362
+ jobject myRunnable = env->NewGlobalRef(r);
363
+
364
+ std::function<void(bool)> callback = [myRunnable](bool adding) {
365
+ JNIEnv *env = get_jni_env();
366
+ jclass runnable_class = env->FindClass("java/lang/Runnable");
367
+ jmethodID runnable_run_method_id = env->GetMethodID(runnable_class, "run", "()V");
368
+ env->CallVoidMethod(myRunnable, runnable_run_method_id);
369
+ if (env->ExceptionCheck()) {
370
+ // Retrieve the last Java exception
371
+ env->ExceptionDescribe();
372
+
373
+ // Clear the exception
374
+ env->ExceptionClear();
375
+ }
376
+ };
377
+
378
+ mod->registerWindowUpdateCallback(std_name, (void *)handle, callback, myRunnable);
379
+ }
380
+
381
+ void LibGodot::unregisterWindowUpdateCallbackNative(jlong handle) {
382
+ GodotModule *mod = GodotModule::get_singleton();
383
+ if (!mod) {
384
+ LOGE("GodotModule not available");
385
+ return;
386
+ }
387
+ mod->unregisterWindowUpdateCallback((void *)handle);
388
+ }
389
+
390
+ static std::string convertToStdString(JNIEnv *env, jstring s) {
391
+ std::string result;
392
+ {
393
+ jboolean isCopy;
394
+ const char *val = env->GetStringUTFChars(s, &isCopy);
395
+ result = val;
396
+ env->ReleaseStringUTFChars(s, val);
397
+ }
398
+ return result;
399
+ }
400
+
401
+ struct TouchPos {
402
+ int id = 0;
403
+ godot::Point2 pos;
404
+ float pressure = 0;
405
+ godot::Vector2 tilt;
406
+ };
407
+
408
+ static std::map<int32_t, std::vector<TouchPos>> touches;
409
+
410
+ void LibGodot::clear_input_state() {
411
+ touches.clear();
412
+ std::lock_guard<std::mutex> lock(windowMapMutex);
413
+ for (const auto &[name, state] : windowMap) {
414
+ state->id = name.empty() ? 0 : -1;
415
+ }
416
+ }
417
+
418
+ extern "C" {
419
+
420
+ JNIEXPORT void JNICALL Java_com_preeternal_reactnativegodot_RTNLibGodot_initialize(JNIEnv *env, jclass clazz, jobject p_asset_manager, jobject p_net_utils, jobject p_dir_access_handler, jobject p_file_access_handler, jobject p_godot_io, jobject p_main_surface, jint p_width, jint p_height) {
421
+ LibGodot::initialize(env, p_asset_manager, p_net_utils, p_dir_access_handler, p_file_access_handler, p_godot_io, p_main_surface, p_width, p_height);
422
+ }
423
+
424
+ JNIEXPORT void JNICALL Java_com_preeternal_reactnativegodot_RTNLibGodot_updateWindowNative(JNIEnv *env, jclass clazz, jstring p_name, jobject p_surface, jint p_width, jint p_height) {
425
+ LibGodot::updateWindowNative(env, p_name, p_surface, p_width, p_height);
426
+ }
427
+
428
+ JNIEXPORT void JNICALL Java_com_preeternal_reactnativegodot_RTNLibGodot_removeWindowNative(JNIEnv *env, jclass clazz, jstring p_name) {
429
+ LibGodot::removeWindowNative(env, p_name);
430
+ }
431
+
432
+ JNIEXPORT void JNICALL Java_com_preeternal_reactnativegodot_RTNLibGodot_cleanup(JNIEnv *env, jclass clazz) {
433
+ LibGodot::cleanup(env);
434
+ }
435
+
436
+ // Called on the UI thread
437
+ JNIEXPORT void JNICALL Java_com_preeternal_reactnativegodot_RTNLibGodot_dispatchMouseEvent(JNIEnv *env, jclass clazz, jstring p_name, jint p_event_type, jint p_button_mask, jfloat p_x, jfloat p_y, jfloat p_delta_x, jfloat p_delta_y, jboolean p_double_click, jboolean p_source_mouse_relative, jfloat p_pressure, jfloat p_tilt_x, jfloat p_tilt_y) {
438
+ // godot::GodotInstance *instance = GodotModule::get_singleton()->get_instance();
439
+ // if (!instance || !instance->is_started()) {
440
+ // return;
441
+ // }
442
+
443
+ // input_handler->process_mouse_event(p_event_type, p_button_mask, godot::Point2(p_x, p_y), godot::Vector2(p_delta_x, p_delta_y), p_double_click, p_source_mouse_relative, p_pressure, godot::Vector2(p_tilt_x, p_tilt_y));
444
+ }
445
+
446
+ JNIEXPORT jobjectArray JNICALL Java_com_preeternal_reactnativegodot_RTNLibGodot_getRendererInfo(JNIEnv *env, jclass clazz) {
447
+ std::string driver, method;
448
+ GodotModule::get_singleton()->runOnGodotThread([&]() {
449
+ auto *instance = GodotModule::get_singleton()->get_instance();
450
+ if (!instance || !instance->is_started()) {
451
+ return;
452
+ }
453
+ driver = godot::RenderingServer::get_singleton()->get_current_rendering_driver_name().utf8().get_data();
454
+ method = godot::RenderingServer::get_singleton()->get_current_rendering_method().utf8().get_data();
455
+ },
456
+ true);
457
+ jobjectArray result = env->NewObjectArray(2, env->FindClass("java/lang/String"), nullptr);
458
+ env->SetObjectArrayElement(result, 0, env->NewStringUTF(driver.c_str()));
459
+ env->SetObjectArrayElement(result, 1, env->NewStringUTF(method.c_str()));
460
+ return result;
461
+ }
462
+
463
+ JNIEXPORT jstring JNICALL Java_com_preeternal_reactnativegodot_RTNLibGodot_getGlobal(JNIEnv *env, jclass clazz, jstring path) {
464
+ std::string key = convertToStdString(env, path), value;
465
+ GodotModule::get_singleton()->runOnGodotThread([&]() {
466
+ auto *instance = GodotModule::get_singleton()->get_instance();
467
+ if (!instance || !instance->is_started()) {
468
+ return;
469
+ }
470
+ godot::Variant setting = godot::ProjectSettings::get_singleton()->get_setting_with_override(godot::String::utf8(key.c_str()));
471
+ if (setting.get_type() != godot::Variant::NIL) {
472
+ godot::String text = setting;
473
+ value = text.utf8().get_data();
474
+ }
475
+ },
476
+ true);
477
+ return env->NewStringUTF(value.c_str());
478
+ }
479
+
480
+ JNIEXPORT void JNICALL
481
+ Java_com_preeternal_reactnativegodot_RTNLibGodot_registerWindowUpdateCallbackNative(JNIEnv *env, jclass clazz, jstring name, jlong handle, jobject r) {
482
+ LibGodot::registerWindowUpdateCallbackNative(env, name, handle, r);
483
+ }
484
+
485
+ JNIEXPORT void JNICALL
486
+ Java_com_preeternal_reactnativegodot_RTNLibGodot_unregisterWindowUpdateCallbackNative(JNIEnv *env, jclass clazz, jlong handle) {
487
+ LibGodot::unregisterWindowUpdateCallbackNative(handle);
488
+ }
489
+
490
+ // Called on the UI thread
491
+ JNIEXPORT void JNICALL Java_com_preeternal_reactnativegodot_RTNLibGodot_dispatchTouchEvent(JNIEnv *env, jclass clazz, jstring p_name, jint p_event, jint p_pointer, jint pointer_count, jfloatArray position, jboolean p_double_tap) {
492
+ if (!position || pointer_count < 1 || pointer_count > env->GetArrayLength(position) / 6) {
493
+ return;
494
+ }
495
+ std::vector<float> positions(pointer_count * 6);
496
+ env->GetFloatArrayRegion(position, 0, positions.size(), positions.data());
497
+ if (env->ExceptionCheck()) {
498
+ return;
499
+ }
500
+ std::string windowName = convertToStdString(env, p_name);
501
+ auto *module = GodotModule::get_singleton();
502
+ const uint64_t engineGeneration = module->get_instance_generation();
503
+ std::shared_ptr<WindowData> state;
504
+ uint64_t attachmentGeneration;
505
+ {
506
+ std::lock_guard<std::mutex> lock(windowMapMutex);
507
+ auto it = windowMap.find(windowName);
508
+ if (it == windowMap.end() || !it->second->active) {
509
+ return;
510
+ }
511
+ state = it->second;
512
+ attachmentGeneration = state->attachmentGeneration;
513
+ }
514
+ module->runWithInstance(engineGeneration, [windowName, state, attachmentGeneration, positions = std::move(positions), p_event, p_pointer, pointer_count, p_double_tap]() {
515
+ int32_t windowId;
516
+ {
517
+ std::lock_guard<std::mutex> lock(windowMapMutex);
518
+ if (!state->active || state->attachmentGeneration != attachmentGeneration) {
519
+ return;
520
+ }
521
+ windowId = state->id;
522
+ }
523
+ if (windowId < 0) {
524
+ LOGE("Could not find window for name: %s", windowName.c_str());
525
+ return;
526
+ }
527
+ std::vector<TouchPos> points;
528
+ for (int i = 0; i < pointer_count; i++) {
529
+ const float *p = positions.data() + i * 6;
530
+ TouchPos tp;
531
+ tp.id = (int)p[0];
532
+ tp.pos = godot::Point2(p[1], p[2]);
533
+ tp.pressure = p[3];
534
+ tp.tilt = godot::Vector2(p[4], p[5]);
535
+ points.push_back(tp);
536
+ }
537
+
538
+ switch (p_event) {
539
+ case AMOTION_EVENT_ACTION_DOWN: { //gesture begin
540
+
541
+ touches[windowId] = points;
542
+ std::vector<TouchPos> &touch = touches[windowId];
543
+
544
+ //send touch
545
+ for (int i = 0; i < touch.size(); i++) {
546
+ godot::Ref<godot::InputEventScreenTouch> ev;
547
+ ev.instantiate();
548
+ ev->set_window_id(windowId);
549
+ ev->set_index(touch[i].id);
550
+ ev->set_pressed(true);
551
+ ev->set_canceled(false);
552
+ ev->set_position(touch[i].pos);
553
+ ev->set_double_tap(p_double_tap);
554
+ godot::Input::get_singleton()->parse_input_event(ev);
555
+ }
556
+ } break;
557
+ case AMOTION_EVENT_ACTION_MOVE: { //motion
558
+ std::vector<TouchPos> &touch = touches[windowId];
559
+ if (touch.size() != points.size()) {
560
+ return;
561
+ }
562
+
563
+ for (int i = 0; i < touch.size(); i++) {
564
+ int idx = -1;
565
+ for (int j = 0; j < points.size(); j++) {
566
+ if (touch[i].id == points[j].id) {
567
+ idx = j;
568
+ break;
569
+ }
570
+ }
571
+
572
+ ERR_CONTINUE(idx == -1);
573
+
574
+ if (touch[i].pos == points[idx].pos) {
575
+ continue; // Don't move unnecessarily.
576
+ }
577
+
578
+ godot::Ref<godot::InputEventScreenDrag> ev;
579
+ ev.instantiate();
580
+ ev->set_window_id(windowId);
581
+ ev->set_index(touch[i].id);
582
+ ev->set_position(points[idx].pos);
583
+ ev->set_relative(points[idx].pos - touch[i].pos);
584
+ //ev->set_relative_screen_position(ev->get_relative());
585
+ ev->set_pressure(points[idx].pressure);
586
+ ev->set_tilt(points[idx].tilt);
587
+ godot::Input::get_singleton()->parse_input_event(ev);
588
+ touch[i].pos = points[idx].pos;
589
+ }
590
+
591
+ } break;
592
+ case AMOTION_EVENT_ACTION_CANCEL: {
593
+ std::vector<TouchPos> &touch = touches[windowId];
594
+ for (int i = 0; i < touch.size(); i++) {
595
+ godot::Ref<godot::InputEventScreenTouch> ev;
596
+ ev.instantiate();
597
+ ev->set_window_id(windowId);
598
+ ev->set_index(touch[i].id);
599
+ ev->set_pressed(false);
600
+ ev->set_canceled(true);
601
+ ev->set_position(touch[i].pos);
602
+ ev->set_double_tap(p_double_tap);
603
+ godot::Input::get_singleton()->parse_input_event(ev);
604
+ }
605
+ } break;
606
+ case AMOTION_EVENT_ACTION_UP: { //release
607
+ std::vector<TouchPos> &touch = touches[windowId];
608
+ for (int i = 0; i < touch.size(); i++) {
609
+ godot::Ref<godot::InputEventScreenTouch> ev;
610
+ ev.instantiate();
611
+ ev->set_window_id(windowId);
612
+ ev->set_index(touch[i].id);
613
+ ev->set_pressed(false);
614
+ ev->set_canceled(false);
615
+ ev->set_position(touch[i].pos);
616
+ ev->set_double_tap(p_double_tap);
617
+ godot::Input::get_singleton()->parse_input_event(ev);
618
+ }
619
+ } break;
620
+ case AMOTION_EVENT_ACTION_POINTER_DOWN: { // add touch
621
+ std::vector<TouchPos> &touch = touches[windowId];
622
+ for (int i = 0; i < points.size(); i++) {
623
+ if (points[i].id == p_pointer) {
624
+ TouchPos tp = points[i];
625
+ touch.push_back(tp);
626
+
627
+ godot::Ref<godot::InputEventScreenTouch> ev;
628
+ ev.instantiate();
629
+ ev->set_window_id(windowId);
630
+ ev->set_index(tp.id);
631
+ ev->set_pressed(true);
632
+ ev->set_position(tp.pos);
633
+ godot::Input::get_singleton()->parse_input_event(ev);
634
+
635
+ break;
636
+ }
637
+ }
638
+ } break;
639
+ case AMOTION_EVENT_ACTION_POINTER_UP: { // remove touch
640
+ std::vector<TouchPos> &touch = touches[windowId];
641
+ for (int i = 0; i < touch.size(); i++) {
642
+ if (touch[i].id == p_pointer) {
643
+ godot::Ref<godot::InputEventScreenTouch> ev;
644
+ ev.instantiate();
645
+ ev->set_window_id(windowId);
646
+ ev->set_index(touch[i].id);
647
+ ev->set_pressed(false);
648
+ ev->set_position(touch[i].pos);
649
+ godot::Input::get_singleton()->parse_input_event(ev);
650
+ touch.erase(touch.begin() + i);
651
+ break;
652
+ }
653
+ }
654
+ } break;
655
+ }
656
+ });
657
+ }
658
+ }