extends Control @onready var console_log: RichTextLabel = %ConsoleLog @onready var suggestions_list: ItemList = %SuggestionsList @onready var console_input: LineEdit = %ConsoleInput ## maybe should change the arrays to dicts so we can give them descriptions var context = {} var spaces: Dictionary = {"player" : "everything that is to do with the player", "level" : "everything that is to do with the level", "server" : "everything that is to do with the server"} var player_commands: Dictionary = {"add_inventory_slot" : "add a slot to your inventory. max inv slots = 5", "remove_inventory_slot" : "remove a slot from your inventory. min inv slots = 2"} var level_commands: Dictionary = {"reset": "resets the current level"} var avaliable_commands: Dictionary = { "help" : "print the available commands. \"help player\" prints the available commands for the player", "clear" : "clear the console", "whoami" : "returns the player node as text", "exit" : "currently just exits the game fully"} var toggled: bool = false ## print to the console from the console line and empty it, like a submit func print_to_console_from_console(...str:Array): console_log.add_text("> " + console_input.text + "\n") for x in str: console_log.add_text(str(x)) console_log.add_text("\n\n") console_input.clear() func print_command_not_found(command:String): print(command) print_to_console_from_console("\"", command, "\" command not found. Type \"help\" for a list of available commands.") func print_to_console(new_line:bool = false, ...str:Array, ): for x in str: console_log.add_text(str(x)) if new_line: console_log.add_text("\n\n") func print_help(space:String = ""): var help_text: String = "" match space: "player": for x in player_commands: help_text += x + " - " + player_commands[x] help_text += "\n" "level": for x in level_commands: help_text += x + " - " + player_commands[x] help_text += "\n" _: for x in avaliable_commands: help_text += x + " - " + avaliable_commands[x] help_text += "\n" help_text += "Avaliable spaces: \n" for x in spaces: help_text += "\t\t" + x + " - " + spaces[x] help_text += "\n" print_to_console_from_console(help_text) func clear_console(): console_log.text = "" print_to_console_from_console("Cleared console output.") func toggle_caret(char:String = "_"): print_to_console_from_console("toggle") console_input.text += char var selected_suggestion = 0 func fill_from_suggestion(): var cur_space = console_input.text.split(".") if cur_space.size() > 1: console_input.text = cur_space[0] + "." + suggestions_list.get_item_text(selected_suggestion).split(" ")[0] else: if suggestions_list.item_count > 0: var fill = suggestions_list.get_item_text(selected_suggestion).split(" ") if fill[0] in spaces.keys(): console_input.text = fill[0] + "." suggestions_list.clear() show_space_commands_suggestions(fill[0], "") else: console_input.text = fill[0] + " " console_input.grab_focus() console_input.caret_column = console_input.text.length() selected_suggestion = 0 suggestions_list.clear() func show_space_suggestions(current_string:String): for space: String in spaces: if current_string in space: suggestions_list.add_item(space + " - " + spaces[space]) func show_commands_suggestions(current_string:String): if console_input.text != "" and not ("." in console_input.text): for command: String in avaliable_commands: if command.contains(current_string): suggestions_list.add_item(command + " - " + avaliable_commands[command]) func show_space_commands_suggestions(space:String, command:String): match space: "player": for player_command: String in player_commands: if command in player_command: suggestions_list.add_item(player_command + " - " + player_commands[player_command]) "level": for player_command: String in level_commands: if command in player_command: suggestions_list.add_item(player_command + " - " + level_commands[player_command]) func show_suggestions(current_string: String): suggestions_list.clear() if !current_string.contains("."): show_space_suggestions(current_string) show_commands_suggestions(current_string) else: var space = current_string.split(".") if space.size() > 1: show_space_commands_suggestions(space[0], space[1]) func parsed_command(command:String): if command == "": return var space = command.split(".") var parts = command.split(" ", false) if space.size() < 2: match parts[0]: "help": print_help(parts[1] if (parts.size() > 1) else "") "whoami": print_to_console_from_console(context["player"] if context.has("player") else "bruh") "clear": clear_console() _: print_command_not_found(command) else: match space[0]: "player": match space[1]: "add_inventory_slot": emit_signal("add_inventory") print_to_console_from_console("Added inventory slot.") "remove_inventory_slot": emit_signal("remove_inventory") print_to_console_from_console("Removed inventory slot.") _: print_command_not_found(command) "level": match space[1]: "reset": get_tree().reload_current_scene() _: print_command_not_found(command) _: print_command_not_found(command) console_input.text = "" signal add_inventory signal remove_inventory func _ready() -> void: console_input.caret_blink = true console_input.text_submitted.connect(parsed_command) console_input.text_changed.connect(show_suggestions) suggestions_list.item_selected.connect(fill_from_suggestion) func _input(event: InputEvent) -> void: if Input.is_action_just_pressed("toggle_console") and not Input.is_key_pressed(KEY_SHIFT) or (toggled and Input.is_action_just_pressed("open_menu")): toggled = !toggled if toggled: console_input.grab_focus() Input.mouse_mode = Input.MOUSE_MODE_VISIBLE else: console_input.release_focus() Input.mouse_mode = Input.MOUSE_MODE_CAPTURED get_tree().create_timer(.1).timeout.connect(console_input.clear) if toggled and (Input.is_action_just_pressed("ui_text_completion_accept") or Input.is_key_pressed(KEY_ENTER)) and suggestions_list.item_count > 0: fill_from_suggestion() if toggled and suggestions_list.item_count > 0: if Input.is_key_pressed(KEY_DOWN): if selected_suggestion + 1 > suggestions_list.item_count - 1: selected_suggestion = 0 else: selected_suggestion = selected_suggestion + 1 if Input.is_key_pressed(KEY_UP): if selected_suggestion - 1 < 0: selected_suggestion = suggestions_list.item_count - 1 else: selected_suggestion = selected_suggestion - 1 suggestions_list.select(selected_suggestion) func _process(delta: float) -> void: offset_transform_position_ratio.y = lerpf(offset_transform_position_ratio.y, -3 if !toggled else 0, delta * 10)