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 /*
24  * This code is based on Labyrinth of Time code with assistance of
25  *
26  * Copyright (c) 1993 Terra Nova Development
27  * Copyright (c) 2004 The Wyrmkeep Entertainment Co.
28  *
29  */
30 
31 #include "common/events.h"
32 
33 #include "graphics/cursorman.h"
34 
35 #include "lab/lab.h"
36 
37 #include "lab/dispman.h"
38 #include "lab/eventman.h"
39 #include "lab/image.h"
40 #include "lab/interface.h"
41 
42 namespace Lab {
43 
44 static const byte mouseData[] = {
45 	1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
46 	1, 7, 1, 0, 0, 0, 0, 0, 0, 0,
47 	1, 7, 7, 1, 0, 0, 0, 0, 0, 0,
48 	1, 7, 7, 7, 1, 0, 0, 0, 0, 0,
49 	1, 7, 7, 7, 7, 1, 0, 0, 0, 0,
50 	1, 7, 7, 7, 7, 7, 1, 0, 0, 0,
51 	1, 7, 7, 7, 7, 7, 7, 1, 0, 0,
52 	1, 7, 7, 7, 7, 7, 7, 7, 1, 0,
53 	1, 7, 7, 7, 7, 7, 1, 1, 1, 1,
54 	1, 7, 7, 1, 7, 7, 1, 0, 0, 0,
55 	1, 7, 1, 0, 1, 7, 7, 1, 0, 0,
56 	1, 1, 0, 0, 1, 7, 7, 1, 0, 0,
57 	0, 0, 0, 0, 0, 1, 7, 7, 1, 0,
58 	0, 0, 0, 0, 0, 1, 7, 7, 1, 0,
59 	0, 0, 0, 0, 0, 0, 1, 1, 0, 0
60 };
61 
62 #define MOUSE_WIDTH 10
63 #define MOUSE_HEIGHT 15
64 
EventManager(LabEngine * vm)65 EventManager::EventManager(LabEngine *vm) : _vm(vm) {
66 	_leftClick = false;
67 	_rightClick = false;
68 	_buttonHit = false;
69 	_mousePos = Common::Point(0, 0);
70 	_keyPressed = Common::KEYCODE_INVALID;
71 }
72 
initMouse()73 void EventManager::initMouse() {
74 	CursorMan.pushCursor(mouseData, MOUSE_WIDTH, MOUSE_HEIGHT, 0, 0, 0);
75 	CursorMan.showMouse(false);
76 
77 	setMousePos(Common::Point(_vm->_graphics->_screenWidth / 2, _vm->_graphics->_screenHeight / 2));
78 }
79 
mouseShow()80 void EventManager::mouseShow() {
81 	CursorMan.showMouse(true);
82 }
83 
mouseHide()84 void EventManager::mouseHide() {
85 	CursorMan.showMouse(false);
86 }
87 
setMousePos(Common::Point pos)88 void EventManager::setMousePos(Common::Point pos) {
89 	if (_vm->_isHiRes)
90 		_vm->_system->warpMouse(pos.x, pos.y);
91 	else
92 		_vm->_system->warpMouse(pos.x * 2, pos.y);
93 }
94 
processInput()95 void EventManager::processInput() {
96 	Common::Event event;
97 
98 	while (_vm->_system->getEventManager()->pollEvent(event)) {
99 		switch (event.type) {
100 		case Common::EVENT_LBUTTONDOWN:
101 			if (_vm->_interface->checkButtonHit(_mousePos))
102 				_buttonHit = true;
103 			else
104 				_leftClick = true;
105 			break;
106 		case Common::EVENT_RBUTTONDOWN:
107 			_rightClick = true;
108 			break;
109 		case Common::EVENT_MOUSEMOVE:
110 			_mousePos = event.mouse;
111 			break;
112 		case Common::EVENT_KEYDOWN:
113 			switch (event.kbd.keycode) {
114 			case Common::KEYCODE_LEFTBRACKET:
115 				_vm->changeVolume(-1);
116 				break;
117 			case Common::KEYCODE_RIGHTBRACKET:
118 				_vm->changeVolume(1);
119 				break;
120 			default:
121 				_keyPressed = event.kbd;
122 				break;
123 			}
124 			break;
125 		case Common::EVENT_QUIT:
126 		case Common::EVENT_RETURN_TO_LAUNCHER:
127 		default:
128 			break;
129 		}
130 	}
131 }
132 
getMsg()133 IntuiMessage *EventManager::getMsg() {
134 	static IntuiMessage message;
135 
136 	_vm->_interface->handlePressedButton();
137 	processInput();
138 
139 	if (_buttonHit) {
140 		Button *lastButtonHit = _vm->_interface->checkButtonHit(_mousePos);
141 		_buttonHit = false;
142 		if (lastButtonHit) {
143 			_vm->_interface->handlePressedButton();
144 			message._msgClass = kMessageButtonUp;
145 			message._code = lastButtonHit->_buttonId;
146 			message._qualifier = _keyPressed.flags;
147 
148 			return &message;
149 		} else
150 			return nullptr;
151 	} else if (_leftClick || _rightClick) {
152 		message._msgClass = (_leftClick) ? kMessageLeftClick : kMessageRightClick;
153 		message._qualifier = 0;
154 		message._mouse = _mousePos;
155 		_leftClick = _rightClick = false;
156 		return &message;
157 	} else if (_keyPressed.keycode != Common::KEYCODE_INVALID) {
158 		Button *curButton = _vm->_interface->checkNumButtonHit(_keyPressed.keycode);
159 
160 		if (curButton) {
161 			message._msgClass = kMessageButtonUp;
162 			message._code = curButton->_buttonId;
163 		} else {
164 			message._msgClass = kMessageRawKey;
165 			message._code = _keyPressed.keycode;
166 		}
167 
168 		message._qualifier = _keyPressed.flags;
169 		message._mouse = _mousePos;
170 
171 		_keyPressed.keycode = Common::KEYCODE_INVALID;
172 
173 		return &message;
174 	} else
175 		return nullptr;
176 }
177 
updateAndGetMousePos()178 Common::Point EventManager::updateAndGetMousePos() {
179 	processInput();
180 
181 	return _mousePos;
182 }
183 
simulateEvent()184 void EventManager::simulateEvent() {
185 	// Simulate an event by setting an unused key
186 	_keyPressed = Common::KeyState(Common::KEYCODE_SEMICOLON);
187 }
188 
189 } // End of namespace Lab
190