Skip to content

Commit

Permalink
Merge pull request #187 from xmoto/fixes
Browse files Browse the repository at this point in the history
Fix minor input related bugs
  • Loading branch information
Nikekson committed Apr 25, 2024
2 parents 4ffa82f + e0ddb96 commit efb995e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 27 deletions.
36 changes: 21 additions & 15 deletions src/states/StateOptions.cpp
Expand Up @@ -2866,14 +2866,14 @@ void StateOptions::updateControlsList() {

// player script keys
for (unsigned int k = 0; k < MAX_SCRIPT_KEY_HOOKS; k++) {
std::ostringstream v_k;
v_k << (k + 1);

p = pList->addEntry(GAMETEXT_SCRIPTACTION + std::string(" ") + v_k.str());
p->Text.push_back(
Input::instance()->getSCRIPTACTION(i, k).toFancyString());
p->Text.push_back(
Input::instance()->getSCRIPTACTION(i, k).toString());
std::ostringstream keyName;
keyName
<< GAMETEXT_SCRIPTACTION
<< " "
<< (k + 1);
p = pList->addEntry(keyName.str());
p->Text.push_back(Input::instance()->getSCRIPTACTION(i, k).toFancyString());
p->Text.push_back(Input::instance()->getSCRIPTACTION(i, k).toString());
}
}
}
Expand Down Expand Up @@ -3236,22 +3236,28 @@ void StateOptions::sendFromMessageBox(const std::string &i_id,
void StateOptions::setInputKey(const std::string &i_strKey,
const std::string &i_key) {
for (int i = 0; i < INPUT_NB_PLAYERS; i++) {
std::ostringstream v_n;
v_n << " " << (i + 1);
std::string playerNumber = std::to_string(i + 1);

// player keys
for (unsigned int j = 0; j < INPUT_NB_PLAYERKEYS; j++) {
if (i_strKey ==
Input::instance()->getPlayerKeyHelp(j, i) + v_n.str()) {
std::string keyName = Input::instance()->getPlayerKeyHelp(j, i) + " " + playerNumber;

if (i_strKey == keyName) {
Input::instance()->setPlayerKey(j, i, XMKey(i_key));
}
}

// player script keys
for (unsigned int k = 0; k < MAX_SCRIPT_KEY_HOOKS; k++) {
std::ostringstream v_k;
v_k << (k + 1);
if (i_strKey == GAMETEXT_SCRIPTACTION + v_n.str() + " " + v_k.str()) {
std::ostringstream keyName;
keyName
<< GAMETEXT_SCRIPTACTION
<< " "
<< (k + 1)
<< " "
<< playerNumber;

if (i_strKey == keyName.str()) {
Input::instance()->setSCRIPTACTION(i, k, XMKey(i_key));
}
}
Expand Down
28 changes: 16 additions & 12 deletions src/xmoto/input/Input.cpp
Expand Up @@ -95,14 +95,13 @@ void Input::loadConfig(UserConfig *pConfig,

/* Get settings for mode */
for (unsigned int player = 0; player < INPUT_NB_PLAYERS; player++) {
std::ostringstream v_n;
v_n << (player + 1);
std::string playerNumber = std::to_string(player + 1);

for (IFullKey &f : m_controls[player].playerKeys) {
try {
f.key =
XMKey(pDb->config_getString(i_id_profile,
f.name + v_n.str(),
f.name + playerNumber,
f.key.toString()));
} catch (InvalidSystemKeyException &e) {
/* keep default key */
Expand All @@ -117,11 +116,9 @@ void Input::loadConfig(UserConfig *pConfig,
// script keys
for (unsigned int k = 0; k < MAX_SCRIPT_KEY_HOOKS; k++) {
IFullKey &f = m_controls[player].scriptActionKeys[k];
std::ostringstream v_k;
v_k << (k);

v_key = pDb->config_getString(
i_id_profile, "KeyActionScript" + v_n.str() + "_" + v_k.str(), "");
i_id_profile, "KeyActionScript" + playerNumber + "_" + std::to_string(k), "");
if (v_key != "") { // don't override the default key if there is nothing
// in the config
try {
Expand Down Expand Up @@ -324,22 +321,27 @@ void Input::saveConfig(UserConfig *pConfig,

// player keys
for (unsigned int j = 0; j < INPUT_NB_PLAYERKEYS; j++) {
if (m_controls[i].playerKeys[j].key.isDefined()) {
auto &playerKey = m_controls[i].playerKeys[j];
auto &key = playerKey.key;

if (key.isDefined() || isANotGameSetKey(&key)) {
pDb->config_setString(i_id_profile,
m_controls[i].playerKeys[j].name + v_n.str(),
m_controls[i].playerKeys[j].key.toString());
playerKey.name + v_n.str(),
key.toString());
}
}

// script keys
for (unsigned int k = 0; k < MAX_SCRIPT_KEY_HOOKS; k++) {
if (m_controls[i].scriptActionKeys[k].key.isDefined()) {
auto &key = m_controls[i].scriptActionKeys[k].key;

if (key.isDefined() || isANotGameSetKey(&key)) {
std::ostringstream v_k;
v_k << (k);

pDb->config_setString(i_id_profile,
"KeyActionScript" + v_n.str() + "_" + v_k.str(),
m_controls[i].scriptActionKeys[k].key.toString());
key.toString());
}
}
}
Expand Down Expand Up @@ -402,10 +404,12 @@ bool Input::isANotGameSetKey(XMKey *i_xmkey) const {
}

for (unsigned int k = 0; k < MAX_SCRIPT_KEY_HOOKS; k++) {
if (m_controls[i].scriptActionKeys[k].key == *i_xmkey)
if (m_controls[i].scriptActionKeys[k].key == *i_xmkey) {
return false;
}
}
}

return true;
}

Expand Down

0 comments on commit efb995e

Please sign in to comment.