From 7dc0af21e7ded692ec455acb145dad2a17cfd5ed Mon Sep 17 00:00:00 2001 From: Emmanouil Papadeas Date: Fri, 13 Jan 2023 05:19:35 +0200 Subject: [PATCH] Create ValueSlider and CollapsibleContainer child nodes through code This lets us create ValueSlider and CollapsibleContainer nodes properly via the add node button, instead of instancing their scenes. They have also become more a bit more themeable. Eventually, ValueSlider.tscn and CollapsibleContainer.tscn will be deleted. --- assets/themes/blue/theme.tres | 13 ++- assets/themes/caramel/theme.tres | 13 ++- assets/themes/dark/theme.tres | 13 ++- assets/themes/gray/theme.tres | 13 ++- assets/themes/light/theme.tres | 13 ++- assets/themes/purple/theme.tres | 13 ++- src/UI/Dialogs/ExportDialog.tscn | 6 +- src/UI/Nodes/CollapsibleContainer.gd | 46 ++++++-- src/UI/Nodes/CollapsibleContainer.tscn | 30 +---- src/UI/Nodes/ValueSlider.gd | 153 ++++++++++++++++++------- src/UI/Nodes/ValueSlider.tscn | 60 +--------- 11 files changed, 226 insertions(+), 147 deletions(-) diff --git a/assets/themes/blue/theme.tres b/assets/themes/blue/theme.tres index c5130a6be..7a73d04b7 100644 --- a/assets/themes/blue/theme.tres +++ b/assets/themes/blue/theme.tres @@ -1,7 +1,11 @@ -[gd_resource type="Theme" load_steps=171 format=2] +[gd_resource type="Theme" load_steps=175 format=2] [ext_resource path="res://assets/fonts/Roboto-Regular.tres" type="DynamicFont" id=1] [ext_resource path="res://assets/fonts/Roboto-Big.tres" type="DynamicFont" id=2] +[ext_resource path="res://assets/graphics/misc/value_arrow_hover.svg" type="Texture" id=3] +[ext_resource path="res://assets/graphics/misc/value_slider.png" type="Texture" id=4] +[ext_resource path="res://assets/graphics/misc/value_arrow_press.svg" type="Texture" id=5] +[ext_resource path="res://assets/graphics/misc/value_arrow.svg" type="Texture" id=6] [sub_resource type="StyleBoxFlat" id=2] content_margin_left = 6.0 @@ -1493,6 +1497,7 @@ CheckButton/styles/disabled = SubResource( 22 ) CheckButton/styles/hover = SubResource( 22 ) CheckButton/styles/normal = SubResource( 22 ) CheckButton/styles/pressed = SubResource( 22 ) +CollapsibleContainer/icons/arrow_normal = ExtResource( 6 ) ColorPicker/constants/h_width = 30 ColorPicker/constants/label_width = 10 ColorPicker/constants/margin = 8 @@ -1783,6 +1788,12 @@ VSplitContainer/icons/grabber = SubResource( 164 ) VSplitContainer/styles/bg = SubResource( 167 ) ValueSlider/colors/progress_color = Color( 0.4, 0.423529, 0.482353, 1 ) ValueSlider/colors/under_color = Color( 0.101961, 0.113725, 0.156863, 1 ) +ValueSlider/icons/arrow_hover = ExtResource( 3 ) +ValueSlider/icons/arrow_normal = ExtResource( 6 ) +ValueSlider/icons/arrow_pressed = ExtResource( 5 ) +ValueSlider/icons/texture_over = null +ValueSlider/icons/texture_progress = ExtResource( 4 ) +ValueSlider/icons/texture_under = ExtResource( 4 ) WindowDialog/colors/title_color = Color( 0.8, 0.8075, 0.8275, 1 ) WindowDialog/constants/close_h_ofs = 22 WindowDialog/constants/close_v_ofs = 20 diff --git a/assets/themes/caramel/theme.tres b/assets/themes/caramel/theme.tres index 3313279b4..9c04b7b7e 100644 --- a/assets/themes/caramel/theme.tres +++ b/assets/themes/caramel/theme.tres @@ -1,7 +1,11 @@ -[gd_resource type="Theme" load_steps=174 format=2] +[gd_resource type="Theme" load_steps=178 format=2] [ext_resource path="res://assets/fonts/Roboto-Regular.tres" type="DynamicFont" id=1] [ext_resource path="res://assets/fonts/Roboto-Big.tres" type="DynamicFont" id=2] +[ext_resource path="res://assets/graphics/misc/value_arrow_hover.svg" type="Texture" id=3] +[ext_resource path="res://assets/graphics/misc/value_arrow_press.svg" type="Texture" id=4] +[ext_resource path="res://assets/graphics/misc/value_arrow.svg" type="Texture" id=5] +[ext_resource path="res://assets/graphics/misc/value_slider.png" type="Texture" id=6] [sub_resource type="StyleBoxFlat" id=2] content_margin_left = 6.0 @@ -1518,6 +1522,7 @@ CheckButton/styles/disabled = SubResource( 23 ) CheckButton/styles/hover = SubResource( 23 ) CheckButton/styles/normal = SubResource( 23 ) CheckButton/styles/pressed = SubResource( 23 ) +CollapsibleContainer/icons/arrow_normal = ExtResource( 5 ) ColorPicker/constants/h_width = 30 ColorPicker/constants/label_width = 10 ColorPicker/constants/margin = 8 @@ -1876,6 +1881,12 @@ VSplitContainer/icons/grabber = SubResource( 167 ) VSplitContainer/styles/bg = SubResource( 170 ) ValueSlider/colors/progress_color = Color( 0.713726, 0.580392, 0.423529, 1 ) ValueSlider/colors/under_color = Color( 0.47451, 0.388235, 0.282353, 1 ) +ValueSlider/icons/arrow_hover = ExtResource( 3 ) +ValueSlider/icons/arrow_normal = ExtResource( 5 ) +ValueSlider/icons/arrow_pressed = ExtResource( 4 ) +ValueSlider/icons/texture_over = null +ValueSlider/icons/texture_progress = ExtResource( 6 ) +ValueSlider/icons/texture_under = ExtResource( 6 ) WindowDialog/colors/title_color = Color( 0.237305, 0.193295, 0.141827, 1 ) WindowDialog/constants/close_h_ofs = 22 WindowDialog/constants/close_v_ofs = 20 diff --git a/assets/themes/dark/theme.tres b/assets/themes/dark/theme.tres index 20921d078..50a4ede12 100644 --- a/assets/themes/dark/theme.tres +++ b/assets/themes/dark/theme.tres @@ -1,7 +1,11 @@ -[gd_resource type="Theme" load_steps=178 format=2] +[gd_resource type="Theme" load_steps=182 format=2] [ext_resource path="res://assets/fonts/Roboto-Regular.tres" type="DynamicFont" id=1] [ext_resource path="res://assets/fonts/Roboto-Big.tres" type="DynamicFont" id=2] +[ext_resource path="res://assets/graphics/misc/value_slider.png" type="Texture" id=3] +[ext_resource path="res://assets/graphics/misc/value_arrow_hover.svg" type="Texture" id=4] +[ext_resource path="res://assets/graphics/misc/value_arrow_press.svg" type="Texture" id=5] +[ext_resource path="res://assets/graphics/misc/value_arrow.svg" type="Texture" id=6] [sub_resource type="StyleBoxFlat" id=2] content_margin_left = 6.0 @@ -1545,6 +1549,7 @@ CheckButton/styles/hover = SubResource( 23 ) CheckButton/styles/hover_pressed = SubResource( 24 ) CheckButton/styles/normal = SubResource( 23 ) CheckButton/styles/pressed = SubResource( 23 ) +CollapsibleContainer/icons/arrow_normal = ExtResource( 6 ) ColorPicker/constants/h_width = 30 ColorPicker/constants/label_width = 10 ColorPicker/constants/margin = 8 @@ -1912,6 +1917,12 @@ VSplitContainer/icons/grabber = SubResource( 172 ) VSplitContainer/styles/bg = SubResource( 175 ) ValueSlider/colors/progress_color = Color( 0.333333, 0.333333, 0.333333, 1 ) ValueSlider/colors/under_color = Color( 0.0666667, 0.0666667, 0.0666667, 1 ) +ValueSlider/icons/arrow_hover = ExtResource( 4 ) +ValueSlider/icons/arrow_normal = ExtResource( 6 ) +ValueSlider/icons/arrow_pressed = ExtResource( 5 ) +ValueSlider/icons/texture_over = null +ValueSlider/icons/texture_progress = ExtResource( 3 ) +ValueSlider/icons/texture_under = ExtResource( 3 ) WindowDialog/colors/title_color = Color( 0.77832, 0.77832, 0.77832, 1 ) WindowDialog/constants/close_h_ofs = 22 WindowDialog/constants/close_v_ofs = 20 diff --git a/assets/themes/gray/theme.tres b/assets/themes/gray/theme.tres index c051a1004..6fbbbc0f5 100644 --- a/assets/themes/gray/theme.tres +++ b/assets/themes/gray/theme.tres @@ -1,7 +1,11 @@ -[gd_resource type="Theme" load_steps=178 format=2] +[gd_resource type="Theme" load_steps=182 format=2] [ext_resource path="res://assets/fonts/Roboto-Regular.tres" type="DynamicFont" id=1] [ext_resource path="res://assets/fonts/Roboto-Big.tres" type="DynamicFont" id=2] +[ext_resource path="res://assets/graphics/misc/value_slider.png" type="Texture" id=3] +[ext_resource path="res://assets/graphics/misc/value_arrow_hover.svg" type="Texture" id=4] +[ext_resource path="res://assets/graphics/misc/value_arrow_press.svg" type="Texture" id=5] +[ext_resource path="res://assets/graphics/misc/value_arrow.svg" type="Texture" id=6] [sub_resource type="StyleBoxFlat" id=2] content_margin_left = 6.0 @@ -1557,6 +1561,7 @@ CheckButton/styles/disabled = SubResource( 23 ) CheckButton/styles/hover = SubResource( 23 ) CheckButton/styles/normal = SubResource( 23 ) CheckButton/styles/pressed = SubResource( 23 ) +CollapsibleContainer/icons/arrow_normal = ExtResource( 6 ) ColorPicker/constants/h_width = 30 ColorPicker/constants/label_width = 10 ColorPicker/constants/margin = 8 @@ -1920,6 +1925,12 @@ VSplitContainer/icons/grabber = SubResource( 171 ) VSplitContainer/styles/bg = SubResource( 174 ) ValueSlider/colors/progress_color = Color( 0.545098, 0.545098, 0.545098, 1 ) ValueSlider/colors/under_color = Color( 0.235294, 0.235294, 0.235294, 1 ) +ValueSlider/icons/arrow_hover = ExtResource( 4 ) +ValueSlider/icons/arrow_normal = ExtResource( 6 ) +ValueSlider/icons/arrow_pressed = ExtResource( 5 ) +ValueSlider/icons/texture_over = null +ValueSlider/icons/texture_progress = ExtResource( 3 ) +ValueSlider/icons/texture_under = ExtResource( 3 ) WindowDialog/colors/title_color = Color( 0.848039, 0.848039, 0.848039, 1 ) WindowDialog/constants/close_h_ofs = 22 WindowDialog/constants/close_v_ofs = 20 diff --git a/assets/themes/light/theme.tres b/assets/themes/light/theme.tres index f3b2caadf..a8976c1ce 100644 --- a/assets/themes/light/theme.tres +++ b/assets/themes/light/theme.tres @@ -1,7 +1,11 @@ -[gd_resource type="Theme" load_steps=178 format=2] +[gd_resource type="Theme" load_steps=182 format=2] [ext_resource path="res://assets/fonts/Roboto-Big.tres" type="DynamicFont" id=1] +[ext_resource path="res://assets/graphics/misc/value_arrow_hover.svg" type="Texture" id=2] +[ext_resource path="res://assets/graphics/misc/value_slider.png" type="Texture" id=3] +[ext_resource path="res://assets/graphics/misc/value_arrow_press.svg" type="Texture" id=4] [ext_resource path="res://assets/fonts/Roboto-Regular.tres" type="DynamicFont" id=5] +[ext_resource path="res://assets/graphics/misc/value_arrow.svg" type="Texture" id=6] [sub_resource type="StyleBoxFlat" id=2] content_margin_left = 6.0 @@ -1557,6 +1561,7 @@ CheckButton/styles/disabled = SubResource( 23 ) CheckButton/styles/hover = SubResource( 23 ) CheckButton/styles/normal = SubResource( 23 ) CheckButton/styles/pressed = SubResource( 23 ) +CollapsibleContainer/icons/arrow_normal = ExtResource( 6 ) ColorPicker/constants/h_width = 30 ColorPicker/constants/label_width = 10 ColorPicker/constants/margin = 8 @@ -1924,6 +1929,12 @@ VSplitContainer/icons/grabber = SubResource( 171 ) VSplitContainer/styles/bg = SubResource( 174 ) ValueSlider/colors/progress_color = Color( 0.74902, 0.74902, 0.74902, 1 ) ValueSlider/colors/under_color = Color( 0.839216, 0.839216, 0.839216, 1 ) +ValueSlider/icons/arrow_hover = ExtResource( 2 ) +ValueSlider/icons/arrow_normal = ExtResource( 6 ) +ValueSlider/icons/arrow_pressed = ExtResource( 4 ) +ValueSlider/icons/texture_over = null +ValueSlider/icons/texture_progress = ExtResource( 3 ) +ValueSlider/icons/texture_under = ExtResource( 3 ) WindowDialog/colors/title_color = Color( 0.25, 0.25, 0.25, 1 ) WindowDialog/constants/close_h_ofs = 22 WindowDialog/constants/close_v_ofs = 20 diff --git a/assets/themes/purple/theme.tres b/assets/themes/purple/theme.tres index 4adaa76c9..aa990853c 100644 --- a/assets/themes/purple/theme.tres +++ b/assets/themes/purple/theme.tres @@ -1,7 +1,11 @@ -[gd_resource type="Theme" load_steps=173 format=2] +[gd_resource type="Theme" load_steps=177 format=2] [ext_resource path="res://assets/fonts/Roboto-Regular.tres" type="DynamicFont" id=1] [ext_resource path="res://assets/fonts/Roboto-Big.tres" type="DynamicFont" id=2] +[ext_resource path="res://assets/graphics/misc/value_arrow_hover.svg" type="Texture" id=3] +[ext_resource path="res://assets/graphics/misc/value_slider.png" type="Texture" id=4] +[ext_resource path="res://assets/graphics/misc/value_arrow_press.svg" type="Texture" id=5] +[ext_resource path="res://assets/graphics/misc/value_arrow.svg" type="Texture" id=6] [sub_resource type="StyleBoxFlat" id=2] content_margin_left = 6.0 @@ -1504,6 +1508,7 @@ CheckButton/styles/disabled = SubResource( 22 ) CheckButton/styles/hover = SubResource( 22 ) CheckButton/styles/normal = SubResource( 22 ) CheckButton/styles/pressed = SubResource( 22 ) +CollapsibleContainer/icons/arrow_normal = ExtResource( 6 ) ColorPicker/constants/h_width = 30 ColorPicker/constants/label_width = 10 ColorPicker/constants/margin = 8 @@ -1787,6 +1792,12 @@ VSplitContainer/icons/grabber = SubResource( 166 ) VSplitContainer/styles/bg = SubResource( 169 ) ValueSlider/colors/progress_color = Color( 0.396078, 0.341176, 0.462745, 1 ) ValueSlider/colors/under_color = Color( 0.0980392, 0.0588235, 0.141176, 1 ) +ValueSlider/icons/arrow_hover = ExtResource( 3 ) +ValueSlider/icons/arrow_normal = ExtResource( 6 ) +ValueSlider/icons/arrow_pressed = ExtResource( 5 ) +ValueSlider/icons/texture_over = null +ValueSlider/icons/texture_progress = ExtResource( 4 ) +ValueSlider/icons/texture_under = ExtResource( 4 ) WindowDialog/colors/title_color = Color( 0.79902, 0.780392, 0.820588, 1 ) WindowDialog/constants/close_h_ofs = 22 WindowDialog/constants/close_v_ofs = 20 diff --git a/src/UI/Dialogs/ExportDialog.tscn b/src/UI/Dialogs/ExportDialog.tscn index ed86e0ae9..5298f3585 100644 --- a/src/UI/Dialogs/ExportDialog.tscn +++ b/src/UI/Dialogs/ExportDialog.tscn @@ -2,7 +2,7 @@ [ext_resource path="res://src/UI/Dialogs/ExportDialog.gd" type="Script" id=1] [ext_resource path="res://src/UI/Nodes/TransparentChecker.tscn" type="PackedScene" id=2] -[ext_resource path="res://src/UI/Nodes/CollapsibleContainer.tscn" type="PackedScene" id=3] +[ext_resource path="res://src/UI/Nodes/CollapsibleContainer.gd" type="Script" id=3] [node name="ExportDialog" type="ConfirmationDialog"] margin_right = 532.0 @@ -281,10 +281,12 @@ margin_top = 234.0 margin_right = 516.0 margin_bottom = 238.0 -[node name="CollapsibleContainer" parent="VBoxContainer/VSplitContainer/VBoxContainer" instance=ExtResource( 3 )] +[node name="CollapsibleContainer" type="VBoxContainer" parent="VBoxContainer/VSplitContainer/VBoxContainer"] margin_top = 242.0 margin_right = 516.0 margin_bottom = 262.0 +theme_type_variation = "CollapsibleContainer" +script = ExtResource( 3 ) text = "Advanced options" [node name="GridContainer" type="GridContainer" parent="VBoxContainer/VSplitContainer/VBoxContainer/CollapsibleContainer"] diff --git a/src/UI/Nodes/CollapsibleContainer.gd b/src/UI/Nodes/CollapsibleContainer.gd index edb27742e..9fe05c911 100644 --- a/src/UI/Nodes/CollapsibleContainer.gd +++ b/src/UI/Nodes/CollapsibleContainer.gd @@ -5,25 +5,55 @@ extends VBoxContainer export var text := "" setget _set_text export var visible_content := false setget _set_visible_content -onready var texture_rect: TextureRect = $Button/TextureRect +var _button := Button.new() +var _texture_rect := TextureRect.new() +var _label := Label.new() + + +func _init() -> void: + theme_type_variation = "CollapsibleContainer" func _ready() -> void: - _set_visible($Button.pressed) + _button.toggle_mode = true + _button.mouse_default_cursor_shape = Control.CURSOR_POINTING_HAND + _button.connect("toggled", self, "_on_Button_toggled") + add_child(_button) + move_child(_button, 0) + _texture_rect.anchor_top = 0.5 + _texture_rect.anchor_bottom = 0.5 + _texture_rect.margin_left = 2 + _texture_rect.margin_top = -6 + _texture_rect.margin_right = 14 + _texture_rect.margin_bottom = 6 + _texture_rect.rect_rotation = -90 + _texture_rect.rect_pivot_offset = Vector2(6, 6) + _texture_rect.add_to_group("UIButtons") + _button.add_child(_texture_rect) + _label.valign = Label.VALIGN_CENTER + _label.rect_position = Vector2(14, 0) + _label.size_flags_vertical = Control.SIZE_SHRINK_CENTER + _button.add_child(_label) + _button.pressed = visible_content for child in get_children(): - if not child is CanvasItem or child == $Button: + if not child is CanvasItem or child == _button: continue child.connect("visibility_changed", self, "_child_visibility_changed", [child]) +func _notification(what: int) -> void: + if what == NOTIFICATION_THEME_CHANGED: + _texture_rect.texture = get_icon("arrow_normal", "CollapsibleContainer") + + func _set_text(value: String) -> void: text = value - $Button/Label.text = value + _label.text = value func _set_visible_content(value: bool) -> void: visible_content = value - $Button.pressed = value + _button.pressed = value func _on_Button_toggled(button_pressed: bool) -> void: @@ -33,9 +63,9 @@ func _on_Button_toggled(button_pressed: bool) -> void: func _set_visible(pressed: bool) -> void: var angle := 0.0 if pressed else -90.0 var tween := create_tween() - tween.tween_property(texture_rect, "rect_rotation", angle, 0.05) + tween.tween_property(_texture_rect, "rect_rotation", angle, 0.05) for child in get_children(): - if not child is CanvasItem or child == $Button: + if not child is CanvasItem or child == _button: continue child.visible = pressed @@ -43,5 +73,5 @@ func _set_visible(pressed: bool) -> void: # Checks if a child becomes visible from another sure and ensures # it remains invisible if the button is not pressed func _child_visibility_changed(child: CanvasItem) -> void: - if not $Button.pressed: + if not _button.pressed: child.visible = false diff --git a/src/UI/Nodes/CollapsibleContainer.tscn b/src/UI/Nodes/CollapsibleContainer.tscn index 3fe8f05a1..71cc81cb5 100644 --- a/src/UI/Nodes/CollapsibleContainer.tscn +++ b/src/UI/Nodes/CollapsibleContainer.tscn @@ -1,35 +1,9 @@ -[gd_scene load_steps=3 format=2] +[gd_scene load_steps=2 format=2] [ext_resource path="res://src/UI/Nodes/CollapsibleContainer.gd" type="Script" id=2] -[ext_resource path="res://assets/graphics/misc/value_arrow.svg" type="Texture" id=3] [node name="CollapsibleContainer" type="VBoxContainer"] margin_right = 40.0 margin_bottom = 20.0 +theme_type_variation = "CollapsibleContainer" script = ExtResource( 2 ) - -[node name="Button" type="Button" parent="."] -margin_right = 40.0 -margin_bottom = 20.0 -mouse_default_cursor_shape = 2 -theme_type_variation = "CollapsibleCheckBox" -toggle_mode = true - -[node name="TextureRect" type="TextureRect" parent="Button" groups=["UIButtons"]] -anchor_top = 0.5 -anchor_bottom = 0.5 -margin_left = 2.0 -margin_top = -6.0 -margin_right = 14.0 -margin_bottom = 6.0 -rect_rotation = -90.0 -rect_pivot_offset = Vector2( 6, 6 ) -texture = ExtResource( 3 ) - -[node name="Label" type="Label" parent="Button"] -anchor_right = 1.0 -anchor_bottom = 1.0 -margin_left = 14.0 -valign = 1 - -[connection signal="toggled" from="Button" to="." method="_on_Button_toggled"] diff --git a/src/UI/Nodes/ValueSlider.gd b/src/UI/Nodes/ValueSlider.gd index 55c5e89ad..ecb076179 100644 --- a/src/UI/Nodes/ValueSlider.gd +++ b/src/UI/Nodes/ValueSlider.gd @@ -29,21 +29,36 @@ export var is_global := false var state := NORMAL var arrow_is_held := 0 # Used for arrow button echo behavior. Is 1 for ValueUp, -1 for ValueDown. -onready var line_edit: LineEdit = $LineEdit -onready var timer: Timer = $Timer +var _line_edit := LineEdit.new() +var _value_up_button := TextureButton.new() +var _value_down_button := TextureButton.new() +var _timer := Timer.new() + + +func _init() -> void: + nine_patch_stretch = true + stretch_margin_left = 3 + stretch_margin_top = 3 + stretch_margin_right = 3 + stretch_margin_bottom = 3 + theme_type_variation = "ValueSlider" func _ready() -> void: + connect("value_changed", self, "_on_value_changed") + _setup_nodes() set_process_input(is_global) - _reset_display() + _reset_display(true) if not Engine.editor_hint: # Pixelorama specific code - $ValueUp.modulate = Global.modulate_icon_color - $ValueDown.modulate = Global.modulate_icon_color + _value_up_button.modulate = Global.modulate_icon_color + _value_down_button.modulate = Global.modulate_icon_color func _notification(what: int) -> void: - if what == NOTIFICATION_THEME_CHANGED or what == NOTIFICATION_TRANSLATION_CHANGED: - _reset_display() + if what == NOTIFICATION_THEME_CHANGED: + _reset_display(true) + elif what == NOTIFICATION_TRANSLATION_CHANGED: + _reset_display(false) func _input(event: InputEvent) -> void: @@ -90,12 +105,12 @@ func _gui_input(event: InputEvent) -> void: elif state == HELD: if event.is_action_released("left_mouse"): state = TYPING - line_edit.text = str(value) - line_edit.editable = true - line_edit.grab_focus() - line_edit.selecting_enabled = true - line_edit.select_all() - line_edit.caret_position = line_edit.text.length() + _line_edit.text = str(value) + _line_edit.editable = true + _line_edit.grab_focus() + _line_edit.selecting_enabled = true + _line_edit.select_all() + _line_edit.caret_position = _line_edit.text.length() tint_progress = Color.transparent elif event is InputEventMouseMotion: if get_meta("mouse_start_position").distance_to(get_local_mouse_position()) > 2: @@ -134,6 +149,47 @@ func _gui_input(event: InputEvent) -> void: value = round(value / snap_step) * snap_step +func _setup_nodes() -> void: # Only called once on _ready() + _line_edit.align = LineEdit.ALIGN_CENTER + _line_edit.anchor_right = 1 + _line_edit.anchor_bottom = 1 + _line_edit.mouse_filter = Control.MOUSE_FILTER_IGNORE + _line_edit.add_stylebox_override("read_only", StyleBoxEmpty.new()) + _line_edit.add_stylebox_override("normal", StyleBoxEmpty.new()) + _line_edit.connect("text_entered", self, "_on_LineEdit_text_entered") + _line_edit.connect("focus_exited", self, "_confirm_text") + _line_edit.connect("gui_input", self, "_on_LineEdit_gui_input") + add_child(_line_edit) + + _value_up_button.rect_scale.y = -1 + _value_up_button.anchor_left = 1 + _value_up_button.anchor_right = 1 + _value_up_button.margin_left = -15 + _value_up_button.margin_top = 12 + _value_up_button.margin_right = -3 + _value_up_button.margin_bottom = 24 + _value_up_button.add_to_group("UIButtons") + _value_up_button.connect("button_down", self, "_on_Value_button_down", [1]) + _value_up_button.connect("button_up", self, "_on_Value_button_up") + add_child(_value_up_button) + + _value_down_button.anchor_left = 1 + _value_down_button.anchor_top = 1 + _value_down_button.anchor_right = 1 + _value_down_button.anchor_bottom = 1 + _value_down_button.margin_left = -15 + _value_down_button.margin_top = -12 + _value_down_button.margin_right = -3 + _value_down_button.margin_bottom = 0 + _value_down_button.add_to_group("UIButtons") + _value_down_button.connect("button_down", self, "_on_Value_button_down", [-1]) + _value_down_button.connect("button_up", self, "_on_Value_button_up") + add_child(_value_down_button) + + _timer.connect("timeout", self, "_on_Timer_timeout") + add_child(_timer) + + func _prefix_changed(v: String) -> void: prefix = v _reset_display() @@ -146,17 +202,17 @@ func _suffix_changed(v: String) -> void: func _show_arrows_changed(v: bool) -> void: show_arrows = v - if not line_edit: + if not _line_edit: return - $ValueUp.visible = v - $ValueDown.visible = v + _value_up_button.visible = v + _value_down_button.visible = v func _on_LineEdit_gui_input(event: InputEvent) -> void: if state == TYPING: if event is InputEventKey and event.scancode == KEY_ESCAPE: _confirm_text(false) # Cancel - line_edit.release_focus() + _line_edit.release_focus() func _on_value_changed(_value: float) -> void: @@ -165,7 +221,7 @@ func _on_value_changed(_value: float) -> void: func _on_LineEdit_text_entered(_new_text) -> void: # When pressing enter, release focus, which will call _confirm_text on focus_exited signal - line_edit.release_focus() + _line_edit.release_focus() # Called on LineEdit's focus_exited signal @@ -176,7 +232,7 @@ func _confirm_text(confirm := true) -> void: state = NORMAL if confirm: var expression := Expression.new() - var error := expression.parse(line_edit.text, []) + var error := expression.parse(_line_edit.text, []) if error != OK: _reset_display() return @@ -188,24 +244,33 @@ func _confirm_text(confirm := true) -> void: _reset_display() -func _reset_display() -> void: - if not line_edit: - return - line_edit.selecting_enabled = false # Remove the selection - line_edit.editable = false - if not Engine.editor_hint: # Pixelorama specific code - var line_edit_color := line_edit.get_color("font_color") - var line_edit_disabled_col: Color = Global.control.theme.get_color("read_only", "LineEdit") +func _reset_display(theme_changed := false) -> void: + _line_edit.selecting_enabled = false # Remove the selection + _line_edit.editable = false + if theme_changed and not Engine.editor_hint: + texture_under = get_icon("texture_under", "ValueSlider") + texture_over = get_icon("texture_over", "ValueSlider") + texture_progress = get_icon("texture_progress", "ValueSlider") + _value_up_button.texture_normal = get_icon("arrow_normal", "ValueSlider") + _value_up_button.texture_pressed = get_icon("arrow_pressed", "ValueSlider") + _value_up_button.texture_hover = get_icon("arrow_hover", "ValueSlider") + + _value_down_button.texture_normal = get_icon("arrow_normal", "ValueSlider") + _value_down_button.texture_pressed = get_icon("arrow_pressed", "ValueSlider") + _value_down_button.texture_hover = get_icon("arrow_hover", "ValueSlider") + + var line_edit_color := _line_edit.get_color("font_color") + var line_edit_disabled_col: Color = get_color("read_only", "LineEdit") if editable: - line_edit.add_color_override("font_color_uneditable", line_edit_color) + _line_edit.add_color_override("font_color_uneditable", line_edit_color) else: - line_edit.add_color_override("font_color_uneditable", line_edit_disabled_col) - tint_under = get_color("under_color", "ValueSlider") - if show_progress: - tint_progress = get_color("progress_color", "ValueSlider") - else: - tint_progress = Color.transparent - line_edit.text = str(tr(prefix), " ", value, " ", tr(suffix)).strip_edges() + _line_edit.add_color_override("font_color_uneditable", line_edit_disabled_col) + tint_under = get_color("under_color", "ValueSlider") + if show_progress: + tint_progress = get_color("progress_color", "ValueSlider") + else: + tint_progress = Color.transparent + _line_edit.text = str(tr(prefix), " ", value, " ", tr(suffix)).strip_edges() func _on_Value_button_down(direction: int) -> void: @@ -214,23 +279,23 @@ func _on_Value_button_down(direction: int) -> void: # Direction is either 1 or -1 value += (snap_step if Input.is_action_pressed("ctrl") else step) * direction arrow_is_held = direction - timer.wait_time = echo_arrow_time * 8 # 0.6 with the default value - timer.one_shot = true - timer.start() + _timer.wait_time = echo_arrow_time * 8 # 0.6 with the default value + _timer.one_shot = true + _timer.start() func _on_Value_button_up() -> void: arrow_is_held = 0 - timer.stop() + _timer.stop() # Echo behavior. If the user keeps pressing the button, the value keeps changing. func _on_Timer_timeout() -> void: if arrow_is_held == 0: - timer.stop() + _timer.stop() return value += (snap_step if Input.is_action_pressed("ctrl") else step) * arrow_is_held - if timer.one_shot: - timer.wait_time = echo_arrow_time - timer.one_shot = false - timer.start() + if _timer.one_shot: + _timer.wait_time = echo_arrow_time + _timer.one_shot = false + _timer.start() diff --git a/src/UI/Nodes/ValueSlider.tscn b/src/UI/Nodes/ValueSlider.tscn index 331c0d7a9..512d44c9e 100644 --- a/src/UI/Nodes/ValueSlider.tscn +++ b/src/UI/Nodes/ValueSlider.tscn @@ -1,12 +1,7 @@ -[gd_scene load_steps=7 format=2] +[gd_scene load_steps=3 format=2] [ext_resource path="res://src/UI/Nodes/ValueSlider.gd" type="Script" id=1] [ext_resource path="res://assets/graphics/misc/value_slider.png" type="Texture" id=2] -[ext_resource path="res://assets/graphics/misc/value_arrow.svg" type="Texture" id=3] -[ext_resource path="res://assets/graphics/misc/value_arrow_hover.svg" type="Texture" id=4] -[ext_resource path="res://assets/graphics/misc/value_arrow_press.svg" type="Texture" id=5] - -[sub_resource type="StyleBoxEmpty" id=1] [node name="ValueSlider" type="TextureProgress"] margin_right = 24.0 @@ -26,56 +21,3 @@ stretch_margin_top = 3 stretch_margin_right = 3 stretch_margin_bottom = 3 script = ExtResource( 1 ) - -[node name="LineEdit" type="LineEdit" parent="."] -anchor_right = 1.0 -anchor_bottom = 1.0 -mouse_filter = 2 -custom_styles/read_only = SubResource( 1 ) -custom_styles/normal = SubResource( 1 ) -text = "0" -align = 1 -editable = false -selecting_enabled = false - -[node name="ValueUp" type="TextureButton" parent="." groups=["UIButtons"]] -anchor_left = 1.0 -anchor_right = 1.0 -margin_left = -15.0 -margin_top = 12.0 -margin_right = -3.0 -margin_bottom = 24.0 -rect_scale = Vector2( 1, -1 ) -mouse_default_cursor_shape = 2 -size_flags_vertical = 3 -texture_normal = ExtResource( 3 ) -texture_pressed = ExtResource( 5 ) -texture_hover = ExtResource( 4 ) -stretch_mode = 3 - -[node name="ValueDown" type="TextureButton" parent="." groups=["UIButtons"]] -anchor_left = 1.0 -anchor_top = 1.0 -anchor_right = 1.0 -anchor_bottom = 1.0 -margin_left = -15.0 -margin_top = -12.0 -margin_right = -3.0 -mouse_default_cursor_shape = 2 -size_flags_vertical = 3 -texture_normal = ExtResource( 3 ) -texture_pressed = ExtResource( 5 ) -texture_hover = ExtResource( 4 ) -stretch_mode = 3 - -[node name="Timer" type="Timer" parent="."] - -[connection signal="value_changed" from="." to="." method="_on_value_changed"] -[connection signal="focus_exited" from="LineEdit" to="." method="_confirm_text"] -[connection signal="gui_input" from="LineEdit" to="." method="_on_LineEdit_gui_input"] -[connection signal="text_entered" from="LineEdit" to="." method="_on_LineEdit_text_entered"] -[connection signal="button_down" from="ValueUp" to="." method="_on_Value_button_down" binds= [ 1 ]] -[connection signal="button_up" from="ValueUp" to="." method="_on_Value_button_up"] -[connection signal="button_down" from="ValueDown" to="." method="_on_Value_button_down" binds= [ -1 ]] -[connection signal="button_up" from="ValueDown" to="." method="_on_Value_button_up"] -[connection signal="timeout" from="Timer" to="." method="_on_Timer_timeout"]