Fix Camera2D is not working inside EditorPlugin

This commit is contained in:
Mounir Tohami 2023-08-13 22:14:13 +00:00
parent c5da2e54fe
commit 4e4a725fff
2 changed files with 46 additions and 32 deletions

View File

@ -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;
}
@ -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) {
@ -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 {
@ -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]));
@ -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;
@ -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 {
@ -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();
}
@ -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;
}
@ -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();
@ -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;
@ -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;
@ -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;

View File

@ -85,6 +85,7 @@ protected:
bool drag_vertical_offset_changed = false;
Point2 camera_screen_center;
bool _is_editing_in_editor() const;
void _update_process_callback();
void _update_scroll();