Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Antialiasing flag #228

Merged
merged 3 commits into from
Jul 6, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/penguin_dance/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class _MyHomePageState extends State<MyHomePage> with FlareController {
Positioned.fill(
child: FlareActor("assets/Penguin.flr",
alignment: Alignment.center,
isPaused: _isPaused,
isPaused: _isPaused,
fit: BoxFit.cover,
animation: "walk",
controller: this)),
Expand Down
1 change: 1 addition & 0 deletions example/teddy/lib/teddy_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class TeddyController extends FlareControls {
play("idle");
}

@override
onCompleted(String name) {
play("idle");
}
Expand Down
9 changes: 7 additions & 2 deletions example/teddy/lib/tracking_text_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ typedef void TextChanged(String text);
// Helper widget to track caret position.
class TrackingTextInput extends StatefulWidget {
TrackingTextInput(
{Key key, this.onCaretMoved, this.onTextChanged, this.hint, this.label, this.isObscured = false})
{Key key,
this.onCaretMoved,
this.onTextChanged,
this.hint,
this.label,
this.isObscured = false})
: super(key: key);
final CaretMoved onCaretMoved;
final TextChanged onTextChanged;
Expand Down Expand Up @@ -61,7 +66,7 @@ class _TrackingTextInputState extends State<TrackingTextInput> {
),
key: _fieldKey,
controller: _textController,
obscureText: widget.isObscured,
obscureText: widget.isObscured,
validator: (value) {}),
);
}
Expand Down
98 changes: 84 additions & 14 deletions flare_flutter/lib/flare.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ export 'package:flare_dart/animation/actor_animation.dart';
export 'package:flare_dart/actor_node.dart';

