Skip to content

Commit

Permalink
refactor: Change the ClipComponent factory Constructor to redirect Co…
Browse files Browse the repository at this point in the history
…nstructor (#3089)

The ClipComponent factory Constructor "circle", "rectangle" and
"polygon" are now redirect Constructor while they persist their previous
behaviour and syntax.
This allows subclasses of ClipComponent to directly address them.
```dart
/// Examples: 

SubClass.fancyShape() : super.polygon();
Smiley() : super.circle();
```

---------

Co-authored-by: Lukas Klingsbo <lukas.klingsbo@gmail.com>
  • Loading branch information
YukiAttano and spydon committed Mar 21, 2024
1 parent 8f50c92 commit cc035fb
Showing 1 changed file with 58 additions and 54 deletions.
112 changes: 58 additions & 54 deletions packages/flame/lib/src/components/clip_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ClipComponent extends PositionComponent implements SizeProvider {
/// {@macro circle_clip_component}
///
/// Clips the canvas in the form of a circle based on its size.
factory ClipComponent.circle({
ClipComponent.circle({
Vector2? position,
Vector2? size,
Vector2? scale,
Expand All @@ -38,24 +38,22 @@ class ClipComponent extends PositionComponent implements SizeProvider {
Iterable<Component>? children,
int? priority,
ComponentKey? key,
}) {
return ClipComponent(
builder: (size) => Circle(size / 2, size.x / 2),
position: position,
size: size,
scale: scale,
angle: angle,
anchor: anchor,
children: children,
priority: priority,
key: key,
);
}
}) : this(
builder: (size) => Circle(size / 2, size.x / 2),
position: position,
size: size,
scale: scale,
angle: angle,
anchor: anchor,
children: children,
priority: priority,
key: key,
);

/// {@macro rectangle_clip_component}
///
/// Clips the canvas in the form of a rectangle based on its size.
factory ClipComponent.rectangle({
ClipComponent.rectangle({
Vector2? position,
Vector2? size,
Vector2? scale,
Expand All @@ -64,24 +62,22 @@ class ClipComponent extends PositionComponent implements SizeProvider {
Iterable<Component>? children,
int? priority,
ComponentKey? key,
}) {
return ClipComponent(
builder: (size) => Rectangle.fromRect(size.toRect()),
position: position,
size: size,
scale: scale,
angle: angle,
anchor: anchor,
children: children,
priority: priority,
key: key,
);
}
}) : this(
builder: (size) => Rectangle.fromRect(size.toRect()),
position: position,
size: size,
scale: scale,
angle: angle,
anchor: anchor,
children: children,
priority: priority,
key: key,
);

/// {@macro polygon_clip_component}
///
/// Clips the canvas in the form of a polygon based on its size.
factory ClipComponent.polygon({
ClipComponent.polygon({
required List<Vector2> points,
Vector2? position,
Vector2? size,
Expand All @@ -91,31 +87,17 @@ class ClipComponent extends PositionComponent implements SizeProvider {
Iterable<Component>? children,
int? priority,
ComponentKey? key,
}) {
assert(
points.length > 2,
'PolygonClipComponent requires at least 3 points.',
);

return ClipComponent(
builder: (size) {
final translatedPoints = points
.map(
(p) => p.clone()..multiply(size),
)
.toList();
return Polygon(translatedPoints);
},
position: position,
size: size,
scale: scale,
angle: angle,
anchor: anchor,
children: children,
priority: priority,
key: key,
);
}
}) : this(
builder: _polygonShapeBuilder(points),
position: position,
size: size,
scale: scale,
angle: angle,
anchor: anchor,
children: children,
priority: priority,
key: key,
);

late Path _path;
late Shape _shape;
Expand Down Expand Up @@ -144,4 +126,26 @@ class ClipComponent extends PositionComponent implements SizeProvider {
bool containsLocalPoint(Vector2 point) {
return _shape.containsPoint(point);
}

/// Returns the [ShapeBuilder] function that builds a polygon
///
/// this allows us to use an assertion during Constructor initialization
/// rather than at the execution of the builder function.
static ShapeBuilder _polygonShapeBuilder(List<Vector2> points) {
assert(
points.length >= 3,
'PolygonClipComponent requires at least 3 points.',
);

return (Vector2 size) => _polygonBuilder(points, size);
}

static Shape _polygonBuilder(List<Vector2> points, Vector2 size) {
final translatedPoints = points
.map(
(p) => p.clone()..multiply(size),
)
.toList();
return Polygon(translatedPoints);
}
}

0 comments on commit cc035fb

Please sign in to comment.