From 64809c64d9c29d2113e04a5a1a32ffd6b5cd25cc Mon Sep 17 00:00:00 2001 From: Emmanouil Papadeas <35376950+OverloadedOrama@users.noreply.github.com> Date: Mon, 25 Nov 2024 00:37:33 +0200 Subject: [PATCH] Prevent from setting tile indices out of bounds of the canvas --- src/Classes/Cels/BaseCel.gd | 2 +- src/Classes/Cels/CelTileMap.gd | 19 ++++++++++++------- src/Tools/BaseDraw.gd | 1 + 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/Classes/Cels/BaseCel.gd b/src/Classes/Cels/BaseCel.gd index f5ccc7b73..e9455d338 100644 --- a/src/Classes/Cels/BaseCel.gd +++ b/src/Classes/Cels/BaseCel.gd @@ -77,7 +77,7 @@ func update_texture() -> void: cel.texture_changed.emit() -func on_undo_redo(undo: bool) -> void: +func on_undo_redo(_undo: bool) -> void: pass diff --git a/src/Classes/Cels/CelTileMap.gd b/src/Classes/Cels/CelTileMap.gd index f17a493b3..476bba033 100644 --- a/src/Classes/Cels/CelTileMap.gd +++ b/src/Classes/Cels/CelTileMap.gd @@ -81,7 +81,7 @@ func update_tileset(undo: bool) -> void: if undo: var tile_removed := tileset.remove_unused_tiles() if tile_removed: - re_index_tiles() + re_index_all_tiles() ## Cases:[br] @@ -207,21 +207,26 @@ func update_cel_portions() -> void: func get_tile_coords(portion_position: int) -> Vector2i: var x_coord := float(tileset.tile_size.x) * (portion_position % indices_x) + @warning_ignore("integer_division") var y_coord := float(tileset.tile_size.y) * (portion_position / indices_x) return Vector2i(x_coord, y_coord) func get_tile_position(coords: Vector2i) -> int: - var x := floori(coords.x / tileset.tile_size.x) - var y := floori(coords.y / tileset.tile_size.y) * indices_x + @warning_ignore("integer_division") + var x := coords.x / tileset.tile_size.x + x = clampi(x, 0, indices_x - 1) + @warning_ignore("integer_division") + var y := coords.y / tileset.tile_size.y + y = clampi(y, 0, indices_y - 1) + y *= indices_x return x + y -func re_index_tiles() -> void: +func re_index_all_tiles() -> void: for i in indices.size(): - var x_coord := float(tileset.tile_size.x) * (i % indices_x) - var y_coord := float(tileset.tile_size.y) * (i / indices_x) - var rect := Rect2i(Vector2i(x_coord, y_coord), tileset.tile_size) + var coords := get_tile_coords(i) + var rect := Rect2i(coords, tileset.tile_size) var image_portion := image.get_region(rect) if image_portion.is_invisible(): indices[i] = 0 diff --git a/src/Tools/BaseDraw.gd b/src/Tools/BaseDraw.gd index 0a90e7790..bb9da1a22 100644 --- a/src/Tools/BaseDraw.gd +++ b/src/Tools/BaseDraw.gd @@ -306,6 +306,7 @@ func draw_end(pos: Vector2i) -> void: func draw_tile(pos: Vector2i, tile_index: int) -> void: if Global.current_project.get_current_cel() is not CelTileMap: return + pos = Global.current_project.tiles.get_canon_position(pos) var tile_position := get_tile_position(pos) var cel := Global.current_project.get_current_cel() as CelTileMap cel.set_index(tile_position, tile_index)