Gamedev Framework (gf) 1.2.0
A C++17 framework for 2D games
Action.h
1/*
2 * Gamedev Framework (gf)
3 * Copyright (C) 2016-2022 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 "GraphicsApi.h"
31#include "Keyboard.h"
32#include "Mouse.h"
33#include "Ref.h"
34
35namespace gf {
36#ifndef DOXYGEN_SHOULD_SKIP_THIS
37inline namespace v1 {
38#endif
39
40 struct Event;
41
53 class GF_GRAPHICS_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
94
100 bool isContinuous() const;
101
111
117 bool isInstantaneous() const;
133
142
151
161
172
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_GRAPHICS_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
A set of actions.
Definition: Action.h:240
void processEvent(const Event &event)
Update all the actions.
const Action & getAction(const std::string &name) const
Get an action thanks to its name.
void addAction(Action &action)
Add an action.
bool hasAction(const std::string &name) const
Check if an action exists.
void reset()
Reset all the actions.
Action & getAction(const std::string &name)
Get an action thanks to its name.
An action that can be triggered by different controls.
Definition: Action.h:53
void processEvent(const Event &event)
Update the state of the action thanks to an event.
void addMouseButtonControl(MouseButton button)
Add a mouse button control.
bool isActive() const
Check if the action is active.
void addControl(Control &control)
Add a user-defined control.
Action & operator=(const Action &)=delete
Deleted copy assignment.
void addCloseControl()
Add a close control.
void addGamepadButtonControl(GamepadId id, GamepadButton button)
Add a gamepad button control.
bool isInstantaneous() const
Check if the action is instantaneous.
const std::string & getName() const
Get the name of the action.
Definition: Action.h:77
void setContinuous()
Set the action continuous.
void addScancodeKeyControl(Scancode code)
Add a key control.
void addKeycodeKeyControl(Keycode code)
Add a key control.
void reset()
Reset the state of the action.
void setInstantaneous()
Set the action instantaneous.
Action(std::string name)
Construct an action with a name.
void addGamepadAxisControl(GamepadId id, GamepadAxis axis, GamepadAxisDirection dir)
Add a gamepad axis control.
Action(const Action &)=delete
Deleted copy constructor.
bool isContinuous() const
Check if the action is continuous.
A physical control.
Definition: Control.h:38
GamepadId
A gamepad identifier.
Definition: Gamepad.h:123
GamepadAxis
The gamepad axis.
Definition: Gamepad.h:71
MouseButton
Mouse buttons.
Definition: Mouse.h:36
GamepadButton
The gamepad buttons.
Definition: Gamepad.h:43
Scancode
Scancodes.
Definition: Keyboard.h:51
GamepadAxisDirection
A gamepad axis direction.
Definition: Gamepad.h:89
Keycode
Keycodes.
Definition: Keyboard.h:280
@ Event
An event is pending on the sockets.
The namespace for gf classes.
Defines a system event and its parameters.
Definition: Event.h:224