A web-based drawing app
git clone git://soophie.de/outline
blob: 537728fb407338ed651043eba6d72ee38d8559e7 (5546 bytes)[doc][blame][raw]
1
const CONTROLLER_PATTERN = {2
0: 'A',3
1: 'B',4
2: 'X',5
3: 'Y',6
4: 'L1',7
5: 'R1',8
6: 'L2',9
7: 'R2',10
8: 'Option',11
9: 'Menu',12
10: 'L3',13
11: 'R3',14
12: 'Up',15
13: 'Down',16
14: 'Left',17
15: 'Right',18
16: 'Power',19
17: 'Assist',20
18: 'Capture'21
};23
let gamepad;24
let on_connect_callback;25
let on_disconnect_callback;27
/**28
Binds gamepad event handler.30
@function init31
@contructor32
*/33
function init() {34
window.addEventListener('gamepadconnected', function(event) {35
gamepad = event.gamepad;36
if(gamepad.vibrationActuator) {37
gamepad.vibrationActuator.playEffect('dual-rumble', {38
startDelay: 0,39
duration: 100,40
weakMagnitude: 1.0,41
strongMagnitude: 1.042
});43
}44
if(on_connect_callback) {45
on_connect_callback(gamepad);46
}47
});48
window.addEventListener('gamepaddisconnected', function() {49
gamepad = null;50
if(on_disconnect_callback) {51
on_disconnect_callback(gamepad);52
}53
});54
}56
/**57
Return controller formatted input.59
@function get60
@return {Object} The formatted input object.61
*/62
function get() {63
let result = {64
button: {},65
axes: {}66
};67
for(let [ id, key ] of Object.entries(CONTROLLER_PATTERN)) {68
if(gamepad.buttons[id]) {69
result.button[key] = gamepad.buttons[id];70
}71
}72
result.axes['Left'] = {73
x: gamepad.axes[0],74
y: gamepad.axes[1]75
};76
result.axes['Right'] = {77
x: gamepad.axes[2],78
y: gamepad.axes[3]79
};80
return result;81
}83
function on_connect(callback) {84
on_connect_callback = callback;85
}87
function on_disconnect(callback) {88
on_disconnect_callback = callback;89
}91
/**92
The callback gets called if a gamepad button was pressed.94
@function on_pressed95
@param {string} key - The input key.96
@callback callback97
*/98
function on_pressed(key, callback) {99
/*100
if(input.button[key]) {101
if(!pressed[key]) {102
pressed[key] = true;103
if(callback) {104
callback();105
}106
}107
}108
else {109
pressed[key] = false;110
}111
*/112
}114
export default {115
init,116
get,117
on_connect,118
on_disconnect,119
on_pressed120
};122
// !!! TODO !!!123
/*124
gamepad.init();125
// detect gamepad126
gamepad.on_connect(function(gamepad) {127
state.device_list.gamepad = true;128
show_notification('notification.input-device.gamepad.add');129
if(!state.device) {130
state.device = 'gamepad';131
state.device_fallback = 'gamepad';132
}133
update_device_list();134
});135
// gamepad removed136
gamepad.on_disconnect(function(gamepad) {137
state.device_list.gamepad = false;138
show_notification('notification.input-device.gamepad.remove');139
if(state.device === 'gamepad') {140
if(state.device !== state.device_fallback) {141
state.device = state.device_fallback;142
}143
else {144
state.device = null;145
}146
// point = { x: null, y: null };147
// last = { x: null, y: null };148
}149
update_device_list();150
});151
*/153
// !!! TODO !!!154
/*155
// cursor visible156
canvas.style.cursor = device === 'mouse' ? 'none' : 'default';157
if(device !== 'gamepad') {158
let pos = arrow_y * 5 + arrow_x;159
if(modal_colors[pos].classList.contains('focus')) {160
modal_colors[pos].classList.remove('focus');161
}162
}163
// navigate colors164
if(device === 'gamepad') {165
let pos = arrow_y * 5 + arrow_x;166
for(let [ i, item ] of Object.entries(modal_colors)) {167
item.classList[parseInt(i) == pos ? 'add' : 'remove']('focus');168
}169
}170
*/172
// !!! TODO !!!173
/*174
let gamepad = navigator.getGamepads()[0];175
if(gamepad) {176
if(device === 'gamepad') {177
if(point.x == null && point.y == null) {178
point.x = parseInt(window.innerWidth / 2);179
point.y = parseInt(window.innerHeight / 2);180
}181
let speed = 4;182
if(pressed['L2'] && !pressed['R2']) {183
speed = 10;184
}185
if(pressed['R2'] && !pressed['L2']) {186
speed = 1;187
}188
// colors189
key_pressed('Option', input, function() {190
toggle_modal(colors_modal);191
});192
// capture193
key_pressed('Capture', input, function() {194
toggle_modal(capture_modal);195
});196
// draw197
key_pressed('A', input, function() {198
last.x = null;199
last.y = null;200
if(!current_modal) {201
left_modal = false;202
}203
});204
// erase205
key_pressed('B', input, function() {206
last.x = null;207
last.y = null;208
if(current_tab) {209
close_tab(current_tab);210
left_modal = true;211
}212
else if(current_modal) {213
toggle_modal(current_modal);214
left_modal = true;215
}216
else {217
left_modal = false;218
}219
});220
// sprint221
key_pressed('L2', input);222
// sneak223
key_pressed('R2', input);224
// clear225
key_pressed('Y', input, function() {226
toggle_modal(clear_modal);227
});228
// radius229
let y = -(input.axes['Right'].y / 2);230
if(y !== 0 && radius + y > 2 && radius + y < 50 + 1) {231
radius += y;232
scale_range.value = radius;233
}234
// lock y235
key_pressed('L3', input, function() {236
lock_y = !lock_y;237
lock_x = false;238
});239
// lock x240
key_pressed('R3', input, function() {241
lock_x = !lock_x;242
lock_y = false;243
});244
// color-wheel left245
key_pressed('L1', input, function() {246
if(index > 0) {247
index--;248
for(let i in colors) {249
if(COLOR_LENGTH > i) {250
colors[i].classList[i == index ? 'add' : 'remove']('active');251
}252
}253
}254
});255
// color-wheel right256
key_pressed('R1', input, function() {257
if(index < COLOR_LENGTH - 1) {258
index++;259
for(let i in colors) {260
if(COLOR_LENGTH > i) {261
colors[i].classList[i == index ? 'add' : 'remove']('active');262
}263
}264
}265
});266
key_pressed('Up', input, function() {267
if(arrow_y > 0) {268
arrow_y--;269
}270
});271
key_pressed('Down', input, function() {272
if(arrow_y < 10) {273
arrow_y++;274
}275
});276
key_pressed('Left', input, function() {277
if(arrow_x > 0) {278
arrow_x--;279
}280
});281
key_pressed('Right', input, function() {282
if(arrow_x < 4) {283
arrow_x++;284
}285
});286
}287
}288
*/