Gamedev Framework (gf)  0.7.0
A C++14 framework for 2D games
Action.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2018 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 
52  class GF_API Action {
53  public:
59  explicit Action(std::string name);
60 
64  Action(const Action&) = delete;
65 
69  Action& operator=(const Action&) = delete;
70 
76  const std::string& getName() const {
77  return m_name;
78  }
79 
92  void setContinuous();
93 
99  bool isContinuous() const;
100 
109  void setInstantaneous();
110 
116  bool isInstantaneous() const;
131  void addKeycodeKeyControl(Keycode code);
132 
140  void addScancodeKeyControl(Scancode code);
141 
149  void addMouseButtonControl(MouseButton button);
150 
159  void addGamepadButtonControl(GamepadId id, GamepadButton button);
160 
170  void addGamepadAxisControl(GamepadId id, GamepadAxis axis, GamepadAxisDirection dir);
171 
177  void addCloseControl();
178 
179 
185  void addControl(Control& control);
199  void processEvent(const Event& event);
200 
210  bool isActive();
211 
219  void reset();
222  private:
223  enum class Type {
224  Instantaneous,
225  Continuous,
226  };
227 
228  std::string m_name;
229  Type m_type;
230  std::vector<std::unique_ptr<Control>> m_ownedControls;
231  std::vector<Control *> m_controls;
232  };
233 
239  class GF_API ActionContainer {
240  public:
246  void addAction(Action& action);
247 
254  bool hasAction(const std::string& name) const;
255 
263  Action& getAction(const std::string& name);
264 
272  const Action& getAction(const std::string& name) const;
273 
281  void processEvent(const Event& event);
282 
288  void reset();
289 
290  private:
291  std::vector<Action*> m_actions;
292  };
293 
294 #ifndef DOXYGEN_SHOULD_SKIP_THIS
295 }
296 #endif
297 }
298 
299 #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:239
Scancode
Scancodes.
Definition: Keyboard.h:59
A physical control.
Definition: Control.h:38
const std::string & getName() const
Get the name of the action.
Definition: Action.h:76
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:52
MouseButton
Mouse buttons.
Definition: Mouse.h:36
Defines a system event and its parameters.
Definition: Event.h:118
Keycode
Keycodes.
Definition: Keyboard.h:288