From a237c671bb750f77fcb5835bf3177ef122f3ff07 Mon Sep 17 00:00:00 2001 From: lawnjelly Date: Fri, 25 Dec 2020 18:53:00 +0000 Subject: [PATCH] Improve 2d snapping Partially revert change allowing sprite get_rect snapping to be controlled by `pixel_snap` again rather than `transform_snap` (to prevent breaking compatibility). Adds a final `use_camera_snap` project setting to allow snapping viewports as in reduz original PR. --- core/engine.h | 2 ++ main/main.cpp | 1 + scene/2d/sprite.cpp | 4 ++-- servers/visual/visual_server_viewport.cpp | 16 ++++++++++++++-- 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/core/engine.h b/core/engine.h index 4345a53db3d3..0280d47f7625 100644 --- a/core/engine.h +++ b/core/engine.h @@ -60,6 +60,7 @@ class Engine { float _time_scale; bool _pixel_snap; bool _snap_2d_transforms; + bool _snap_2d_viewports; uint64_t _physics_frames; float _physics_interpolation_fraction; @@ -109,6 +110,7 @@ class Engine { _FORCE_INLINE_ bool get_use_pixel_snap() const { return _pixel_snap; } bool get_snap_2d_transforms() const { return _snap_2d_transforms; } + bool get_snap_2d_viewports() const { return _snap_2d_viewports; } #ifdef TOOLS_ENABLED _FORCE_INLINE_ void set_editor_hint(bool p_enabled) { editor_hint = p_enabled; } diff --git a/main/main.cpp b/main/main.cpp index 81bc9a58f0c3..4f3f4523cef9 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -1114,6 +1114,7 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph Engine::get_singleton()->_pixel_snap = GLOBAL_DEF("rendering/quality/2d/use_pixel_snap", false); Engine::get_singleton()->_snap_2d_transforms = GLOBAL_DEF("rendering/quality/2d/use_transform_snap", false); + Engine::get_singleton()->_snap_2d_viewports = GLOBAL_DEF("rendering/quality/2d/use_camera_snap", false); OS::get_singleton()->_keep_screen_on = GLOBAL_DEF("display/window/energy_saving/keep_screen_on", true); if (rtm == -1) { rtm = GLOBAL_DEF("rendering/threads/thread_model", OS::RENDER_THREAD_SAFE); diff --git a/scene/2d/sprite.cpp b/scene/2d/sprite.cpp index b228eba5fb96..42eb9f7834ae 100644 --- a/scene/2d/sprite.cpp +++ b/scene/2d/sprite.cpp @@ -99,7 +99,7 @@ void Sprite::_get_rects(Rect2 &r_src_rect, Rect2 &r_dst_rect, bool &r_filter_cli Point2 dest_offset = offset; if (centered) dest_offset -= frame_size / 2; - if (Engine::get_singleton()->get_snap_2d_transforms()) { + if (Engine::get_singleton()->get_use_pixel_snap()) { dest_offset = dest_offset.floor(); } @@ -378,7 +378,7 @@ Rect2 Sprite::get_rect() const { Point2 ofs = offset; if (centered) ofs -= Size2(s) / 2; - if (Engine::get_singleton()->get_snap_2d_transforms()) { + if (Engine::get_singleton()->get_use_pixel_snap()) { ofs = ofs.floor(); } diff --git a/servers/visual/visual_server_viewport.cpp b/servers/visual/visual_server_viewport.cpp index c844da7e9122..9fe2f0388529 100644 --- a/servers/visual/visual_server_viewport.cpp +++ b/servers/visual/visual_server_viewport.cpp @@ -40,12 +40,24 @@ static Transform2D _canvas_get_transform(VisualServerViewport::Viewport *p_viewp Transform2D xf = p_viewport->global_transform; float scale = 1.0; + + bool snap = Engine::get_singleton()->get_snap_2d_viewports(); + if (p_viewport->canvas_map.has(p_canvas->parent)) { - xf = xf * p_viewport->canvas_map[p_canvas->parent].transform; + + Transform2D c_xform = p_viewport->canvas_map[p_canvas->parent].transform; + if (snap) { + c_xform.elements[2] = c_xform.elements[2].floor(); + } + xf = xf * c_xform; scale = p_canvas->parent_scale; } - xf = xf * p_canvas_data->transform; + Transform2D c_xform = p_canvas_data->transform; + if (snap) { + c_xform.elements[2] = c_xform.elements[2].floor(); + } + xf = xf * c_xform; if (scale != 1.0 && !VSG::canvas->disable_scale) { Vector2 pivot = p_vp_size * 0.5;