lottie: code refactoring

Access its type from a property instance,
through coherent data structure.
This commit is contained in:
Hermet Park 2024-07-31 15:03:02 +09:00
parent 2b3930b46d
commit fc61fc42c2
3 changed files with 19 additions and 17 deletions

View file

@ -317,7 +317,7 @@ static void _buildLayer(jerry_value_t context, float frameNo, LottieLayer* layer
static jerry_value_t _value(float frameNo, LottieExpression* exp)
{
switch (exp->type) {
switch (exp->property->type) {
case LottieProperty::Type::Point: {
auto value = jerry_object();
auto pos = (*static_cast<LottiePoint*>(exp->property))(frameNo);
@ -352,7 +352,7 @@ static jerry_value_t _value(float frameNo, LottieExpression* exp)
return value;
}
default: {
TVGERR("LOTTIE", "Non supported type for value? = %d", (int) exp->type);
TVGERR("LOTTIE", "Non supported type for value? = %d", (int) exp->property->type);
}
}
return jerry_undefined();
@ -735,7 +735,7 @@ static jerry_value_t _velocityAtTime(const jerry_call_info_t* info, const jerry_
Point cur, prv;
//compute the velocity
switch (exp->type) {
switch (exp->property->type) {
case LottieProperty::Type::Point: {
prv = (*static_cast<LottiePoint*>(exp->property))(pframe);
cur = (*static_cast<LottiePoint*>(exp->property))(cframe);
@ -779,7 +779,7 @@ static jerry_value_t _speedAtTime(const jerry_call_info_t* info, const jerry_val
Point cur, prv;
//compute the velocity
switch (exp->type) {
switch (exp->property->type) {
case LottieProperty::Type::Point: {
prv = (*static_cast<LottiePoint*>(exp->property))(pframe);
cur = (*static_cast<LottiePoint*>(exp->property))(cframe);
@ -1307,7 +1307,7 @@ jerry_value_t LottieExpressions::evaluate(float frameNo, LottieExpression* exp)
jerry_object_set_native_ptr(thisProperty, nullptr, exp->property);
_buildProperty(frameNo, global, exp);
if (exp->type == LottieProperty::Type::PathSet) _buildPath(thisProperty, exp);
if (exp->property->type == LottieProperty::Type::PathSet) _buildPath(thisProperty, exp);
if (exp->object->type == LottieObject::Transform) _buildTransform(global, frameNo, static_cast<LottieTransform*>(exp->object));
//evaluate the code

View file

@ -34,7 +34,7 @@
#define KEY_AS(name) !strcmp(key, name)
static LottieExpression* _expression(char* code, LottieComposition* comp, LottieLayer* layer, LottieObject* object, LottieProperty* property, LottieProperty::Type type)
static LottieExpression* _expression(char* code, LottieComposition* comp, LottieLayer* layer, LottieObject* object, LottieProperty* property)
{
if (!comp->expressions) comp->expressions = true;
@ -44,7 +44,6 @@ static LottieExpression* _expression(char* code, LottieComposition* comp, Lottie
inst->layer = layer;
inst->object = object;
inst->property = property;
inst->type = type;
inst->enabled = true;
return inst;
@ -509,14 +508,14 @@ void LottieParser::parseProperty(T& prop, LottieObject* obj)
for (auto slot = comp->slots.begin(); slot < comp->slots.end(); ++slot) {
if (strcmp((*slot)->sid, sid)) continue;
(*slot)->pairs.push({obj});
return;
break;
}
comp->slots.push(new LottieSlot(sid, obj, type));
} else if (!strcmp(key, "x")) {
prop.exp = _expression(getStringCopy(), comp, context.layer, context.parent, &prop, type);
}
else skip(key);
} else if (KEY_AS("x")) {
prop.exp = _expression(getStringCopy(), comp, context.layer, context.parent, &prop);
} else skip(key);
}
prop.type = type;
}
@ -603,9 +602,10 @@ LottieTransform* LottieParser::parseTransform(bool ddd)
//check separateCoord to figure out whether "x(expression)" / "x(coord)"
else if (transform->coords && KEY_AS("x")) parseProperty<LottieProperty::Type::Float>(transform->coords->x);
else if (transform->coords && KEY_AS("y")) parseProperty<LottieProperty::Type::Float>(transform->coords->y);
else if (KEY_AS("x")) transform->position.exp = _expression(getStringCopy(), comp, context.layer, context.parent, &transform->position, LottieProperty::Type::Position);
else if (KEY_AS("x")) transform->position.exp = _expression(getStringCopy(), comp, context.layer, context.parent, &transform->position);
else skip(key);
}
transform->position.type = LottieProperty::Type::Position;
}
else if (KEY_AS("a")) parseProperty<LottieProperty::Type::Point>(transform->anchor);
else if (KEY_AS("s")) parseProperty<LottieProperty::Type::Point>(transform->scale);
@ -696,10 +696,11 @@ void LottieParser::getPathSet(LottiePathSet& path)
} else {
getValue(path.value);
}
} else if (!strcmp(key, "x")) {
path.exp = _expression(getStringCopy(), comp, context.layer, context.parent, &path, LottieProperty::Type::PathSet);
} else if (KEY_AS("x")) {
path.exp = _expression(getStringCopy(), comp, context.layer, context.parent, &path);
} else skip(key);
}
path.type = LottieProperty::Type::PathSet;
}

View file

@ -176,9 +176,11 @@ struct LottieVectorFrame
struct LottieProperty
{
enum class Type : uint8_t { Point = 0, Float, Opacity, Color, PathSet, ColorStop, Position, TextDoc, Invalid };
virtual ~LottieProperty() {}
LottieExpression* exp = nullptr;
Type type;
virtual ~LottieProperty() {}
//TODO: Apply common bodies?
virtual uint32_t frameCnt() = 0;
@ -196,7 +198,6 @@ struct LottieExpression
LottieLayer* layer;
LottieObject* object;
LottieProperty* property;
LottieProperty::Type type;
bool enabled;