Gamedev Framework (gf)  0.5.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 
46  class GF_API Action {
47  public:
53  explicit Action(std::string name);
54 
58  Action(const Action&) = delete;
59 
63  Action& operator=(const Action&) = delete;
64 
70  const std::string& getName() const {
71  return m_name;
72  }
73 
86  void setContinuous();
87 
93  bool isContinuous() const;
94 
103  void setInstantaneous();
104 
110  bool isInstantaneous() const;
125  void addKeycodeKeyControl(Keycode code);
126 
134  void addScancodeKeyControl(Scancode code);
135 
143  void addMouseButtonControl(MouseButton button);
144 
153  void addGamepadButtonControl(GamepadId id, GamepadButton button);
154 
164  void addGamepadAxisControl(GamepadId id, GamepadAxis axis, GamepadAxisDirection dir);
165 
171  void addCloseControl();
172 
173 
179  void addControl(Control& control);
193  void processEvent(const Event& event);
194 
204  bool isActive();
205 
213  void reset();
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 
233  class GF_API ActionContainer {
234  public:
240  void addAction(Action& action);
241 
248  bool hasAction(const std::string& name) const;
249 
257  Action& getAction(const std::string& name);
258 
266  const Action& getAction(const std::string& name) const;
267 
275  void processEvent(const Event& event);
276 
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
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
Scancode
Scancodes.
Definition: Keyboard.h:70
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
GamepadId
A gamepad identifier.
Definition: Gamepad.h:120
An action that can be triggered by different controls.
Definition: Action.h:46
MouseButton
Mouse buttons.
Definition: Mouse.h:36
Defines a system event and its parameters.
Definition: Event.h:118
Keycode
Keycodes.
Definition: Keyboard.h:299