Leviathan  0.8.0.0
Leviathan game engine
Leviathan::KeyConfiguration Class Reference

Holds key configuration for an application. More...

#include <KeyConfiguration.h>

+ Inheritance diagram for Leviathan::KeyConfiguration:

Public Member Functions

DLLEXPORT KeyConfiguration (const std::string &configfile)
 
DLLEXPORT ~KeyConfiguration ()
 
DLLEXPORT bool Init (std::function< void(Lock &guard, KeyConfiguration *checkfrom)> functocheck)
 Loads the defined keys from a file. More...
 
DLLEXPORT void Release ()
 Saves all keys if modified. More...
 
DLLEXPORT bool AddKeyIfMissing (Lock &guard, const std::string &name, const std::vector< std::string > &defaultkeys)
 Adds a key if one with the name isn't defined. More...
 
DLLEXPORT void MarkAsChanged ()
 Marks current keys as changed and that the configuration should be saved to a file. More...
 
DLLEXPORT void Save (Lock &guard)
 Saves current keys. More...
 
DLLEXPORT std::shared_ptr< std::vector< GKey > > ResolveControlNameToKey (const std::string &controlkey)
 Resolves a control key string ("WalkForward") to a key. More...
 
DLLEXPORT GKey ResolveControlNameToFirstKey (const std::string &controlkey)
 Resolve variant for getting the first binding or throwing. More...
 
DLLEXPORT std::string ResolveKeyToControlName (const GKey &key)
 
- Public Member Functions inherited from Leviathan::ThreadSafeGeneric< MutexType >
DLLEXPORT ThreadSafeGeneric ()
 
DLLEXPORT ~ThreadSafeGeneric ()
 
FORCE_INLINE void VerifyLock (RecursiveLock &guard) const
 
FORCE_INLINE void VerifyLock (Lock &lockit) const
 

Additional Inherited Members

- Protected Attributes inherited from Leviathan::ThreadSafeGeneric< MutexType >
MutexType ObjectsLock
 

Detailed Description

Holds key configuration for an application.

Definition at line 15 of file KeyConfiguration.h.

Constructor & Destructor Documentation

◆ KeyConfiguration()

DLLEXPORT KeyConfiguration::KeyConfiguration ( const std::string &  configfile)
Parameters
configfileThe file which is used for saving and loading keys

Definition at line 8 of file KeyConfiguration.cpp.

8  :
9  KeyStorageFile(configfile)
10 {
11 }

◆ ~KeyConfiguration()

DLLEXPORT KeyConfiguration::~KeyConfiguration ( )

Definition at line 13 of file KeyConfiguration.cpp.

14 {
15  // The keys should already be saved by now //
16 }

Member Function Documentation

◆ AddKeyIfMissing()

DLLEXPORT bool KeyConfiguration::AddKeyIfMissing ( Lock guard,
const std::string &  name,
const std::vector< std::string > &  defaultkeys 
)

Adds a key if one with the name isn't defined.

Returns
True if added

Definition at line 73 of file KeyConfiguration.cpp.

75 {
76  if(defaultkeys.empty()) {
77  LOG_WARNING("KeyConfiguration: AddKeyIfMissing: defaultkeys is empty, not adding one "
78  "with name: " +
79  name);
80  return false;
81  }
82 
83  auto iter = KeyConfigurations.find(name);
84  if(iter != KeyConfigurations.end())
85  return false;
86 
87  auto keys = std::make_shared<std::vector<GKey>>();
88 
89  for(const auto& key : defaultkeys)
90  keys->push_back(GKey::GenerateKeyFromString(key));
91 
92  KeyConfigurations[name] = keys;
93 
94  Marked = true;
95  return true;
96 }
#define LOG_WARNING(x)
Definition: Define.h:82
static Key< T > GenerateKeyFromString(const std::string &representation)
https://wiki.libsdl.org/SDL_Keycode for key names
Definition: Key.h:150

◆ Init()

DLLEXPORT bool KeyConfiguration::Init ( std::function< void(Lock &guard, KeyConfiguration *checkfrom)>  functocheck)

Loads the defined keys from a file.

the function argument is called to verify that all required keys are defined and it can add missing keys

Definition at line 18 of file KeyConfiguration.cpp.

