@preeternal/react-native-godot 0.1.0 → 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.
@@ -46,24 +46,39 @@
46
46
  #include <godot_cpp/classes/project_settings.hpp>
47
47
  #include <godot_cpp/classes/rendering_server.hpp>
48
48
  #include <map>
49
+ #include <memory>
49
50
  #include <mutex>
50
51
  #include <string>
52
+ #include <vector>
51
53
 
52
- typedef struct WindowData {
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 {
53
65
  int width;
54
66
  int height;
55
67
  int32_t id;
56
- ANativeWindow *surface;
57
- WindowData() :
58
- width(0), height(0), id(-1), surface(nullptr) {}
59
- WindowData(ANativeWindow *p_surface, int p_width, int p_height, int32_t p_id = -1) :
60
- width(p_width), height(p_height), id(p_id), surface(p_surface) {}
61
- } WindowData;
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
+ };
62
77
 
63
- static std::map<std::string, WindowData> windowMap;
64
- static std::recursive_mutex windowMapMutex;
78
+ static std::map<std::string, std::shared_ptr<WindowData>> windowMap;
79
+ static std::mutex windowMapMutex;
65
80
 
66
- 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, jobject p_godot_engine, jobject p_host_activity, jobject p_class_loader) {
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) {
67
82
  LOGI("LibGodot::initialize");
68
83
  env->GetJavaVM(&java_vm);
69
84
  asset_manager = env->NewGlobalRef(p_asset_manager);
@@ -72,25 +87,30 @@ void LibGodot::initialize(JNIEnv *env, jobject p_asset_manager, jobject p_net_ut
72
87
  file_access_handler = env->NewGlobalRef(p_file_access_handler);
73
88
  godot_io = env->NewGlobalRef(p_godot_io);
74
89
  ANativeWindow *mainSurface = ANativeWindow_fromSurface(env, p_main_surface);
75
- windowMap[""] = WindowData(mainSurface, p_width, p_height, 0);
76
- godot_engine = env->NewGlobalRef(p_godot_engine);
77
- host_activity = env->NewGlobalRef(p_host_activity);
78
- class_loader = env->NewGlobalRef(p_class_loader);
90
+ std::lock_guard<std::mutex> lock(windowMapMutex);
91
+ windowMap[""] = std::make_shared<WindowData>(adoptWindow(mainSurface), p_width, p_height, 0);
79
92
  }
80
93
 
81
94
  ANativeWindow *LibGodot::get_main_surface() {
82
- std::lock_guard<std::recursive_mutex> lock(windowMapMutex);
83
- return windowMap[""].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();
84
102
  }
85
103
 
86
104
  int LibGodot::get_main_width() {
87
- std::lock_guard<std::recursive_mutex> lock(windowMapMutex);
88
- return windowMap[""].width;
105
+ std::lock_guard<std::mutex> lock(windowMapMutex);
106
+ auto it = windowMap.find("");
107
+ return it == windowMap.end() ? 0 : it->second->width;
89
108
  }
90
109
 
91
110
  int LibGodot::get_main_height() {
92
- std::lock_guard<std::recursive_mutex> lock(windowMapMutex);
93
- return windowMap[""].height;
111
+ std::lock_guard<std::mutex> lock(windowMapMutex);
112
+ auto it = windowMap.find("");
113
+ return it == windowMap.end() ? 0 : it->second->height;
94
114
  }
95
115
 
96
116
  void LibGodot::cleanup(JNIEnv *env) {
@@ -114,24 +134,11 @@ void LibGodot::cleanup(JNIEnv *env) {
114
134
  env->DeleteGlobalRef(godot_io);
115
135
  godot_io = nullptr;
116
136
  }
117
- if (godot_engine) {
118
- env->DeleteGlobalRef(godot_engine);
119
- godot_engine = nullptr;
120
- }
121
- if (host_activity) {
122
- env->DeleteGlobalRef(host_activity);
123
- host_activity = nullptr;
124
- }
125
- if (class_loader) {
126
- env->DeleteGlobalRef(class_loader);
127
- class_loader = nullptr;
128
- }
129
-
130
137
  {
131
- std::lock_guard<std::recursive_mutex> lock(windowMapMutex);
132
- for (auto item : windowMap) {
133
- WindowData &data = item.second;
134
- ANativeWindow_release(data.surface);
138
+ std::lock_guard<std::mutex> lock(windowMapMutex);
139
+ for (const auto &[name, state] : windowMap) {
140
+ state->active = false;
141
+ ++state->attachmentGeneration;
135
142
  }
136
143
  windowMap.clear();
137
144
  }
@@ -153,58 +160,90 @@ void LibGodot::set_java_vm(JavaVM *vm) {
153
160
  java_vm = vm;
154
161
  }
155
162
 
156
- static std::function<void()> createUpdateWindowFunc(std::string p_window_name, int p_width, int p_height, ANativeWindow *p_window_surface) {
157
- return [p_window_name, p_width, p_height, p_window_surface]() {
158
- godot::DisplayServerEmbedded *dse = godot::DisplayServerEmbedded::get_singleton();
159
- int32_t windowId = -1;
160
- if (p_window_name == "") {
161
- // Default id
162
- windowId = 0;
163
- } else {
164
- // Find window
165
- godot::MainLoop *mainLoop = godot::Engine::get_singleton()->get_main_loop();
166
- godot::SceneTree *sceneTree = godot::Object::cast_to<godot::SceneTree>(mainLoop);
167
- if (!sceneTree) {
168
- LOGE("Unable to get SceneTree from Godot!");
169
- return;
170
- }
171
- godot::Node *node = sceneTree->get_root()->find_child(
172
- godot::String::utf8(p_window_name.c_str()), true, false);
173
- godot::Window *newWindow = godot::Object::cast_to<godot::Window>(node);
174
-
175
- if (newWindow) {
176
- bool change_surface = true;
177
- godot::Ref<godot::RenderingNativeSurfaceAndroid> ns = newWindow->get_native_surface();
178
- if (ns.is_valid()) {
179
- ANativeWindow *current_window = (ANativeWindow *)ns->get_window();
180
- if (current_window == p_window_surface) {
181
- change_surface = false;
182
- }
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;
183
209
  }
210
+ }
184
211
 
185
- if (change_surface) {
186
- LOGI("Changing surface");
187
- godot::Ref<godot::RenderingNativeSurfaceAndroid> androidSurface = godot::RenderingNativeSurfaceAndroid::create(
188
- (uint64_t)p_window_surface, p_width, p_height);
189
-
190
- newWindow->set_visible(true);
191
- newWindow->set_native_surface(androidSurface);
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;
192
218
  }
193
219
 
194
- windowId = newWindow->get_window_id();
220
+ newWindow->set_visible(true);
221
+ newWindow->set_native_surface(androidSurface);
195
222
  }
223
+
224
+ windowId = newWindow->get_window_id();
196
225
  }
197
- if (windowId >= 0) {
198
- LOGI("Resizing Window: %d %d %d", windowId, p_width, p_height);
199
- dse->resize_window(godot::Vector2i(p_width, p_height), windowId);
200
- {
201
- std::lock_guard<std::recursive_mutex> lock(windowMapMutex);
202
- if (windowId > 0 && windowMap.contains(p_window_name)) {
203
- windowMap[p_window_name].id = windowId;
204
- }
205
- }
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;
206
241
  }
207
- };
242
+ state->updatePending = true;
243
+ }
244
+ GodotModule::get_singleton()->runOnGodotThread([name, state]() {
245
+ applyWindowUpdate(name, state);
246
+ });
208
247
  }
209
248
 
210
249
  void LibGodot::updateWindowNative(JNIEnv *env, jstring p_name, jobject p_surface, jint p_width, jint p_height) {
@@ -216,105 +255,99 @@ void LibGodot::updateWindowNative(JNIEnv *env, jstring p_name, jobject p_surface
216
255
  env->ReleaseStringUTFChars(p_name, val);
217
256
  }
218
257
 
219
- ANativeWindow *windowSurface = ANativeWindow_fromSurface(env, p_surface);
220
- bool changeSurface = false;
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;
221
263
  {
222
- std::lock_guard<std::recursive_mutex> lock(windowMapMutex);
223
-
224
- if (!windowMap.contains(windowName)) {
225
- windowMap[windowName] = WindowData(windowSurface, p_width, p_height);
226
- changeSurface = true;
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);
227
268
  }
228
-
229
- WindowData &winData = windowMap[windowName];
230
- if (winData.surface != windowSurface) {
231
- changeSurface = true;
232
- winData.surface = windowSurface;
233
- }
234
- if (windowName == "" && changeSurface) {
235
- LOGW("Default window surface should never change!");
269
+ state = entry;
270
+ if (!state->active || state->surface.get() != surface.get()) {
271
+ ++state->attachmentGeneration;
236
272
  }
237
- winData.width = p_width;
238
- winData.height = p_height;
239
- }
240
- godot::GodotInstance *instance = GodotModule::get_singleton()->get_instance();
241
- if (instance && instance->is_started()) {
242
- GodotModule::get_singleton()->runOnGodotThread(createUpdateWindowFunc(windowName, p_width, p_height, windowSurface), true);
273
+ state->surface = std::move(surface);
274
+ state->active = true;
275
+ state->width = p_width;
276
+ state->height = p_height;
243
277
  }
278
+ // Only one pending job per window; it reads the latest requested size.
279
+ scheduleWindowUpdate(windowName, state);
244
280
  }
245
281
 
246
282
  void LibGodot::removeWindowNative(JNIEnv *env, jstring p_name) {
247
- std::string windowName;
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;
248
288
  {
249
- jboolean isCopy;
250
- const char *val = env->GetStringUTFChars(p_name, &isCopy);
251
- windowName = val;
252
- env->ReleaseStringUTFChars(p_name, val);
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
+ }
253
300
  }
254
- if (windowName == "") {
255
- // Default window cannot be removed
301
+ // The default child SurfaceControl is persistent even while its host is hidden.
302
+ if (windowName.empty()) {
256
303
  return;
257
304
  }
258
- {
259
- std::lock_guard<std::recursive_mutex> lock(windowMapMutex);
260
-
261
- if (!windowMap.contains(windowName)) {
262
- // No window
263
- return;
264
- }
265
-
266
- ANativeWindow *windowSurface = windowMap[windowName].surface;
267
-
268
- godot::GodotInstance *instance = GodotModule::get_singleton()->get_instance();
269
- if (instance && instance->is_started()) {
270
- GodotModule::get_singleton()->runOnGodotThread([windowName, windowSurface]() {
271
- {
272
- // Find window
273
- godot::MainLoop *mainLoop = godot::Engine::get_singleton()->get_main_loop();
274
- godot::SceneTree *sceneTree = godot::Object::cast_to<godot::SceneTree>(
275
- mainLoop);
276
- if (!sceneTree) {
277
- LOGE("Unable to get SceneTree from Godot!");
278
- return;
279
- }
280
- godot::Node *node = sceneTree->get_root()->find_child(
281
- godot::String::utf8(windowName.c_str()), true, false);
282
- godot::Window *window = godot::Object::cast_to<godot::Window>(node);
283
-
284
- if (window) {
285
- godot::Ref<godot::RenderingNativeSurface> nativeSurface;
286
- window->set_native_surface(nativeSurface);
287
- }
288
- ANativeWindow_release(windowSurface);
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>());
289
316
  }
290
- });
317
+ }
291
318
  }
292
- windowMap.erase(windowName);
293
- }
319
+ std::lock_guard<std::mutex> lock(windowMapMutex);
320
+ state->attachedSurface.reset();
321
+ state->surface.reset();
322
+ },
323
+ true);
294
324
  }
