From 97e9165de47301dbb96421a29d8eb492b46998e1 Mon Sep 17 00:00:00 2001 From: kleonc <9283098+kleonc@users.noreply.github.com> Date: Tue, 23 Nov 2021 14:49:37 +0100 Subject: [PATCH] TileSetEditor Fix selecting next/previous subtile --- editor/plugins/tile_set_editor_plugin.cpp | 38 +++++++++++------------ editor/plugins/tile_set_editor_plugin.h | 1 + 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/editor/plugins/tile_set_editor_plugin.cpp b/editor/plugins/tile_set_editor_plugin.cpp index cab2d9983df..1e2542a5465 100644 --- a/editor/plugins/tile_set_editor_plugin.cpp +++ b/editor/plugins/tile_set_editor_plugin.cpp @@ -1227,12 +1227,10 @@ void TileSetEditor::_on_scroll_container_input(const Ref &p_event) { // to allow performing this action anywhere, even if the cursor isn't // hovering the texture in the workspace. if (mb->get_button_index() == BUTTON_WHEEL_UP && mb->is_pressed() && mb->get_control()) { - print_line("zooming in"); _zoom_in(); // Don't scroll up after zooming in. accept_event(); } else if (mb->get_button_index() == BUTTON_WHEEL_DOWN && mb->is_pressed() && mb->get_control()) { - print_line("zooming out"); _zoom_out(); // Don't scroll down after zooming out. accept_event(); @@ -2027,9 +2025,7 @@ void TileSetEditor::_update_tile_data() { data.occlusion_shape = tileset->tile_get_light_occluder(get_current_tile()); current_tile_data[Vector2i()] = data; } else { - int spacing = tileset->autotile_get_spacing(get_current_tile()); - Vector2 size = tileset->tile_get_region(get_current_tile()).size; - Vector2 cell_count = (size / (tileset->autotile_get_size(get_current_tile()) + Vector2(spacing, spacing))).floor(); + Vector2 cell_count = _get_subtiles_count(get_current_tile()); for (int y = 0; y < cell_count.y; y++) { for (int x = 0; x < cell_count.x; x++) { SubtileData data; @@ -2129,10 +2125,7 @@ void TileSetEditor::_select_previous_tile() { case EDITMODE_NAVIGATION: case EDITMODE_PRIORITY: case EDITMODE_Z_INDEX: { - int spacing = tileset->autotile_get_spacing(get_current_tile()); - Vector2 size = tileset->tile_get_region(get_current_tile()).size; - Vector2 cell_count = (size / (tileset->autotile_get_size(get_current_tile()) + Vector2(spacing, spacing))).floor(); - edited_shape_coord = cell_count; + edited_shape_coord = _get_subtiles_count(get_current_tile()) - Vector2(1, 1); _select_edited_shape_coord(); } break; default: { @@ -2175,6 +2168,17 @@ bool TileSetEditor::_sort_tiles(Variant p_a, Variant p_b) { } } +Vector2 TileSetEditor::_get_subtiles_count(int p_tile_id) { + const int spacing = tileset->autotile_get_spacing(p_tile_id); + const Vector2 region_size = tileset->tile_get_region(p_tile_id).size; + const Vector2 subtile_size = tileset->autotile_get_size(p_tile_id); + // In case of not perfect fit the last row/column is allowed to exceed the tile region. + // The return value is the biggest integer-only `(m, n)` satisfying the formula: + // (m, n) * subtile_size + (m - 1, n - 1) * spacing < region_size + subtile_size + Vector2 mn = Vector2(1, 1) + (region_size / (subtile_size + Vector2(spacing, spacing))); + return mn == mn.floor() ? mn.floor() - Vector2(1, 1) : mn.floor(); +} + void TileSetEditor::_select_next_subtile() { if (get_current_tile() == -1) { _select_next_tile(); @@ -2185,14 +2189,12 @@ void TileSetEditor::_select_next_subtile() { } else if (edit_mode == EDITMODE_REGION || edit_mode == EDITMODE_BITMASK || edit_mode == EDITMODE_ICON) { _select_next_tile(); } else { - int spacing = tileset->autotile_get_spacing(get_current_tile()); - Vector2 size = tileset->tile_get_region(get_current_tile()).size; - Vector2 cell_count = (size / (tileset->autotile_get_size(get_current_tile()) + Vector2(spacing, spacing))).floor(); - if (edited_shape_coord.x > cell_count.x - 1 && edited_shape_coord.y > cell_count.y - 1) { + Vector2 cell_count = _get_subtiles_count(get_current_tile()); + if (edited_shape_coord.x >= cell_count.x - 1 && edited_shape_coord.y >= cell_count.y - 1) { _select_next_tile(); } else { edited_shape_coord.x++; - if (edited_shape_coord.x > cell_count.x) { + if (edited_shape_coord.x > cell_count.x - 1) { edited_shape_coord.x = 0; edited_shape_coord.y++; } @@ -2211,15 +2213,13 @@ void TileSetEditor::_select_previous_subtile() { } else if (edit_mode == EDITMODE_REGION || edit_mode == EDITMODE_BITMASK || edit_mode == EDITMODE_ICON) { _select_previous_tile(); } else { - int spacing = tileset->autotile_get_spacing(get_current_tile()); - Vector2 size = tileset->tile_get_region(get_current_tile()).size; - Vector2 cell_count = (size / (tileset->autotile_get_size(get_current_tile()) + Vector2(spacing, spacing))).floor(); + Vector2 cell_count = _get_subtiles_count(get_current_tile()); if (edited_shape_coord.x <= 0 && edited_shape_coord.y <= 0) { _select_previous_tile(); } else { edited_shape_coord.x--; - if (edited_shape_coord.x == -1) { - edited_shape_coord.x = cell_count.x; + if (edited_shape_coord.x < 0) { + edited_shape_coord.x = cell_count.x - 1; edited_shape_coord.y--; } _select_edited_shape_coord(); diff --git a/editor/plugins/tile_set_editor_plugin.h b/editor/plugins/tile_set_editor_plugin.h index 5798c22f91e..f4c47c2f1c7 100644 --- a/editor/plugins/tile_set_editor_plugin.h +++ b/editor/plugins/tile_set_editor_plugin.h @@ -213,6 +213,7 @@ private: void _select_previous_tile(); Array _get_tiles_in_current_texture(bool sorted = false); bool _sort_tiles(Variant p_a, Variant p_b); + Vector2 _get_subtiles_count(int p_tile_id); void _select_next_subtile(); void _select_previous_subtile(); void _select_next_shape();