ge211
ge211_event.h
1 #pragma once
2 
3 #include "ge211_forward.h"
4 #include "ge211_error.h"
5 
6 #include <sstream>
7 #include <string>
8 
9 namespace ge211 {
10 
12 namespace events {
13 
18 enum class Mouse_button
19 {
20  left, middle, right,
21 };
22 
26 std::ostream& operator<<(std::ostream&, Mouse_button);
27 
28 } // end namespace events
29 
30 namespace detail {
31 
32 // Attempts to convert an SDL mouse button code to a ge211 Mouse_button.
33 // Returns true on success, or false if the SDL mouse button code does
34 // not correspond to left, middle, or right.
35 bool map_button(uint8_t, Mouse_button&) noexcept;
36 
37 // Unicode constants.
38 static char32_t const lowest_unicode_surrogate = 0xD800;
39 static char32_t const highest_unicode_surrogate = 0xDFFF;
40 static char32_t const highest_unicode_code_point = 0x10FFFF;
41 
42 // Checks for valid Unicode code points.
43 inline bool is_valid_unicode(char32_t code)
44 {
45  return code < lowest_unicode_surrogate ||
46  (code > highest_unicode_surrogate &&
47  code <= highest_unicode_code_point);
48 }
49 
50 } // end namespace detail
51 
52 namespace events {
53 
119 class Key
120 {
121 public:
124 
126  Key() noexcept : Key{Type::other} { }
127 
133  static Key code(char32_t c)
134  {
135  if (detail::is_valid_unicode(c))
136  return Key{c};
137 
138  throw Client_logic_error{"Not a valid Unicode code point"};
139  }
140 
142  static Key up() noexcept { return Key{Type::up}; };
143 
145  static Key down() noexcept { return Key{Type::down}; };
146 
148  static Key left() noexcept { return Key{Type::left}; };
149 
151  static Key right() noexcept { return Key{Type::right}; };
152 
154  static Key shift() noexcept { return Key{Type::shift}; };
155 
157  static Key control() noexcept { return Key{Type::control}; };
158 
160  static Key alt() noexcept { return Key{Type::alt}; };
161 
163  static Key command() noexcept { return Key{Type::command}; };
164 
167  static Key other() noexcept { return Key{Type::other}; }
168 
170 
172  enum class Type
173  {
176  code,
178  up,
180  down,
182  left,
184  right,
186  shift,
188  control,
190  alt,
192  command,
194  other,
195  };
196 
198  Type type() const noexcept { return type_; }
199 
201  char32_t code() const noexcept { return code_; }
202 
205  bool is_textual() const noexcept;
206 
215  std::string as_text() const;
216 
217 private:
218  explicit Key(Type type) noexcept
219  : type_{type},
220  code_{0}
221  { }
222 
223  explicit Key(char32_t c) noexcept
224  : type_{Type::code},
225  code_{c}
226  { }
227 
228  friend detail::Engine;
229  explicit Key(SDL_KeyboardEvent const&) noexcept;
230 
231  Type type_;
232  char32_t code_;
233 };
234 
236 inline bool operator==(Key a, Key b) noexcept
237 {
238  return a.type() == b.type() && a.code() == b.code();
239 }
240 
242 inline bool operator!=(Key a, Key b) noexcept
243 {
244  return !(a == b);
245 }
246 
249 std::ostream& operator<<(std::ostream&, Key::Type);
250 
253 std::ostream& operator<<(std::ostream&, Key);
254 
255 } // end namespace events
256 
257 }
Mouse_button
A representation of a mouse button.
Definition: ge211_event.h:18
Represents a key on the keyboard.
Definition: ge211_event.h:119
static Key shift() noexcept
Constructs the shift key.
Definition: ge211_event.h:154
std::string as_text() const
Returns a representation of the key&#39;s code as a std::string.
Type
The possible types of keys.
Definition: ge211_event.h:172
static Key command() noexcept
Constructs the command (or meta) key.
Definition: ge211_event.h:163
The game engine namespace.
Definition: ge211.h:17
The left arrow key.
bool operator!=(Key a, Key b) noexcept
Disequality for keys.
Definition: ge211_event.h:242
bool is_textual() const noexcept
Does the key represent printable text? This is true for some but not all Type::code keys...
Key() noexcept
Constructs the empty key, with type Key::Type::other.
Definition: ge211_event.h:126
static Key left() noexcept
Constructs the left arrow key.
Definition: ge211_event.h:148
The down arrow key.
bool operator==(Key a, Key b) noexcept
Equality for keys.
Definition: ge211_event.h:236
The command or meta key.
Any other, unknown or invalid key.
std::ostream & operator<<(std::ostream &os, Mouse_button button)
Prints a Mouse_button on a std::ostream.
Definition: ge211_event.cpp:85
char32_t code() const noexcept
The Unicode code point of the key, if it has one.
Definition: ge211_event.h:201
Indicates a key with an Unicode value, which can be gotten with Key::code() const.
The right arrow key.
static Key control() noexcept
Constructs the control key.
Definition: ge211_event.h:157
static Key code(char32_t c)
Constructs a key with the given Unicode code point code.
Definition: ge211_event.h:133
static Key up() noexcept
Constructs the up arrow key.
Definition: ge211_event.h:142
static Key down() noexcept
Constructs the down arrow key.
Definition: ge211_event.h:145
static Key other() noexcept
Constructs an invalid or unknown key.
Definition: ge211_event.h:167
An exception that indicates that a logic error was performed by the client.
Definition: ge211_error.h:46
static Key alt() noexcept
Constructs the alt (or option) key.
Definition: ge211_event.h:160
The alt or option key.
static Key right() noexcept
Constructs the right arrow key.
Definition: ge211_event.h:151
Type type() const noexcept
The type of the key.
Definition: ge211_event.h:198