hamncheese/hamncheese/Scripts/dialog.gd
2023-09-19 20:07:55 -05:00

102 lines
2.5 KiB
GDScript

extends Node
signal _dialog_closed
var _dialog
var _line_edit
var _result
func _accepted():
_dialog.queue_free()
_result = true
emit_signal("_dialog_closed")
func _canceled():
_dialog.queue_free()
_result = false
emit_signal("_dialog_closed")
func _closed():
_dialog.queue_free()
emit_signal("_dialog_closed")
func _submitted(_text: String):
_dialog.queue_free()
_result = true
emit_signal("_dialog_closed")
func alert(title: String, text: String, parent: Node = null):
_dialog = AcceptDialog.new()
_dialog.dialog_text = text
_dialog.title = title
_dialog.unresizable = true
_dialog.get_ok_button().pressed.connect(_closed)
if parent == null:
var scene_tree = Engine.get_main_loop()
scene_tree.current_scene.add_child(_dialog)
else:
parent.add_child(_dialog)
_dialog.popup_centered()
await _dialog_closed
func confirm(title: String, text: String, parent: Node = null):
_dialog = ConfirmationDialog.new()
_dialog.dialog_text = text
_dialog.title = title
_dialog.unresizable = true
_dialog.get_ok_button().pressed.connect(_accepted)
_dialog.get_cancel_button().pressed.connect(_canceled)
if parent == null:
var scene_tree = Engine.get_main_loop()
scene_tree.current_scene.add_child(_dialog)
else:
parent.add_child(_dialog)
_dialog.popup_centered()
await _dialog_closed
return _result
func prompt(title: String, text: String, parent: Node = null, chars: int = 32):
var hbox := HBoxContainer.new()
hbox.size_flags_horizontal = Control.SIZE_EXPAND_FILL
var label := Label.new()
label.text = text
hbox.add_child(label)
hbox.add_spacer(false)
_line_edit = LineEdit.new()
var size = _line_edit.get_minimum_size()
size.x = _line_edit.get_theme_default_font_size() * chars
_line_edit.set_custom_minimum_size(size)
_line_edit.max_length = chars
_line_edit.expand_to_text_length = true
_line_edit.secret = true
_line_edit.select_all_on_focus = true
_line_edit.text_submitted.connect(_submitted)
hbox.add_child(_line_edit)
_dialog = AcceptDialog.new()
_dialog.title = title
_dialog.unresizable = true
_dialog.register_text_enter(_line_edit)
var cancel = _dialog.add_cancel_button("Cancel")
_dialog.get_ok_button().pressed.connect(_accepted)
cancel.pressed.connect(_canceled)
_dialog.add_child(hbox)
if parent == null:
var scene_tree = Engine.get_main_loop()
scene_tree.current_scene.add_child(_dialog)
else:
parent.add_child(_dialog)
_dialog.popup_centered()
await _dialog_closed
if _result:
return _line_edit.text
else:
return null