feat(instance): added ClassName as (read-only) property

This commit is contained in:
maelstrom 2025-04-13 18:48:00 +02:00
parent 7e14e3f968
commit cf8b9529c5
2 changed files with 14 additions and 3 deletions

View file

@ -25,13 +25,23 @@ const InstanceType Instance::TYPE = {
// return &TYPE_; // return &TYPE_;
// } // }
constexpr FieldCodec classNameCodec() {
return FieldCodec {
.write = nullptr,
.read = [](void* source) -> Data::Variant {
return Data::String(((const InstanceType*)source)->className);
},
};
}
Instance::Instance(const InstanceType* type) { Instance::Instance(const InstanceType* type) {
this->name = type->className; this->name = type->className;
this->memberMap = std::make_unique<MemberMap>( MemberMap { this->memberMap = std::make_unique<MemberMap>( MemberMap {
.super = std::nullopt, .super = std::nullopt,
.members = { .members = {
{ "Name", { .backingField = &name, .type = &Data::String::TYPE, .codec = fieldCodecOf<Data::String, std::string>() } } { "Name", { .backingField = &name, .type = &Data::String::TYPE, .codec = fieldCodecOf<Data::String, std::string>() } },
{ "ClassName", { .backingField = const_cast<InstanceType*>(type), .type = &Data::String::TYPE, .codec = classNameCodec(), .flags = PROP_READONLY } },
} }
}); });
} }
@ -156,7 +166,7 @@ auto meta = GetPropertyMeta(name);
tl::expected<void, MemberNotFound> Instance::SetPropertyValue(std::string name, Data::Variant value) { tl::expected<void, MemberNotFound> Instance::SetPropertyValue(std::string name, Data::Variant value) {
auto meta = GetPropertyMeta(name); auto meta = GetPropertyMeta(name);
if (!meta) return tl::make_unexpected(MemberNotFound()); if (!meta || meta->flags & PROP_READONLY) return tl::make_unexpected(MemberNotFound());
meta->codec.write(value, meta->backingField); meta->codec.write(value, meta->backingField);
if (meta->updateCallback) meta->updateCallback.value()(name); if (meta->updateCallback) meta->updateCallback.value()(name);

View file

@ -48,11 +48,12 @@ enum PropertyFlags {
PROP_HIDDEN = 1 << 0, // Hidden from the editor PROP_HIDDEN = 1 << 0, // Hidden from the editor
PROP_NOSAVE = 1 << 1, // Do not serialize PROP_NOSAVE = 1 << 1, // Do not serialize
PROP_UNIT_FLOAT = 1 << 2, // Float between 0 and 1 PROP_UNIT_FLOAT = 1 << 2, // Float between 0 and 1
PROP_READONLY = 1 << 3, // Read only property, do not write
}; };
enum PropertyCategory { enum PropertyCategory {
PROP_CATEGORY_DATA,
PROP_CATEGORY_APPEARENCE, PROP_CATEGORY_APPEARENCE,
PROP_CATEGORY_DATA,
PROP_CATEGORY_BEHAVIOR, PROP_CATEGORY_BEHAVIOR,
PROP_CATEGORY_PART, PROP_CATEGORY_PART,
PROP_CATEGORY_SURFACE, PROP_CATEGORY_SURFACE,