Skip to content

Commit

Permalink
Implemented base console variables
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunkTreasure1 committed Dec 29, 2023
1 parent 691b6b7 commit a474e8b
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 16 deletions.
68 changes: 67 additions & 1 deletion Volt/Sandbox/src/Sandbox/Window/LogPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

#include <Volt/Log/Log.h>
#include <Volt/Utility/UIUtility.h>
#include <Volt/Utility/StringUtility.h>

#include <Volt/Console/ConsoleVariableRegistry.h>

namespace Utility
{
Expand Down Expand Up @@ -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<const float*>(variable->Get()));
}
else if (variable->IsInteger())
{
message += std::to_string(*static_cast<const int32_t*>(variable->Get()));
}
else if (variable->IsString())
{
message += *static_cast<const std::string*>(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)
Expand Down
56 changes: 41 additions & 15 deletions Volt/Volt/src/Volt/Console/ConsoleVariableRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,38 @@ 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<typename T>
template<typename T>
concept ValidConsoleVariableType = std::is_same_v<T, int32_t> || std::is_same_v<T, float> || std::is_same_v<T, std::string>;

template<ValidConsoleVariableType T>
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<T>; }
[[nodiscard]] inline constexpr bool IsFloat() const override { return std::is_floating_point_v<T>; }
[[nodiscard]] inline constexpr bool IsString() const override { return std::is_same_v<T, std::string>; }

private:
T m_value;
std::string_view m_variableName;
std::string_view m_description;
};

template<typename T>
template<ValidConsoleVariableType T>
class ConsoleVariable
{
public:
Expand All @@ -58,7 +69,7 @@ namespace Volt
Weak<RegisteredConsoleVariable<T>> m_variableReference;
};

template<typename T>
template<ValidConsoleVariableType T>
class ConsoleVariableRef
{
public:
Expand All @@ -84,43 +95,53 @@ namespace Volt
class ConsoleVariableRegistry
{
public:
template<typename T>
template<ValidConsoleVariableType T>
static Weak<RegisteredConsoleVariable<T>> RegisterVariable(std::string_view variableName, const T& defaultValue, std::string_view description);

template<typename T>
template<ValidConsoleVariableType T>
static Weak<RegisteredConsoleVariable<T>> FindVariable(std::string_view variableName);

inline static Weak<RegisteredConsoleVariableBase> GetVariable(std::string_view variableName);
inline static bool VariableExists(const std::string& variableName);

static std::unordered_map<std::string_view, Ref<RegisteredConsoleVariableBase>>& GetRegisteredVariables() { return s_registeredVariables; }

private:
ConsoleVariableRegistry() = delete;

inline static std::unordered_map<std::string_view, Ref<RegisteredConsoleVariableBase>> s_registeredVariables;
};

template<typename T>
inline bool ConsoleVariableRegistry::VariableExists(const std::string& variableName)
{
return s_registeredVariables.contains(std::string_view(variableName));
}

template<ValidConsoleVariableType T>
inline RegisteredConsoleVariable<T>::RegisteredConsoleVariable(std::string_view variableName, const T& defaultValue, std::string_view description)
: m_value(defaultValue)
{
}

template<typename T>
template<ValidConsoleVariableType T>
inline const void* RegisteredConsoleVariable<T>::Get() const
{
return reinterpret_cast<const void*>(&m_value);
}

template<typename T>
template<ValidConsoleVariableType T>
inline void RegisteredConsoleVariable<T>::Set(const void* value)
{
m_value = *reinterpret_cast<const T*>(value);
}

template<typename T>
template<ValidConsoleVariableType T>
inline ConsoleVariable<T>::ConsoleVariable(std::string_view variableName, const T& defaultValue, std::string_view description)
{
m_variableReference = ConsoleVariableRegistry::RegisterVariable<T>(variableName, defaultValue, description);
}

template<typename T>
template<ValidConsoleVariableType T>
inline Weak<RegisteredConsoleVariable<T>> ConsoleVariableRegistry::RegisterVariable(std::string_view variableName, const T& defaultValue, std::string_view description)
{
Ref<RegisteredConsoleVariable<T>> consoleVariable = CreateRef<RegisteredConsoleVariable<T>>(variableName, defaultValue, description);
Expand All @@ -131,7 +152,7 @@ namespace Volt
return consoleVariable;
}

template<typename T>
template<ValidConsoleVariableType T>
inline Weak<RegisteredConsoleVariable<T>> ConsoleVariableRegistry::FindVariable(std::string_view variableName)
{
if (s_registeredVariables.contains(variableName))
Expand All @@ -142,10 +163,15 @@ namespace Volt
return Weak<RegisteredConsoleVariable<T>>();
}

template<typename T>
template<ValidConsoleVariableType T>
inline ConsoleVariableRef<T>::ConsoleVariableRef(std::string_view variableName)
{
m_variableReference = ConsoleVariableRegistry::FindVariable(variableName);
VT_CORE_ASSERT(m_variableReference, "Variable with name not found!");
}

inline Weak<RegisteredConsoleVariableBase> ConsoleVariableRegistry::GetVariable(std::string_view variableName)
{
return s_registeredVariables.at(variableName);
}
}

0 comments on commit a474e8b

Please sign in to comment.