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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "illusions/input.h"
namespace Illusions {
// KeyMap
void KeyMap::addKey(Common::KeyCode key) {
add(key, MOUSE_NONE);
}
void KeyMap::addMouseButton(int mouseButton) {
add(Common::KEYCODE_INVALID, mouseButton);
}
void KeyMap::add(Common::KeyCode key, int mouseButton) {
KeyMapping keyMapping;
keyMapping._key = key;
keyMapping._mouseButton = mouseButton;
keyMapping._down = false;
push_back(keyMapping);
}
// InputEvent
InputEvent::InputEvent() : _bitMask(0) {
}
InputEvent& InputEvent::setBitMask(uint bitMask) {
_bitMask = bitMask;
return *this;
}
InputEvent& InputEvent::addKey(Common::KeyCode key) {
_keyMap.addKey(key);
return *this;
}
InputEvent& InputEvent::addMouseButton(int mouseButton) {
_keyMap.addMouseButton(mouseButton);
return *this;
}
uint InputEvent::handle(Common::KeyCode key, int mouseButton, bool down) {
uint newKeys = 0;
for (KeyMap::iterator it = _keyMap.begin(); it != _keyMap.end(); ++it) {
KeyMapping &keyMapping = *it;
if ((keyMapping._key != Common::KEYCODE_INVALID && keyMapping._key == key) ||
(keyMapping._mouseButton != MOUSE_NONE && keyMapping._mouseButton == mouseButton)) {
if (down && !keyMapping._down) {
newKeys |= _bitMask;
keyMapping._down = true;
} else if (!down)
keyMapping._down = false;
}
}
return newKeys;
}
// Input
const uint kAllButtons = 0xFFFF;
static const char kCheatCode[] = "gosanta";
Input::Input() {
_buttonStates = 0;
_newButtons = 0;
_buttonsDown = 0;
_newKeys = 0;
_enabledButtons = kAllButtons;
_cursorPos.x = 0;
_cursorPos.y = 0;
_prevCursorPos.x = 0;
_prevCursorPos.y = 0;
_cheatCodeIndex = 0;
}
void Input::processEvent(Common::Event event) {
switch (event.type) {
case Common::EVENT_KEYDOWN:
handleKey(event.kbd.keycode, MOUSE_NONE, true);
break;
case Common::EVENT_KEYUP:
handleKey(event.kbd.keycode, MOUSE_NONE, false);
break;
case Common::EVENT_MOUSEMOVE:
_cursorPos.x = event.mouse.x;
_cursorPos.y = event.mouse.y;
break;
case Common::EVENT_LBUTTONDOWN:
handleMouseButton(MOUSE_LEFT_BUTTON, true);
break;
case Common::EVENT_LBUTTONUP:
handleMouseButton(MOUSE_LEFT_BUTTON, false);
break;
case Common::EVENT_RBUTTONDOWN:
handleMouseButton(MOUSE_RIGHT_BUTTON, true);
break;
case Common::EVENT_RBUTTONUP:
handleMouseButton(MOUSE_RIGHT_BUTTON, false);
break;
default:
break;
}
}
bool Input::pollEvent(uint evt) {
return pollButton(_inputEvents[evt].getBitMask());
}
bool Input::hasNewEvents() {
return lookNewButtons(kAllButtons);
}
void Input::discardEvent(uint evt) {
discardButtons(_inputEvents[evt].getBitMask());
}
void Input::discardAllEvents() {
discardButtons(kAllButtons);
}
void Input::activateButton(uint bitMask) {
_enabledButtons |= bitMask;
_buttonStates &= ~bitMask;
}
void Input::deactivateButton(uint bitMask) {
_enabledButtons &= ~bitMask;
}
Common::Point Input::getCursorPosition() {
return _cursorPos;
}
void Input::setCursorPosition(Common::Point mousePos) {
_prevCursorPos = _cursorPos = mousePos;
}
Common::Point Input::getCursorDelta() {
Common::Point deltaPos;
deltaPos.x = _prevCursorPos.x - _cursorPos.x;
deltaPos.y = _prevCursorPos.y - _cursorPos.y;
_prevCursorPos = _cursorPos;
return deltaPos;
}
InputEvent& Input::setInputEvent(uint evt, uint bitMask) {
InputEvent& inputEvent = _inputEvents[evt];
return inputEvent.setBitMask(bitMask);
}
void Input::handleKey(Common::KeyCode key, int mouseButton, bool down) {
for (uint i = 0; i < kEventMax; ++i) {
_newKeys |= _inputEvents[i].handle(key, mouseButton, down);
}
uint prevButtonStates = _buttonStates;
_buttonStates |= _newKeys;
_newKeys = 0;
_newButtons = ~prevButtonStates & _buttonStates;
if ( !down && !isCheatModeActive()) {
if ( _cheatCodeIndex < 7 && key == kCheatCode[_cheatCodeIndex]) {
_cheatCodeIndex++;
} else {
_cheatCodeIndex = 0;
}
}
}
void Input::handleMouseButton(int mouseButton, bool down) {
if (down)
_buttonsDown |= mouseButton;
else
_buttonsDown &= ~mouseButton;
handleKey(Common::KEYCODE_INVALID, mouseButton, down);
}
bool Input::pollButton(uint bitMask) {
if (lookButtonStates(bitMask)) {
_buttonStates &= ~bitMask;
return true;
}
return false;
}
bool Input::lookButtonStates(uint bitMask) {
return (bitMask & (_buttonStates & _enabledButtons)) != 0;
}
bool Input::lookNewButtons(uint bitMask) {
return (bitMask & (_newButtons & _enabledButtons)) != 0;
}
void Input::setButtonState(uint bitMask) {
_buttonStates |= _enabledButtons & bitMask;
}
void Input::discardButtons(uint bitMask) {
_buttonStates &= ~bitMask;
}
bool Input::isCheatModeActive() {
return _cheatCodeIndex == 7;
}
} // End of namespace Illusions
|