Now drawing registred module settings in project settings

This commit is contained in:
antopilo
2025-04-02 00:23:40 -04:00
parent 68519dc28b
commit 5ae981db06
7 changed files with 180 additions and 10 deletions

View File

@@ -6,6 +6,7 @@
#include "../EditorInterface.h"
#include "../../Commands/Commands/Commands.h"
#include <Nuake/Audio/AudioManager.h>
#include <Nuake/Modules/ModuleDB.h>
ProjectSettingsCategoryWindowGeneral::ProjectSettingsCategoryWindowGeneral(Ref<Nuake::Project> project) :
m_Project(project)
@@ -56,6 +57,11 @@ void ProjectSettingsWindow::Init(Ref<Nuake::Project> project)
m_CategoryWindows.push_back(CreateRef<ProjectSettingsCategoryScripting>(project));
m_CategoryWindows.push_back(CreateRef<ProjectSettingsCategoryPhysics>(project));
m_CategoryWindows.push_back(CreateRef<ProjectSettingsCategoryAudio>(project));
for (auto& m : ModuleDB::Get().GetModules())
{
m_CategoryWindows.push_back(CreateRef<ProjectSettingsModuleWindow>(m));
}
}
void ProjectSettingsWindow::Draw()
@@ -197,6 +203,78 @@ ProjectSettingsCategoryAudio::ProjectSettingsCategoryAudio(Ref<Nuake::Project> p
Name = "Audio";
}
ProjectSettingsModuleWindow::ProjectSettingsModuleWindow(const std::string& inModuleName) :
moduleName(inModuleName)
{
Name = moduleName;
}
void ProjectSettingsModuleWindow::Draw()
{
auto meta = entt::resolve(entt::hashed_string(Name.c_str()));
for (auto [id, data] : meta.data())
{
auto propDisplayName = data.prop(HashedName::DisplayName);
if (propDisplayName)
{
auto propVal = propDisplayName.value();
const char* settingName = *propVal.try_cast<const char*>();
ImGui::Text(settingName);
}
}
for (auto [id, func] : meta.func())
{
auto propDisplayName = func.prop(HashedName::DisplayName);
if (propDisplayName)
{
auto propVal = propDisplayName.value();
const char* settingName = *propVal.try_cast<const char*>();
ImGui::Text(settingName);
}
// Setting
//std::string funcName = "UnknownFunc";
//if (propDisplayName)
//{
// funcName = std::string(*propDisplayName.value().try_cast<const char*>());
//}
//
//std::string msg = std::string(returnType) + " " + std::string(funcName) + "(";
//std::vector<std::string_view> args;
//
//if (func.arity() > 0)
//{
// auto propArgsName = func.prop(HashedName::ArgsName);
// if (propArgsName)
// {
// std::vector<std::string> argsName = *propArgsName.value().try_cast<std::vector<std::string>>();
// for (int i = 0; i < func.arity(); i++)
// {
// const std::string argType = std::string(func.arg(i).info().name());
// args.push_back(argType);
//
// msg += argType + " " + argsName[i];
//
// if (i < func.arity() - 1)
// {
// msg += ", ";
// }
// }
// }
//}
//
//
//msg += ")";
//
//Logger::Log(msg, "", VERBOSE);
}
}
void ProjectSettingsCategoryAudio::Draw()
{
float oldVolume = Engine::GetProject()->Settings.GlobalVolume;

View File

@@ -70,6 +70,17 @@ public:
void Draw() override;
};
class ProjectSettingsModuleWindow : public ProjectSettingsCategoryWindow
{
private:
std::string moduleName;
public:
ProjectSettingsModuleWindow(const std::string& inModuleName);
void Draw() override;
};
class ProjectSettingsWindow
{
private:

View File

@@ -24,6 +24,7 @@ namespace Nuake
NK_HASHED_STATIC_STR(AddToEntity)
NK_HASHED_STATIC_STR(RemoveFromEntity)
NK_HASHED_STATIC_STR(ActionName)
NK_HASHED_STATIC_STR(ModuleSettingName)
};
struct HashedFieldPropName

View File

