1
0
Fork 0
mirror of https://github.com/Orama-Interactive/Pixelorama.git synced 2025-01-18 17:19:50 +00:00

Reduce duplicated code by calling mirror_array by tools less times

This commit is contained in:
Emmanouil Papadeas 2024-11-21 02:15:50 +02:00
parent 2d28136449
commit 3615ce087c
8 changed files with 68 additions and 156 deletions

View file

@ -168,18 +168,9 @@ func draw_preview() -> void:
if Rect2i(Vector2i.ZERO, image.get_size()).has_point(points[i]): if Rect2i(Vector2i.ZERO, image.get_size()).has_point(points[i]):
image.set_pixelv(points[i], Color.WHITE) image.set_pixelv(points[i], Color.WHITE)
# Handle mirroring # Handle mirroring
if Tools.horizontal_mirror: for point in mirror_array(points, Tools.horizontal_mirror, Tools.vertical_mirror):
for point in mirror_array(points, true, false): if Rect2i(Vector2i.ZERO, image.get_size()).has_point(point):
if Rect2i(Vector2i.ZERO, image.get_size()).has_point(point): image.set_pixelv(point, Color.WHITE)
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)
var texture := ImageTexture.create_from_image(image) var texture := ImageTexture.create_from_image(image)
canvas.texture = texture canvas.texture = texture
else: else:

View file

@ -205,18 +205,36 @@ func snap_position(pos: Vector2) -> Vector2:
return pos 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 new_array: Array[Vector2i] = []
var project := Global.current_project var project := Global.current_project
for point in array: if h and v:
if h and v: var hv_array: Array[Vector2i] = []
new_array.append( for point in array:
hv_array.append(
Vector2i(project.x_symmetry_point - point.x, project.y_symmetry_point - point.y) Vector2i(project.x_symmetry_point - point.x, project.y_symmetry_point - point.y)
) )
elif h: if callable.is_valid():
new_array.append(Vector2i(project.x_symmetry_point - point.x, point.y)) callable.call(hv_array)
elif v: new_array += hv_array
new_array.append(Vector2i(point.x, project.y_symmetry_point - point.y)) 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 return new_array

View file

@ -141,18 +141,9 @@ func draw_preview() -> void:
image.set_pixelv(points[i], Color.WHITE) image.set_pixelv(points[i], Color.WHITE)
# Handle mirroring # Handle mirroring
if Tools.horizontal_mirror: for point in mirror_array(points, Tools.horizontal_mirror, Tools.vertical_mirror):
for point in mirror_array(points, true, false): if Rect2i(Vector2i.ZERO, image.get_size()).has_point(point):
if Rect2i(Vector2i.ZERO, image.get_size()).has_point(point): image.set_pixelv(point, Color.WHITE)
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)
var texture := ImageTexture.create_from_image(image) var texture := ImageTexture.create_from_image(image)
previews.texture = texture previews.texture = texture

View file

@ -157,18 +157,9 @@ func draw_preview() -> void:
if Rect2i(Vector2i.ZERO, image.get_size()).has_point(point): if Rect2i(Vector2i.ZERO, image.get_size()).has_point(point):
image.set_pixelv(point, Color.WHITE) image.set_pixelv(point, Color.WHITE)
# Handle mirroring # Handle mirroring
if Tools.horizontal_mirror: for point in mirror_array(points, Tools.horizontal_mirror, Tools.vertical_mirror):
for point in mirror_array(points, true, false): if Rect2i(Vector2i.ZERO, image.get_size()).has_point(point):
if Rect2i(Vector2i.ZERO, image.get_size()).has_point(point): image.set_pixelv(point, Color.WHITE)
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)
var texture := ImageTexture.create_from_image(image) var texture := ImageTexture.create_from_image(image)
canvas.texture = texture canvas.texture = texture
else: else:

View file

@ -63,18 +63,9 @@ func draw_preview() -> void:
if Rect2i(Vector2i.ZERO, image.get_size()).has_point(points[i]): if Rect2i(Vector2i.ZERO, image.get_size()).has_point(points[i]):
image.set_pixelv(points[i], Color.WHITE) image.set_pixelv(points[i], Color.WHITE)
# Handle mirroring # Handle mirroring
if Tools.horizontal_mirror: for point in mirror_array(points, Tools.horizontal_mirror, Tools.vertical_mirror):
for point in mirror_array(points, true, false): if Rect2i(Vector2i.ZERO, image.get_size()).has_point(point):
if Rect2i(Vector2i.ZERO, image.get_size()).has_point(point): image.set_pixelv(point, Color.WHITE)
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)
var texture := ImageTexture.create_from_image(image) var texture := ImageTexture.create_from_image(image)
canvas.texture = texture canvas.texture = texture
else: else:

View file

