## Wooden knocks for the menus: a soft tick when a button is pointed at or focused, a ## peg knocked home when it's pressed, a two-step knock for going back. Attached to a ## Control, it hooks every button under it, including ones made later (the server list). class_name MenuSounds extends Node const BACK_WORDS: Array[String] = ["Back", "Resume", "Leave", "Cancel"] ## Sounds played since attached, newest last: for tests. var heard: Array[String] = [] var audible: bool = true var _root: Control var _voice: AudioStreamPlayer func attach(root: Control) -> void: _root = root SoundBank.ensure_bus(SoundBank.EFFECTS_BUS) _voice = AudioStreamPlayer.new() _voice.bus = SoundBank.EFFECTS_BUS _voice.max_polyphony = 4 add_child(_voice) _hook_all(root) func _ready() -> void: get_tree().node_added.connect(_hook_later) func _hook_later(node: Node) -> void: if _root != null and (node == _root or _root.is_ancestor_of(node)): _hook(node) func _hook_all(node: Node) -> void: _hook(node) for child: Node in node.get_children(): _hook_all(child) func _hook(node: Node) -> void: var button: BaseButton = node as BaseButton if button == null or button.has_meta("menu_sounds"): return button.set_meta("menu_sounds", true) var text: String = (button as Button).text if button is Button else "" var press: String = "ui_back" if BACK_WORDS.has(text) else "ui_click" button.pressed.connect(play.bind(press)) button.mouse_entered.connect(play.bind("ui_hover")) button.focus_entered.connect(play.bind("ui_hover")) func play(sound: String) -> void: heard.append(sound) if heard.size() > 64: heard.pop_front() if not audible or _voice == null: return _voice.stream = SoundBank.stream(sound) _voice.play()