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

feat: Adding paint attribute to SpriteWidget and SpriteAnimationWidget #3298

Merged
merged 3 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
28 changes: 28 additions & 0 deletions examples/lib/stories/widgets/paints.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import 'dart:ui';

final paintChoices = [
'none',
'transparent',
'red tinted',
'green tinted',
'blue tinted',
];
final paintList = [
null,
Paint()..color = const Color(0x22FFFFFF),
Paint()
..colorFilter = const ColorFilter.mode(
Color(0x88FF0000),
BlendMode.srcATop,
),
Paint()
..colorFilter = const ColorFilter.mode(
Color(0x8800FF00),
BlendMode.srcATop,
),
Paint()
..colorFilter = const ColorFilter.mode(
Color(0x880000FF),
BlendMode.srcATop,
),
];
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:dashbook/dashbook.dart';
import 'package:examples/stories/widgets/paints.dart';
import 'package:flame/extensions.dart';
import 'package:flame/widgets.dart';
import 'package:flutter/widgets.dart';
Expand All @@ -20,6 +21,13 @@ Widget spriteAnimationWidgetBuilder(DashbookContext ctx) {
anchor: Anchor.valueOf(
ctx.listProperty('anchor', 'center', anchorOptions),
),
paint: paintList[paintChoices.indexOf(
ctx.listProperty(
'paint',
'none',
paintChoices,
),
)],
),
);
}
8 changes: 8 additions & 0 deletions examples/lib/stories/widgets/sprite_widget_example.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:math';

import 'package:dashbook/dashbook.dart';
import 'package:examples/stories/widgets/paints.dart';
import 'package:flame/widgets.dart';
import 'package:flutter/material.dart';

Expand All @@ -17,6 +18,13 @@ Widget spriteWidgetBuilder(DashbookContext ctx) {
anchor: Anchor.valueOf(
ctx.listProperty('anchor', 'center', anchorOptions),
),
paint: paintList[paintChoices.indexOf(
ctx.listProperty(
'paint',
'none',
paintChoices,
),
)],
),
);
}
11 changes: 11 additions & 0 deletions packages/flame/lib/src/widgets/animation_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class SpriteAnimationWidget extends StatelessWidget {
/// A callback that is called when the animation completes.
final VoidCallback? onComplete;

/// A custom [Paint] to be used when rendering the sprite.
/// When omitted the default paint from the [Sprite] class will be used.
final Paint? paint;

const SpriteAnimationWidget({
required SpriteAnimation animation,
required SpriteAnimationTicker animationTicker,
Expand All @@ -38,6 +42,7 @@ class SpriteAnimationWidget extends StatelessWidget {
this.errorBuilder,
this.loadingBuilder,
this.onComplete,
this.paint,
super.key,
}) : _animationFuture = animation,
_animationTicker = animationTicker;
Expand All @@ -57,6 +62,7 @@ class SpriteAnimationWidget extends StatelessWidget {
this.errorBuilder,
this.loadingBuilder,
this.onComplete,
this.paint,
super.key,
}) : _animationFuture = SpriteAnimation.load(path, data, images: images),
_animationTicker = null;
Expand All @@ -74,6 +80,7 @@ class SpriteAnimationWidget extends StatelessWidget {
animationTicker: ticker,
anchor: anchor,
playing: playing,
paint: paint,
);
},
errorBuilder: errorBuilder,
Expand All @@ -97,11 +104,14 @@ class InternalSpriteAnimationWidget extends StatefulWidget {
/// Should the [animation] be playing or not
final bool playing;

final Paint? paint;

const InternalSpriteAnimationWidget({
required this.animation,
required this.animationTicker,
this.playing = true,
this.anchor = Anchor.topLeft,
this.paint,
super.key,
});

Expand Down Expand Up @@ -193,6 +203,7 @@ class _InternalSpriteAnimationWidgetState
painter: SpritePainter(
widget.animationTicker.getSprite(),
widget.anchor,
widget.paint,
),
);
}
Expand Down
14 changes: 10 additions & 4 deletions packages/flame/lib/src/widgets/sprite_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ import 'package:flutter/widgets.dart';
class SpritePainter extends CustomPainter {
final Sprite _sprite;
final Anchor _anchor;
final Paint? _paint;
final double _angle;

SpritePainter(this._sprite, this._anchor, {double angle = 0})
SpritePainter(this._sprite, this._anchor, this._paint, {double angle = 0})
: _angle = angle;

@override
bool shouldRepaint(SpritePainter oldDelegate) {
return oldDelegate._sprite != _sprite ||
oldDelegate._anchor != _anchor ||
oldDelegate._angle != _angle;
oldDelegate._angle != _angle ||
oldDelegate._paint != _paint;
}

@override
Expand All @@ -33,12 +35,16 @@ class SpritePainter extends CustomPainter {
canvas.translateVector(boxAnchorPosition..sub(spriteAnchorPosition));

if (_angle == 0) {
_sprite.render(canvas, size: paintSize);
_sprite.render(canvas, size: paintSize, overridePaint: _paint);
} else {
canvas.renderRotated(
_angle,
spriteAnchorPosition,
(canvas) => _sprite.render(canvas, size: paintSize),
(canvas) => _sprite.render(
canvas,
size: paintSize,
overridePaint: _paint,
),
);
}
}
Expand Down
12 changes: 11 additions & 1 deletion packages/flame/lib/src/widgets/sprite_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ class SpriteWidget extends StatelessWidget {
/// A builder function that is called while the loading is on the way
final WidgetBuilder? loadingBuilder;

/// A custom [Paint] to be used when rendering the sprite.
/// When omitted the default paint from the [Sprite] class will be used.
final Paint? paint;

final FutureOr<Sprite> _spriteFuture;

/// renders the [sprite] as a Widget.
Expand All @@ -36,6 +40,7 @@ class SpriteWidget extends StatelessWidget {
this.angle = 0,
this.errorBuilder,
this.loadingBuilder,
this.paint,
super.key,
}) : _spriteFuture = sprite;

Expand All @@ -54,6 +59,7 @@ class SpriteWidget extends StatelessWidget {
Vector2? srcSize,
this.errorBuilder,
this.loadingBuilder,
this.paint,
super.key,
}) : _spriteFuture = Sprite.load(
path,
Expand All @@ -71,6 +77,7 @@ class SpriteWidget extends StatelessWidget {
sprite: sprite,
anchor: anchor,
angle: angle,
paint: paint,
);
},
errorBuilder: errorBuilder,
Expand All @@ -91,17 +98,20 @@ class InternalSpriteWidget extends StatelessWidget {
/// The angle to rotate this [sprite], in rad. (default = 0)
final double angle;

final Paint? paint;

const InternalSpriteWidget({
required this.sprite,
this.anchor = Anchor.topLeft,
this.angle = 0,
this.paint,
super.key,
});

@override
Widget build(BuildContext context) {
return CustomPaint(
painter: SpritePainter(sprite, anchor, angle: angle),
painter: SpritePainter(sprite, anchor, paint, angle: angle),
size: sprite.srcSize.toSize(),
);
}
Expand Down