breadwinner/scenes/ui/options_menu/option_v_box_container.gd
Hellie-xd 900f5eb6c3 kinda working options menu, but needs work
also audio lags on my pc, probably the cpu cant keep up with the audio
2026-08-12 01:47:15 +02:00

120 lines
3.5 KiB
GDScript

extends VBoxContainer
class_name DynamicOptionsMenu
# 1. We store the current options class and a dictionary of unsaved changes
var _current_target: Object
var pending_changes: Dictionary = {}
func build_menu(options_node: Options) -> void:
_current_target = options_node
pending_changes.clear() # Wipe any old unsaved changes when loading a new menu
for child in get_children():
child.queue_free()
var properties = options_node.get_property_list()
for prop in properties:
if prop["usage"] & PROPERTY_USAGE_SCRIPT_VARIABLE:
var prop_name = prop["name"]
if prop_name.ends_with("_options"):
continue
var current_value = options_node.get(prop_name)
var ui_row = _create_ui_row(prop, current_value)
if ui_row:
add_child(ui_row)
# Add a spacer to push the button down a bit
add_child(HSeparator.new())
# 2. When pressed, push all staged changes to the actual target class
func on_accept_pressed() -> void:
for prop_name in pending_changes:
var value = pending_changes[prop_name]
_current_target.set(prop_name, value)
# Clear the dictionary so we don't re-apply old changes later
pending_changes.clear()
print("Changes Applied!")
func _create_ui_row(prop: Dictionary, value: Variant) -> Control:
var row = HBoxContainer.new()
var prop_name = prop["name"]
var label = Label.new()
label.text = prop_name.capitalize()
label.custom_minimum_size = Vector2(250, 0)
row.add_child(label)
# --- ENUMS (Dropdowns) ---
if prop["hint"] == PROPERTY_HINT_ENUM:
var dropdown = OptionButton.new()
dropdown.size_flags_horizontal = Control.SIZE_EXPAND_FILL
var enum_names = prop["hint_string"].split(",")
for i in range(enum_names.size()):
dropdown.add_item(enum_names[i], i)
dropdown.selected = value
# THE FIX: Save to dictionary instead of setting directly!
dropdown.item_selected.connect(func(index: int): pending_changes[prop_name] = index)
row.add_child(dropdown)
return row
# --- STANDARD TYPES ---
match typeof(value):
TYPE_BOOL:
var checkbox = CheckBox.new()
checkbox.button_pressed = value
checkbox.toggled.connect(func(toggled: bool): pending_changes[prop_name] = toggled)
row.add_child(checkbox)
TYPE_FLOAT, TYPE_INT:
var slider = HSlider.new()
slider.size_flags_horizontal = Control.SIZE_EXPAND_FILL
slider.min_value = 0.0
slider.max_value = 100.0
slider.step = 0.1 if typeof(value) == TYPE_FLOAT else 1.0
slider.value = float(value)
slider.value_changed.connect(func(new_val: float):
var final_val = new_val if typeof(value) == TYPE_FLOAT else int(new_val)
pending_changes[prop_name] = final_val
)
row.add_child(slider)
TYPE_VECTOR2:
if prop_name == "current_resolution" and "available_resolutions" in _current_target:
var dropdown = OptionButton.new()
dropdown.size_flags_horizontal = Control.SIZE_EXPAND_FILL
var resolutions: Array = _current_target.get("available_resolutions")
var current_res_index = 0
for i in range(resolutions.size()):
var res: Vector2 = resolutions[i]
var res_string = str(res.x) + " x " + str(res.y)
dropdown.add_item(res_string, i)
if res == value:
current_res_index = i
dropdown.selected = current_res_index
dropdown.item_selected.connect(func(index: int):
pending_changes[prop_name] = resolutions[index]
)
row.add_child(dropdown)
else:
var generic_label = Label.new()
generic_label.text = str(value)
row.add_child(generic_label)
_:
row.queue_free()
return null
return row