From b954ddce28a1247b92442a65014bad820f1f8be8 Mon Sep 17 00:00:00 2001 From: BT <149745752+btgs-0@users.noreply.github.com> Date: Sat, 18 Jan 2025 14:16:35 +1030 Subject: [PATCH] Draw the mouse position's tilemap co-ordinate (and rectangle size) on the screen when using the TileMapLayer's Terrains plugin. --- .../plugins/tiles/tile_map_layer_editor.cpp | 40 ++++++++++++------- editor/plugins/tiles/tile_map_layer_editor.h | 1 + 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/editor/plugins/tiles/tile_map_layer_editor.cpp b/editor/plugins/tiles/tile_map_layer_editor.cpp index 4f09e738d1..33e914e967 100644 --- a/editor/plugins/tiles/tile_map_layer_editor.cpp +++ b/editor/plugins/tiles/tile_map_layer_editor.cpp @@ -50,6 +50,23 @@ TileMapLayer *TileMapLayerSubEditorPlugin::_get_edited_layer() const { return Object::cast_to(ObjectDB::get_instance(edited_tile_map_layer_id)); } +void TileMapLayerSubEditorPlugin::draw_tile_coords_over_viewport(Control *p_overlay, const TileMapLayer *p_edited_layer, Ref p_tile_set, bool p_show_rectangle_size, const Vector2i &p_rectangle_origin) { + Point2 msgpos = Point2(20 * EDSCALE, p_overlay->get_size().y - 20 * EDSCALE); + String text = p_tile_set->local_to_map(p_edited_layer->get_local_mouse_position()); + + if (p_show_rectangle_size) { + Vector2i rect_size = p_tile_set->local_to_map(p_edited_layer->get_local_mouse_position()) - p_tile_set->local_to_map(p_rectangle_origin); + text += vformat(" %s (%dx%d)", TTR("Drawing Rect:"), Math::abs(rect_size.x) + 1, Math::abs(rect_size.y) + 1); + } + + Ref font = p_overlay->get_theme_font(SceneStringName(font), SNAME("Label")); + int font_size = p_overlay->get_theme_font_size(SceneStringName(font_size), SNAME("Label")); + + p_overlay->draw_string(font, msgpos + Point2(1, 1), text, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, Color(0, 0, 0, 0.8)); + p_overlay->draw_string(font, msgpos + Point2(-1, -1), text, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, Color(0, 0, 0, 0.8)); + p_overlay->draw_string(font, msgpos, text, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, Color(1, 1, 1, 1)); +} + void TileMapLayerEditorTilesPlugin::tile_set_changed() { _update_fix_selected_and_hovered(); _update_tile_set_sources_list(); @@ -66,7 +83,7 @@ void TileMapLayerEditorTilesPlugin::_on_scattering_spinbox_changed(double p_valu } void TileMapLayerEditorTilesPlugin::_update_toolbar() { - // Stop draggig if needed. + // Stop dragging if needed. _stop_dragging(); // Hide all settings. @@ -783,7 +800,7 @@ bool TileMapLayerEditorTilesPlugin::forward_canvas_gui_input(const Ref font = p_overlay->get_theme_font(SceneStringName(font), SNAME("Label")); - int font_size = p_overlay->get_theme_font_size(SceneStringName(font_size), SNAME("Label")); - Point2 msgpos = Point2(20 * EDSCALE, p_overlay->get_size().y - 20 * EDSCALE); - - String text = tile_set->local_to_map(edited_layer->get_local_mouse_position()); - if (drawing_rect) { - Vector2i size = tile_set->local_to_map(edited_layer->get_local_mouse_position()) - tile_set->local_to_map(drag_start_mouse_pos); - text += vformat(" %s (%dx%d)", TTR("Drawing Rect:"), Math::abs(size.x) + 1, Math::abs(size.y) + 1); - } - - p_overlay->draw_string(font, msgpos + Point2(1, 1), text, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, Color(0, 0, 0, 0.8)); - p_overlay->draw_string(font, msgpos + Point2(-1, -1), text, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, Color(0, 0, 0, 0.8)); - p_overlay->draw_string(font, msgpos, text, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, Color(1, 1, 1, 1)); + draw_tile_coords_over_viewport(p_overlay, edited_layer, tile_set, drawing_rect, drag_start_mouse_pos); } } @@ -3168,6 +3173,7 @@ void TileMapLayerEditorTerrainsPlugin::forward_canvas_draw_over_viewport(Control Transform2D xform = CanvasItemEditor::get_singleton()->get_canvas_transform() * edited_layer->get_global_transform_with_canvas(); Vector2 mpos = edited_layer->get_local_mouse_position(); Vector2i tile_shape_size = tile_set->get_tile_size(); + bool drawing_rect = false; // Handle the preview of the tiles to be placed. if (main_vbox_container->is_visible_in_tree() && has_mouse) { // Only if the tilemap editor is opened and the viewport is hovered. @@ -3214,6 +3220,8 @@ void TileMapLayerEditorTerrainsPlugin::forward_canvas_draw_over_viewport(Control preview.insert(Vector2i(x, y)); } } + + drawing_rect = !preview.is_empty(); expand_grid = true; } else if (tool_buttons_group->get_pressed_button() == bucket_tool_button && drag_type == DRAG_TYPE_NONE) { // Preview for a fill. @@ -3272,6 +3280,8 @@ void TileMapLayerEditorTerrainsPlugin::forward_canvas_draw_over_viewport(Control } } } + + draw_tile_coords_over_viewport(p_overlay, edited_layer, tile_set, drawing_rect, drag_start_mouse_pos); } } diff --git a/editor/plugins/tiles/tile_map_layer_editor.h b/editor/plugins/tiles/tile_map_layer_editor.h index aa19ead81b..1abc6890cb 100644 --- a/editor/plugins/tiles/tile_map_layer_editor.h +++ b/editor/plugins/tiles/tile_map_layer_editor.h @@ -67,6 +67,7 @@ public: virtual void forward_canvas_draw_over_viewport(Control *p_overlay) {} virtual void tile_set_changed() {} virtual void edit(ObjectID p_tile_map_layer_id) {} + virtual void draw_tile_coords_over_viewport(Control *p_overlay, const TileMapLayer *p_edited_layer, Ref p_tile_set, bool p_show_rectangle_size, const Vector2i &p_rectangle_origin); }; class TileMapLayerEditorTilesPlugin : public TileMapLayerSubEditorPlugin {