1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "titanic/input_handler.h"
24 #include "titanic/input_translator.h"
25 #include "titanic/events.h"
26 #include "titanic/messages/mouse_messages.h"
27 #include "titanic/titanic.h"
28 
29 namespace Titanic {
30 
CInputTranslator(CInputHandler * inputHandler)31 CInputTranslator::CInputTranslator(CInputHandler *inputHandler) :
32 		_inputHandler(inputHandler) {
33 	inputHandler->setTranslator(this);
34 }
35 
getButtons(int special) const36 int CInputTranslator::getButtons(int special) const {
37 	int buttons = 0;
38 	if (special & MK_LBUTTON)
39 		buttons |= MB_LEFT;
40 	if (special & MK_MBUTTON)
41 		buttons |= MB_MIDDLE;
42 	if (special & MK_RBUTTON)
43 		buttons |= MB_RIGHT;
44 
45 	return buttons;
46 }
47 
mouseMove(int special,const Point & pt)48 void CInputTranslator::mouseMove(int special, const Point &pt) {
49 	CMouseMoveMsg msg(pt, getButtons(special));
50 	_inputHandler->handleMessage(msg);
51 }
52 
leftButtonDown(int special,const Point & pt)53 void CInputTranslator::leftButtonDown(int special, const Point &pt) {
54 	CMouseButtonDownMsg msg(pt, MB_LEFT);
55 	_inputHandler->handleMessage(msg);
56 }
57 
leftButtonUp(int special,const Point & pt)58 void CInputTranslator::leftButtonUp(int special, const Point &pt) {
59 	CMouseButtonUpMsg msg(pt, MB_LEFT);
60 	_inputHandler->handleMessage(msg);
61 }
62 
leftButtonDoubleClick(int special,const Point & pt)63 void CInputTranslator::leftButtonDoubleClick(int special, const Point &pt) {
64 	CMouseDoubleClickMsg msg(pt, MB_LEFT);
65 	_inputHandler->handleMessage(msg);
66 }
67 
middleButtonDown(int special,const Point & pt)68 void CInputTranslator::middleButtonDown(int special, const Point &pt) {
69 	CMouseButtonDownMsg msg(pt, MB_MIDDLE);
70 	_inputHandler->handleMessage(msg);
71 }
72 
middleButtonUp(int special,const Point & pt)73 void CInputTranslator::middleButtonUp(int special, const Point &pt) {
74 	CMouseButtonUpMsg msg(pt, MB_MIDDLE);
75 	_inputHandler->handleMessage(msg);
76 }
77 
middleButtonDoubleClick(int special,const Point & pt)78 void CInputTranslator::middleButtonDoubleClick(int special, const Point &pt) {
79 	CMouseDoubleClickMsg msg(pt, MB_MIDDLE);
80 	_inputHandler->handleMessage(msg);
81 }
82 
mouseWheel(bool wheelUp,const Point & pt)83 void CInputTranslator::mouseWheel(bool wheelUp, const Point &pt) {
84 	CMouseWheelMsg msg(pt, wheelUp);
85 	_inputHandler->handleMessage(msg);
86 }
87 
keyDown(const Common::KeyState & keyState)88 void CInputTranslator::keyDown(const Common::KeyState &keyState) {
89 	if (keyState.ascii > 0 && keyState.ascii <= 127) {
90 		CKeyCharMsg msg(keyState.ascii);
91 		if (_inputHandler->handleMessage(msg))
92 			return;
93 	}
94 
95 	if (CMovementMsg::getMovement(keyState.keycode) != MOVE_NONE) {
96 		CMovementMsg msg(keyState.keycode);
97 		if (_inputHandler->handleMessage(msg))
98 			return;
99 	}
100 
101 	if (isSpecialKey(keyState.keycode)) {
102 		CVirtualKeyCharMsg msg(keyState);
103 		msg._keyState.ascii = 0;
104 		_inputHandler->handleMessage(msg);
105 	}
106 }
107 
isMousePressed() const108 bool CInputTranslator::isMousePressed() const {
109 	return g_vm->_events->getSpecialButtons() & (MK_LBUTTON | MK_RBUTTON | MK_MBUTTON);
110 }
111 
isSpecialKey(Common::KeyCode key)112 bool CInputTranslator::isSpecialKey(Common::KeyCode key) {
113 	if ((key >= Common::KEYCODE_F1 && key <= Common::KEYCODE_F8) ||
114 		(key >= Common::KEYCODE_KP1 && key <= Common::KEYCODE_KP9))
115 		return true;
116 
117 	if (key == Common::KEYCODE_PAGEUP || key == Common::KEYCODE_PAGEDOWN ||
118 		key == Common::KEYCODE_HOME || key == Common::KEYCODE_END ||
119 		key == Common::KEYCODE_LEFT || key == Common::KEYCODE_RIGHT ||
120 		key == Common::KEYCODE_UP || key == Common::KEYCODE_DOWN)
121 		return true;
122 
123 	return false;
124 }
125 
126 } // End of namespace Titanic
127