forked from Aloshi/EmulationStation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputConfig.cpp
More file actions
184 lines (154 loc) · 4.03 KB
/
InputConfig.cpp
File metadata and controls
184 lines (154 loc) · 4.03 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "InputConfig.h"
#include <string>
#include <algorithm>
#include <SDL.h>
#include <iostream>
#include "Log.h"
//some util functions
std::string inputTypeToString(InputType type)
{
switch(type)
{
case TYPE_AXIS:
return "axis";
case TYPE_BUTTON:
return "button";
case TYPE_HAT:
return "hat";
case TYPE_KEY:
return "key";
default:
return "error";
}
}
InputType stringToInputType(const std::string& type)
{
if(type == "axis")
return TYPE_AXIS;
if(type == "button")
return TYPE_BUTTON;
if(type == "hat")
return TYPE_HAT;
if(type == "key")
return TYPE_KEY;
return TYPE_COUNT;
}
std::string toLower(std::string str)
{
for(unsigned int i = 0; i < str.length(); i++)
{
str[i] = tolower(str[i]);
}
return str;
}
//end util functions
InputConfig::InputConfig(int deviceId) : mDeviceId(deviceId)
{
mPlayerNum = -1;
}
void InputConfig::clear()
{
mNameMap.clear();
}
void InputConfig::mapInput(const std::string& name, Input input)
{
mNameMap[toLower(name)] = input;
}
Input InputConfig::getInputByName(const std::string& name)
{
return mNameMap[toLower(name)];
}
bool InputConfig::isMappedTo(const std::string& name, Input input)
{
Input comp = getInputByName(name);
if(comp.configured && comp.type == input.type && comp.id == input.id)
{
if(comp.type == TYPE_HAT)
{
return (input.value == 0 || input.value & comp.value);
}
if(comp.type == TYPE_AXIS)
{
return input.value == 0 || comp.value == input.value;
}else{
return true;
}
}
return false;
}
std::vector<std::string> InputConfig::getMappedTo(Input input)
{
std::vector<std::string> maps;
typedef std::map<std::string, Input>::iterator it_type;
for(it_type iterator = mNameMap.begin(); iterator != mNameMap.end(); iterator++)
{
Input chk = iterator->second;
if(!chk.configured)
continue;
if(chk.device == input.device && chk.type == input.type && chk.id == input.id)
{
if(chk.type == TYPE_HAT)
{
if(input.value == 0 || input.value & chk.value)
{
maps.push_back(iterator->first);
}
continue;
}
if(input.type == TYPE_AXIS)
{
if(input.value == 0 || chk.value == input.value)
maps.push_back(iterator->first);
}else{
maps.push_back(iterator->first);
}
}
}
return maps;
}
void InputConfig::loadFromXML(pugi::xml_node node, int playerNum)
{
this->clear();
setPlayerNum(playerNum);
for(pugi::xml_node input = node.child("input"); input; input = input.next_sibling("input"))
{
std::string name = input.attribute("name").as_string();
std::string type = input.attribute("type").as_string();
InputType typeEnum = stringToInputType(type);
if(typeEnum == TYPE_COUNT)
{
LOG(LogError) << "InputConfig load error - input of type \"" << type << "\" is invalid! Skipping input \"" << name << "\".\n";
continue;
}
int id = input.attribute("id").as_int();
int value = input.attribute("value").as_int();
if(value == 0)
LOG(LogWarning) << "WARNING: InputConfig value is 0 for " << type << " " << id << "!\n";
mNameMap[toLower(name)] = Input(mDeviceId, typeEnum, id, value, true);
}
}
void InputConfig::writeToXML(pugi::xml_node parent)
{
pugi::xml_node cfg = parent.append_child("inputConfig");
if(mDeviceId == DEVICE_KEYBOARD)
{
cfg.append_attribute("type") = "keyboard";
}else{
cfg.append_attribute("type") = "joystick";
cfg.append_attribute("deviceName") = SDL_JoystickName(mDeviceId);
}
typedef std::map<std::string, Input>::iterator it_type;
for(it_type iterator = mNameMap.begin(); iterator != mNameMap.end(); iterator++)
{
if(!iterator->second.configured)
continue;
pugi::xml_node input = cfg.append_child("input");
input.append_attribute("name") = iterator->first.c_str();
input.append_attribute("type") = inputTypeToString(iterator->second.type).c_str();
input.append_attribute("id").set_value(iterator->second.id);
input.append_attribute("value").set_value(iterator->second.value);
}
}
void InputConfig::setPlayerNum(int num) { mPlayerNum = num; }
int InputConfig::getPlayerNum() { return mPlayerNum; }
int InputConfig::getDeviceId() { return mDeviceId; }