abstract class FlutterActorDrawable {
bool _useAntialias;
ui.BlendMode _blendMode;

int get blendModeId {
return _blendMode.index;
}
Expand All @@ -54,6 +56,15 @@ abstract class FlutterActorDrawable {
onBlendModeChanged(_blendMode);
}

bool get useAntialias => _useAntialias;
set useAntialias(bool value) {
if (value != _useAntialias) {
_useAntialias = value;
onAntialiasChanged(_useAntialias);
}
}

void onAntialiasChanged(bool useAA);
void onBlendModeChanged(ui.BlendMode blendMode);

void draw(ui.Canvas canvas);
Expand Down Expand Up @@ -233,8 +244,7 @@ class FlutterActorShape extends ActorShape with FlutterActorDrawable {
stroke?.markPathEffectsDirty();
}

@override
void onBlendModeChanged(ui.BlendMode mode) {
void _markPaintDirty() {
if (fills != null) {
for (final ActorFill actorFill in fills) {
(actorFill as ActorPaint).markPaintDirty();
Expand All @@ -247,6 +257,16 @@ class FlutterActorShape extends ActorShape with FlutterActorDrawable {
}
}

@override
void onBlendModeChanged(ui.BlendMode mode) {
_markPaintDirty();
}

@override
void onAntialiasChanged(bool useAA) {
_markPaintDirty();
}

ui.Path get path {
if (_isValid) {
return _path;
Expand Down Expand Up @@ -375,9 +395,11 @@ class FlutterColorFill extends ColorFill with FlutterFill {
@override
void update(int dirt) {
super.update(dirt);
var parentShape = parent as FlutterActorShape;
_paint
..color = uiColor
..blendMode = (parent as FlutterActorShape).blendMode;
..isAntiAlias = parentShape.useAntialias
..blendMode = parentShape.blendMode;
onPaintUpdated(_paint);
}
}
Expand Down Expand Up @@ -407,10 +429,13 @@ class FlutterColorStroke extends ColorStroke with FlutterStroke {
@override
void update(int dirt) {
super.update(dirt);

var parentShape = parent as FlutterActorShape;
_paint
..color = uiColor
..strokeWidth = width
..blendMode = (parent as FlutterActorShape).blendMode;
..isAntiAlias = parentShape.useAntialias
..blendMode = parentShape.blendMode;
onPaintUpdated(_paint);
}
}
Expand Down Expand Up @@ -456,9 +481,12 @@ class FlutterGradientFill extends GradientFill with FlutterFill {
(overrideColor[2] * 255.0).round(),
o);
}

var parentShape = parent as FlutterActorShape;
_paint
..color = paintColor
..blendMode = (parent as FlutterActorShape).blendMode
..isAntiAlias = parentShape.useAntialias
..blendMode = parentShape.blendMode
..shader = ui.Gradient.linear(ui.Offset(renderStart[0], renderStart[1]),
ui.Offset(renderEnd[0], renderEnd[1]), colors, stops);
onPaintUpdated(_paint);
Expand Down Expand Up @@ -513,9 +541,12 @@ class FlutterGradientStroke extends GradientStroke with FlutterStroke {
(overrideColor[2] * 255.0).round(),
o);
}

var parentShape = parent as FlutterActorShape;
_paint
..color = paintColor
..blendMode = (parent as FlutterActorShape).blendMode
..isAntiAlias = parentShape.useAntialias
..blendMode = parentShape.blendMode
..strokeWidth = width
..shader = ui.Gradient.linear(ui.Offset(renderStart[0], renderStart[1]),
ui.Offset(renderEnd[0], renderEnd[1]), colors, stops);
Expand Down Expand Up @@ -579,9 +610,11 @@ class FlutterRadialFill extends RadialGradientFill with FlutterFill {
o);
}

var parentShape = parent as FlutterActorShape;
_paint
..color = paintColor
..blendMode = (parent as FlutterActorShape).blendMode
..isAntiAlias = parentShape.useAntialias
..blendMode = parentShape.blendMode
..shader = radial;
onPaintUpdated(_paint);
}
Expand Down Expand Up @@ -637,10 +670,12 @@ class FlutterRadialStroke extends RadialGradientStroke with FlutterStroke {
o);
}

var parentShape = parent as FlutterActorShape;
_paint
..color = paintColor
..strokeWidth = width
..blendMode = (parent as FlutterActorShape).blendMode
..isAntiAlias = parentShape.useAntialias
..blendMode = parentShape.blendMode
..shader = ui.Gradient.radial(Offset(renderStart[0], renderStart[1]),
radius, colors, stops, ui.TileMode.clamp);
onPaintUpdated(_paint);
Expand Down Expand Up @@ -812,8 +847,21 @@ class FlutterActor extends Actor {
}

class FlutterActorArtboard extends ActorArtboard {
bool _useAntialias;
FlutterActorArtboard(FlutterActor actor) : super(actor);

bool get useAntialias => _useAntialias;
set useAntialias(bool value) {
if (_useAntialias != value) {
_useAntialias = value;
if (drawableNodes != null) {
for (final ActorDrawable drawable in drawableNodes) {
(drawable as FlutterActorDrawable).useAntialias = _useAntialias;
}
}
}
}

void draw(ui.Canvas canvas) {
if (clipContents) {
canvas.save();
Expand Down Expand Up @@ -1066,7 +1114,7 @@ class FlutterActorImage extends ActorImage with FlutterActorDrawable {
ui.TileMode.clamp, _identityMatrix)
: null
..filterQuality = ui.FilterQuality.low
..isAntiAlias = true;
..isAntiAlias = useAntialias;
onPaintUpdated(_paint);
}
}
Expand All @@ -1086,6 +1134,14 @@ class FlutterActorImage extends ActorImage with FlutterActorDrawable {
}
}

@override
void onAntialiasChanged(bool useAA) {
if (_paint != null) {
_paint.isAntiAlias = useAA;
onPaintUpdated(_paint);
}
}

/// Swap the image used to draw the mesh for this image node.
/// Returns true when successful.
bool changeImage(ui.Image image) {
Expand Down Expand Up @@ -1187,9 +1243,8 @@ class FlutterActorImage extends ActorImage with FlutterActorDrawable {
..shader = image != null
? ui.ImageShader(
image, ui.TileMode.clamp, ui.TileMode.clamp, _identityMatrix)
: null;
_paint.filterQuality = ui.FilterQuality.low;
_paint.isAntiAlias = true;
: null
..filterQuality = ui.FilterQuality.low;
onPaintUpdated(_paint);
}

Expand Down Expand Up @@ -1322,7 +1377,7 @@ class FlutterActorLayerEffectRenderer extends ActorLayerEffectRenderer

double baseBlurX = 0;
double baseBlurY = 0;
Paint layerPaint = Paint();
Paint layerPaint = Paint()..isAntiAlias = useAntialias;
Color layerColor = Colors.white.withOpacity(parent.renderOpacity);
layerPaint.color = layerColor;
if (blur?.isActive ?? false) {
Expand All @@ -1343,6 +1398,7 @@ class FlutterActorLayerEffectRenderer extends ActorLayerEffectRenderer
var color = dropShadow.color;
canvas.translate(dropShadow.offsetX, dropShadow.offsetY);
var shadowPaint = Paint()
..isAntiAlias = useAntialias
..color = layerColor
..imageFilter = _blurFilter(
dropShadow.blurX + baseBlurX, dropShadow.blurY + baseBlurY)
Expand Down Expand Up @@ -1375,7 +1431,9 @@ class FlutterActorLayerEffectRenderer extends ActorLayerEffectRenderer
// to then draw the shadow on top of with srcIn to only show the
// shadow and finally composite with the desired blend mode requested
// here.
var extraLayerPaint = Paint()..blendMode = blendMode;
var extraLayerPaint = Paint()
..blendMode = blendMode
..isAntiAlias = useAntialias;
drawPass(canvas, bounds, extraLayerPaint);
}

Expand All @@ -1385,6 +1443,7 @@ class FlutterActorLayerEffectRenderer extends ActorLayerEffectRenderer

var color = innerShadow.color;
var shadowPaint = Paint()
..isAntiAlias = useAntialias
..color = layerColor
..blendMode =
extraBlendPass ? ui.BlendMode.srcIn : ui.BlendMode.srcATop
Expand All @@ -1403,6 +1462,7 @@ class FlutterActorLayerEffectRenderer extends ActorLayerEffectRenderer

// Invert the alpha to compute inner part.
var invertPaint = Paint()
..isAntiAlias = useAntialias
..colorFilter = const ui.ColorFilter.matrix([
1,
0,
Expand Down Expand Up @@ -1517,6 +1577,7 @@ class FlutterActorLayerEffectRenderer extends ActorLayerEffectRenderer
}

maskPaint.blendMode = BlendMode.dstIn;
maskPaint.isAntiAlias = useAntialias;
canvas.saveLayer(bounds, maskPaint);
for (final drawable in renderMask.drawables) {
bool wasHidden = drawable.isHidden;
Expand All @@ -1537,4 +1598,13 @@ class FlutterActorLayerEffectRenderer extends ActorLayerEffectRenderer
// We don't currently support custom blend modes on the layer effect
// renderer.
}

@override
void onAntialiasChanged(bool useAA) {
for (final drawable in drawables) {
if (drawable is FlutterActorDrawable) {
(drawable as FlutterActorDrawable).useAntialias = useAA;
}
}
}
}
29 changes: 25 additions & 4 deletions flare_flutter/lib/flare_actor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ class FlareActor extends LeafRenderObjectWidget {
/// dimensions of this widget.
final bool sizeFromArtboard;

/// When false disables antialiasing on drawables.
final bool useAntialias;

const FlareActor(
this.filename, {
this.boundsNode,
Expand All @@ -88,6 +91,7 @@ class FlareActor extends LeafRenderObjectWidget {
this.shouldClip = true,
this.sizeFromArtboard = false,
this.artboard,
this.useAntialias = true,
}) : flareProvider = null;

FlareActor.bundle(
Expand All @@ -104,6 +108,7 @@ class FlareActor extends LeafRenderObjectWidget {
this.shouldClip = true,
this.sizeFromArtboard = false,
this.artboard,
this.useAntialias = true,
AssetBundle bundle,
}) : filename = null,
flareProvider = AssetFlare(bundle: bundle ?? rootBundle, name: name);
Expand All @@ -122,6 +127,7 @@ class FlareActor extends LeafRenderObjectWidget {
this.shouldClip = true,
this.sizeFromArtboard = false,
this.artboard,
this.useAntialias = true,
}) : filename = null,
flareProvider = MemoryFlare(bytes: bytes);

Expand All @@ -139,7 +145,8 @@ class FlareActor extends LeafRenderObjectWidget {
this.shouldClip = true,
this.sizeFromArtboard = false,
this.artboard,
}) : filename = null;
this.useAntialias = true,
}) : filename = null;

@override
RenderObject createRenderObject(BuildContext context) {
Expand All @@ -157,7 +164,8 @@ class FlareActor extends LeafRenderObjectWidget {
..shouldClip = shouldClip
..boundsNodeName = boundsNode
..useIntrinsicSize = sizeFromArtboard
..artboardName = artboard;
..artboardName = artboard
..useAntialias = useAntialias;
}

@override
Expand All @@ -175,7 +183,8 @@ class FlareActor extends LeafRenderObjectWidget {
..shouldClip = shouldClip
..boundsNodeName = boundsNode
..useIntrinsicSize = sizeFromArtboard
..artboardName = artboard;
..artboardName = artboard
..useAntialias = useAntialias;
}

@override
Expand Down Expand Up @@ -206,6 +215,7 @@ class FlareActorRenderObject extends FlareRenderBox {
FlareCompletedCallback _completedCallback;
bool snapToEnd = false;
bool _isPaused = false;
bool _useAntialias = true;
FlutterActor _actor;

String get artboardName => _artboardName;
Expand All @@ -226,6 +236,17 @@ class FlareActorRenderObject extends FlareRenderBox {
updatePlayState();
}

bool get useAntialias => _useAntialias;
set useAntialias(bool value) {
if (value != _useAntialias) {
_useAntialias = value;
if (_artboard != null) {
_artboard.useAntialias = _useAntialias;
}
markNeedsPaint();
}
}

final List<FlareAnimationLayer> _animationLayers = [];
bool shouldClip;

Expand Down Expand Up @@ -348,7 +369,7 @@ class FlareActorRenderObject extends FlareRenderBox {
_color.blue / 255.0,
_color.opacity
]);

_artboard.useAntialias = _useAntialias;
if (_controller != null) {
_controller.initialize(_artboard);
}
Expand Down