Skip to content

Commit

Permalink
Merge pull request #44690 from lawnjelly/transform_snap2
Browse files Browse the repository at this point in the history
[3.2] Improve 2d snapping
  • Loading branch information
akien-mga authored Jan 5, 2021
2 parents 6a708d6 + a237c67 commit 3032b38
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
2 changes: 2 additions & 0 deletions core/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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; }
Expand Down
1 change: 1 addition & 0 deletions main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,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);
Expand Down
4 changes: 2 additions & 2 deletions scene/2d/sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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();
}

Expand Down
16 changes: 14 additions & 2 deletions servers/visual/visual_server_viewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 3032b38

Please sign in to comment.