From c67d45375bcd48130a8c22c264a832bd4ffaa441 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 9 Mar 2024 16:27:43 +0100 Subject: [PATCH] Fix LayeredTileMapPattern::get_used_cells(). --- .../editor/layered_tile_map_layer_editor.cpp | 22 ++++++++++++------- modules/layered_tile_map/layered_tile_set.cpp | 7 ++++-- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/modules/layered_tile_map/editor/layered_tile_map_layer_editor.cpp b/modules/layered_tile_map/editor/layered_tile_map_layer_editor.cpp index 67f2f81b7..255359f73 100644 --- a/modules/layered_tile_map/editor/layered_tile_map_layer_editor.cpp +++ b/modules/layered_tile_map/editor/layered_tile_map_layer_editor.cpp @@ -900,8 +900,10 @@ void LayeredTileMapLayerEditorTilesPlugin::forward_canvas_draw_over_viewport(Con PoolVector2iArray selection_used_cells = selection_pattern->get_used_cells(); for (int i = 0; i < selection_used_cells.size(); i++) { - Vector2i coords = tile_set->map_pattern(offset + top_left, selection_used_cells[i], selection_pattern); - preview[coords] = LayeredTileMapCell(selection_pattern->get_cell_source_id(selection_used_cells[i]), selection_pattern->get_cell_atlas_coords(selection_used_cells[i]), selection_pattern->get_cell_alternative_tile(selection_used_cells[i])); + Vector2i cc = selection_used_cells[i]; + Vector2i coords = tile_set->map_pattern(offset + top_left, cc, selection_pattern); + + preview[coords] = LayeredTileMapCell(selection_pattern->get_cell_source_id(cc), selection_pattern->get_cell_atlas_coords(cc), selection_pattern->get_cell_alternative_tile(cc)); } } } else if (drag_type == DRAG_TYPE_CLIPBOARD_PASTE) { @@ -1139,19 +1141,23 @@ HashMap LayeredTileMapLayerEditorTilesPlugin::_dra } else { // Paint the pattern. // If we paint several tiles, we virtually move the mouse as if it was in the center of the "brush" - Vector2 mouse_offset = (Vector2(pattern->get_size()) / 2.0 - Vector2(0.5, 0.5)) * tile_set->get_tile_size(); + Vector2i pattern_size = pattern->get_size(); + Vector2 mouse_offset = (Vector2(pattern_size) / 2.0 - Vector2(0.5, 0.5)) * tile_set->get_tile_size(); Vector2i last_hovered_cell = tile_set->local_to_map(p_from_mouse_pos - mouse_offset); Vector2i new_hovered_cell = tile_set->local_to_map(p_to_mouse_pos - mouse_offset); Vector2i drag_start_cell = tile_set->local_to_map(p_start_drag_mouse_pos - mouse_offset); PoolVector2iArray used_cells = pattern->get_used_cells(); - Vector2i offset = Vector2i(Math::posmod(drag_start_cell.x, pattern->get_size().x), Math::posmod(drag_start_cell.y, pattern->get_size().y)); // Note: no posmodv for Vector2i for now. Meh.s - Vector line = LayeredTileMapLayerEditor::get_line(edited_layer, (last_hovered_cell - offset) / pattern->get_size(), (new_hovered_cell - offset) / pattern->get_size()); + + Vector2i offset = Vector2i(Math::posmod(drag_start_cell.x, pattern_size.x), Math::posmod(drag_start_cell.y, pattern_size.y)); // Note: no posmodv for Vector2i for now. Meh.s + Vector line = LayeredTileMapLayerEditor::get_line(edited_layer, (last_hovered_cell - offset) / pattern_size, (new_hovered_cell - offset) / pattern_size); for (int i = 0; i < line.size(); i++) { - Vector2i top_left = line[i] * pattern->get_size() + offset; + Vector2i top_left = line[i] * pattern_size + offset; for (int j = 0; j < used_cells.size(); j++) { - Vector2i coords = tile_set->map_pattern(top_left, used_cells[j], pattern); - output.insert(coords, LayeredTileMapCell(pattern->get_cell_source_id(used_cells[j]), pattern->get_cell_atlas_coords(used_cells[j]), pattern->get_cell_alternative_tile(used_cells[j]))); + Vector2i cc = used_cells[j]; + Vector2i coords = tile_set->map_pattern(top_left, cc, pattern); + + output.insert(coords, LayeredTileMapCell(pattern->get_cell_source_id(cc), pattern->get_cell_atlas_coords(cc), pattern->get_cell_alternative_tile(cc))); } } } diff --git a/modules/layered_tile_map/layered_tile_set.cpp b/modules/layered_tile_map/layered_tile_set.cpp index 2f0e44c52..41af3de20 100644 --- a/modules/layered_tile_map/layered_tile_set.cpp +++ b/modules/layered_tile_map/layered_tile_set.cpp @@ -152,10 +152,13 @@ PoolVector2iArray LayeredTileMapPattern::get_used_cells() const { // Returns the cells used in the tilemap. PoolVector2iArray a; a.resize(pattern.size()); + PoolVector2iArray::Write w = a.write(); + Vector2i *wptr = w.ptr(); + int i = 0; + for (const HashMap::Element *E = pattern.front(); E; E = E->next) { - Vector2i p(E->key().x, E->key().y); - a[i++] = p; + wptr[i++] = E->key(); } return a;