-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathjsonactioniohandler.inl
More file actions
159 lines (135 loc) · 5.91 KB
/
jsonactioniohandler.inl
File metadata and controls
159 lines (135 loc) · 5.91 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
template<class Action>
void JsonActionIOHandler<Action>::loadBindingsFile(const std::string& path, std::function<std::string(Action)> stringToAction)
{
mPrimaryBindings.clear();
mSecondaryBindings.clear();
std::ifstream file(path);
if(!file)
throw fea::InputFileNotFoundException("Error! Entity file not found: " + path + '\n');
Json::Value root;
Json::Reader reader;
reader.parse(file, root, false);
file.close();
std::string names[2] {"primary", "secondary"};
for(int i = 0; i < 2; i++)
{
const Json::Value& array = root[names[i]];
if(array.isArray())
{
for(const Json::Value& binding : array)
{
std::string type = binding["type"].asString();
ActionTrigger tempTrigger;
Action tempAction = stringToAction(binding["action"].asString());
// TODO: std::stoi(Json::Value.asString()) needed?
if(type == "keypress")
{
tempTrigger.type = ActionTrigger::KEYPRESS;
tempTrigger.keyCode = (Keyboard::Code) std::stoi(binding["key"].asString());
}
else if(type == "keyrelease")
{
tempTrigger.type = ActionTrigger::KEYRELEASE;
tempTrigger.keyCode = (Keyboard::Code) std::stoi(binding["key"].asString());
}
else if(type == "mousepress")
{
tempTrigger.type = ActionTrigger::MOUSEPRESS;
tempTrigger.mouseButton = (Mouse::Button) std::stoi(binding["mousebutton"].asString());
}
else if(type == "mouserelease")
{
tempTrigger.type = ActionTrigger::MOUSERELEASE;
tempTrigger.mouseButton = (Mouse::Button) std::stoi(binding["mousebutton"].asString());
}
else if(type == "gamepadpress")
{
tempTrigger.type = ActionTrigger::GAMEPADPRESS;
tempTrigger.gamepadButton = (uint32_t) std::stoi(binding["gamepadbutton"].asString());
tempTrigger.gamepadId = (uint32_t) std::stoi(binding["gamepadid"].asString());
}
else if(type == "gamepadrelease")
{
tempTrigger.type = ActionTrigger::GAMEPADRELEASE;
tempTrigger.gamepadButton = (uint32_t) std::stoi(binding["gamepadbutton"].asString());
tempTrigger.gamepadId = (uint32_t) std::stoi(binding["gamepadid"].asString());
}
mPrimaryBindings.emplace(tempTrigger, tempAction);
}
}
}
}
template<class Action>
void JsonActionIOHandler<Action>::saveBindingsFile(const std::string& path, const std::map<ActionTrigger, Action>& primaryActions) const
{
saveBindingsFile(path, primaryActions, std::map<ActionTrigger, Action>());
}
template<class Action>
void JsonActionIOHandler<Action>::saveBindingsFile(const std::string& path, const std::map<ActionTrigger, Action>& primaryActions, const std::map<ActionTrigger, Action>& secondaryActions) const
{
std::ofstream file(path);
if(!file)
throw fea::InputFileNotFoundException("Error! Action bindings file not found: " + path + '\n');
Json::Value root;
const std::map<ActionTrigger, Action>* actions[2] {&primaryActions, &secondaryActions};
Json::Value arrays[2];
std::string names[2] {"primary", "secondary"};
for(int i = 0; i < 2; i++)
{
for(auto binding : *actions[i])
{
Json::Value bindingEntry;
switch(binding.first.type)
{
case ActionTrigger::KEYPRESS:
bindingEntry["type"] = "keypress";
bindingEntry["key"] = std::to_string(binding.first.keyCode);
break;
case ActionTrigger::KEYRELEASE:
bindingEntry["type"] = "keyrelease";
bindingEntry["key"] = std::to_string(binding.first.keyCode);
break;
case ActionTrigger::MOUSEPRESS:
bindingEntry["type"] = "mousepress";
bindingEntry["mousebutton"] = std::to_string(binding.first.mouseButton);
break;
case ActionTrigger::MOUSERELEASE:
bindingEntry["type"] = "mouserelease";
bindingEntry["mousebutton"] = std::to_string(binding.first.mouseButton);
break;
case ActionTrigger::GAMEPADPRESS:
bindingEntry["type"] = "gamepadpress";
bindingEntry["gamepadid"] = std::to_string(binding.first.gamepadId);
bindingEntry["gamepadbutton"] = std::to_string(binding.first.gamepadButton);
break;
case ActionTrigger::GAMEPADRELEASE:
bindingEntry["type"] = "gamepadrelease";
bindingEntry["gamepadid"] = std::to_string(binding.first.gamepadId);
bindingEntry["gamepadbutton"] = std::to_string(binding.first.gamepadButton);
break;
default:
break;
}
std::stringstream ss;
ss << binding.second;
bindingEntry["action"] = ss.str();
arrays[i].append(bindingEntry);
}
if(!actions[i]->empty())
root[names[i]] = arrays[i];
}
// TODO: use FastWriter or StyledWriter?
Json::FastWriter writer;
file << writer.write(root);
file.close();
}
template<class Action>
const std::map<ActionTrigger, Action>& JsonActionIOHandler<Action>::getPrimaryBindings() const
{
return mPrimaryBindings;
}
template<class Action>
const std::map<ActionTrigger, Action>& JsonActionIOHandler<Action>::getSecondaryBindings() const
{
return mSecondaryBindings;
}