summaryrefslogtreecommitdiff
path: root/ssg/include/lottiemodel.h
blob: 18be7e6b787b806f487a2646f1791a88c0428f97 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#ifndef LOTTIEMODEL_H
#define LOTTIEMODEL_H

#include<vector>
#include<memory>
#include<unordered_map>
#include"sgpoint.h"
#include"sgrect.h"
#include"sginterpolator.h"


class LottieComposition;
class LottieLayer;
class LottieTransform;
class LottieShapeGroup;
class LottieShapeObject;
class LottieRectObject;
class LottieEllipseObject;
class LottieTrimObject;
class LottieRepeaterObject;
class LottieFillObject;
class LottieStrokeObject;
class LottieGroupObject;

class LottieObjectVisitor
{
public:
    virtual ~LottieObjectVisitor() {}
    virtual void visit(LottieComposition *) = 0;
    virtual void visit(LottieLayer *) = 0;
    virtual void visit(LottieTransform *) = 0;
    virtual void visit(LottieShapeGroup *) = 0;
    virtual void visit(LottieShapeObject *) = 0;
    virtual void visit(LottieRectObject *) = 0;
    virtual void visit(LottieEllipseObject *) = 0;
    virtual void visit(LottieTrimObject *) = 0;
    virtual void visit(LottieRepeaterObject *) = 0;
    virtual void visit(LottieFillObject *) = 0;
    virtual void visit(LottieStrokeObject *) = 0;
    virtual void visitChildren(LottieGroupObject *) = 0;
};

class LottieColor
{
public:
    float r;
    float g;
    float b;
};


template<typename T>
class LottieKeyFrame
{
public:
    LottieKeyFrame():mStartValue(),
                     mEndValue(),
                     mStartFrame(0),
                     mEndFrame(0),
                     mInterpolator(nullptr),
                     mInTangent(),
                     mOutTangent(),
                     mPathKeyFrame(false){}
    bool containsProgress(float progress);

public:
    T                   mStartValue;
    T                   mEndValue;
    int                 mStartFrame;
    int                 mEndFrame;
    std::shared_ptr<SGInterpolator> mInterpolator;

    /* this is for interpolating position along a path
     * Need to move to other place because its only applicable
     * for positional property.
     */
    SGPointF            mInTangent;
    SGPointF            mOutTangent;
    bool                mPathKeyFrame;
};

template<typename T>
class LottieAnimInfo
{
public:
    std::vector<LottieKeyFrame<T>> mKeyFrames;
};

template<typename T>
class LottieAnimatable
{
public:
    LottieAnimatable():mValue(),mAnimInfo(nullptr){}
    LottieAnimatable(const T &value): mValue(value){}
    constexpr bool isStatic() const {if (mAnimInfo) return false; else return true;}
public:
    T                                    mValue;
    int                                  mPropertyIndex; /* "ix" */
    std::shared_ptr<LottieAnimInfo<T>>   mAnimInfo;
};

enum class LottieBlendMode
{
    Normal = 0,
    Multiply = 1,
    Screen = 2,
    OverLay = 3
};

class LottieObjectVisitor;
class LottieObject
{
public:
    enum class Type {
        Composition = 1,
        Asset,
        Layer,
        ShapeGroup,
        Transform,
        Fill,
        Stroke,
        GFill,
        GStroke,
        Rect,
        Ellipse,
        Shape,
        Star,
        Trim,
        Repeater
    };
    inline LottieObject::Type type() const {return mType;}
    virtual void accept(LottieObjectVisitor *){}
    virtual ~LottieObject(){}
    LottieObject(LottieObject::Type  type): mStatic(true), mType(type){}
    bool isStatic() {return mStatic;}
    void setStatic(bool value) {mStatic = value;}
    virtual bool hasChildren() {return false;}
public:
    bool                mStatic;
    LottieObject::Type  mType;
};

class LottieGroupObject: public LottieObject
{
public:
    LottieGroupObject(LottieObject::Type  type):LottieObject(type){}
    virtual bool hasChildren() {return true;}
public:
    std::vector<std::shared_ptr<LottieObject>> mChildren;
};

