Skip to content

Commit cd29065

Browse files
Finalized event, only the "which" stuff is missing.
1 parent 61cb816 commit cd29065

File tree

6 files changed

+628
-22
lines changed

6 files changed

+628
-22
lines changed

keyboard_event.cc

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
namespace SDL {
55
namespace events {
66
KeyboardEvent::KeyboardEvent()
7-
: KeyboardRelatedEvent(), repeat(this->underlying_event.key.repeat),
8-
key_symbol(this->underlying_event.key.keysym) {
7+
: KeyboardRelatedEvent(), repeat(this->underlying_event.key.repeat) {
98
this->underlying_event.key.type = SDL_KEYDOWN;
109
this->underlying_event.key.timestamp = 0;
1110
this->underlying_event.key.windowID = 0;
@@ -14,26 +13,24 @@ namespace SDL {
1413
}
1514

1615
KeyboardEvent::KeyboardEvent(const SDL_Event& ev)
17-
: KeyboardRelatedEvent(ev), repeat(this->underlying_event.key.repeat),
18-
key_symbol(this->underlying_event.key.keysym) {
16+
: KeyboardRelatedEvent(ev), repeat(this->underlying_event.key.repeat) {
1917
}
2018

2119
KeyboardEvent::KeyboardEvent(SDL_Event&& ev)
22-
: KeyboardRelatedEvent(ev), repeat(this->underlying_event.key.repeat),
23-
key_symbol(this->underlying_event.key.keysym) {
20+
: KeyboardRelatedEvent(ev), repeat(this->underlying_event.key.repeat) {
2421
}
2522

2623
KeyboardEvent::KeyboardEvent(
2724
const unsigned char repeat,
2825
const bool pressed,
29-
const SDL_Keysym key
26+
const KeyCombination key
3027
) : KeyboardEvent() {
3128
this->repeat = repeat;
3229
this->pressed(pressed);
33-
this->key_symbol = key;
30+
this->key(key);
3431
}
3532

36-
KeyboardEvent::KeyboardEvent(const bool pressed, const SDL_Keysym key)
33+
KeyboardEvent::KeyboardEvent(const bool pressed, const KeyCombination key)
3734
: KeyboardEvent(0, pressed, key) {
3835
}
3936

@@ -45,18 +42,22 @@ namespace SDL {
4542
this->underlying_event.key.state = value? SDL_PRESSED : SDL_RELEASED;
4643
}
4744

48-
KeyPressedEvent::KeyPressedEvent(const unsigned char repeat, const SDL_Keysym key)
45+
KeyCombination KeyboardEvent::key() const {
46+
return Key(this->underlying_event.key.keysym);
47+
}
48+
49+
KeyPressedEvent::KeyPressedEvent(const unsigned char repeat, const KeyCombination key)
4950
: KeyboardEvent(repeat, true, key) {
5051
}
5152

52-
KeyPressedEvent::KeyPressedEvent(const SDL_Keysym key) : KeyboardEvent(true, key) {
53+
KeyPressedEvent::KeyPressedEvent(const KeyCombination key) : KeyboardEvent(true, key) {
5354
}
5455

5556
KeyReleasedEvent::KeyReleasedEvent() : KeyboardEvent() {
5657
this->pressed(false);
5758
}
5859

59-
KeyReleasedEvent::KeyReleasedEvent(const SDL_Keysym key) : KeyboardEvent(false, key) {
60+
KeyReleasedEvent::KeyReleasedEvent(const KeyCombination key) : KeyboardEvent(false, key) {
6061
}
6162

6263
TextEditingEvent::TextEditingEvent()

keyboard_event.hh

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#define OBJSDL_KEYBOARD_EVENT_HH
33
#include "event.hh"
44

5+
#include "keys.hh"
6+
57
namespace SDL {
68
namespace events {
79
class KeyboardRelatedEvent : public BuiltinEvent {
@@ -11,15 +13,21 @@ namespace SDL {
1113
class KeyboardEvent : public KeyboardRelatedEvent {
1214
public:
1315
Uint8& repeat;
14-
SDL_Keysym& key_symbol;
1516

1617
KeyboardEvent();
1718
KeyboardEvent(const SDL_Event& ev);
1819
KeyboardEvent(SDL_Event&& ev);
19-
KeyboardEvent(const unsigned char repeat, const bool pressed, const SDL_Keysym key);
20-
KeyboardEvent(const bool pressed, const SDL_Keysym key);
20+
KeyboardEvent(
21+
const unsigned char repeat,
22+
const bool pressed,
23+
const KeyCombination key
24+
);
25+
KeyboardEvent(const bool pressed, const KeyCombination key);
2126
bool pressed() const;
2227
void pressed(const bool);
28+
29+
KeyCombination key() const;
30+
void key(const KeyCombination);
2331
};
2432

2533
class KeyPressedEvent : public KeyboardEvent {
@@ -28,11 +36,11 @@ namespace SDL {
2836
KeyPressedEvent(
2937
const unsigned char repeat,
3038
const bool pressed,
31-
const SDL_Keysym key
39+
const KeyCombination key
3240
) = delete;
33-
KeyPressedEvent(const bool pressed, const SDL_Keysym key) = delete;
34-
KeyPressedEvent(const unsigned char repeat, const SDL_Keysym key);
35-
KeyPressedEvent(const SDL_Keysym key);
41+
KeyPressedEvent(const bool pressed, const KeyCombination key) = delete;
42+
KeyPressedEvent(const unsigned char repeat, const KeyCombination key);
43+
KeyPressedEvent(const KeyCombination key);
3644
};
3745

3846
class KeyReleasedEvent : public KeyboardEvent {
@@ -42,10 +50,10 @@ namespace SDL {
4250
KeyReleasedEvent(
4351
const unsigned char repeat,
4452
const bool pressed,
45-
const SDL_Keysym key
53+
const KeyCombination key
4654
) = delete;
47-
KeyReleasedEvent(const bool pressed, const SDL_Keysym key) = delete;
48-
KeyReleasedEvent(const SDL_Keysym key);
55+
KeyReleasedEvent(const bool pressed, const KeyCombination key) = delete;
56+
KeyReleasedEvent(const KeyCombination key);
4957
};
5058

5159
class TextInputRelatedEvent : public KeyboardRelatedEvent {

keys.cc

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include "keys.hh"
2+
3+
4+
namespace SDL {
5+
namespace events {
6+
KeyCombination::KeyCombination(const SDL_Keysym keysym) {
7+
this->sdl_keysym = keysym;
8+
}
9+
10+
KeyCombination::KeyCombination(const Uint16 modifiers, const ScanKey key) {
11+
this->scan_key(key);
12+
this->sdl_keysym.mod = modifiers;
13+
}
14+
15+
KeyCombination::KeyCombination(const Uint16 modifiers, const Key key) {
16+
this->key(key);
17+
this->sdl_keysym.mod = modifiers;
18+
}
19+
20+
KeyCombination::KeyCombination(const ScanKey key) : KeyCombination(0, key) {
21+
}
22+
23+
KeyCombination::KeyCombination(const Key key) : KeyCombination(0, key) {
24+
}
25+
26+
bool KeyCombination::modifier(const Modifier m) const {
27+
return this->sdl_keysym.mod & Uint16(m);
28+
}
29+
30+
void KeyCombination::modifier(const Modifier m, const bool value) {
31+
if (this->modifier(m) != value)
32+
this->sdl_keysym.mod = this->sdl_keysym.mod ^ Uint16(m);
33+
}
34+
35+
ScanKey KeyCombination::scan_key() const {
36+
return ScanKey(this->sdl_keysym.scancode);
37+
}
38+
39+
void KeyCombination::scan_key(const ScanKey key) {
40+
this->sdl_keysym.scancode = SDL_Scancode(key);
41+
this->sdl_keysym.sym = SDL_GetKeyFromScancode(SDL_Scancode(key));
42+
}
43+
44+
Key KeyCombination::key() const {
45+
return Key(this->sdl_keysym.sym);
46+
}
47+
48+
void KeyCombination::key(const Key key) {
49+
this->sdl_keysym.sym = SDL_Keycode(key);
50+
this->sdl_keysym.scancode = SDL_GetScancodeFromKey(SDL_Keycode(key));
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)