From 9a1464f73be540d77102483d86fbb135ba64df9b Mon Sep 17 00:00:00 2001 From: Emmanouil Papadeas <35376950+OverloadedOrama@users.noreply.github.com> Date: Sat, 13 Apr 2024 17:57:51 +0300 Subject: [PATCH] Move selection offset code inside `SelectionMap.is_pixel_selected()` --- src/Classes/Project.gd | 10 +--------- src/Classes/SelectionMap.gd | 8 +++++++- src/Tools/BaseSelectionTool.gd | 18 ++---------------- src/UI/Canvas/Selection.gd | 6 +++--- 4 files changed, 13 insertions(+), 29 deletions(-) diff --git a/src/Classes/Project.gd b/src/Classes/Project.gd index 0143baa36..df03767b9 100644 --- a/src/Classes/Project.gd +++ b/src/Classes/Project.gd @@ -526,11 +526,7 @@ func is_empty() -> bool: ) -func can_pixel_get_drawn( - pixel: Vector2i, - image: SelectionMap = selection_map, - selection_position: Vector2i = Global.canvas.selection.big_bounding_rectangle.position -) -> bool: +func can_pixel_get_drawn(pixel: Vector2i, image := selection_map) -> bool: if pixel.x < 0 or pixel.y < 0 or pixel.x >= size.x or pixel.y >= size.y: return false @@ -538,10 +534,6 @@ func can_pixel_get_drawn( return false if has_selection: - if selection_position.x < 0: - pixel.x -= selection_position.x - if selection_position.y < 0: - pixel.y -= selection_position.y return image.is_pixel_selected(pixel) else: return true diff --git a/src/Classes/SelectionMap.gd b/src/Classes/SelectionMap.gd index 08059b92e..f6292d77a 100644 --- a/src/Classes/SelectionMap.gd +++ b/src/Classes/SelectionMap.gd @@ -4,7 +4,13 @@ extends Image var invert_shader := preload("res://src/Shaders/Effects/Invert.gdshader") -func is_pixel_selected(pixel: Vector2i) -> bool: +func is_pixel_selected(pixel: Vector2i, calculate_offset := true) -> bool: + var selection_position: Vector2i = Global.canvas.selection.big_bounding_rectangle.position + if calculate_offset: + if selection_position.x < 0: + pixel.x -= selection_position.x + if selection_position.y < 0: + pixel.y -= selection_position.y if pixel.x < 0 or pixel.y < 0 or pixel.x >= get_width() or pixel.y >= get_height(): return false var selected: bool = get_pixelv(pixel).a > 0 diff --git a/src/Tools/BaseSelectionTool.gd b/src/Tools/BaseSelectionTool.gd index 4374ca148..c8a9fe287 100644 --- a/src/Tools/BaseSelectionTool.gd +++ b/src/Tools/BaseSelectionTool.gd @@ -84,27 +84,13 @@ func draw_start(pos: Vector2i) -> void: _start_pos = pos _offset = pos - var selection_position: Vector2i = selection_node.big_bounding_rectangle.position - var offsetted_pos := pos - if selection_position.x < 0: - offsetted_pos.x -= selection_position.x - if selection_position.y < 0: - offsetted_pos.y -= selection_position.y - var quick_copy := Input.is_action_pressed("transform_copy_selection_content", true) if ( - offsetted_pos.x >= 0 - and offsetted_pos.y >= 0 - and project.selection_map.is_pixel_selected(offsetted_pos) + project.selection_map.is_pixel_selected(pos) and (!_add and !_subtract and !_intersect or quick_copy) and !_ongoing_selection ): - if !( - Global - . current_project - . layers[Global.current_project.current_layer] - . can_layer_get_drawn() - ): + if not project.layers[project.current_layer].can_layer_get_drawn(): return # Move current selection _move = true diff --git a/src/UI/Canvas/Selection.gd b/src/UI/Canvas/Selection.gd index 607373106..2e63972d8 100644 --- a/src/UI/Canvas/Selection.gd +++ b/src/UI/Canvas/Selection.gd @@ -430,11 +430,11 @@ func select_rect(rect: Rect2i, operation := SelectionOperation.ADD) -> void: if !Rect2i(Vector2i.ZERO, previous_selection_map.get_size()).has_point(pos): continue project.selection_map.select_pixel( - pos, previous_selection_map.is_pixel_selected(pos) + pos, previous_selection_map.is_pixel_selected(pos, false) ) big_bounding_rectangle = project.selection_map.get_used_rect() - if offset_position != Vector2i.ZERO: + if offset_position != Vector2i.ZERO and big_bounding_rectangle.get_area() != 0: big_bounding_rectangle.position += offset_position project.selection_map.move_bitmap_values(project) @@ -685,7 +685,7 @@ func copy() -> void: offset_pos.x = 0 if offset_pos.y < 0: offset_pos.y = 0 - if not project.selection_map.is_pixel_selected(pos + offset_pos): + if not project.selection_map.is_pixel_selected(pos + offset_pos, false): to_copy.set_pixelv(pos, Color(0)) cl_selection_map.copy_from(project.selection_map) cl_big_bounding_rectangle = big_bounding_rectangle