-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
205 lines (201 loc) · 7.44 KB
/
index.js
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import { Extension, api, type } from 'clipcc-extension';
import Controller from './Controller.js/Controller';
import './Controller.js/Controller.layouts'
class GamepadExtension extends Extension {
constructor() {
super();
this.gamepad = null
this.pressButton = []
this.buttonMenu = this.buttonMenu.bind(this)
this.joystickPress = this.joystickPress.bind(this)
this.onInit = this.onInit.bind(this)
this.onFoundGamepad = this.onFoundGamepad.bind(this)
this.isConnect = this.isConnect.bind(this)
this.isButtonPress = this.isButtonPress.bind(this)
this.onGamepadPress = this.onGamepadPress.bind(this)
this.onGamepadRelease = this.onGamepadRelease.bind(this)
}
onInit() {
Controller.search();
window.addEventListener('gc.controller.found', this.onFoundGamepad, false);
window.addEventListener('gc.button.press', this.onGamepadPress, false);
window.addEventListener('gc.button.hold', this.onGamepadPress, false);
window.addEventListener('gc.button.release', this.onGamepadPress, false);
api.addCategory({
categoryId: 'frank.gamepad.category',
messageId: 'frank.gamepad.name',
color: '#66CCFF'
});
api.addBlock({
opcode: 'frank.gamepad.isConnect',
type: type.BlockType.BOOLEAN,
messageId: 'frank.gamepad.isConnect',
categoryId: 'frank.gamepad.category',
function: () => this.isConnect()
});
api.addBlock({
opcode: 'frank.gamepad.gamepadName',
type: type.BlockType.REPORTER,
messageId: 'frank.gamepad.gamepadName',
categoryId: 'frank.gamepad.category',
function: () => this.gamepad ? this.gamepad.name : ''
});
api.addBlock({
opcode: 'frank.gamepad.onPress',
type: type.BlockType.HAT,
messageId: 'frank.gamepad.onPress',
categoryId: 'frank.gamepad.category',
param: {
BUTTON: {
type: type.ParameterType.STRING,
menu: () => this.buttonMenu(),
default: ''
}
},
function: args => {
if (!this.gamepad) return false
return this.gamepad.inputs.buttons[args.BUTTON] ? this.gamepad.inputs.buttons[args.BUTTON].pressed : false
}
});
api.addBlock({
opcode: 'frank.gamepad.isPress',
type: type.BlockType.BOOLEAN,
messageId: 'frank.gamepad.isPress',
categoryId: 'frank.gamepad.category',
param: {
BUTTON: {
type: type.ParameterType.STRING,
menu: () => this.buttonMenu(),
default: ''
}
},
function: args => this.isButtonPress(args.BUTTON)
});
api.addBlock({
opcode: 'frank.gamepad.joystick',
type: type.BlockType.REPORTER,
messageId: 'frank.gamepad.joystick',
categoryId: 'frank.gamepad.category',
param: {
STICKS: {
type: type.ParameterType.STRING,
menu: () => {
if (!this.gamepad) return [['','']]
const joystick = [];
for (const index in this.gamepad.inputs.analogSticks) {
const button = this.gamepad.inputs.analogSticks[index].name
joystick.push([button, button]);
}
return joystick
},
default: ''
},
VALUE: {
type: type.ParameterType.NUMBER,
menu: [{
messageId: 'frank.gamepad.menu.x',
value: 'x'
},
{
messageId: 'frank.gamepad.menu.y',
value: 'y'
},
{
messageId: 'frank.gamepad.menu.degrees',
value: 'degrees'
},
{
messageId: 'frank.gamepad.menu.radians',
value: 'radians'
}
],
default: 'x'
}
},
function: args => this.joystickPress(args.STICKS, args.VALUE)
});
}
onUninit() {
window.removeEventListener('gc.controller.found', this.onFoundGamepad, false);
window.removeEventListener('gc.button.press', this.onGamepadPress, false);
window.removeEventListener('gc.button.hold', this.onGamepadPress, false);
window.removeEventListener('gc.button.release', this.onGamepadPress, false);
api.removeCategory('frank.gamepad.category')
}
buttonMenu () {
if (!this.gamepad) return [['','']]
const buttons = [];
for (const index in this.gamepad.inputs.buttons) {
const button = this.gamepad.inputs.buttons[index].name
buttons.push([button, button]);
}
return buttons
}
joystickPress (sticks, value) {
if (!this.gamepad) return 0
const gamepadJoystick = this.gamepad.inputs.analogSticks
for (const i in gamepadJoystick) {
const b = gamepadJoystick[i];
if (b.name === sticks) {
switch (value) {
case 'x':
return b.position.x
case 'y':
return b.position.y
case 'degrees':
return b.angle.degrees
case 'radians':
return b.angle.radians
default:
return NaN
}
}
}
return NaN
}
isButtonPress (button) {
if (!this.gamepad) return false
const gamepadButton = this.gamepad.inputs.buttons;
for (const i in gamepadButton) {
const b = gamepadButton[i];
if (b.name === button && b.pressed) {
return true
}
}
return false
}
onGamepadPress(event) {
const button = event.detail;
if (!this.pressButton.includes(button.name) && button.pressed) {
this.pressButton.push(button.name)
} else {
this.pressButton.map((value,index,arr) => {
if (value == button.name) {
return arr.pop(index)
}
})
}
// console.log(button);
}
onGamepadRelease(event) {
const button = event.detail;
if (this.pressButton.includes(button.name)) {
this.pressButton.map((value,index,arr) => {
if (value == button.name) {
return arr.pop(index)
}
})
}
// console.log(this.pressButton);
}
onFoundGamepad(event) {
const controller = event.detail.controller;
console.log(`Gamepad found ${controller.name} index:${controller.index}`)
this.gamepad = Controller.getController(controller.index)
// console.log(this.gamepad)
}
isConnect() {
return !!this.gamepad
}
}
export default GamepadExtension;