Skip to content

Commit

Permalink
Create a get_mirrored_positions() method in Tools
Browse files Browse the repository at this point in the history
Reduces some code replication across tools
  • Loading branch information
OverloadedOrama committed Feb 13, 2024
1 parent 47fc659 commit f56d536
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 105 deletions.
19 changes: 19 additions & 0 deletions src/Autoload/Tools.gd
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,25 @@ func get_assigned_color(button: int) -> Color:
return _slots[button].color


func get_mirrored_positions(
pos: Vector2i, project := Global.current_project, offset := 0
) -> Array[Vector2i]:
var positions: Array[Vector2i] = []
if horizontal_mirror:
var mirror_x := pos
mirror_x.x = project.x_symmetry_point - pos.x + offset
positions.append(mirror_x)
if vertical_mirror:
var mirror_xy := mirror_x
mirror_xy.y = project.y_symmetry_point - pos.y + offset
positions.append(mirror_xy)
if vertical_mirror:
var mirror_y := pos
mirror_y.y = project.y_symmetry_point - pos.y + offset
positions.append(mirror_y)
return positions


func set_button_size(button_size: int) -> void:
var size := Vector2(24, 24) if button_size == Global.ButtonSize.SMALL else Vector2(32, 32)
for t in _tool_buttons.get_children():
Expand Down
19 changes: 6 additions & 13 deletions src/Classes/Drawers.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ var pixel_perfect := false:
drawers = pixel_perfect_drawers.duplicate()
else:
drawers = [simple_drawer, simple_drawer, simple_drawer, simple_drawer]
var horizontal_mirror := false
var vertical_mirror := false
var color_op := ColorOp.new()

var simple_drawer := SimpleDrawer.new()
Expand Down Expand Up @@ -72,14 +70,9 @@ func set_pixel(image: Image, position: Vector2i, color: Color, ignore_mirroring
drawers[0].set_pixel(image, position, color, color_op)
if ignore_mirroring:
return

# Handle Mirroring
var mirror_x := project.x_symmetry_point - position.x
var mirror_y := project.y_symmetry_point - position.y

if horizontal_mirror and project.can_pixel_get_drawn(Vector2i(mirror_x, position.y)):
drawers[1].set_pixel(image, Vector2i(mirror_x, position.y), color, color_op)
if vertical_mirror and project.can_pixel_get_drawn(Vector2i(position.x, mirror_y)):
drawers[2].set_pixel(image, Vector2i(mirror_x, mirror_y), color, color_op)
if vertical_mirror and project.can_pixel_get_drawn(Vector2i(position.x, mirror_y)):
drawers[3].set_pixel(image, Vector2i(position.x, mirror_y), color, color_op)
# Handle mirroring
var i := 1
for mirror_pos in Tools.get_mirrored_positions(position, project):
if project.can_pixel_get_drawn(mirror_pos):
drawers[i].set_pixel(image, mirror_pos, color, color_op)
i += 1
2 changes: 0 additions & 2 deletions src/Tools/BaseDraw.gd
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,6 @@ func _prepare_tool() -> void:
lerpf(Tools.brush_size_min, Tools.brush_size_max, Tools.mouse_velocity)
)
_drawer.pixel_perfect = Tools.pixel_perfect if _brush_size == 1 else false
_drawer.horizontal_mirror = Tools.horizontal_mirror
_drawer.vertical_mirror = Tools.vertical_mirror
_drawer.color_op.strength = strength
_indicator = _create_brush_indicator()
_polylines = _create_polylines(_indicator)
Expand Down
17 changes: 4 additions & 13 deletions src/Tools/DesignTools/Bucket.gd
Original file line number Diff line number Diff line change
Expand Up @@ -231,19 +231,10 @@ func fill_in_color(pos: Vector2i) -> void:
func fill_in_area(pos: Vector2i) -> void:
var project := Global.current_project
_flood_fill(pos)

# Handle Mirroring
var mirror_x := project.x_symmetry_point - pos.x
var mirror_y := project.y_symmetry_point - pos.y
var mirror_x_inside := project.can_pixel_get_drawn(Vector2i(mirror_x, pos.y))
var mirror_y_inside := project.can_pixel_get_drawn(Vector2i(pos.x, mirror_y))

