Skip to content

Commit 63d8397

Browse files
authored
Merge pull request #14 from XanatosX/develop
Release 0.2.0
2 parents 0d3d7a4 + bd1c687 commit 63d8397

File tree

12 files changed

+125
-15
lines changed

12 files changed

+125
-15
lines changed

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,55 @@ To learn how to add and remove custom commands please checkout the example [scri
66

77
I do know there is a addon like this already in development, check it out at the end of this readme. I still wanted to create my own interpretation of this used in games I created. But as I already developed it I decided to make it public and give something back to the Godot developer community.
88

9+
![Welcome Image](https://i.imgur.com/Z7XDN6T.jpeg)
10+
![List commands](https://i.imgur.com/XN2kKRB.jpeg)
11+
912
## How to install
1013

1114
To install the addon download the source files and put the "addons" folder on a root level to your project. After that enable the plugin via the Godot plugin menu.
1215

1316
Checkout this part of the [Godot documentation][installing-and-enable-plugin]
1417

18+
## Quickstart
19+
20+
### Register a command
21+
22+
```gdscript
23+
Console.register_custom_command("reload", _reload, [], "Reload current scene")
24+
25+
func _reload() -> String:
26+
get_tree().reload_current_scene()
27+
return "reloaded scene"
28+
```
29+
30+
### Unregister a command
31+
32+
```gdscript
33+
Console.remove_command("reload")
34+
```
35+
36+
### Other important Options
37+
38+
```gdscript
39+
## Set toggle key
40+
Console.set_console_key(KEY_F12)
41+
42+
## Hide console
43+
Console.hide_console()
44+
45+
## Show console
46+
Console.show_console()
47+
48+
## Pause game tree if console does open up
49+
Console.should_pause_on_open()
50+
51+
## Disable console completely, can be used to remove it on release builds
52+
Console.disable()
53+
54+
## Enable a disabled console
55+
Console.enable()
56+
```
57+
1558
## Example Project
1659

1760
I added a test and example project to this addon so you can check out the console in action. The example is not much but does show the usage in a really basic manner.

addons/gameconsole/builtin/command_close.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
extends CommandTemplate
22

33
func create_command() -> Command:
4-
var command = Command.new("close", _close, [], "_close the console")
4+
var command = Command.new("close", _close, [], "close the console")
55
return command
66

77
func _close() -> String:
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
extends CommandTemplate
2+
3+
func create_command() -> Command:
4+
var command = Command.new("help", _get_help, [], "Get help for this console")
5+
return command
6+
7+
func _get_help() -> String:
8+
var config = Console.get_plugin_config()
9+
var section = config.get_sections()[0]
10+
var addon_name = config.get_value(section, "name")
11+
var author_name = config.get_value(section, "author")
12+
var version = config.get_value(section, "version")
13+
var return_data = "[center][b]%s[/b] by %s[/center]\n" % [addon_name, author_name] \
14+
+ "[center]%s[/center]\n" % version \
15+
+ _get_description() \
16+
+ "[center][url={\"type\": \"execute\", \"command\": \"list_commands\"}]List commands[/url][/center]"
17+
return return_data
18+
19+
func _get_description() -> String:
20+
return "[center]This addon does allow you to run built in or custom commands for your game.\n" \
21+
+ " If you need help run the [url={\"type\": \"enter\", \"command\": \"man\"}]man[/url] command followed by the command you need help with.\n" \
22+
+ " Also if text is underlined you might be able to click it, test the \"[u]List Commands[/u]\" below [/center]\n\n"

addons/gameconsole/builtin/command_list_commands.gd

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ func _list_commands() -> String:
88
var commands = Console.get_all_commands() as Array[Command]
99
var built_in = commands.filter(func(command): return command.built_in) as Array[Command]
1010
var custom = commands.filter(func(command): return !command.built_in) as Array[Command]
11-
var return_data = "[color=yellow]=== All commands ===[/color]\n"
12-
return_data += "== Built In ==\n"
11+
var return_data = "[color=yellow][b]All commands[/b][/color]\n"
12+
return_data += "[u]Built In[/u]\n"
1313
return_data += _generate_command_list(built_in)
14-
return_data += "\n== Custom ==\n"
14+
return_data += "\n[u]Custom[/u]\n"
1515
return_data += _generate_command_list(custom)
1616
return return_data
1717

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
extends CommandTemplate
22

33
func create_command() -> Command:
4-
var command = Command.new("man", manual, ["Name of the command"], "Command to get a manual for an command")
4+
var command = Command.new("man", manual, ["Name of the command"], "Command to get a manual for an command", "", ["man list_commands"])
55
return command
66

77
func manual(command_name: String) -> String:
88
var command = Console.get_specific_command(command_name)
9-
if command == null or command.built_in:
9+
if command == null or command.is_hidden:
1010
return "[color=red]No command with name %s was found[/color]" % command_name
1111
return command.get_man_page()

addons/gameconsole/builtin/command_pause.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
extends CommandTemplate
22

33
func create_command() -> Command:
4-
var command = Command.new("pause", _pause, [], "_pause the game")
4+
var command = Command.new("pause", _pause, [], "pause the game")
55
return command
66

77
func _pause() -> String:

addons/gameconsole/console/command/command.gd

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ func execute(arguments: Array) -> String:
3434
return data
3535

3636
func get_self_listed():
37-
var return_data = "- %s %s" % [get_command_name(), get_arguments()]
37+
var url_part = "[url={\"type\": \"man\", \"command\": \"%s\"}]" % command
38+
var return_data = "- %s%s %s[/url]" % [url_part, get_command_name(), get_arguments()]
3839
if short_description != "":
3940
return_data += " => %s" % short_description
4041

@@ -54,18 +55,20 @@ func as_stripped() -> StrippedCommand:
5455
return return_data
5556

5657
func get_man_page() -> String:
57-
var return_text = "== %s ==\n" % command
58+
var command_url = "[url={\"type\": \"enter\", \"command\": \"%s\"}]" % command
59+
var return_text = "%s[b]%s[/b][/url]\n\n" % [command_url, command]
5860
var description_to_show = description
5961
if description_to_show == "":
6062
description_to_show = short_description
6163
return_text += "%s\n" % description_to_show
6264
if arguments.size() > 0:
63-
return_text += "= Arguments =\n"
65+
return_text += "\n[i][b]Arguments[/b][/i]\n"
6466
for argument in arguments:
6567
return_text += "- %s\n" % argument
6668
if examples.size() > 0:
67-
return_text += "= Examples =\n"
69+
return_text += "\n[i][b]Examples[/b][/i]\n"
6870
for example in examples:
69-
return_text += "- %s\n" % example
71+
var example_url = "[url={\"type\": \"enter\", \"command\": \"%s\"}]" % example
72+
return_text += "- %s%s[/url]\n" % [example_url, example]
7073

7174
return return_text

addons/gameconsole/console/console.gd

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ signal console_open()
55

66
signal console_output(text: String)
77
signal is_current_command_valid(confirmed: bool)
8+
signal copy_command_to_input(command: String)
89

910
@onready var console_template: PackedScene = preload("res://addons/gameconsole/console/console/default_console_template.tscn")
11+
var config_file = "res://addons/gameconsole/plugin.cfg"
12+
var _plugin_config: ConfigFile = null
1013

1114
var _console_commands := {}
1215
var _used_template: PackedScene
@@ -20,12 +23,19 @@ var _last_state: bool = false
2023

2124
var _stored_console_content: String = ""
2225
var _console_key: int = KEY_QUOTELEFT
26+
var _first_time_open: bool = true
2327

2428
func _ready():
2529
_preregister_commands()
2630
add_child(_overlay_node)
2731
_used_template = console_template
2832
process_mode = PROCESS_MODE_ALWAYS
33+
_plugin_config = ConfigFile.new()
34+
if _plugin_config.load(config_file) != OK:
35+
printerr("addon config not loaded correctly")
36+
37+
func get_plugin_config() -> ConfigFile:
38+
return _plugin_config
2939

3040
func should_pause_on_open(pause: bool):
3141
_should_pause = pause
@@ -60,6 +70,8 @@ func show_console():
6070
template.store_content.connect(_store_console_content)
6171
template.clear_output.connect(_clear_stored_console_content)
6272
template.confirm_command.connect(_check_command)
73+
template.url_meta_requested.connect(url_requested)
74+
copy_command_to_input.connect(template.force_set_input)
6375
is_current_command_valid.connect(template.command_valid)
6476
console_output.connect(template.add_console_output)
6577
_overlay_node.add_child(template)
@@ -68,6 +80,9 @@ func show_console():
6880
if _should_pause:
6981
search_and_execute_command("pause")
7082
_console_shown = true
83+
if _first_time_open:
84+
search_and_execute_command("help")
85+
_first_time_open = false
7186
console_open.emit()
7287

7388
func hide_console():
@@ -87,6 +102,7 @@ func _store_console_content(text: String):
87102

88103
func _clear_stored_console_content():
89104
_stored_console_content = ""
105+
search_and_execute_command("help")
90106

91107
func _check_command(text: String):
92108
var executer = CommandDefinition.new(text)
@@ -215,3 +231,12 @@ func get_specific_command(command_name: String) -> Command:
215231
if !_console_commands.has(command_name):
216232
return null
217233
return _console_commands[command_name]
234+
235+
func url_requested(data: Dictionary):
236+
match data.type:
237+
"man":
238+
search_and_execute_command("man %s" % data.command)
239+
"enter":
240+
copy_command_to_input.emit(data.command)
241+
"execute":
242+
search_and_execute_command(data.command)

addons/gameconsole/console/console/console_template.gd

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ signal output_append(text: String)
55
signal store_content(text: String)
66
signal confirm_command(text: String)
77
signal is_command_valid(confirmed: bool)
8+
signal url_meta_requested(data: Dictionary)
9+
signal set_input(command: String)
810
signal clear_output()
911
signal clear_input()
1012

@@ -64,4 +66,16 @@ func check_if_is_valid_command(text: String):
6466
confirm_command.emit(text)
6567

6668
func command_valid(confirmed: bool):
67-
is_command_valid.emit(confirmed)
69+
is_command_valid.emit(confirmed)
70+
71+
func force_set_input(command: String):
72+
set_input.emit(command)
73+
74+
func url_requested(data):
75+
var json_parser = JSON.new()
76+
var parsed_json = json_parser.parse(data)
77+
if parsed_json != OK:
78+
Console.print_as_error("url data was not correctly parsed")
79+
return
80+
var dictionary = json_parser.get_data()
81+
url_meta_requested.emit(dictionary)

addons/gameconsole/console/console/default_console_template.tscn

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,10 @@ script = ExtResource("4_jxlgx")
8282
[connection signal="clear_output" from="." to="PanelContainer/MarginContainer/VBoxContainer/ConsoleOutput" method="clear_stored_text"]
8383
[connection signal="is_command_valid" from="." to="PanelContainer/MarginContainer/VBoxContainer/VBoxContainer/HBoxContainer/ConsoleInput" method="is_command_valid"]
8484
[connection signal="output_append" from="." to="PanelContainer/MarginContainer/VBoxContainer/ConsoleOutput" method="append_bbcode_text"]
85-
[connection signal="text_changed" from="PanelContainer/MarginContainer/VBoxContainer/VBoxContainer/HBoxContainer/ConsoleInput" to="." method="check_if_is_valid_command"]
85+
[connection signal="set_input" from="." to="PanelContainer/MarginContainer/VBoxContainer/VBoxContainer/HBoxContainer/ConsoleInput" method="autocomplete_accepted"]
86+
[connection signal="meta_clicked" from="PanelContainer/MarginContainer/VBoxContainer/ConsoleOutput" to="." method="url_requested"]
8687
[connection signal="text_changed" from="PanelContainer/MarginContainer/VBoxContainer/VBoxContainer/HBoxContainer/ConsoleInput" to="." method="autocomplete_requested"]
88+
[connection signal="text_changed" from="PanelContainer/MarginContainer/VBoxContainer/VBoxContainer/HBoxContainer/ConsoleInput" to="." method="check_if_is_valid_command"]
8789
[connection signal="text_changed" from="PanelContainer/MarginContainer/VBoxContainer/VBoxContainer/HBoxContainer/ConsoleInput" to="PanelContainer/MarginContainer/VBoxContainer/VBoxContainer/ConsoleAutocomplete" method="text_updated"]
8890
[connection signal="text_submitted" from="PanelContainer/MarginContainer/VBoxContainer/VBoxContainer/HBoxContainer/ConsoleInput" to="." method="request_command"]
8991
[connection signal="autocomplete_accepted" from="PanelContainer/MarginContainer/VBoxContainer/VBoxContainer/ConsoleAutocomplete" to="PanelContainer/MarginContainer/VBoxContainer/VBoxContainer/HBoxContainer/ConsoleInput" method="autocomplete_accepted"]

0 commit comments

Comments
 (0)