From ee2f406c73c6a49ce80f54e3bfe30477bc9cda95 Mon Sep 17 00:00:00 2001 From: Tan Wang Leng Date: Fri, 24 Jan 2020 23:33:01 +0800 Subject: [PATCH] Fix wrong guideline values shown during dragging Suppose that the user wants to use some guidelines in 2D mode. The user has enabled "Use Pixel Snap", and configured the "Grid Step" to 1px. On some zoom levels, when dragging the guidelines step by step, some offsets shows the wrong value. The offsets that are wrong vary - it is affected by the zoom level, so some zoom levels do not display this problem. For example, a user may see this while dragging the guideline: 0px 1px 1px 3px 4px 5px 5px 7px 8px whereby 2px and 6px are missing. This is due to a floating-point error. The values are printed as (truncated) integers, but they are actually decimals, so they were actually 1.9999 and 5.9999 for the missing cases. Let's fix that by rounding up the values before printing them to get rid of the errors. This fixes #35010. --- editor/plugins/canvas_item_editor_plugin.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/editor/plugins/canvas_item_editor_plugin.cpp b/editor/plugins/canvas_item_editor_plugin.cpp index d3cbf2947ec..1d8f3a2bbd7 100644 --- a/editor/plugins/canvas_item_editor_plugin.cpp +++ b/editor/plugins/canvas_item_editor_plugin.cpp @@ -2599,14 +2599,14 @@ void CanvasItemEditor::_draw_guides() { Color text_color = get_color("font_color", "Editor"); text_color.a = 0.5; if (drag_type == DRAG_DOUBLE_GUIDE || drag_type == DRAG_V_GUIDE) { - String str = vformat("%d px", xform.affine_inverse().xform(dragged_guide_pos).x); + String str = vformat("%d px", Math::round(xform.affine_inverse().xform(dragged_guide_pos).x)); Ref font = get_font("font", "Label"); Size2 text_size = font->get_string_size(str); viewport->draw_string(font, Point2(dragged_guide_pos.x + 10, RULER_WIDTH + text_size.y / 2 + 10), str, text_color); viewport->draw_line(Point2(dragged_guide_pos.x, 0), Point2(dragged_guide_pos.x, viewport->get_size().y), guide_color, Math::round(EDSCALE)); } if (drag_type == DRAG_DOUBLE_GUIDE || drag_type == DRAG_H_GUIDE) { - String str = vformat("%d px", xform.affine_inverse().xform(dragged_guide_pos).y); + String str = vformat("%d px", Math::round(xform.affine_inverse().xform(dragged_guide_pos).y)); Ref font = get_font("font", "Label"); Size2 text_size = font->get_string_size(str); viewport->draw_string(font, Point2(RULER_WIDTH + 10, dragged_guide_pos.y + text_size.y / 2 + 10), str, text_color);