Skip to content

Commit

Permalink
feat: Add showBackground and backgroundColor attributes
Browse files Browse the repository at this point in the history
Enables giving a character a solid background in a different color.
This prevents clutter when the character is drawn
on top of a background with guides or other elements.
  • Loading branch information
Mr-Pepe committed Jun 6, 2024
1 parent 29a6541 commit ebb2ab2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
20 changes: 20 additions & 0 deletions lib/src/stroke_order_animation_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ class StrokeOrderAnimationController extends ChangeNotifier {
/// * Animation speed of stroke animations and hints in quiz mode (try 3 and adjust from there)
/// * Whether to show/hide strokes
/// * Whether to show/hide outlines
/// * Whether to show/hide backgrounds for the strokes
/// * Whether to show/hide medians
/// * Whether to show/hide the correct strokes the user writes during a quiz
/// * Whether to highlight radicals
/// * Stroke color
/// * Outline color
/// * Background color (of the strokes, not the whole widget)
/// * Median color
/// * Radical color
/// * Brush color in quiz mode
Expand All @@ -61,11 +63,13 @@ class StrokeOrderAnimationController extends ChangeNotifier {
double hintAnimationSpeed = 3,
bool showStroke = true,
bool showOutline = true,
bool showBackground = false,
bool showMedian = false,
bool showUserStroke = false,
bool highlightRadical = false,
Color strokeColor = Colors.blue,
Color outlineColor = Colors.black,
Color backgroundColor = Colors.grey,
Color medianColor = Colors.black,
Color radicalColor = Colors.red,
Color brushColor = Colors.black,
Expand All @@ -80,10 +84,12 @@ class StrokeOrderAnimationController extends ChangeNotifier {
}) : _strokeColor = strokeColor,
_showStroke = showStroke,
_showOutline = showOutline,
_showBackground = showBackground,
_showMedian = showMedian,
_showUserStroke = showUserStroke,
_highlightRadical = highlightRadical,
_outlineColor = outlineColor,
_backgroundColor = backgroundColor,
_medianColor = medianColor,
_radicalColor = radicalColor,
_brushColor = brushColor,
Expand Down Expand Up @@ -155,25 +161,29 @@ class StrokeOrderAnimationController extends ChangeNotifier {

bool _showStroke;
bool _showOutline;
bool _showBackground;
bool _showMedian;
bool _showUserStroke;
bool _highlightRadical;

bool get showStroke => _showStroke;
bool get showOutline => _showOutline;
bool get showBackground => _showBackground;
bool get showMedian => _showMedian;
bool get showUserStroke => _showUserStroke;
bool get highlightRadical => _highlightRadical;

Color _strokeColor;
Color _outlineColor;
Color _backgroundColor;
Color _medianColor;
Color _radicalColor;
Color _brushColor;
Color _hintColor;

Color get strokeColor => _strokeColor;
Color get outlineColor => _outlineColor;
Color get backgroundColor => _backgroundColor;
Color get medianColor => _medianColor;
Color get radicalColor => _radicalColor;
Color get brushColor => _brushColor;
Expand Down Expand Up @@ -324,6 +334,11 @@ class StrokeOrderAnimationController extends ChangeNotifier {
notifyListeners();
}

void setShowBackground(bool value) {
_showBackground = value;
notifyListeners();
}

void setShowMedian(bool value) {
_showMedian = value;
notifyListeners();
Expand All @@ -344,6 +359,11 @@ class StrokeOrderAnimationController extends ChangeNotifier {
notifyListeners();
}

void setBackgroundColor(Color value) {
_backgroundColor = value;
notifyListeners();
}

void setMedianColor(Color value) {
_medianColor = value;
notifyListeners();
Expand Down
39 changes: 28 additions & 11 deletions lib/src/stroke_order_animator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ class StrokeOrderAnimatorState extends State<StrokeOrderAnimator> {
showOutline: widget._controller.showOutline,
outlineColor: widget._controller.outlineColor,
outlineWidth: widget._controller.outlineWidth,
showBackground: widget._controller.showBackground,
backgroundColor: widget._controller.backgroundColor,
showMedian: widget._controller.showMedian,
medianColor: widget._controller.medianColor,
medianWidth: widget._controller.medianWidth,
Expand Down Expand Up @@ -162,17 +164,19 @@ class StrokeOrderAnimatorState extends State<StrokeOrderAnimator> {
class StrokePainter extends CustomPainter {
StrokePainter(
this.strokeOutlinePath, {
this.showStroke = true,
this.strokeColor = Colors.grey,
this.showOutline = false,
this.outlineColor = Colors.black,
this.outlineWidth = 2.0,
this.showMedian = false,
this.medianColor = Colors.black,
this.medianWidth = 2.0,
this.animate = false,
this.animation,
this.median = const [],
required this.showStroke,
required this.strokeColor,
required this.showBackground,
required this.backgroundColor,
required this.showOutline,
required this.outlineColor,
required this.outlineWidth,
required this.showMedian,
required this.medianColor,
required this.medianWidth,
required this.animate,
required this.animation,
required this.median,
}) : super(repaint: animation);
// If the stroke should be animated, an animation and the median have to be provided
final bool animate;
Expand All @@ -181,6 +185,8 @@ class StrokePainter extends CustomPainter {
final Color strokeColor;
final Color outlineColor;
final double outlineWidth;
final bool showBackground;
final Color backgroundColor;
final Color medianColor;
final double medianWidth;
final bool showOutline;
Expand All @@ -195,6 +201,17 @@ class StrokePainter extends CustomPainter {

@override
void paint(Canvas canvas, Size size) {
if (showBackground) {
final backgroundPaint = Paint()
..color = backgroundColor
..style = PaintingStyle.fill;

canvas.drawPath(
_scalePath(strokeOutlinePath, size),
backgroundPaint,
);
}

if (strokeStart < 0) {
// Calculate the points on strokeOutlinePath that are closest to the start and end points of the median
strokeStart = getClosestPointOnPathAsDistanceOnPath(
Expand Down

0 comments on commit ebb2ab2

Please sign in to comment.