diff --git a/Volt/Sandbox/src/Sandbox/Window/LogPanel.cpp b/Volt/Sandbox/src/Sandbox/Window/LogPanel.cpp index 1216446f4..7662c0e62 100644 --- a/Volt/Sandbox/src/Sandbox/Window/LogPanel.cpp +++ b/Volt/Sandbox/src/Sandbox/Window/LogPanel.cpp @@ -3,6 +3,9 @@ #include #include +#include + +#include namespace Utility { @@ -48,10 +51,73 @@ void LogPanel::UpdateMainContent() myCategories.emplace_back("Default"); } + ImGui::SameLine(); + ImGui::PushItemWidth(200.f); + + static std::string commandStr; + + if (ImGui::InputTextWithHintString("##commandLine", "Command...", &commandStr, ImGuiInputTextFlags_EnterReturnsTrue)) + { + auto strings = Utility::SplitStringsByCharacter(commandStr, ' '); + if (!strings.empty()) + { + if (Volt::ConsoleVariableRegistry::VariableExists(strings[0])) + { + auto variable = Volt::ConsoleVariableRegistry::GetVariable(strings[0]); + + std::string message = strings[0] + " = "; + + if (strings.size() > 1) + { + if (variable->IsFloat()) + { + const float value = std::stof(strings[1]); + variable->Set(&value); + } + else if (variable->IsInteger()) + { + const int32_t value = std::stoi(strings[1]); + variable->Set(&value); + } + else if (variable->IsString()) + { + variable->Set(&strings[1]); + } + + message += strings[1]; + } + else + { + if (variable->IsFloat()) + { + message += std::to_string(*static_cast(variable->Get())); + } + else if (variable->IsInteger()) + { + message += std::to_string(*static_cast(variable->Get())); + } + else if (variable->IsString()) + { + message += *static_cast(variable->Get()); + } + + } + + VT_CORE_TRACE(message); + } + else + { + VT_CORE_TRACE("Command {0} not found!", strings[0]); + } + } + + commandStr = ""; + } + + ImGui::SameLine(); static int32_t logLevel = 0; - ImGui::PushItemWidth(200.f); if (ImGui::Combo("##level", &logLevel, "Trace\0Info\0Warning\0Error\0Critical")) { switch (logLevel) diff --git a/Volt/Volt/src/Volt/Console/ConsoleVariableRegistry.h b/Volt/Volt/src/Volt/Console/ConsoleVariableRegistry.h index 6764eef0a..669763d0a 100644 --- a/Volt/Volt/src/Volt/Console/ConsoleVariableRegistry.h +++ b/Volt/Volt/src/Volt/Console/ConsoleVariableRegistry.h @@ -15,19 +15,30 @@ namespace Volt virtual std::string_view GetName() const = 0; virtual std::string_view GetDescription() const = 0; + + virtual bool IsInteger() const = 0; + virtual bool IsFloat() const = 0; + virtual bool IsString() const = 0; }; - template + template + concept ValidConsoleVariableType = std::is_same_v || std::is_same_v || std::is_same_v; + + template class RegisteredConsoleVariable : public RegisteredConsoleVariableBase { public: RegisteredConsoleVariable(std::string_view variableName, const T& defaultValue, std::string_view description); - const void* Get() const override; + [[nodiscard]] const void* Get() const override; void Set(const void* value) override; - std::string_view GetName() const override { return m_variableName; } - std::string_view GetDescription() const override { return m_description; } + [[nodiscard]] inline std::string_view GetName() const override { return m_variableName; } + [[nodiscard]] inline std::string_view GetDescription() const override { return m_description; } + + [[nodiscard]] inline constexpr bool IsInteger() const override { return std::is_integral_v; } + [[nodiscard]] inline constexpr bool IsFloat() const override { return std::is_floating_point_v; } + [[nodiscard]] inline constexpr bool IsString() const override { return std::is_same_v; } private: T m_value; @@ -35,7 +46,7 @@ namespace Volt std::string_view m_description; }; - template + template class ConsoleVariable { public: @@ -58,7 +69,7 @@ namespace Volt Weak> m_variableReference; }; - template + template class ConsoleVariableRef { public: @@ -84,43 +95,53 @@ namespace Volt class ConsoleVariableRegistry { public: - template + template static Weak> RegisterVariable(std::string_view variableName, const T& defaultValue, std::string_view description); - template + template static Weak> FindVariable(std::string_view variableName); + inline static Weak GetVariable(std::string_view variableName); + inline static bool VariableExists(const std::string& variableName); + static std::unordered_map>& GetRegisteredVariables() { return s_registeredVariables; } private: + ConsoleVariableRegistry() = delete; + inline static std::unordered_map> s_registeredVariables; }; - template + inline bool ConsoleVariableRegistry::VariableExists(const std::string& variableName) + { + return s_registeredVariables.contains(std::string_view(variableName)); + } + + template inline RegisteredConsoleVariable::RegisteredConsoleVariable(std::string_view variableName, const T& defaultValue, std::string_view description) : m_value(defaultValue) { } - template + template inline const void* RegisteredConsoleVariable::Get() const { return reinterpret_cast(&m_value); } - template + template inline void RegisteredConsoleVariable::Set(const void* value) { m_value = *reinterpret_cast(value); } - template + template inline ConsoleVariable::ConsoleVariable(std::string_view variableName, const T& defaultValue, std::string_view description) { m_variableReference = ConsoleVariableRegistry::RegisterVariable(variableName, defaultValue, description); } - template + template inline Weak> ConsoleVariableRegistry::RegisterVariable(std::string_view variableName, const T& defaultValue, std::string_view description) { Ref> consoleVariable = CreateRef>(variableName, defaultValue, description); @@ -131,7 +152,7 @@ namespace Volt return consoleVariable; } - template + template inline Weak> ConsoleVariableRegistry::FindVariable(std::string_view variableName) { if (s_registeredVariables.contains(variableName)) @@ -142,10 +163,15 @@ namespace Volt return Weak>(); } - template + template inline ConsoleVariableRef::ConsoleVariableRef(std::string_view variableName) { m_variableReference = ConsoleVariableRegistry::FindVariable(variableName); VT_CORE_ASSERT(m_variableReference, "Variable with name not found!"); } + + inline Weak ConsoleVariableRegistry::GetVariable(std::string_view variableName) + { + return s_registeredVariables.at(variableName); + } }