Skip to content

Commit

Permalink
feat: Add onReleased callback for HudButtonComponent (#1296)
Browse files Browse the repository at this point in the history
Adding an `onReleased` callback to `HudButtonComponent` which will be called when button is released.
  • Loading branch information
ufrshubham authored Jan 7, 2022
1 parent b9b8c64 commit 87ee34c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
6 changes: 3 additions & 3 deletions doc/other-inputs.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ As the name suggests this button is a hud by default, which means that it will b
screen even if the camera for the game moves around. You can also use this component as a non-hud by
setting `hudButtonComponent.respectCamera = true;`.

If you want to act upon the button being pressed (which would be the common thing to do) you can either pass in
a callback function as the `onPressed` argument, or you extend the component and override
`onTapDown`, `onTapUp` and/or `onTapCancel` and implement your logic there.
If you want to act upon the button being pressed (which would be the common thing to do) and released,
you can either pass in callback functions as the `onPressed` and `onReleased` arguments, or you can
extend the component and override `onTapDown`, `onTapUp` and/or `onTapCancel` and implement your logic there.

## SpriteButtonComponent

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,21 @@ class HudButtonComponent extends HudMarginComponent with Tappable {
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 [HudButtonComponent].
/// If you want to directly interact with [onTapUp], [onTapDown] or
/// [onTapCancel] it is recommended to extend [HudButtonComponent].
void Function()? onPressed;

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

HudButtonComponent({
this.button,
this.buttonDown,
EdgeInsets? margin,
this.onPressed,
this.onReleased,
Vector2? position,
Vector2? size,
Vector2? scale,
Expand Down Expand Up @@ -69,6 +75,7 @@ class HudButtonComponent extends HudMarginComponent with Tappable {
@mustCallSuper
bool onTapUp(TapUpInfo info) {
onTapCancel();
onReleased?.call();
return true;
}

Expand Down

0 comments on commit 87ee34c

Please sign in to comment.