Skip to content

Commit b8bd6b4

Browse files
fix high priority keyboard handling in non-fullscreen menus
toggle fullscreen etc. didn't work as they were never sent to the keyboard controller as we are still refactoring code...
1 parent 6562588 commit b8bd6b4

File tree

3 files changed

+39
-26
lines changed

3 files changed

+39
-26
lines changed

game/src/engine.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,6 +1177,8 @@ void Engine::eventloop(SDL &sdl, gfx::GLprogram &prog, GLuint vao) {
11771177

11781178
if ((mi && mi->menu) || (capture_keys() && !io.WantCaptureKeyboard))
11791179
kbp_down(keyctl.down(event.key));
1180+
else
1181+
keyctl.down_hp(event.key);
11801182
break;
11811183
case SDL_MOUSEBUTTONDOWN:
11821184
ImGui_ImplSDL2_ProcessEvent(&event);

game/src/engine/keyctl.cpp

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -68,45 +68,43 @@ void KeyboardController::clear(KeyboardMode mode) {
6868
std::fill(state_tapped.begin(), state_tapped.end(), false);
6969
}
7070

71-
GameKey KeyboardController::down(const SDL_KeyboardEvent &e) {
72-
if (mode == KeyboardMode::fullscreen_menu) {
73-
auto it = scan_keys.find(e.keysym.scancode);
74-
75-
if (it == scan_keys.end())
76-
return GameKey::max;
77-
78-
GameKey k = it->second;
79-
size_t idx = (size_t)k;
80-
81-
if (state[idx])
82-
return GameKey::max; // no repeating keys
83-
84-
state[idx] = true;
85-
state_tapped[idx] = false;
86-
return k;
87-
}
88-
89-
auto it = keys.find(e.keysym.sym);
90-
91-
if (it == keys.end())
71+
template<typename T> GameKey RegisterKey(const T t, std::map<T, GameKey> &kbmap, std::vector<bool> &state, std::vector<bool> &tapped)
72+
{
73+
auto it = kbmap.find(t);
74+
if (it == kbmap.end())
9275
return GameKey::max;
9376

9477
GameKey k = it->second;
9578
size_t idx = (size_t)k;
9679

80+
if (state[idx])
81+
return GameKey::max; // no repeating keys
82+
9783
state[idx] = true;
98-
state_tapped[idx] = false;
84+
tapped[idx] = false;
85+
9986
return k;
10087
}
10188

89+
GameKey KeyboardController::down_hp(const SDL_KeyboardEvent &e) {
90+
return RegisterKey(e.keysym.scancode, scan_keys, state, state_tapped);
91+
}
92+
93+
GameKey KeyboardController::down(const SDL_KeyboardEvent &e) {
94+
if (mode == KeyboardMode::fullscreen_menu)
95+
return RegisterKey(e.keysym.scancode, scan_keys, state, state_tapped);
96+
97+
return RegisterKey(e.keysym.sym, keys, state, state_tapped);
98+
}
99+
102100
GameKey KeyboardController::up(const SDL_KeyboardEvent &e) {
103-
if (mode == KeyboardMode::fullscreen_menu) {
104-
auto it = scan_keys.find(e.keysym.scancode);
101+
auto it0 = scan_keys.find(e.keysym.scancode);
105102

106-
if (it == scan_keys.end())
103+
if (mode == KeyboardMode::fullscreen_menu || (it0 != scan_keys.end() && is_hp(it0->second))) {
104+
if (it0 == scan_keys.end())
107105
return GameKey::max;
108106

109-
GameKey k = it->second;
107+
GameKey k = it0->second;
110108
size_t idx = (size_t)k;
111109

112110
if (!state[idx]) {

game/src/engine/keyctl.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@ enum class GameKey {
4242
max,
4343
};
4444

45+
static inline constexpr bool is_hp(GameKey k) {
46+
switch (k) {
47+
case GameKey::toggle_debug_window:
48+
case GameKey::open_help:
49+
case GameKey::toggle_fullscreen:
50+
return true;
51+
default:
52+
return false;
53+
}
54+
}
55+
4556
class KeyboardController final {
4657
std::vector<bool> state, state_tapped;
4758
std::map<SDL_Scancode, GameKey> scan_keys;
@@ -52,6 +63,8 @@ class KeyboardController final {
5263

5364
void clear(KeyboardMode mode);
5465

66+
// High Priority. this is to accommodate for menus that still need to be converted
67+
GameKey down_hp(const SDL_KeyboardEvent&);
5568
GameKey down(const SDL_KeyboardEvent&);
5669
GameKey up(const SDL_KeyboardEvent&);
5770

0 commit comments

Comments
 (0)