Chat.gd 2.1 KB

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