ge211
ge211_event.cpp
1 #include "ge211_event.h"
2 
3 #include <SDL_events.h>
4 #include "../3rdparty/utf8-cpp/utf8.h"
5 
6 #include <cctype>
7 
8 namespace ge211 {
9 
10 namespace detail {
11 
12 bool map_button(uint8_t input, Mouse_button& output) noexcept
13 {
14  switch (input) {
15  case SDL_BUTTON_LEFT:
16  output = Mouse_button::left;
17  return true;
18  case SDL_BUTTON_MIDDLE:
19  output = Mouse_button::middle;
20  return true;
21  case SDL_BUTTON_RIGHT:
22  output = Mouse_button::right;
23  return true;
24  default:
25  return false;
26  }
27 }
28 
29 } // end namespace detail
30 
31 namespace events {
32 
33 static Key map_key(const SDL_KeyboardEvent& e) noexcept
34 {
35  if (e.keysym.sym >= 0 && e.keysym.sym < 128) {
36  return Key::code(uint32_t(e.keysym.sym));
37  } else {
38  switch (e.keysym.sym) {
39  case SDLK_KP_ENTER:
40  return Key::code('\r');
41  case SDLK_UP:
42  return Key::up();
43  case SDLK_DOWN:
44  return Key::down();
45  case SDLK_LEFT:
46  return Key::left();
47  case SDLK_RIGHT:
48  return Key::right();
49  case SDLK_LSHIFT:
50  case SDLK_RSHIFT:
51  return Key::shift();
52  case SDLK_LCTRL:
53  case SDLK_RCTRL:
54  return Key::control();
55  case SDLK_LALT:
56  case SDLK_RALT:
57  return Key::alt();
58  case SDLK_LGUI:
59  case SDLK_RGUI:
60  return Key::command();
61  default:
62  return Key();
63  }
64  }
65 }
66 
67 Key::Key(const SDL_KeyboardEvent& e) noexcept
68  : Key{map_key(e)}
69 { }
70 
71 static const char* mouse_button_name(Mouse_button button) noexcept
72 {
73  switch (button) {
74  case Mouse_button::left:
75  return "left";
76  case Mouse_button::middle:
77  return "middle";
78  case Mouse_button::right:
79  return "right";
80  }
81 
82  return "<unknown>";
83 }
84 
85 std::ostream& operator<<(std::ostream& os, Mouse_button button)
86 {
87  return os << mouse_button_name(button);
88 }
89 
90 static const char* key_type_name(Key::Type type) noexcept
91 {
92  switch (type) {
93  case Key::Type::code:
94  return "ascii";
95  case Key::Type::up:
96  return "up";
97  case Key::Type::down:
98  return "down";
99  case Key::Type::left:
100  return "left";
101  case Key::Type::right:
102  return "right";
103  case Key::Type::shift:
104  return "shift";
105  case Key::Type::control:
106  return "control";
107  case Key::Type::alt:
108  return "alt";
109  case Key::Type::command:
110  return "command";
111  case Key::Type::other:
112  return "other";
113  }
114 
115  return "<unknown>";
116 }
117 
118 std::ostream& operator<<(std::ostream& os, Key::Type type)
119 {
120  return os << key_type_name(type);
121 }
122 
123 
124 std::ostream& operator<<(std::ostream& os, Key key)
125 {
126  if (key.type() == Key::Type::code) {
127  if (key.code() < 128 && key.is_textual())
128  return os << "Key::code('" << char(key.code()) << "')";
129  else
130  return os << "Key::code(" << key.code() << ")";
131  } else {
132  return os << "Key::" << key.type() << "()";
133  }
134 }
135 
136 bool Key::is_textual() const noexcept
137 {
138  return type_ == Type::code && !iswcntrl(code_);
139 }
140 
141 std::string Key::as_text() const
142 {
143  if (!is_textual()) return std::string{};
144 
145  char buffer[4];
146  char* end = utf8::append(code_, buffer);
147  return std::string(buffer, end);
148 }
149 
150 } // end namespace events
151 
152 }
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 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.
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 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