ge211  2021.5.1
A student game engine
ge211_event.cxx
1 #include "ge211_event.hxx"
2 
3 #include <SDL_events.h>
4 #include "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";
79  return "right";
80  }
81 
82  return "<unknown>";
83 }
84 
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 
119 {
120  return os << key_type_name(type);
121 }
122 
123 
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 
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 }
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
std::string
ge211::events::Key::alt
static Key alt()
Constructs the alt (or option) key.
Definition: ge211_event.hxx:168
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::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::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::Mouse_button::left
@ left
The primary mouse button. This is an ordinary click.
ge211::events::Key::Type::alt
@ alt
The alt or option key.