Add Console Command List Data Structure

This commit is contained in:
2025-02-12 22:34:47 -05:00
parent dd6a284b6f
commit 01edeb9e37
2 changed files with 113 additions and 27 deletions

View File

@@ -20,6 +20,16 @@ namespace CaveGame::ClientApp {
using CaveGame::Client::Scene;
using CaveGame::Client::SceneManager;
using CommandArgs = std::vector<std::string>;
struct Command
{
std::string name;
std::vector<std::string> aliases;
std::function<void(CommandArgs)> callback;
};
/// The main program class. Everything originates here.
/// This class is derived from RWindow class.
class CaveGameWindow : public ReWindow::OpenGLWindow, public SceneManager {
@@ -45,43 +55,26 @@ namespace CaveGame::ClientApp {
/// Constructs and returns an instance of the game program.
/// @param title
CaveGameWindow(const std::string& title, int width, int height);
~CaveGameWindow();
Vector2 GetMouseV2() const;
Vector2 GetSizeV2() const
{
auto isize = GetSize();
return Vector2(isize.x, isize.y);
}
Vector2 GetSizeV2() const;
bool InGame() const;
/// This function runs the totality of the game loop, start to finish.
/// @note The game loop runs until Die() is called, at which point final cleanup is performed.
void Run();
/// This function triggers an internal signal that the game window is ready to close.
/// @note This does not immediately close the window, rather, time is given to allow for final data saving and other processes.
void Die();
/// This function sets up the initial program state, particularly that which must be performed **after** the window is opened.
void Init();
void tile_draw(int x, int y, int radius, Core::TileID tile);
/// This function cleans up any data or assets before closing the game.
void Cleanup();
/// This function performs logic calculation routines, once per refresh.
void Update(float elapsed);
/// This function performs rendering routines, once per refresh.
void Draw();
public:
void OnRefresh(float elapsed) override;
void OnMouseButtonDown(const ReWindow::MouseButtonDownEvent &ev) override;
@@ -91,7 +84,6 @@ namespace CaveGame::ClientApp {
void OnKeyDown(const ReWindow::KeyDownEvent& ev) override;
void OnMouseMove(const ReWindow::MouseMoveEvent &ev) override;
bool OnResizeRequest(const ReWindow::WindowResizeRequestEvent &ev) override;
void OnClosing() override;
protected:
void create_tool_window();
@@ -99,7 +91,6 @@ namespace CaveGame::ClientApp {
void create_stats_window();
void create_settings_window();
void create_window_widgets();
/// Draws a sequence of 'debug' information strings to the screen, in a descending list.
void draw_debug_info(std::vector<std::string> tokens);
void CreateMenuWindows();
@@ -114,5 +105,42 @@ namespace CaveGame::ClientApp {
float tool_percent = 100.f;
bool wanna_die = false; // This field indicates the program is ready to close.
void DrawTileToolOverlayCursor();
/// Forwards a message to the console log.
void Log(const std::string& msg, const Color4& color = Colors::White);
bool HelpCommand(const CommandArgs& args);
bool ListCommand(const CommandArgs& args);
// What is this dog shit?
std::vector<Command> commands {
{"help", {}, std::bind(&CaveGameWindow::HelpCommand, this, std::placeholders::_1)},
{"list", {}, [this](auto args) { return ListCommand(args); }},
{"quit", {"q"}, std::bind(&CaveGameWindow::HelpCommand, this, std::placeholders::_1)},
{"worldedit", {"q"}, [this](auto args) { tile_tool->Enable(!tile_tool->IsEnabled()); return true;}},
{"tilesim", {"q"}, std::bind(&CaveGameWindow::HelpCommand, this, std::placeholders::_1)},
{"grid", {"q"}, std::bind(&CaveGameWindow::HelpCommand, this, std::placeholders::_1)},
{"credits", {"q"}, std::bind(&CaveGameWindow::HelpCommand, this, std::placeholders::_1)},
{"settings", {"q"}, std::bind(&CaveGameWindow::HelpCommand, this, std::placeholders::_1)},
{"spawn", {"q"}, std::bind(&CaveGameWindow::HelpCommand, this, std::placeholders::_1)},
{"settings", {"q"}, std::bind(&CaveGameWindow::HelpCommand, this, std::placeholders::_1)},
{"give", {"q"}, std::bind(&CaveGameWindow::HelpCommand, this, std::placeholders::_1)},
{"tp", {"q"}, std::bind(&CaveGameWindow::HelpCommand, this, std::placeholders::_1)},
{"freecam", {"q"}, std::bind(&CaveGameWindow::HelpCommand, this, std::placeholders::_1)},
{"noclip", {"q"}, std::bind(&CaveGameWindow::HelpCommand, this, std::placeholders::_1)},
{"god", {"q"}, std::bind(&CaveGameWindow::HelpCommand, this, std::placeholders::_1)},
{"fullbright", {"q"}, std::bind(&CaveGameWindow::HelpCommand, this, std::placeholders::_1)},
{"connect", {"q"}, std::bind(&CaveGameWindow::HelpCommand, this, std::placeholders::_1)},
{"disconnect", {"q"}, std::bind(&CaveGameWindow::HelpCommand, this, std::placeholders::_1)},
{"server", {"q"}, std::bind(&CaveGameWindow::HelpCommand, this, std::placeholders::_1)},
{"sandbox", {"q"}, std::bind(&CaveGameWindow::HelpCommand, this, std::placeholders::_1)},
{"crash", {"q"}, std::bind(&CaveGameWindow::HelpCommand, this, std::placeholders::_1)},
{"time", {"q"}, std::bind(&CaveGameWindow::HelpCommand, this, std::placeholders::_1)},
{"jui_debug", {"q"}, std::bind(&CaveGameWindow::HelpCommand, this, std::placeholders::_1)},
{"jui_debug", {"q"}, std::bind(&CaveGameWindow::HelpCommand, this, std::placeholders::_1)},
};
bool HasCommand(const std::string &command);
Command GetCommand(const std::string &command);
};
}

View File

@@ -238,7 +238,7 @@ namespace CaveGame::ClientApp
// Temp hack until Input Focus Priority is implemented.
for (auto window : wm->GetChildren())
if (window->IsMouseInside())
if (window->IsVisible() && window->IsMouseInside())
return;
if (IsMouseButtonDown(MouseButtons::Left))
@@ -549,6 +549,8 @@ namespace CaveGame::ClientApp
wanna_die = true;
}
using namespace std::placeholders;
std::string str_tolower(const std::string& s)
{
std::string copy = s;
@@ -556,16 +558,49 @@ namespace CaveGame::ClientApp
return copy;
}
void CaveGameWindow::OnConsoleCommandInput(const std::string& command) {
auto tokens = string_split(command, ' ');
bool CaveGameWindow::HasCommand(const std::string& command)
{
for (const Command& c : commands) {
if (c.name == command)
return true;
for(const std::string& alias : c.aliases)
if (alias == command)
return true;
}
return false;
}
Command CaveGameWindow::GetCommand(const std::string& command) {
for (Command c : commands) {
if (c.name == command)
return c;
}
}
void CaveGameWindow::OnConsoleCommandInput(const std::string& command_sequence) {
auto tokens = string_split(command_sequence, ' ');
if (tokens.empty())
return;
// Commands that are single length argument.
if (tokens.size() == 1) {
if (str_tolower(tokens[0]) == "worldedit")
tile_tool->Enable(!tile_tool->IsEnabled());
std::string command_str = str_tolower(tokens[0]);
if (!HasCommand(command_str)) {
console_window->Log("Invalid command!");
return;
}
Command cmd = GetCommand(command_str);
cmd.callback(tokens);
}
void CaveGameWindow::create_console_window() {
@@ -587,6 +622,29 @@ namespace CaveGame::ClientApp
return Vector2(coords.x, coords.y);
}
Vector2 CaveGameWindow::GetSizeV2() const {
auto isize = GetSize();
return Vector2(isize.x, isize.y);
}
void CaveGameWindow::Log(const std::string &msg, const Color4 &color) {
console_window->Log(msg, color);
}
bool CaveGameWindow::HelpCommand(const CommandArgs &args) {
return true;
}
bool CaveGameWindow::ListCommand(const CommandArgs &args) {
for (Command command: commands)
{
Log(std::format("{}", command.name));
}
return true;
}
}