Merge pull request #104136 from KoBeWi/cleaning_camera

Camera2D code cleanup
This commit is contained in:
Rémi Verschelde 2025-03-19 12:27:14 +01:00
commit 9ca0de58bb
No known key found for this signature in database
GPG key ID: C3336907360768E1
2 changed files with 73 additions and 60 deletions

View file

@ -86,20 +86,12 @@ bool Camera2D::_edit_use_rect() const {
} }
#endif // DEBUG_ENABLED #endif // DEBUG_ENABLED
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() { void Camera2D::_update_scroll() {
if (!is_inside_tree() || !viewport) { if (!is_inside_tree() || !viewport) {
return; return;
} }
if (_is_editing_in_editor()) { if (is_part_of_edited_scene()) {
queue_redraw(); queue_redraw();
return; return;
} }
@ -149,7 +141,7 @@ void Camera2D::_update_process_callback() {
WARN_PRINT_ONCE("Camera2D overridden to physics process mode due to use of physics interpolation."); WARN_PRINT_ONCE("Camera2D overridden to physics process mode due to use of physics interpolation.");
} }
#endif #endif
} else if (_is_editing_in_editor()) { } else if (is_part_of_edited_scene()) {
set_process_internal(false); set_process_internal(false);
set_physics_process_internal(false); set_physics_process_internal(false);
} else { } else {
@ -164,7 +156,7 @@ void Camera2D::_update_process_callback() {
} }
void Camera2D::set_zoom(const Vector2 &p_zoom) { void Camera2D::set_zoom(const Vector2 &p_zoom) {
// Setting zoom to zero causes 'affine_invert' issues // Setting zoom to zero causes 'affine_invert' issues.
ERR_FAIL_COND_MSG(Math::is_zero_approx(p_zoom.x) || Math::is_zero_approx(p_zoom.y), "Zoom level must be different from 0 (can be negative)."); ERR_FAIL_COND_MSG(Math::is_zero_approx(p_zoom.x) || Math::is_zero_approx(p_zoom.y), "Zoom level must be different from 0 (can be negative).");
zoom = p_zoom; zoom = p_zoom;
@ -192,7 +184,7 @@ Transform2D Camera2D::get_camera_transform() {
if (!first) { if (!first) {
if (anchor_mode == ANCHOR_MODE_DRAG_CENTER) { if (anchor_mode == ANCHOR_MODE_DRAG_CENTER) {
if (drag_horizontal_enabled && !_is_editing_in_editor() && !drag_horizontal_offset_changed) { if (drag_horizontal_enabled && !is_part_of_edited_scene() && !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 = 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])); camera_pos.x = MAX(camera_pos.x, (new_camera_pos.x - screen_size.x * 0.5 * zoom_scale.x * drag_margin[SIDE_RIGHT]));
} else { } else {
@ -205,7 +197,7 @@ Transform2D Camera2D::get_camera_transform() {
drag_horizontal_offset_changed = false; drag_horizontal_offset_changed = false;
} }
if (drag_vertical_enabled && !_is_editing_in_editor() && !drag_vertical_offset_changed) { if (drag_vertical_enabled && !is_part_of_edited_scene() && !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 = 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])); camera_pos.y = MAX(camera_pos.y, (new_camera_pos.y - screen_size.y * 0.5 * zoom_scale.y * drag_margin[SIDE_BOTTOM]));
@ -249,7 +241,7 @@ Transform2D Camera2D::get_camera_transform() {
// It may be called MULTIPLE TIMES on certain frames, // It may be called MULTIPLE TIMES on certain frames,
// therefore smoothing is not currently applied only once per frame / tick, // therefore smoothing is not currently applied only once per frame / tick,
// which will result in some haphazard results. // which will result in some haphazard results.
if (position_smoothing_enabled && !_is_editing_in_editor()) { if (position_smoothing_enabled && !is_part_of_edited_scene()) {
bool physics_process = (process_callback == CAMERA2D_PROCESS_PHYSICS) || is_physics_interpolated_and_enabled(); bool physics_process = (process_callback == CAMERA2D_PROCESS_PHYSICS) || is_physics_interpolated_and_enabled();
real_t delta = physics_process ? get_physics_process_delta_time() : get_process_delta_time(); real_t delta = physics_process ? get_physics_process_delta_time() : get_process_delta_time();
real_t c = position_smoothing_speed * delta; real_t c = position_smoothing_speed * delta;
@ -268,7 +260,7 @@ Transform2D Camera2D::get_camera_transform() {
Point2 screen_offset = (anchor_mode == ANCHOR_MODE_DRAG_CENTER ? (screen_size * 0.5 * zoom_scale) : Point2()); Point2 screen_offset = (anchor_mode == ANCHOR_MODE_DRAG_CENTER ? (screen_size * 0.5 * zoom_scale) : Point2());
if (!ignore_rotation) { if (!ignore_rotation) {
if (rotation_smoothing_enabled && !_is_editing_in_editor()) { if (rotation_smoothing_enabled && !is_part_of_edited_scene()) {
real_t step = rotation_smoothing_speed * (process_callback == CAMERA2D_PROCESS_PHYSICS ? get_physics_process_delta_time() : get_process_delta_time()); 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); camera_angle = Math::lerp_angle(camera_angle, get_global_rotation(), step);
} else { } else {
@ -368,7 +360,7 @@ void Camera2D::_notification(int p_what) {
} break; } break;
case NOTIFICATION_TRANSFORM_CHANGED: { case NOTIFICATION_TRANSFORM_CHANGED: {
if ((!position_smoothing_enabled && !is_physics_interpolated_and_enabled()) || _is_editing_in_editor()) { if ((!position_smoothing_enabled && !is_physics_interpolated_and_enabled()) || is_part_of_edited_scene()) {
_update_scroll(); _update_scroll();
} }
if (is_physics_interpolated_and_enabled()) { if (is_physics_interpolated_and_enabled()) {
@ -396,7 +388,7 @@ void Camera2D::_notification(int p_what) {
add_to_group(group_name); add_to_group(group_name);
add_to_group(canvas_group_name); add_to_group(canvas_group_name);
if (!_is_editing_in_editor() && enabled && !viewport->get_camera_2d()) { if (!is_part_of_edited_scene() && enabled && !viewport->get_camera_2d()) {
make_current(); make_current();
} }
@ -427,7 +419,7 @@ void Camera2D::_notification(int p_what) {
#ifdef TOOLS_ENABLED #ifdef TOOLS_ENABLED
case NOTIFICATION_DRAW: { case NOTIFICATION_DRAW: {
if (!is_inside_tree() || !_is_editing_in_editor()) { if (!is_inside_tree() || !is_part_of_edited_scene()) {
break; break;
} }
@ -504,6 +496,9 @@ void Camera2D::_notification(int p_what) {
} }
void Camera2D::set_offset(const Vector2 &p_offset) { void Camera2D::set_offset(const Vector2 &p_offset) {
if (offset == p_offset) {
return;
}
offset = p_offset; offset = p_offset;
Point2 old_smoothed_camera_pos = smoothed_camera_pos; Point2 old_smoothed_camera_pos = smoothed_camera_pos;
_update_scroll(); _update_scroll();
@ -515,6 +510,9 @@ Vector2 Camera2D::get_offset() const {
} }
void Camera2D::set_anchor_mode(AnchorMode p_anchor_mode) { void Camera2D::set_anchor_mode(AnchorMode p_anchor_mode) {
if (anchor_mode == p_anchor_mode) {
return;
}
anchor_mode = p_anchor_mode; anchor_mode = p_anchor_mode;
_update_scroll(); _update_scroll();
} }
@ -524,6 +522,9 @@ Camera2D::AnchorMode Camera2D::get_anchor_mode() const {
} }
void Camera2D::set_ignore_rotation(bool p_ignore) { void Camera2D::set_ignore_rotation(bool p_ignore) {
if (ignore_rotation == p_ignore) {
return;
}
ignore_rotation = p_ignore; ignore_rotation = p_ignore;
Point2 old_smoothed_camera_pos = smoothed_camera_pos; Point2 old_smoothed_camera_pos = smoothed_camera_pos;
@ -541,13 +542,13 @@ bool Camera2D::is_ignoring_rotation() const {
} }
void Camera2D::set_limit_enabled(bool p_limit_enabled) { void Camera2D::set_limit_enabled(bool p_limit_enabled) {
if (p_limit_enabled == limit_enabled) { if (limit_enabled == p_limit_enabled) {
return; return;
} }
limit_enabled = p_limit_enabled; limit_enabled = p_limit_enabled;
_update_scroll(); _update_scroll();
#ifdef TOOLS_ENABLED #ifdef TOOLS_ENABLED
emit_signal("_camera_limit_enabled_updated"); // Used for Camera2DEditorPlugin emit_signal("_camera_limit_enabled_updated"); // Used for Camera2DEditorPlugin.
#endif #endif
notify_property_list_changed(); notify_property_list_changed();
} }
@ -566,6 +567,9 @@ void Camera2D::set_process_callback(Camera2DProcessCallback p_mode) {
} }
void Camera2D::set_enabled(bool p_enabled) { void Camera2D::set_enabled(bool p_enabled) {
if (enabled == p_enabled) {
return;
}
enabled = p_enabled; enabled = p_enabled;
if (!is_inside_tree()) { if (!is_inside_tree()) {
@ -613,7 +617,7 @@ void Camera2D::_make_current(Object *p_which) {
} }
void Camera2D::_update_process_internal_for_smoothing() { void Camera2D::_update_process_internal_for_smoothing() {
bool is_not_in_scene_or_editor = !(is_inside_tree() && _is_editing_in_editor()); bool is_not_in_scene_or_editor = !(is_inside_tree() && is_part_of_edited_scene());
bool is_any_smoothing_valid = position_smoothing_speed > 0 || rotation_smoothing_speed > 0; 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; bool enable = is_any_smoothing_valid && is_not_in_scene_or_editor;
@ -670,6 +674,9 @@ bool Camera2D::is_current() const {
void Camera2D::set_limit(Side p_side, int p_limit) { void Camera2D::set_limit(Side p_side, int p_limit) {
ERR_FAIL_INDEX((int)p_side, 4); ERR_FAIL_INDEX((int)p_side, 4);
if (limit[p_side] == p_limit) {
return;
}
limit[p_side] = p_limit; limit[p_side] = p_limit;
Point2 old_smoothed_camera_pos = smoothed_camera_pos; Point2 old_smoothed_camera_pos = smoothed_camera_pos;
_update_scroll(); _update_scroll();
@ -681,8 +688,11 @@ int Camera2D::get_limit(Side p_side) const {
return limit[p_side]; return limit[p_side];
} }
void Camera2D::set_limit_smoothing_enabled(bool enable) { void Camera2D::set_limit_smoothing_enabled(bool p_enabled) {
limit_smoothing_enabled = enable; if (limit_smoothing_enabled == p_enabled) {
return;
}
limit_smoothing_enabled = p_enabled;
_update_scroll(); _update_scroll();
} }
@ -692,6 +702,9 @@ bool Camera2D::is_limit_smoothing_enabled() const {
void Camera2D::set_drag_margin(Side p_side, real_t p_drag_margin) { void Camera2D::set_drag_margin(Side p_side, real_t p_drag_margin) {
ERR_FAIL_INDEX((int)p_side, 4); ERR_FAIL_INDEX((int)p_side, 4);
if (drag_margin[p_side] == p_drag_margin) {
return;
}
drag_margin[p_side] = p_drag_margin; drag_margin[p_side] = p_drag_margin;
queue_redraw(); queue_redraw();
} }
@ -739,6 +752,9 @@ void Camera2D::align() {
} }
void Camera2D::set_position_smoothing_speed(real_t p_speed) { void Camera2D::set_position_smoothing_speed(real_t p_speed) {
if (position_smoothing_speed == p_speed) {
return;
}
position_smoothing_speed = MAX(0, p_speed); position_smoothing_speed = MAX(0, p_speed);
_update_process_internal_for_smoothing(); _update_process_internal_for_smoothing();
} }
@ -748,6 +764,9 @@ real_t Camera2D::get_position_smoothing_speed() const {
} }
void Camera2D::set_rotation_smoothing_speed(real_t p_speed) { void Camera2D::set_rotation_smoothing_speed(real_t p_speed) {
if (rotation_smoothing_speed == p_speed) {
return;
}
rotation_smoothing_speed = MAX(0, p_speed); rotation_smoothing_speed = MAX(0, p_speed);
_update_process_internal_for_smoothing(); _update_process_internal_for_smoothing();
} }
@ -757,6 +776,9 @@ real_t Camera2D::get_rotation_smoothing_speed() const {
} }
void Camera2D::set_rotation_smoothing_enabled(bool p_enabled) { void Camera2D::set_rotation_smoothing_enabled(bool p_enabled) {
if (rotation_smoothing_enabled == p_enabled) {
return;
}
rotation_smoothing_enabled = p_enabled; rotation_smoothing_enabled = p_enabled;
notify_property_list_changed(); notify_property_list_changed();
} }
@ -770,7 +792,7 @@ Point2 Camera2D::get_camera_screen_center() const {
} }
Size2 Camera2D::_get_camera_screen_size() const { Size2 Camera2D::_get_camera_screen_size() const {
if (_is_editing_in_editor()) { if (is_part_of_edited_scene()) {
return Size2(GLOBAL_GET("display/window/size/viewport_width"), GLOBAL_GET("display/window/size/viewport_height")); return Size2(GLOBAL_GET("display/window/size/viewport_width"), GLOBAL_GET("display/window/size/viewport_height"));
} }
return get_viewport_rect().size; return get_viewport_rect().size;
@ -793,6 +815,9 @@ bool Camera2D::is_drag_vertical_enabled() const {
} }
void Camera2D::set_drag_vertical_offset(real_t p_offset) { void Camera2D::set_drag_vertical_offset(real_t p_offset) {
if (drag_vertical_offset == p_offset) {
return;
}
drag_vertical_offset = p_offset; drag_vertical_offset = p_offset;
drag_vertical_offset_changed = true; drag_vertical_offset_changed = true;
Point2 old_smoothed_camera_pos = smoothed_camera_pos; Point2 old_smoothed_camera_pos = smoothed_camera_pos;
@ -805,6 +830,9 @@ real_t Camera2D::get_drag_vertical_offset() const {
} }
void Camera2D::set_drag_horizontal_offset(real_t p_offset) { void Camera2D::set_drag_horizontal_offset(real_t p_offset) {
if (drag_horizontal_offset == p_offset) {
return;
}
drag_horizontal_offset = p_offset; drag_horizontal_offset = p_offset;
drag_horizontal_offset_changed = true; drag_horizontal_offset_changed = true;
Point2 old_smoothed_camera_pos = smoothed_camera_pos; Point2 old_smoothed_camera_pos = smoothed_camera_pos;
@ -816,15 +844,10 @@ real_t Camera2D::get_drag_horizontal_offset() const {
return drag_horizontal_offset; return drag_horizontal_offset;
} }
void Camera2D::_set_old_smoothing(real_t p_enable) {
//compatibility
if (p_enable > 0) {
position_smoothing_enabled = true;
set_position_smoothing_speed(p_enable);
}
}
void Camera2D::set_position_smoothing_enabled(bool p_enabled) { void Camera2D::set_position_smoothing_enabled(bool p_enabled) {
if (position_smoothing_enabled == p_enabled) {
return;
}
position_smoothing_enabled = p_enabled; position_smoothing_enabled = p_enabled;
notify_property_list_changed(); notify_property_list_changed();
} }
@ -835,6 +858,10 @@ bool Camera2D::is_position_smoothing_enabled() const {
void Camera2D::set_custom_viewport(Node *p_viewport) { void Camera2D::set_custom_viewport(Node *p_viewport) {
ERR_FAIL_NULL(p_viewport); ERR_FAIL_NULL(p_viewport);
if (custom_viewport == p_viewport) {
return;
}
if (is_inside_tree()) { if (is_inside_tree()) {
remove_from_group(group_name); remove_from_group(group_name);
remove_from_group(canvas_group_name); remove_from_group(canvas_group_name);
@ -867,8 +894,8 @@ Node *Camera2D::get_custom_viewport() const {
return custom_viewport; return custom_viewport;
} }
void Camera2D::set_screen_drawing_enabled(bool enable) { void Camera2D::set_screen_drawing_enabled(bool p_enabled) {
screen_drawing_enabled = enable; screen_drawing_enabled = p_enabled;
#ifdef TOOLS_ENABLED #ifdef TOOLS_ENABLED
queue_redraw(); queue_redraw();
#endif #endif
@ -878,8 +905,8 @@ bool Camera2D::is_screen_drawing_enabled() const {
return screen_drawing_enabled; return screen_drawing_enabled;
} }
void Camera2D::set_limit_drawing_enabled(bool enable) { void Camera2D::set_limit_drawing_enabled(bool p_enabled) {
limit_drawing_enabled = enable; limit_drawing_enabled = p_enabled;
#ifdef TOOLS_ENABLED #ifdef TOOLS_ENABLED
queue_redraw(); queue_redraw();
#endif #endif
@ -889,8 +916,8 @@ bool Camera2D::is_limit_drawing_enabled() const {
return limit_drawing_enabled; return limit_drawing_enabled;
} }
void Camera2D::set_margin_drawing_enabled(bool enable) { void Camera2D::set_margin_drawing_enabled(bool p_enabled) {
margin_drawing_enabled = enable; margin_drawing_enabled = p_enabled;
#ifdef TOOLS_ENABLED #ifdef TOOLS_ENABLED
queue_redraw(); queue_redraw();
#endif #endif
@ -983,8 +1010,6 @@ void Camera2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("reset_smoothing"), &Camera2D::reset_smoothing); ClassDB::bind_method(D_METHOD("reset_smoothing"), &Camera2D::reset_smoothing);
ClassDB::bind_method(D_METHOD("align"), &Camera2D::align); ClassDB::bind_method(D_METHOD("align"), &Camera2D::align);
ClassDB::bind_method(D_METHOD("_set_old_smoothing", "follow_smoothing"), &Camera2D::_set_old_smoothing);
ClassDB::bind_method(D_METHOD("set_screen_drawing_enabled", "screen_drawing_enabled"), &Camera2D::set_screen_drawing_enabled); ClassDB::bind_method(D_METHOD("set_screen_drawing_enabled", "screen_drawing_enabled"), &Camera2D::set_screen_drawing_enabled);
ClassDB::bind_method(D_METHOD("is_screen_drawing_enabled"), &Camera2D::is_screen_drawing_enabled); ClassDB::bind_method(D_METHOD("is_screen_drawing_enabled"), &Camera2D::is_screen_drawing_enabled);
@ -1040,20 +1065,10 @@ void Camera2D::_bind_methods() {
} }
Camera2D::Camera2D() { Camera2D::Camera2D() {
limit[SIDE_LEFT] = -10000000;
limit[SIDE_TOP] = -10000000;
limit[SIDE_RIGHT] = 10000000;
limit[SIDE_BOTTOM] = 10000000;
drag_margin[SIDE_LEFT] = 0.2;
drag_margin[SIDE_TOP] = 0.2;
drag_margin[SIDE_RIGHT] = 0.2;
drag_margin[SIDE_BOTTOM] = 0.2;
set_notify_transform(true); set_notify_transform(true);
set_hide_clip_children(true); set_hide_clip_children(true);
#ifdef TOOLS_ENABLED #ifdef TOOLS_ENABLED
add_user_signal(MethodInfo("_camera_limit_enabled_updated")); // Camera2DEditorPlugin listens to this add_user_signal(MethodInfo("_camera_limit_enabled_updated")); // Camera2DEditorPlugin listens to this.
#endif #endif
} }

View file

@ -52,7 +52,7 @@ protected:
bool first = true; bool first = true;
bool just_exited_tree = false; bool just_exited_tree = false;
ObjectID custom_viewport_id; // to check validity ObjectID custom_viewport_id; // To check validity.
Viewport *custom_viewport = nullptr; Viewport *custom_viewport = nullptr;
Viewport *viewport = nullptr; Viewport *viewport = nullptr;
@ -73,10 +73,10 @@ protected:
bool rotation_smoothing_enabled = false; bool rotation_smoothing_enabled = false;
bool limit_enabled = true; bool limit_enabled = true;
int limit[4]; int limit[4] = { -10000000, -10000000, 10000000, 10000000 }; // Left, top, right, bottom.
bool limit_smoothing_enabled = false; bool limit_smoothing_enabled = false;
real_t drag_margin[4]; real_t drag_margin[4] = { 0.2, 0.2, 0.2, 0.2 };
bool drag_horizontal_enabled = false; bool drag_horizontal_enabled = false;
bool drag_vertical_enabled = false; bool drag_vertical_enabled = false;
real_t drag_horizontal_offset = 0.0; real_t drag_horizontal_offset = 0.0;
@ -97,8 +97,6 @@ protected:
void _make_current(Object *p_which); void _make_current(Object *p_which);
void _reset_just_exited() { just_exited_tree = false; } void _reset_just_exited() { just_exited_tree = false; }
void _set_old_smoothing(real_t p_enable);
void _update_process_internal_for_smoothing(); void _update_process_internal_for_smoothing();
void _set_limit_rect(const Rect2 &p_limit_rect); void _set_limit_rect(const Rect2 &p_limit_rect);
@ -160,7 +158,7 @@ public:
void set_limit(Side p_side, int p_limit); void set_limit(Side p_side, int p_limit);
int get_limit(Side p_side) const; int get_limit(Side p_side) const;
void set_limit_smoothing_enabled(bool enable); void set_limit_smoothing_enabled(bool p_enabled);
bool is_limit_smoothing_enabled() const; bool is_limit_smoothing_enabled() const;
void set_drag_horizontal_enabled(bool p_enabled); void set_drag_horizontal_enabled(bool p_enabled);
@ -213,13 +211,13 @@ public:
void reset_smoothing(); void reset_smoothing();
void align(); void align();
void set_screen_drawing_enabled(bool enable); void set_screen_drawing_enabled(bool p_enabled);
bool is_screen_drawing_enabled() const; bool is_screen_drawing_enabled() const;
void set_limit_drawing_enabled(bool enable); void set_limit_drawing_enabled(bool p_enabled);
bool is_limit_drawing_enabled() const; bool is_limit_drawing_enabled() const;
void set_margin_drawing_enabled(bool enable); void set_margin_drawing_enabled(bool p_enabled);
bool is_margin_drawing_enabled() const; bool is_margin_drawing_enabled() const;
Camera2D(); Camera2D();