295
325
 
296
326
  void LibGodot::updateWindow(std::string windowName) {
297
- std::lock_guard<std::recursive_mutex> lock(windowMapMutex);
298
- if (windowMap.contains(windowName)) {
299
- godot::GodotInstance *instance = GodotModule::get_singleton()->get_instance();
300
- WindowData &data = windowMap[windowName];
301
- if (instance && instance->is_started()) {
302
- GodotModule::get_singleton()->runOnGodotThread(createUpdateWindowFunc(windowName, data.width, data.height, data.surface));
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;
303
333
  }
304
334
  }
335
+ if (state) {
336
+ scheduleWindowUpdate(windowName, state);
337
+ }
305
338
  }
306
339
 
307
340
  void LibGodot::updateWindows() {
308
- std::lock_guard<std::recursive_mutex> lock(windowMapMutex);
309
- godot::GodotInstance *instance = GodotModule::get_singleton()->get_instance();
310
- if (instance && instance->is_started()) {
311
- for (auto item : windowMap) {
312
- std::string windowName = item.first;
313
- WindowData &data = item.second;
314
- GodotModule::get_singleton()->runOnGodotThread(
315
- createUpdateWindowFunc(windowName, data.width, data.height, data.surface));
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);
316
346
  }
317
347
  }
348
+ for (const auto &[name, state] : windows) {
349
+ scheduleWindowUpdate(name, state);
350
+ }
318
351
  }
