Refractor headers and split code into program files for app & localization files.

Split keybinds from window.h file. General cleanup
This commit is contained in:
2026-06-01 23:20:07 +01:00
parent dcb8b46245
commit c24285b0af
11 changed files with 755 additions and 646 deletions
+221
View File
@@ -0,0 +1,221 @@
#ifndef KEYBINDS_H
#define KEYBINDS_H
#include "pch.h"
#include "utils.h"
enum Key : uint16_t {
Unknown = SDL_SCANCODE_UNKNOWN,
A = SDL_SCANCODE_A,
B,
C,
D,
E,
F,
G,
H,
I,
J,
K,
L,
M,
N,
O,
P,
Q,
R,
S,
T,
U,
V,
W,
X,
Y,
Z,
Num0 = SDL_SCANCODE_0,
Num1,
Num2,
Num3,
Num4,
Num5,
Num6,
Num7,
Num8,
Num9,
Space = SDL_SCANCODE_SPACE,
Enter = SDL_SCANCODE_RETURN,
Escape = SDL_SCANCODE_ESCAPE,
Tab = SDL_SCANCODE_TAB,
Backspace = SDL_SCANCODE_BACKSPACE,
LeftShift = SDL_SCANCODE_LSHIFT,
RightShift = SDL_SCANCODE_RSHIFT,
LeftCtrl = SDL_SCANCODE_LCTRL,
RightCtrl = SDL_SCANCODE_RCTRL,
LeftAlt = SDL_SCANCODE_LALT,
RightAlt = SDL_SCANCODE_RALT,
CapsLock = SDL_SCANCODE_CAPSLOCK,
Up = SDL_SCANCODE_UP,
Down = SDL_SCANCODE_DOWN,
Left = SDL_SCANCODE_LEFT,
Right = SDL_SCANCODE_RIGHT,
Home = SDL_SCANCODE_HOME,
End = SDL_SCANCODE_END,
PageUp = SDL_SCANCODE_PAGEUP,
PageDown = SDL_SCANCODE_PAGEDOWN,
Delete = SDL_SCANCODE_DELETE,
Insert = SDL_SCANCODE_INSERT,
Minus = SDL_SCANCODE_MINUS,
Equals = SDL_SCANCODE_EQUALS,
LeftBracket = SDL_SCANCODE_LEFTBRACKET,
RightBracket = SDL_SCANCODE_RIGHTBRACKET,
Semicolon = SDL_SCANCODE_SEMICOLON,
Apostrophe = SDL_SCANCODE_APOSTROPHE,
Grave = SDL_SCANCODE_GRAVE,
Comma = SDL_SCANCODE_COMMA,
Period = SDL_SCANCODE_PERIOD,
Slash = SDL_SCANCODE_SLASH,
Backslash = SDL_SCANCODE_NONUSBACKSLASH,
F1 = SDL_SCANCODE_F1,
F2,
F3,
F4,
F5,
F6,
F7,
F8,
F9,
F10,
F11,
F12,
MouseLeft = 513,
MouseMiddle,
MouseRight
};
constexpr std::pair<std::string, Key> KEY_NAMES[] = {
{"a", Key::A},
{"b", Key::B},
{"c", Key::C},
{"d", Key::D},
{"e", Key::E},
{"f", Key::F},
{"g", Key::G},
{"h", Key::H},
{"i", Key::I},
{"j", Key::J},
{"k", Key::K},
{"l", Key::L},
{"m", Key::M},
{"n", Key::N},
{"o", Key::O},
{"p", Key::P},
{"q", Key::Q},
{"r", Key::R},
{"s", Key::S},
{"t", Key::T},
{"u", Key::U},
{"v", Key::V},
{"w", Key::W},
{"x", Key::X},
{"y", Key::Y},
{"z", Key::Z},
{"num0", Key::Num0},
{"num1", Key::Num1},
{"num2", Key::Num2},
{"num3", Key::Num3},
{"num4", Key::Num4},
{"num5", Key::Num5},
{"num6", Key::Num6},
{"num7", Key::Num7},
{"num8", Key::Num8},
{"num9", Key::Num9},
{"space", Key::Space},
{"enter", Key::Enter},
{"escape", Key::Escape},
{"tab", Key::Tab},
{"backspace", Key::Backspace},
{"left_shift", Key::LeftShift},
{"right_shift", Key::RightShift},
{"left_ctrl", Key::LeftCtrl},
{"right_ctrl", Key::RightCtrl},
{"left_alt", Key::LeftAlt},
{"right_alt", Key::RightAlt},
{"capslock", Key::CapsLock},
{"up", Key::Up},
{"down", Key::Down},
{"left", Key::Left},
{"right", Key::Right},
{"home", Key::Home},
{"end", Key::End},
{"page_up", Key::PageUp},
{"page_down", Key::PageDown},
{"delete", Key::Delete},
{"insert", Key::Insert},
{"minus", Key::Minus},
{"equals", Key::Equals},
{"left_bracket", Key::LeftBracket},
{"right_bracket", Key::RightBracket},
{"semicolon", Key::Semicolon},
{"apostrophe", Key::Apostrophe},
{"grave", Key::Grave},
{"comma", Key::Comma},
{"period", Key::Period},
{"slash", Key::Slash},
{"backslash", Key::Backslash},
{"f1", Key::F1},
{"f2", Key::F2},
{"f3", Key::F3},
{"f4", Key::F4},
{"f5", Key::F5},
{"f6", Key::F6},
{"f7", Key::F7},
{"f8", Key::F8},
{"f9", Key::F9},
{"f10", Key::F10},
{"f11", Key::F11},
{"f12", Key::F12},
{"mouse_left", Key::MouseLeft},
{"mouse_middle", Key::MouseMiddle},
{"mouse_right", Key::MouseRight}
};
inline Key get_key_from_name(std::string_view name) {
for (const auto &[key_name, key] : KEY_NAMES)
if (key_name == name)
return key;
return Key::Unknown;
}
inline std::unordered_map<std::string, Key> action_key_map;
inline void set_action_mapping(const std::string &action_name, const std::string &key_name) {
Key key = get_key_from_name(key_name);
if (key != Key::Unknown)
action_key_map[action_name] = key;
}
inline Key get_key(std::string &name) {
auto it = action_key_map.find(name);
if (it != action_key_map.end())
return it->second;
return get_key_from_name(name);
}
#endif
+1 -214
View File
@@ -3,6 +3,7 @@
#include "pch.h"
#include "utils.h"
#include "window/keybinds.h"
struct Image {
SDL_Texture *texture;
@@ -40,220 +41,6 @@ inline Pool<Font> font_pool;
// Text pool
inline Pool<Text> text_pool;
enum Key : uint16_t {
Unknown = SDL_SCANCODE_UNKNOWN,
A = SDL_SCANCODE_A,
B,
C,
D,
E,
F,
G,
H,
I,
J,
K,
L,
M,
N,
O,
P,
Q,
R,
S,
T,
U,
V,
W,
X,
Y,
Z,
Num0 = SDL_SCANCODE_0,
Num1,
Num2,
Num3,
Num4,
Num5,
Num6,
Num7,
Num8,
Num9,
Space = SDL_SCANCODE_SPACE,
Enter = SDL_SCANCODE_RETURN,
Escape = SDL_SCANCODE_ESCAPE,
Tab = SDL_SCANCODE_TAB,
Backspace = SDL_SCANCODE_BACKSPACE,
LeftShift = SDL_SCANCODE_LSHIFT,
RightShift = SDL_SCANCODE_RSHIFT,
LeftCtrl = SDL_SCANCODE_LCTRL,
RightCtrl = SDL_SCANCODE_RCTRL,
LeftAlt = SDL_SCANCODE_LALT,
RightAlt = SDL_SCANCODE_RALT,
CapsLock = SDL_SCANCODE_CAPSLOCK,
Up = SDL_SCANCODE_UP,
Down = SDL_SCANCODE_DOWN,
Left = SDL_SCANCODE_LEFT,
Right = SDL_SCANCODE_RIGHT,
Home = SDL_SCANCODE_HOME,
End = SDL_SCANCODE_END,
PageUp = SDL_SCANCODE_PAGEUP,
PageDown = SDL_SCANCODE_PAGEDOWN,
Delete = SDL_SCANCODE_DELETE,
Insert = SDL_SCANCODE_INSERT,
Minus = SDL_SCANCODE_MINUS,
Equals = SDL_SCANCODE_EQUALS,
LeftBracket = SDL_SCANCODE_LEFTBRACKET,
RightBracket = SDL_SCANCODE_RIGHTBRACKET,
Semicolon = SDL_SCANCODE_SEMICOLON,
Apostrophe = SDL_SCANCODE_APOSTROPHE,
Grave = SDL_SCANCODE_GRAVE,
Comma = SDL_SCANCODE_COMMA,
Period = SDL_SCANCODE_PERIOD,
Slash = SDL_SCANCODE_SLASH,
Backslash = SDL_SCANCODE_NONUSBACKSLASH,
F1 = SDL_SCANCODE_F1,
F2,
F3,
F4,
F5,
F6,
F7,
F8,
F9,
F10,
F11,
F12,
MouseLeft = 513,
MouseMiddle,
MouseRight
};
constexpr std::pair<std::string, Key> KEY_NAMES[] = {
{"a", Key::A},
{"b", Key::B},
{"c", Key::C},
{"d", Key::D},
{"e", Key::E},
{"f", Key::F},
{"g", Key::G},
{"h", Key::H},
{"i", Key::I},
{"j", Key::J},
{"k", Key::K},
{"l", Key::L},
{"m", Key::M},
{"n", Key::N},
{"o", Key::O},
{"p", Key::P},
{"q", Key::Q},
{"r", Key::R},
{"s", Key::S},
{"t", Key::T},
{"u", Key::U},
{"v", Key::V},
{"w", Key::W},
{"x", Key::X},
{"y", Key::Y},
{"z", Key::Z},
{"num0", Key::Num0},
{"num1", Key::Num1},
{"num2", Key::Num2},
{"num3", Key::Num3},
{"num4", Key::Num4},
{"num5", Key::Num5},
{"num6", Key::Num6},
{"num7", Key::Num7},
{"num8", Key::Num8},
{"num9", Key::Num9},
{"space", Key::Space},
{"enter", Key::Enter},
{"escape", Key::Escape},
{"tab", Key::Tab},
{"backspace", Key::Backspace},
{"left_shift", Key::LeftShift},
{"right_shift", Key::RightShift},
{"left_ctrl", Key::LeftCtrl},
{"right_ctrl", Key::RightCtrl},
{"left_alt", Key::LeftAlt},
{"right_alt", Key::RightAlt},
{"capslock", Key::CapsLock},
{"up", Key::Up},
{"down", Key::Down},
{"left", Key::Left},
{"right", Key::Right},
{"home", Key::Home},
{"end", Key::End},
{"page_up", Key::PageUp},
{"page_down", Key::PageDown},
{"delete", Key::Delete},
{"insert", Key::Insert},
{"minus", Key::Minus},
{"equals", Key::Equals},
{"left_bracket", Key::LeftBracket},
{"right_bracket", Key::RightBracket},
{"semicolon", Key::Semicolon},
{"apostrophe", Key::Apostrophe},
{"grave", Key::Grave},
{"comma", Key::Comma},
{"period", Key::Period},
{"slash", Key::Slash},
{"backslash", Key::Backslash},
{"f1", Key::F1},
{"f2", Key::F2},
{"f3", Key::F3},
{"f4", Key::F4},
{"f5", Key::F5},
{"f6", Key::F6},
{"f7", Key::F7},
{"f8", Key::F8},
{"f9", Key::F9},
{"f10", Key::F10},
{"f11", Key::F11},
{"f12", Key::F12},
{"mouse_left", Key::MouseLeft},
{"mouse_middle", Key::MouseMiddle},
{"mouse_right", Key::MouseRight}
};
inline Key get_key_from_name(std::string_view name) {
for (const auto &[key_name, key] : KEY_NAMES)
if (key_name == name)
return key;
return Key::Unknown;
}
inline std::unordered_map<std::string, Key> action_key_map;
inline void set_action_mapping(const std::string &action_name, const std::string &key_name) {
Key key = get_key_from_name(key_name);
if (key != Key::Unknown)
action_key_map[action_name] = key;
}
inline Key get_key(std::string &name) {
auto it = action_key_map.find(name);
if (it != action_key_map.end())
return it->second;
return get_key_from_name(name);
}
struct Event {
enum Type {
NONE,