Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Camera2D is not working inside a MainScreenEditorPlugin #79867

Merged
merged 1 commit into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 45 additions & 32 deletions scene/2d/camera_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,21 @@
#include "core/config/project_settings.h"
#include "scene/main/window.h"

bool Camera2D::_is_editing_in_editor() const {
#ifdef TOOLS_ENABLED
return is_part_of_edited_scene();
#else
return false;
#endif // TOOLS_ENABLED
}

void Camera2D::_update_scroll() {
if (!is_inside_tree()) {
if (!is_inside_tree() || !viewport) {
return;
}

if (Engine::get_singleton()->is_editor_hint()) {
if (_is_editing_in_editor()) {
queue_redraw();
// Only set viewport transform when not bound to the main viewport.
if (get_tree()->get_edited_scene_root() && get_viewport() == get_tree()->get_edited_scene_root()->get_viewport()) {
return;
}
}

if (!viewport) {
return;
}

Expand All @@ -65,7 +66,7 @@ void Camera2D::_update_scroll() {
}

void Camera2D::_update_process_callback() {
if (Engine::get_singleton()->is_editor_hint()) {
if (_is_editing_in_editor()) {
set_process_internal(false);
set_physics_process_internal(false);
} else if (process_callback == CAMERA2D_PROCESS_IDLE) {
Expand Down Expand Up @@ -106,7 +107,7 @@ Transform2D Camera2D::get_camera_transform() {

if (!first) {
if (anchor_mode == ANCHOR_MODE_DRAG_CENTER) {
if (drag_horizontal_enabled && !Engine::get_singleton()->is_editor_hint() && !drag_horizontal_offset_changed) {
if (drag_horizontal_enabled && !_is_editing_in_editor() && !drag_horizontal_offset_changed) {
camera_pos.x = MIN(camera_pos.x, (new_camera_pos.x + screen_size.x * 0.5 * zoom_scale.x * drag_margin[SIDE_LEFT]));
camera_pos.x = MAX(camera_pos.x, (new_camera_pos.x - screen_size.x * 0.5 * zoom_scale.x * drag_margin[SIDE_RIGHT]));
} else {
Expand All @@ -119,7 +120,7 @@ Transform2D Camera2D::get_camera_transform() {
drag_horizontal_offset_changed = false;
}

if (drag_vertical_enabled && !Engine::get_singleton()->is_editor_hint() && !drag_vertical_offset_changed) {
if (drag_vertical_enabled && !_is_editing_in_editor() && !drag_vertical_offset_changed) {
camera_pos.y = MIN(camera_pos.y, (new_camera_pos.y + screen_size.y * 0.5 * zoom_scale.y * drag_margin[SIDE_TOP]));
camera_pos.y = MAX(camera_pos.y, (new_camera_pos.y - screen_size.y * 0.5 * zoom_scale.y * drag_margin[SIDE_BOTTOM]));

Expand Down Expand Up @@ -158,7 +159,7 @@ Transform2D Camera2D::get_camera_transform() {
}
}

if (position_smoothing_enabled && !Engine::get_singleton()->is_editor_hint()) {
if (position_smoothing_enabled && !_is_editing_in_editor()) {
real_t c = position_smoothing_speed * (process_callback == CAMERA2D_PROCESS_PHYSICS ? get_physics_process_delta_time() : get_process_delta_time());
smoothed_camera_pos = ((camera_pos - smoothed_camera_pos) * c) + smoothed_camera_pos;
ret_camera_pos = smoothed_camera_pos;
Expand All @@ -175,7 +176,7 @@ Transform2D Camera2D::get_camera_transform() {
Point2 screen_offset = (anchor_mode == ANCHOR_MODE_DRAG_CENTER ? (screen_size * 0.5 * zoom_scale) : Point2());

if (!ignore_rotation) {
if (rotation_smoothing_enabled && !Engine::get_singleton()->is_editor_hint()) {
if (rotation_smoothing_enabled && !_is_editing_in_editor()) {
real_t step = rotation_smoothing_speed * (process_callback == CAMERA2D_PROCESS_PHYSICS ? get_physics_process_delta_time() : get_process_delta_time());
camera_angle = Math::lerp_angle(camera_angle, get_global_rotation(), step);
} else {
Expand Down Expand Up @@ -250,7 +251,7 @@ void Camera2D::_notification(int p_what) {
add_to_group(group_name);
add_to_group(canvas_group_name);

if (!Engine::get_singleton()->is_editor_hint() && enabled && !viewport->get_camera_2d()) {
if (!_is_editing_in_editor() && enabled && !viewport->get_camera_2d()) {
make_current();
}

Expand All @@ -272,7 +273,7 @@ void Camera2D::_notification(int p_what) {

#ifdef TOOLS_ENABLED
case NOTIFICATION_DRAW: {
if (!is_inside_tree() || !Engine::get_singleton()->is_editor_hint()) {
if (!is_inside_tree() || !_is_editing_in_editor()) {
break;
}

Expand Down Expand Up @@ -398,7 +399,11 @@ void Camera2D::set_process_callback(Camera2DProcessCallback p_mode) {
void Camera2D::set_enabled(bool p_enabled) {
enabled = p_enabled;

if (enabled && is_inside_tree() && !viewport->get_camera_2d()) {
if (!is_inside_tree()) {
return;
}

if (enabled && !viewport->get_camera_2d()) {
make_current();
} else if (!enabled && is_current()) {
clear_current();
Expand All @@ -414,27 +419,27 @@ Camera2D::Camera2DProcessCallback Camera2D::get_process_callback() const {
}

void Camera2D::_make_current(Object *p_which) {
if (!viewport || (custom_viewport && !ObjectDB::get_instance(custom_viewport_id))) {
if (!is_inside_tree() || !viewport) {
return;
}

if (custom_viewport && !ObjectDB::get_instance(custom_viewport_id)) {
return;
}

queue_redraw();

if (p_which == this) {
if (is_inside_tree()) {
viewport->_camera_2d_set(this);
queue_redraw();
}
viewport->_camera_2d_set(this);
} else {
if (is_inside_tree()) {
if (viewport->get_camera_2d() == this) {
viewport->_camera_2d_set(nullptr);
}
queue_redraw();
if (viewport->get_camera_2d() == this) {
viewport->_camera_2d_set(nullptr);
}
}
}

void Camera2D::_update_process_internal_for_smoothing() {
bool is_not_in_scene_or_editor = !(is_inside_tree() && Engine::get_singleton()->is_editor_hint());
bool is_not_in_scene_or_editor = !(is_inside_tree() && _is_editing_in_editor());
bool is_any_smoothing_valid = position_smoothing_speed > 0 || rotation_smoothing_speed > 0;

bool enable = is_any_smoothing_valid && is_not_in_scene_or_editor;
Expand All @@ -453,13 +458,22 @@ void Camera2D::make_current() {

void Camera2D::clear_current() {
ERR_FAIL_COND(!is_current());
if (viewport && !(custom_viewport && !ObjectDB::get_instance(custom_viewport_id)) && viewport->is_inside_tree()) {

if (!viewport || !viewport->is_inside_tree()) {
return;
}

if (!custom_viewport || ObjectDB::get_instance(custom_viewport_id)) {
viewport->assign_next_enabled_camera_2d(group_name);
}
}

bool Camera2D::is_current() const {
if (viewport && !(custom_viewport && !ObjectDB::get_instance(custom_viewport_id))) {
if (!viewport) {
return false;
}

if (!custom_viewport || ObjectDB::get_instance(custom_viewport_id)) {
return viewport->get_camera_2d() == this;
}
return false;
Expand Down Expand Up @@ -567,8 +581,7 @@ Point2 Camera2D::get_camera_screen_center() const {
}

Size2 Camera2D::_get_camera_screen_size() const {
// special case if the camera2D is in the root viewport
if (Engine::get_singleton()->is_editor_hint() && get_viewport()->get_parent_viewport() == get_tree()->get_root()) {
if (_is_editing_in_editor()) {
return Size2(GLOBAL_GET("display/window/size/viewport_width"), GLOBAL_GET("display/window/size/viewport_height"));
}
return get_viewport_rect().size;
Expand Down
1 change: 1 addition & 0 deletions scene/2d/camera_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class Camera2D : public Node2D {
bool drag_vertical_offset_changed = false;

Point2 camera_screen_center;
bool _is_editing_in_editor() const;
void _update_process_callback();
void _update_scroll();

Expand Down