ge211  2021.5.1
A student game engine
ge211_event.hxx
1 #pragma once
2 
3 #include "ge211_forward.hxx"
4 #include "ge211_error.hxx"
5 #include "ge211_noexcept.hxx"
6 
7 #include <sstream>
8 #include <string>
9 
10 namespace ge211 {
11 
13 namespace events {
14 
19 enum class Mouse_button
20 {
22  left,
25  middle,
28  right,
29 };
30 
35 
36 } // end namespace events
37 
38 namespace detail {
39 
40 // Attempts to convert an SDL mouse button code to a ge211 Mouse_button.
41 // Returns true on success, or false if the SDL mouse button code does
42 // not correspond to left, middle, or right.
43 bool map_button(uint8_t, Mouse_button&) NOEXCEPT;
44 
45 // Unicode constants.
46 static char32_t const lowest_unicode_surrogate = 0xD800;
47 static char32_t const highest_unicode_surrogate = 0xDFFF;
48 static char32_t const highest_unicode_code_point = 0x10FFFF;
49 
50 // Checks for valid Unicode code points.
51 inline bool is_valid_unicode(char32_t code)
52 {
53  return code < lowest_unicode_surrogate ||
54  (code > highest_unicode_surrogate &&
55  code <= highest_unicode_code_point);
56 }
57 
58 } // end namespace detail
59 
60 namespace events {
61 
127 class Key
128 {
129 public:
132 
134  Key() NOEXCEPT : Key{Type::other} { }
135 
141  static Key code(char32_t c)
142  {
143  if (detail::is_valid_unicode(c))
144  return Key{c};
145 
146  throw Client_logic_error{"Not a valid Unicode code point"};
147  }
148 
150  static Key up() NOEXCEPT { return Key{Type::up}; };
151 
153  static Key down() NOEXCEPT { return Key{Type::down}; };
154 
156  static Key left() NOEXCEPT { return Key{Type::left}; };
157 
159  static Key right() NOEXCEPT { return Key{Type::right}; };
160 
162  static Key shift() NOEXCEPT { return Key{Type::shift}; };
163 
165  static Key control() NOEXCEPT { return Key{Type::control}; };
166 
168  static Key alt() NOEXCEPT { return Key{Type::alt}; };
169 
171  static Key command() NOEXCEPT { return Key{Type::command}; };
172 
175  static Key other() NOEXCEPT { return Key{Type::other}; }
176 
178 
180  enum class Type
181  {
184  code,
186  up,
188  down,
190  left,
192  right,
194  shift,
196  control,
198  alt,
200  command,
202  other,
203  };
204 
206  Type type() const NOEXCEPT { return type_; }
207 
209  char32_t code() const NOEXCEPT { return code_; }
210 
213  bool is_textual() const NOEXCEPT;
214 
223  std::string as_text() const;
224 
225 private:
226  explicit Key(Type type) NOEXCEPT
227  : type_{type},
228  code_{0}
229  { }
230 
231  explicit Key(char32_t c) NOEXCEPT
232  : type_{Type::code},
233  code_{c}
234  { }
235 
236  friend class detail::Engine;
237  explicit Key(SDL_KeyboardEvent const&) NOEXCEPT;
238 
239  Type type_;
240  char32_t code_;
241 };
242 
244 inline bool operator==(Key a, Key b) NOEXCEPT
245 {
246  return a.type() == b.type() && a.code() == b.code();
247 }
248 
250 inline bool operator!=(Key a, Key b) NOEXCEPT
251 {
252  return !(a == b);
253 }
254 
258 
262 
263 } // end namespace events
264 
265 }
ge211::events::Key::as_text
std::string as_text() const
Returns a representation of the key's code as a std::string.
Definition: ge211_event.cxx:141
ge211::events::Key::is_textual
bool is_textual() const
Does the key represent printable text? This is true for some but not all Type::code keys.
Definition: ge211_event.cxx:136
ge211::events::Mouse_button
Mouse_button
A representation of a mouse button.
Definition: ge211_event.hxx:20
ge211::events::Key::control
static Key control()
Constructs the control key.
Definition: ge211_event.hxx:165
ge211::events::Key::command
static Key command()
Constructs the command (or meta) key.
Definition: ge211_event.hxx:171
ge211::events::Key::alt
static Key alt()
Constructs the alt (or option) key.
Definition: ge211_event.hxx:168
ge211::events::Key::other
static Key other()
Constructs an invalid or unknown key.
Definition: ge211_event.hxx:175
ge211::events::Key::Type::up
@ up
The up arrow key.
ge211::events::Key::down
static Key down()
Constructs the down arrow key.
Definition: ge211_event.hxx:153
ge211::events::Key::Type
Type
The possible types of keys.
Definition: ge211_event.hxx:181
ge211::events::Key
Represents a key on the keyboard.
Definition: ge211_event.hxx:128
ge211::events::Key::Type::control
@ control
The control key.
ge211
The game engine namespace.
Definition: ge211.hxx:4
ge211::events::Key::Type::command
@ command
The command or meta key.
std::ostream
ge211::exceptions::Client_logic_error
An exception that indicates that a logic error was performed by the client.
Definition: ge211_error.hxx:48
ge211::events::Key::Type::down
@ down
The down arrow key.
ge211::events::Key::Type::left
@ left
The left arrow key.
ge211::events::Key::up
static Key up()
Constructs the up arrow key.
Definition: ge211_event.hxx:150
ge211::events::Key::right
static Key right()
Constructs the right arrow key.
Definition: ge211_event.hxx:159
ge211::events::Key::Type::other
@ other
Any other, unknown or invalid key.
ge211::events::Key::Type::shift
@ shift
The shift key.
ge211::events::Key::Type::code
@ code
Indicates a key with an Unicode value, which can be gotten with Key::code() const.
ge211::events::operator==
bool operator==(Key a, Key b)
Equality for keys.
Definition: ge211_event.hxx:244
ge211::events::Key::code
static Key code(char32_t c)
Constructs a key with the given Unicode code point code.
Definition: ge211_event.hxx:141
ge211::events::Key::code
char32_t code() const
The Unicode code point of the key, if it has one.
Definition: ge211_event.hxx:209
ge211::events::operator<<
std::ostream & operator<<(std::ostream &, Mouse_button)
Prints a Mouse_button on a std::ostream.
Definition: ge211_event.cxx:85
ge211::events::Key::shift
static Key shift()
Constructs the shift key.
Definition: ge211_event.hxx:162
ge211::events::Key::left
static Key left()
Constructs the left arrow key.
Definition: ge211_event.hxx:156
ge211::events::Key::Type::right
@ right
The right arrow key.
ge211::events::Key::type
Type type() const
The type of the key.
Definition: ge211_event.hxx:206
ge211::events::Mouse_button::right
@ right
The secondary mouse button.
ge211::events::Key::Key
Key()
Constructs the empty key, with type Key::Type::other.
Definition: ge211_event.hxx:134
ge211::events::operator!=
bool operator!=(Key a, Key b)
Disequality for keys.
Definition: ge211_event.hxx:250
ge211::events::Mouse_button::left
@ left
The primary mouse button. This is an ordinary click.
ge211::events::Key::Type::alt
@ alt
The alt or option key.