Skip to content

Commit

Permalink
feat: virtual press and release virtual buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
lajbel committed Oct 23, 2024
1 parent df6d3c7 commit 1de70d8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,16 @@ export const initApp = (opt: {
};
}

function pressButton(btn: string) {
state.buttonState.press(btn);
state.events.trigger("buttonPress", btn);
}

function releaseButton(btn: string) {
state.buttonState.release(btn);
state.events.trigger("buttonRelease", btn);
}

function onResize(action: () => void): KEventController {
return state.events.on("resize", action);
}
Expand Down Expand Up @@ -1262,6 +1272,8 @@ export const initApp = (opt: {
isButtonReleased,
setButton,
getButton,
pressButton,
releaseButton,
charInputted,
onResize,
onKeyDown,
Expand Down
2 changes: 2 additions & 0 deletions src/kaplay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,8 @@ const kaplay = <
isButtonReleased: app.isButtonReleased,
setButton: app.setButton,
getButton: app.getButton,
pressButton: app.pressButton,
releaseButton: app.releaseButton,
getLastInputDeviceType: app.getLastInputDeviceType,
charInputted: app.charInputted,
// timer
Expand Down
28 changes: 28 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2259,6 +2259,34 @@ export interface KAPLAYCtx<
* @group Input
*/
setButton(button: string, def: ButtonBinding): void;
/**
* Press a button virtually.
*
* @since v3001.0
* @group Input
*
* @example
* ```js
* // press "jump" button
* pressButton("jump"); // triggers onButtonPress, starts onButtonDown
* releaseButton("jump"); // triggers onButtonRelease, stops onButtonDown
* ```
*/
pressButton(button: TButton): void;
/**
* Release a button virtually.
*
* @since v3001.0
* @group Input
*
* @example
* ```js
* // press "jump" button
* pressButton("jump"); // triggers onButtonPress, starts onButtonDown
* releaseButton("jump"); // triggers onButtonRelease, stops onButtonDown
* ```
*/
releaseButton(button: TButton): void;
/**
* Get stick axis values from a gamepad.
*
Expand Down

0 comments on commit 1de70d8

Please sign in to comment.