From afefd9b1daebe35a88970c9c4881b9df144283bf Mon Sep 17 00:00:00 2001 From: redstrate <54911369+redstrate@users.noreply.github.com> Date: Sun, 20 Sep 2020 22:37:15 -0400 Subject: [PATCH] Add framework for console commands --- engine/core/CMakeLists.txt | 4 +++- engine/core/include/console.hpp | 33 +++++++++++++++++++++++++ engine/core/src/console.cpp | 40 +++++++++++++++++++++++++++++++ engine/core/src/engine.cpp | 7 ++++++ engine/core/src/imguilayer.cpp | 4 +--- engine/utility/CMakeLists.txt | 2 ++ tools/common/src/commoneditor.cpp | 11 +++++++++ 7 files changed, 97 insertions(+), 4 deletions(-) create mode 100755 engine/core/include/console.hpp create mode 100644 engine/core/src/console.cpp diff --git a/engine/core/CMakeLists.txt b/engine/core/CMakeLists.txt index f275ce0..12ab62c 100755 --- a/engine/core/CMakeLists.txt +++ b/engine/core/CMakeLists.txt @@ -21,6 +21,7 @@ set(SRC include/platform.hpp include/file.hpp include/imgui_utility.hpp + include/console.hpp src/file.cpp src/engine.cpp @@ -29,7 +30,8 @@ set(SRC src/imguilayer.cpp src/screen.cpp src/scene.cpp - src/debug.cpp) + src/debug.cpp + src/console.cpp) if(NOT ENABLE_IOS AND NOT ENABLE_TVOS) set(EXTRA_LIBRARIES Audio) diff --git a/engine/core/include/console.hpp b/engine/core/include/console.hpp new file mode 100755 index 0000000..8dc232d --- /dev/null +++ b/engine/core/include/console.hpp @@ -0,0 +1,33 @@ +#pragma once + +#include +#include +#include +#include + +namespace console { + using ConsoleArgument = std::variant; + + enum class ArgType { + String, + Integer, + Boolean + }; + + struct Arguments { + std::vector arguments; + }; + + using FunctionPtr = std::function; + + struct ArgumentFormat { + ArgumentFormat(const int num_arguments) : num_arguments(num_arguments) {} + + int num_arguments = 0; + std::vector argument_types; + }; + + void register_command(const std::string_view name, const ArgumentFormat expected_format, const FunctionPtr function); + + void invoke_command(const std::string_view name, const Arguments arguments); +} diff --git a/engine/core/src/console.cpp b/engine/core/src/console.cpp new file mode 100644 index 0000000..1a1c89d --- /dev/null +++ b/engine/core/src/console.cpp @@ -0,0 +1,40 @@ +#include "console.hpp" + +#include +#include + +#include "log.hpp" + +struct RegisteredCommand { + RegisteredCommand(const console::ArgumentFormat format, const console::FunctionPtr function) : expected_format(format), function(function) {} + + console::ArgumentFormat expected_format; + std::function function; +}; + +static std::unordered_map registered_commands; + +void console::register_command(const std::string_view name, const ArgumentFormat expected_format, const console::FunctionPtr function) { + registered_commands.try_emplace(name.data(), RegisteredCommand(expected_format, function)); +} + +void console::invoke_command(const std::string_view name, const Arguments arguments) { + for(auto& [command_name, command_data] : registered_commands) { + if(command_name == name) { + bool invalid_format = false; + + if(arguments.arguments.size() != command_data.expected_format.num_arguments) + invalid_format = true; + + if(invalid_format) { + console::info(System::Core, "Invalid command format!"); + } else { + command_data.function(console::Arguments()); + } + + return; + } + } + + console::info(System::Core, "{} is not a valid command!", name.data()); +} diff --git a/engine/core/src/engine.cpp b/engine/core/src/engine.cpp index 839fd3d..f2c4ca1 100755 --- a/engine/core/src/engine.cpp +++ b/engine/core/src/engine.cpp @@ -16,10 +16,17 @@ #include "json_conversions.hpp" #include "debug.hpp" #include "assertions.hpp" +#include "console.hpp" Engine::Engine(const int argc, char* argv[]) { console::info(System::Core, "Prism Engine loading..."); + console::register_command("test_cmd", console::ArgumentFormat(0), [](const console::Arguments arguments) { + console::info(System::Core, "Test cmd!"); + }); + + console::invoke_command("test_cmd", console::Arguments()); + for(int i = 0; i < argc; i++) command_line_arguments.push_back(argv[i]); diff --git a/engine/core/src/imguilayer.cpp b/engine/core/src/imguilayer.cpp index e648923..e1b2350 100755 --- a/engine/core/src/imguilayer.cpp +++ b/engine/core/src/imguilayer.cpp @@ -195,9 +195,7 @@ void ImGuiLayer::process_key_down(unsigned int keyCode) { Expects(keyCode >= 0); ImGuiIO& io = ImGui::GetIO(); - - if(keyCode != 52) - io.AddInputCharactersUTF8(platform::translate_keycode(keyCode)); + io.AddInputCharactersUTF8(platform::translate_keycode(keyCode)); io.KeysDown[keyCode] = true; } diff --git a/engine/utility/CMakeLists.txt b/engine/utility/CMakeLists.txt index ec34d3e..ffb09fe 100755 --- a/engine/utility/CMakeLists.txt +++ b/engine/utility/CMakeLists.txt @@ -5,6 +5,8 @@ set(SRC include/timer.hpp include/common.hpp include/aabb.hpp + include/path.hpp + include/format.hpp src/string_utils.cpp) diff --git a/tools/common/src/commoneditor.cpp b/tools/common/src/commoneditor.cpp index 9f705f8..cc7a9ee 100755 --- a/tools/common/src/commoneditor.cpp +++ b/tools/common/src/commoneditor.cpp @@ -19,6 +19,7 @@ #include "gfx_commandbuffer.hpp" #include "imgui_utility.hpp" #include "screen.hpp" +#include "console.hpp" const std::map imToPl = { {ImGuiKey_Tab, InputButton::Tab}, @@ -1028,6 +1029,16 @@ GFXTexture* CommonEditor::generate_common_preview(Scene& scene, const Vector3 ca } void CommonEditor::drawConsole() { + static std::string command_buffer; + ImGui::InputText("Command", &command_buffer); + + ImGui::SameLine(); + + if(ImGui::Button("Run")) { + console::invoke_command(command_buffer, console::Arguments()); + command_buffer.clear(); + } + ImGui::BeginChild("console_output", ImVec2(-1, -1), true); for(const auto& message : console::stored_output)