Updated controls.conf to allow multiple keys/buttons for each input

This commit is contained in:
catelite 2017-01-22 17:20:06 -08:00
parent 4af4d8fa2b
commit 7564e317af
2 changed files with 129 additions and 109 deletions

View File

@ -29,7 +29,6 @@ UserInput::UserInput(Configuration &c)
{ {
for(unsigned int i = 0; i < KeyCodeMax; ++i) for(unsigned int i = 0; i < KeyCodeMax; ++i)
{ {
keyHandlers_[i] = NULL;
currentKeyState_[i] = false; currentKeyState_[i] = false;
lastKeyState_[i] = false; lastKeyState_[i] = false;
} }
@ -114,40 +113,52 @@ bool UserInput::MapKey(std::string keyDescription, KeyCode_E key, bool required)
return false; return false;
} }
description = Utils::replace(description, " ", "");
description = Utils::replace(description, ".", "");
scanCode = SDL_GetScancodeFromName(description.c_str()); std::istringstream ss(description);
std::string token;
if(scanCode != SDL_SCANCODE_UNKNOWN) bool success = true;
while (std::getline(ss, token, ','))
{
scanCode = SDL_GetScancodeFromName(token.c_str());
bool found = false;
if (scanCode != SDL_SCANCODE_UNKNOWN)
{ {
Logger::write(Logger::ZONE_INFO, "Input", "Binding key " + configKey); Logger::write(Logger::ZONE_INFO, "Input", "Binding key " + configKey);
keyHandlers_[key] = new KeyboardHandler(scanCode); keyHandlers_.push_back(std::pair<InputHandler *, KeyCode_E>(new KeyboardHandler(scanCode), key));
return true; found = true;
} }
else
description = Utils::toLower(description);
if(description.find("mouse") == 0)
{ {
std::string mousedesc = Utils::replace(Utils::toLower(description), "mouse", ""); token = Utils::toLower(token);
if(mousedesc.find("button") == 0)
if (token.find("mouse") == 0)
{
std::string mousedesc = Utils::replace(Utils::toLower(token), "mouse", "");
if (mousedesc.find("button") == 0)
{ {
int button = 0; int button = 0;
std::stringstream ss; std::stringstream ss;
mousedesc = Utils::replace(mousedesc, "button", ""); mousedesc = Utils::replace(mousedesc, "button", "");
if(mousedesc == "left") button = SDL_BUTTON_LEFT; if (mousedesc == "left") button = SDL_BUTTON_LEFT;
else if(mousedesc == "middle") button = SDL_BUTTON_MIDDLE; else if (mousedesc == "middle") button = SDL_BUTTON_MIDDLE;
else if(mousedesc == "right") button = SDL_BUTTON_RIGHT; else if (mousedesc == "right") button = SDL_BUTTON_RIGHT;
else if(mousedesc == "x1") button = SDL_BUTTON_X1; else if (mousedesc == "x1") button = SDL_BUTTON_X1;
else if(mousedesc == "x2") button = SDL_BUTTON_X2; else if (mousedesc == "x2") button = SDL_BUTTON_X2;
keyHandlers_[key] = new MouseButtonHandler(button); keyHandlers_.push_back(std::pair<InputHandler *, KeyCode_E>(new MouseButtonHandler(button), key));
Logger::write(Logger::ZONE_INFO, "Input", "Binding mouse button " + ss.str() ); Logger::write(Logger::ZONE_INFO, "Input", "Binding mouse button " + ss.str());
return true; found = true;
} }
} }
else if(description.find("joy") == 0) else if (token.find("joy") == 0)
{ {
std::string joydesc = Utils::replace(Utils::toLower(description), "joy", ""); std::string joydesc = Utils::replace(Utils::toLower(token), "joy", "");
std::stringstream ssjoy; std::stringstream ssjoy;
ssjoy << joydesc.at(0); ssjoy << joydesc.at(0);
int joynum; int joynum;
@ -155,17 +166,17 @@ bool UserInput::MapKey(std::string keyDescription, KeyCode_E key, bool required)
joydesc = joydesc.erase(0, 1); joydesc = joydesc.erase(0, 1);
if(joydesc.find("button") == 0) if (joydesc.find("button") == 0)
{ {
unsigned int button; unsigned int button;
std::stringstream ss; std::stringstream ss;
ss << Utils::replace(joydesc, "button", ""); ss << Utils::replace(joydesc, "button", "");
ss >> button; ss >> button;
keyHandlers_[key] = new JoyButtonHandler(joynum, button); keyHandlers_.push_back(std::pair<InputHandler *, KeyCode_E>(new JoyButtonHandler(joynum, button), key));
Logger::write(Logger::ZONE_INFO, "Input", "Binding joypad button " + ss.str() ); Logger::write(Logger::ZONE_INFO, "Input", "Binding joypad button " + ss.str());
return true; found = true;
} }
else if(joydesc.find("hat") == 0) else if (joydesc.find("hat") == 0)
{ {
Uint8 hat; Uint8 hat;
@ -176,21 +187,21 @@ bool UserInput::MapKey(std::string keyDescription, KeyCode_E key, bool required)
sshat >> hatnum; sshat >> hatnum;
joydesc = joydesc.erase(0, 1); joydesc = joydesc.erase(0, 1);
if(joydesc == "leftup") hat = SDL_HAT_LEFTUP; if (joydesc == "leftup") hat = SDL_HAT_LEFTUP;
else if(joydesc == "left") hat = SDL_HAT_LEFT; else if (joydesc == "left") hat = SDL_HAT_LEFT;
else if(joydesc == "leftdown") hat = SDL_HAT_LEFTDOWN; else if (joydesc == "leftdown") hat = SDL_HAT_LEFTDOWN;
else if(joydesc == "up") hat = SDL_HAT_UP; else if (joydesc == "up") hat = SDL_HAT_UP;
//else if(joydesc == "centered") hat = SDL_HAT_CENTERED; //else if(joydesc == "centered") hat = SDL_HAT_CENTERED;
else if(joydesc == "down") hat = SDL_HAT_DOWN; else if (joydesc == "down") hat = SDL_HAT_DOWN;
else if(joydesc == "rightup") hat = SDL_HAT_RIGHTUP; else if (joydesc == "rightup") hat = SDL_HAT_RIGHTUP;
else if(joydesc == "right") hat = SDL_HAT_RIGHT; else if (joydesc == "right") hat = SDL_HAT_RIGHT;
else if(joydesc == "rightdown") hat = SDL_HAT_RIGHTDOWN; else if (joydesc == "rightdown") hat = SDL_HAT_RIGHTDOWN;
keyHandlers_[key] = new JoyHatHandler(joynum, hatnum, hat); keyHandlers_.push_back(std::pair<InputHandler *, KeyCode_E>(new JoyHatHandler(joynum, hatnum, hat), key));
Logger::write(Logger::ZONE_INFO, "Input", "Binding joypad hat " + joydesc ); Logger::write(Logger::ZONE_INFO, "Input", "Binding joypad hat " + joydesc);
return true; found = true;
} }
else if(joydesc.find("axis") == 0) else if (joydesc.find("axis") == 0)
{ {
// string is now axis0+ // string is now axis0+
unsigned int axis; unsigned int axis;
@ -200,19 +211,19 @@ bool UserInput::MapKey(std::string keyDescription, KeyCode_E key, bool required)
joydesc = Utils::replace(joydesc, "axis", ""); joydesc = Utils::replace(joydesc, "axis", "");
if(!config_.getProperty("controls.deadZone", deadZone)) if (!config_.getProperty("controls.deadZone", deadZone))
{ {
deadZone = 3; deadZone = 3;
} }
// string is now 0+ // string is now 0+
if(joydesc.find("-") != std::string::npos) if (joydesc.find("-") != std::string::npos)
{ {
min = -32768; min = -32768;
max = -32768 / 100 * deadZone; max = -32768 / 100 * deadZone;
joydesc = Utils::replace(joydesc, "-", ""); joydesc = Utils::replace(joydesc, "-", "");
} }
else if(joydesc.find("+") != std::string::npos) else if (joydesc.find("+") != std::string::npos)
{ {
min = 32767 / 100 * deadZone; min = 32767 / 100 * deadZone;
max = 32767; max = 32767;
@ -224,23 +235,31 @@ bool UserInput::MapKey(std::string keyDescription, KeyCode_E key, bool required)
ss << joydesc; ss << joydesc;
ss >> axis; ss >> axis;
Logger::write(Logger::ZONE_INFO, "Input", "Binding joypad axis " + ss.str()); Logger::write(Logger::ZONE_INFO, "Input", "Binding joypad axis " + ss.str());
keyHandlers_[key] = new JoyAxisHandler(joynum, axis, min, max); keyHandlers_.push_back(std::pair<InputHandler *, KeyCode_E>(new JoyAxisHandler(joynum, axis, min, max), key));
return true; found = true;
} }
} }
Logger::write(Logger::ZONE_ERROR, "Input", "Unsupported property value for " + configKey + "(" + description + "). See Documentation/Keycodes.txt for valid inputs");
return false; if (!found)
{
Logger::write(Logger::ZONE_ERROR, "Input", "Unsupported property value for " + configKey + "(" + token + "). See Documentation/Keycodes.txt for valid inputs");
success = false;
}
}
}
return success;
} }
void UserInput::resetStates() void UserInput::resetStates()
{ {
for(unsigned int i = 0; i < KeyCodeMax; ++i) for (unsigned int i = 0; i < keyHandlers_.size(); ++i)
{ {
if(keyHandlers_[i]) if (keyHandlers_[i].first)
{ {
keyHandlers_[i]->reset(); keyHandlers_[i].first->reset();
} }
currentKeyState_[i] = false; currentKeyState_[keyHandlers_[i].second] = false;
} }
} }
@ -249,15 +268,16 @@ bool UserInput::update(SDL_Event &e)
bool updated = false; bool updated = false;
memcpy(lastKeyState_, currentKeyState_, sizeof(lastKeyState_)); memcpy(lastKeyState_, currentKeyState_, sizeof(lastKeyState_));
memset(currentKeyState_, 0, sizeof(currentKeyState_));
for(unsigned int i = 0; i < KeyCodeMax; ++i) for(unsigned int i = 0; i < KeyCodeMax; ++i)
{ {
InputHandler *h = keyHandlers_[i]; InputHandler *h = keyHandlers_[i].first;
if(h) if(h)
{ {
if(h->update(e)) updated = true; if(h->update(e)) updated = true;
currentKeyState_[i] = h->pressed(); currentKeyState_[keyHandlers_[i].second] |= h->pressed();
} }
} }

View File

@ -64,7 +64,7 @@ private:
bool MapKey(std::string keyDescription, KeyCode_E key, bool required); bool MapKey(std::string keyDescription, KeyCode_E key, bool required);
Configuration &config_; Configuration &config_;
std::vector<SDL_Joystick *> joysticks_; std::vector<SDL_Joystick *> joysticks_;
InputHandler *keyHandlers_[KeyCodeMax]; std::vector<std::pair<InputHandler *, KeyCode_E>> keyHandlers_;
bool lastKeyState_[KeyCodeMax]; bool lastKeyState_[KeyCodeMax];
bool currentKeyState_[KeyCodeMax]; bool currentKeyState_[KeyCodeMax];
}; };