Gamedev Framework (gf)  0.6.0
A C++11 framework for 2D games
Action.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2017 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 #ifndef GF_ACTION_H
22 #define GF_ACTION_H
23 
24 #include <memory>
25 #include <string>
26 #include <vector>
27 
28 #include "Control.h"
29 #include "Gamepad.h"
30 #include "Keyboard.h"
31 #include "Mouse.h"
32 #include "Portability.h"
33 
34 namespace gf {
35 #ifndef DOXYGEN_SHOULD_SKIP_THIS
36 inline namespace v1 {
37 #endif
38 
39  struct Event;
40 
41  /**
42  * @ingroup window
43  * @brief An action that can be triggered by different controls.
44  *
45  */
46  class GF_API Action {
47  public:
48  /**
49  * @brief Construct an action with a name.
50  *
51  * @param name the name of the action.
52  */
53  explicit Action(std::string name);
54 
55  /**
56  * @brief Deleted copy constructor
57  */
58  Action(const Action&) = delete;
59 
60  /**
61  * @brief Deleted copy assignment
62  */
63  Action& operator=(const Action&) = delete;
64 
65  /**
66  * @brief Get the name of the action.
67  *
68  * @return the name of the action.
69  */
70  const std::string& getName() const {
71  return m_name;
72  }
73 
74  /**
75  * @name Type of the action
76  * @{
77  */
78  /**
79  * @brief Set the action continuous.
80  *
81  * A continuous action is an action that is active as long as the user do
82  * not desactivate it. A reset() call does not desactivate the action.
83  *
84  * @sa reset(), setInstantaneous()
85  */
86  void setContinuous();
87 
88  /**
89  * @brief Check if the action is continuous.
90  *
91  * @return true if the action is continuous.
92  */
93  bool isContinuous() const;
94 
95  /**
96  * @brief Set the action instantaneous.
97  *
98  * An instantaneous action is an action that is active until the next
99  * reset() call.
100  *
101  * @sa reset(), setContinuous()
102  */
103  void setInstantaneous();
104 
105  /**
106  * @brief Check if the action is instantaneous.
107  *
108  * @return true if the action is instantaneous.
109  */
110  bool isInstantaneous() const;
111  /** @} */
112 
113  /**
114  * @name Controls for the action
115  * @{
116  */
117 
118  /**
119  * @brief Add a key control.
120  *
121  * @param code the keycode of the key
122  *
123  * @sa KeycodeKeyControl
124  */
125  void addKeycodeKeyControl(Keycode code);
126 
127  /**
128  * @brief Add a key control.
129  *
130  * @param code the scancode of the key
131  *
132  * @sa ScancodeKeyControl
133  */
134  void addScancodeKeyControl(Scancode code);
135 
136  /**
137  * @brief Add a mouse button control.
138  *
139  * @param button the button of the mouse.
140  *
141  * @sa MouseButtonControl
142  */
143  void addMouseButtonControl(MouseButton button);
144 
145  /**
146  * @brief Add a gamepad button control.
147  *
148  * @param id the id of the gamepad.
149  * @param button the button of the gamepad
150  *
151  * @sa GamepadButtonControl
152  */
154 
155  /**
156  * @brief Add a gamepad axis control.
157  *
158  * @param id the id of the gamepad.
159  * @param axis the axis of the gamepad.
160  * @param dir the direction of the axis of the gamepad.
161  *
162  * @sa GamepadAxisControl
163  */
165 
166  /**
167  * @brief Add a close control.
168  *
169  * @sa CloseControl
170  */
171  void addCloseControl();
172 
173 
174  /**
175  * @brief Add a user-defined control
176  *
177  * @param control The control
178  */
179  void addControl(Control& control);
180  /** @} */
181 
182  /**
183  * @name State of the action
184  * @{
185  */
186  /**
187  * @brief Update the state of the action thanks to an event.
188  *
189  * @param event the event to update the action.
190  *
191  * @sa Control::processEvent()
192  */
193  void processEvent(const Event& event);
194 
195  /**
196  * @brief Check if the action is active.
197  *
198  * An action is active if at least one of its control is active.
199  *
200  * @return true if the action is active.
201  *
202  * @sa Control::isActive()
203  */
204  bool isActive();
205 
206  /**
207  * @brief Reset the state of the action.
208  *
209  * This function depends of the type of the action.
210  *
211  * @sa setContinuous(), setInstantaneous(), Control::reset()
212  */
213  void reset();
214  /** @} */
215 
216  private:
217  enum class Type {
218  Instantaneous,
219  Continuous,
220  };
221 
222  std::string m_name;
223  Type m_type;
224  std::vector<std::unique_ptr<Control>> m_ownedControls;
225  std::vector<Control *> m_controls;
226  };
227 
228  /**
229  * @ingroup window
230  * @brief A set of actions.
231  *
232  */
234  public:
235  /**
236  * @brief Add an action.
237  *
238  * @param action The action to add to the set.
239  */
240  void addAction(Action& action);
241 
242  /**
243  * @brief Check if an action exists
244  *
245  * @param name The name of the action
246  * @returns True if there is an action with that name
247  */
248  bool hasAction(const std::string& name) const;
249 
250  /**
251  * @brief Get an action thanks to its name.
252  *
253  * @param name The name of the action
254  * @returns The action with that name
255  * @throw std::runtime_error If the action is not found
256  */
257  Action& getAction(const std::string& name);
258 
259  /**
260  * @brief Get an action thanks to its name.
261  *
262  * @param name The name of the action
263  * @returns The action with that name
264  * @throw std::runtime_error If the action is not found
265  */
266  const Action& getAction(const std::string& name) const;
267 
268  /**
269  * @brief Update all the actions.
270  *
271  * @param event the event to update the actions.
272  *
273  * @sa Action;:processEvent()
274  */
275  void processEvent(const Event& event);
276 
277  /**
278  * @brief Reset all the actions.
279  *
280  * @sa Action::reset()
281  */
282  void reset();
283 
284  private:
285  std::vector<Action*> m_actions;
286  };
287 
288 #ifndef DOXYGEN_SHOULD_SKIP_THIS
289 }
290 #endif
291 }
292 
293 #endif // GF_ACTION_H
void addAction(Action &action)
Add an action.
GamepadAxisDirection
A gamepad axis direction.
Definition: Gamepad.h:88
GamepadButton
The gamepad buttons.
Definition: Gamepad.h:42
A set of actions.
Definition: Action.h:233
void reset()
Reset the state of the action.
bool hasAction(const std::string &name) const
Check if an action exists.
Action & operator=(const Action &)=delete
Deleted copy assignment.
void processEvent(const Event &event)
Update the state of the action thanks to an event.
void addGamepadButtonControl(GamepadId id, GamepadButton button)
Add a gamepad button control.
void reset()
Reset all the actions.
Scancode
Scancodes.
Definition: Keyboard.h:59
bool isActive()
Check if the action is active.
A physical control.
Definition: Control.h:38
const std::string & getName() const
Get the name of the action.
Definition: Action.h:70
GamepadAxis
The gamepad axis.
Definition: Gamepad.h:70
The namespace for gf classes.
Definition: Action.h:34
void addControl(Control &control)
Add a user-defined control.
void processEvent(const Event &event)
Update all the actions.
void addGamepadAxisControl(GamepadId id, GamepadAxis axis, GamepadAxisDirection dir)
Add a gamepad axis control.
GamepadId
A gamepad identifier.
Definition: Gamepad.h:120
void addKeycodeKeyControl(Keycode code)
Add a key control.
Action(std::string name)
Construct an action with a name.
const Action & getAction(const std::string &name) const
Get an action thanks to its name.
An action that can be triggered by different controls.
Definition: Action.h:46
void addCloseControl()
Add a close control.
bool isContinuous() const
Check if the action is continuous.
void setInstantaneous()
Set the action instantaneous.
bool isInstantaneous() const
Check if the action is instantaneous.
MouseButton
Mouse buttons.
Definition: Mouse.h:36
Defines a system event and its parameters.
Definition: Event.h:118
Action & getAction(const std::string &name)
Get an action thanks to its name.
#define GF_API
Definition: Portability.h:35
Action(const Action &)=delete
Deleted copy constructor.
void addMouseButtonControl(MouseButton button)
Add a mouse button control.
Keycode
Keycodes.
Definition: Keyboard.h:288
void setContinuous()
Set the action continuous.
void addScancodeKeyControl(Scancode code)
Add a key control.