Skip to content

Commit

Permalink
Use custom container type for path command editors (#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
MewPurPur authored Nov 27, 2023
1 parent 3a421ca commit d0cc8b9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 25 deletions.
24 changes: 24 additions & 0 deletions src/ui_elements/CustomSpacedHBoxContainer.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## Allows passing an array of custom horizontal spacings.
class_name CustomSpacedHBoxContainer extends Container

# Array for the spacing between elements.
var _spacing_array: Array[int]

func _ready() -> void:
sort_children.connect(_sort_children)

func set_spacing_array(new_arr: Array[int]) -> void:
_spacing_array = new_arr
queue_sort()

func _sort_children():
var current_x: float = 0
var spacing_arr_size := _spacing_array.size()

for i in get_child_count():
var child: Control = get_child(i)
child.position.x = current_x

current_x += child.size.x
if i < spacing_arr_size:
current_x += _spacing_array[i]
44 changes: 21 additions & 23 deletions src/ui_elements/path_command_editor.gd
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
## An editor for a single path command.
extends PanelContainer

const spacing_dict = {
"A": [3, 4, 4, 4, 4, 3],
"C": [3, 4, 3, 4, 3],
"Q": [3, 4, 3],
"S": [3, 4, 3],
"M": [3],
"L": [3],
"T": [3],
}

signal cmd_update_value(idx: int, new_value: float, property: StringName)
signal cmd_delete(idx: int)
signal cmd_toggle_relative(idx: int)
Expand All @@ -18,7 +28,7 @@ var path_command: PathCommand

@onready var relative_button: Button = $HBox/RelativeButton
@onready var more_button: Button = $HBox/MoreButton
@onready var fields_container: HBoxContainer = $HBox/Fields
@onready var fields_container: CustomSpacedHBoxContainer = $HBox/Fields

var fields: Array[Control] = []

Expand All @@ -28,11 +38,12 @@ func update_type() -> void:
fields.clear()
setup_relative_button()

fields_container.set_spacing_array(spacing_dict[command_type])

# Instantiate the input fields.
if command_type == "A":
var fields_rx_ry: Array[BetterLineEdit] = add_number_field_pair()
var field_rx := fields_rx_ry[0]
var field_ry := fields_rx_ry[1]
var field_rx: BetterLineEdit = add_number_field()
var field_ry: BetterLineEdit = add_number_field()
var field_rot: BetterLineEdit = add_number_field()
var field_large_arc_flag: Button = add_flag_field()
var field_sweep_flag: Button = add_flag_field()
Expand Down Expand Up @@ -61,9 +72,8 @@ func update_type() -> void:
fields.append(field_large_arc_flag)
fields.append(field_sweep_flag)
if command_type == "Q" or command_type == "C":
var fields_x1_y1: Array[BetterLineEdit] = add_number_field_pair()
var field_x1 := fields_x1_y1[0]
var field_y1 := fields_x1_y1[1]
var field_x1: BetterLineEdit = add_number_field()
var field_y1: BetterLineEdit = add_number_field()
field_x1.set_value(path_command.x1)
field_y1.set_value(path_command.y1)
field_x1.tooltip_text = "x1"
Expand All @@ -73,9 +83,8 @@ func update_type() -> void:
fields.append(field_x1)
fields.append(field_y1)
if command_type == "C" or command_type == "S":
var fields_x2_y2: Array[BetterLineEdit] = add_number_field_pair()
var field_x2 := fields_x2_y2[0]
var field_y2 := fields_x2_y2[1]
var field_x2: BetterLineEdit = add_number_field()
var field_y2: BetterLineEdit = add_number_field()
field_x2.set_value(path_command.x2)
field_y2.set_value(path_command.y2)
field_x2.tooltip_text = "x2"
Expand All @@ -98,9 +107,8 @@ func update_type() -> void:
field_y.value_changed.connect(update_value.bind(&"y"))
fields.append(field_y)
else:
var fields_x_y: Array[BetterLineEdit] = add_number_field_pair()
var field_x := fields_x_y[0]
var field_y := fields_x_y[1]
var field_x: BetterLineEdit = add_number_field()
var field_y: BetterLineEdit = add_number_field()
field_x.set_value(path_command.x)
field_x.tooltip_text = "x"
field_y.set_value(path_command.y)
Expand Down Expand Up @@ -245,16 +253,6 @@ func add_flag_field() -> Button:
fields_container.add_child(new_field)
return new_field

func add_number_field_pair() -> Array[BetterLineEdit]:
var hbox := HBoxContainer.new()
hbox.add_theme_constant_override(&"separation", 3)
var new_fields: Array[BetterLineEdit] =\
[MiniNumberField.instantiate(), MiniNumberField.instantiate()]
hbox.add_child(new_fields[0])
hbox.add_child(new_fields[1])
fields_container.add_child(hbox)
return new_fields


func _on_relative_button_pressed() -> void:
cmd_char = cmd_char.to_upper() if Utils.is_string_lower(cmd_char)\
Expand Down
6 changes: 4 additions & 2 deletions src/ui_elements/path_command_editor.tscn
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
[gd_scene load_steps=5 format=3 uid="uid://dcdrc3r60bgg3"]
[gd_scene load_steps=6 format=3 uid="uid://dcdrc3r60bgg3"]

[ext_resource type="Script" path="res://src/ui_elements/path_command_editor.gd" id="1_om2fk"]
[ext_resource type="FontFile" uid="uid://dtb4wkus51hxs" path="res://visual/fonts/FontMono.ttf" id="2_o5eem"]
[ext_resource type="Texture2D" uid="uid://cmepkbqde0jh0" path="res://visual/icons/SmallMore.svg" id="3_a76tm"]
[ext_resource type="Script" path="res://src/ui_elements/CustomSpacedHBoxContainer.gd" id="3_p38uu"]

[sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_sewij"]

Expand All @@ -27,8 +28,9 @@ theme_override_colors/font_color = Color(0.866667, 0.933333, 1, 1)
theme_override_fonts/font = ExtResource("2_o5eem")
theme_override_font_sizes/font_size = 13

[node name="Fields" type="HBoxContainer" parent="HBox"]
[node name="Fields" type="Container" parent="HBox"]
layout_mode = 2
script = ExtResource("3_p38uu")

[node name="MoreButton" type="Button" parent="HBox"]
layout_mode = 2
Expand Down

0 comments on commit d0cc8b9

Please sign in to comment.