1 /*
2  * This file is part of EasyRPG Player.
3  *
4  * EasyRPG Player is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * EasyRPG Player is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with EasyRPG Player. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef EP_INPUT_H
19 #define EP_INPUT_H
20 
21 // Headers
22 #include <vector>
23 #include <bitset>
24 #include "point.h"
25 #include "system.h"
26 #include "input_buttons.h"
27 #include "input_source.h"
28 #include "keys.h"
29 
30 /**
31  * Input namespace.
32  * Input works with Button states. Buttons are
33  * representations of one or more keys or actions (like
34  * keyboard keys, mouse buttons, joystick axis). This way
35  * buttons are platform and device independent, while the
36  * assigned keys can vary by the system.
37  */
38 namespace Input {
39 	/**
40 	 * Initializes Input.
41 	 *
42 	 * @param buttons the button mappings to use
43 	 * @param directions the direction mappings to use
44 	 * @param replay_from_path path to a log file to
45 	 *  replay from, or the empty string if not replaying
46 	 * @param record_to_path path to a file to record
47 	 *  input to, or the empty string if not recording
48 	 */
49 	void Init(
50 		ButtonMappingArray buttons,
51 		DirectionMappingArray directions,
52 		const std::string& replay_from_path,
53 		const std::string& record_to_path
54 	);
55 
56 	/**
57 	 * Updates Input state.
58 	 */
59 	void Update();
60 
61 	/**
62 	 * Updates Input state for only system keys not used by game logic
63 	 */
64 	void UpdateSystem();
65 
66 	/**
67 	 * Resets all button states.
68 	 */
69 	void ResetKeys();
70 
71 	/**
72 	 * Resets only triggered states.
73 	 */
74 	void ResetTriggerKeys();
75 
76 	/**
77 	 * Gets if a button is being pressed.
78 	 *
79 	 * @param button button ID.
80 	 * @return whether the button is being pressed.
81 	 */
82 	bool IsPressed(InputButton button);
83 
84 	/**
85 	 * Gets if a button is starting to being pressed.
86 	 *
87 	 * @param button button ID.
88 	 * @return whether the button is being triggered.
89 	 */
90 	bool IsTriggered(InputButton button);
91 
92 	/**
93 	 * Gets if a button is being repeated. A button is being
94 	 * repeated while it is maintained pressed and a
95 	 * certain amount of frames has passed after last
96 	 * repetition.
97 	 *
98 	 * @param button button ID.
99 	 * @return whether the button is being repeated.
100 	 */
101 	bool IsRepeated(InputButton button);
102 
103 	/**
104 	 * Gets if a button is being released.
105 	 *
106 	 * @param button button ID.
107 	 * @return whether the button is being released.
108 	 */
109 	bool IsReleased(InputButton button);
110 
111 	/**
112 	 * Gets if any button is being pressed.
113 	 *
114 	 * @return whether any button is being pressed.
115 	 */
116 	bool IsAnyPressed();
117 
118 	/**
119 	 * Gets if any button is being triggered.
120 	 *
121 	 * @return whether any button is being triggered.
122 	 */
123 	bool IsAnyTriggered();
124 
125 	/**
126 	 * Gets if any button is being repeated.
127 	 *
128 	 * @return whether any button is being repeated.
129 	 */
130 	bool IsAnyRepeated();
131 
132 	/**
133 	 * Gets if any button is being released.
134 	 *
135 	 * @return whether any button is being released.
136 	 */
137 	bool IsAnyReleased();
138 
139 	/**
140 	 * Gets if a system button is being pressed.
141 	 *
142 	 * @param button button ID.
143 	 * @return whether the button is being pressed.
144 	 */
145 	bool IsSystemPressed(InputButton button);
146 
147 	/**
148 	 * Gets if a system button is starting to being pressed.
149 	 *
150 	 * @param button button ID.
151 	 * @return whether the button is being triggered.
152 	 */
153 	bool IsSystemTriggered(InputButton button);
154 
155 	/**
156 	 * Gets if a system button is being repeated. A button is being
157 	 * repeated while it is maintained pressed and a
158 	 * certain amount of frames has passed after last
159 	 * repetition.
160 	 *
161 	 * @param button button ID.
162 	 * @return whether the button is being repeated.
163 	 */
164 	bool IsSystemRepeated(InputButton button);
165 
166 	/**
167 	 * Gets if a system button is being released.
168 	 *
169 	 * @param button button ID.
170 	 * @return whether the button is being released.
171 	 */
172 	bool IsSystemReleased(InputButton button);
173 
174 	/**
175 	 * Gets all buttons being pressed.
176 	 *
177 	 * @return a vector with the buttons IDs.
178 	 */
179 	std::vector<InputButton> GetAllPressed();
180 
181 	/**
182 	 * Gets all buttons being triggered.
183 	 *
184 	 * @return a vector with the buttons IDs.
185 	 */
186 	std::vector<InputButton> GetAllTriggered();
187 
188 	/**
189 	 * Gets all buttons being repeated.
190 	 *
191 	 * @return a vector with the buttons IDs.
192 	 */
193 	std::vector<InputButton> GetAllRepeated();
194 
195 	/**
196 	 * Gets all buttons being released.
197 	 *
198 	 * @return a vector with the buttons IDs.
199 	 */
200 	std::vector<InputButton> GetAllReleased();
201 
202 	/**
203 	 * Returns the key mask for manipulation. When a bit is set the key is
204 	 * ignored even if it is mapped. Only raw reads will return the key state.
205 	 *
206 	 * @return key mask
207 	 */
208 	Input::KeyStatus GetMask();
209 
210 	/**
211 	 * @param new_mask The new key mask to set
212 	 */
213 	void SetMask(Input::KeyStatus new_mask);
214 
215 	/**
216 	 * Resets the key mask. All keys are reported again.
217 	 */
218 	void ResetMask();
219 
220 	/*
221 	 * Gets if a key is pressed.
222 	 * Low level function accessing keys directly bypassing the button mapping.
223 	 *
224 	 * @param key key ID.
225 	 * @return whether the key is being pressed.
226 	 */
227 	bool IsRawKeyPressed(Input::Keys::InputKey key);
228 
229 	/*
230 	 * Gets if a key is triggered.
231 	 * Low level function accessing keys directly bypassing the button mapping.
232 	 *
233 	 * @param key key ID.
234 	 * @return whether the key is being released.
235 	 */
236 	bool IsRawKeyTriggered(Input::Keys::InputKey key);
237 
238 	/*
239 	 * Gets if a key is released.
240 	 * Low level function accessing keys directly bypassing the button mapping.
241 	 *
242 	 * @param key key ID.
243 	 * @return whether the key is being released.
244 	 */
245 	bool IsRawKeyReleased(Input::Keys::InputKey key);
246 
247 	/**
248 	 * Gets all raw keys being pressed.
249 	 *
250 	 * @return a vector with the key IDs.
251 	 */
252 	const Input::KeyStatus& GetAllRawPressed();
253 
254 	/**
255 	 * Gets all raw keys being triggered.
256 	 *
257 	 * @return a vector with the key IDs.
258 	 */
259 	const Input::KeyStatus& GetAllRawTriggered();
260 
261 	/**
262 	 * Gets all raw keys being released.
263 	 *
264 	 * @return a vector with the key IDs.
265 	 */
266 	const Input::KeyStatus& GetAllRawReleased();
267 
268 	/**
269 	 * @return Position of the mouse cursor relative to the screen
270 	 */
271 	Point GetMousePosition();
272 
273 	/**
274 	 * Used to submit additional metadata for input recording
275 	 * @param type type of data sent
276 	 * @param data Sent data
277 	 */
278 	void AddRecordingData(RecordingData type, StringView data);
279 
280 	/** @return If the input is recorded */
281 	bool IsRecording();
282 
283 	/** Buttons press time (in frames). */
284 	extern std::array<int, BUTTON_COUNT> press_time;
285 
286 	/** Buttons trigger state. */
287 	extern std::bitset<BUTTON_COUNT> triggered;
288 
289 	/** Buttons trigger state. */
290 	extern std::bitset<BUTTON_COUNT> repeated;
291 
292 	/** Buttons trigger state. */
293 	extern std::bitset<BUTTON_COUNT> released;
294 
295 	/** Raw keys triggered state. */
296 	extern std::bitset<Input::Keys::KEYS_COUNT> raw_triggered;
297 
298 	/** Raw keys pressed state. */
299 	extern std::bitset<Input::Keys::KEYS_COUNT> raw_pressed;
300 
301 	/** Raw keys released state. */
302 	extern std::bitset<Input::Keys::KEYS_COUNT> raw_released;
303 
304 	/** Horizontal and vertical directions state. */
305 	extern int dir4;
306 
307 	/** All cardinal directions state. */
308 	extern int dir8;
309 
310 	bool IsWaitingInput();
311 	void WaitInput(bool val);
312 }
313 
314 #endif
315