aboutsummaryrefslogtreecommitdiff
path: root/engines/sludge/event.cpp
blob: acccd7ff84a60f49ab3574d05955a60f9b8b2ddd (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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/* 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 "common/events.h"
#include "common/keyboard.h"

#include "sludge/event.h"
#include "sludge/freeze.h"
#include "sludge/function.h"
#include "sludge/graphics.h"
#include "sludge/newfatal.h"
#include "sludge/region.h"
#include "sludge/sludge.h"
#include "sludge/sludger.h"
#include "sludge/variable.h"

namespace Sludge {

extern Variable *launchResult;
extern VariableStack *noStack;

EventManager::EventManager(SludgeEngine *vm) {
	_vm = vm;
	_currentEvents = new EventHandlers;
	init();
}

EventManager::~EventManager() {
	kill();
	if (_currentEvents) {
		delete _currentEvents;
		_currentEvents = nullptr;
	}
}

void EventManager::init() {
	_weAreDoneSoQuit = 0;
	_reallyWantToQuit = false;

	_input.leftClick = _input.rightClick = _input.justMoved = _input.leftRelease = _input.rightRelease = false;
	_input.keyPressed = 0;

	for (uint i = 0; i < EVENT_FUNC_NB; ++i) {
		_currentEvents->func[i] = 0;
	}
}

void EventManager::kill() {
}

void EventManager::checkInput() {
	float cameraZoom = _vm->_gfxMan->getCamZoom();
#if 0
	static bool fakeRightclick = false;
#endif
	Common::Event event;

	/* Check for events */
	while (g_system->getEventManager()->pollEvent(event)) {
		switch (event.type) {
#if 0
			case SDL_VIDEORESIZE:
			realWinWidth = event.resize.w;
			realWinHeight = event.resize.h;
			setGraphicsWindow(false, true, true);
			break;
#endif
			case Common::EVENT_MOUSEMOVE:
				_input.justMoved = true;
				_input.mouseX = event.mouse.x * cameraZoom;
				_input.mouseY = event.mouse.y * cameraZoom;
				break;

			case Common::EVENT_LBUTTONDOWN:
				_input.leftClick = true;
				_input.mouseX = event.mouse.x * cameraZoom;
				_input.mouseY = event.mouse.y * cameraZoom;
#if 0
				if (SDL_GetModState() & KMOD_CTRL) {
					input.rightClick = true;
					fakeRightclick = true;
				} else {
					input.leftClick = true;
					fakeRightclick = false;
				}
#endif
				break;

			case Common::EVENT_RBUTTONDOWN:
				_input.rightClick = true;
				_input.mouseX = event.mouse.x * cameraZoom;
				_input.mouseY = event.mouse.y * cameraZoom;
				break;

			case Common::EVENT_LBUTTONUP:
				_input.leftRelease = true;
				_input.mouseX = event.mouse.x * cameraZoom;
				_input.mouseY = event.mouse.y * cameraZoom;
				break;

			case Common::EVENT_RBUTTONUP:
				_input.rightRelease = true;
				_input.mouseX = event.mouse.x * cameraZoom;
				_input.mouseY = event.mouse.y * cameraZoom;
				break;

			case Common::EVENT_KEYDOWN:
				switch (event.kbd.keycode) {

					case Common::KEYCODE_BACKSPACE:
						// fall through
					case Common::KEYCODE_DELETE:
						_input.keyPressed = Common::KEYCODE_DELETE;
						break;
					default:
						_input.keyPressed = event.kbd.keycode;
						break;
				}
				break;

			case Common::EVENT_QUIT:
				_weAreDoneSoQuit = 1;
				// TODO: if _reallyWantToQuit, popup a message box to confirm
				break;

			default:
				break;
		}
	}
}

