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

feat: add onCancelled to ButtonComponent and HudButtonComponent #2193

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions packages/flame/lib/src/components/input/button_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ class ButtonComponent extends PositionComponent with Tappable {
late final PositionComponent? buttonDown;

/// Callback for what should happen when the button is pressed.
/// If you want to interact with [onTapCancel] it is recommended
/// to extend [ButtonComponent].
void Function()? onPressed;

/// Callback for what should happen when the button is released.
/// If you want to interact with [onTapCancel] it is recommended
/// to extend [ButtonComponent].
void Function()? onReleased;

/// Callback for what should happen when the button is cancelled.
void Function()? onCancelled;

ButtonComponent({
this.button,
this.buttonDown,
this.onPressed,
this.onReleased,
this.onCancelled,
super.position,
Vector2? size,
super.scale,
Expand Down Expand Up @@ -64,7 +64,10 @@ class ButtonComponent extends PositionComponent with Tappable {
@override
@mustCallSuper
bool onTapUp(TapUpInfo info) {
onTapCancel();
if (buttonDown != null) {
buttonDown!.removeFromParent();
button!.parent = this;
}
onReleased?.call();
return true;
}
Expand All @@ -76,6 +79,7 @@ class ButtonComponent extends PositionComponent with Tappable {
buttonDown!.removeFromParent();
button!.parent = this;
}
onCancelled?.call();
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class HudButtonComponent extends ButtonComponent
EdgeInsets? margin,
Function()? super.onPressed,
Function()? super.onReleased,
Function()? super.onCancelled,
super.position,
Vector2? size,
super.scale,
Expand Down
35 changes: 35 additions & 0 deletions packages/flame/test/components/button_component_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ void main() {
'correctly registers taps', GameWithTappables.new, (game) async {
var pressedTimes = 0;
var releasedTimes = 0;
var cancelledTimes = 0;
final initialGameSize = Vector2.all(100);
final componentSize = Vector2.all(10);
final buttonPosition = Vector2.all(100);
Expand All @@ -23,17 +24,20 @@ void main() {
button: RectangleComponent(size: componentSize),
onPressed: () => pressedTimes++,
onReleased: () => releasedTimes++,
onCancelled: () => cancelledTimes++,
position: buttonPosition,
size: componentSize,
),
);

expect(pressedTimes, 0);
expect(releasedTimes, 0);
expect(cancelledTimes, 0);

game.onTapDown(1, createTapDownEvent(game));
expect(pressedTimes, 0);
expect(releasedTimes, 0);
expect(cancelledTimes, 0);

game.onTapUp(
1,
Expand All @@ -44,6 +48,7 @@ void main() {
);
expect(pressedTimes, 0);
expect(releasedTimes, 0);
expect(cancelledTimes, 0);

game.onTapDown(
1,
Expand All @@ -54,6 +59,7 @@ void main() {
);
expect(pressedTimes, 1);
expect(releasedTimes, 0);
expect(cancelledTimes, 0);

game.onTapUp(
1,
Expand All @@ -64,13 +70,27 @@ void main() {
);
expect(pressedTimes, 1);
expect(releasedTimes, 1);
expect(cancelledTimes, 0);

game.onTapDown(
1,
createTapDownEvent(
game,
globalPosition: buttonPosition.toOffset(),
),
);
game.onTapCancel(1);
expect(pressedTimes, 2);
expect(releasedTimes, 1);
expect(cancelledTimes, 1);
});

testWithGame<GameWithTappables>(
'correctly registers taps onGameResize', GameWithTappables.new,
(game) async {
var pressedTimes = 0;
var releasedTimes = 0;
var cancelledTimes = 0;
final initialGameSize = Vector2.all(100);
final componentSize = Vector2.all(10);
final buttonPosition = Vector2.all(100);
Expand All @@ -81,6 +101,7 @@ void main() {
button: RectangleComponent(size: componentSize),
onPressed: () => pressedTimes++,
onReleased: () => releasedTimes++,
onCancelled: () => cancelledTimes++,
position: buttonPosition,
size: componentSize,
),
Expand All @@ -98,6 +119,7 @@ void main() {
);
expect(pressedTimes, 1);
expect(releasedTimes, 0);
expect(cancelledTimes, 0);

game.onTapUp(
1,
Expand All @@ -108,6 +130,19 @@ void main() {
);
expect(pressedTimes, 1);
expect(releasedTimes, 1);
expect(cancelledTimes, 0);

game.onTapDown(
1,
createTapDownEvent(
game,
globalPosition: previousPosition,
),
);
game.onTapCancel(1);
expect(pressedTimes, 2);
expect(releasedTimes, 1);
expect(cancelledTimes, 1);
});

testWidgets(
Expand Down
39 changes: 39 additions & 0 deletions packages/flame/test/components/hud_button_component_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ void main() {
'correctly registers taps', GameWithTappables.new, (game) async {
var pressedTimes = 0;
var releasedTimes = 0;
var cancelledTimes = 0;
final initialGameSize = Vector2.all(100);
final componentSize = Vector2.all(10);
const margin = EdgeInsets.only(bottom: 10, right: 10);
Expand All @@ -23,17 +24,20 @@ void main() {
button: RectangleComponent(size: componentSize),
onPressed: () => pressedTimes++,
onReleased: () => releasedTimes++,
onCancelled: () => cancelledTimes++,
size: componentSize,
margin: margin,
),
);

expect(pressedTimes, 0);
expect(releasedTimes, 0);
expect(cancelledTimes, 0);

game.onTapDown(1, createTapDownEvent(game));
expect(pressedTimes, 0);
expect(releasedTimes, 0);
expect(cancelledTimes, 0);

game.onTapUp(
1,
Expand All @@ -44,6 +48,7 @@ void main() {
);
expect(pressedTimes, 0);
expect(releasedTimes, 0);
expect(cancelledTimes, 0);

game.onTapDown(
1,
Expand All @@ -56,6 +61,7 @@ void main() {
);
expect(pressedTimes, 1);
expect(releasedTimes, 0);
expect(cancelledTimes, 0);

game.onTapUp(
1,
Expand All @@ -66,13 +72,29 @@ void main() {
);
expect(pressedTimes, 1);
expect(releasedTimes, 1);
expect(cancelledTimes, 0);

game.onTapDown(
1,
createTapDownEvent(
game,
globalPosition: initialGameSize.toOffset() +
margin.bottomRight -
const Offset(1, 1),
),
);
game.onTapCancel(1);
expect(pressedTimes, 2);
expect(releasedTimes, 1);
expect(cancelledTimes, 1);
});

testWithGame<GameWithTappables>(
'correctly registers taps onGameResize', GameWithTappables.new,
(game) async {
var pressedTimes = 0;
var releasedTimes = 0;
var cancelledTimes = 0;
final initialGameSize = Vector2.all(100);
final componentSize = Vector2.all(10);
const margin = EdgeInsets.only(bottom: 10, right: 10);
Expand All @@ -83,6 +105,7 @@ void main() {
button: RectangleComponent(size: componentSize),
onPressed: () => pressedTimes++,
onReleased: () => releasedTimes++,
onCancelled: () => cancelledTimes++,
size: componentSize,
margin: margin,
),
Expand All @@ -107,6 +130,7 @@ void main() {
);
expect(pressedTimes, 0);
expect(releasedTimes, 0);
expect(cancelledTimes, 0);

game.onTapDown(
1,
Expand All @@ -118,6 +142,7 @@ void main() {
);
expect(pressedTimes, 1);
expect(releasedTimes, 0);
expect(cancelledTimes, 0);

game.onTapUp(
1,
Expand All @@ -129,6 +154,20 @@ void main() {
);
expect(pressedTimes, 1);
expect(releasedTimes, 1);
expect(cancelledTimes, 0);

game.onTapDown(
1,
createTapDownEvent(
game,
globalPosition:
game.size.toOffset() + margin.bottomRight - const Offset(1, 1),
),
);
game.onTapCancel(1);
expect(pressedTimes, 2);
expect(releasedTimes, 1);
expect(cancelledTimes, 1);
});
});
}