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(useGamepad): add PS5 DualSense mapping #3914

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 17 additions & 2 deletions packages/core/useGamepad/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ function vibrate() {

To make the Gamepad API easier to use, we provide mappings to map a controller to a controllers button layout.

#### Xbox360 Controller
#### Xbox360 / PS5 DualSense Controller

```vue
<script setup>
Expand All @@ -102,4 +102,19 @@ const controller = mapGamepadToXbox360Controller(gamepad)
</template>
```

Currently there are only mappings for the Xbox 360 controller. If you have controller you want to add mappings for, feel free to open a PR for more controller mappings!
```vue
<script setup>
import { mapGamepadToPS5DualSenseController } from '@vueuse/core'

const controller = mapGamepadToPS5DualSenseController(gamepad)
</script>

<template>
<span>{{ controller.buttons["⨯"].pressed }}</span>
<span>{{ controller.buttons["○"].pressed }}</span>
<span>{{ controller.buttons["□"].pressed }}</span>
<span>{{ controller.buttons["△"].pressed }}</span>
</template>
```

Currently there are only mappings for the Xbox 360 and the PS5 DualSense controller. If you have controller you want to add mappings for, feel free to open a PR for more controller mappings!
93 changes: 66 additions & 27 deletions packages/core/useGamepad/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,20 @@ export interface UseGamepadOptions extends ConfigurableWindow, ConfigurableNavig
}

/**
* Maps a standard standard gamepad to an Xbox 360 Controller.
* Maps a [standard gamepad](https://w3c.github.io/gamepad/#remapping) to an Xbox 360 Controller.
* https://support.xbox.com/en-US/help/xbox-360/accessories/controllers
*/
export function mapGamepadToXbox360Controller(gamepad: Ref<Gamepad | undefined>) {
return computed(() => {
if (gamepad.value) {
return {
...mapGamepadToStandardController(gamepad.value),
buttons: {
a: gamepad.value.buttons[0],
b: gamepad.value.buttons[1],
x: gamepad.value.buttons[2],
y: gamepad.value.buttons[3],
},
bumper: {
left: gamepad.value.buttons[4],
right: gamepad.value.buttons[5],
},
triggers: {
left: gamepad.value.buttons[6],
right: gamepad.value.buttons[7],
},
stick: {
left: {
horizontal: gamepad.value.axes[0],
vertical: gamepad.value.axes[1],
button: gamepad.value.buttons[10],
},
right: {
horizontal: gamepad.value.axes[2],
vertical: gamepad.value.axes[3],
button: gamepad.value.buttons[11],
},
},
dpad: {
up: gamepad.value.buttons[12],
down: gamepad.value.buttons[13],
left: gamepad.value.buttons[14],
right: gamepad.value.buttons[15],
},
back: gamepad.value.buttons[8],
start: gamepad.value.buttons[9],
}
Expand All @@ -59,6 +35,69 @@ export function mapGamepadToXbox360Controller(gamepad: Ref<Gamepad | undefined>)
})
}

/**
* Maps a [standard gamepad](https://w3c.github.io/gamepad/#remapping) to a PS5 DualSense Controller.
*
* You can find more about PS5 DualSense controller's original part names at
* https://controller.dl.playstation.net/controller/lang/en/DS_partnames.html.
*/
export function mapGamepadToPS5DualSenseController(gamepad: Ref<Gamepad | undefined>) {
return computed(() => {
if (gamepad.value) {
return {
...mapGamepadToStandardController(gamepad.value),
buttons: {
'⨯': gamepad.value.buttons[0],
'○': gamepad.value.buttons[1],
'□': gamepad.value.buttons[2],
'△': gamepad.value.buttons[3],
Comment on lines +50 to +53
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are picked visually and may have better alternatives.

'L1': gamepad.value.buttons[4],
'R1': gamepad.value.buttons[5],
'L2': gamepad.value.buttons[6],
'R2': gamepad.value.buttons[7],
},
create: gamepad.value.buttons[8],
options: gamepad.value.buttons[9],
Comment on lines +59 to +60
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO create/options and (back/start for XBOX) should be inside buttons but that will be a breaking change and doing this only in mapGamepadToPS5DualSenseController will cause inconsistency.

}
}
return null
})
}

/**
* Shared name mapping across different controllers
*/
function mapGamepadToStandardController(gamepad: Gamepad) {
return {
bumper: {
left: gamepad.buttons[4],
right: gamepad.buttons[5],
},
triggers: {
left: gamepad.buttons[6],
right: gamepad.buttons[7],
},
stick: {
left: {
horizontal: gamepad.axes[0],
vertical: gamepad.axes[1],
button: gamepad.buttons[10],
},
right: {
horizontal: gamepad.axes[2],
vertical: gamepad.axes[3],
button: gamepad.buttons[11],
},
},
dpad: {
up: gamepad.buttons[12],
down: gamepad.buttons[13],
left: gamepad.buttons[14],
right: gamepad.buttons[15],
},
}
}

export function useGamepad(options: UseGamepadOptions = {}) {
const {
navigator = defaultNavigator,
Expand Down