319
352
 
320
353
  void LibGodot::registerWindowUpdateCallbackNative(JNIEnv *env, jstring name, jlong handle, jobject r) {
@@ -372,20 +405,20 @@ struct TouchPos {
372
405
  godot::Vector2 tilt;
373
406
  };
374
407
 
375
- static int32_t getWindowId(std::string p_name) {
376
- std::lock_guard<std::recursive_mutex> lock(windowMapMutex);
377
- if (windowMap.contains(p_name)) {
378
- return windowMap[p_name].id;
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;
379
415
  }
380
- return -1;
381
416
  }
382
417
 
383
- static std::map<int32_t, godot::Vector<TouchPos>> touches;
384
-
385
418
  extern "C" {
386
419
 
387
- 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, jobject p_godot_engine, jobject p_host_activity, jobject p_class_loader) {
388
- 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, p_godot_engine, p_host_activity, p_class_loader);
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);
389
422
  }
390
423
 
391
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) {
@@ -411,22 +444,37 @@ JNIEXPORT void JNICALL Java_com_preeternal_reactnativegodot_RTNLibGodot_dispatch
411
444
  }
412
445
 
413
446
  JNIEXPORT jobjectArray JNICALL Java_com_preeternal_reactnativegodot_RTNLibGodot_getRendererInfo(JNIEnv *env, jclass clazz) {
414
- godot::String rendering_driver = godot::RenderingServer::get_singleton()->get_current_rendering_driver_name();
415
- godot::String rendering_method = godot::RenderingServer::get_singleton()->get_current_rendering_method();
416
-
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);
417
457
  jobjectArray result = env->NewObjectArray(2, env->FindClass("java/lang/String"), nullptr);
