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 onReleased action to AdvancedButtonComponent which will be called within onTapUp #3152

Merged
merged 2 commits into from
May 10, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class AdvancedButtonComponent extends PositionComponent
with HoverCallbacks, TapCallbacks {
AdvancedButtonComponent({
this.onPressed,
this.onReleased,
this.onChangeState,
PositionComponent? defaultSkin,
PositionComponent? downSkin,
Expand Down Expand Up @@ -46,6 +47,9 @@ class AdvancedButtonComponent extends PositionComponent
/// Callback for what should happen when the button is pressed.
void Function()? onPressed;

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

/// Callback when button state changes
void Function(ButtonState state)? onChangeState;

Expand Down Expand Up @@ -93,8 +97,12 @@ class AdvancedButtonComponent extends PositionComponent

@override
void onTapUp(TapUpEvent event) {
if (_isDisabled) {
return;
}
isPressed = false;
updateState();
onReleased?.call();
}

@override
Expand Down
18 changes: 18 additions & 0 deletions packages/flame/test/components/advanced_button_component_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ void main() {

testWithFlameGame('correctly registers taps', (game) async {
var pressedTimes = 0;
var releasedTimes = 0;
final initialGameSize = Vector2.all(200);
final componentSize = Vector2.all(10);
final buttonPosition = Vector2.all(100);
Expand All @@ -35,16 +36,19 @@ void main() {
button = AdvancedButtonComponent(
defaultSkin: RectangleComponent(size: componentSize),
onPressed: () => pressedTimes++,
onReleased: () => releasedTimes++,
position: buttonPosition,
size: componentSize,
),
);

expect(pressedTimes, 0);
expect(releasedTimes, 0);
final tapDispatcher = game.firstChild<MultiTapDispatcher>()!;

tapDispatcher.handleTapDown(1, TapDownDetails());
expect(pressedTimes, 0);
expect(releasedTimes, 0);

tapDispatcher.handleTapUp(
1,
Expand All @@ -53,29 +57,34 @@ void main() {
),
);
expect(pressedTimes, 0);
expect(releasedTimes, 0);

tapDispatcher.handleTapDown(
1,
TapDownDetails(globalPosition: buttonPosition.toOffset()),
);
expect(pressedTimes, 1);
expect(releasedTimes, 0);

tapDispatcher.handleTapUp(
1,
createTapUpDetails(globalPosition: buttonPosition.toOffset()),
);
expect(pressedTimes, 1);
expect(releasedTimes, 1);

tapDispatcher.handleTapDown(
1,
TapDownDetails(globalPosition: buttonPosition.toOffset()),
);
tapDispatcher.handleTapCancel(1);
expect(pressedTimes, 2);
expect(releasedTimes, 1);
});

testWithFlameGame('correctly registers taps onGameResize', (game) async {
var pressedTimes = 0;
var releasedTimes = 0;
final initialGameSize = Vector2.all(100);
final componentSize = Vector2.all(10);
final buttonPosition = Vector2.all(100);
Expand All @@ -85,6 +94,7 @@ void main() {
button = AdvancedButtonComponent(
defaultSkin: RectangleComponent(size: componentSize),
onPressed: () => pressedTimes++,
onReleased: () => releasedTimes++,
position: buttonPosition,
size: componentSize,
),
Expand All @@ -99,23 +109,27 @@ void main() {
TapDownDetails(globalPosition: previousPosition),
);
expect(pressedTimes, 1);
expect(releasedTimes, 0);

tapDispatcher.handleTapUp(
1,
createTapUpDetails(globalPosition: previousPosition),
);
expect(pressedTimes, 1);
expect(releasedTimes, 1);

tapDispatcher.handleTapDown(
1,
TapDownDetails(globalPosition: previousPosition),
);
tapDispatcher.handleTapCancel(1);
expect(pressedTimes, 2);
expect(releasedTimes, 1);
});

testWithFlameGame('correctly work isDisabled', (game) async {
var pressedTimes = 0;
var releasedTimes = 0;
final initialGameSize = Vector2.all(100);
final componentSize = Vector2.all(10);
final buttonPosition = Vector2.all(100);
Expand All @@ -125,6 +139,7 @@ void main() {
button = AdvancedButtonComponent(
defaultSkin: RectangleComponent(size: componentSize),
onPressed: () => pressedTimes++,
onReleased: () => releasedTimes++,
position: buttonPosition,
size: componentSize,
),
Expand All @@ -140,19 +155,22 @@ void main() {
TapDownDetails(globalPosition: previousPosition),
);
expect(pressedTimes, 0);
expect(releasedTimes, 0);

tapDispatcher.handleTapUp(
1,
createTapUpDetails(globalPosition: previousPosition),
);
expect(pressedTimes, 0);
expect(releasedTimes, 0);

tapDispatcher.handleTapDown(
1,
TapDownDetails(globalPosition: previousPosition),
);
tapDispatcher.handleTapCancel(1);
expect(pressedTimes, 0);
expect(releasedTimes, 0);
});

testWidgets(
Expand Down
Loading