aboutsummaryrefslogtreecommitdiff
path: root/engines/sword25/input/inputengine.cpp
blob: 0d1c4498053494c6cf97c07e9621e489d5cbb8ac (plain)
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/* 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.
 *
 */

/*
 * This code is based on Broken Sword 2.5 engine
 *
 * Copyright (c) Malte Thiesen, Daniel Queteschiner and Michael Elsdoerfer
 *
 * Licensed under GNU GPL v2
 *
 */

#include "common/algorithm.h"
#include "common/events.h"
#include "common/system.h"
#include "common/util.h"

#include "sword25/sword25.h"
#include "sword25/kernel/kernel.h"
#include "sword25/kernel/inputpersistenceblock.h"
#include "sword25/kernel/outputpersistenceblock.h"
#include "sword25/input/inputengine.h"

namespace Sword25 {

#define DOUBLE_CLICK_TIME 500
#define DOUBLE_CLICK_RECT_SIZE 4

InputEngine::InputEngine(Kernel *pKernel) :
	Service(pKernel),
	_currentState(0),
	_leftMouseDown(false),
	_rightMouseDown(false),
	_mouseX(0),
	_mouseY(0),
	_leftDoubleClick(false),
	_doubleClickTime(DOUBLE_CLICK_TIME),
	_doubleClickRectWidth(DOUBLE_CLICK_RECT_SIZE),
	_doubleClickRectHeight(DOUBLE_CLICK_RECT_SIZE),
	_lastLeftClickTime(0),
	_lastLeftClickMouseX(0),
	_lastLeftClickMouseY(0) {
	memset(_keyboardState[0], 0, sizeof(_keyboardState[0]));
	memset(_keyboardState[1], 0, sizeof(_keyboardState[1]));
	_leftMouseState[0] = false;
	_leftMouseState[1] = false;
	_rightMouseState[0] = false;
	_rightMouseState[1] = false;

	if (!registerScriptBindings())
		error("Script bindings could not be registered.");
	else
		debugC(kDebugScript, "Script bindings registered.");
}

InputEngine::~InputEngine() {
	unregisterScriptBindings();
}

bool InputEngine::init() {
	// No initialisation needed
	return true;
}

void InputEngine::update() {
	Common::Event event;

	// We keep two sets of keyboard states: The current one, and that of
	// the previous frame. This allows us to detect which keys changed
	// state. Also, by keeping a single central keystate array, we
	// ensure that all script queries for key state during a single
	// frame get the same consistent replies.
	_currentState ^= 1;
	memcpy(_keyboardState[_currentState], _keyboardState[_currentState ^ 1], sizeof(_keyboardState[0]));

	// Loop through processing any pending events
	bool handleEvents = true;
	while (handleEvents && g_system->getEventManager()->pollEvent(event)) {
		switch (event.type) {
		case Common::EVENT_LBUTTONDOWN:
		case Common::EVENT_LBUTTONUP:
			_leftMouseDown = event.type == Common::EVENT_LBUTTONDOWN;
			_mouseX = event.mouse.x;
			_mouseY = event.mouse.y;
			handleEvents = false;
			break;
		case Common::EVENT_RBUTTONDOWN:
		case Common::EVENT_RBUTTONUP:
			_rightMouseDown = event.type == Common::EVENT_RBUTTONDOWN;
			_mouseX = event.mouse.x;
			_mouseY = event.mouse.y;
			handleEvents = false;
			break;

		case Common::EVENT_MOUSEMOVE:
			_mouseX = event.mouse.x;
			_mouseY = event.mouse.y;
			break;

		case Common::EVENT_KEYDOWN:
		case Common::EVENT_KEYUP:
			// FIXME - Need to work out how to expose getDebugger() to this module
			//if (event.kbd.hasFlags(Common::KBD_CTRL) && event.kbd.keycode == Common::KEYCODE_d && event.type == Common::EVENT_KEYDOWN) {
			//	_vm->getDebugger()->attach();
			//	_vm->getDebugger()->onFrame();
			//}

			alterKeyboardState(event.kbd.keycode, (event.type == Common::EVENT_KEYDOWN) ? 0x80 : 0);
			break;

		default:
			break;
		}
	}

	_leftMouseState[_currentState] = _leftMouseDown;
	_rightMouseState[_currentState] = _rightMouseDown;

	testForLeftDoubleClick();
}

bool InputEngine::isLeftMouseDown() {
	return _leftMouseDown;
}

bool InputEngine::isRightMouseDown() {
	return _rightMouseDown;
}

void InputEngine::testForLeftDoubleClick() {
	_leftDoubleClick = false;

	// Only bother checking for a double click if the left mouse button was clicked
	if (wasLeftMouseDown()) {
		// Get the time now
		uint now = Kernel::getInstance()->getMilliTicks();

		// A double click is signalled if
		// 1. The two clicks are close enough together
		// 2. The mouse cursor hasn't moved much
		if (now - _lastLeftClickTime <= _doubleClickTime &&
		        ABS(_mouseX - _lastLeftClickMouseX) <= _doubleClickRectWidth / 2 &&
		        ABS(_mouseY - _lastLeftClickMouseY) <= _doubleClickRectHeight / 2) {
			_leftDoubleClick = true;

			// Reset the time and position of the last click, so that clicking is not
			// interpreted as the first click of a further double-click
			_lastLeftClickTime = 0;
			_lastLeftClickMouseX = 0;
			_lastLeftClickMouseY = 0;
		} else {
			// There is no double click. Remember the position and time of the click,
			// in case it's the first click of a double-click sequence
			_lastLeftClickTime = now;
			_lastLeftClickMouseX = _mouseX;
			_lastLeftClickMouseY = _mouseY;
		}
	}
}

void InputEngine::alterKeyboardState(int keycode, byte newState) {
	assert(keycode < ARRAYSIZE(_keyboardState[_currentState]));
	_keyboardState[_currentState][keycode] = newState;
}

bool InputEngine::isLeftDoubleClick() {
	return _leftDoubleClick;
}

bool InputEngine::wasLeftMouseDown() {
	return (_leftMouseState[_currentState] == false) && (_leftMouseState[_currentState ^ 1] == true);
}

bool InputEngine::wasRightMouseDown() {
	return (_rightMouseState[_currentState] == false) && (_rightMouseState[_currentState ^ 1] == true);
}

int InputEngine::getMouseX() {
	return _mouseX;
}

int InputEngine::getMouseY() {
	return _mouseY;
}

bool InputEngine::isKeyDown(uint keyCode) {
	assert(keyCode < ARRAYSIZE(_keyboardState[_currentState]));
	return (_keyboardState[_currentState][keyCode] & 0x80) != 0;
}

bool InputEngine::wasKeyDown(uint keyCode) {
	assert(keyCode < ARRAYSIZE(_keyboardState[_currentState]));
	return ((_keyboardState[_currentState][keyCode] & 0x80) == 0) &&
	       ((_keyboardState[_currentState ^ 1][keyCode] & 0x80) != 0);
}

void InputEngine::setCharacterCallback(CharacterCallback callback) {
	_characterCallback = callback;
}

void InputEngine::setCommandCallback(CommandCallback callback) {
	_commandCallback = callback;
}

void InputEngine::reportCharacter(byte character) {
	if (_characterCallback)
		(*_characterCallback)(character);
}

void InputEngine::reportCommand(KEY_COMMANDS command) {
	if (_commandCallback)
		(*_commandCallback)(command);
}

bool InputEngine::persist(OutputPersistenceBlock &writer) {
	// Write out the number of command callbacks and their names.
	// Note: We do this only for compatibility with older engines resp.
	// the original engine.
	writer.write((uint32)1);
	writer.writeString("LuaCommandCB");

	// Write out the number of command callbacks and their names.
	// Note: We do this only for compatibility with older engines resp.
	// the original engine.
	writer.write((uint32)1);
	writer.writeString("LuaCharacterCB");

	return true;
}

bool InputEngine::unpersist(InputPersistenceBlock &reader) {
	Common::String callbackFunctionName;

	// Read number of command callbacks and their names.
	// Note: We do this only for compatibility with older engines resp.
	// the original engine.
	uint32 commandCallbackCount;
	reader.read(commandCallbackCount);
	assert(commandCallbackCount == 1);

	reader.readString(callbackFunctionName);
	assert(callbackFunctionName == "LuaCommandCB");

	// Read number of character callbacks and their names.
	// Note: We do this only for compatibility with older engines resp.
	// the original engine.
	uint32 characterCallbackCount;
	reader.read(characterCallbackCount);
	assert(characterCallbackCount == 1);

	reader.readString(callbackFunctionName);
	assert(callbackFunctionName == "LuaCharacterCB");

	return reader.isGood();
}

} // End of namespace Sword25