From 1f6998e7239c58ddb5b1da974600253ca483d2ef Mon Sep 17 00:00:00 2001 From: Emmanouil Papadeas <35376950+OverloadedOrama@users.noreply.github.com> Date: Thu, 16 Jan 2025 22:11:02 +0200 Subject: [PATCH] Add support for gradient presets --- src/UI/Nodes/GradientEdit.gd | 36 +++++++++++++++++++++++++++++++--- src/UI/Nodes/GradientEdit.tscn | 12 ++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/UI/Nodes/GradientEdit.gd b/src/UI/Nodes/GradientEdit.gd index 7bc197358..be11f9ef0 100644 --- a/src/UI/Nodes/GradientEdit.gd +++ b/src/UI/Nodes/GradientEdit.gd @@ -19,9 +19,14 @@ var active_cursor: GradientCursor: ## Showing a color picker popup to change a i.queue_redraw() var texture := GradientTexture2D.new() var gradient := Gradient.new() +var presets: Array[Gradient] = [] @onready var x_offset: float = size.x - GradientCursor.WIDTH @onready var offset_value_slider := %OffsetValueSlider as ValueSlider +@onready var interpolation_option_button: OptionButton = %InterpolationOptionButton +@onready var color_space_option_button: OptionButton = %ColorSpaceOptionButton +@onready var tools_menu_button: MenuButton = %ToolsMenuButton +@onready var presets_menu_button: MenuButton = %PresetsMenuButton @onready var texture_rect := $TextureRect as TextureRect @onready var color_picker := $Popup.get_node("ColorPicker") as ColorPicker @onready var divide_dialog := $DivideConfirmationDialog as ConfirmationDialog @@ -131,14 +136,23 @@ class GradientCursor: func _init() -> void: texture.gradient = gradient + presets.append(Gradient.new()) # Left to right + presets.append(Gradient.new()) # Left to transparent + presets.append(Gradient.new()) # Black to white func _ready() -> void: texture_rect.texture = texture _create_cursors() - %InterpolationOptionButton.select(gradient.interpolation_mode) - %ColorSpaceOptionButton.select(gradient.interpolation_color_space) - %ToolsMenuButton.get_popup().index_pressed.connect(_on_tools_menu_button_index_pressed) + interpolation_option_button.select(gradient.interpolation_mode) + color_space_option_button.select(gradient.interpolation_color_space) + tools_menu_button.get_popup().index_pressed.connect(_on_tools_menu_button_index_pressed) + presets_menu_button.get_popup().index_pressed.connect(_on_presets_menu_button_index_pressed) + for preset in presets: + var grad_texture := GradientTexture2D.new() + grad_texture.height = 32 + grad_texture.gradient = preset + presets_menu_button.get_popup().add_icon_item(grad_texture, "") func _create_cursors() -> void: @@ -256,6 +270,22 @@ func _on_tools_menu_button_index_pressed(index: int) -> void: divide_dialog.popup_centered() +func _on_presets_menu_button_about_to_popup() -> void: + # Update left to right and left to transparent gradients + presets[0].set_color(0, Tools.get_assigned_color(MOUSE_BUTTON_LEFT)) + presets[0].set_color(1, Tools.get_assigned_color(MOUSE_BUTTON_RIGHT)) + presets[1].set_color(0, Tools.get_assigned_color(MOUSE_BUTTON_LEFT)) + presets[1].set_color(1, Color(0, 0, 0, 0)) + + +func _on_presets_menu_button_index_pressed(index: int) -> void: + var item_icon := presets_menu_button.get_popup().get_item_icon(index) as GradientTexture2D + gradient = item_icon.gradient.duplicate() + texture.gradient = gradient + _create_cursors() + updated.emit(gradient, continuous_change) + + func _on_DivideConfirmationDialog_confirmed() -> void: var add_point_to_end := add_point_end_check_box.button_pressed var parts := number_of_parts_spin_box.value diff --git a/src/UI/Nodes/GradientEdit.tscn b/src/UI/Nodes/GradientEdit.tscn index 691fbd198..d30382913 100644 --- a/src/UI/Nodes/GradientEdit.tscn +++ b/src/UI/Nodes/GradientEdit.tscn @@ -15,6 +15,7 @@ layout_mode = 2 [node name="OffsetValueSlider" type="TextureProgressBar" parent="InterpolationContainer"] unique_name_in_owner = true +custom_minimum_size = Vector2(64, 0) layout_mode = 2 size_flags_horizontal = 3 focus_mode = 2 @@ -38,6 +39,7 @@ size_flags_horizontal = 3 tooltip_text = "Interpolation" mouse_default_cursor_shape = 2 selected = 0 +fit_to_longest_item = false item_count = 3 popup/item_0/text = "Linear" popup/item_1/text = "Constant" @@ -52,6 +54,7 @@ size_flags_horizontal = 3 tooltip_text = "Color space" mouse_default_cursor_shape = 2 selected = 0 +fit_to_longest_item = false item_count = 3 popup/item_0/text = "sRGB" popup/item_1/text = "Linear sRGB" @@ -74,6 +77,14 @@ popup/item_1/id = 1 popup/item_2/text = "Divide into equal parts" popup/item_2/id = 2 +[node name="PresetsMenuButton" type="MenuButton" parent="InterpolationContainer"] +unique_name_in_owner = true +layout_mode = 2 +size_flags_horizontal = 3 +mouse_default_cursor_shape = 2 +text = "Presets" +flat = false + [node name="TextureRect" type="TextureRect" parent="."] custom_minimum_size = Vector2(0, 30) layout_mode = 2 @@ -129,5 +140,6 @@ text = "Add point at the end" [connection signal="value_changed" from="InterpolationContainer/OffsetValueSlider" to="." method="_on_offset_value_slider_value_changed"] [connection signal="item_selected" from="InterpolationContainer/InterpolationOptionButton" to="." method="_on_InterpolationOptionButton_item_selected"] [connection signal="item_selected" from="InterpolationContainer/ColorSpaceOptionButton" to="." method="_on_color_space_option_button_item_selected"] +[connection signal="about_to_popup" from="InterpolationContainer/PresetsMenuButton" to="." method="_on_presets_menu_button_about_to_popup"] [connection signal="color_changed" from="Popup/ColorPicker" to="." method="_on_ColorPicker_color_changed"] [connection signal="confirmed" from="DivideConfirmationDialog" to="." method="_on_DivideConfirmationDialog_confirmed"]