bool EventManager::handleInput() {
	static int l = 0;

	if (!_vm->launchMe.empty()) {
		if (l) {
			// Still paused because of spawned thingy...
		} else {
			l = 1;

			setVariable(*launchResult, SVT_INT, 0/*launch(launchMe) > 31*/); //TODO:false value
			_vm->launchMe.clear();
			launchResult = nullptr;
		}
		return true;
	} else {
		l = 0;
	}

	if (!_vm->_regionMan->getOverRegion())
		_vm->_regionMan->updateOverRegion();

	if (_input.justMoved) {
		if (_currentEvents->func[kMoveMouse]) {
			if (!startNewFunctionNum(_currentEvents->func[kMoveMouse], 0, nullptr, noStack))
				return false;
		}
	}
	_input.justMoved = false;

	if (_vm-> _regionMan->isRegionChanged()&& _currentEvents->func[kFocus]) {
		VariableStack *tempStack = new VariableStack;
		if (!checkNew(tempStack))
			return false;

		ScreenRegion *overRegion = _vm->_regionMan->getOverRegion();
		if (overRegion) {
			setVariable(tempStack->thisVar, SVT_OBJTYPE, overRegion->thisType->objectNum);
		} else {
			setVariable(tempStack->thisVar, SVT_INT, 0);
		}
		tempStack->next = nullptr;
		if (!startNewFunctionNum(_currentEvents->func[kFocus], 1, nullptr, tempStack))
			return false;
	}
	if (_input.leftRelease && _currentEvents->func[kLeftMouseUp]) {
		if (!startNewFunctionNum(_currentEvents->func[kLeftMouseUp], 0, nullptr, noStack))
			return false;
	}
	if (_input.rightRelease && _currentEvents->func[kRightMouseUp]) {
		if (!startNewFunctionNum(_currentEvents->func[kRightMouseUp], 0, nullptr, noStack))
			return false;
	}
	if (_input.leftClick && _currentEvents->func[kLeftMouse])
		if (!startNewFunctionNum(_currentEvents->func[kLeftMouse], 0, nullptr, noStack))
			return false;
	if (_input.rightClick && _currentEvents->func[kRightMouse]) {
		if (!startNewFunctionNum(_currentEvents->func[kRightMouse], 0, nullptr, noStack))
			return false;
	}
	if (_input.keyPressed && _currentEvents->func[kSpace]) {
		Common::String tempString = "";
		switch (_input.keyPressed) {
			case 127:
				tempString = "BACKSPACE";
				break;
			case 9:
				tempString = "TAB";
				break;
			case 13:
				tempString = "ENTER";
				break;
			case 27:
				tempString = "ESCAPE";
				break;
				/*
				 case 1112:  tempString = copyString ("ALT+F1");     break;
				 case 1113:  tempString = copyString ("ALT+F2");     break;
				 case 1114:  tempString = copyString ("ALT+F3");     break;
				 case 1115:  tempString = copyString ("ALT+F4");     break;
				 case 1116:  tempString = copyString ("ALT+F5");     break;
				 case 1117:  tempString = copyString ("ALT+F6");     break;
				 case 1118:  tempString = copyString ("ALT+F7");     break;
				 case 1119:  tempString = copyString ("ALT+F8");     break;
				 case 1120:  tempString = copyString ("ALT+F9");     break;
				 case 1121:  tempString = copyString ("ALT+F10");    break;
				 case 1122:  tempString = copyString ("ALT+F11");    break;
				 case 1123:  tempString = copyString ("ALT+F12");    break;

				 case 2019:  tempString = copyString ("PAUSE");      break;
				 */
			case 63276:
				tempString = "PAGE UP";
				break;
			case 63277:
				tempString = "PAGE DOWN";
				break;
			case 63275:
				tempString = "END";
				break;
			case 63273:
				tempString = "HOME";
				break;
			case 63234:
				tempString = "LEFT";
				break;
			case 63232:
				tempString = "UP";
				break;
			case 63235:
				tempString = "RIGHT";
				break;
			case 63233:
				tempString = "DOWN";
				break;
				/*
				 case 2045:   tempString = copyString ("INSERT");     break;
				 case 2046:   tempString = copyString ("DELETE");     break;
				 */
			case 63236:
				tempString = "F1";
				break;
			case 63237:
				tempString = "F2";
				break;
			case 63238:
				tempString = "F3";
				break;
			case 63239:
				tempString = "F4";
				break;
			case 63240:
				tempString = "F5";
				break;
			case 63241:
				tempString = "F6";
				break;
			case 63242:
				tempString = "F7";
				break;
			case 63243:
				tempString = "F8";
				break;
			case 63244:
				tempString = "F9";
				break;
			case 63245:
				tempString = "F10";
				break;
			case 63246:
				tempString = "F11";
				break;
			case 63247:
				tempString = "F12";
				break;

			default:
				if (_input.keyPressed >= 256) {
					char tmp[7] = "ABCDEF";
					sprintf(tmp, "%i", _input.keyPressed);
					tempString = tmp;
					//}
				} else {
					char tmp[2] = " ";
					tmp[0] = _input.keyPressed;
					tempString = tmp;
				}
		}

		if (!tempString.empty()) {
			VariableStack *tempStack = new VariableStack;
			if (!checkNew(tempStack))
				return false;
			makeTextVar(tempStack->thisVar, tempString);
			tempStack->next = nullptr;
			if (!startNewFunctionNum(_currentEvents->func[kSpace], 1, nullptr, tempStack))
				return false;
		}
	}
	_input.rightClick = false;
	_input.leftClick = false;
	_input.rightRelease = false;
	_input.leftRelease = false;
	_input.keyPressed = 0;
	_vm->_regionMan->updateLastRegion();
	return true;
}

void EventManager::loadHandlers(Common::SeekableReadStream *stream) {
	for (uint i = 0; i < EVENT_FUNC_NB; ++i) {
		_currentEvents->func[i] = stream->readUint16BE();
	}
}

void EventManager::saveHandlers(Common::WriteStream *stream) {
	for (uint i = 0; i < EVENT_FUNC_NB; ++i) {
		stream->writeUint16BE(_currentEvents->func[i]);
	}
}

bool EventManager::freeze(FrozenStuffStruct *frozenStuff) {
	frozenStuff->currentEvents = _currentEvents;
	_currentEvents = new EventHandlers;
	if (!checkNew(_currentEvents))
		return false;
	for (uint i = 0; i < EVENT_FUNC_NB; ++i) {
		_currentEvents->func[i] = 0;
	}
	return true;
}

void EventManager::restore(FrozenStuffStruct *frozenStuff) {
	delete _currentEvents;
	_currentEvents = frozenStuff->currentEvents;
}

} /* namespace Sludge */