Dialogue.gd 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. extends VBoxContainer
  2. #
  3. @onready var button : TouchScreenButton = $BottomVbox/Dialogue/Button/TouchButton
  4. @onready var buttonLabel : Label = $BottomVbox/Dialogue/Button/Label
  5. @onready var scrollable : Scrollable = $BottomVbox/Dialogue/FixedHBox/Scrollable
  6. @onready var scrollbar: VScrollBar = $BottomVbox/Dialogue/FixedHBox/Scrollable/Scroll/_v_scroll
  7. var lastName : String = ""
  8. var lastTween : Tween = null
  9. const PlayerNameLabel : PackedScene = preload("res://presets/gui/labels/PlayerNameLabel.tscn")
  10. const NPCNameLabel : PackedScene = preload("res://presets/gui/labels/NpcNameLabel.tscn")
  11. const PlayerDialogueLabel : PackedScene = preload("res://presets/gui/labels/PlayerDialogueLabel.tscn")
  12. #
  13. func Toggle(toggle : bool):
  14. if toggle:
  15. Launcher.GUI.DisplayInfoContext(["ui_accept", "ui_cancel"])
  16. Launcher.GUI.dialogueWindow.Clear()
  17. else:
  18. Launcher.GUI.infoContext.FadeOut()
  19. Launcher.GUI.dialogueContainer.set_visible(toggle)
  20. func AddName(text : String):
  21. if lastName != text:
  22. lastName = text
  23. var label : RichTextLabel = PlayerNameLabel.instantiate() if lastName == Launcher.Player.nick else NPCNameLabel.instantiate()
  24. label.text = "[color=#" + UICommons.LightTextColor.to_html(false) + "]" + lastName + "[/color]"
  25. scrollable.textContainer.add_child.call_deferred(label)
  26. func AddDialogue(text : String):
  27. var isPlayer : bool = lastName == Launcher.Player.nick
  28. var label : RichTextLabel = PlayerDialogueLabel.instantiate() if isPlayer else Scrollable.contentLabel.instantiate()
  29. label.text = "[color=#" + UICommons.TextColor.to_html(false) + "]" + text + "[/color]"
  30. scrollable.textContainer.add_child.call_deferred(label)
  31. if not isPlayer:
  32. var textSize : int = label.text.length()
  33. label.visible_characters = 0
  34. if lastTween:
  35. lastTween.custom_step(INF)
  36. lastTween = create_tween()
  37. lastTween.tween_property(label, "visible_characters", textSize, textSize * UICommons.DialogueTextSpeed)
  38. lastTween.tween_callback(DialogueDisplayed)
  39. func DialogueDisplayed():
  40. lastTween = null
  41. func AutoScroll():
  42. scrollbar.value = scrollbar.max_value
  43. func ToggleButton(enable : bool, text : String):
  44. if enable and lastTween:
  45. await lastTween.finished
  46. button.set_visible(enable)
  47. buttonLabel.set_text(text)
  48. func ButtonPressed():
  49. if lastTween:
  50. lastTween.custom_step(INF)
  51. return
  52. if Launcher.Player:
  53. Network.TriggerNextContext()
  54. if buttonLabel.text == "Close":
  55. Launcher.GUI.dialogueContainer.set_visible(false)
  56. func Clear():
  57. scrollable.Clear()
  58. lastName = ""
  59. button.set_visible(false)
  60. func AddChoices(choices : PackedStringArray):
  61. if lastTween:
  62. await lastTween.finished
  63. ToggleButton(false, "")
  64. Launcher.GUI.choiceContext.Clear()
  65. if choices.size() > 0:
  66. Launcher.GUI.choiceContext.Push(ContextData.new("ui_context_validate", choices[0], Network.TriggerChoice.bind(0)))
  67. if choices.size() > 1:
  68. Launcher.GUI.choiceContext.Push(ContextData.new("ui_context_cancel", choices[1], Network.TriggerChoice.bind(1)))
  69. if choices.size() > 2:
  70. Launcher.GUI.choiceContext.Push(ContextData.new("ui_context_secondary", choices[2], Network.TriggerChoice.bind(2)))
  71. if choices.size() > 3:
  72. Launcher.GUI.choiceContext.Push(ContextData.new("ui_context_tertiary", choices[3], Network.TriggerChoice.bind(3)))
  73. Launcher.GUI.choiceContext.FadeIn(true)
  74. # Overloaded functions
  75. func _ready():
  76. scrollbar.changed.connect(AutoScroll)
  77. func _input(event : InputEvent):
  78. if Launcher.GUI and Launcher.GUI.dialogueContainer.is_visible() and not Launcher.GUI.choiceContext.is_visible():
  79. if Launcher.Action.TryJustPressed(event, "ui_accept", true):
  80. ButtonPressed()
  81. if Launcher.Action.TryJustPressed(event, "ui_cancel", true):
  82. Network.TriggerCloseContext()