Leviathan  0.8.0.0
Leviathan game engine
Leviathan::Key< T > Class Template Reference

#include <Key.h>

Public Member Functions

 Key ()
 
 Key (const T &character, const short &additional)
 
 ~Key ()
 
bool Match (const Key< T > &other, bool strict=false) const
 
std::string GenerateStringMessage (const int &style=0)
 
bool Match (const T &chara, const short &additional, bool strict=false) const
 
bool Match (const T &chara) const
 
GetCharacter () const
 
short GetAdditional () const
 
void Set (const T &character, const short &additional)
 
void SetAdditional (const short &additional)
 
std::string GenerateStringFromKey ()
 

Static Public Member Functions

static void DeConstructSpecial (short keyspes, bool &Shift, bool &Alt, bool &Ctrl)
 
static Key< T > GenerateKeyFromString (const std::string &representation)
 https://wiki.libsdl.org/SDL_Keycode for key names More...
 

Detailed Description

template<class T>
class Leviathan::Key< T >

Definition at line 51 of file Key.h.

Constructor & Destructor Documentation

◆ Key() [1/2]

template<class T>
Leviathan::Key< T >::Key ( )
inline

Definition at line 53 of file Key.h.

54  {
55  Extras = 0;
56  Character = (T)0;
57  }

◆ Key() [2/2]

template<class T>
Leviathan::Key< T >::Key ( const T &  character,
const short &  additional 
)
inline

Definition at line 58 of file Key.h.

59  {
60  Extras = additional;
61  Character = character;
62  }

◆ ~Key()

template<class T>
Leviathan::Key< T >::~Key ( )
inline

Definition at line 63 of file Key.h.

63 {}

Member Function Documentation

◆ DeConstructSpecial()

template<class T>
static void Leviathan::Key< T >::DeConstructSpecial ( short  keyspes,
bool &  Shift,
bool &  Alt,
bool &  Ctrl 
)
inlinestatic

Definition at line 138 of file Key.h.

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  }

◆ GenerateKeyFromString()

template<class T>
static Key<T> Leviathan::Key< T >::GenerateKeyFromString ( const std::string &  representation)
inlinestatic

https://wiki.libsdl.org/SDL_Keycode for key names

Definition at line 150 of file Key.h.

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  std::unique_ptr<std::string> str;
160 
161  // Allow plus key
162  if(representation[0] == '+') {
163  itr.MoveToNext();
164  str = std::make_unique<std::string>("+");
165  } else {
166 
167  str = itr.GetUntilNextCharacterOrAll<std::string>('+');
168  }
169 
170  if(!str) {
171 
172  throw Leviathan::InvalidArgument("Key: can't parse: " + representation);
173  }
174 
175  T character;
176 
177  // Only upcase single letters as long key codes have also lowercase letters
178  if(str->length() <= 1) {
179 
180  auto converted = StringOperations::ToUpperCase<std::string>(*str);
181 
182  character = Leviathan::KeyMapping::ConvertStringToKeyCode(converted);
183 
184  } else {
185 
186  // Detect things like "Keypad +" and make them work
187  const auto pos = itr.GetPosition();
188  if((pos + 1 >= representation.length() && pos < representation.length() &&
189  representation[pos] == '+') ||
190  // This is things like "Keypad ++SHIFT"
191  (pos + 1 <= representation.length() && representation[pos] == '+' &&
192  representation[pos + 1] == '+')) {
193 
194  character = Leviathan::KeyMapping::ConvertStringToKeyCode(*str + "+");
195  } else {
196 
198  }
199  }
200 
201  short special = 0;
202 
203  while((str = itr.GetUntilNextCharacterOrAll<std::string>('+')) && (str->size() > 0)) {
204 
205  if(StringOperations::CompareInsensitive(*str, std::string("ALT"))) {
206  special |= KEYSPECIAL_ALT;
207  }
208  if(StringOperations::CompareInsensitive(*str, std::string("SHIFT"))) {
209  special |= KEYSPECIAL_SHIFT;
210  }
211  if(StringOperations::CompareInsensitive(*str, std::string("CTRL"))) {
212  special |= KEYSPECIAL_CTRL;
213  }
214  if(StringOperations::CompareInsensitive(*str, std::string("WIN")) ||
215  StringOperations::CompareInsensitive(*str, std::string("META")) ||
216  StringOperations::CompareInsensitive(*str, std::string("SUPER")) ||
217  StringOperations::CompareInsensitive(*str, std::string("GUI"))) {
218  special |= KEYSPECIAL_SUPER;
219  }
220  }
221 
222  return Key<T>(character, special);
223  }
DLLEXPORT int32_t ConvertStringToKeyCode(const std::string &str)
Definition: KeyMapping.cpp:38
static bool CompareInsensitive(const StringTypeN &data, const StringTypeN &second)

◆ GenerateStringFromKey()

template<class T>
std::string Leviathan::Key< T >::GenerateStringFromKey ( )
inline

Definition at line 225 of file Key.h.

226  {
227  // First the actual key value //
228  auto resultstr = Leviathan::KeyMapping::ConvertKeyCodeToString(Character);
229 
230  // Add special modifiers //
231  if(Extras & KEYSPECIAL_ALT)
232  resultstr += "+ALT";
233  if(Extras & KEYSPECIAL_CTRL)
234  resultstr += "+CTRL";
235  if(Extras & KEYSPECIAL_SHIFT)
236  resultstr += "+SHIFT";
237  if(Extras & KEYSPECIAL_SUPER)
238  resultstr += "+META";
239  // Result is done //
240  return resultstr;
241  }
DLLEXPORT std::string ConvertKeyCodeToString(const int32_t &code)
Definition: KeyMapping.cpp:52

◆ GenerateStringMessage()

template<class T>
std::string Leviathan::Key< T >::GenerateStringMessage ( const int &  style = 0)
inline

Definition at line 87 of file Key.h.

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  }
static std::string ToString(const T &val)
Definition: Convert.h:72

◆ GetAdditional()

template<class T>
short Leviathan::Key< T >::GetAdditional ( ) const
inline

Definition at line 121 of file Key.h.

122  {
123  return Extras;
124  }

◆ GetCharacter()

template<class T>
T Leviathan::Key< T >::GetCharacter ( ) const
inline

Definition at line 117 of file Key.h.

118  {
119  return Character;
120  }

◆ Match() [1/3]

template<class T>
bool Leviathan::Key< T >::Match ( const Key< T > &  other,
bool  strict = false 
) const
inline

Definition at line 65 of file Key.h.

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  }

◆ Match() [2/3]

template<class T>
bool Leviathan::Key< T >::Match ( const T &  chara,
const short &  additional,
bool  strict = false 
) const
inline

Definition at line 104 of file Key.h.

105  {
106  return Match(Key<T>(chara, additional), strict);
107  }
bool Match(const Key< T > &other, bool strict=false) const
Definition: Key.h:65

◆ Match() [3/3]

template<class T>
bool Leviathan::Key< T >::Match ( const T &  chara) const
inline

Definition at line 109 of file Key.h.

110  {
111  if(chara != this->Character)
112  return false;
113  return true;
114  }

◆ Set()

template<class T>
void Leviathan::Key< T >::Set ( const T &  character,
const short &  additional 
)
inline

Definition at line 126 of file Key.h.

127  {
128  Character = character;
129  Extras = additional;
130  }

◆ SetAdditional()

template<class T>
void Leviathan::Key< T >::SetAdditional ( const short &  additional)
inline

Definition at line 132 of file Key.h.

133  {
134 
135  Extras = additional;
136  }

The documentation for this class was generated from the following file: