mirror of
https://github.com/Orama-Interactive/Pixelorama.git
synced 2025-01-19 09:39:48 +00:00
Implement placing tiles for pencil tool
Still needs undo support
This commit is contained in:
parent
65895374ab
commit
3e2587e4ca
|
@ -15,6 +15,14 @@ func _init(_tileset: TileSetCustom, _image: ImageExtended, _opacity := 1.0) -> v
|
|||
indices.resize(indices_x * indices_y)
|
||||
|
||||
|
||||
func set_index(tile_position: int, index: int) -> void:
|
||||
index = clampi(index, 0, tileset.tiles.size() - 1)
|
||||
tileset.tiles[index].times_used += 1
|
||||
indices[tile_position] = index
|
||||
update_cel_portion(tile_position)
|
||||
Global.canvas.queue_redraw()
|
||||
|
||||
|
||||
func update_texture() -> void:
|
||||
if TileSetPanel.tile_editing_mode == TileSetPanel.TileEditingMode.MANUAL:
|
||||
for i in indices.size():
|
||||
|
@ -173,24 +181,34 @@ func re_index_tiles_after_index(index: int) -> void:
|
|||
indices[i] -= 1
|
||||
|
||||
|
||||
func update_cel_portions() -> void:
|
||||
for i in indices.size():
|
||||
var coords := get_tile_coords(i)
|
||||
func update_cel_portion(tile_position: int) -> void:
|
||||
var coords := get_tile_coords(tile_position)
|
||||
var rect := Rect2i(coords, tileset.tile_size)
|
||||
var image_portion := image.get_region(rect)
|
||||
var index := indices[i]
|
||||
var index := indices[tile_position]
|
||||
var current_tile := tileset.tiles[index]
|
||||
if image_portion.get_data() != current_tile.image.get_data():
|
||||
var tile_size := current_tile.image.get_size()
|
||||
image.blit_rect(current_tile.image, Rect2i(Vector2i.ZERO, tile_size), coords)
|
||||
|
||||
|
||||
func get_tile_coords(portion_index: int) -> Vector2i:
|
||||
var x_coord := float(tileset.tile_size.x) * (portion_index % indices_x)
|
||||
var y_coord := float(tileset.tile_size.y) * (portion_index / indices_x)
|
||||
func update_cel_portions() -> void:
|
||||
for i in indices.size():
|
||||
update_cel_portion(i)
|
||||
|
||||
|
||||
func get_tile_coords(portion_position: int) -> Vector2i:
|
||||
var x_coord := float(tileset.tile_size.x) * (portion_position % indices_x)
|
||||
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
|
||||
return x + y
|
||||
|
||||
|
||||
## Unused, should delete.
|
||||
func re_index_tiles() -> void:
|
||||
for i in indices.size():
|
||||
|
|
|
@ -303,6 +303,14 @@ func draw_end(pos: Vector2i) -> void:
|
|||
_polylines = _create_polylines(_indicator)
|
||||
|
||||
|
||||
func draw_tile(pos: Vector2i, tile_index: int) -> void:
|
||||
if Global.current_project.get_current_cel() is not CelTileMap:
|
||||
return
|
||||
var tile_position := get_tile_position(pos)
|
||||
var cel := Global.current_project.get_current_cel() as CelTileMap
|
||||
cel.set_index(tile_position, tile_index)
|
||||
|
||||
|
||||
func _prepare_tool() -> void:
|
||||
if !Global.current_project.layers[Global.current_project.current_layer].can_layer_get_drawn():
|
||||
return
|
||||
|
|
|
@ -86,6 +86,19 @@ func update_cels(project := Global.current_project) -> void:
|
|||
cel.tool_finished_drawing()
|
||||
|
||||
|
||||
func is_placing_tiles() -> bool:
|
||||
return Global.current_project.get_current_cel() is CelTileMap and TileSetPanel.placing_tiles
|
||||
|
||||
|
||||
func get_tile_position(pos: Vector2i) -> int:
|
||||
var tile_pos := 0
|
||||
if Global.current_project.get_current_cel() is not CelTileMap:
|
||||
return tile_pos
|
||||
var cel := Global.current_project.get_current_cel() as CelTileMap
|
||||
tile_pos = cel.get_tile_position(pos)
|
||||
return tile_pos
|
||||
|
||||
|
||||
func cursor_move(pos: Vector2i) -> void:
|
||||
_cursor = pos
|
||||
if _spacing_mode and is_moving:
|
||||
|
|
|
@ -96,6 +96,9 @@ func draw_start(pos: Vector2i) -> void:
|
|||
_old_spacing_mode = _spacing_mode
|
||||
pos = snap_position(pos)
|
||||
super.draw_start(pos)
|
||||
if is_placing_tiles():
|
||||
draw_tile(pos, TileSetPanel.selected_tile_index)
|
||||
return
|
||||
if Input.is_action_pressed(&"draw_color_picker", true):
|
||||
_picking_color = true
|
||||
_pick_color(pos)
|
||||
|
@ -138,6 +141,9 @@ func draw_move(pos_i: Vector2i) -> void:
|
|||
var pos := _get_stabilized_position(pos_i)
|
||||
pos = snap_position(pos)
|
||||
super.draw_move(pos)
|
||||
if is_placing_tiles():
|
||||
draw_tile(pos, TileSetPanel.selected_tile_index)
|
||||
return
|
||||
if _picking_color: # Still return even if we released Alt
|
||||
if Input.is_action_pressed(&"draw_color_picker", true):
|
||||
_pick_color(pos)
|
||||
|
@ -164,6 +170,10 @@ func draw_move(pos_i: Vector2i) -> void:
|
|||
|
||||
func draw_end(pos: Vector2i) -> void:
|
||||
pos = snap_position(pos)
|
||||
if is_placing_tiles():
|
||||
super.draw_end(pos)
|
||||
draw_tile(pos, TileSetPanel.selected_tile_index)
|
||||
return
|
||||
if _picking_color:
|
||||
super.draw_end(pos)
|
||||
return
|
||||
|
|
Loading…
Reference in a new issue