418
- env->SetObjectArrayElement(result, 0, env->NewStringUTF(rendering_driver.utf8().get_data()));
419
- env->SetObjectArrayElement(result, 1, env->NewStringUTF(rendering_method.utf8().get_data()));
420
-
458
+ env->SetObjectArrayElement(result, 0, env->NewStringUTF(driver.c_str()));
459
+ env->SetObjectArrayElement(result, 1, env->NewStringUTF(method.c_str()));
421
460
  return result;
422
461
  }
423
462
 
424
463
  JNIEXPORT jstring JNICALL Java_com_preeternal_reactnativegodot_RTNLibGodot_getGlobal(JNIEnv *env, jclass clazz, jstring path) {
425
- godot::String js = jstring_to_string(path, env);
426
-
427
- godot::Variant setting_with_override = godot::ProjectSettings::get_singleton()->get_setting_with_override(js);
428
- godot::String setting_value = (setting_with_override.get_type() == godot::Variant::NIL) ? "" : setting_with_override;
429
- return env->NewStringUTF(setting_value.utf8().get_data());
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());
430
478
  }
431
479
 
432
480
  JNIEXPORT void JNICALL
@@ -441,145 +489,170 @@ Java_com_preeternal_reactnativegodot_RTNLibGodot_unregisterWindowUpdateCallbackN
441
489
 
