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 #ifndef SHERLOCK_EVENTS_H
24 #define SHERLOCK_EVENTS_H
25 
26 #include "common/scummsys.h"
27 #include "common/events.h"
28 #include "common/stack.h"
29 #include "sherlock/image_file.h"
30 
31 namespace Sherlock {
32 
33 #define GAME_FRAME_RATE 30
34 #define GAME_FRAME_TIME (1000 / GAME_FRAME_RATE)
35 
36 enum CursorId { ARROW = 0, MAGNIFY = 1, WAIT = 2, EXIT_ZONES_START = 5, INVALID_CURSOR = -1 };
37 
38 class SherlockEngine;
39 
40 class Events {
41 private:
42 	SherlockEngine *_vm;
43 	uint32 _frameCounter;
44 	uint32 _priorFrameTime;
45 	ImageFile *_cursorImages;
46 	int _mouseButtons;
47 	Common::Point _mousePos;
48 	int _waitCounter;
49 	uint _frameRate;
50 
51 	/**
52 	 * Check whether it's time to display the next screen frame
53 	 */
54 	bool checkForNextFrameCounter();
55 public:
56 	CursorId _cursorId;
57 	bool _pressed;
58 	bool _released;
59 	bool _rightPressed;
60 	bool _rightReleased;
61 	bool _oldButtons;
62 	bool _oldRightButton;
63 	bool _firstPress;
64 	Common::Stack<Common::KeyState> _pendingKeys;
65 	Common::Point _hotspotPos;
66 public:
67 	Events(SherlockEngine *vm);
68 	~Events();
69 
70 	/**
71 	 * Load a set of cursors from the specified file
72 	 */
73 	void loadCursors(const Common::String &filename);
74 
75 	/**
76 	 * Set the cursor to show
77 	 */
78 	void setCursor(CursorId cursorId);
79 
80 	/**
81 	 * Set the cursor to show from a passed frame
82 	 */
83 	void setCursor(const Graphics::Surface &src, int hotspotX = 0, int hotspotY = 0);
84 
85 	/**
86 	 * Set both a standard cursor as well as an inventory item above it
87 	 */
88 	void setCursor(CursorId cursorId, const Common::Point &cursorPos, const Graphics::Surface &surface);
89 
90 	/**
91 	 * Animates the mouse cursor if the Wait cursor is showing
92 	 */
93 	void animateCursorIfNeeded();
94 
95 	/**
96 	 * Show the mouse cursor
97 	 */
98 	void showCursor();
99 
100 	/**
101 	 * Hide the mouse cursor
102 	 */
103 	void hideCursor();
104 
105 	/**
106 	 * Returns the cursor
107 	 */
108 	CursorId getCursor() const;
109 
110 	/**
111 	 * Returns true if the mouse cursor is visible
112 	 */
113 	bool isCursorVisible() const;
114 
115 	/**
116 	 * Check for any pending events
117 	 */
118 	void pollEvents();
119 
120 	/**
121 	 * Poll for events and introduce a small delay, to allow the system to
122 	 * yield to other running programs
123 	 */
124 	void pollEventsAndWait();
125 
126 	/**
127 	 * Move the mouse cursor
128 	 */
129 	void warpMouse(const Common::Point &pt);
130 
131 	/**
132 	* Move the mouse cursor to the center of the screen
133 	*/
134 	void warpMouse();
135 
136 	/**
137 	 * Get the current mouse position
138 	 */
screenMousePos()139 	Common::Point screenMousePos() const { return _mousePos; }
140 
141 	/**
142 	 * Get the current mouse position within the scene, adjusted by the scroll position
143 	 */
144 	Common::Point mousePos() const;
145 
146 	/**
147 	 * Override the default frame rate
148 	 */
149 	void setFrameRate(int newRate);
150 
151 	/**
152 	 * Toggle between standard game speed and an "extra fast" mode
153 	 */
154 	void toggleSpeed();
155 
156 	/**
157 	 * Return the current game frame number
158 	 */
getFrameCounter()159 	uint32 getFrameCounter() const { return _frameCounter; }
160 
161 	/**
162 	 * Returns true if there's a pending keyboard key
163 	 */
kbHit()164 	bool kbHit() const { return !_pendingKeys.empty(); }
165 
166 	/**
167 	 * Get a pending keypress
168 	 */
169 	Common::KeyState getKey();
170 
171 	/**
172 	 * Clear any current keypress or mouse click
173 	 */
174 	void clearEvents();
175 
176 	/**
177 	 * Clear any pending keyboard inputs
178 	 */
179 	void clearKeyboard();
180 
181 	/**
182 	 * Delay for a given number of game frames, where each frame is 1/60th of a second
183 	 */
184 	void wait(int numFrames);
185 
186 	/**
187 	 * Does a delay of the specified number of milliseconds
188 	 */
189 	bool delay(uint32 time, bool interruptable = false);
190 
191 	/**
192 	 * Sets the pressed and released button flags on the raw button state previously set in pollEvents calls.
193 	 * @remarks		The events manager has separate variables for the raw immediate and old button state
194 	 *		versus the current buttons states for the frame. This method is expected to be called only once
195 	 *		per game frame
196 	 */
197 	void setButtonState();
198 
199 	/**
200 	 * Checks to see to see if a key or a mouse button is pressed.
201 	 */
202 	bool checkInput();
203 
204 	/**
205 	 * Increment the wait counter
206 	 */
207 	void incWaitCounter();
208 
209 	/**
210 	 * Decrement the wait counter
211 	 */
212 	void decWaitCounter();
213 };
214 
215 } // End of namespace Sherlock
216 
217 #endif /* SHERLOCK_EVENTS_H */
218