TableTower/ui/sound_panel.gd
RobinFiredrake bc2936cf15 Sound slider in View ▾ and ☰; built-in themes hide Delete
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 14:26:39 -06:00

43 lines
1.7 KiB
GDScript

class_name SoundPanel
extends RefCounted
## View ▾ → Sound… (and the ☰ menu): a small pop-up with a volume slider from
## Off to Loud. Dragging changes the volume at once; letting go saves it and
## plays the roll sound at the new level.
static func open(m: Main, at: Vector2) -> PopupPanel:
var popup := PopupPanel.new()
popup.theme = Style.card_theme()
var column := VBoxContainer.new()
column.add_theme_constant_override("separation", 6)
column.custom_minimum_size = Vector2(240, 0)
popup.add_child(column)
var heading := HBoxContainer.new()
heading.add_child(Style.make_label("Sound", Style.body_bold(), 15, Style.INK))
var shown := Style.make_label("", Style.body(), 14, Style.MUTED)
shown.size_flags_horizontal = Control.SIZE_EXPAND_FILL
shown.horizontal_alignment = HORIZONTAL_ALIGNMENT_RIGHT
heading.add_child(shown)
column.add_child(heading)
var slider := HSlider.new()
slider.min_value = 0
slider.max_value = 100
slider.step = 1
slider.value = roundf(m.sound.volume * 100.0)
slider.tooltip_text = "How loud the theme's sounds are"
column.add_child(slider)
var words := func(percent: float) -> String:
return "Off" if percent <= 0.0 else "%d%%" % int(percent)
shown.text = words.call(slider.value)
slider.value_changed.connect(func(percent: float) -> void:
m.sound.preview_volume(percent / 100.0)
shown.text = words.call(percent))
slider.drag_ended.connect(func(_changed: bool) -> void:
m.sound.set_volume(slider.value / 100.0)
m.sound.play("roll"))
popup.popup_hide.connect(func() -> void:
m.sound.set_volume(slider.value / 100.0)
popup.queue_free())
m.add_child(popup)
popup.popup(Rect2i(Vector2i(at), Vector2i(260, 0)))
return popup