diff --git a/src/Tools/BaseShapeDrawer.gd b/src/Tools/BaseShapeDrawer.gd index 9544c7438..9d8faa993 100644 --- a/src/Tools/BaseShapeDrawer.gd +++ b/src/Tools/BaseShapeDrawer.gd @@ -168,18 +168,9 @@ func draw_preview() -> void: if Rect2i(Vector2i.ZERO, image.get_size()).has_point(points[i]): image.set_pixelv(points[i], Color.WHITE) # Handle mirroring - if Tools.horizontal_mirror: - for point in mirror_array(points, true, false): - if Rect2i(Vector2i.ZERO, image.get_size()).has_point(point): - image.set_pixelv(point, Color.WHITE) - if Tools.vertical_mirror: - for point in mirror_array(points, true, true): - if Rect2i(Vector2i.ZERO, image.get_size()).has_point(point): - image.set_pixelv(point, Color.WHITE) - if Tools.vertical_mirror: - for point in mirror_array(points, false, true): - if Rect2i(Vector2i.ZERO, image.get_size()).has_point(point): - image.set_pixelv(point, Color.WHITE) + for point in mirror_array(points, Tools.horizontal_mirror, Tools.vertical_mirror): + if Rect2i(Vector2i.ZERO, image.get_size()).has_point(point): + image.set_pixelv(point, Color.WHITE) var texture := ImageTexture.create_from_image(image) canvas.texture = texture else: diff --git a/src/Tools/BaseTool.gd b/src/Tools/BaseTool.gd index 75eb53106..4a8397477 100644 --- a/src/Tools/BaseTool.gd +++ b/src/Tools/BaseTool.gd @@ -205,18 +205,36 @@ func snap_position(pos: Vector2) -> Vector2: return pos -func mirror_array(array: Array[Vector2i], h: bool, v: bool) -> Array[Vector2i]: +## Returns an array that mirrors each point of the [param array], based on [param h] and [param v]. +## An optional [param callable] can be passed, which gets called for each type of symmetry. +func mirror_array( + array: Array[Vector2i], h: bool, v: bool, callable := func(_array): pass +) -> Array[Vector2i]: var new_array: Array[Vector2i] = [] var project := Global.current_project - for point in array: - if h and v: - new_array.append( + if h and v: + var hv_array: Array[Vector2i] = [] + for point in array: + hv_array.append( Vector2i(project.x_symmetry_point - point.x, project.y_symmetry_point - point.y) ) - elif h: - new_array.append(Vector2i(project.x_symmetry_point - point.x, point.y)) - elif v: - new_array.append(Vector2i(point.x, project.y_symmetry_point - point.y)) + if callable.is_valid(): + callable.call(hv_array) + new_array += hv_array + if h: + var h_array: Array[Vector2i] = [] + for point in array: + h_array.append(Vector2i(project.x_symmetry_point - point.x, point.y)) + if callable.is_valid(): + callable.call(h_array) + new_array += h_array + if v: + var v_array: Array[Vector2i] = [] + for point in array: + v_array.append(Vector2i(point.x, project.y_symmetry_point - point.y)) + if callable.is_valid(): + callable.call(v_array) + new_array += v_array return new_array diff --git a/src/Tools/DesignTools/CurveTool.gd b/src/Tools/DesignTools/CurveTool.gd index c02a2bb06..0c6634db1 100644 --- a/src/Tools/DesignTools/CurveTool.gd +++ b/src/Tools/DesignTools/CurveTool.gd @@ -141,18 +141,9 @@ func draw_preview() -> void: image.set_pixelv(points[i], Color.WHITE) # Handle mirroring - if Tools.horizontal_mirror: - for point in mirror_array(points, true, false): - if Rect2i(Vector2i.ZERO, image.get_size()).has_point(point): - image.set_pixelv(point, Color.WHITE) - if Tools.vertical_mirror: - for point in mirror_array(points, true, true): - if Rect2i(Vector2i.ZERO, image.get_size()).has_point(point): - image.set_pixelv(point, Color.WHITE) - if Tools.vertical_mirror: - for point in mirror_array(points, false, true): - if Rect2i(Vector2i.ZERO, image.get_size()).has_point(point): - image.set_pixelv(point, Color.WHITE) + for point in mirror_array(points, Tools.horizontal_mirror, Tools.vertical_mirror): + if Rect2i(Vector2i.ZERO, image.get_size()).has_point(point): + image.set_pixelv(point, Color.WHITE) var texture := ImageTexture.create_from_image(image) previews.texture = texture diff --git a/src/Tools/DesignTools/LineTool.gd b/src/Tools/DesignTools/LineTool.gd index c591a0fe0..163984905 100644 --- a/src/Tools/DesignTools/LineTool.gd +++ b/src/Tools/DesignTools/LineTool.gd @@ -157,18 +157,9 @@ func draw_preview() -> void: if Rect2i(Vector2i.ZERO, image.get_size()).has_point(point): image.set_pixelv(point, Color.WHITE) # Handle mirroring - if Tools.horizontal_mirror: - for point in mirror_array(points, true, false): - if Rect2i(Vector2i.ZERO, image.get_size()).has_point(point): - image.set_pixelv(point, Color.WHITE) - if Tools.vertical_mirror: - for point in mirror_array(points, true, true): - if Rect2i(Vector2i.ZERO, image.get_size()).has_point(point): - image.set_pixelv(point, Color.WHITE) - if Tools.vertical_mirror: - for point in mirror_array(points, false, true): - if Rect2i(Vector2i.ZERO, image.get_size()).has_point(point): - image.set_pixelv(point, Color.WHITE) + for point in mirror_array(points, Tools.horizontal_mirror, Tools.vertical_mirror): + if Rect2i(Vector2i.ZERO, image.get_size()).has_point(point): + image.set_pixelv(point, Color.WHITE) var texture := ImageTexture.create_from_image(image) canvas.texture = texture else: diff --git a/src/Tools/SelectionTools/EllipseSelect.gd b/src/Tools/SelectionTools/EllipseSelect.gd index 66906d5e4..797e5cfe2 100644 --- a/src/Tools/SelectionTools/EllipseSelect.gd +++ b/src/Tools/SelectionTools/EllipseSelect.gd @@ -63,18 +63,9 @@ func draw_preview() -> void: if Rect2i(Vector2i.ZERO, image.get_size()).has_point(points[i]): image.set_pixelv(points[i], Color.WHITE) # Handle mirroring - if Tools.horizontal_mirror: - for point in mirror_array(points, true, false): - if Rect2i(Vector2i.ZERO, image.get_size()).has_point(point): - image.set_pixelv(point, Color.WHITE) - if Tools.vertical_mirror: - for point in mirror_array(points, true, true): - if Rect2i(Vector2i.ZERO, image.get_size()).has_point(point): - image.set_pixelv(point, Color.WHITE) - if Tools.vertical_mirror: - for point in mirror_array(points, false, true): - if Rect2i(Vector2i.ZERO, image.get_size()).has_point(point): - image.set_pixelv(point, Color.WHITE) + for point in mirror_array(points, Tools.horizontal_mirror, Tools.vertical_mirror): + if Rect2i(Vector2i.ZERO, image.get_size()).has_point(point): + image.set_pixelv(point, Color.WHITE) var texture := ImageTexture.create_from_image(image) canvas.texture = texture else: diff --git a/src/Tools/SelectionTools/Lasso.gd b/src/Tools/SelectionTools/Lasso.gd index a03dd4c72..bcf2b88ac 100644 --- a/src/Tools/SelectionTools/Lasso.gd +++ b/src/Tools/SelectionTools/Lasso.gd @@ -46,27 +46,12 @@ func draw_preview() -> void: image.set_pixelv(draw_point, Color.WHITE) # Handle mirroring - if Tools.horizontal_mirror: - for point in mirror_array(_draw_points, true, false): - var draw_point := point - if Global.mirror_view: # This fixes previewing in mirror mode - draw_point.x = image.get_width() - draw_point.x - 1 - if Rect2i(Vector2i.ZERO, image.get_size()).has_point(draw_point): - image.set_pixelv(draw_point, Color.WHITE) - if Tools.vertical_mirror: - for point in mirror_array(_draw_points, true, true): - var draw_point := point - if Global.mirror_view: # This fixes previewing in mirror mode - draw_point.x = image.get_width() - draw_point.x - 1 - if Rect2i(Vector2i.ZERO, image.get_size()).has_point(draw_point): - image.set_pixelv(draw_point, Color.WHITE) - if Tools.vertical_mirror: - for point in mirror_array(_draw_points, false, true): - var draw_point := point - if Global.mirror_view: # This fixes previewing in mirror mode - draw_point.x = image.get_width() - draw_point.x - 1 - if Rect2i(Vector2i.ZERO, image.get_size()).has_point(draw_point): - image.set_pixelv(draw_point, Color.WHITE) + for point in mirror_array(_draw_points, Tools.horizontal_mirror, Tools.vertical_mirror): + var draw_point := point + if Global.mirror_view: # This fixes previewing in mirror mode + draw_point.x = image.get_width() - draw_point.x - 1 + if Rect2i(Vector2i.ZERO, image.get_size()).has_point(draw_point): + image.set_pixelv(draw_point, Color.WHITE) var texture := ImageTexture.create_from_image(image) canvas.texture = texture else: @@ -85,19 +70,10 @@ func apply_selection(_position) -> void: if _draw_points.size() > 3: if _intersect: project.selection_map.clear() - lasso_selection(project.selection_map, previous_selection_map, _draw_points) - + lasso_selection(_draw_points, project.selection_map, previous_selection_map) # Handle mirroring - if Tools.horizontal_mirror: - var mirror_x := mirror_array(_draw_points, true, false) - lasso_selection(project.selection_map, previous_selection_map, mirror_x) - if Tools.vertical_mirror: - var mirror_xy := mirror_array(_draw_points, true, true) - lasso_selection(project.selection_map, previous_selection_map, mirror_xy) - if Tools.vertical_mirror: - var mirror_y := mirror_array(_draw_points, false, true) - lasso_selection(project.selection_map, previous_selection_map, mirror_y) - + var callable := lasso_selection.bind(project.selection_map, previous_selection_map) + mirror_array(_draw_points, Tools.horizontal_mirror, Tools.vertical_mirror, callable) Global.canvas.selection.big_bounding_rectangle = project.selection_map.get_used_rect() else: if !cleared: @@ -109,7 +85,7 @@ func apply_selection(_position) -> void: func lasso_selection( - selection_map: SelectionMap, previous_selection_map: SelectionMap, points: Array[Vector2i] + points: Array[Vector2i], selection_map: SelectionMap, previous_selection_map: SelectionMap ) -> void: var selection_size := selection_map.get_size() var bounding_rect := Rect2i(points[0], Vector2i.ZERO) diff --git a/src/Tools/SelectionTools/PaintSelect.gd b/src/Tools/SelectionTools/PaintSelect.gd index 1a36972f7..cb7787334 100644 --- a/src/Tools/SelectionTools/PaintSelect.gd +++ b/src/Tools/SelectionTools/PaintSelect.gd @@ -74,27 +74,12 @@ func draw_preview() -> void: image.set_pixelv(draw_point, Color.WHITE) # Handle mirroring - if Tools.horizontal_mirror: - for point in mirror_array(_draw_points, true, false): - var draw_point := point - if Global.mirror_view: # This fixes previewing in mirror mode - draw_point.x = image.get_width() - draw_point.x - 1 - if Rect2i(Vector2i.ZERO, image.get_size()).has_point(draw_point): - image.set_pixelv(draw_point, Color.WHITE) - if Tools.vertical_mirror: - for point in mirror_array(_draw_points, true, true): - var draw_point := point - if Global.mirror_view: # This fixes previewing in mirror mode - draw_point.x = image.get_width() - draw_point.x - 1 - if Rect2i(Vector2i.ZERO, image.get_size()).has_point(draw_point): - image.set_pixelv(draw_point, Color.WHITE) - if Tools.vertical_mirror: - for point in mirror_array(_draw_points, false, true): - var draw_point := point - if Global.mirror_view: # This fixes previewing in mirror mode - draw_point.x = image.get_width() - draw_point.x - 1 - if Rect2i(Vector2i.ZERO, image.get_size()).has_point(draw_point): - image.set_pixelv(draw_point, Color.WHITE) + for point in mirror_array(_draw_points, Tools.horizontal_mirror, Tools.vertical_mirror): + var draw_point := point + if Global.mirror_view: # This fixes previewing in mirror mode + draw_point.x = image.get_width() - draw_point.x - 1 + if Rect2i(Vector2i.ZERO, image.get_size()).has_point(draw_point): + image.set_pixelv(draw_point, Color.WHITE) var texture := ImageTexture.create_from_image(image) canvas.texture = texture else: @@ -115,18 +100,9 @@ func apply_selection(pos: Vector2i) -> void: if _intersect: project.selection_map.clear() paint_selection(project.selection_map, previous_selection_map, _draw_points) - # Handle mirroring - if Tools.horizontal_mirror: - var mirror_x := mirror_array(_draw_points, true, false) - paint_selection(project.selection_map, previous_selection_map, mirror_x) - if Tools.vertical_mirror: - var mirror_xy := mirror_array(_draw_points, true, true) - paint_selection(project.selection_map, previous_selection_map, mirror_xy) - if Tools.vertical_mirror: - var mirror_y := mirror_array(_draw_points, false, true) - paint_selection(project.selection_map, previous_selection_map, mirror_y) - + var mirror := mirror_array(_draw_points, Tools.horizontal_mirror, Tools.vertical_mirror) + paint_selection(project.selection_map, previous_selection_map, mirror) Global.canvas.selection.big_bounding_rectangle = project.selection_map.get_used_rect() else: if !cleared: diff --git a/src/Tools/SelectionTools/PolygonSelect.gd b/src/Tools/SelectionTools/PolygonSelect.gd index 291a8cfd9..9758d16e9 100644 --- a/src/Tools/SelectionTools/PolygonSelect.gd +++ b/src/Tools/SelectionTools/PolygonSelect.gd @@ -81,27 +81,14 @@ func draw_preview() -> void: ) # Handle mirroring - if Tools.horizontal_mirror: - for point in mirror_array(preview_draw_points, true, false): - var draw_point := point - if Global.mirror_view: # This fixes previewing in mirror mode - draw_point.x = image.get_width() - draw_point.x - 1 - if Rect2i(Vector2i.ZERO, image.get_size()).has_point(draw_point): - image.set_pixelv(draw_point, Color.WHITE) - if Tools.vertical_mirror: - for point in mirror_array(preview_draw_points, true, true): - var draw_point := point - if Global.mirror_view: # This fixes previewing in mirror mode - draw_point.x = image.get_width() - draw_point.x - 1 - if Rect2i(Vector2i.ZERO, image.get_size()).has_point(draw_point): - image.set_pixelv(draw_point, Color.WHITE) - if Tools.vertical_mirror: - for point in mirror_array(preview_draw_points, false, true): - var draw_point := point - if Global.mirror_view: # This fixes previewing in mirror mode - draw_point.x = image.get_width() - draw_point.x - 1 - if Rect2i(Vector2i.ZERO, image.get_size()).has_point(draw_point): - image.set_pixelv(draw_point, Color.WHITE) + for point in mirror_array( + preview_draw_points, Tools.horizontal_mirror, Tools.vertical_mirror + ): + var draw_point := point + if Global.mirror_view: # This fixes previewing in mirror mode + draw_point.x = image.get_width() - draw_point.x - 1 + if Rect2i(Vector2i.ZERO, image.get_size()).has_point(draw_point): + image.set_pixelv(draw_point, Color.WHITE) var texture := ImageTexture.create_from_image(image) previews.texture = texture else: @@ -122,19 +109,10 @@ func apply_selection(pos: Vector2i) -> void: if _draw_points.size() > 3: if _intersect: project.selection_map.clear() - lasso_selection(project.selection_map, previous_selection_map, _draw_points) - + lasso_selection(_draw_points, project.selection_map, previous_selection_map) # Handle mirroring - if Tools.horizontal_mirror: - var mirror_x := mirror_array(_draw_points, true, false) - lasso_selection(project.selection_map, previous_selection_map, mirror_x) - if Tools.vertical_mirror: - var mirror_xy := mirror_array(_draw_points, true, true) - lasso_selection(project.selection_map, previous_selection_map, mirror_xy) - if Tools.vertical_mirror: - var mirror_y := mirror_array(_draw_points, false, true) - lasso_selection(project.selection_map, previous_selection_map, mirror_y) - + var callable := lasso_selection.bind(project.selection_map, previous_selection_map) + mirror_array(_draw_points, Tools.horizontal_mirror, Tools.vertical_mirror, callable) Global.canvas.selection.big_bounding_rectangle = project.selection_map.get_used_rect() else: if !cleared: @@ -152,7 +130,7 @@ func _clear() -> void: func lasso_selection( - selection_map: SelectionMap, previous_selection_map: SelectionMap, points: Array[Vector2i] + points: Array[Vector2i], selection_map: SelectionMap, previous_selection_map: SelectionMap ) -> void: var selection_size := selection_map.get_size() var bounding_rect := Rect2i(points[0], Vector2i.ZERO)