442
490
  // Called on the UI thread
443
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) {
444
- godot::GodotInstance *instance = GodotModule::get_singleton()->get_instance();
445
- if (!instance || !instance->is_started()) {
492
+ if (!position || pointer_count < 1 || pointer_count > env->GetArrayLength(position) / 6) {
446
493
  return;
447
494
  }
448
- std::string windowName = convertToStdString(env, p_name);
449
- int32_t windowId = getWindowId(windowName);
450
- if (windowId < 0) {
451
- LOGE("Could not find window for name: %s", windowName.c_str());
495
+ std::vector<float> positions(pointer_count * 6);
496
+ env->GetFloatArrayRegion(position, 0, positions.size(), positions.data());
497
+ if (env->ExceptionCheck()) {
452
498
  return;
453
499
  }
454
- godot::Vector<TouchPos> points;
455
- for (int i = 0; i < pointer_count; i++) {
456
- jfloat p[6];
457
- env->GetFloatArrayRegion(position, i * 6, 6, p);
458
- TouchPos tp;
459
- tp.id = (int)p[0];
460
- tp.pos = godot::Point2(p[1], p[2]);
461
- tp.pressure = p[3];
462
- tp.tilt = godot::Vector2(p[4], p[5]);
463
- points.push_back(tp);
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;
464
513
  }
465
-
466
- switch (p_event) {
467
- case AMOTION_EVENT_ACTION_DOWN: { //gesture begin
468
-
469
- touches[windowId] = points;
470
- godot::Vector<TouchPos> &touch = touches[windowId];
471
-
472
- //send touch
473
- for (int i = 0; i < touch.size(); i++) {
474
- godot::Ref<godot::InputEventScreenTouch> ev;
475
- ev.instantiate();
476
- ev->set_window_id(windowId);
477
- ev->set_index(touch[i].id);
478
- ev->set_pressed(true);
479
- ev->set_canceled(false);
480
- ev->set_position(touch[i].pos);
481
- ev->set_double_tap(p_double_tap);
482
- godot::Input::get_singleton()->parse_input_event(ev);
483
- }
484
- } break;
485
- case AMOTION_EVENT_ACTION_MOVE: { //motion
486
- godot::Vector<TouchPos> &touch = touches[windowId];
487
- if (touch.size() != points.size()) {
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) {
488
519
  return;
489
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
+ }
490
537
 
491
- for (int i = 0; i < touch.size(); i++) {
492
- int idx = -1;
493
- for (int j = 0; j < points.size(); j++) {
494
- if (touch[i].id == points[j].id) {
495
- idx = j;
496
- break;
497
- }
498
- }
538
+ switch (p_event) {
539
+ case AMOTION_EVENT_ACTION_DOWN: { //gesture begin
499
540
 
500
- ERR_CONTINUE(idx == -1);
541
+ touches[windowId] = points;
542
+ std::vector<TouchPos> &touch = touches[windowId];
501
543
 
502
- if (touch[i].pos == points[idx].pos) {
503
- continue; // Don't move unnecessarily.
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;
504
561
  }
505
562
 
506
- godot::Ref<godot::InputEventScreenDrag> ev;
507
- ev.instantiate();
508
- ev->set_window_id(windowId);
509
- ev->set_index(touch[i].id);
510
- ev->set_position(points[idx].pos);
511
- ev->set_relative(points[idx].pos - touch[i].pos);
512
- //ev->set_relative_screen_position(ev->get_relative());
513
- ev->set_pressure(points[idx].pressure);
514
- ev->set_tilt(points[idx].tilt);
515
- godot::Input::get_singleton()->parse_input_event(ev);
516
- touch.write[i].pos = points[idx].pos;
517
- }
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
+ }
518
571
 
519
- } break;
520
- case AMOTION_EVENT_ACTION_CANCEL: {
521
- godot::Vector<TouchPos> &touch = touches[windowId];
522
- for (int i = 0; i < touch.size(); i++) {
523
- godot::Ref<godot::InputEventScreenTouch> ev;
524
- ev.instantiate();
525
- ev->set_window_id(windowId);
526
- ev->set_index(touch[i].id);
527
- ev->set_pressed(false);
528
- ev->set_canceled(true);
529
- ev->set_position(touch[i].pos);
530
- ev->set_double_tap(p_double_tap);
531
- godot::Input::get_singleton()->parse_input_event(ev);
532
- }
533
- } break;
534
- case AMOTION_EVENT_ACTION_UP: { //release
535
- godot::Vector<TouchPos> &touch = touches[windowId];
536
- for (int i = 0; i < touch.size(); i++) {
537
- godot::Ref<godot::InputEventScreenTouch> ev;
538
- ev.instantiate();
539
- ev->set_window_id(windowId);
540
- ev->set_index(touch[i].id);
541
- ev->set_pressed(false);
542
- ev->set_canceled(false);
543
- ev->set_position(touch[i].pos);
544
- ev->set_double_tap(p_double_tap);
545
- godot::Input::get_singleton()->parse_input_event(ev);
546
- }
547
- } break;
548
- case AMOTION_EVENT_ACTION_POINTER_DOWN: { // add touch
549
- godot::Vector<TouchPos> &touch = touches[windowId];
550
- for (int i = 0; i < points.size(); i++) {
551
- if (points[i].id == p_pointer) {
552
- TouchPos tp = points[i];
553
- touch.push_back(tp);
572
+ ERR_CONTINUE(idx == -1);
554
573
 
555
- godot::Ref<godot::InputEventScreenTouch> ev;
574
+ if (touch[i].pos == points[idx].pos) {
575
+ continue; // Don't move unnecessarily.
576
+ }
577
+
578
+ godot::Ref<godot::InputEventScreenDrag> ev;
556
579
  ev.instantiate();
557
580
  ev->set_window_id(windowId);
558
- ev->set_index(tp.id);
559
- ev->set_pressed(true);
560
- ev->set_position(tp.pos);
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);
561
587
  godot::Input::get_singleton()->parse_input_event(ev);
588
+ touch[i].pos = points[idx].pos;
589
+ }
562
590
 
563
- break;
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);
564
604
  }
565
- }
566
- } break;
567
- case AMOTION_EVENT_ACTION_POINTER_UP: { // remove touch
568
- godot::Vector<TouchPos> &touch = touches[windowId];
569
- for (int i = 0; i < touch.size(); i++) {
570
- if (touch[i].id == p_pointer) {
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++) {
571
609
  godot::Ref<godot::InputEventScreenTouch> ev;
572
610
  ev.instantiate();
573
611
  ev->set_window_id(windowId);
574
612
  ev->set_index(touch[i].id);
575
613
  ev->set_pressed(false);
614
+ ev->set_canceled(false);
576
615
  ev->set_position(touch[i].pos);
616
+ ev->set_double_tap(p_double_tap);
577
617
  godot::Input::get_singleton()->parse_input_event(ev);
578
- touch.remove_at(i);
579
- break;
580
618
  }
581
- }
582
- } break;
583
- }
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
+ });
584
657
  }
585
658
  }