HoverRace  2.0
Action.h
Go to the documentation of this file.
1 
2 // Action.h
3 //
4 // Copyright (c) 2013-2016 Michael Imamura.
5 //
6 // Licensed under GrokkSoft HoverRace SourceCode License v1.0(the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // A copy of the license should have been attached to the package from which
10 // you have taken this file. If you can not find the license you can not use
11 // this file.
12 //
13 //
14 // The author makes no representations about the suitability of
15 // this software for any purpose. It is provided "as is" "AS IS",
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
17 // implied.
18 //
19 // See the License for the specific language governing permissions
20 // and limitations under the License.
21 
22 #pragma once
23 
24 #include <SDL2/SDL_keycode.h>
25 #include <SDL2/SDL_mouse.h>
26 
27 #include "../Vec.h"
28 #include "../Exception.h"
29 
30 #include "ControlAction.h"
31 
32 #if defined(_WIN32) && defined(HR_ENGINE_SHARED)
33 # ifdef MR_ENGINE
34 # define MR_DllDeclare __declspec( dllexport )
35 # else
36 # define MR_DllDeclare __declspec( dllimport )
37 # endif
38 #else
39 # define MR_DllDeclare
40 #endif
41 
42 namespace HoverRace {
43 namespace Control {
44 
45 namespace TextControl {
46 
48 enum key_t {
49  BACKSPACE = SDLK_BACKSPACE,
50  ENTER = SDLK_RETURN
51 };
52 
53 } // namespace TextControl
54 
55 namespace Mouse {
56 
58 enum button_t {
59  LEFT = SDL_BUTTON_LEFT,
60  MIDDLE = SDL_BUTTON_MIDDLE,
61  RIGHT = SDL_BUTTON_RIGHT,
62  B4 = SDL_BUTTON_X1,
63  B5 = SDL_BUTTON_X2
64 };
65 
67 struct Click
68 {
69  constexpr Click(double x, double y, button_t btn) noexcept :
70  pos(x, y), btn(btn) { }
71 
74 };
75 
77 struct Scroll
78 {
79  constexpr Scroll(double x, double y, double dx, double dy) noexcept :
80  pos(x, y), motion(dx, dy) { }
81 
84 };
85 
86 } // namespace Mouse
87 
93 {
94  using result_type = bool;
95 
96  template<typename InputIterator>
97  bool operator()(InputIterator first, InputIterator last) const
98  {
99  while (first != last) {
100  if (*first) return true;
101  ++first;
102  }
103  return false;
104  }
105 };
106 
108 using voidSignal_t = boost::signals2::signal<void()>;
109 
111 using valueSignal_t = boost::signals2::signal<void(int)>;
112 
114 using stringSignal_t = boost::signals2::signal<void(const std::string&)>;
115 
117 using textControlSignal_t = boost::signals2::signal<void(TextControl::key_t)>;
118 
120 using vec2Signal_t = boost::signals2::signal<void(const Vec2&)>;
121 
123 using mouseClickSignal_t =
124  boost::signals2::signal<bool(const Mouse::Click&), CancelCombiner>;
125 
127 using mouseScrollSignal_t =
128  boost::signals2::signal<bool(const Mouse::Scroll&), CancelCombiner>;
129 
130 template<class T, class Val>
131 inline void PerformAction(const T&, Val)
132 {
133  std::ostringstream oss;
134  oss << "PerformAction<" << typeid(T).name() << ", " <<
135  typeid(Val).name() << '>';
136  throw UnimplementedExn(oss.str());
137 }
138 
139 template<>
141  int value)
142 {
143  // voidSignals are only triggered if the value is > 0.
144  // For keys and buttons, that means key-down / button-down.
145  if (value > 0) {
146  signal();
147  }
148 }
149 
150 template<>
152  int value)
153 {
154  signal(value);
155 }
156 
157 template<>
158 inline void PerformAction<stringSignal_t, const std::string &>(
159  const stringSignal_t &signal, const std::string &s)
160 {
161  signal(s);
162 }
163 
164 template<>
165 inline void PerformAction<textControlSignal_t, TextControl::key_t>(
166  const textControlSignal_t &signal, TextControl::key_t key)
167 {
168  signal(key);
169 }
170 
171 template<>
173  const vec2Signal_t &signal, const Vec2 &vec)
174 {
175  signal(vec);
176 }
177 
178 template<>
179 inline void PerformAction<mouseClickSignal_t, const Mouse::Click&>(
180  const mouseClickSignal_t &signal, const Mouse::Click &vec)
181 {
182  signal(vec);
183 }
184 
185 template<>
186 inline void PerformAction<mouseScrollSignal_t, const Mouse::Scroll&>(
187  const mouseScrollSignal_t &signal, const Mouse::Scroll &scroll)
188 {
189  signal(scroll);
190 }
191 
203 template<class T, class Val = int>
204 class Action : public ControlAction<Val>
205 {
207 
208 public:
209  Action() : SUPER(), primaryTrigger(0) { }
210  Action(const std::string &name, int listOrder) :
211  SUPER(name, listOrder), primaryTrigger(0) { }
212  virtual ~Action() { }
213 
214  virtual void operator()(Val value) {
215  PerformAction<T, Val>(signal, value);
216  }
217 
223  template<class U>
224  boost::signals2::connection Connect(U &&del) {
225  return signal.connect(std::forward<U>(del));
226  }
227 
234  int GetPrimaryTrigger() const { return primaryTrigger; }
235 
242  void SetPrimaryTrigger(int hash) { primaryTrigger = hash; }
243 
244  T &GetSignal() { return signal; }
245 
246 private:
249 };
250 
251 } // namespace Control
252 } // namespace HoverRace
253 
254 #undef MR_DllDeclare
T & GetSignal()
Definition: Action.h:244
virtual ~Action()
Definition: Action.h:212
boost::signals2::signal< void(const std::string &)> stringSignal_t
Signals which have a single string payload.
Definition: Action.h:114
Definition: Action.h:60
void PerformAction(const T &, Val)
Definition: Action.h:131
Mouse click events.
Definition: Action.h:67
void PerformAction< voidSignal_t, int >(const voidSignal_t &signal, int value)
Definition: Action.h:140
Definition: Vec.h:38
boost::signals2::signal< void(TextControl::key_t)> textControlSignal_t
Signals for text input control.
Definition: Action.h:117
button_t btn
Definition: Action.h:73
Abstract base class which allows us a simple reference to arbitrary functors of type ControlActionImp...
Definition: ControlAction.h:50
void PerformAction< valueSignal_t, int >(const valueSignal_t &signal, int value)
Definition: Action.h:151
constexpr Scroll(double x, double y, double dx, double dy) noexcept
Definition: Action.h:79
Action(const std::string &name, int listOrder)
Definition: Action.h:210
void PerformAction< vec2Signal_t, const Vec2 & >(const vec2Signal_t &signal, const Vec2 &vec)
Definition: Action.h:172
T signal
Definition: Action.h:248
Vec2 pos
Definition: Action.h:72
boost::signals2::signal< void(const Vec2 &)> vec2Signal_t
Signals which have a Vec2 payload.
Definition: Action.h:120
Definition: Action.h:63
boost::signals2::signal< void(int)> valueSignal_t
Signals which have a single (action-dependent) payload.
Definition: Action.h:111
Mouse scroll events.
Definition: Action.h:77
Combiner that allows slots to cancel the remaining slots by returning true.
Definition: Action.h:92
Definition: Action.h:62
Vec2 motion
Definition: Action.h:83
bool operator()(InputIterator first, InputIterator last) const
Definition: Action.h:97
bool result_type
Definition: Action.h:94
void SetPrimaryTrigger(int hash)
Set the hash of the key or button assigned to this action.
Definition: Action.h:242
Vec2 pos
Definition: Action.h:82
virtual void operator()(Val value)
Definition: Action.h:214
Exception indicating an unimplemented bit of code has been hit.
Definition: Exception.h:65
Definition: Action.h:59
Definition: Announcement.h:24
int primaryTrigger
Definition: Action.h:247
boost::signals2::signal< void()> voidSignal_t
Signals which are self-contained (no payload).
Definition: Action.h:108
key_t
Keycodes used for text input control.
Definition: Action.h:48
boost::signals2::connection Connect(U &&del)
Connect a slot to the signal.
Definition: Action.h:224
Actions are effectively signals that can be triggered by both the InputEventController and other thin...
Definition: Action.h:204
boost::signals2::signal< bool(const Mouse::Click &), CancelCombiner > mouseClickSignal_t
Signals for mouse clicks.
Definition: Action.h:124
Definition: Action.h:61
button_t
Mouse buttons.
Definition: Action.h:58
boost::signals2::signal< bool(const Mouse::Scroll &), CancelCombiner > mouseScrollSignal_t
Signals for mouse scrolling.
Definition: Action.h:128
constexpr Click(double x, double y, button_t btn) noexcept
Definition: Action.h:69
int GetPrimaryTrigger() const
Retrieve the hash of the key or button assigned to this action by the InputEventController.
Definition: Action.h:234
Action()
Definition: Action.h:209