Skip to content

Commit

Permalink
feat: Add baseColor to Shadow3DDecorator (#3375)
Browse files Browse the repository at this point in the history
The Shadow3DDecorator is very useful, but the shadow defaults to being
pure black. While this is fine in most cases, sometimes a designer might
request something a little bit different, so this change to allow the
base color of the shadow to be changed if so desired.
  • Loading branch information
Taormina authored Dec 3, 2024
1 parent b2e63d8 commit b5d7ee0
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
17 changes: 14 additions & 3 deletions packages/flame/lib/src/rendering/shadow3d_decorator.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import 'dart:ui';

import 'package:flame/src/palette.dart';
import 'package:flame/src/rendering/decorator.dart';
import 'package:vector_math/vector_math_64.dart';
import 'package:vector_math/vector_math_64.dart' show Matrix4, Vector2;

/// [Shadow3DDecorator] casts a realistic-looking shadow from the component
/// onto the ground.
Expand All @@ -24,13 +25,15 @@ class Shadow3DDecorator extends Decorator {
double? yScale,
double? blur,
double? opacity,
Color? baseColor,
}) : _base = base?.clone() ?? Vector2.zero(),
_ascent = ascent ?? 0,
_angle = angle ?? -1.4,
_shift = xShift ?? 100.0,
_scale = yScale ?? 1.0,
_blur = blur ?? 0,
_opacity = opacity ?? 0.6;
_opacity = opacity ?? 0.6,
_baseColor = baseColor ?? BasicPalette.black.color;

/// Coordinates of the point where the component "touches the ground". If the
/// component is airborne (i.e. [ascent] is non-zero), then this should be the
Expand Down Expand Up @@ -117,10 +120,18 @@ class Shadow3DDecorator extends Decorator {
_paint = null;
}

/// Shadow's base color before opacity. This defaults to pitch-black.
Color get baseColor => _baseColor;
Color _baseColor;
set baseColor(Color value) {
_baseColor = value;
_paint = null;
}

Paint? _paint;
Paint _makePaint() {
final paint = Paint();
final color = Color.fromRGBO(0, 0, 0, opacity);
final color = baseColor.withOpacity(opacity);
paint.colorFilter = ColorFilter.mode(color, BlendMode.srcIn);
if (_blur > 0) {
paint.imageFilter = ImageFilter.blur(sigmaX: blur, sigmaY: blur / _scale);
Expand Down
Binary file modified packages/flame/test/_goldens/shadow3d_decorator_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified packages/flame/test/_goldens/shadow3d_decorator_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion packages/flame/test/rendering/shadow3d_decorator_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:ui';

import 'package:flame/components.dart';
import 'package:flame/palette.dart';
import 'package:flame/rendering.dart';
import 'package:flame_test/flame_test.dart';
import 'package:flutter_test/flutter_test.dart';
Expand All @@ -16,6 +17,7 @@ void main() {
expect(decorator.yScale, 1.0);
expect(decorator.blur, 0.0);
expect(decorator.opacity, 0.6);
expect(decorator.baseColor, BasicPalette.black.color);
});

testGolden(
Expand Down Expand Up @@ -79,7 +81,8 @@ void main() {
..xShift = 250.0
..yScale = 1.5
..opacity = 0.4
..blur = 1.0,
..blur = 1.0
..baseColor = BasicPalette.red.color,
),
]);
},
Expand Down

0 comments on commit b5d7ee0

Please sign in to comment.