-
-
Notifications
You must be signed in to change notification settings - Fork 882
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added a ButtonComponent * Don't import VoidCallback * Change epsilon since test is flaky * Added SpriteButtonComponent * Add changelog entry * Add docs
- Loading branch information
Showing
8 changed files
with
226 additions
and
3 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
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
85 changes: 85 additions & 0 deletions
85
packages/flame/lib/src/components/input/button_component.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,85 @@ | ||
import 'package:meta/meta.dart'; | ||
|
||
import '../../../components.dart'; | ||
import '../../../extensions.dart'; | ||
import '../../../input.dart'; | ||
import '../../gestures/events.dart'; | ||
|
||
/// The [ButtonComponent] bundles two [PositionComponent]s, one that shows while | ||
/// the button is being pressed, and one that shows otherwise. | ||
/// | ||
/// Note: You have to set the [button] in [onLoad] if you are not passing it in | ||
/// through the constructor. | ||
class ButtonComponent extends PositionComponent with Tappable { | ||
late final PositionComponent? button; | ||
late final PositionComponent? buttonDown; | ||
|
||
/// Callback for what should happen when the button is pressed. | ||
/// If you want to interact with [onTapUp] or [onTapCancel] it is recommended | ||
/// to extend [ButtonComponent]. | ||
void Function()? onPressed; | ||
|
||
ButtonComponent({ | ||
this.button, | ||
this.buttonDown, | ||
this.onPressed, | ||
Vector2? position, | ||
Vector2? size, | ||
Vector2? scale, | ||
double? angle, | ||
Anchor? anchor, | ||
int? priority, | ||
}) : super( | ||
position: position, | ||
size: size ?? button?.size, | ||
scale: scale, | ||
angle: angle, | ||
anchor: anchor, | ||
priority: priority, | ||
); | ||
|
||
@override | ||
@mustCallSuper | ||
void onMount() { | ||
assert( | ||
button != null, | ||
'The button has to either be passed in as an argument or set in onLoad', | ||
); | ||
final idleButton = button; | ||
if (idleButton != null && !contains(idleButton)) { | ||
add(idleButton); | ||
} | ||
} | ||
|
||
@override | ||
@mustCallSuper | ||
bool onTapDown(TapDownInfo info) { | ||
if (buttonDown != null) { | ||
if (button != null) { | ||
remove(button!); | ||
} | ||
add(buttonDown!); | ||
} | ||
onPressed?.call(); | ||
return false; | ||
} | ||
|
||
@override | ||
@mustCallSuper | ||
bool onTapUp(TapUpInfo info) { | ||
onTapCancel(); | ||
return true; | ||
} | ||
|
||
@override | ||
@mustCallSuper | ||
bool onTapCancel() { | ||
if (buttonDown != null) { | ||
remove(buttonDown!); | ||
if (button != null) { | ||
add(button!); | ||
} | ||
} | ||
return false; | ||
} | ||
} |
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
72 changes: 72 additions & 0 deletions
72
packages/flame/lib/src/components/input/sprite_button_component.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,72 @@ | ||
import '../../../components.dart'; | ||
import '../../../extensions.dart'; | ||
import '../../../input.dart'; | ||
|
||
enum _ButtonState { | ||
up, | ||
down, | ||
} | ||
|
||
/// The [SpriteButtonComponent] bundles two [Sprite]s, one that shows while | ||
/// the button is being pressed, and one that shows otherwise. | ||
/// | ||
/// Note: You have to set the [button] in [onLoad] if you are not passing it in | ||
/// through the constructor. | ||
class SpriteButtonComponent extends SpriteGroupComponent<_ButtonState> | ||
with Tappable { | ||
/// Callback for what should happen when the button is pressed. | ||
void Function()? onPressed; | ||
|
||
Sprite? button; | ||
Sprite? buttonDown; | ||
|
||
SpriteButtonComponent({ | ||
this.button, | ||
this.buttonDown, | ||
this.onPressed, | ||
Vector2? position, | ||
Vector2? size, | ||
Vector2? scale, | ||
double? angle, | ||
Anchor? anchor, | ||
int? priority, | ||
}) : super( | ||
current: _ButtonState.up, | ||
position: position, | ||
size: size ?? button?.originalSize, | ||
scale: scale, | ||
angle: angle, | ||
anchor: anchor, | ||
priority: priority, | ||
); | ||
|
||
@override | ||
void onMount() { | ||
assert( | ||
button != null, | ||
'The button sprite has to be set either in onLoad or in the constructor', | ||
); | ||
sprites = {_ButtonState.up: button!}; | ||
sprites![_ButtonState.down] = buttonDown ?? button!; | ||
super.onMount(); | ||
} | ||
|
||
@override | ||
bool onTapDown(_) { | ||
current = _ButtonState.down; | ||
return false; | ||
} | ||
|
||
@override | ||
bool onTapUp(_) { | ||
onTapCancel(); | ||
return false; | ||
} | ||
|
||
@override | ||
bool onTapCancel() { | ||
current = _ButtonState.up; | ||
onPressed?.call(); | ||
return false; | ||
} | ||
} |
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