class LottieShapeGroup : public LottieGroupObject
{
public:
    void accept(LottieObjectVisitor *visitor) override
    {visitor->visit(this); visitor->visitChildren(this);}

    LottieShapeGroup():LottieGroupObject(LottieObject::Type::ShapeGroup){}
public:
    std::shared_ptr<LottieObject>    mTransform;
};

class LottieLayer;
class LottieAsset : public LottieObject
{
public:
    LottieAsset():LottieObject(LottieObject::Type::Asset){}
    int                                         mAssetType; //lottie aset type  (precomp/char/image)
    std::string                                 mRefId; // ref id
    std::vector<std::shared_ptr<LottieObject>>   mLayers;
};

class LottieTransform;
class LottieComposition : public LottieGroupObject
{
public:
    void processPathOperatorObjects();
    void processPaintOperatorObjects();
    void processRepeaterObjects();
    void accept(LottieObjectVisitor *visitor) override
    {visitor->visit(this); visitor->visitChildren(this);}
    LottieComposition():LottieGroupObject(LottieObject::Type::Composition){}
    float getFrameDuration()const{return mEndFrame - mStartFrame;}
    float getDuration()const;
    long startFrame() const {return mStartFrame;}
    long endFrame() const {return mEndFrame;}

public:
    SGRect               mBound;
    bool                 mAnimation = false;
    long                 mStartFrame = 0;
    long                 mEndFrame = 0;
    float                mFrameRate;
    LottieBlendMode      mBlendMode;
    std::unordered_map<std::string,
                       std::shared_ptr<SGInterpolator>> mInterpolatorCache;
    std::vector<std::shared_ptr<LottieAsset>> mAssets;
};

class LottieLayer : public LottieGroupObject
{
public:
    void accept(LottieObjectVisitor *visitor) override
    {visitor->visit(this); visitor->visitChildren(this);}
    LottieLayer():LottieGroupObject(LottieObject::Type::Layer),mParentId(-1),mId(-1){}
public:
    SGRect               mBound;
    int                  mlayerType; //lottie layer type  (solid/shape/precomp)
    int                  mParentId; // Lottie the id of the parent in the composition
    int                  mId;  // Lottie the group id  used for parenting.
    int                  mGroupType; //lottie layer type  (solid/shape/precomp)
    long                 mStartFrame = 0;
    long                 mEndFrame = 0;
    long                 mStartTime;
    LottieBlendMode      mBlendMode;
    float                mTimeStreatch;
    std::string          mPreCompRefId;
    LottieAnimatable<float>     mTimeRemap;  /* "tm" */
    std::shared_ptr<LottieObject>    mTransform;
};

class LottieTransform : public LottieObject
{
public:
    void accept(LottieObjectVisitor *visitor) final
    {visitor->visit(this);}
    LottieTransform():LottieObject(LottieObject::Type::Transform),
                      mRotation(0),
                      mScale(SGPointF(100, 100)),
                      mPosition(SGPointF(0, 0)),
                      mAnchor(SGPointF(0, 0)),
                      mOpacity(100),
                      mSkew(0),
                      mSkewAxis(0){}
public:
    LottieAnimatable<float>     mRotation;  /* "r" */
    LottieAnimatable<SGPointF>  mScale;     /* "s" */
    LottieAnimatable<SGPointF>  mPosition;  /* "p" */
    LottieAnimatable<SGPointF>  mAnchor;    /* "a" */
    LottieAnimatable<float>     mOpacity;   /* "o" */
    LottieAnimatable<float>     mSkew;      /* "sk" */
    LottieAnimatable<float>     mSkewAxis;  /* "sa" */
};

class LottieFillObject : public LottieObject
{
public:
    void accept(LottieObjectVisitor *visitor) final
    {visitor->visit(this);}
    LottieFillObject():LottieObject(LottieObject::Type::Fill){}
public:
    LottieAnimatable<LottieColor>     mColor;   /* "c" */
    LottieAnimatable<int>             mOpacity;  /* "o" */
    bool                              mEnabled = true; /* "fillEnabled" */
};

