This repository was archived by the owner on Dec 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 131
Merged
gamepad client apis #573
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,36 @@ | ||
| import * as ds from "@devicescript/core" | ||
|
|
||
| declare module "@devicescript/core" { | ||
| interface Gamepad { | ||
| /** | ||
| * The thumbstick position register if any | ||
| */ | ||
| axes(): ds.ClientRegister<{ x: number; y: number }> | ||
|
|
||
| /** | ||
| * Button (or combo) register | ||
| */ | ||
| button(value: ds.GamepadButtons): ds.ClientRegister<boolean> | ||
| } | ||
| } | ||
|
|
||
| ds.Gamepad.prototype.axes = function axes() { | ||
| let r = (this as any).__axes as ds.ClientRegister<{ x: number; y: number }> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you can extend the ds.Gamepad interface (I know it's a class, but I think you can do interface) and add this field
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do i have to? :)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nah ;) |
||
| if (!r) { | ||
| ;(this as any).__axes = r = ds.clientRegister<{ x: number; y: number }>( | ||
| { x: 0, y: 0 } | ||
| ) | ||
| this.reading.subscribe(rv => r.emit({ x: rv[1], y: rv[2] })) | ||
| } | ||
| return r | ||
| } | ||
|
|
||
| ds.Gamepad.prototype.button = function button(value: ds.GamepadButtons) { | ||
| const key = `__${value}` | ||
| let r = (this as any)[key] as ds.ClientRegister<boolean> | ||
| if (!r) { | ||
| ;(this as any)[key] = r = ds.clientRegister(false) | ||
| this.reading.subscribe(rv => r.emit((rv[0] & value) === value)) | ||
| } | ||
| return r | ||
| } | ||
This file contains hidden or 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 hidden or 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,11 @@ | ||
| import { Gamepad, GamepadButtons } from "@devicescript/core" | ||
|
|
||
| const gamepad = new Gamepad() | ||
|
|
||
| const axes = gamepad.axes() | ||
| axes.subscribe(({ x, y }) => console.log({ x, y })) | ||
|
|
||
| const A = gamepad.button(GamepadButtons.A) | ||
| A.subscribe(a => console.log({ a })) | ||
| const B = gamepad.button(GamepadButtons.B) | ||
| B.subscribe(b => console.log({ b })) |
This file contains hidden or 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,32 @@ | ||
| ### axes {#ro:axes} | ||
|
|
||
| An array representing the controls with axes present on the device (e.g. analog thumb sticks), | ||
| as `[x, y]`. Each entry in the array is a floating point value in the range ` -1.0 – 1.0``, representing the axis position from the lowest value ( `-1.0`) to the highest value (`1.0`). | ||
|
|
||
| - type: `ClientRegister<{ x: number; y: number }>` (packing format `i1.15 i1.15`) | ||
|
|
||
| ```ts | ||
| import { Gamepad } from "@devicescript/core" | ||
|
|
||
| const gamepad = new Gamepad() | ||
| // ... | ||
| gamepad.axes.subscribe(async ({ x, y }) => { | ||
| console.log({ x, y }) | ||
| }) | ||
| ``` | ||
|
|
||
| ### button {#ro:button} | ||
|
|
||
| A client register for the requested button or combination of buttons. The value is `true` if the button is pressed, `false` otherwise. | ||
|
|
||
| - type: `ClientRegister<GamepadButtons>` | ||
|
|
||
| ```ts | ||
| import { Gamepad, GamepadButtons } from "@devicescript/core" | ||
|
|
||
| const gamepad = new Gamepad() | ||
| // ... | ||
| gamepad.button(GamepadButtons.Down).subscribe(async pressed => { | ||
| console.log({ pressed }) | ||
| }) | ||
| ``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
was there a problem with this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's a bit misleading because in the case of axes ({x, y}), the equality check is not really useful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure, but this is generic - what if people use for say booleans?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
0x355724dd