Chat.gd 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. extends VBoxContainer
  2. class_name ChatContainer
  3. @onready var tabContainer : TabContainer = $ChatTabContainer
  4. @onready var lineEdit : LineEdit = $NewText
  5. @onready var tabInstance : Object = FileSystem.LoadGui("labels/ChatLabel", false)
  6. @onready var backlog : ChatBacklog = ChatBacklog.new()
  7. var enabledLastFrame : bool = false
  8. #
  9. func AddPlayerText(playerName : String, speech : String):
  10. AddText(playerName + ": " + speech, UICommons.LightTextColor)
  11. func AddSystemText(speech : String):
  12. AddText(speech, UICommons.TextColor)
  13. func AddText(speech : String, color : Color):
  14. if tabContainer && tabContainer.get_current_tab_control():
  15. tabContainer.get_current_tab_control().text += "[color=#" + color.to_html(false) + "]" + speech + "[/color]\n"
  16. func isNewLineEnabled() -> bool:
  17. return lineEdit.is_visible() and lineEdit.has_focus() if lineEdit else false
  18. func SetNewLineEnabled(enable : bool):
  19. if Launcher.Action and lineEdit and not enabledLastFrame:
  20. enabledLastFrame = true
  21. if not LauncherCommons.isMobile:
  22. lineEdit.set_visible(enable)
  23. Launcher.Action.Enable(!enable)
  24. if enable:
  25. lineEdit.grab_focus()
  26. #
  27. func OnNewTextSubmitted(newText : String):
  28. if lineEdit:
  29. if newText.is_empty() == false:
  30. lineEdit.clear()
  31. if Launcher.Player:
  32. backlog.Add(newText)
  33. Network.TriggerChat(newText)
  34. SetNewLineEnabled(false)
  35. else:
  36. SetNewLineEnabled(false)
  37. #
  38. func _input(event : InputEvent):
  39. if FSM.IsGameState() and isNewLineEnabled():
  40. if Launcher.Action.TryJustPressed(event, "ui_cancel", true):
  41. SetNewLineEnabled(false)
  42. elif Launcher.Action.TryJustPressed(event, "ui_up", true):
  43. backlog.Up()
  44. lineEdit.text = backlog.Get()
  45. elif Launcher.Action.TryJustPressed(event, "ui_down", true):
  46. backlog.Down()
  47. lineEdit.text = backlog.Get()
  48. elif Launcher.Action.TryJustPressed(event, "ui_validate", true):
  49. OnNewTextSubmitted(lineEdit.text)
  50. func _physics_process(_delta):
  51. if enabledLastFrame:
  52. enabledLastFrame = false
  53. func _ready():
  54. assert(tabContainer && tabInstance, "TabContainer or TabInstance not correctly set")
  55. if tabContainer && tabInstance:
  56. var newTab : RichTextLabel = tabInstance.instantiate()
  57. newTab.set_name("General")
  58. tabContainer.add_child(newTab)
  59. AddSystemText("Welcome to " + LauncherCommons.ProjectName)
  60. SetNewLineEnabled(false)