/
shapes.dart
160 lines (121 loc) · 4.49 KB
/
shapes.dart
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
import 'package:Lotie_Flutter/src/animatables.dart';
import 'package:Lotie_Flutter/src/converters.dart';
import 'package:Lotie_Flutter/src/values.dart';
import 'package:flutter/painting.dart' show Offset, PathFillType, StrokeCap;
abstract class Shape {
final String _name;
String get name => _name;
Shape(this._name);
}
class ShapeGroup extends Shape {
final List<Shape> _shapes;
List<Shape> get shapes => _shapes;
ShapeGroup(String name, this._shapes) : super(name);
ShapeGroup.fromMap(dynamic map, double scale)
: _shapes = parseRawShapes(map['it'], scale),
super(map['nm']);
static List<Shape> parseRawShapes(List rawShapes, double scale) =>
rawShapes.map((rawShape) => shapeFromMap(rawShape, scale))
.toList();
}
enum JoinType { Miter, Round, Bevel }
class ShapeStroke extends Shape {
final AnimatableDoubleValue _offset;
final List<AnimatableDoubleValue> _lineDashPattern;
final AnimatableColorValue _color;
final AnimatableIntegerValue _opacity;
final AnimatableDoubleValue _width;
final StrokeCap _capType;
// TODO: Open issue about Paint.Join
final JoinType joinType;
AnimatableDoubleValue get offset => _offset;
List<AnimatableDoubleValue> get lineDashPattern => _lineDashPattern;
AnimatableColorValue get color => _color;
AnimatableIntegerValue get opacity => _opacity;
AnimatableDoubleValue get width => _width;
StrokeCap get capType => _capType;
ShapeStroke._(String name, this._offset, this._lineDashPattern, this._color,
this._opacity, this._width, this._capType, this.joinType) : super(name);
factory ShapeStroke(dynamic map, double scale){
final String name = map["nm"];
final color = new AnimatableColorValue.fromMap(map["c"]);
final shapeWith = new AnimatableDoubleValue.fromMap(map["w"], scale);
final opacity = new AnimatableIntegerValue.fromMap(map["o"]);
final capType = StrokeCap.values[map["lc"] - 1];
final joinType = JoinType.values[map["lj"] - 1];
AnimatableDoubleValue offset;
final lineDashPattern = new List<AnimatableDoubleValue>();
if (map.contains("d")) {
List rawDashes = map["d"];
for (var rawDash in rawDashes) {
final String n = rawDash["n"];
if (n == "o") {
offset = new AnimatableDoubleValue.fromMap(rawDash["v"], scale);
} else if (n == "d" || n == "g") {
lineDashPattern.add(
new AnimatableDoubleValue.fromMap(rawDash["v"], scale));
}
}
if (lineDashPattern.length == 1) {
// If there is only 1 value then it is assumed to be equal parts on and off.
lineDashPattern.add(lineDashPattern[0]);
}
}
return new ShapeStroke._(
name,
offset,
lineDashPattern,
color,
opacity,
shapeWith,
capType,
joinType);
}
}
class ShapeFill extends Shape {
final bool _fillEnabled;
final PathFillType _fillType;
final AnimatableColorValue _color;
final AnimatableIntegerValue _opacity;
ShapeFill._(String name, this._fillEnabled, this._fillType, this._color,
this._opacity) : super(name);
factory ShapeFill (dynamic map, double scale) {
final String name = map["nm"];
final color = map.containsKey("c") ? new AnimatableColorValue.fromMap(
map["c"])
: null;
final opacity = map.containsKey("o") ? new AnimatableIntegerValue.fromMap(
map["c"]) : null;
bool fillEnabled = map["fillEnabled"];
int rawFillType = map.containsKey("r") ? map["r"] : 1;
final fillType = rawFillType == 1 ? PathFillType.nonZero : PathFillType.evenOdd;
return new ShapeFill._(name, fillEnabled, fillType, color, opacity);
}
}
class GradientStroke extends Shape {
GradientStroke.fromMap(dynamic map, double scale) : super('');
}
class GradientFill extends Shape {
GradientFill.fromMap(dynamic map, double scale) : super('');
}
class ShapePath extends Shape {
ShapePath.fromMap(dynamic map, double scale) : super('');
}
class CircleShape extends Shape {
CircleShape.fromMap(dynamic map, double scale): super('');
}
class RectangleShape extends Shape {
RectangleShape.fromMap(dynamic map, double scale): super('');
}
class ShapeTrimPath extends Shape {
ShapeTrimPath.fromMap(dynamic map, double scale): super('');
}
class PolystarShape extends Shape {
PolystarShape.fromMap(dynamic map, double scale): super('');
}
class MergePaths extends Shape {
MergePaths.fromMap(dynamic map, double scale): super('');
}
class UnknownShape extends Shape {
UnknownShape() : super('');
}