forked from libretro/RetroArch
-
Notifications
You must be signed in to change notification settings - Fork 2
/
library_rwebinput.js
143 lines (127 loc) · 4.97 KB
/
library_rwebinput.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
//"use strict";
var LibraryRWebInput = {
$RI__deps: ['$Browser'],
$RI: {
temp: null,
contexts: [],
stateX: 0,
stateY: 0,
currentX: 0,
currentY: 0,
canvasEventHandler: function(event) {
switch (event.type) {
case 'mouseup':
case 'mousedown':
var value;
var offset;
if (event.button === 0) offset = 40;
else if (event.button === 2) offset = 41;
else break;
if (event.type === 'mouseup') value = 0;
else value = 1;
for (var i = 0; i < RI.contexts.length; i++) {
{{{ makeSetValue('RI.contexts[i].state', 'offset', 'value', 'i8') }}};
}
break;
}
},
eventHandler: function(event) {
var i;
switch (event.type) {
case 'mousemove':
var x = 0;
var y = 0;
var newX = event['clientX'] - Module.canvas.offsetLeft;
var newY = event['clientY'] - Module.canvas.offsetTop;
if (newX < 0) {
newX = 0;
x = -Module.canvas.offsetWidth;
} else if (newX > Module.canvas.offsetWidth) {
newX = Module.canvas.offsetWidth;
x = Module.canvas.offsetWidth;
} else {
x = newX - RI.currentX;
}
if (newY < 0) {
newY = 0;
y = -Module.canvas.offsetHeight;
} else if (newY > Module.canvas.offsetHeight) {
newY = Module.canvas.offsetHeight;
y = Module.canvas.offsetHeight;
} else {
y = newY - RI.currentY;
}
RI.currentX = newX;
RI.currentY = newY;
for (i = 0; i < RI.contexts.length; i++) {
{{{ makeSetValue('RI.contexts[i].state', '32', 'x', 'i32') }}};
{{{ makeSetValue('RI.contexts[i].state', '36', 'y', 'i32') }}};
}
break;
case 'keyup':
case 'keydown':
var key = event.keyCode;
var offset = key >> 3;
var bit = 1 << (key & 7);
if (offset >= 32) throw 'key code error! bad code: ' + key;
for (i = 0; i < RI.contexts.length; i++) {
var value = {{{ makeGetValue('RI.contexts[i].state', 'offset', 'i8') }}};
if (event.type === 'keyup') value &= ~bit;
else value |= bit;
{{{ makeSetValue('RI.contexts[i].state', 'offset', 'value', 'i8') }}};
}
event.preventDefault();
break;
case 'blur':
case 'visibilitychange':
for (i = 0; i < RI.contexts.length; i++) {
_memset(RI.contexts[i].state, 0, 42);
}
break;
}
}
},
RWebInputInit: function() {
if (RI.contexts.length === 0) {
document.addEventListener('keyup', RI.eventHandler, false);
document.addEventListener('keydown', RI.eventHandler, false);
document.addEventListener('mousemove', RI.eventHandler, false);
Module.canvas.addEventListener('mouseup', RI.canvasEventHandler, false);
Module.canvas.addEventListener('mousedown', RI.canvasEventHandler, false);
document.addEventListener('blur', RI.eventHandler, false);
document.addEventListener('onvisbilitychange', RI.eventHandler, false);
}
if (RI.temp === null) RI.temp = _malloc(42);
var s = _malloc(42);
_memset(s, 0, 42);
RI.contexts.push({
state: s
});
return RI.contexts.length;
},
RWebInputPoll: function(context) {
context -= 1;
var state = RI.contexts[context].state;
_memcpy(RI.temp, state, 42);
// reset mouse movements
{{{ makeSetValue('RI.contexts[context].state', '32', '0', 'i32') }}};
{{{ makeSetValue('RI.contexts[context].state', '36', '0', 'i32') }}};
return RI.temp;
},
RWebInputDestroy: function (context) {
if (context === RI.contexts.length) {
RI.contexts.pop();
if (RI.contexts.length === 0) {
document.removeEventListener('keyup', RI.eventHandler, false);
document.removeEventListener('keydown', RI.eventHandler, false);
document.removeEventListener('mousemove', RI.eventHandler, false);
Module.canvas.removeEventListener('mouseup', RI.canvasEventHandler, false);
Module.canvas.removeEventListener('mousedown', RI.canvasEventHandler, false);
document.removeEventListener('blur', RI.eventHandler, false);
document.removeEventListener('onvisbilitychange', RI.eventHandler, false);
}
}
}
};
autoAddDeps(LibraryRWebInput, '$RI');
mergeInto(LibraryManager.library, LibraryRWebInput);