diff --git a/autogen/src/codegen.cpp b/autogen/src/codegen.cpp index b6306cf..2eff211 100644 --- a/autogen/src/codegen.cpp +++ b/autogen/src/codegen.cpp @@ -85,8 +85,6 @@ void writePropertySetHandler(std::ofstream& out, ClassAnalysis state) { } else { out << "\n this->" << prop.fieldName << " = " << castFromVariant("value", prop.backingFieldType) << ";"; } - if (!prop.onUpdateCallback.empty()) - out << "\n " << prop.onUpdateCallback << "(name);"; out << "\n }"; first = false; @@ -106,6 +104,26 @@ void writePropertySetHandler(std::ofstream& out, ClassAnalysis state) { out << "\n};\n\n"; } +void writePropertyUpdateHandler(std::ofstream& out, ClassAnalysis state) { + out << "void " << state.name << "::InternalUpdateProperty(std::string name) {"; + + out << "\n "; + bool first = true; + for (auto& prop : state.properties) { + if (prop.onUpdateCallback.empty()) continue; + out << (first ? "" : " else ") << "if (name == \"" << prop.name << "\") {"; + + out << "\n " << prop.onUpdateCallback << "(name);"; + + out << "\n }"; + first = false; + } + + out << "\n " << state.baseClass << "::InternalUpdateProperty(name);"; + + out << "\n};\n\n"; +} + void writePropertyGetHandler(std::ofstream& out, ClassAnalysis state) { out << "result " << state.name << "::InternalGetPropertyValue(std::string name) {"; @@ -224,5 +242,6 @@ void writeCodeForClass(std::ofstream& out, ClassAnalysis& state) { writePropertySetHandler(out, state); writePropertyGetHandler(out, state); writePropertyMetaHandler(out, state); + writePropertyUpdateHandler(out, state); writePropertiesList(out, state); } \ No newline at end of file diff --git a/core/src/objects/annotation.h b/core/src/objects/annotation.h index 37280b7..47fa778 100644 --- a/core/src/objects/annotation.h +++ b/core/src/objects/annotation.h @@ -24,10 +24,11 @@ #define AUTOGEN_PREAMBLE \ protected: \ -result InternalGetPropertyMeta(std::string name) override; \ -fallible InternalSetPropertyValue(std::string name, Data::Variant value) override; \ -result InternalGetPropertyValue(std::string name) override; \ -std::vector InternalGetProperties() override; \ +virtual result InternalGetPropertyMeta(std::string name) override; \ +virtual fallible InternalSetPropertyValue(std::string name, Data::Variant value) override; \ +virtual result InternalGetPropertyValue(std::string name) override; \ +virtual void InternalUpdateProperty(std::string name) override; \ +virtual std::vector InternalGetProperties() override; \ public: \ const static InstanceType TYPE; \ virtual const InstanceType* GetClass() override; \ diff --git a/core/src/objects/base/instance.cpp b/core/src/objects/base/instance.cpp index 5d37592..a29d20d 100644 --- a/core/src/objects/base/instance.cpp +++ b/core/src/objects/base/instance.cpp @@ -189,9 +189,12 @@ result Instance::GetPropertyValue(std::string nam return InternalGetPropertyValue(name); } -fallible Instance::SetPropertyValue(std::string name, Data::Variant value) { +fallible Instance::SetPropertyValue(std::string name, Data::Variant value, bool sendUpdateEvent) { auto result = InternalSetPropertyValue(name, value); - if (result.isSuccess()) sendPropertyUpdatedSignal(shared_from_this(), name, value); + if (result.isSuccess() && sendUpdateEvent) { + InternalUpdateProperty(name); + sendPropertyUpdatedSignal(shared_from_this(), name, value); + } return result; } @@ -236,6 +239,9 @@ fallible Instance::InternalSetPropertyVa return {}; } +void Instance::InternalUpdateProperty(std::string name) { +} + std::vector Instance::InternalGetProperties() { std::vector members; members.push_back("Name"); @@ -245,12 +251,7 @@ std::vector Instance::InternalGetProperties() { } void Instance::UpdateProperty(std::string name) { - // TODO: temporary workaround because I'm too lazy to implement this in autogen - InternalSetPropertyValue(name, InternalGetPropertyValue(name).expect()).expect(); - - // PropertyMeta meta = GetPropertyMeta(name).expect(); - // if (!meta.updateCallback) return; // Nothing to update, exit. - // meta.updateCallback.value()(name); + InternalUpdateProperty(name); } std::vector Instance::GetProperties() { diff --git a/core/src/objects/base/instance.h b/core/src/objects/base/instance.h index 254591a..2a28ff2 100644 --- a/core/src/objects/base/instance.h +++ b/core/src/objects/base/instance.h @@ -72,6 +72,7 @@ protected: virtual result InternalGetPropertyValue(std::string name); virtual fallible InternalSetPropertyValue(std::string name, Data::Variant value); virtual result InternalGetPropertyMeta(std::string name); + virtual void InternalUpdateProperty(std::string name); virtual std::vector InternalGetProperties(); virtual void OnParentUpdated(std::optional> oldParent, std::optional> newParent); @@ -110,7 +111,7 @@ public: // Properties result GetPropertyValue(std::string name); - fallible SetPropertyValue(std::string name, Data::Variant value); + fallible SetPropertyValue(std::string name, Data::Variant value, bool sendUpdateEvent = true); result GetPropertyMeta(std::string name); // Manually trigger the update of a property. Useful internally when setting properties directly void UpdateProperty(std::string name);