Skip to content

Commit

Permalink
make console more flexible
Browse files Browse the repository at this point in the history
  • Loading branch information
redthing1 committed Sep 2, 2024
1 parent be1cb81 commit a3e0cee
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 25 deletions.
3 changes: 3 additions & 0 deletions demo/nuidemo/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -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()]);
}
Expand Down
17 changes: 17 additions & 0 deletions demo/nuidemo/source/gui_scene.d
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,37 @@ import re.gfx;
import re.math;
import std.format;
import re.util.interop;
import re.ng.diag.console;

import gui_root;

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;

// 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() {
Expand Down
47 changes: 33 additions & 14 deletions source/re/ng/diag/console.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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() {
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 0 additions & 11 deletions source/re/ng/diag/default_inspect_commands.d
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit a3e0cee

Please sign in to comment.