Files

77 lines
2.4 KiB
GDScript

extends Control
## This entire script is just for handling the ChatBox UI
## if you are here looking for command handlers
## they are in GameShit/ChatSystem/commands
## if you are looking for chat var storage
## they are in GameShit/ChatSystem/ChatBox.gd
## This dict holds a reference to all command IDs and their corresponding call
var Commands : Dictionary = {
"give": CommandHandler.registerCommand("base:giveItem").new(),
"say": CommandHandler.registerCommand("base:say").new()
}
## load the language manager
var language = LanguageManager.loadLanguage()
## parse message
func _parseMessage():
## get the contents
var messageContents : String = %Input.text
## handle if its a command
if messageContents.begins_with("/"):
## strip out the /
var command = messageContents.right(-1)
## run the command
runCommand(command)
%Input.text = ""
MainGame.hideChatbox.emit()
## if its not a command, send the message and reset the field and hide the box
else:
sendMessage(messageContents)
%Input.text = ""
MainGame.hideChatbox.emit()
func sendMessage(message):
%TextDisplay.newline()
%TextDisplay.add_text("{a}: {b}".format({"a": MainGame.username, "b": message}))
func sendMessageAs(user, message):
%TextDisplay.newline()
%TextDisplay.add_text("{a}: {b}".format({"a": user, "b": message}))
func sendSystemErrorMessage(error:Dictionary):
%TextDisplay.newline()
%TextDisplay.add_text("Error: {Error} {Details}".format({"Error": language[error["type"]], "Details": error["caller"]}))
func _ready() -> void:
## here imma add callbacks to signals for chatbox
CommandBus.sendChatMessage.connect(sendMessage)
CommandBus.sendChatMessageAs.connect(sendMessageAs)
CommandBus.castError.connect(sendSystemErrorMessage)
func runCommand(message: String):
var regex = RegEx.new()
regex.compile("(\"[^\"]*\"|'[^']*'|\\S+)")
var matches = regex.search_all(message)
var commandArgs = []
for m in matches:
var arg = m.get_string()
if (arg.begins_with("\"") and arg.ends_with("\"")) or (arg.begins_with("'") and arg.ends_with("'")):
arg = arg.substr(1, arg.length() - 2)
commandArgs.append(arg)
if commandArgs.size() > 0:
var command_name = commandArgs[0]
if Commands.has(command_name):
Commands[command_name].runCommand(commandArgs.slice(1))
else:
CommandBus.castError.emit({"type": "base.command.commandNotFoundError", "caller": command_name})
func _on_input_text_submitted(new_text: String) -> void:
_parseMessage()