-
Notifications
You must be signed in to change notification settings - Fork 6
/
Input.js
214 lines (186 loc) · 5.89 KB
/
Input.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
206
207
208
209
210
211
212
213
214
// >>> Section: Key state class
// The key state class contains detailed information about a keys state
function KeyState()
{
var pressed_time = 0.0; // Time the key has been pressed or 0.0 if currently not pressed
var pressedframe_time = 0.0; // Time the key has been pressed in this frame or 0.0 if currently not pressed
var pressedframe_duration = 0.0; // Total duration the key had been pressed during this frame (even if currently not pressed)
this.onKeyDown = function(t)
{
pressed_time = t;
pressedframe_time = t;
}
this.onKeyUp = function(t)
{
pressed_time = 0.0;
pressedframe_duration += (pressedframe_time != 0.0) * (t - pressedframe_time);
pressedframe_time = 0.0;
}
this.onFrame = function(t)
{
if(pressedframe_time)
pressedframe_time = t;
pressedframe_duration = 0.0;
}
this.reset = function()
{
pressed_time = 0.0;
pressedframe_time = 0.0;
pressedframe_duration = 0.0;
}
this.isPressed = function()
{
return pressed_time != 0.0;
}
this.pressedDuration = function(t) // Duration since the key was pressed or 0.0 if not currently pressed
{
return pressed_time == 0.0 ? 0.0 : t - pressed_time;
}
this.pressedFrameDuration = function(t) // See pressedframe_duration
{
return pressedframe_duration + (pressedframe_time != 0.0) * (t - pressedframe_time);
}
}
/*// >>> Section: Keyboard input handler
var pressedkeys = [];
for(i = 0; i < 255; i++) pressedkeys[i] = false;
function handleKeyDown(event)
{
event.preventDefault();
if(pressedkeys[event.keyCode] == false)
{
var t = new Date().getTime() / 1000.0 - starttime;
pressedkeys[event.keyCode] = true;
var statekeymap = keymap[statemachine.getState()];
if(statekeymap)
{
// Perform any-key action
var anykeyaction = statekeymap[-1];
if(anykeyaction && anykeyaction[0])
anykeyaction[0](t, event.keyCode);
// Perform key specific action
var keyaction = statekeymap[event.keyCode];
if(keyaction && keyaction[0])
keyaction[0](t, event.keyCode);
}
}
}
function handleKeyUp(event)
{
event.preventDefault();
if(pressedkeys[event.keyCode] == true)
{
var t = new Date().getTime() / 1000.0 - starttime;
pressedkeys[event.keyCode] = false;
var statekeymap = keymap[statemachine.getState()];
if(statekeymap)
{
// Perform any-key action
var anykeyaction = statekeymap[-1];
if(anykeyaction && anykeyaction[1])
anykeyaction[1](t, event.keyCode);
// Perform key specific action
var keyaction = statekeymap[event.keyCode];
if(keyaction && keyaction[1])
keyaction[1](t, event.keyCode);
}
}
}*/
// >>> Section: Mouse input handler
var mouseHandlers = [], mouseHandlerCanvasList = [];
var pressedmousebuttons = [];
function RegisterMouseHandler(cls, canvas)
{
pressedmousebuttons[mouseHandlers.length] = [false, false, false, false];
mouseHandlers.push(cls);
mouseHandlerCanvasList.push(canvas);
}
function handleMouseDown(event)
{
var canvas = event.target;
var idx = mouseHandlerCanvasList.indexOf(event.target);
var mouseHandler = mouseHandlers[idx];
event.preventDefault();
pressedmousebuttons[idx][event.which] = true;
if(typeof(mouseHandler.onMouseDown) == 'function')
{
var canvasBounds = canvas.getBoundingClientRect();
mouseHandler.onMouseDown(canvas, event.clientX - canvasBounds.left, event.clientY - canvasBounds.top, event.which);
}
}
function handleMouseUp(event)
{
for(var i = 0; i < mouseHandlers.length; ++i)
{
var canvas = mouseHandlerCanvasList[i];
var mouseHandler = mouseHandlers[i];
if(pressedmousebuttons[i][event.which] == true)
{
event.preventDefault();
pressedmousebuttons[i][event.which] = false;
if(typeof(mouseHandler.onMouseUp) == 'function')
{
var canvasBounds = canvas.getBoundingClientRect();
mouseHandler.onMouseUp(canvas, event.clientX - canvasBounds.left, event.clientY - canvasBounds.top, event.which, event.target);
}
}
}
}
function handleMouseMove(event)
{
// Fire event for canvas with mouse down
for(var i = 0; i < mouseHandlers.length; ++i)
{
var canvas = mouseHandlerCanvasList[i];
var mouseHandler = mouseHandlers[i];
if(pressedmousebuttons[i].some(function(button) { return button; }))
{
event.preventDefault();
if(typeof(mouseHandler.onMouseMove) == 'function')
{
var canvasBounds = canvas.getBoundingClientRect();
mouseHandler.onMouseMove(canvas, event.clientX - canvasBounds.left, event.clientY - canvasBounds.top, pressedmousebuttons[i]);
return;
}
}
}
// If no canvas had mouse down, fire event for target (canvas under mouse cursor)
var idx = mouseHandlerCanvasList.indexOf(event.target);
if(idx != -1)
{
var canvas = event.target;
var mouseHandler = mouseHandlers[idx];
if(typeof(mouseHandler.onMouseMove) == 'function')
{
canvas = mouseHandler.canvas;
event.preventDefault();
var canvasBounds = canvas.getBoundingClientRect();
mouseHandler.onMouseMove(event.target, event.clientX - canvasBounds.left, event.clientY - canvasBounds.top, pressedmousebuttons[idx]);
}
}
}
function handleMouseLeave(event)
{
var canvas = event.target;
var idx = mouseHandlerCanvasList.indexOf(event.target);
var mouseHandler = mouseHandlers[idx];
event.preventDefault();
if(typeof(mouseHandler.onMouseLeave) == 'function')
{
var canvasBounds = canvas.getBoundingClientRect();
mouseHandler.onMouseLeave(canvas, event.clientX - canvasBounds.left, event.clientY - canvasBounds.top, pressedmousebuttons[idx]);
}
}
function handleMouseWheel(event)
{
var canvas = event.target;
var idx = mouseHandlerCanvasList.indexOf(event.target);
var mouseHandler = mouseHandlers[idx];
if(mouseHandler != null && typeof(mouseHandler.onMouseWheel) == 'function')
{
var deltaZ = event.wheelDelta == null ? event.detail : -event.wheelDelta / 20.0;
event.preventDefault();
var canvasBounds = canvas.getBoundingClientRect();
mouseHandler.onMouseWheel(canvas, event.clientX - canvasBounds.left, event.clientY - canvasBounds.top, deltaZ, pressedmousebuttons[idx]);
}
}