From 07fe676f4f655ffe6d6f66d1afe243ac25770fba Mon Sep 17 00:00:00 2001 From: Variable <77773850+Variable-ind@users.noreply.github.com> Date: Mon, 20 Nov 2023 17:43:40 +0500 Subject: [PATCH] Exposed get_enclosed_image() to API (#944) * Exposed get_enclosed_image() to API * minor adjustments * did the suggested changes --- src/Autoload/ExtensionsApi.gd | 13 +++++--- src/UI/Canvas/Selection.gd | 62 ++++++++++++++++------------------- 2 files changed, 37 insertions(+), 38 deletions(-) diff --git a/src/Autoload/ExtensionsApi.gd b/src/Autoload/ExtensionsApi.gd index 6c1fcfced..1c6e381eb 100644 --- a/src/Autoload/ExtensionsApi.gd +++ b/src/Autoload/ExtensionsApi.gd @@ -422,7 +422,7 @@ class ToolAPI: ## Gives access to pixelorama's selection system. class SelectionAPI: - ## Clears the selection Gizmo. + ## Clears the selection. func clear_selection() -> void: Global.canvas.selection.clear_selection(true) @@ -471,7 +471,7 @@ class SelectionAPI: if not transform_standby and with_content: Global.canvas.selection.transform_content_confirm() - ## Inverts the selection gizmo. + ## Inverts the selection. func invert() -> void: Global.canvas.selection.invert() @@ -479,6 +479,11 @@ class SelectionAPI: func make_brush() -> void: Global.canvas.selection.new_brush() + ## Returns the portion of current cel's image enclosed by the selection. + ## It's similar to [method make_brush] but it returns the image instead. + func get_enclosed_image() -> Image: + return Global.canvas.selection.get_enclosed_image() + ## Copies the selection content (works in or between pixelorama instances only). func copy() -> void: Global.canvas.selection.copy() @@ -488,8 +493,8 @@ class SelectionAPI: Global.canvas.selection.paste(in_place) ## Deletes the drawing on current cel enclosed within the selection's area. - func delete_content() -> void: - Global.canvas.selection.delete() + func delete_content(selected_cels := true) -> void: + Global.canvas.selection.delete(selected_cels) ## Gives access to basic project manipulation functions. diff --git a/src/UI/Canvas/Selection.gd b/src/UI/Canvas/Selection.gd index f7d2d5077..31fc7e39b 100644 --- a/src/UI/Canvas/Selection.gd +++ b/src/UI/Canvas/Selection.gd @@ -645,6 +645,24 @@ func _get_selected_draw_images() -> Array[Image]: return images +## Returns the portion of current cel's image enclosed by the selection. +func get_enclosed_image() -> Image: + var project := Global.current_project + if !project.has_selection: + return + + var image := project.get_current_cel().get_image() + var enclosed_img := Image.new() + if is_moving_content: + enclosed_img.copy_from(preview_image) + var selection_map_copy := SelectionMap.new() + selection_map_copy.copy_from(project.selection_map) + selection_map_copy.move_bitmap_values(project, false) + else: + enclosed_img = _get_selected_image(image) + return enclosed_img + + func cut() -> void: var project := Global.current_project if !project.layers[project.current_layer].can_layer_get_drawn(): @@ -653,6 +671,7 @@ func cut() -> void: delete(false) +## Copies the selection content (works in or between pixelorama instances only). func copy() -> void: var project := Global.current_project var cl_image := Image.new() @@ -709,6 +728,7 @@ func copy() -> void: container.get_child(0).get_child(0).texture = tex +## Pastes the selection content. func paste(in_place := false) -> void: if !FileAccess.file_exists(CLIPBOARD_FILE_PATH): return @@ -769,6 +789,7 @@ func paste(in_place := false) -> void: project.selection_map_changed() +## Deletes the drawing enclosed within the selection's area. func delete(selected_cels := true) -> void: var project := Global.current_project if !project.layers[project.current_layer].can_layer_get_drawn(): @@ -806,45 +827,16 @@ func delete(selected_cels := true) -> void: commit_undo("Draw", undo_data_tmp) +## Makes a project brush out of the current selection's content. func new_brush() -> void: - var project := Global.current_project - if !project.has_selection: - return - - var image := project.get_current_cel().get_image() - var brush := Image.new() - if is_moving_content: - brush.copy_from(preview_image) - var selection_map_copy := SelectionMap.new() - selection_map_copy.copy_from(project.selection_map) - selection_map_copy.move_bitmap_values(project, false) - var clipboard = str_to_var(DisplayServer.clipboard_get()) - if typeof(clipboard) == TYPE_DICTIONARY: # A sanity check - if not clipboard.has_all( - ["image", "selection_map", "big_bounding_rectangle", "selection_offset"] - ): - return - clipboard.selection_map = selection_map_copy - else: - brush = image.get_region(big_bounding_rectangle) - # Remove unincluded pixels if the selection is not a single rectangle - for x in brush.get_size().x: - for y in brush.get_size().y: - var pos := Vector2i(x, y) - var offset_pos := big_bounding_rectangle.position - if offset_pos.x < 0: - offset_pos.x = 0 - if offset_pos.y < 0: - offset_pos.y = 0 - if not project.selection_map.is_pixel_selected(pos + offset_pos): - brush.set_pixelv(pos, Color(0)) - - if !brush.is_invisible(): + var brush = get_enclosed_image() + if brush and !brush.is_invisible(): var brush_used: Image = brush.get_region(brush.get_used_rect()) - project.brushes.append(brush_used) + Global.current_project.brushes.append(brush_used) Brushes.add_project_brush(brush_used) +## Select the entire region of current cel. func select_all() -> void: var undo_data_tmp := get_undo_data(false) clear_selection() @@ -853,6 +845,7 @@ func select_all() -> void: commit_undo("Select", undo_data_tmp) +## Inverts the selection. func invert() -> void: transform_content_confirm() var project := Global.current_project @@ -865,6 +858,7 @@ func invert() -> void: commit_undo("Select", undo_data_tmp) +## Clears the selection. func clear_selection(use_undo := false) -> void: var project := Global.current_project if !project.has_selection: