1
0
Fork 0
mirror of https://github.com/Orama-Interactive/Pixelorama.git synced 2025-01-19 09:39:48 +00:00

Prevent from setting tile indices out of bounds of the canvas

This commit is contained in:
Emmanouil Papadeas 2024-11-25 00:37:33 +02:00
parent 6b95908ef3
commit 64809c64d9
3 changed files with 14 additions and 8 deletions

View file

@ -77,7 +77,7 @@ func update_texture() -> void:
cel.texture_changed.emit() cel.texture_changed.emit()
func on_undo_redo(undo: bool) -> void: func on_undo_redo(_undo: bool) -> void:
pass pass

View file

@ -81,7 +81,7 @@ func update_tileset(undo: bool) -> void:
if undo: if undo:
var tile_removed := tileset.remove_unused_tiles() var tile_removed := tileset.remove_unused_tiles()
if tile_removed: if tile_removed:
re_index_tiles() re_index_all_tiles()
## Cases:[br] ## Cases:[br]
@ -207,21 +207,26 @@ func update_cel_portions() -> void:
func get_tile_coords(portion_position: int) -> Vector2i: func get_tile_coords(portion_position: int) -> Vector2i:
var x_coord := float(tileset.tile_size.x) * (portion_position % indices_x) 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) var y_coord := float(tileset.tile_size.y) * (portion_position / indices_x)
return Vector2i(x_coord, y_coord) return Vector2i(x_coord, y_coord)
func get_tile_position(coords: Vector2i) -> int: func get_tile_position(coords: Vector2i) -> int:
var x := floori(coords.x / tileset.tile_size.x) @warning_ignore("integer_division")
var y := floori(coords.y / tileset.tile_size.y) * indices_x 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 return x + y
func re_index_tiles() -> void: func re_index_all_tiles() -> void:
for i in indices.size(): for i in indices.size():
var x_coord := float(tileset.tile_size.x) * (i % indices_x) var coords := get_tile_coords(i)
var y_coord := float(tileset.tile_size.y) * (i / indices_x) var rect := Rect2i(coords, tileset.tile_size)
var rect := Rect2i(Vector2i(x_coord, y_coord), tileset.tile_size)
var image_portion := image.get_region(rect) var image_portion := image.get_region(rect)
if image_portion.is_invisible(): if image_portion.is_invisible():
indices[i] = 0 indices[i] = 0

View file

@ -306,6 +306,7 @@ func draw_end(pos: Vector2i) -> void:
func draw_tile(pos: Vector2i, tile_index: int) -> void: func draw_tile(pos: Vector2i, tile_index: int) -> void:
if Global.current_project.get_current_cel() is not CelTileMap: if Global.current_project.get_current_cel() is not CelTileMap:
return return
pos = Global.current_project.tiles.get_canon_position(pos)
var tile_position := get_tile_position(pos) var tile_position := get_tile_position(pos)
var cel := Global.current_project.get_current_cel() as CelTileMap var cel := Global.current_project.get_current_cel() as CelTileMap
cel.set_index(tile_position, tile_index) cel.set_index(tile_position, tile_index)