-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimation.cs
91 lines (76 loc) · 2.36 KB
/
Animation.cs
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace PopLottie
{
using FrameNumber = System.Single; // float
enum AnimationFileType
{
Lottie,
Svg,
}
public abstract class Animation : IDisposable
{
public abstract bool IsStatic{get;}
public abstract bool HasTextLayers{get;}
public abstract FrameNumber TimeToFrame(TimeSpan Time,bool Looped);
public abstract TimeSpan FrameToTime(FrameNumber Frame);
public abstract TimeSpan Duration{get;} // could make this nullable, which would infer the animation is static
public abstract int FrameCount{get;} // this, or duration, should be redundant
static AnimationFileType? PeekFileType(string FileContents)
{
// todo: better hinting
if ( FileContents.StartsWith("<svg") )
return AnimationFileType.Svg;
return null;
}
static Animation AllocateAnimation(AnimationFileType Type,string FileContents)
{
switch (Type)
{
case AnimationFileType.Lottie: return new LottieAnimation(FileContents);
case AnimationFileType.Svg: return new SvgAnimation(FileContents);
default:
throw new Exception($"AllocateAnimation() Unhandled file type {Type}");
}
}
public static Animation Parse(string LottieJsonOrSvg)
{
// try loading all formats, starting with a format we think
// it might be.
// Probably shouldn't bother trying the first type twice though
var LoadTypes = new AnimationFileType?[]
{
PeekFileType(LottieJsonOrSvg),
AnimationFileType.Lottie,
AnimationFileType.Svg
};
string ParseErrors = null;
foreach (var LoadType in LoadTypes )
{
if ( LoadType == null )
continue;
try
{
var Anim = AllocateAnimation(LoadType.Value,LottieJsonOrSvg);
return Anim;
}
catch(Exception e)
{
ParseErrors += e.Message;
}
}
throw new Exception($"Failed to load file as animation; {ParseErrors}");
}
public abstract void Dispose();
public RenderCommands.AnimationFrame Render(TimeSpan PlayTime, Rect ContentRect,ScaleMode scaleMode)
{
// get the time, move it to lottie-anim space and loop it
var Frame = TimeToFrame(PlayTime,Looped:true);
return Render( Frame, ContentRect, scaleMode );
}
public abstract RenderCommands.AnimationFrame Render(FrameNumber Frame, Rect ContentRect,ScaleMode scaleMode);
}
}