lottie/slot: Support expressions overriding

issue: #3168

Co-Authored-By: Hermet Park <hermet@lottiefiles.com>
This commit is contained in:
Jinny You 2025-02-24 16:57:53 +08:00 committed by Hermet Park
parent e235eb8152
commit 0e76ba3e47
3 changed files with 76 additions and 43 deletions

View file

@ -919,7 +919,7 @@ struct LottieSlot
void assign(LottieObject* target, bool byDefault);
void reset();
LottieSlot(char* sid, LottieObject* obj, LottieProperty::Type type) : sid(sid), type(type)
LottieSlot(LottieLayer* layer, LottieObject* parent, char* sid, LottieObject* obj, LottieProperty::Type type) : context{layer, parent}, sid(sid), type(type)
{
pairs.push({obj});
}
@ -931,9 +931,15 @@ struct LottieSlot
ARRAY_FOREACH(pair, pairs) delete(pair->prop);
}
struct {
LottieLayer* layer;
LottieObject* parent;
} context;
char* sid;
Array<Pair> pairs;
LottieProperty::Type type;
bool overridden = false;
};

View file

@ -471,7 +471,7 @@ void LottieParser::registerSlot(LottieObject* obj, const char* sid)
(*p)->pairs.push({obj});
return;
}
comp->slots.push(new LottieSlot(duplicate(sid), obj, type));
comp->slots.push(new LottieSlot(context.layer, context.parent, duplicate(sid), obj, type));
}
@ -1502,41 +1502,36 @@ bool LottieParser::apply(LottieSlot* slot, bool byDefault)
//OPTIMIZE: we can create the property directly, without object
LottieObject* obj = nullptr; //slot object
context = {slot->context.layer, slot->context.parent};
switch (slot->type) {
case LottieProperty::Type::Position: {
obj = new LottieTransform;
context.parent = obj;
parseSlotProperty<LottieProperty::Type::Position>(static_cast<LottieTransform*>(obj)->position);
break;
}
case LottieProperty::Type::Point: {
obj = new LottieTransform;
context.parent = obj;
parseSlotProperty<LottieProperty::Type::Point>(static_cast<LottieTransform*>(obj)->scale);
break;
}
case LottieProperty::Type::Float: {
obj = new LottieTransform;
context.parent = obj;
parseSlotProperty<LottieProperty::Type::Float>(static_cast<LottieTransform*>(obj)->rotation);
break;
}
case LottieProperty::Type::Opacity: {
obj = new LottieSolid;
context.parent = obj;
parseSlotProperty<LottieProperty::Type::Opacity>(static_cast<LottieSolid*>(obj)->opacity);
break;
}
case LottieProperty::Type::Color: {
obj = new LottieSolid;
context.parent = obj;
parseSlotProperty<LottieProperty::Type::Color>(static_cast<LottieSolid*>(obj)->color);
break;
}
case LottieProperty::Type::ColorStop: {
obj = new LottieGradient;
context.parent = obj;
while (auto key = nextObjectKey()) {
if (KEY_AS("p")) parseColorStop(static_cast<LottieGradient*>(obj));
else skip();
@ -1545,7 +1540,6 @@ bool LottieParser::apply(LottieSlot* slot, bool byDefault)
}
case LottieProperty::Type::TextDoc: {
obj = new LottieText;
context.parent = obj;
parseSlotProperty<LottieProperty::Type::TextDoc>(static_cast<LottieText*>(obj)->doc);
break;
}
@ -1554,7 +1548,6 @@ bool LottieParser::apply(LottieSlot* slot, bool byDefault)
if (KEY_AS("p")) obj = parseAsset();
else skip();
}
context.parent = obj;
break;
}
default: break;

View file