@@ -15,20 +15,38 @@ void StopAudio()
}
float Volume = 1.0f;
void SetVolume(float volume)
{
Volume = volume;
}
bool Muted = false;
void SetMuted(bool muted)
{
Muted = muted;
}
NUAKEMODULE(AudioModule)
void AudioModule_Startup()
{
using namespace Nuake;
auto& module = ModuleDB::Get().RegisterModule<AudioModule>();
module.Name = "AudioModule";
module.Description = "Core audio module.";
module.RegisterSetting<&Volume>("Volume");
module.RegisterSetting<&Muted>("Muted");
module.BindFunction<PlayAudio>("PlayAudio", "id", "volume");
module.BindFunction<StopAudio>("StopAudio");
Logger::Log("AudioModule exposed API:", "Module", VERBOSE);
auto reflection = module.Resolve();
auto metaTypeid = entt::hashed_string(module.Name.c_str());
auto s = reflection.func(metaTypeid);
for (auto [id, func] : reflection.func())
{
const std::string_view returnType = func.ret().info().name();

View File

@@ -62,6 +62,8 @@ void ExampleModuleLog(const std::string& hi)
Nuake::Logger::Log(hi, "ExampleModule", Nuake::VERBOSE);
}
float mySetting = 1.0f;
NUAKEMODULE(ExampleModule)
void ExampleModule_Startup()
{
@@ -74,6 +76,8 @@ void ExampleModule_Startup()
module.Description = "This is an example module";
module.RegisterSetting<&mySetting>("Hello World!");
// This is to expose functions to the rest of the engine
module.BindFunction<ExampleFunction>("ExampleFunction");
module.BindFunction<ExampleModuleLog>("ExampleModuleLog", "hi");

View File

@@ -8,6 +8,22 @@
#include <entt/entt.hpp>
#include "Nuake/Core/Logger.h"
class ModuleInstance
{
public:
entt::meta_any instance;
std::string Name;
entt::meta_type Resolve()
{
Nuake::Logger::Log("Resolved!" + Name, "ModuleReflection", Nuake::LOG_TYPE::VERBOSE); \
return entt::resolve(entt::hashed_string(Name.c_str()));
}
};
class Class
{
protected:
@@ -82,16 +98,16 @@ public: \
#define NUAKEMODULE(moduleName) \
using TypeNameMap = std::map<entt::id_type, std::string>; \
\
struct moduleName \
class moduleName : public ModuleInstance \
{ \
entt::meta_any instance; \
const std::string Name = #moduleName; \
public: \
std::string Description = "No Description"; \
entt::meta_factory<moduleName> ModuleFactory = entt::meta<moduleName>(); \
TypeNameMap TypeNames; \
\
std::map<entt::id_type, std::vector<std::string>> FuncArgNames; \
\
inline static auto ClassFactory = entt::meta<moduleName>(); \
template<auto T, typename... Args> \
inline moduleName& BindFunction(const std::string& name, Args... argNames) \
{ \
@@ -101,11 +117,14 @@ struct moduleName \
ModuleFactory.func<T>(typeId); \
return *this; \
} \
\
auto Resolve() \
template<auto Func> \
static auto RegisterSetting(const char* settingName) \
{ \
return entt::resolve<moduleName>(); \
} \
return ClassFactory \
.data<Func>(entt::hashed_string(settingName)) \
.prop(Nuake::HashedName::DisplayName, settingName); \
} \
\
\
template<typename... Args> \
auto Invoke(const std::string& funcName, Args... args) \
@@ -166,8 +185,8 @@ namespace Nuake
template<typename T>
T& RegisterModule()
{
Modules[typeid(T).name()] = T();
return std::any_cast<T&>(Modules[typeid(T).name()]);
Modules[typeid(T).name()] = (ModuleInstance*)(new T());
return *(T*)std::any_cast<ModuleInstance*>(Modules[typeid(T).name()]);
}
template<typename T>
@@ -180,7 +199,40 @@ namespace Nuake
return;
}
return std::any_cast<T>(Modules[typeName]);
return *(T*)std::any_cast<ModuleInstance*>(Modules[typeName]);
}
entt::meta_type GetModuleMeta(const std::string& moduleName)
{
for (auto& [name, _] : Modules)
{
if (name == moduleName)
{
return std::any_cast<ModuleInstance*>(Modules[name])->Resolve();
}
}
//auto foundIt = std::find_if(Modules.begin(), Modules.end(), moduleName);
//if (foundIt != Modules.end())
//{
// return std::any_cast<ModuleInstance*>(foundIt->second)->Resolve();
//}
assert(false && "Module not found");
return entt::meta_type();
}
std::vector<std::string> GetModules()
{
std::vector<std::string> moduleNames;
moduleNames.reserve(Modules.size());
for(auto& [moduleName, _] : Modules)
{
moduleNames.push_back(moduleName);
}
return moduleNames;
}
private:

View File

@@ -1,9 +1,15 @@
#pragma once
#include <map>
#include <string>
namespace Nuake
{
class Modules
{
private:
std::map<std::string, void*> modules;
public:
static void StartupModules();
static void ShutdownModules();