/
index.ts
150 lines (127 loc) · 4.21 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { createEventHook, tryOnMounted } from '@vueuse/shared'
import type { Ref } from 'vue-demi'
import { computed, ref } from 'vue-demi'
import { useRafFn } from '../useRafFn'
import { useEventListener } from '../useEventListener'
import type { ConfigurableNavigator, ConfigurableWindow } from '../_configurable'
import { defaultNavigator } from '../_configurable'
export interface UseGamepadOptions extends ConfigurableWindow, ConfigurableNavigator {
}
/**
* Maps a standard standard gamepad to an Xbox 360 Controller.
*/
export function mapGamepadToXbox360Controller(gamepad: Ref<Gamepad | undefined>) {
return computed(() => {
if (gamepad.value) {
return {
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],
}
}
return null
})
}
export function useGamepad(options: UseGamepadOptions = {}) {
const {
navigator = defaultNavigator,
} = options
const isSupported = navigator && 'getGamepads' in navigator
const gamepads = ref<Gamepad[]>([])
const onConnectedHook = createEventHook<number>()
const onDisconnectedHook = createEventHook<number>()
const stateFromGamepad = (gamepad: Gamepad) => {
const hapticActuators = []
const vibrationActuator = 'vibrationActuator' in gamepad ? (gamepad as any).vibrationActuator : null
if (vibrationActuator)
hapticActuators.push(vibrationActuator)
if (gamepad.hapticActuators)
hapticActuators.push(...gamepad.hapticActuators)
return {
id: gamepad.id,
hapticActuators,
index: gamepad.index,
mapping: gamepad.mapping,
connected: gamepad.connected,
timestamp: gamepad.timestamp,
axes: gamepad.axes.map(axes => axes),
buttons: gamepad.buttons.map(button => ({ pressed: button.pressed, touched: button.touched, value: button.value })),
}
}
const updateGamepadState = () => {
const _gamepads = navigator?.getGamepads() || []
for (let i = 0; i < _gamepads.length; ++i) {
const gamepad = _gamepads[i]
if (gamepad) {
const index = gamepads.value.findIndex(({ index }) => index === gamepad.index)
if (index > -1)
gamepads.value[index] = stateFromGamepad(gamepad)
}
}
}
const { isActive, pause, resume } = useRafFn(updateGamepadState)
const onGamepadConnected = (gamepad: Gamepad) => {
if (!gamepads.value.some(({ index }) => index === gamepad.index)) {
gamepads.value.push(stateFromGamepad(gamepad))
onConnectedHook.trigger(gamepad.index)
}
resume()
}
const onGamepadDisconnected = (gamepad: Gamepad) => {
gamepads.value = gamepads.value.filter(x => x.index !== gamepad.index)
onDisconnectedHook.trigger(gamepad.index)
}
useEventListener('gamepadconnected', e => onGamepadConnected(e.gamepad))
useEventListener('gamepaddisconnected', e => onGamepadDisconnected(e.gamepad))
tryOnMounted(() => {
const _gamepads = navigator?.getGamepads() || []
if (_gamepads) {
for (let i = 0; i < _gamepads.length; ++i) {
const gamepad = _gamepads[i]
if (gamepad)
onGamepadConnected(gamepad)
}
}
})
pause()
return {
isSupported,
onConnected: onConnectedHook.on,
onDisconnected: onDisconnectedHook.on,
gamepads,
pause,
resume,
isActive,
}
}