-
-
Notifications
You must be signed in to change notification settings - Fork 905
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
feat: Callbacks in HudButtonComponent
constructor and ViewportMargin
mixin to avoid code duplication
#1685
Merged
Merged
feat: Callbacks in HudButtonComponent
constructor and ViewportMargin
mixin to avoid code duplication
#1685
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
70ac1fe
refactor: ViewportMargin mixin to avoid code duplication
spydon dc43979
ViewportMargin -> ComponentViewportMargin
spydon 0f1d584
Fix analysis error
spydon ad1d5c0
Without widget
spydon 744403d
Add test for HudButtonComponent
spydon 85f3bb8
Add ButtonComponentTest
spydon f8be6c5
Fix analyze
spydon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
82 changes: 82 additions & 0 deletions
82
packages/flame/lib/src/components/mixins/component_viewport_margin.dart
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,82 @@ | ||
import 'package:flame/components.dart'; | ||
import 'package:flame/game.dart'; | ||
import 'package:flutter/widgets.dart' show EdgeInsets; | ||
import 'package:meta/meta.dart'; | ||
|
||
/// The [ComponentViewportMargin] positions itself by a margin to the edge of | ||
/// the [Viewport] instead of by an absolute position on the screen or on the | ||
/// game, so if the game is resized the component will move to keep its margin. | ||
/// | ||
/// Note that the margin is calculated to the [Anchor], not to the edge of the | ||
/// component. | ||
/// | ||
/// If you set the position of the component instead of a margin when | ||
/// initializing the component, the margin to the edge of the screen from that | ||
/// position will be used. | ||
mixin ComponentViewportMargin on PositionComponent, HasGameRef { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Idk if hud button or button component have tests, but if they don't this would be a good opportunity to introduce a test suite that would end up testing both things. |
||
@override | ||
PositionType positionType = PositionType.viewport; | ||
|
||
/// Instead of setting a position of the [PositionComponent] that uses | ||
/// [ComponentViewportMargin] a margin from the edges of the viewport can be | ||
/// used instead. | ||
EdgeInsets? margin; | ||
|
||
@override | ||
@mustCallSuper | ||
Future<void> onLoad() async { | ||
super.onLoad(); | ||
// If margin is not null we will update the position `onGameResize` instead | ||
if (margin == null) { | ||
final screenSize = gameRef.size; | ||
final topLeft = anchor.toOtherAnchorPosition( | ||
position, | ||
Anchor.topLeft, | ||
scaledSize, | ||
); | ||
final bottomRight = screenSize - | ||
anchor.toOtherAnchorPosition( | ||
position, | ||
Anchor.bottomRight, | ||
scaledSize, | ||
); | ||
margin = EdgeInsets.fromLTRB( | ||
topLeft.x, | ||
topLeft.y, | ||
bottomRight.x, | ||
bottomRight.y, | ||
); | ||
} else { | ||
size.addListener(_updateMargins); | ||
} | ||
_updateMargins(); | ||
} | ||
|
||
@override | ||
@mustCallSuper | ||
void onGameResize(Vector2 gameSize) { | ||
super.onGameResize(gameSize); | ||
if (isMounted) { | ||
_updateMargins(); | ||
} | ||
} | ||
|
||
void _updateMargins() { | ||
final screenSize = positionType == PositionType.viewport | ||
? gameRef.camera.viewport.effectiveSize | ||
: gameRef.canvasSize; | ||
final margin = this.margin!; | ||
final x = margin.left != 0 | ||
? margin.left + scaledSize.x / 2 | ||
: screenSize.x - margin.right - scaledSize.x / 2; | ||
final y = margin.top != 0 | ||
? margin.top + scaledSize.y / 2 | ||
: screenSize.y - margin.bottom - scaledSize.y / 2; | ||
position.setValues(x, y); | ||
position = Anchor.center.toOtherAnchorPosition( | ||
position, | ||
anchor, | ||
scaledSize, | ||
); | ||
} | ||
} |
111 changes: 111 additions & 0 deletions
111
packages/flame/test/components/button_component_test.dart
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,111 @@ | ||
import 'package:flame/components.dart'; | ||
import 'package:flame/input.dart'; | ||
import 'package:flame_test/flame_test.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
import '../game/flame_game_test.dart'; | ||
|
||
void main() async { | ||
group('ButtonComponent', () { | ||
testWithGame<GameWithTappables>( | ||
'correctly registers taps', GameWithTappables.new, (game) async { | ||
var pressedTimes = 0; | ||
var releasedTimes = 0; | ||
final initialGameSize = Vector2.all(100); | ||
final componentSize = Vector2.all(10); | ||
final buttonPosition = Vector2.all(100); | ||
late final ButtonComponent button; | ||
game.onGameResize(initialGameSize); | ||
await game.ensureAdd( | ||
button = ButtonComponent( | ||
button: RectangleComponent(size: componentSize), | ||
onPressed: () => pressedTimes++, | ||
onReleased: () => releasedTimes++, | ||
position: buttonPosition, | ||
size: componentSize, | ||
), | ||
); | ||
|
||
expect(pressedTimes, 0); | ||
expect(releasedTimes, 0); | ||
|
||
game.onTapDown(1, createTapDownEvent(game)); | ||
expect(pressedTimes, 0); | ||
expect(releasedTimes, 0); | ||
|
||
game.onTapUp( | ||
1, | ||
createTapUpEvent( | ||
game, | ||
globalPosition: button.positionOfAnchor(Anchor.center).toOffset(), | ||
), | ||
); | ||
expect(pressedTimes, 0); | ||
expect(releasedTimes, 0); | ||
|
||
game.onTapDown( | ||
1, | ||
createTapDownEvent( | ||
game, | ||
globalPosition: buttonPosition.toOffset(), | ||
), | ||
); | ||
expect(pressedTimes, 1); | ||
expect(releasedTimes, 0); | ||
|
||
game.onTapUp( | ||
1, | ||
createTapUpEvent( | ||
game, | ||
globalPosition: buttonPosition.toOffset(), | ||
), | ||
); | ||
expect(pressedTimes, 1); | ||
expect(releasedTimes, 1); | ||
}); | ||
|
||
testWithGame<GameWithTappables>( | ||
'correctly registers taps onGameResize', GameWithTappables.new, | ||
(game) async { | ||
var pressedTimes = 0; | ||
var releasedTimes = 0; | ||
final initialGameSize = Vector2.all(100); | ||
final componentSize = Vector2.all(10); | ||
final buttonPosition = Vector2.all(100); | ||
late final ButtonComponent button; | ||
game.onGameResize(initialGameSize); | ||
await game.ensureAdd( | ||
button = ButtonComponent( | ||
button: RectangleComponent(size: componentSize), | ||
onPressed: () => pressedTimes++, | ||
onReleased: () => releasedTimes++, | ||
position: buttonPosition, | ||
size: componentSize, | ||
), | ||
); | ||
final previousPosition = | ||
button.positionOfAnchor(Anchor.center).toOffset(); | ||
game.onGameResize(initialGameSize * 2); | ||
|
||
game.onTapDown( | ||
1, | ||
createTapDownEvent( | ||
game, | ||
globalPosition: previousPosition, | ||
), | ||
); | ||
expect(pressedTimes, 1); | ||
expect(releasedTimes, 0); | ||
|
||
game.onTapUp( | ||
1, | ||
createTapUpEvent( | ||
game, | ||
globalPosition: previousPosition, | ||
), | ||
); | ||
expect(pressedTimes, 1); | ||
expect(releasedTimes, 1); | ||
}); | ||
}); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldnt this be part of a separate PR? For the sake of clear commits
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was the other way around really, I wanted the button components to be the same and to do that I introduced the mixin, I can change the title to reflect on that.