Implement KRNodeProperty to reduce boilerplate needed for adding new node attributes
This commit is contained in:
@@ -37,6 +37,7 @@
|
||||
#include "KROctreeNode.h"
|
||||
#include "KRBehavior.h"
|
||||
#include "KRShaderReflection.h"
|
||||
#include <type_traits>
|
||||
|
||||
using namespace kraken;
|
||||
|
||||
@@ -65,6 +66,59 @@ class XMLNode;
|
||||
class XMLAttribute;
|
||||
}
|
||||
|
||||
template <typename T, class config>
|
||||
class KRNodeProperty
|
||||
{
|
||||
public:
|
||||
static constexpr T defaultVal = config::defaultVal;
|
||||
static constexpr const char* name = config::name;
|
||||
KRNodeProperty()
|
||||
: val(config::defaultVal)
|
||||
{
|
||||
}
|
||||
|
||||
KRNodeProperty(T& val)
|
||||
: val(val)
|
||||
{
|
||||
}
|
||||
|
||||
KRNodeProperty& operator=(const T& v)
|
||||
{
|
||||
val = v;
|
||||
return *this;
|
||||
}
|
||||
|
||||
operator const T& () const
|
||||
{
|
||||
return val;
|
||||
}
|
||||
|
||||
void save(tinyxml2::XMLElement* element) const
|
||||
{
|
||||
element->SetAttribute(config::name, val);
|
||||
}
|
||||
|
||||
void load(tinyxml2::XMLElement* element)
|
||||
{
|
||||
if constexpr (std::is_same<T, float>::value) {
|
||||
if (element->QueryFloatAttribute(config::name, &val) != tinyxml2::XML_SUCCESS) {
|
||||
val = config::defaultVal;
|
||||
}
|
||||
} else {
|
||||
static_assert(false, "Typename not implemented.");
|
||||
}
|
||||
}
|
||||
|
||||
T val;
|
||||
};
|
||||
|
||||
#define KRNODE_PROPERTY(PROP_TYPE, VAR, PROP_DEFAULT, PROP_NAME) \
|
||||
struct VAR ## _config { \
|
||||
static constexpr float defaultVal = PROP_DEFAULT; \
|
||||
static constexpr const char* name = PROP_NAME; \
|
||||
}; \
|
||||
KRNodeProperty<PROP_TYPE, VAR ## _config> VAR;
|
||||
|
||||
class KRNode
|
||||
: public KRContextObject
|
||||
, public KRReflectedObject
|
||||
|
||||
@@ -50,7 +50,7 @@ using namespace hydra;
|
||||
void KRSprite::InitNodeInfo(KrNodeInfo* nodeInfo)
|
||||
{
|
||||
KRNode::InitNodeInfo(nodeInfo);
|
||||
nodeInfo->sprite.alpha = 1.0f;
|
||||
nodeInfo->sprite.alpha = decltype(m_spriteAlpha)::defaultVal;
|
||||
nodeInfo->sprite.texture = -1;
|
||||
}
|
||||
|
||||
@@ -58,7 +58,6 @@ KRSprite::KRSprite(KRScene& scene, std::string name)
|
||||
: KRNode(scene, name)
|
||||
, m_spriteTexture(KRTextureBinding(KRTexture::TEXTURE_USAGE_SPRITE))
|
||||
{
|
||||
m_spriteAlpha = 1.0f;
|
||||
}
|
||||
|
||||
KRSprite::~KRSprite()
|
||||
@@ -73,7 +72,7 @@ tinyxml2::XMLElement* KRSprite::saveXML(tinyxml2::XMLNode* parent)
|
||||
{
|
||||
tinyxml2::XMLElement* e = KRNode::saveXML(parent);
|
||||
e->SetAttribute("sprite_texture", m_spriteTexture.getName().c_str());
|
||||
e->SetAttribute("sprite_alpha", m_spriteAlpha);
|
||||
m_spriteAlpha.save(e);
|
||||
return e;
|
||||
}
|
||||
|
||||
@@ -81,9 +80,7 @@ void KRSprite::loadXML(tinyxml2::XMLElement* e)
|
||||
{
|
||||
KRNode::loadXML(e);
|
||||
|
||||
if (e->QueryFloatAttribute("sprite_alpha", &m_spriteAlpha) != tinyxml2::XML_SUCCESS) {
|
||||
m_spriteAlpha = 1.0f;
|
||||
}
|
||||
m_spriteAlpha.load(e);
|
||||
|
||||
const char* szSpriteTexture = e->Attribute("sprite_texture");
|
||||
if (szSpriteTexture) {
|
||||
@@ -164,4 +161,4 @@ bool KRSprite::getShaderValue(ShaderValue value, float* output) const
|
||||
return true;
|
||||
}
|
||||
return KRNode::getShaderValue(value, output);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,5 +62,6 @@ protected:
|
||||
bool getShaderValue(ShaderValue value, float* output) const override;
|
||||
|
||||
KRTextureBinding m_spriteTexture;
|
||||
float m_spriteAlpha;
|
||||
|
||||
KRNODE_PROPERTY(float, m_spriteAlpha, 1.f, "sprite_alpha");
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user