Gamedev Framework (gf)  0.11.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 #include "Ref.h"
34 
35 namespace gf {
36 #ifndef DOXYGEN_SHOULD_SKIP_THIS
37 inline namespace v1 {
38 #endif
39 
40  struct Event;
41 
53  class GF_API Action {
54  public:
60  explicit Action(std::string name);
61 
65  Action(const Action&) = delete;
66 
70  Action& operator=(const Action&) = delete;
71 
77  const std::string& getName() const {
78  return m_name;
79  }
80 
93  void setContinuous();
94 
100  bool isContinuous() const;
101 
110  void setInstantaneous();
111 
117  bool isInstantaneous() const;
132  void addKeycodeKeyControl(Keycode code);
133 
141  void addScancodeKeyControl(Scancode code);
142 
150  void addMouseButtonControl(MouseButton button);
151 
160  void addGamepadButtonControl(GamepadId id, GamepadButton button);
161 
171  void addGamepadAxisControl(GamepadId id, GamepadAxis axis, GamepadAxisDirection dir);
172 
178  void addCloseControl();
179 
180 
186  void addControl(Control& control);
200  void processEvent(const Event& event);
201 
211  bool isActive() const;
212 
220  void reset();
223  private:
224  enum class Type {
225  Instantaneous,
226  Continuous,
227  };
228 
229  std::string m_name;
230  Type m_type;
231  std::vector<std::unique_ptr<Control>> m_ownedControls;
232  std::vector<Ref<Control>> m_controls;
233  };
234 
240  class GF_API ActionContainer {
241  public:
247  void addAction(Action& action);
248 
255  bool hasAction(const std::string& name) const;
256 
264  Action& getAction(const std::string& name);
265 
273  const Action& getAction(const std::string& name) const;
274 
282  void processEvent(const Event& event);
283 
289  void reset();
290 
291  private:
292  std::vector<Ref<Action>> m_actions;
293  };
294 
295 #ifndef DOXYGEN_SHOULD_SKIP_THIS
296 }
297 #endif
298 }
299 
300 #endif // GF_ACTION_H
GamepadAxisDirection
A gamepad axis direction.
Definition: Gamepad.h:89
GamepadButton
The gamepad buttons.
Definition: Gamepad.h:43
A set of actions.
Definition: Action.h:240
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:77
GamepadAxis
The gamepad axis.
Definition: Gamepad.h:71
The namespace for gf classes.
Definition: Action.h:35
GamepadId
A gamepad identifier.
Definition: Gamepad.h:121
An action that can be triggered by different controls.
Definition: Action.h:53
MouseButton
Mouse buttons.
Definition: Mouse.h:36
Defines a system event and its parameters.
Definition: Event.h:118
Keycode
Keycodes.
Definition: Keyboard.h:288