extends CanvasLayer ## Dialog-Box am unteren Bildrand im Stil von Baldur's Gate 3 ## (Change Request, siehe docs/CHANGEREQUESTS_DONE.md). ## ## Zeigt jede Dialog-Zeile gut lesbar mit Sprechernamen an und beherbergt ## die fokus-navigierbare Antwortauswahl; die Sprechblasen über den Köpfen ## bleiben als Ambiente bestehen. Navigation über Pfeiltasten/D-Pad/linken ## Stick, Aktivierung über confirm (Enter/Ⓐ/✕) nach dem Muster des ## Hauptmenü-Fixes: Events, die auch Godots ui_accept matchen, verarbeitet ## der fokussierte Button selbst, nur die übrigen (Controller) lösen ## manuell aus. choose() liefert den Index der gewählten Option als ## Coroutine. signal option_chosen(index: int) const ABORT_INDEX := -1 @onready var _speaker_label: Label = $Root/Panel/Margin/Content/SpeakerLabel @onready var _line_label: Label = $Root/Panel/Margin/Content/LineLabel @onready var _options_box: VBoxContainer = $Root/Panel/Margin/Content/OptionsBox ## Startet unsichtbar; sichtbar wird die Box über show_line(). func _ready() -> void: visible = false ## Zeigt eine Dialog-Zeile mit Sprechernamen; eine noch offene ## Antwortauswahl wird dabei geleert. func show_line(speaker_name: String, text: String) -> void: _speaker_label.text = speaker_name _line_label.text = text _clear_options() visible = true ## Zeigt die Antwortoptionen unterhalb der aktuellen Zeile an und liefert ## den Index der gewählten Option, sobald der Spieler bestätigt hat. func choose(option_texts: Array) -> int: _show_options(option_texts) var index: int = await option_chosen return index ## Blendet die Box am Dialogende aus. func hide_box() -> void: _clear_options() visible = false ## Meldet, ob gerade eine Antwortauswahl offen ist (auch für Test-Sonden). func has_open_options() -> bool: return visible and _options_box.visible ## Aktiviert mit confirm den fokussierten Options-Button und meldet mit ## cancel den Dialog-Abbruch als ABORT_INDEX (nur solange eine Auswahl ## offen ist); Fokus-Verlust wird auf die erste Option repariert. func _unhandled_input(event: InputEvent) -> void: if not has_open_options(): return if event.is_action_pressed("cancel") or event.is_action_pressed("dialogue_abort"): _clear_options() option_chosen.emit(ABORT_INDEX) return if not event.is_action_pressed("confirm"): return var focused := get_viewport().gui_get_focus_owner() if focused == null or focused.get_parent() != _options_box: if _options_box.get_child_count() > 0: (_options_box.get_child(0) as Button).grab_focus() return if event.is_action("ui_accept"): return var button := focused as BaseButton if button != null and not button.disabled: button.pressed.emit() ## Baut die Options-Buttons neu auf und setzt den Fokus auf die erste ## Option. func _show_options(option_texts: Array) -> void: _clear_options() var first_button: Button = null for i in option_texts.size(): var button := Button.new() button.text = str(option_texts[i]) button.custom_minimum_size = Vector2(0, 40) button.pressed.connect(_on_option_pressed.bind(i)) _options_box.add_child(button) if first_button == null: first_button = button _options_box.visible = true if first_button != null: first_button.grab_focus.call_deferred() ## Entfernt alle Options-Buttons und blendet die Options-Leiste aus. func _clear_options() -> void: for child in _options_box.get_children(): _options_box.remove_child(child) child.queue_free() _options_box.visible = false ## Schließt die Auswahl und meldet den gewählten Index. func _on_option_pressed(index: int) -> void: _clear_options() option_chosen.emit(index)