Action.hpp
Go to the documentation of this file.
1 //
3 // Thor C++ Library
4 // Copyright (c) 2011-2015 Jan Haller
5 //
6 // This software is provided 'as-is', without any express or implied
7 // warranty. In no event will the authors be held liable for any damages
8 // arising from the use of this software.
9 //
10 // Permission is granted to anyone to use this software for any purpose,
11 // including commercial applications, and to alter it and redistribute it
12 // freely, subject to the following restrictions:
13 //
14 // 1. The origin of this software must not be misrepresented; you must not
15 // claim that you wrote the original software. If you use this software
16 // in a product, an acknowledgment in the product documentation would be
17 // appreciated but is not required.
18 //
19 // 2. Altered source versions must be plainly marked as such, and must not be
20 // misrepresented as being the original software.
21 //
22 // 3. This notice may not be removed or altered from any source distribution.
23 //
25 
28 
29 #ifndef THOR_ACTION_HPP
30 #define THOR_ACTION_HPP
31 
32 #include <Thor/Input/Detail/ActionOperations.hpp>
33 #include <Thor/Config.hpp>
34 
35 #include <functional>
36 
37 
38 namespace thor
39 {
40 namespace detail
41 {
42 
43  class EventBuffer;
44 
45 } // namespace detail
46 
47 // ---------------------------------------------------------------------------------------------------------------------------
48 
49 
52 
56 class THOR_API Action
57 {
58  // ---------------------------------------------------------------------------------------------------------------------------
59  // Public types
60  public:
61 
65  {
66  Hold,
69  };
70 
71 
72  // ---------------------------------------------------------------------------------------------------------------------------
73  // Public member functions
74  public:
78  explicit Action(sf::Keyboard::Key key, ActionType action = Hold);
79 
83  explicit Action(sf::Mouse::Button mouseButton, ActionType action = Hold);
84 
89  explicit Action(JoystickButton joystickState, ActionType action = Hold);
90 
96  explicit Action(JoystickAxis joystickAxis);
97 
100  explicit Action(sf::Event::EventType eventType);
101 
102 
103  // ---------------------------------------------------------------------------------------------------------------------------
104  // Implementation details
105  public:
106  // Default constructor: Required for ActionMap::operator[] - constructs an uninitialized action
107  Action();
108 
109  // Tests if the event/real time input constellation in the argument is true for this action
110  bool isActive(const detail::EventBuffer& buffer) const;
111 
112  // Test if active and store relevant events
113  bool isActive(const detail::EventBuffer& buffer, detail::ActionResult& out) const;
114 
115 
116  // ---------------------------------------------------------------------------------------------------------------------------
117  // Private member functions
118  private:
119  // Construct from movable pointer to the action's operation
120  explicit Action(detail::ActionNode::CopiedPtr operation);
121 
122 
123  // ---------------------------------------------------------------------------------------------------------------------------
124  // Private variables
125  private:
126  detail::ActionNode::CopiedPtr mOperation;
127 
128 
129  // ---------------------------------------------------------------------------------------------------------------------------
130  // Friends
132  friend Action THOR_API operator|| (const Action& lhs, const Action& rhs);
133  friend Action THOR_API operator&& (const Action& lhs, const Action& rhs);
134  friend Action THOR_API operator! (const Action& action);
135  friend Action THOR_API eventAction(std::function<bool(const sf::Event&)> filter);
136  friend Action THOR_API realtimeAction(std::function<bool()> filter);
138 };
139 
140 
150 Action THOR_API operator|| (const Action& lhs, const Action& rhs);
151 
162 Action THOR_API operator&& (const Action& lhs, const Action& rhs);
163 
174 Action THOR_API operator! (const Action& action);
175 
190 Action THOR_API eventAction(std::function<bool(const sf::Event&)> filter);
191 
206 Action THOR_API realtimeAction(std::function<bool()> filter);
207 
208 
210 
211 } // namespace thor
212 
213 #endif // THOR_ACTION_HPP
Class for dynamic actions that are connected with SFML events.
Definition: Action.hpp:56
Contains information about a joystick number, an axis and its threshold.
Definition: Joystick.hpp:61
Release events that occur only once (e.g. key released).
Definition: Action.hpp:68
Definition: Animator.hpp:42
ActionType
Type for actions.
Definition: Action.hpp:64
Configuration header of the library.
Press events that occur only once (e.g. key pressed).
Definition: Action.hpp:67
Repeated input (e.g. a key that is held down).
Definition: Action.hpp:66
Contains information about a joystick number and button number.
Definition: Joystick.hpp:45