Skip to content

Commit 85ec5b9

Browse files
committed
Add platform lifecycle callbacks to CameraServer base class
1 parent f5918a9 commit 85ec5b9

File tree

8 files changed

+44
-30
lines changed

8 files changed

+44
-30
lines changed

drivers/apple_embedded/godot_view_controller.mm

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#import "os_apple_embedded.h"
3939

4040
#include "core/config/project_settings.h"
41+
#include "servers/camera/camera_server.h"
4142

4243
#import <AVFoundation/AVFoundation.h>
4344
#import <GameController/GameController.h>
@@ -238,6 +239,24 @@ - (void)dealloc {
238239

239240
// MARK: Orientation
240241

242+
#if TARGET_OS_IPHONE
243+
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
244+
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
245+
246+
[coordinator animateAlongsideTransition:nil
247+
completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
248+
// Get the new interface orientation after rotation completes (iOS only)
249+
UIInterfaceOrientation orientation = self.view.window.windowScene.interfaceOrientation;
250+
251+
// Notify camera server of orientation change
252+
CameraServer *camera_server = CameraServer::get_singleton();
253+
if (camera_server) {
254+
camera_server->handle_display_rotation_change((int)orientation);
255+
}
256+
}];
257+
}
258+
#endif // TARGET_OS_IPHONE
259+
241260
- (UIRectEdge)preferredScreenEdgesDeferringSystemGestures {
242261
if (GLOBAL_GET("display/window/ios/suppress_ui_gesture")) {
243262
return UIRectEdgeAll;

modules/camera/camera_android.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ void CameraAndroid::set_monitoring_feeds(bool p_monitoring_feeds) {
622622
}
623623
}
624624

625-
void CameraAndroid::handle_pause() {
625+
void CameraAndroid::handle_application_pause() {
626626
for (int i = 0; i < feeds.size(); i++) {
627627
Ref<CameraFeedAndroid> feed = feeds[i];
628628
if (feed.is_valid()) {
@@ -631,7 +631,7 @@ void CameraAndroid::handle_pause() {
631631
}
632632
}
633633

634-
void CameraAndroid::handle_resume() {
634+
void CameraAndroid::handle_application_resume() {
635635
for (int i = 0; i < feeds.size(); i++) {
636636
Ref<CameraFeedAndroid> feed = feeds[i];
637637
if (feed.is_valid()) {
@@ -640,7 +640,7 @@ void CameraAndroid::handle_resume() {
640640
}
641641
}
642642

643-
void CameraAndroid::handle_rotation_change() {
643+
void CameraAndroid::handle_display_rotation_change(int) {
644644
for (int i = 0; i < feeds.size(); i++) {
645645
Ref<CameraFeedAndroid> feed = feeds[i];
646646
if (feed.is_valid()) {

modules/camera/camera_android.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ class CameraAndroid : public CameraServer {
113113

114114
public:
115115
void set_monitoring_feeds(bool p_monitoring_feeds) override;
116-
void handle_pause();
117-
void handle_resume();
118-
void handle_rotation_change();
116+
void handle_application_pause() override;
117+
void handle_application_resume() override;
118+
void handle_display_rotation_change(int p_orientation) override;
119119

120120
~CameraAndroid();
121121
};

platform/android/java/lib/src/main/java/org/godotengine/godot/Godot.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ class Godot private constructor(val context: Context) {
766766

767767
if (currentConfig.orientation != newConfig.orientation) {
768768
runOnRenderThread {
769-
GodotLib.onScreenRotationChange()
769+
GodotLib.onScreenRotationChange(newConfig.orientation)
770770
}
771771
}
772772
currentConfig = newConfig

platform/android/java/lib/src/main/java/org/godotengine/godot/GodotLib.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,9 @@ public static void calldeferred(long p_id, String p_method, Object[] p_params) {
297297

298298
/**
299299
* Invoked when the screen orientation changes.
300+
* @param orientation the new screen orientation
300301
*/
301-
static native void onScreenRotationChange();
302+
static native void onScreenRotationChange(int orientation);
302303

303304
/**
304305
* @return true if input must be dispatched from the render thread. If false, input is

platform/android/java_godot_lib_jni.cpp

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,7 @@
5353
#include "main/main.h"
5454
#include "servers/rendering/rendering_server.h"
5555

56-
#include "modules/modules_enabled.gen.h" // For camera.
57-
58-
#ifdef MODULE_CAMERA_ENABLED
59-
#include "modules/camera/camera_android.h"
6056
#include "servers/camera/camera_server.h"
61-
#endif
6257

6358
#ifndef XR_DISABLED
6459
#include "servers/xr/xr_server.h"
@@ -606,12 +601,10 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_onRendererResumed(JNI
606601

607602
// We force redraw to ensure we render at least once when resuming the app.
608603
Main::force_redraw();
609-
#ifdef MODULE_CAMERA_ENABLED
610-
CameraAndroid *camera_android = Object::cast_to<CameraAndroid>(CameraServer::get_singleton());
611-
if (camera_android) {
612-
camera_android->handle_resume();
604+
CameraServer *camera_server = CameraServer::get_singleton();
605+
if (camera_server) {
606+
camera_server->handle_application_resume();
613607
}
614-
#endif // MODULE_CAMERA_ENABLED
615608
if (os_android->get_main_loop()) {
616609
os_android->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_RESUMED);
617610
}
@@ -622,29 +615,25 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_onRendererPaused(JNIE
622615
return;
623616
}
624617

625-
#ifdef MODULE_CAMERA_ENABLED
626-
CameraAndroid *camera_android = Object::cast_to<CameraAndroid>(CameraServer::get_singleton());
627-
if (camera_android) {
628-
camera_android->handle_pause();
618+
CameraServer *camera_server = CameraServer::get_singleton();
619+
if (camera_server) {
620+
camera_server->handle_application_pause();
629621
}
630-
#endif // MODULE_CAMERA_ENABLED
631622

632623
if (os_android->get_main_loop()) {
633624
os_android->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_PAUSED);
634625
}
635626
}
636627

637-
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_onScreenRotationChange(JNIEnv *env, jclass clazz) {
628+
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_onScreenRotationChange(JNIEnv *env, jclass clazz, jint p_orientation) {
638629
if (step.get() <= STEP_SETUP) {
639630
return;
640631
}
641632

642-
#ifdef MODULE_CAMERA_ENABLED
643-
CameraAndroid *camera_android = Object::cast_to<CameraAndroid>(CameraServer::get_singleton());
644-
if (camera_android) {
645-
camera_android->handle_rotation_change();
633+
CameraServer *camera_server = CameraServer::get_singleton();
634+
if (camera_server) {
635+
camera_server->handle_display_rotation_change(p_orientation);
646636
}
647-
#endif // MODULE_CAMERA_ENABLED
648637
}
649638

650639
JNIEXPORT jboolean JNICALL Java_org_godotengine_godot_GodotLib_shouldDispatchInputToRenderThread(JNIEnv *env, jclass clazz) {

platform/android/java_godot_lib_jni.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_hardwareKeyboardConne
7272
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_filePickerCallback(JNIEnv *env, jclass clazz, jboolean p_ok, jobjectArray p_selected_paths);
7373
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_onRendererResumed(JNIEnv *env, jclass clazz);
7474
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_onRendererPaused(JNIEnv *env, jclass clazz);
75-
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_onScreenRotationChange(JNIEnv *env, jclass clazz);
75+
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_onScreenRotationChange(JNIEnv *env, jclass clazz, jint p_orientation);
7676
JNIEXPORT jboolean JNICALL Java_org_godotengine_godot_GodotLib_shouldDispatchInputToRenderThread(JNIEnv *env, jclass clazz);
7777
JNIEXPORT jstring JNICALL Java_org_godotengine_godot_GodotLib_getProjectResourceDir(JNIEnv *env, jclass clazz);
7878
JNIEXPORT jboolean JNICALL Java_org_godotengine_godot_GodotLib_isEditorHint(JNIEnv *env, jclass clazz);

servers/camera/camera_server.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ class CameraServer : public Object {
111111
// Intended for use with custom CameraServer implementation.
112112
RID feed_texture(int p_id, FeedImage p_texture);
113113

114+
// Platform lifecycle callbacks (virtual, default empty implementation).
115+
virtual void handle_application_pause() {}
116+
virtual void handle_application_resume() {}
117+
virtual void handle_display_rotation_change(int p_orientation) { (void)p_orientation; }
118+
114119
CameraServer();
115120
~CameraServer();
116121
};

0 commit comments

Comments
 (0)