class LottieStrokeObject : public LottieObject
{
public:
    void accept(LottieObjectVisitor *visitor) final
    {visitor->visit(this);}
    LottieStrokeObject():LottieObject(LottieObject::Type::Stroke){}
public:
    LottieAnimatable<LottieColor>     mColor;      /* "c" */
    LottieAnimatable<int>             mOpacity;    /* "o" */
    LottieAnimatable<float>           mWidth;      /* "w" */
    CapStyle                          mCapStyle;   /* "lc" */
    JoinStyle                         mJoinStyle;  /* "lj" */
    float                             mMeterLimit; /* "ml" */
    bool                              mEnabled = true;    /* "fillEnabled" */
};

class LottieShapeData
{
public:
    void process(bool closed =false);
    std::vector<SGPointF>    mInPoint;
    std::vector<SGPointF>    mOutPoint;
    std::vector<SGPointF>    mVertices;
    std::vector<SGPointF>    mPoints;
    int                      mSegments;
};

class LottieShape
{
public:
    void process(bool closed =false);
    std::shared_ptr<LottieShapeData> mShapeData;
};

class LottiePathObject : public LottieObject
{
public:
    LottiePathObject(LottieObject::Type  type):LottieObject(type){}
public:
    std::vector<std::shared_ptr<LottieObject>> mPathOperations;
    std::vector<std::shared_ptr<LottieObject>> mPaintOperations;
};

class LottieShapeObject : public LottiePathObject
{
public:
    void accept(LottieObjectVisitor *visitor) final
    {visitor->visit(this);}
    void process();
    LottieShapeObject():LottiePathObject(LottieObject::Type::Shape){}
public:
    LottieAnimatable<LottieShape>    mShape;
    bool                             mClosed = false;
};

class LottieRectObject : public LottiePathObject
{
public:
    void accept(LottieObjectVisitor *visitor) final
    {visitor->visit(this);}
    LottieRectObject():LottiePathObject(LottieObject::Type::Rect),
                       mPos(SGPointF(0,0)),
                       mSize(SGPointF(0,0)),
                       mRound(0){}
public:
    LottieAnimatable<SGPointF>    mPos;
    LottieAnimatable<SGPointF>    mSize;
    LottieAnimatable<float>       mRound;
};

class LottieEllipseObject : public LottiePathObject
{
public:
    void accept(LottieObjectVisitor *visitor) final
    {visitor->visit(this);}
    LottieEllipseObject():LottiePathObject(LottieObject::Type::Ellipse),
                          mPos(SGPointF(0,0)),
                          mSize(SGPointF(0,0)){}
public:
    LottieAnimatable<SGPointF>   mPos;
    LottieAnimatable<SGPointF>   mSize;
};

class LottieTrimObject : public LottieObject
{
public:
    void accept(LottieObjectVisitor *visitor) final
    {visitor->visit(this);}
    enum class TrimType {
        Simultaneously,
        Individually
    };
    LottieTrimObject():LottieObject(LottieObject::Type::Trim),
                       mStart(0),
                       mEnd(0),
                       mOffset(0),
                       mTrimType(TrimType::Simultaneously){}
public:
    LottieAnimatable<float>             mStart;
    LottieAnimatable<float>             mEnd;
    LottieAnimatable<float>             mOffset;
    LottieTrimObject::TrimType          mTrimType;
};

class LottieRepeaterObject : public LottieGroupObject
{
public:
    void accept(LottieObjectVisitor *visitor) final
    {visitor->visit(this); visitor->visitChildren(this);}
    LottieRepeaterObject():LottieGroupObject(LottieObject::Type::Repeater),
                           mCopies(0),
                           mOffset(0){}
public:
    LottieAnimatable<float>             mCopies;
    LottieAnimatable<float>             mOffset;
    std::shared_ptr<LottieObject>       mTransform;
};

class LottieDrawable
{
public:
    LottieDrawable(std::shared_ptr<LottieObject> model):mModelData(model){}
public:
    std::shared_ptr<LottieObject>    mModelData;
    std::vector<std::unique_ptr<LottieDrawable>> mChildren;
};

#endif // LOTTIEMODEL_H