aboutsummaryrefslogtreecommitdiff
path: root/engines/titanic/input_handler.cpp
blob: df09cf24fb4ab6135032106b1dfb7fadf6ece8ef (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
/* 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/input_handler.h"
#include "titanic/game_manager.h"
#include "titanic/titanic.h"
#include "titanic/messages/mouse_messages.h"
#include "titanic/pet_control/pet_control.h"
#include "titanic/support/screen_manager.h"

namespace Titanic {

CInputHandler::CInputHandler(CGameManager *owner) :
		_gameManager(owner), _inputTranslator(nullptr), _dragging(false),
		_buttonDown(false), _dragItem(nullptr),  _lockCount(0),
		_singleton(false) {
	CScreenManager::_screenManagerPtr->_inputHandler = this;
}

CInputHandler::~CInputHandler() {
	CScreenManager::_screenManagerPtr->_inputHandler = nullptr;
}

void CInputHandler::setTranslator(CInputTranslator *translator) {
	_inputTranslator = translator;
}

void CInputHandler::incLockCount() {
	++_lockCount;
}

void CInputHandler::decLockCount() {
	if (--_lockCount == 0 && _inputTranslator) {
		if (_dragging && !_inputTranslator->isMousePressed()) {
			CMouseButtonUpMsg upMsg(_mousePos, MK_LBUTTON);
			handleMessage(upMsg);
		}

		_buttonDown = _inputTranslator->isMousePressed();
		_singleton = true;
	}
}

void CInputHandler::handleMessage(CMessage &msg, bool respectLock) {
	if (!respectLock || _lockCount <= 0) {
		if (_gameManager->_gameState._mode == GSMODE_INTERACTIVE) {
			processMessage(&msg);
		} else if (!msg.isMouseMsg()) {
			g_vm->_filesManager->loadDrive();
		}
	}
}

void CInputHandler::processMessage(CMessage *msg) {
	const CMouseMsg *mouseMsg = dynamic_cast<const CMouseMsg *>(msg);
	_singleton = false;
	dispatchMessage(msg);

	if (_singleton) {
		_singleton = false;
	} else if (mouseMsg) {
		// Keep the game state mouse position up to date
		if (_mousePos != mouseMsg->_mousePos) {
			_mousePos = mouseMsg->_mousePos;
			_gameManager->_gameState.setMousePos(mouseMsg->_mousePos);
		}

		// Set flag for whether a mouse button is currently being pressed
		if (mouseMsg->isButtonDownMsg())
			_buttonDown = true;
		else if (mouseMsg->isButtonUpMsg())
			_buttonDown = false;

		// Drag events generation
		if (_dragging) {
			if (mouseMsg->isMouseMoveMsg()) {
				if (_dragItem) {
					CMouseDragMoveMsg moveMsg(_mousePos);
					moveMsg.execute(_dragItem);
				}
			} else {
				if (mouseMsg->isButtonUpMsg() && _dragItem) {
					// Mouse drag ended
					CGameObject *target = dragEnd(_mousePos, _dragItem);
					CMouseDragEndMsg endMsg(_mousePos, target);
					endMsg.execute(_dragItem);
				}

				_dragging = false;
				_dragItem = nullptr;
			}
		} else if (_buttonDown) {
			if (!mouseMsg->isMouseMoveMsg()) {
				// Save where the drag movement started from
				_dragStartPos = _mousePos;
			} else {
				Point delta = mouseMsg->_mousePos - _dragStartPos;
				int distance = (int)sqrt(double(delta.x * delta.x + delta.y * delta.y));

				if (distance > 4) {
					// We've moved far enough with the mouse button held down
					// to trigger an official dragging operation
					CMouseDragStartMsg startMsg(_dragStartPos);
					dispatchMessage(&startMsg);

					// Set the drag item, if any, that a handler will have set on the message
					_dragItem = startMsg._dragItem;
					_gameManager->_dragItem = startMsg._dragItem;

					if (_dragItem) {
						CMouseDragMoveMsg moveMsg(_dragStartPos);
						dispatchMessage(&moveMsg);
					}

					_dragging = true;
				}
			}
		}
	}
}

void CInputHandler::dispatchMessage(CMessage *msg) {
	CPetControl *pet = _gameManager->_project->getPetControl();
	if (!pet || !msg->execute(pet, nullptr, MSGFLAG_BREAK_IF_HANDLED)) {
		CViewItem *view = _gameManager->getView();
		msg->execute(view);
	}
}

CGameObject *CInputHandler::dragEnd(const Point &pt, CTreeItem *dragItem) {
	CViewItem *view = _gameManager->getView();
	if (!view)
		return nullptr;

	// Scan through the view items to find the item being dropped on
	CGameObject *target = nullptr;
	for (CTreeItem *treeItem = view->scan(view); treeItem; treeItem = treeItem->scan(view)) {
		CGameObject *gameObject = dynamic_cast<CGameObject *>(treeItem);
		if (gameObject && gameObject != dragItem) {
			if (gameObject->checkPoint(pt))
				target = gameObject;
		}
	}

	if (target) {
		// Check if the cursor is on the PET. If so, pass to the PET
		// to see what specific element the drag ended on
		CProjectItem *project = view->getRoot();		
		if (project) {
			CPetControl *petControl = project->getPetControl();
			if (petControl && petControl->contains(pt)) {
				target = petControl->dragEnd(pt);
				if (!target)
					target = petControl;
			}
		}
	}

	return target;
}

} // End of namespace Titanic