initial shit
This commit is contained in:
66
GameShit/PlayerController/ChatSystem/ChatBox.gd
Normal file
66
GameShit/PlayerController/ChatSystem/ChatBox.gd
Normal file
@@ -0,0 +1,66 @@
|
||||
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
|
||||
|
||||
var Commands : Dictionary = {
|
||||
"give": CommandHandler.registerCommand("base:giveItem").new(),
|
||||
"say": CommandHandler.registerCommand("base:say").new()
|
||||
}
|
||||
|
||||
var language = LanguageManager.loadLanguage()
|
||||
|
||||
func _parseMessage():
|
||||
var messageContents : String = %Input.text
|
||||
|
||||
if messageContents.begins_with("/"):
|
||||
var command = messageContents.right(-1)
|
||||
runCommand(command)
|
||||
%Input.text = ""
|
||||
MainGame.hideChatbox.emit()
|
||||
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()
|
||||
Reference in New Issue
Block a user