20 {
21  GUARD_LOCK();
22 
23  // Skip if not given a file //
24  if(KeyStorageFile.size() == 0)
25  return true;
26 
27  // Load the values from the file //
28  std::vector<std::shared_ptr<NamedVariableList>> tmpvalues;
29 
30  if(FileSystem::LoadDataDump(KeyStorageFile, tmpvalues, Logger::Get())) {
31  // Create keys from the values //
32 
33  for(auto iter = tmpvalues.begin(); iter != tmpvalues.end(); ++iter) {
34  // Get name for storing //
35  auto name = (*iter)->GetName();
36  // Try to create a key //
37  auto keys = std::make_shared<std::vector<GKey>>();
38 
39  std::vector<VariableBlock*>& values = (*iter)->GetValues();
40 
41  for(size_t i = 0; i < values.size(); i++) {
42  // Parse a key //
43  if(values[i]->IsConversionAllowedNonPtr<std::string>()) {
44  keys->push_back(
45  GKey::GenerateKeyFromString(values[i]->operator std::string()));
46  }
47  }
48 
49  // Assign to the map //
50  KeyConfigurations[name] = keys;
51  }
52  }
53 
54 
55  // Call the function with a pointer to this for it to verify loaded keys //
56  functocheck(guard, this);
57 
58  if(Marked)
59  Save(guard);
60 
61  return true;
62 }
DLLEXPORT void Save(Lock &guard)
Saves current keys.
static Key< T > GenerateKeyFromString(const std::string &representation)
https://wiki.libsdl.org/SDL_Keycode for key names
Definition: Key.h:150
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106
static DLLEXPORT bool LoadDataDump(const std::string &file, std::vector< std::shared_ptr< NamedVariableList >> &vec, LErrorReporter *errorreport)
Definition: FileSystem.cpp:309
#define GUARD_LOCK()
Definition: ThreadSafe.h:91

◆ MarkAsChanged()

DLLEXPORT void KeyConfiguration::MarkAsChanged ( )

Marks current keys as changed and that the configuration should be saved to a file.

Useful during initially checking that all keys exist

Definition at line 98 of file KeyConfiguration.cpp.

99 {
100  Marked = true;
101 }

◆ Release()

DLLEXPORT void KeyConfiguration::Release ( )

Saves all keys if modified.

Definition at line 64 of file KeyConfiguration.cpp.

65 {
66  GUARD_LOCK();
67 
68  // Save all the keys //
69  if(Marked)
70  Save(guard);
71 }
DLLEXPORT void Save(Lock &guard)
Saves current keys.
#define GUARD_LOCK()
Definition: ThreadSafe.h:91

◆ ResolveControlNameToFirstKey()

DLLEXPORT GKey KeyConfiguration::ResolveControlNameToFirstKey ( const std::string &  controlkey)

Resolve variant for getting the first binding or throwing.

Exceptions
InvalidArgumentif controlkey not found

Definition at line 149 of file KeyConfiguration.cpp.

150 {
151  GUARD_LOCK();
152  auto iter = KeyConfigurations.find(controlkey);
153 
154  if(iter != KeyConfigurations.end())
155  return iter->second->at(0);
156 
157  throw InvalidArgument("Key with name \"" + controlkey + "\" not found");
158 }
#define GUARD_LOCK()
Definition: ThreadSafe.h:91

◆ ResolveControlNameToKey()

DLLEXPORT std::shared_ptr< std::vector< GKey > > KeyConfiguration::ResolveControlNameToKey ( const std::string &  controlkey)

Resolves a control key string ("WalkForward") to a key.

Definition at line 137 of file KeyConfiguration.cpp.

139 {
140  GUARD_LOCK();
141  auto iter = KeyConfigurations.find(controlkey);
142 
143  if(iter != KeyConfigurations.end())
144  return iter->second;
145 
146  return NULL;
147 }
#define GUARD_LOCK()
Definition: ThreadSafe.h:91

◆ ResolveKeyToControlName()

DLLEXPORT std::string KeyConfiguration::ResolveKeyToControlName ( const GKey key)

Checks which configuration key string matches input key

Warning
this is quite costly function

Definition at line 160 of file KeyConfiguration.cpp.

161 {
162  GUARD_LOCK();
163  // We need to loop through all the keys and see if any of them match //
164  for(auto iter = KeyConfigurations.begin(); iter != KeyConfigurations.end(); ++iter) {
165 
166  for(size_t i = 0; i < iter->second->size(); i++) {
167  // We'll use the partial match function //
168  if(iter->second->at(i).Match(key, false))
169  return iter->first;
170  }
171  }
172  // Not found, return an empty string //
173  return "";
174 }
#define GUARD_LOCK()
Definition: ThreadSafe.h:91

◆ Save()

DLLEXPORT void KeyConfiguration::Save ( Lock guard)

Saves current keys.

Definition at line 103 of file KeyConfiguration.cpp.

104 {
105  // Skip if not given a file //
106  if(KeyStorageFile.size() == 0)
107  return;
108 
109  // First generate a string for this //
110  std::string savedata = "";
111 
112  // Loop through all keys and create string representations from them //
113  for(auto iter = KeyConfigurations.begin(); iter != KeyConfigurations.end(); ++iter) {
114 
115  // Print the name //
116  savedata += iter->first + " = [";
117 
118  // Create a list of data //
119  for(size_t i = 0; i < iter->second->size(); i++) {
120 
121  if(i != 0)
122  savedata += ", ";
123  auto strrepresentation = iter->second->at(i).GenerateStringFromKey();
124 
125  savedata += "[\"" + strrepresentation + "\"]";
126  }
127 
128  // Add the line end //
129  savedata += "];\n";
130  }
131 
132 
133  // Save the file //
134  FileSystem::WriteToFile(savedata, KeyStorageFile);
135 }
static DLLEXPORT bool WriteToFile(const std::string &data, const std::string &filename)
Definition: FileSystem.cpp:462

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