-
-
Notifications
You must be signed in to change notification settings - Fork 935
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added AlignComponent layout component (#2350)
This PR adds first layout component: AlignComponent, an equivalent of Align widget in Flutter. AlignComponent sizes itself to its parent, and then keeps its child aligned to the specified anchor within its own bounding box. Also adding onParentResize() lifecycle method, which is similar to onGameResize, but fires whenever the parent of the current component changes its size for any reason. (FlameGame is assumed to have the size canvasSize, and will invoke onParentResize whenever the canvas size changes). Additional layout components are planned to be added in future PRs.
- Loading branch information
Showing
18 changed files
with
403 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# AlignComponent | ||
|
||
```{dartdoc} | ||
:package: flame | ||
:symbol: AlignComponent | ||
:file: src/layout/align_component.dart | ||
[Align]: https://api.flutter.dev/flutter/widgets/Align-class.html | ||
[Alignment]: https://api.flutter.dev/flutter/painting/Alignment-class.html | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Layout | ||
|
||
```{toctree} | ||
:hidden: | ||
AlignComponent <align_component.md> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import 'package:flame/components.dart'; | ||
import 'package:flame/effects.dart'; | ||
import 'package:flame/game.dart'; | ||
import 'package:flame/layout.dart'; | ||
|
||
class AlignComponentExample extends FlameGame { | ||
static const String description = ''' | ||
In this example the AlignComponent is used to arrange the circles | ||
so that there is one in the middle and 8 more surrounding it in | ||
the shape of a diamond. | ||
The arrangement will remain intact if you change the window size. | ||
'''; | ||
|
||
@override | ||
void onLoad() { | ||
addAll([ | ||
AlignComponent( | ||
child: CircleComponent( | ||
radius: 40, | ||
children: [ | ||
SizeEffect.by( | ||
Vector2.all(25), | ||
EffectController( | ||
infinite: true, | ||
duration: 0.75, | ||
reverseDuration: 0.5, | ||
), | ||
), | ||
AlignComponent( | ||
alignment: Anchor.topCenter, | ||
child: CircleComponent( | ||
radius: 10, | ||
anchor: Anchor.bottomCenter, | ||
), | ||
keepChildAnchor: true, | ||
), | ||
AlignComponent( | ||
alignment: Anchor.bottomCenter, | ||
child: CircleComponent( | ||
radius: 10, | ||
anchor: Anchor.topCenter, | ||
), | ||
keepChildAnchor: true, | ||
), | ||
AlignComponent( | ||
alignment: Anchor.centerLeft, | ||
child: CircleComponent( | ||
radius: 10, | ||
anchor: Anchor.centerRight, | ||
), | ||
keepChildAnchor: true, | ||
), | ||
AlignComponent( | ||
alignment: Anchor.centerRight, | ||
child: CircleComponent( | ||
radius: 10, | ||
anchor: Anchor.centerLeft, | ||
), | ||
keepChildAnchor: true, | ||
), | ||
], | ||
), | ||
alignment: Anchor.center, | ||
), | ||
AlignComponent( | ||
child: CircleComponent(radius: 30), | ||
alignment: Anchor.topCenter, | ||
), | ||
AlignComponent( | ||
child: CircleComponent(radius: 30), | ||
alignment: Anchor.bottomCenter, | ||
), | ||
AlignComponent( | ||
child: CircleComponent(radius: 30), | ||
alignment: Anchor.centerLeft, | ||
), | ||
AlignComponent( | ||
child: CircleComponent(radius: 30), | ||
alignment: Anchor.centerRight, | ||
), | ||
AlignComponent( | ||
child: CircleComponent(radius: 10), | ||
alignment: const Anchor(0.25, 0.25), | ||
), | ||
AlignComponent( | ||
child: CircleComponent(radius: 10), | ||
alignment: const Anchor(0.25, 0.75), | ||
), | ||
AlignComponent( | ||
child: CircleComponent(radius: 10), | ||
alignment: const Anchor(0.75, 0.25), | ||
), | ||
AlignComponent( | ||
child: CircleComponent(radius: 10), | ||
alignment: const Anchor(0.75, 0.75), | ||
), | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import 'package:dashbook/dashbook.dart'; | ||
import 'package:examples/commons/commons.dart'; | ||
import 'package:examples/stories/layout/align_component.dart'; | ||
import 'package:flame/game.dart'; | ||
|
||
void addLayoutStories(Dashbook dashbook) { | ||
dashbook.storiesOf('Layout').add( | ||
'AlignComponent', | ||
(_) => GameWidget(game: AlignComponentExample()), | ||
codeLink: baseLink('layout/align_component.dart'), | ||
info: AlignComponentExample.description, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export 'src/layout/align_component.dart' show AlignComponent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.