if Tools.horizontal_mirror and mirror_x_inside:
_flood_fill(Vector2i(mirror_x, pos.y))
if Tools.vertical_mirror and mirror_y_inside:
_flood_fill(Vector2i(mirror_x, mirror_y))
if Tools.vertical_mirror and mirror_y_inside:
_flood_fill(Vector2i(pos.x, mirror_y))
# Handle mirroring
for mirror_pos in Tools.get_mirrored_positions(pos, project):
if project.can_pixel_get_drawn(mirror_pos):
_flood_fill(mirror_pos)


func fill_in_selection() -> void:
Expand Down
43 changes: 14 additions & 29 deletions src/Tools/SelectionTools/EllipseSelect.gd
Original file line number Diff line number Diff line change
Expand Up @@ -75,35 +75,20 @@ func apply_selection(_position: Vector2i) -> void:
Global.canvas.selection.clear_selection()
if _rect.size == Vector2i.ZERO and Global.current_project.has_selection:
Global.canvas.selection.commit_undo("Select", undo_data)

if _rect.size != Vector2i.ZERO:
set_ellipse(project.selection_map, _rect.position)

# Handle mirroring
if Tools.horizontal_mirror:
var mirror_x_rect := _rect
mirror_x_rect.position.x = (
Global.current_project.x_symmetry_point - _rect.position.x + 1
)
mirror_x_rect.end.x = Global.current_project.x_symmetry_point - _rect.end.x + 1
set_ellipse(project.selection_map, mirror_x_rect.abs().position)
if Tools.vertical_mirror:
var mirror_xy_rect := mirror_x_rect
mirror_xy_rect.position.y = (
Global.current_project.y_symmetry_point - _rect.position.y + 1
)
mirror_xy_rect.end.y = Global.current_project.y_symmetry_point - _rect.end.y + 1
set_ellipse(project.selection_map, mirror_xy_rect.abs().position)
if Tools.vertical_mirror:
var mirror_y_rect := _rect
mirror_y_rect.position.y = (
Global.current_project.y_symmetry_point - _rect.position.y + 1
)
mirror_y_rect.end.y = Global.current_project.y_symmetry_point - _rect.end.y + 1
set_ellipse(project.selection_map, mirror_y_rect.abs().position)

Global.canvas.selection.big_bounding_rectangle = project.selection_map.get_used_rect()
Global.canvas.selection.commit_undo("Select", undo_data)
if _rect.size == Vector2i.ZERO:
return
set_ellipse(project.selection_map, _rect.position)
# Handle mirroring
var mirror_positions := Tools.get_mirrored_positions(_rect.position, project, 1)
var mirror_ends := Tools.get_mirrored_positions(_rect.end, project, 1)
for i in mirror_positions.size():
var mirror_rect := Rect2i()
mirror_rect.position = mirror_positions[i]
mirror_rect.end = mirror_ends[i]
set_ellipse(project.selection_map, mirror_rect.abs().position)

Global.canvas.selection.big_bounding_rectangle = project.selection_map.get_used_rect()
Global.canvas.selection.commit_undo("Select", undo_data)


func set_ellipse(selection_map: SelectionMap, pos: Vector2i) -> void:
Expand Down
16 changes: 3 additions & 13 deletions src/Tools/SelectionTools/MagicWand.gd
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,10 @@ func apply_selection(pos: Vector2i) -> void:
var cel_image := Image.new()
cel_image.copy_from(_get_draw_image())
_flood_fill(pos, cel_image, project.selection_map)

# Handle mirroring
if Tools.horizontal_mirror:
var mirror_x := pos
mirror_x.x = Global.current_project.x_symmetry_point - pos.x
_flood_fill(mirror_x, cel_image, project.selection_map)
if Tools.vertical_mirror:
var mirror_xy := mirror_x
mirror_xy.y = Global.current_project.y_symmetry_point - pos.y
_flood_fill(mirror_xy, cel_image, project.selection_map)
if Tools.vertical_mirror:
var mirror_y := pos
mirror_y.y = Global.current_project.y_symmetry_point - pos.y
_flood_fill(mirror_y, cel_image, project.selection_map)
for mirror_pos in Tools.get_mirrored_positions(pos):
_flood_fill(mirror_pos, cel_image, project.selection_map)