@ -46,27 +46,12 @@ func draw_preview() -> void:
image.set_pixelv(draw_point, Color.WHITE) image.set_pixelv(draw_point, Color.WHITE)
# Handle mirroring # Handle mirroring
if Tools.horizontal_mirror: for point in mirror_array(_draw_points, Tools.horizontal_mirror, Tools.vertical_mirror):
for point in mirror_array(_draw_points, true, false): var draw_point := point
var draw_point := point if Global.mirror_view: # This fixes previewing in mirror mode
if Global.mirror_view: # This fixes previewing in mirror mode draw_point.x = image.get_width() - draw_point.x - 1
draw_point.x = image.get_width() - draw_point.x - 1 if Rect2i(Vector2i.ZERO, image.get_size()).has_point(draw_point):
if Rect2i(Vector2i.ZERO, image.get_size()).has_point(draw_point): image.set_pixelv(draw_point, Color.WHITE)
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)
var texture := ImageTexture.create_from_image(image) var texture := ImageTexture.create_from_image(image)
canvas.texture = texture canvas.texture = texture
else: else:
@ -85,19 +70,10 @@ func apply_selection(_position) -> void:
if _draw_points.size() > 3: if _draw_points.size() > 3:
if _intersect: if _intersect:
project.selection_map.clear() 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 # Handle mirroring
if Tools.horizontal_mirror: var callable := lasso_selection.bind(project.selection_map, previous_selection_map)
var mirror_x := mirror_array(_draw_points, true, false) mirror_array(_draw_points, Tools.horizontal_mirror, Tools.vertical_mirror, callable)
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)
Global.canvas.selection.big_bounding_rectangle = project.selection_map.get_used_rect() Global.canvas.selection.big_bounding_rectangle = project.selection_map.get_used_rect()
else: else:
if !cleared: if !cleared:
@ -109,7 +85,7 @@ func apply_selection(_position) -> void:
func lasso_selection( func lasso_selection(
selection_map: SelectionMap, previous_selection_map: SelectionMap, points: Array[Vector2i] points: Array[Vector2i], selection_map: SelectionMap, previous_selection_map: SelectionMap
) -> void: ) -> void:
var selection_size := selection_map.get_size() var selection_size := selection_map.get_size()
var bounding_rect := Rect2i(points[0], Vector2i.ZERO) var bounding_rect := Rect2i(points[0], Vector2i.ZERO)

View file

@ -74,27 +74,12 @@ func draw_preview() -> void:
image.set_pixelv(draw_point, Color.WHITE) image.set_pixelv(draw_point, Color.WHITE)
# Handle mirroring # Handle mirroring
if Tools.horizontal_mirror: for point in mirror_array(_draw_points, Tools.horizontal_mirror, Tools.vertical_mirror):
for point in mirror_array(_draw_points, true, false): var draw_point := point
var draw_point := point if Global.mirror_view: # This fixes previewing in mirror mode
if Global.mirror_view: # This fixes previewing in mirror mode draw_point.x = image.get_width() - draw_point.x - 1
draw_point.x = image.get_width() - draw_point.x - 1 if Rect2i(Vector2i.ZERO, image.get_size()).has_point(draw_point):
if Rect2i(Vector2i.ZERO, image.get_size()).has_point(draw_point): image.set_pixelv(draw_point, Color.WHITE)
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)
var texture := ImageTexture.create_from_image(image) var texture := ImageTexture.create_from_image(image)
canvas.texture = texture canvas.texture = texture
else: else:
@ -115,18 +100,9 @@ func apply_selection(pos: Vector2i) -> void:
if _intersect: if _intersect:
project.selection_map.clear() project.selection_map.clear()
paint_selection(project.selection_map, previous_selection_map, _draw_points) paint_selection(project.selection_map, previous_selection_map, _draw_points)
# Handle mirroring # Handle mirroring
if Tools.horizontal_mirror: var mirror := mirror_array(_draw_points, Tools.horizontal_mirror, Tools.vertical_mirror)
var mirror_x := mirror_array(_draw_points, true, false) paint_selection(project.selection_map, previous_selection_map, mirror)
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)
Global.canvas.selection.big_bounding_rectangle = project.selection_map.get_used_rect() Global.canvas.selection.big_bounding_rectangle = project.selection_map.get_used_rect()
else: else:
if !cleared: if !cleared:

View file

@ -81,27 +81,14 @@ func draw_preview() -> void:
) )
# Handle mirroring # Handle mirroring
if Tools.horizontal_mirror: for point in mirror_array(
for point in mirror_array(preview_draw_points, true, false): preview_draw_points, Tools.horizontal_mirror, Tools.vertical_mirror
var draw_point := point ):
if Global.mirror_view: # This fixes previewing in mirror mode var draw_point := point
draw_point.x = image.get_width() - draw_point.x - 1 if Global.mirror_view: # This fixes previewing in mirror mode
if Rect2i(Vector2i.ZERO, image.get_size()).has_point(draw_point): draw_point.x = image.get_width() - draw_point.x - 1
image.set_pixelv(draw_point, Color.WHITE) if Rect2i(Vector2i.ZERO, image.get_size()).has_point(draw_point):
if Tools.vertical_mirror: image.set_pixelv(draw_point, Color.WHITE)
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)
var texture := ImageTexture.create_from_image(image) var texture := ImageTexture.create_from_image(image)
previews.texture = texture previews.texture = texture
else: else:
@ -122,19 +109,10 @@ func apply_selection(pos: Vector2i) -> void:
if _draw_points.size() > 3: if _draw_points.size() > 3:
if _intersect: if _intersect:
project.selection_map.clear() 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 # Handle mirroring
if Tools.horizontal_mirror: var callable := lasso_selection.bind(project.selection_map, previous_selection_map)
var mirror_x := mirror_array(_draw_points, true, false) mirror_array(_draw_points, Tools.horizontal_mirror, Tools.vertical_mirror, callable)
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)
Global.canvas.selection.big_bounding_rectangle = project.selection_map.get_used_rect() Global.canvas.selection.big_bounding_rectangle = project.selection_map.get_used_rect()
else: else:
if !cleared: if !cleared:
@ -152,7 +130,7 @@ func _clear() -> void:
func lasso_selection( func lasso_selection(
selection_map: SelectionMap, previous_selection_map: SelectionMap, points: Array[Vector2i] points: Array[Vector2i], selection_map: SelectionMap, previous_selection_map: SelectionMap
) -> void: ) -> void:
var selection_size := selection_map.get_size() var selection_size := selection_map.get_size()
var bounding_rect := Rect2i(points[0], Vector2i.ZERO) var bounding_rect := Rect2i(points[0], Vector2i.ZERO)