From af86856dc60f4ed6deb0fd76e02b079f31e3d4e9 Mon Sep 17 00:00:00 2001 From: Manolis Papadeas <35376950+OverloadedOrama@users.noreply.github.com> Date: Sun, 5 Dec 2021 18:37:15 +0200 Subject: [PATCH] Added a clipboard pattern button It starts empty and gets updated every time the user copies something. --- src/Tools/Bucket.gd | 17 ++++++++++------- src/UI/Canvas/Selection.gd | 8 ++++++++ src/UI/PatternsPopup.gd | 13 +++++++++---- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/Tools/Bucket.gd b/src/Tools/Bucket.gd index c15dc8aec..98dddf3a1 100644 --- a/src/Tools/Bucket.gd +++ b/src/Tools/Bucket.gd @@ -37,11 +37,11 @@ func _on_FillWithOptions_item_selected(index: int) -> void: save_config() -func _on_PatternType_pressed(): - Global.patterns_popup.connect( - "pattern_selected", self, "_on_Pattern_selected", [], CONNECT_ONESHOT - ) - Global.patterns_popup.popup(Rect2($FillPattern/Type.rect_global_position, Vector2(226, 72))) +func _on_PatternType_pressed() -> void: + var popup: Popup = Global.patterns_popup + if !popup.is_connected("pattern_selected", self, "_on_Pattern_selected"): + popup.connect("pattern_selected", self, "_on_Pattern_selected", [], CONNECT_ONESHOT) + popup.popup(Rect2($FillPattern/Type.rect_global_position, Vector2(226, 72))) func _on_Pattern_selected(pattern: Patterns.Pattern) -> void: @@ -101,7 +101,8 @@ func update_pattern() -> void: else: _pattern = Global.patterns_popup.default_pattern var tex := ImageTexture.new() - tex.create_from_image(_pattern.image, 0) + if !_pattern.image.is_empty(): + tex.create_from_image(_pattern.image, 0) $FillPattern/Type/Texture.texture = tex var size := _pattern.image.get_size() $FillPattern/XOffset/OffsetX.max_value = size.x - 1 @@ -232,8 +233,10 @@ func _set_pixel(image: Image, x: int, y: int, color: Color) -> void: if _fill_with == 0 or _pattern == null: image.set_pixel(x, y, color) else: - _pattern.image.lock() var size := _pattern.image.get_size() + if size.x == 0 or size.y == 0: + return + _pattern.image.lock() var px := int(x + _offset_x) % int(size.x) var py := int(y + _offset_y) % int(size.y) var pc := _pattern.image.get_pixel(px, py) diff --git a/src/UI/Canvas/Selection.gd b/src/UI/Canvas/Selection.gd index 81f099925..db418abcb 100644 --- a/src/UI/Canvas/Selection.gd +++ b/src/UI/Canvas/Selection.gd @@ -672,6 +672,14 @@ func copy() -> void: clipboard.big_bounding_rectangle = big_bounding_rectangle clipboard.selection_offset = project.selection_offset + if !to_copy.is_empty(): + var pattern: Patterns.Pattern = Global.patterns_popup.get_pattern(0) + pattern.image = to_copy + var tex := ImageTexture.new() + tex.create_from_image(to_copy, 0) + var container = Global.patterns_popup.get_node("ScrollContainer/PatternContainer") + container.get_child(0).get_child(0).texture = tex + func paste() -> void: if clipboard.image.is_empty(): diff --git a/src/UI/PatternsPopup.gd b/src/UI/PatternsPopup.gd index 9838a1c99..3c3c61acf 100644 --- a/src/UI/PatternsPopup.gd +++ b/src/UI/PatternsPopup.gd @@ -11,25 +11,30 @@ class Pattern: var index: int +func _ready() -> void: + add(Image.new(), "Clipboard") + + func select_pattern(pattern: Pattern) -> void: emit_signal("pattern_selected", pattern) hide() -static func create_button(image: Image) -> Node: +func create_button(image: Image) -> Node: var button: BaseButton = preload("res://src/UI/PatternButton.tscn").instance() var tex := ImageTexture.new() - tex.create_from_image(image, 0) + if !image.is_empty(): + tex.create_from_image(image, 0) button.get_child(0).texture = tex button.mouse_default_cursor_shape = Control.CURSOR_POINTING_HAND return button -static func add(image: Image, hint := "") -> void: +func add(image: Image, hint := "") -> void: var button = create_button(image) button.pattern.image = image button.hint_tooltip = hint - var container = Global.patterns_popup.get_node("ScrollContainer/PatternContainer") + var container = get_node("ScrollContainer/PatternContainer") container.add_child(button) button.pattern.index = button.get_index()