Add framework for console commands
This commit is contained in:
parent
b31534ab62
commit
afefd9b1da
7 changed files with 97 additions and 4 deletions
|
@ -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)
|
||||
|
|
33
engine/core/include/console.hpp
Executable file
33
engine/core/include/console.hpp
Executable file
|
@ -0,0 +1,33 @@
|
|||
#pragma once
|
||||
|
||||
#include <string_view>
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
#include <variant>
|
||||
|
||||
namespace console {
|
||||
using ConsoleArgument = std::variant<std::string, int, bool>;
|
||||
|
||||
enum class ArgType {
|
||||
String,
|
||||
Integer,
|
||||
Boolean
|
||||
};
|
||||
|
||||
struct Arguments {
|
||||
std::vector<ConsoleArgument> arguments;
|
||||
};
|
||||
|
||||
using FunctionPtr = std::function<void(console::Arguments)>;
|
||||
|
||||
struct ArgumentFormat {
|
||||
ArgumentFormat(const int num_arguments) : num_arguments(num_arguments) {}
|
||||
|
||||
int num_arguments = 0;
|
||||
std::vector<ArgType> 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);
|
||||
}
|
40
engine/core/src/console.cpp
Normal file
40
engine/core/src/console.cpp
Normal file
|
@ -0,0 +1,40 @@
|
|||
#include "console.hpp"
|
||||
|
||||
#include <unordered_map>
|
||||
#include <string>
|
||||
|
||||
#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<void(console::Arguments)> function;
|
||||
};
|
||||
|
||||
static std::unordered_map<std::string, RegisteredCommand> 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());
|
||||
}
|
|
@ -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]);
|
||||
|
||||
|
|
|
@ -195,8 +195,6 @@ 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.KeysDown[keyCode] = true;
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
#include "gfx_commandbuffer.hpp"
|
||||
#include "imgui_utility.hpp"
|
||||
#include "screen.hpp"
|
||||
#include "console.hpp"
|
||||
|
||||
const std::map<ImGuiKey, InputButton> 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)
|
||||
|
|
Reference in a new issue