diff --git a/src/objects/base/instance.cpp b/src/objects/base/instance.cpp index d17be0f..88f58b4 100644 --- a/src/objects/base/instance.cpp +++ b/src/objects/base/instance.cpp @@ -96,6 +96,7 @@ tl::expected Instance::SetPropertyValue(std::string name, if (!meta) return tl::make_unexpected(MemberNotFound()); meta->codec.write(value, meta->backingField); + if (meta->updateCallback) meta->updateCallback.value()(name); return {}; } diff --git a/src/objects/base/member.h b/src/objects/base/member.h index 87de137..664a346 100644 --- a/src/objects/base/member.h +++ b/src/objects/base/member.h @@ -2,6 +2,7 @@ #include "../../datatypes/base.h" #include "datatypes/meta.h" +#include #include #include #include @@ -32,10 +33,16 @@ constexpr FieldCodec fieldCodecOf() { }; } +template +std::function memberFunctionOf(void(T::*func)(std::string), T* obj) { + return std::bind(func, obj, std::placeholders::_1); +} + struct PropertyMeta { void* backingField; const Data::TypeInfo* type; FieldCodec codec; + std::optional> updateCallback; }; typedef std::variant MemberMeta; diff --git a/src/objects/part.cpp b/src/objects/part.cpp index 1454c1f..0bf0d36 100644 --- a/src/objects/part.cpp +++ b/src/objects/part.cpp @@ -4,6 +4,7 @@ #include "objects/base/member.h" #include #include +#include "physics/simulation.h" static InstanceType TYPE_ { .super = Instance::TYPE, @@ -22,12 +23,11 @@ Part::Part(): Part(PartConstructParams {}) { } Part::Part(PartConstructParams params): Instance(&TYPE_), position(params.position), rotation(params.rotation), - scale(params.scale), material(params.material), anchored(params.anchored) { - + scale(params.scale), material(params.material), anchored(params.anchored) { this->memberMap = std::make_unique(MemberMap { .super = std::move(this->memberMap), .members = { - { "Anchored", { .backingField = &anchored, .type = &Data::Bool::TYPE, .codec = fieldCodecOf() } } + { "Anchored", { .backingField = &anchored, .type = &Data::Bool::TYPE, .codec = fieldCodecOf(), .updateCallback = memberFunctionOf(&Part::onUpdated, this) } } } }); } @@ -46,4 +46,8 @@ void Part::OnParentUpdated(std::optional> oldParent, s this->rigidBody->setIsActive(newParent.has_value()); // TODO: Sleeping bodies that touch this one also need to be updated +} + +void Part::onUpdated(std::string property) { + syncPartPhysics(std::dynamic_pointer_cast(this->shared_from_this())); } \ No newline at end of file diff --git a/src/objects/part.h b/src/objects/part.h index 480ad99..6d604d5 100644 --- a/src/objects/part.h +++ b/src/objects/part.h @@ -22,6 +22,7 @@ struct PartConstructParams { class Part : public Instance { protected: void OnParentUpdated(std::optional> oldParent, std::optional> newParent) override; + void onUpdated(std::string); public: static InstanceType* TYPE;