Leviathan  0.8.0.0
Leviathan game engine
KeyConfiguration.h
Go to the documentation of this file.
1 // Leviathan Game Engine
2 // Copyright (c) 2012-2019 Henri Hyyryläinen
3 #pragma once
4 #include "Define.h"
5 // ------------------------------------ //
6 #include "Common/ThreadSafe.h"
7 #include "Input/Key.h"
8 
9 #include <atomic>
10 #include <functional>
11 
12 namespace Leviathan {
13 
15 class KeyConfiguration : public ThreadSafe {
16 public:
18  DLLEXPORT KeyConfiguration(const std::string& configfile);
19 
22 
24 
29  DLLEXPORT bool Init(
30  std::function<void(Lock& guard, KeyConfiguration* checkfrom)> functocheck);
31 
33  DLLEXPORT void Release();
34 
38  Lock& guard, const std::string& name, const std::vector<std::string>& defaultkeys);
39 
44  DLLEXPORT void MarkAsChanged();
45 
47  DLLEXPORT void Save(Lock& guard);
48 
50  DLLEXPORT std::shared_ptr<std::vector<GKey>> ResolveControlNameToKey(
51  const std::string& controlkey);
52 
55  DLLEXPORT const std::vector<GKey>& ResolveControlNameToKeyVector(
56  const std::string& controlkey);
57 
60  DLLEXPORT GKey ResolveControlNameToFirstKey(const std::string& controlkey);
61 
64  DLLEXPORT std::string ResolveKeyToControlName(const GKey& key);
65 
66 private:
68  std::atomic<bool> Marked = false;
69 
70  std::string KeyStorageFile;
71 
73  bool InMemory = false;
74 
76  std::map<std::string, std::shared_ptr<std::vector<GKey>>> KeyConfigurations;
77 };
78 
79 } // namespace Leviathan
80 
81 #ifdef LEAK_INTO_GLOBAL
83 #endif
Allows the inherited object to be locked.
Definition: ThreadSafe.h:123
Holds key configuration for an application.
DLLEXPORT void Save(Lock &guard)
Saves current keys.
DLLEXPORT std::string ResolveKeyToControlName(const GKey &key)
DLLEXPORT void MarkAsChanged()
Marks current keys as changed and that the configuration should be saved to a file.
DLLEXPORT void Release()
Saves all keys if modified.
DLLEXPORT GKey ResolveControlNameToFirstKey(const std::string &controlkey)
Resolve variant for getting the first binding or throwing.
DLLEXPORT KeyConfiguration()
Creates an in-memory only configuration.
#define DLLEXPORT
Definition: Include.h:115
DLLEXPORT const std::vector< GKey > & ResolveControlNameToKeyVector(const std::string &controlkey)
Resolve variant for getting a reference to the vector of keys.
DLLEXPORT std::shared_ptr< std::vector< GKey > > ResolveControlNameToKey(const std::string &controlkey)
Resolves a control key string ("WalkForward") to a key.
The access mask controls which registered functions and classes a script sees.
Definition: GameModule.h:12
std::unique_lock< std::mutex > Lock
Definition: ThreadSafe.h:16
DLLEXPORT bool Init(std::function< void(Lock &guard, KeyConfiguration *checkfrom)> functocheck)
Loads the defined keys from a file.
DLLEXPORT bool AddKeyIfMissing(Lock &guard, const std::string &name, const std::vector< std::string > &defaultkeys)
Adds a key if one with the name isn&#39;t defined.