diff --git a/demo/nuidemo/source/app.d b/demo/nuidemo/source/app.d index 4cbe0e9..39aad6a 100644 --- a/demo/nuidemo/source/app.d +++ b/demo/nuidemo/source/app.d @@ -24,6 +24,9 @@ class Game : Core { } override void initialize() { + // we will use a custom console + this.inspector_overlay.console.reset(); + content.paths ~= ["./content/", "./res/"]; load_scenes([new GuiScene()]); } diff --git a/demo/nuidemo/source/gui_scene.d b/demo/nuidemo/source/gui_scene.d index ff63d7d..36a88e0 100644 --- a/demo/nuidemo/source/gui_scene.d +++ b/demo/nuidemo/source/gui_scene.d @@ -5,6 +5,7 @@ import re.gfx; import re.math; import std.format; import re.util.interop; +import re.ng.diag.console; import gui_root; @@ -12,6 +13,8 @@ static import raylib; static import raygui; class GuiScene : Scene2D { + size_t yoop_counter = 0; + override void on_start() { clear_color = Colors.RAYWHITE; viewports[0].sync_maximized = true; @@ -19,6 +22,20 @@ class GuiScene : Scene2D { // add gui root auto ui_root = create_entity("ui_root", Vector2.zero); ui_root.add_component!GuiRoot(); + + // set up console commands + yoop_counter = 0; + Core.inspector_overlay.console.add_command(ConsoleCommand("yoop", &yoop, "yoop yoop yoop")); + } + + override void on_unload() { + // reset the console + Core.inspector_overlay.console.reset(); + } + + void yoop(string[] args) { + Core.log.info("yoop %s", yoop_counter); + yoop_counter++; } override void update() { diff --git a/source/re/ng/diag/console.d b/source/re/ng/diag/console.d index 924ba5f..5500580 100644 --- a/source/re/ng/diag/console.d +++ b/source/re/ng/diag/console.d @@ -7,6 +7,8 @@ import std.string; import std.array; import std.range; import std.format; +import std.functional; +import std.algorithm : sort; import re.input.input; import re.core; @@ -16,6 +18,13 @@ import re.ng.diag.default_inspect_commands; import re.util.interop; static import raygui; +/// represents a command for the debug console +public struct ConsoleCommand { + string name; + void delegate(string[]) action; + string help; +} + /// overlay debug console class InspectorConsole { /// the key that opens the console @@ -28,34 +37,32 @@ class InspectorConsole { private enum blank = "\0 "; /// console commands - public Command[string] commands; + public ConsoleCommand[string] commands; private enum height = 30; private char* console_text; private Appender!(string[]) _history; private int _history_depth = 0; - /// represents a command for the debug console - public struct Command { - string name; - void function(string[]) action; - string help; - } - /// create a debug console this() { console_text = blank.c_str(); } + private void add_builtin_commands() { + add_command(ConsoleCommand("help", &cmd_help, "lists available commands")); + } + public void add_default_inspector_commands() { - add_command(Command("help", &DefaultEntityInspectorCommands.c_help, "lists available commands")); - add_command(Command("entities", &DefaultEntityInspectorCommands.c_entities, "lists scene entities")); - add_command(Command("dump", &DefaultEntityInspectorCommands.c_dump, "dump a component")); - add_command(Command("inspect", &DefaultEntityInspectorCommands.c_inspect, + add_command(ConsoleCommand("entities", toDelegate( + &DefaultEntityInspectorCommands.c_entities), "lists scene entities")); + add_command(ConsoleCommand("dump", toDelegate(&DefaultEntityInspectorCommands.c_dump), "dump a component")); + add_command(ConsoleCommand("inspect", toDelegate(&DefaultEntityInspectorCommands.c_inspect), "open the inspector on a component")); } public void reset_commands() { commands.clear(); + add_builtin_commands(); } public void reset_history() { @@ -68,10 +75,21 @@ class InspectorConsole { reset_history(); } - public void add_command(Command command) { + public void add_command(ConsoleCommand command) { commands[command.name] = command; } + private void cmd_help(string[] args) { + auto sb = appender!string(); + sb ~= "available commmands:\n"; + auto command_names = commands.keys.sort(); + foreach (command_name; command_names) { + auto command = commands[command_name]; + sb ~= format("%s - %s\n", command.name, command.help); + } + Core.log.info(sb.data); + } + public void update() { // remove all instances of c from str void sstrip(char* str, char c) { @@ -111,7 +129,8 @@ class InspectorConsole { public void render() { alias pad = Core.inspector_overlay.screen_padding; - auto screen_br = Vector2(Core.inspector_overlay.ui_bounds.width, Core.inspector_overlay.ui_bounds.height); + auto screen_br = Vector2(Core.inspector_overlay.ui_bounds.width, Core + .inspector_overlay.ui_bounds.height); // Core.log.info(format("screen_br: (%s", screen_br)); auto console_bg_bounds = Rectangle(pad, screen_br.y - pad - height, screen_br.x - pad * 2, height); diff --git a/source/re/ng/diag/default_inspect_commands.d b/source/re/ng/diag/default_inspect_commands.d index d4b689e..22971d2 100644 --- a/source/re/ng/diag/default_inspect_commands.d +++ b/source/re/ng/diag/default_inspect_commands.d @@ -17,17 +17,6 @@ static class DefaultEntityInspectorCommands { alias dbg = Core.inspector_overlay; alias con = dbg.console; - static void c_help(string[] args) { - auto sb = appender!string(); - sb ~= "available commmands:\n"; - auto command_names = con.commands.keys.sort(); - foreach (command_name; command_names) { - auto command = con.commands[command_name]; - sb ~= format("%s - %s\n", command.name, command.help); - } - log.info(sb.data); - } - static void c_entities(string[] args) { auto sb = appender!string(); sb ~= "entity list:\n";