-
Notifications
You must be signed in to change notification settings - Fork 1
/
Config.cpp
92 lines (75 loc) · 2.55 KB
/
Config.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//
// Created by Valerij Primachenko on 20-06-30.
//
#include "Config.h"
#include "deps/magic_enum.hpp"
std::map<DAS::Key, Config> parse_config(const nlohmann::json &j) {
// FIXME broken af
std::map<DAS::Key, Config> configs;
for (auto& [_key, value] : j.items()) {
auto key = magic_enum::enum_cast<DAS::Key>(_key);
if (!key.has_value()) { continue; }
configs[key.value()] = value.get<Config>();
}
return std::move(configs);
}
//
//void to_json(json& j, const __KeyConfig& p) {
// j = json{{"name", p.name}, {"address", p.address}, {"age", p.age}};
//}
void from_json(const nlohmann::json& j, Config& p) {
if (j.contains("passive")) {
j.at("passive").get_to(p.passive);
}
if (j.contains("active")) {
j.at("active").get_to(p.active);
}
}
void from_json(const nlohmann::json& j, KeyConfig& p) {
p[{}] = {DAS::Effect::PASSIVE_NONE, 0, 0, 0};
for (auto& [_desiredState, cf] : j.items()) {
auto _state = split(_desiredState, '+');
std::bitset<5> desiredState;
desiredState[0] = std::find(_state.cbegin(), _state.cend(), "NUM_LOCK") != _state.cend();
desiredState[1] = std::find(_state.cbegin(), _state.cend(), "CAPS_LOCK") != _state.cend();
desiredState[2] = std::find(_state.cbegin(), _state.cend(), "CONTROL") != _state.cend();
desiredState[3] = std::find(_state.cbegin(), _state.cend(), "MENU") != _state.cend();
desiredState[4] = std::find(_state.cbegin(), _state.cend(), "SHIFT") != _state.cend();
p[desiredState] = cf.get<EffectConfig>();
}
}
void from_json(const nlohmann::json& j, EffectConfig& p) {
auto effect = magic_enum::enum_cast<DAS::Effect>(j.at("effect").get<std::string>());
if (effect.has_value()) {
p.fx = effect.value();
} else {
p.fx = DAS::Effect::PASSIVE_SET;
}
j.at("r").get_to(p.r);
j.at("g").get_to(p.g);
j.at("b").get_to(p.b);
}
bool operator==(const Config &lhs, const Config &rhs) {
return lhs.passive == rhs.passive && lhs.active == rhs.active;
}
bool operator==(const EffectConfig& lhs, const EffectConfig& rhs ) {
return lhs.fx == rhs.fx &&
lhs.r == rhs.r &&
lhs.g == rhs.g &&
lhs.b == rhs.b;
}
std::vector<std::string> split(const std::string& str, char delim) {
std::vector<std::string> strings;
size_t start;
size_t end = 0;
while ((start = str.find_first_not_of(delim, end)) != std::string::npos) {
end = str.find(delim, start);
strings.push_back(str.substr(start, end - start));
}
return strings;
}
//template<DAS::Effect>
//DAS::Effect nlohmann::basic_json::get() const {
//
// return JSONSerializer<ValueType>::from_json(*this);
//}