Global.canvas.selection.big_bounding_rectangle = project.selection_map.get_used_rect()
Global.canvas.selection.commit_undo("Select", undo_data)

Expand Down
52 changes: 17 additions & 35 deletions src/Tools/SelectionTools/RectSelect.gd
Original file line number Diff line number Diff line change
Expand Up @@ -50,33 +50,24 @@ func draw_end(pos: Vector2i) -> void:
func draw_preview() -> void:
if _move:
return
var project := Global.current_project
var canvas: Node2D = Global.canvas.previews
var pos := canvas.position
var canvas_scale := canvas.scale
if Global.mirror_view:
pos.x = pos.x + Global.current_project.size.x
pos.x = pos.x + project.size.x
canvas_scale.x = -1
canvas.draw_set_transform(pos, canvas.rotation, canvas_scale)
canvas.draw_rect(_rect, Color.BLACK, false)

# Handle mirroring
if Tools.horizontal_mirror:
var mirror_x_rect := _rect
mirror_x_rect.position.x = Global.current_project.x_symmetry_point - _rect.position.x + 1
mirror_x_rect.end.x = Global.current_project.x_symmetry_point - _rect.end.x + 1
canvas.draw_rect(mirror_x_rect, Color.BLACK, false)
if Tools.vertical_mirror:
var mirror_xy_rect := mirror_x_rect
mirror_xy_rect.position.y = (
Global.current_project.y_symmetry_point - _rect.position.y + 1
)
mirror_xy_rect.end.y = Global.current_project.y_symmetry_point - _rect.end.y + 1
canvas.draw_rect(mirror_xy_rect, Color.BLACK, false)
if Tools.vertical_mirror:
var mirror_y_rect := _rect
mirror_y_rect.position.y = Global.current_project.y_symmetry_point - _rect.position.y + 1
mirror_y_rect.end.y = Global.current_project.y_symmetry_point - _rect.end.y + 1
canvas.draw_rect(mirror_y_rect, Color.BLACK, false)
var mirror_positions := Tools.get_mirrored_positions(_rect.position, project, 1)
var mirror_ends := Tools.get_mirrored_positions(_rect.end, project, 1)
for i in mirror_positions.size():
var mirror_rect := Rect2i()
mirror_rect.position = mirror_positions[i]
mirror_rect.end = mirror_ends[i]
canvas.draw_rect(mirror_rect, Color.BLACK, false)

canvas.draw_set_transform(canvas.position, canvas.rotation, canvas.scale)


Expand All @@ -95,23 +86,14 @@ func apply_selection(pos: Vector2i) -> void:
elif _intersect:
operation = 2
Global.canvas.selection.select_rect(_rect, operation)

# Handle mirroring
if Tools.horizontal_mirror:
var mirror_x_rect := _rect
mirror_x_rect.position.x = project.x_symmetry_point - _rect.position.x + 1
mirror_x_rect.end.x = project.x_symmetry_point - _rect.end.x + 1
Global.canvas.selection.select_rect(mirror_x_rect.abs(), operation)
if Tools.vertical_mirror:
var mirror_xy_rect := mirror_x_rect
mirror_xy_rect.position.y = project.y_symmetry_point - _rect.position.y + 1
mirror_xy_rect.end.y = project.y_symmetry_point - _rect.end.y + 1
Global.canvas.selection.select_rect(mirror_xy_rect.abs(), operation)
if Tools.vertical_mirror:
var mirror_y_rect := _rect
mirror_y_rect.position.y = project.y_symmetry_point - _rect.position.y + 1
mirror_y_rect.end.y = project.y_symmetry_point - _rect.end.y + 1
Global.canvas.selection.select_rect(mirror_y_rect.abs(), operation)
var mirror_positions := Tools.get_mirrored_positions(_rect.position, project, 1)
var mirror_ends := Tools.get_mirrored_positions(_rect.end, project, 1)
for i in mirror_positions.size():
var mirror_rect := Rect2i()
mirror_rect.position = mirror_positions[i]
mirror_rect.end = mirror_ends[i]
Global.canvas.selection.select_rect(mirror_rect.abs(), operation)

Global.canvas.selection.commit_undo("Select", undo_data)

Expand Down

0 comments on commit f56d536

Please sign in to comment.