OpenGothic
Open source reimplementation of Gothic I and II
Loading...
Searching...
No Matches
keycodec.h
Go to the documentation of this file.
1#pragma once
2
3#include <Tempest/Event>
4#include <string>
5#include <vector>
6
7#include "utils/string_frm.h"
8#include <optional>
9
10class KeyCodec final {
11 public:
12 KeyCodec();
13
16 enum class Mapping {
18 };
19
20 static constexpr auto NumMappings = 2;
21
74
80
81 static auto keysStr(std::string_view keys) -> string_frm<64>;
82
83 Action tr(Tempest::KeyEvent const& e) const;
84 Action tr(Tempest::MouseEvent const& e) const;
85
89 Mapping mapping(Tempest::KeyEvent const& e) const;
90
91 void set(std::string_view section, std::string_view key, int32_t code);
92 void clear(std::string_view section, std::string_view key);
93 void setDefaultKeys(std::string_view preset);
94
95 static int32_t keyToCode(Tempest::Event::KeyType t);
96 static int32_t keyToCode(Tempest::Event::MouseButton t);
97
98 static std::string toCode(int32_t scan);
99
100 private:
101 struct K_Key {
102 K_Key(Tempest::Event::KeyType k, int32_t code):k(k),code(code){}
103 Tempest::Event::KeyType k=Tempest::Event::K_NoKey;
104 int32_t code=0;
105 };
106 struct M_Key {
107 M_Key(Tempest::Event::MouseButton k, int32_t code):k(k),code(code){}
108 Tempest::Event::MouseButton k=Tempest::Event::ButtonNone;
109 int32_t code=0;
110 };
111
112 struct KeyPair {
113 const char* key = "";
114 int32_t k[2]={};
115 bool is(int32_t i) const { return k[0]==i || k[1]==i; }
116
120 auto mapping(int32_t i) const -> std::optional<Mapping> {
121 if(k[0] == i)
122 return Mapping::Primary;
123 if(k[1] == i)
124 return Mapping::Secondary;
125 return std::nullopt;
126 }
127 };
128
129 auto implTr(int32_t code) const -> ActionMapping;
130 static int fetch(std::string_view keys, size_t s, size_t e);
131 static auto keyToStr(int32_t k) -> string_frm<64>;
132 static auto keyToStr(Tempest::Event::KeyType k) -> string_frm<64>;
133 static auto keyToStr(Tempest::Event::MouseButton k) -> string_frm<64>;
134
135 void setDefaultKeysG1();
136 void setDefaultKeysG1Alt();
137 void setupSettings();
138 KeyPair setup(const char* kp);
139 static auto parse(std::string_view kp) -> KeyPair;
140
141 KeyPair keyEnd;
142 KeyPair keyHeal;
143 KeyPair keyPotion;
144 KeyPair keyLockTarget;
145 KeyPair keyParade;
146 KeyPair keyActionRight;
147 KeyPair keyActionLeft;
148 KeyPair keyUp;
149 KeyPair keyDown;
150 KeyPair keyLeft;
151 KeyPair keyRight;
152 KeyPair keyStrafeLeft;
153 KeyPair keyStrafeRight;
154 KeyPair keyAction;
155 KeyPair keySlow;
156 KeyPair keySMove;
157 KeyPair keyWeapon;
158 KeyPair keySneak;
159 KeyPair keyLook;
160 KeyPair keyLookFP;
161 KeyPair keyInventory;
162 KeyPair keyShowStatus;
163 KeyPair keyShowLog;
164 KeyPair keyShowMap;
165
166 std::vector<KeyPair*> allKeys;
167
168 static std::initializer_list<K_Key> keys;
169 static std::initializer_list<M_Key> mkeys;
170 };
171
static auto keysStr(std::string_view keys) -> string_frm< 64 >
Definition keycodec.cpp:305
void set(std::string_view section, std::string_view key, int32_t code)
Definition keycodec.cpp:139
static int32_t keyToCode(Tempest::Event::KeyType t)
Definition keycodec.cpp:423
static constexpr auto NumMappings
Definition keycodec.h:20
static std::string toCode(int32_t scan)
Definition keycodec.cpp:239
void setDefaultKeys(std::string_view preset)
Definition keycodec.cpp:166
void clear(std::string_view section, std::string_view key)
Definition keycodec.cpp:162
@ WeaponMage7
Definition keycodec.h:61
@ ActionLeft
Definition keycodec.h:46
@ ActionRight
Definition keycodec.h:47
@ K_ENTER
Definition keycodec.h:66
@ RotateR
Definition keycodec.h:42
@ WeaponMage4
Definition keycodec.h:58
@ ActionGeneric
Definition keycodec.h:45
@ LookBack
Definition keycodec.h:34
@ FirstPerson
Definition keycodec.h:35
@ WeaponMage10
Definition keycodec.h:64
@ WeaponMele
Definition keycodec.h:54
@ WeaponMage5
Definition keycodec.h:59
@ WeaponMage9
Definition keycodec.h:63
@ RotateL
Definition keycodec.h:41
@ Inventory
Definition keycodec.h:27
@ WeaponBow
Definition keycodec.h:55
@ WeaponMage8
Definition keycodec.h:62
@ Forward
Definition keycodec.h:37
@ WeaponMage3
Definition keycodec.h:57
@ WeaponMage6
Definition keycodec.h:60
Mapping mapping(Tempest::KeyEvent const &e) const
Gets a mapping out of a key event.
Definition keycodec.cpp:135
Action tr(Tempest::KeyEvent const &e) const
Definition keycodec.cpp:107
Encapsulates an in-game action and a key mapping that caused it to be fired.
Definition keycodec.h:76