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

refactor: Change the ClipComponent factory Constructor to redirect Constructor #3089

Merged

Conversation

YukiAttano
Copy link
Contributor

Description

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.

/// Examples: 

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

Checklist

  • I have followed the Contributor Guide when preparing my PR.
  • I have updated/added tests for ALL new/updated/fixed functionality.
  • I have updated/added relevant documentation in docs and added dartdoc comments with ///.
  • I have updated/added relevant examples in examples or docs.

Breaking Change?

  • Yes, this PR is a breaking change.
  • No, this PR is not a breaking change.

Related Issues

Closes #3083

@YukiAttano YukiAttano changed the title refactor: change the ClipComponent factory Constructor to redirect Constructor Refactor: change the ClipComponent factory Constructor to redirect Constructor Mar 21, 2024
@YukiAttano YukiAttano changed the title Refactor: change the ClipComponent factory Constructor to redirect Constructor refactor: change the ClipComponent factory Constructor to redirect Constructor Mar 21, 2024
@YukiAttano YukiAttano changed the title refactor: change the ClipComponent factory Constructor to redirect Constructor Refactor: change the ClipComponent factory Constructor to redirect Constructor Mar 21, 2024
@YukiAttano YukiAttano changed the title Refactor: change the ClipComponent factory Constructor to redirect Constructor Refacter: change the ClipComponent factory Constructor to redirect Constructor Mar 21, 2024
@YukiAttano YukiAttano changed the title Refacter: change the ClipComponent factory Constructor to redirect Constructor Refactor: change the ClipComponent factory Constructor to redirect Constructor Mar 21, 2024
@YukiAttano YukiAttano changed the title Refactor: change the ClipComponent factory Constructor to redirect Constructor refactor: Change the ClipComponent factory Constructor to redirect Constructor Mar 21, 2024
Comment on lines 134 to 150
static ShapeBuilder _polygonBuilderCallback(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);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would merge these two methods and call the resulting method _polygonBuilder, since it isn't really a callback.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea behind them is, that the first gets executed during initialization and therefor the assertion is run imediatly while it then returns the builder function that is used later.

If the assertion is in the builder itself, it gets first executed in the onLoad() method if i am right.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, that makes sense, the second function could still be inlined, but now when looking at it that will be a bit more expensive so probably worth having it like it is now, but coming up with a better name for _polygonBuilderCallback. As always, hard with naming though. 😅

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just _polygonShapeBuilder?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm in hurry but i think they can be merged as there is no way in changing the points for the polygon after calling the constructor. Therefor the assertion would lead back to the source of the error.

Anyway both implementations have pro/cons and are fine for now :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll fix it up then, since you're in a hurry :)

packages/flame/lib/src/components/clip_component.dart Outdated Show resolved Hide resolved
packages/flame/lib/src/components/clip_component.dart Outdated Show resolved Hide resolved
Comment on lines 134 to 150
static ShapeBuilder _polygonBuilderCallback(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);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll fix it up then, since you're in a hurry :)

@spydon spydon merged commit cc035fb into flame-engine:main Mar 21, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Convert ClipComponent factory Constructor to redirecting Constructor.
2 participants