@ -35,6 +35,7 @@
struct LottieFont;
struct LottieLayer;
struct LottieObject;
struct LottieProperty;
//default keyframe updates condition (no tweening)
@ -123,23 +124,6 @@ struct LottieVectorFrame
};
//Property would have an either keyframes or single value.
struct LottieProperty
{
enum class Type : uint8_t { Point = 0, Float, Opacity, Color, PathSet, ColorStop, Position, TextDoc, Image, Invalid };
LottieExpression* exp = nullptr;
Type type;
uint8_t ix; //property index
//TODO: Apply common bodies?
virtual ~LottieProperty() {}
virtual uint32_t frameCnt() = 0;
virtual uint32_t nearest(float time) = 0;
virtual float frameNo(int32_t key) = 0;
};
struct LottieExpression
{
enum LoopMode : uint8_t { None = 0, InCycle = 1, InPingPong, InOffset, InContinue, OutCycle, OutPingPong, OutOffset, OutContinue };
@ -157,6 +141,18 @@ struct LottieExpression
LoopMode mode = None;
} loop;
LottieExpression() {}
LottieExpression(const LottieExpression* rhs)
{
code = strdup(rhs->code);
comp = rhs->comp;
layer = rhs->layer;
object = rhs->object;
property = rhs->property;
disabled = rhs->disabled;
}
~LottieExpression()
{
tvg::free(code);
@ -164,6 +160,36 @@ struct LottieExpression
};
//Property would have an either keyframes or single value.
struct LottieProperty
{
enum class Type : uint8_t { Point = 0, Float, Opacity, Color, PathSet, ColorStop, Position, TextDoc, Image, Invalid };
LottieExpression* exp = nullptr;
Type type;
uint8_t ix; //property index
//TODO: Apply common bodies?
virtual ~LottieProperty() {}
virtual uint32_t frameCnt() = 0;
virtual uint32_t nearest(float time) = 0;
virtual float frameNo(int32_t key) = 0;
bool copy(LottieProperty* rhs, bool shallow)
{
if (!rhs->exp) return false;
if (shallow) {
exp = rhs->exp;
rhs->exp = nullptr;
} else {
exp = new LottieExpression(rhs->exp);
}
exp->property = this;
return true;
}
};
static void _copy(PathSet* pathset, Array<Point>& out, Matrix* transform)
{
Array<Point> in;
@ -279,7 +305,7 @@ struct LottieGenericProperty : LottieProperty
LottieGenericProperty(const LottieGenericProperty<Frame, Value>& rhs)
{
copy(rhs);
copy(const_cast<LottieGenericProperty<Frame, Value>&>(rhs));
type = rhs.type;
ix = rhs.ix;
}
@ -355,12 +381,14 @@ struct LottieGenericProperty : LottieProperty
return tvg::lerp(operator()(frameNo, exps), operator()(tween.frameNo, exps), tween.progress);
}
void copy(const LottieGenericProperty<Frame, Value, Scalar>& rhs, bool shallow = true)
void copy(LottieGenericProperty<Frame, Value, Scalar>& rhs, bool shallow = true)
{
if (LottieProperty::copy(&rhs, shallow)) return;
if (rhs.frames) {
if (shallow) {
frames = rhs.frames;
const_cast<LottieGenericProperty<Frame, Value, Scalar>&>(rhs).frames = nullptr;
rhs.frames = nullptr;
} else {
frames = new Array<Frame>;
*frames = *rhs.frames;
@ -593,7 +621,7 @@ struct LottieColorStop : LottieProperty
LottieColorStop(const LottieColorStop& rhs)
{
copy(rhs);
copy(const_cast<LottieColorStop&>(rhs));
type = rhs.type;
ix = rhs.ix;
}
@ -741,19 +769,21 @@ struct LottieColorStop : LottieProperty
return tweening(frameNo, fill, tween, exps);
}
void copy(const LottieColorStop& rhs, bool shallow = true)
void copy(LottieColorStop& rhs, bool shallow = true)
{
if (LottieProperty::copy(&rhs, shallow)) return;
if (rhs.frames) {
if (shallow) {
frames = rhs.frames;
const_cast<LottieColorStop&>(rhs).frames = nullptr;
rhs.frames = nullptr;
} else {
frames = new Array<LottieScalarFrame<ColorStop>>;
*frames = *rhs.frames;
}
} else {
value = rhs.value;
const_cast<LottieColorStop&>(rhs).value = ColorStop();
rhs.value = ColorStop();
}
populated = rhs.populated;
count = rhs.count;
@ -772,7 +802,7 @@ struct LottieTextDoc : LottieProperty
LottieTextDoc(const LottieTextDoc& rhs)
{
copy(rhs);
copy(const_cast<LottieTextDoc&>(rhs));
type = rhs.type;
ix = rhs.ix;
}
@ -853,20 +883,22 @@ struct LottieTextDoc : LottieProperty
return frame->value;
}
void copy(const LottieTextDoc& rhs, bool shallow = true)
void copy(LottieTextDoc& rhs, bool shallow = true)
{
if (LottieProperty::copy(&rhs, shallow)) return;
if (rhs.frames) {
if (shallow) {
frames = rhs.frames;
const_cast<LottieTextDoc&>(rhs).frames = nullptr;
rhs.frames = nullptr;
} else {
frames = new Array<LottieScalarFrame<TextDocument>>;
*frames = *rhs.frames;
}
} else {
value = rhs.value;
const_cast<LottieTextDoc&>(rhs).value.text = nullptr;
const_cast<LottieTextDoc&>(rhs).value.name = nullptr;
rhs.value.text = nullptr;
rhs.value.name = nullptr;
}
}
@ -889,7 +921,7 @@ struct LottieBitmap : LottieProperty
LottieBitmap(const LottieBitmap& rhs)
{
copy(rhs);
copy(const_cast<LottieBitmap&>(rhs));
type = rhs.type;
ix = rhs.ix;
}
@ -912,8 +944,10 @@ struct LottieBitmap : LottieProperty
uint32_t nearest(float time) override { return 0; }
float frameNo(int32_t key) override { return 0; }
void copy(const LottieBitmap& rhs, bool shallow = true)
void copy(LottieBitmap& rhs, bool shallow = true)
{
if (LottieProperty::copy(&rhs, shallow)) return;
if (shallow) {
b64Data = rhs.b64Data;
mimeType = rhs.mimeType;
@ -927,8 +961,8 @@ struct LottieBitmap : LottieProperty
width = rhs.width;
height = rhs.height;
const_cast<LottieBitmap&>(rhs).b64Data = nullptr;
const_cast<LottieBitmap&>(rhs).mimeType = nullptr;
rhs.b64Data = nullptr;
rhs.mimeType = nullptr;
}
};