frame-of-mind/src/ui/prompter/prompter.gd

106 lines
2.9 KiB
GDScript3
Raw Normal View History

2026-01-23 12:39:47 +00:00
extends Control
## Provides methods and pre-instantiated prompts that can be easily arranged.
class_name Prompter
signal performed(action: String)
var _prompts : Dictionary[StringName, Control] = {}
2026-01-23 14:05:24 +00:00
static var _instance : Prompter = null
func _ready() -> void:
_register_prompts()
2026-01-23 14:05:24 +00:00
clear()
func _register_prompts() -> void:
2026-01-23 14:05:24 +00:00
assert(not _instance, "Cannot have two prompters (are you trying to run the prompter itself?)")
_instance = self
for prompt in get_tree().get_nodes_in_group("prompts") as Array[PromptButton]:
_prompts[prompt.name] = prompt
prompt.get_parent().remove_child(prompt)
prompt.performed.connect(_perform)
prints("Prompter.gd:", "registered", prompt)
func _perform(action : StringName):
prints("Prompter.gd:", "PERFORMED", action)
performed.emit(action)
2026-01-23 14:05:24 +00:00
func _clear(container: Control) -> void:
for child in container.get_children():
container.remove_child(child)
func _clearInteraction() -> void:
%Interaction.text = ""
%Interaction.get_parent().hide()
func _clearInstruction() -> void:
%Instruction.text = ""
%Instruction.get_parent().hide()
func _show(container: Control, controls: Array[Control], overrides: Array[StringName] = []) -> void:
for i in range(len(controls)):
var control := controls[i]
var override : StringName = overrides[i] if overrides else &""
container.add_child(control)
control.appear(override)
2026-01-23 14:05:24 +00:00
func test() -> void:
await get_tree().create_timer(0.5).timeout
var prompts := pick("ui_cancel", "ui_accept")
2026-01-23 14:23:53 +00:00
left_bottom(prompts)
2026-01-23 14:05:24 +00:00
interaction("Hello World.")
instruction("lorem ipsum dolor sit amet")
await get_tree().create_timer(2.5).timeout
clear()
func interaction(text: String) -> void:
if not text: _clearInteraction()
%Interaction.text = text
%Interaction.get_parent().show()
func instruction(text: String) -> void:
if not text: _clearInstruction()
%Instruction.text = text
%Instruction.get_parent().show()
2026-01-23 14:23:53 +00:00
func left_bottom(controls: Array[Control], overrides: Array[StringName] = []) -> void:
_show(%LeftBottom, controls, overrides)
2026-01-23 14:23:53 +00:00
func center(controls: Array[Control], overrides: Array[StringName] = []) -> void:
_show(%Center, controls, overrides)
func right_bottom(controls: Array[Control], overrides: Array[StringName] = []) -> void:
_show(%RightBottom, controls, overrides)
2026-01-23 14:05:24 +00:00
func clear() -> void:
_clear(%LeftBottom)
2026-01-23 14:05:24 +00:00
_clear(%RightBottom)
2026-01-23 14:23:53 +00:00
_clear(%Center)
2026-01-23 14:05:24 +00:00
_clearInteraction()
_clearInstruction()
## Selects a list of up to three prompts to be passed to the show_xxxxx() functions
func pick(p0: StringName, p1: StringName = "", p2: StringName = "") -> Array[Control]:
var result : Array[Control] = []
assert(p0 in _prompts, p0 + " is not a known prompt.")
assert(not p1 or p1 in _prompts, p1 + " is not a known prompt.")
assert(not p2 or p2 in _prompts, p2 + " is not a known prompt.")
result.append(_prompts[p0])
if p1: result.append(_prompts[p1])
if p2: result.append(_prompts[p2])
return result