Skip to content

Commit

Permalink
add ShapeStroke and ShapeFill
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiomsr committed May 16, 2017
1 parent 6260823 commit 21de033
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 22 deletions.
4 changes: 2 additions & 2 deletions lib/src/converters.dart
Expand Up @@ -8,11 +8,11 @@ Shape shapeFromMap(dynamic rawShape, double scale) {
case 'gr':
return new ShapeGroup.fromMap(rawShape, scale);
case 'st':
return new ShapeStroke.fromMap(rawShape, scale);
return new ShapeStroke(rawShape, scale);
case 'gs':
return new GradientStroke.fromMap(rawShape, scale);
case 'fl':
return new ShapeFill.fromMap(rawShape, scale);
return new ShapeFill(rawShape, scale);
case 'gf':
return new GradientFill.fromMap(rawShape, scale);
case 'tr':
Expand Down
2 changes: 1 addition & 1 deletion lib/src/drawing/drawing_layers.dart
Expand Up @@ -568,7 +568,7 @@ class CompositionLayer extends BaseLayer {
final String name = layer.layerModel.name;
if(layerName == null) {
layer.addColorFilter(null, null, colorFilter);
} else {
} else if(name == layerName){
layer.addColorFilter(layerName, contentName, colorFilter);
}
}
Expand Down
126 changes: 108 additions & 18 deletions lib/src/shapes.dart
@@ -1,70 +1,160 @@
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;
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 String _name;
final List<Shape> _shapes;

String get name => name;

List<Shape> get shapes => _shapes;

ShapeGroup(this._name, this._shapes);
ShapeGroup(String name, this._shapes) : super(name);

ShapeGroup.fromMap(dynamic map, double scale)
: _name = map['nm'],
_shapes = parseRawShapes(map['it'], 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 {
ShapeStroke.fromMap(dynamic map, double scale);

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 {
ShapeFill.fromMap(dynamic map, double scale);
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);
GradientStroke.fromMap(dynamic map, double scale) : super('');
}

class GradientFill extends Shape {
GradientFill.fromMap(dynamic map, double scale);
GradientFill.fromMap(dynamic map, double scale) : super('');
}

class ShapePath extends Shape {
ShapePath.fromMap(dynamic map, double scale);
ShapePath.fromMap(dynamic map, double scale) : super('');
}

class CircleShape extends Shape {
CircleShape.fromMap(dynamic map, double scale);
CircleShape.fromMap(dynamic map, double scale): super('');
}

class RectangleShape extends Shape {
RectangleShape.fromMap(dynamic map, double scale);
RectangleShape.fromMap(dynamic map, double scale): super('');
}

class ShapeTrimPath extends Shape {
ShapeTrimPath.fromMap(dynamic map, double scale);
ShapeTrimPath.fromMap(dynamic map, double scale): super('');
}

class PolystarShape extends Shape {
PolystarShape.fromMap(dynamic map, double scale);
PolystarShape.fromMap(dynamic map, double scale): super('');
}

class MergePaths extends Shape {
MergePaths.fromMap(dynamic map, double scale);
MergePaths.fromMap(dynamic map, double scale): super('');
}

class UnknownShape extends Shape {}
class UnknownShape extends Shape {
UnknownShape() : super('');
}

2 changes: 1 addition & 1 deletion lib/src/transform.dart
Expand Up @@ -23,7 +23,7 @@ class AnimatableTransform extends Shape {
AnimatableIntegerValue get opacity => _opacity;

AnimatableTransform._(this._anchorPoint, this._position, this._scale,
this._rotation, this._opacity);
this._rotation, this._opacity): super('');


factory AnimatableTransform([dynamic map, double scale]) {
Expand Down

0 comments on commit 21de033

Please sign in to comment.