aboutsummaryrefslogtreecommitdiff
path: root/engines/titanic/core/view_item.cpp
blob: 60e08a558266ed198cb923e1267eee4affdfc871 (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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/* 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 "titanic/game_manager.h"
#include "titanic/support/screen_manager.h"
#include "titanic/core/project_item.h"
#include "titanic/core/room_item.h"
#include "titanic/core/view_item.h"
#include "titanic/messages/messages.h"
#include "titanic/pet_control/pet_control.h"
#include "titanic/titanic.h"

namespace Titanic {

BEGIN_MESSAGE_MAP(CViewItem, CNamedItem)
	ON_MESSAGE(MouseButtonDownMsg)
	ON_MESSAGE(MouseButtonUpMsg)
	ON_MESSAGE(MouseDoubleClickMsg)
	ON_MESSAGE(MouseMoveMsg)
	ON_MESSAGE(MovementMsg)
END_MESSAGE_MAP()

CViewItem::CViewItem() : CNamedItem() {
	Common::fill(&_buttonUpTargets[0], &_buttonUpTargets[4], (CTreeItem *)nullptr);
	_field24 = 0;
	_angle = 0.0;
	_viewNumber = 0;
	setAngle(0.0);
}

void CViewItem::setAngle(double angle) {
	_angle = angle;
	_viewPos.x = (int16)(cos(_angle) * 30.0);
	_viewPos.y = (int16)(sin(_angle) * -30.0);
}

void CViewItem::save(SimpleFile *file, int indent) {
	file->writeNumberLine(1, indent);
	_resourceKey.save(file, indent);
	file->writeQuotedLine("V", indent);
	file->writeFloatLine(_angle, indent + 1);
	file->writeNumberLine(_viewNumber, indent + 1);

	CNamedItem::save(file, indent);
}

void CViewItem::load(SimpleFile *file) {
	int val = file->readNumber();

	switch (val) {
	case 1:
		_resourceKey.load(file);
		// Intentional fall-through

	default:
		file->readBuffer();
		setAngle(file->readFloat());
		_viewNumber = file->readNumber();
		break;
	}

	CNamedItem::load(file);
}

bool CViewItem::getResourceKey(CResourceKey *key) {
	*key = _resourceKey;
	CString filename = key->getFilename();
	return !filename.empty();
}

void CViewItem::leaveView(CViewItem *newView) {
	// Only do the processing if we've been passed a view, and it's not the same
	if (newView && newView != this) {
		CLeaveViewMsg viewMsg(this, newView);
		viewMsg.execute(this, nullptr, MSGFLAG_SCAN);

		CNodeItem *oldNode = findNode();
		CNodeItem *newNode = newView->findNode();
		if (newNode != oldNode) {
			CLeaveNodeMsg nodeMsg(oldNode, newNode);
			nodeMsg.execute(oldNode, nullptr, MSGFLAG_SCAN);

			CRoomItem *oldRoom = oldNode->findRoom();
			CRoomItem *newRoom = newNode->findRoom();
			if (newRoom != oldRoom) {
				CGameManager *gm = getGameManager();
				if (gm)
					gm->roomChange();

				CLeaveRoomMsg roomMsg(oldRoom, newRoom);
				roomMsg.execute(oldRoom, nullptr, MSGFLAG_SCAN);
			}
		}
	}
}

void CViewItem::preEnterView(CViewItem *newView) {
	// Only do the processing if we've been passed a view, and it's not the same
	if (newView && newView != this) {
		CPreEnterViewMsg viewMsg(this, newView);
		viewMsg.execute(newView, nullptr, MSGFLAG_SCAN);

		CNodeItem *oldNode = findNode();
		CNodeItem *newNode = newView->findNode();
		if (newNode != oldNode) {
			CPreEnterNodeMsg nodeMsg(oldNode, newNode);
			nodeMsg.execute(newNode, nullptr, MSGFLAG_SCAN);

			CRoomItem *oldRoom = oldNode->findRoom();
			CRoomItem *newRoom = newNode->findRoom();
			if (newRoom != oldRoom) {
				CPreEnterRoomMsg roomMsg(oldRoom, newRoom);
				roomMsg.execute(newRoom, nullptr, MSGFLAG_SCAN);
			}
		}
	}
}

void CViewItem::enterView(CViewItem *newView) {
	// Only do the processing if we've been passed a view, and it's not the same
	if (newView && newView != this) {
		CEnterViewMsg viewMsg(this, newView);
		viewMsg.execute(newView, nullptr, MSGFLAG_SCAN);

		CNodeItem *oldNode = findNode();
		CNodeItem *newNode = newView->findNode();
		if (newNode != oldNode) {
			CEnterNodeMsg nodeMsg(oldNode, newNode);
			nodeMsg.execute(newNode, nullptr, MSGFLAG_SCAN);

			CRoomItem *oldRoom = oldNode->findRoom();
			CRoomItem *newRoom = newNode->findRoom();

			CPetControl *petControl = nullptr;
			if (newRoom != nullptr) {
				petControl = newRoom->getRoot()->getPetControl();
				if (petControl)
					petControl->enterNode(newNode);
			}

			if (newRoom != oldRoom) {
				CEnterRoomMsg roomMsg(oldRoom, newRoom);
				roomMsg.execute(newRoom, nullptr, MSGFLAG_SCAN);

				if (petControl)
					petControl->enterRoom(newRoom);
			}
		}

		// WORKAROUND: Do a dummy mouse movement, to allow for the correct cursor 
		// to be set for the current position in the new view
		CMouseMoveMsg moveMsg(g_vm->_events->getMousePos(), 0);
		newView->MouseMoveMsg(&moveMsg);
	}
}

CLinkItem *CViewItem::findLink(CViewItem *newView) {
	for (CTreeItem *treeItem = getFirstChild(); treeItem;
			treeItem = treeItem->scan(this)) {
		CLinkItem *link = dynamic_cast<CLinkItem *>(treeItem);
		if (link && link->connectsTo(newView))
			return link;
	}

	return nullptr;
}

bool CViewItem::MouseButtonDownMsg(CMouseButtonDownMsg *msg) {
	if (msg->_buttons & MB_LEFT) {
		if (!handleMouseMsg(msg, true)) {
			CGameManager *gm = getGameManager();
			if (gm->isntTransitioning()) {
				findNode()->findRoom();

				CLinkItem *linkItem = dynamic_cast<CLinkItem *>(
					findChildInstanceOf(CLinkItem::_type));
				while (linkItem) {
					if (linkItem->_bounds.contains(msg->_mousePos)) {
						gm->_gameState.triggerLink(linkItem);
						return true;
					}

					linkItem = dynamic_cast<CLinkItem *>(
						findNextInstanceOf(CLinkItem::_type, linkItem));
				}

				handleMouseMsg(msg, false);
			}
		}
	}

	return true;
}

bool CViewItem::MouseButtonUpMsg(CMouseButtonUpMsg *msg) {
	if (msg->_buttons & MB_LEFT)
		handleMouseMsg(msg, false);

	return true;
}

bool CViewItem::MouseDoubleClickMsg(CMouseDoubleClickMsg *msg) {
	if (msg->_buttons & MB_LEFT)
		handleMouseMsg(msg, false);

	return true;
}

bool CViewItem::MouseMoveMsg(CMouseMoveMsg *msg) {
	CScreenManager *screenManager = CScreenManager::_screenManagerPtr;
	uint changeCount = screenManager->_mouseCursor->getChangeCount();

	if (handleMouseMsg(msg, true)) {
		// If the cursor hasn't been set in the call to handleMouseMsg,
		// then reset it back to the default arrow cursor
		if (screenManager->_mouseCursor->getChangeCount() == changeCount)
			screenManager->_mouseCursor->setCursor(CURSOR_ARROW);
	} else {
		// Iterate through each link item, and if any is highlighted,
		// change the mouse cursor to the designated cursor for the item
		CTreeItem *treeItem = getFirstChild();
		while (treeItem) {
			CLinkItem *linkItem = dynamic_cast<CLinkItem *>(treeItem);
			if (linkItem && linkItem->_bounds.contains(msg->_mousePos)) {
				screenManager->_mouseCursor->setCursor(linkItem->_cursorId);
				return true;
			}

			treeItem = treeItem->getNextSibling();
		}

		if (!handleMouseMsg(msg, false) || (screenManager->_mouseCursor->getChangeCount() == changeCount))
			screenManager->_mouseCursor->setCursor(CURSOR_ARROW);
	}

	return true;
}

bool CViewItem::handleMouseMsg(CMouseMsg *msg, bool flag) {
	CMouseButtonUpMsg *upMsg = dynamic_cast<CMouseButtonUpMsg *>(msg);
	if (upMsg) {
		handleButtonUpMsg(upMsg);
		return true;
	}

	Common::Array<CGameObject *> gameObjects;
	for (CTreeItem *treeItem = scan(this); treeItem; treeItem = treeItem->scan(this)) {
		CGameObject *gameObject = dynamic_cast<CGameObject *>(treeItem);
		if (gameObject) {
			if (gameObject->checkPoint(msg->_mousePos, false, true) &&
					(!flag || !gameObject->_handleMouseFlag)) {
				if (gameObjects.size() < 256)
					gameObjects.push_back(gameObject);
			}
		}
	}

	const CMouseMoveMsg *moveMsg = dynamic_cast<const CMouseMoveMsg *>(msg);
	if (moveMsg) {
		if (gameObjects.size() == 0)
			return false;

		for (int idx = (int)gameObjects.size() - 1; idx >= 0; --idx) {
			if (gameObjects[idx]->_cursorId != CURSOR_IGNORE) {
				CScreenManager::_screenManagerPtr->_mouseCursor->setCursor(gameObjects[idx]->_cursorId);
				break;
			}
		}
	}
	if (gameObjects.size() == 0)
		return false;

	bool result = false;
	for (int idx = (int)gameObjects.size() - 1; idx >= 0; --idx) {
		if (msg->execute(gameObjects[idx])) {
			if (msg->isButtonDownMsg())
				_buttonUpTargets[msg->_buttons >> 1] = gameObjects[idx];
			return true;
		}

		if (CMouseMsg::isSupportedBy(gameObjects[idx]))
			result = true;
	}

	return result;
}

void CViewItem::handleButtonUpMsg(CMouseButtonUpMsg *msg) {
	CTreeItem *&target = _buttonUpTargets[msg->_buttons >> 1];

	if (target) {
		msg->execute(target);
		target = nullptr;
	}
}

void CViewItem::getPosition(double &xp, double &yp, double &zp) {
	// Get the position of the owning node within the room
	CNodeItem *node = findNode();
	node->getPosition(xp, yp, zp);

	// Adjust the position slightly to compensate for view's angle,
	// ensuring different direction views don't all have the same position
	xp += cos(_angle) * 0.5;
	yp -= sin(_angle) * 0.5;
}

CString CViewItem::getFullViewName() const {
	CNodeItem *node = findNode();
	CRoomItem *room = node->findRoom();

	return CString::format("%s.%s.%s", room->getName().c_str(),
		node->getName().c_str(), getName().c_str());
}

CString CViewItem::getNodeViewName() const {
	CNodeItem *node = findNode();

	return CString::format("%s.%s", node->getName().c_str(), getName().c_str());
}

bool CViewItem::MovementMsg(CMovementMsg *msg) {
	Point pt;
	bool foundPt = false;
	int quadrant;

	// First allow any child objects to handle it
	for (CTreeItem *treeItem = getFirstChild(); treeItem;
		treeItem = treeItem->scan(this)) {
		if (msg->execute(treeItem, nullptr, 0))
			return true;
	}

	if (msg->_posToUse.x != 0 || msg->_posToUse.y != 0) {
		pt = msg->_posToUse;
		foundPt = true;
	} else {
		// Iterate through the view's contents to find a link or item
		// with the appropriate movement action
		for (CTreeItem *treeItem = getFirstChild(); treeItem;
				treeItem = treeItem->scan(this)) {
			CLinkItem *link = dynamic_cast<CLinkItem *>(treeItem);
			CGameObject *gameObj = dynamic_cast<CGameObject *>(treeItem);

			if (link) {
				// Skip links that aren't for the desired direction
				if (link->getMovement() != msg->_movement)
					continue;

				for (quadrant = Q_CENTER; quadrant <= Q_BOTTOM; ++quadrant) {
					if (link->findPoint((Quadrant)quadrant, pt))
						if (link == getItemAtPoint(pt))
							break;
				}
				if (quadrant > Q_BOTTOM)
					continue;
			} else if (gameObj) {
				if (!gameObj->_visible || gameObj->getMovement() != msg->_movement)
					continue;

				for (quadrant = Q_CENTER; quadrant <= Q_BOTTOM; ++quadrant) {
					if (gameObj->findPoint((Quadrant)quadrant, pt))
						if (gameObj == getItemAtPoint(pt))
							break;
				}
				if (quadrant > Q_BOTTOM)
					continue;
			} else {
				// Not a link or object, so ignore
				continue;
			}

			foundPt = true;
			break;
		}
	}

	if (foundPt) {
		// We've found a point on the object or link that has a
		// cursor for the given direction. So simulate a mouse
		// press and release on the desired point
		CMouseButtonDownMsg downMsg(pt, MB_LEFT);
		CMouseButtonUpMsg upMsg(pt, MB_LEFT);
		MouseButtonDownMsg(&downMsg);
		MouseButtonUpMsg(&upMsg);
		return true;
	}

	return false;
}

CTreeItem *CViewItem::getItemAtPoint(const Point &pt) {
	CTreeItem *result = nullptr;

	// First scan for objects
	for (CTreeItem *treeItem = scan(this); treeItem; treeItem = treeItem->scan(this)) {
		CGameObject *gameObject = dynamic_cast<CGameObject *>(treeItem);

		if (gameObject && gameObject->checkPoint(pt, false, true))
			result = treeItem;
	}

	if (result == nullptr) {
		// Scan for links coverign that position
		for (CTreeItem *treeItem = scan(this); treeItem; treeItem = treeItem->scan(this)) {
			CLinkItem *link = dynamic_cast<CLinkItem *>(treeItem);

			if (link && link->_bounds.contains(pt)) {
				result = treeItem;
				break;
			}
		}
	}

	return result;
}

} // End of namespace Titanic