Gamedev Framework (gf)  0.3.0
A C++11 framework for 2D games
Action.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016 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  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  * @name State of the action
176  * @{
177  */
178  /**
179  * @brief Update the state of the action thanks to an event.
180  *
181  * @param event the event to update the action.
182  *
183  * @sa Control::processEvent()
184  */
185  void processEvent(const Event& event);
186 
187  /**
188  * @brief Check if the action is active.
189  *
190  * An action is active if at least one of its control is active.
191  *
192  * @return true if the action is active.
193  *
194  * @sa Control::isActive()
195  */
196  bool isActive();
197 
198  /**
199  * @brief Reset the state of the action.
200  *
201  * This function depends of the type of the action.
202  *
203  * @sa setContinuous(), setInstantaneous(), Control::reset()
204  */
205  void reset();
206  /** @} */
207 
208  private:
209  enum class Type {
211  CONTINUOUS,
212  };
213 
214  std::string m_name;
215  Type m_type;
216  std::vector<std::unique_ptr<Control>> m_controls;
217  };
218 
219  /**
220  * @ingroup window
221  * @brief A set of actions.
222  *
223  */
225  public:
226  /**
227  * @brief Add an action.
228  *
229  * @param action the action to add to the set.
230  */
231  void addAction(Action& action);
232 
233  /**
234  * @brief Update all the actions.
235  *
236  * @param event the event to update the actions.
237  *
238  * @sa Action;:processEvent()
239  */
240  void processEvent(const Event& event);
241 
242  /**
243  * @brief Reset all the actions.
244  *
245  * @sa Action::reset()
246  */
247  void reset();
248 
249  private:
250  std::vector<Action*> m_actions;
251  };
252 
253 #ifndef DOXYGEN_SHOULD_SKIP_THIS
254 }
255 #endif
256 }
257 
258 #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
bool isInstantaneous() const
Check if the action is instantaneous.
A set of actions.
Definition: Action.h:224
void reset()
Reset the state of the action.
Action & operator=(const Action &)=delete
Deleted copy assignment.
const std::string & getName() const
Get the name of the action.
Definition: Action.h:70
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:66
bool isActive()
Check if the action is active.
bool isContinuous() const
Check if the action is continuous.
A physical control.
Definition: Control.h:38
GamepadAxis
The gamepad axis.
Definition: Gamepad.h:70
Definition: Action.h:34
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.
An action that can be triggered by different controls.
Definition: Action.h:46
void addCloseControl()
Add a close control.
void setInstantaneous()
Set the action instantaneous.
MouseButton
Mouse buttons.
Definition: Mouse.h:36
Defines a system event and its parameters.
Definition: Event.h:115
#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:293
void setContinuous()
Set the action continuous.
void addScancodeKeyControl(Scancode code)
Add a key control.