Leviathan  0.8.0.0
Leviathan game engine
Key.h
Go to the documentation of this file.
1 // Leviathan Game Engine
2 // Copyright (c) 2012-2018 Henri Hyyryläinen
3 // ------------------------------------ //
4 #pragma once
5 #include "Define.h"
6 // ------------------------------------ //
8 #include "GUI/KeyMapping.h"
10 #include "Window.h"
11 
12 enum KEYSPECIAL {
18  // = 0x20
19  // 0x40
20  // 0x80 // first byte full
21  // 0x100
22  // 0x200
23  // 0x400
24  // 0x800
25  // 0x1000
26  // 0x2000
27  // 0x4000
28  // 0x8000 // second byte full (int range might stop here(
29  // 0x10000
30  // 0x20000
31  // 0x40000
32  // 0x80000
33  // 0x100000
34  // 0x200000
35  // 0x400000
36  // 0x800000 // third byte full
37  // 0x1000000
38  // 0x2000000
39  // 0x4000000
40  // 0x8000000
41  // 0x10000000
42  // 0x20000000
43  // 0x40000000
44  // 0x80000000 // fourth byte full (will need QWORD here)
45  // 0x100000000
46 };
47 
48 namespace Leviathan {
49 
50 template<class T>
51 class Key {
52 public:
53  inline Key<T>()
54  {
55  Extras = 0;
56  Character = (T)0;
57  }
58  inline Key<T>(const T& character, const short& additional)
59  {
60  Extras = additional;
61  Character = character;
62  }
63  inline ~Key() {}
64 
65  inline bool Match(const Key<T>& other, bool strict = false) const
66  {
67  if(other.Character != this->Character)
68  return false;
69  if(strict) {
70  if(other.Extras != this->Extras)
71  return false;
72  } else {
73 
74  if((Extras & KEYSPECIAL_SHIFT) && !(other.Extras & KEYSPECIAL_SHIFT)) {
75  return false;
76  }
77  if((Extras & KEYSPECIAL_ALT) && !(other.Extras & KEYSPECIAL_ALT)) {
78  return false;
79  }
80  if((Extras & KEYSPECIAL_CTRL) && !(other.Extras & KEYSPECIAL_CTRL)) {
81  return false;
82  }
83  }
84  return true;
85  }
86 
87  inline std::string GenerateStringMessage(const int& style = 0)
88  {
89  // create a string that represents this key //
90  if(style == 0) {
91  // debug string //
92  std::string resultstr = "Key[" + Convert::ToString((char)Character) + "]=";
93 
94  resultstr += Convert::ToString<T>(Character);
95  resultstr += "(0x" + Convert::ToHexadecimalString<T>(Character) + ")+";
96 
97  return resultstr;
98  }
99  // various styles from which a key can be easily parsed //
100  DEBUG_BREAK;
101  return "error";
102  }
103 
104  inline bool Match(const T& chara, const short& additional, bool strict = false) const
105  {
106  return Match(Key<T>(chara, additional), strict);
107  }
108 
109  inline bool Match(const T& chara) const
110  {
111  if(chara != this->Character)
112  return false;
113  return true;
114  }
115 
116 
117  T GetCharacter() const
118  {
119  return Character;
120  }
121  short GetAdditional() const
122  {
123  return Extras;
124  }
125 
126  void Set(const T& character, const short& additional)
127  {
128  Character = character;
129  Extras = additional;
130  }
131 
132  void SetAdditional(const short& additional)
133  {
134 
135  Extras = additional;
136  }
137 
138  static void DeConstructSpecial(short keyspes, bool& Shift, bool& Alt, bool& Ctrl)
139  {
140 
141  if(keyspes & KEYSPECIAL_SHIFT)
142  Shift = true;
143  if(keyspes & KEYSPECIAL_ALT)
144  Alt = true;
145  if(keyspes & KEYSPECIAL_CTRL)
146  Ctrl = true;
147  }
148 
150  static Key<T> GenerateKeyFromString(const std::string& representation)
151  {
152  if(representation.size() == 0) {
153  // empty, nothing to do //
154  return Key<T>((T)0, 0);
155  }
156 
157  StringIterator itr(representation);
158 
159  auto str = itr.GetUntilNextCharacterOrAll<std::string>('+');
160 
161  T character;
162 
163  // Only upcase single letters as long key codes have also lowercase letters
164  if(str->length() <= 1) {
165 
166  auto converted = StringOperations::ToUpperCase<std::string>(*str);
167 
168  character = Leviathan::KeyMapping::ConvertStringToKeyCode(converted);
169 
170  } else {
171 
173  }
174 
175  short special = 0;
176 
177  while((str = itr.GetUntilNextCharacterOrAll<std::string>('+')) && (str->size() > 0)) {
178 
179  if(StringOperations::CompareInsensitive(*str, std::string("ALT"))) {
180  special |= KEYSPECIAL_ALT;
181  }
182  if(StringOperations::CompareInsensitive(*str, std::string("SHIFT"))) {
183  special |= KEYSPECIAL_SHIFT;
184  }
185  if(StringOperations::CompareInsensitive(*str, std::string("CTRL"))) {
186  special |= KEYSPECIAL_CTRL;
187  }
188  if(StringOperations::CompareInsensitive(*str, std::string("WIN")) ||
189  StringOperations::CompareInsensitive(*str, std::string("META")) ||
190  StringOperations::CompareInsensitive(*str, std::string("SUPER")) ||
191  StringOperations::CompareInsensitive(*str, std::string("GUI"))) {
192  special |= KEYSPECIAL_SUPER;
193  }
194  }
195 
196  return Key<T>(character, special);
197  }
198 
199  std::string GenerateStringFromKey()
200  {
201  // First the actual key value //
202  auto resultstr = Leviathan::KeyMapping::ConvertKeyCodeToString(Character);
203 
204  // Add special modifiers //
205  if(Extras & KEYSPECIAL_ALT)
206  resultstr += "+ALT";
207  if(Extras & KEYSPECIAL_CTRL)
208  resultstr += "+CTRL";
209  if(Extras & KEYSPECIAL_SHIFT)
210  resultstr += "+SHIFT";
211  if(Extras & KEYSPECIAL_SUPER)
212  resultstr += "+META";
213  // Result is done //
214  return resultstr;
215  }
216 
217 private:
218  short Extras;
219  T Character;
220 };
221 
222 // This is the most likely type //
224 
225 } // namespace Leviathan
bool Match(const T &chara) const
Definition: Key.h:109
std::string GenerateStringFromKey()
Definition: Key.h:199
DLLEXPORT std::string ConvertKeyCodeToString(const int32_t &code)
Definition: KeyMapping.cpp:52
Key< int32_t > GKey
Definition: Key.h:223
void SetAdditional(const short &additional)
Definition: Key.h:132
T GetCharacter() const
Definition: Key.h:117
KEYSPECIAL
Definition: Key.h:12
std::unique_ptr< RStrType > GetUntilNextCharacterOrAll(int charactertolookfor, int specialflags=0)
Gets characters until a character or all remaining characters.
static Key< T > GenerateKeyFromString(const std::string &representation)
https://wiki.libsdl.org/SDL_Keycode for key names
Definition: Key.h:150
DLLEXPORT int32_t ConvertStringToKeyCode(const std::string &str)
Definition: KeyMapping.cpp:38
bool Match(const Key< T > &other, bool strict=false) const
Definition: Key.h:65
Iterator class for getting parts of a string.
static void DeConstructSpecial(short keyspes, bool &Shift, bool &Alt, bool &Ctrl)
Definition: Key.h:138
std::string GenerateStringMessage(const int &style=0)
Definition: Key.h:87
static std::string ToString(const T &val)
Definition: Convert.h:72
short GetAdditional() const
Definition: Key.h:121
static bool CompareInsensitive(const StringTypeN &data, const StringTypeN &second)
bool Match(const T &chara, const short &additional, bool strict=false) const
Definition: Key.h:104
The access mask controls which registered functions and classes a script sees.
Definition: GameModule.h:12
void Set(const T &character, const short &additional)
Definition: Key.h:126