Added generic ability to (de)serialize ResourceFiles, a templated function to allow easier setting of entt traits and renamed float limits function

This commit is contained in:
WiggleWizard
2024-09-13 13:22:05 +01:00
parent 82771d43da
commit 9e94006d2b
5 changed files with 60 additions and 32 deletions

View File

@@ -30,9 +30,17 @@ namespace Nuake
enum class ComponentTypeTrait : uint16_t
{
None = 0,
// Exposes the component to be added via the inspector
InspectorExposed = 1 << 0,
};
enum class ComponentFieldTrait : uint16_t
{
None = 0,
// Stops field from showing up in the editor inspector
Internal = 1 << 0,
// Marks the field as temporary (ie, do not serialize)
Transient = 1 << 1,
};
template <typename Enum>
@@ -114,7 +122,7 @@ public:
.prop(HashedName::DisplayName, displayName); \
} \
\
static auto FloatFieldLimits(float stepSize, float min, float max) \
static auto FieldFloatLimits(float stepSize, float min, float max) \
{ \
return ComponentFactory \
.prop(HashedFieldPropName::FloatStep, stepSize) \
@@ -126,4 +134,13 @@ public:
{ \
return ComponentFactory \
.prop(HashedFieldPropName::ResourceFileType, fileType); \
} \
\
template <typename... Enums> \
static auto SetFlags(Enums... enums) \
{ \
static_assert((std::is_enum_v<Enums> && ...), "All arguments must be of enum class type"); \
return ComponentFactory.traits((static_cast<uint16_t>(enums) | ...)); \
}

View File

@@ -6,6 +6,13 @@ using json = nlohmann::json;
#define BEGIN_SERIALIZE() json j;
#define SERIALIZE_VAL_LBL(lbl, v) j[lbl] = v;
#define SERIALIZE_VAL(v) j[#v] = this->v;
#define SERIALIZE_RES_FILE(v) \
bool validFile = this->v.file != nullptr && this->v.file->Exist(); \
j["validFile"#v] = validFile; \
if (validFile) \
{ \
j["file"#v] = this->v.file->GetRelativePath(); \
}
#define SERIALIZE_VEC2(v) \
j[#v]["x"] = v.x; \
@@ -23,7 +30,17 @@ using json = nlohmann::json;
if(j.contains(#p)) \
{ \
p = j[#p]; \
}
}
#define DESERIALIZE_RES_FILE(v) \
if (j.contains("validFile"#v)) \
{ \
if (bool validFile = j["validFile"#v]) \
{ \
const std::string filePath = j["file"#v]; \
(v).file = FileSystem::GetFile(filePath); \
} \
}
#define DESERIALIZE_VEC4(v, p) \
p = Vector4(v["x"], v["y"], v["z"], v["w"]);

View File

@@ -7,14 +7,7 @@ namespace Nuake {
json AudioEmitterComponent::Serialize()
{
BEGIN_SERIALIZE();
bool validFile = FilePath.file != nullptr && FilePath.file->Exist();
j["validFile"] = validFile;
if (validFile)
{
j["file"] = FilePath.file->GetRelativePath();
}
SERIALIZE_RES_FILE(FilePath);
SERIALIZE_VAL(IsPlaying);
SERIALIZE_VAL(Volume);
SERIALIZE_VAL(Pan);
@@ -29,16 +22,7 @@ namespace Nuake {
bool AudioEmitterComponent::Deserialize(const json& j)
{
if (j.contains("validFile"))
{
bool validFile = j["validFile"];
if (validFile)
{
std::string filePath = j["file"];
FilePath.file = FileSystem::GetFile(filePath);
}
}
DESERIALIZE_RES_FILE(FilePath);
DESERIALIZE_VAL(Volume);
DESERIALIZE_VAL(IsPlaying);
DESERIALIZE_VAL(Pan);

View File

@@ -4,7 +4,6 @@
#include "FieldTypes.h"
#include "src/Core/Core.h"
#include "src/FileSystem/File.h"
#include "src/Resource/Resource.h"
#include "src/Resource/Serializable.h"
@@ -23,20 +22,20 @@ namespace Nuake {
BindComponentField<&AudioEmitterComponent::Loop>("Loop", "Loop");
BindComponentField<&AudioEmitterComponent::Volume>("Volume", "Volume");
FloatFieldLimits(0.001f, 0.0f, 2.0f);
FieldFloatLimits(0.001f, 0.0f, 2.0f);
BindComponentField<&AudioEmitterComponent::Pan>("Pan", "Pan");
FloatFieldLimits(0.01f, -1.0f, 1.0f);
FieldFloatLimits(0.01f, -1.0f, 1.0f);
BindComponentField<&AudioEmitterComponent::PlaybackSpeed>("PlaybackSpeed", "Playback Speed");
FloatFieldLimits(0.01f, 0.0001f, 0.f);
FieldFloatLimits(0.01f, 0.0001f, 0.f);
BindComponentField<&AudioEmitterComponent::Spatialized>("Spatialized", "Spatialized");
BindComponentField<&AudioEmitterComponent::MinDistance>("MinDistance", "Min Distance");
FloatFieldLimits(0.001f, 0.f, 0.f);
FieldFloatLimits(0.001f, 0.f, 0.f);
BindComponentField<&AudioEmitterComponent::MaxDistance>("MaxDistance", "Max Distance");
FloatFieldLimits(0.001f, 0.f, 0.f);
FieldFloatLimits(0.001f, 0.f, 0.f);
BindComponentField<&AudioEmitterComponent::AttenuationFactor>("AttenuationFactor", "Attenuation Factor");
FloatFieldLimits(0.001f, 0.f, 0.f);
FieldFloatLimits(0.001f, 0.f, 0.f);
}
public:

View File

@@ -1,7 +1,10 @@
#pragma once
#include "Component.h"
#include "FieldTypes.h"
#include "src/Core/Logger.h"
#include "src/FileSystem/File.h"
#include "src/FileSystem/FileSystem.h"
#include "src/Resource/UUID.h"
#include "src/Resource/Serializable.h"
@@ -11,24 +14,32 @@ namespace Nuake
struct UIComponent : public Component
{
NUAKECOMPONENT(UIComponent, "UI")
static void InitializeComponentClass()
{
BindComponentField<&UIComponent::UIResource>("UIResource", "UIResource");
SetFlags(ComponentFieldTrait::Internal, ComponentFieldTrait::Transient);
BindComponentField<&UIComponent::UIFilePath>("UIFilePath", "File Path");
BindComponentField<&UIComponent::IsWorldSpace>("IsWorldspace", "Is Worldspace");
}
public:
UUID UIResource = UUID(0);
std::string CSharpUIController;
std::string UIFilePath;
ResourceFile UIFilePath;
bool IsWorldSpace;
// TODO: Z-Ordering
json Serialize()
{
BEGIN_SERIALIZE();
SERIALIZE_VAL(UIFilePath);
SERIALIZE_RES_FILE(UIFilePath);
END_SERIALIZE();
}
bool Deserialize(const json& j)
{
DESERIALIZE_VAL(UIFilePath);
DESERIALIZE_RES_FILE(UIFilePath);
return true;
}
};