Skip to content

Commit cd8a292

Browse files
committed
Merge pull request #113297 from shiena/feature/lifecycle-callbacks
Add platform lifecycle callbacks to CameraServer base class
2 parents 5f12ada + 85ec5b9 commit cd8a292

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"
@@ -607,12 +602,10 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_onRendererResumed(JNI
607602

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

626-
#ifdef MODULE_CAMERA_ENABLED
627-
CameraAndroid *camera_android = Object::cast_to<CameraAndroid>(CameraServer::get_singleton());
628-
if (camera_android) {
629-
camera_android->handle_pause();
619+
CameraServer *camera_server = CameraServer::get_singleton();
620+
if (camera_server) {
621+
camera_server->handle_application_pause();
630622
}
631-
#endif // MODULE_CAMERA_ENABLED
632623

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

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

643-
#ifdef MODULE_CAMERA_ENABLED
644-
CameraAndroid *camera_android = Object::cast_to<CameraAndroid>(CameraServer::get_singleton());
645-
if (camera_android) {
646-
camera_android->handle_rotation_change();
634+
CameraServer *camera_server = CameraServer::get_singleton();
635+
if (camera_server) {
636+
camera_server->handle_display_rotation_change(p_orientation);
647637
}
648-
#endif // MODULE_CAMERA_ENABLED
649638
}
650639

651640
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)