diff --git a/kraken/nodes/KRNode.h b/kraken/nodes/KRNode.h index 9debc05..5d2f96f 100755 --- a/kraken/nodes/KRNode.h +++ b/kraken/nodes/KRNode.h @@ -38,6 +38,7 @@ #include "KROctreeNode.h" #include "KRBehavior.h" #include "KRShaderReflection.h" +#include "KRNodeProperty.h" #include using namespace kraken; @@ -68,103 +69,7 @@ class XMLNode; class XMLAttribute; } -template -class KRNodeProperty -{ -public: - static constexpr decltype(config::defaultVal) 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 - { - save(element, config::name); - } - - void save(tinyxml2::XMLElement* element, const char* attributeName) const - { - if constexpr (std::is_same::value) { - element->SetAttribute(attributeName, val ? "true" : "false"); - } else if constexpr (std::is_same::value) { - kraken::setXMLAttribute(attributeName, element, val, config::defaultVal); - } else if constexpr (std::is_same::value) { - kraken::setXMLAttribute(attributeName, element, val, config::defaultVal); - } else if constexpr (std::is_same::value) { - element->SetAttribute(attributeName, val.c_str()); - } else if constexpr (std::is_base_of::value) { - element->SetAttribute(attributeName, val.getName().c_str()); - } else { - element->SetAttribute(attributeName, val); - } - } - - void load(tinyxml2::XMLElement* element) - { - load(element, config::name); - } - - void load(tinyxml2::XMLElement* element, const char* attributeName) - { - if constexpr (std::is_same::value) { - if (element->QueryIntAttribute(attributeName, &val) != tinyxml2::XML_SUCCESS) { - val = config::defaultVal; - } - } else if constexpr (std::is_same::value) { - if (element->QueryUnsignedAttribute(attributeName, &val) != tinyxml2::XML_SUCCESS) { - val = config::defaultVal; - } - } else if constexpr (std::is_same::value) { - if (element->QueryFloatAttribute(attributeName, &val) != tinyxml2::XML_SUCCESS) { - val = config::defaultVal; - } - } else if constexpr (std::is_same::value) { - if (element->QueryBoolAttribute(attributeName, &val) != tinyxml2::XML_SUCCESS) { - val = config::defaultVal; - } - } else if constexpr (std::is_same::value) { - val = kraken::getXMLAttribute(attributeName, element, config::defaultVal); - } else if constexpr (std::is_same::value) { - val = kraken::getXMLAttribute(attributeName, element, config::defaultVal); - } else if constexpr (std::is_same::value) { - const char* name = element->Attribute(attributeName); - if (name) { - val = name; - } else { - val = config::defaultVal; - } - } else if constexpr (std::is_base_of::value) { - const char* name = element->Attribute(attributeName); - if (name) { - val.set(name); - } else { - val.clear(); - } - } else { - static_assert(false, "Typename not implemented."); - } - } - - T val; -}; #define KRNODE_PROPERTY(PROP_TYPE, VAR, PROP_DEFAULT, PROP_NAME) \ struct VAR ## _config { \ diff --git a/kraken/nodes/KRNodeProperty.h b/kraken/nodes/KRNodeProperty.h new file mode 100644 index 0000000..5262faf --- /dev/null +++ b/kraken/nodes/KRNodeProperty.h @@ -0,0 +1,132 @@ +// +// KRNodeProperty.h +// Kraken Engine +// +// Copyright 2025 Kearwood Gilbert. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other materials +// provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED +// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR +// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those of the +// authors and should not be interpreted as representing official policies, either expressed +// or implied, of Kearwood Gilbert. +// + +#pragma once + +#include "resources/KRResourceBinding.h" + +template +class KRNodeProperty +{ +public: + static constexpr decltype(config::defaultVal) 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 + { + save(element, config::name); + } + + void save(tinyxml2::XMLElement* element, const char* attributeName) const + { + if constexpr (std::is_same::value) { + element->SetAttribute(attributeName, val ? "true" : "false"); + } else if constexpr (std::is_same::value) { + kraken::setXMLAttribute(attributeName, element, val, config::defaultVal); + } else if constexpr (std::is_same::value) { + kraken::setXMLAttribute(attributeName, element, val, config::defaultVal); + } else if constexpr (std::is_same::value) { + element->SetAttribute(attributeName, val.c_str()); + } else if constexpr (std::is_base_of::value) { + element->SetAttribute(attributeName, val.getName().c_str()); + } else { + element->SetAttribute(attributeName, val); + } + } + + void load(tinyxml2::XMLElement* element) + { + load(element, config::name); + } + + void load(tinyxml2::XMLElement* element, const char* attributeName) + { + if constexpr (std::is_same::value) { + if (element->QueryIntAttribute(attributeName, &val) != tinyxml2::XML_SUCCESS) { + val = config::defaultVal; + } + } else if constexpr (std::is_same::value) { + if (element->QueryUnsignedAttribute(attributeName, &val) != tinyxml2::XML_SUCCESS) { + val = config::defaultVal; + } + } else if constexpr (std::is_same::value) { + if (element->QueryFloatAttribute(attributeName, &val) != tinyxml2::XML_SUCCESS) { + val = config::defaultVal; + } + } else if constexpr (std::is_same::value) { + if (element->QueryBoolAttribute(attributeName, &val) != tinyxml2::XML_SUCCESS) { + val = config::defaultVal; + } + } else if constexpr (std::is_same::value) { + val = kraken::getXMLAttribute(attributeName, element, config::defaultVal); + } else if constexpr (std::is_same::value) { + val = kraken::getXMLAttribute(attributeName, element, config::defaultVal); + } else if constexpr (std::is_same::value) { + const char* name = element->Attribute(attributeName); + if (name) { + val = name; + } else { + val = config::defaultVal; + } + } else if constexpr (std::is_base_of::value) { + const char* name = element->Attribute(attributeName); + if (name) { + val.set(name); + } else { + val.clear(); + } + } else { + static_assert(false, "Typename not implemented."); + } + } + + T val; +};