Skip to content

Commit

Permalink
KeyMappings tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
past-due authored and pull[bot] committed Apr 20, 2024
1 parent a2114a9 commit 1111230
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
21 changes: 12 additions & 9 deletions src/input/mapping.cpp
Expand Up @@ -101,7 +101,7 @@ bool KeyMapping::hasMeta() const
return keys.meta != KEY_CODE::KEY_IGNORE;
}

bool KeyMapping::toString(char* pOutStr) const
std::string KeyMapping::toString() const
{
// Figure out if the keycode is for mouse or keyboard and print the name of
// the respective key/mouse button to `asciiSub`
Expand All @@ -115,23 +115,21 @@ bool KeyMapping::toString(char* pOutStr) const
mouseKeyCodeToString(keys.input.value.mouseKeyCode, (char*)&asciiSub, 20);
break;
default:
strcpy(asciiSub, "NOT VALID");
debug(LOG_WZ, "Encountered invalid key mapping source %u while converting mapping to string!", static_cast<unsigned int>(keys.input.source));
return true;
return std::string("NOT VALID");
}

if (hasMeta())
{
char asciiMeta[20] = "\0";
keyScanToString(keys.meta, (char*)&asciiMeta, 20);

sprintf(pOutStr, "%s %s", asciiMeta, asciiSub);
return astringf("%s %s", asciiMeta, asciiSub);
}
else
{
sprintf(pOutStr, "%s", asciiSub);
return std::string(asciiSub);
}
return true;
}


Expand Down Expand Up @@ -185,10 +183,10 @@ KeyMapping& KeyMappings::add(const KeyCombination keys, const KeyFunctionInfo& i
return keyMappings.back();
}

nonstd::optional<std::reference_wrapper<KeyMapping>> KeyMappings::get(const KeyFunctionInfo& info, const KeyMappingSlot slot)
nonstd::optional<std::reference_wrapper<KeyMapping>> KeyMappings::get(const std::string& name, const KeyMappingSlot slot)
{
auto mapping = std::find_if(keyMappings.begin(), keyMappings.end(), [&info, slot](const KeyMapping& mapping) {
return mapping.info.name == info.name && mapping.slot == slot;
auto mapping = std::find_if(keyMappings.begin(), keyMappings.end(), [&name, slot](const KeyMapping& mapping) {
return mapping.info.name == name && mapping.slot == slot;
});
if (mapping != keyMappings.end())
{
Expand All @@ -198,6 +196,11 @@ nonstd::optional<std::reference_wrapper<KeyMapping>> KeyMappings::get(const KeyF
return nonstd::nullopt;
}

nonstd::optional<std::reference_wrapper<KeyMapping>> KeyMappings::get(const KeyFunctionInfo& info, const KeyMappingSlot slot)
{
return get(info.name, slot);
}

std::vector<std::reference_wrapper<KeyMapping>> KeyMappings::find(const KEY_CODE meta, const KeyMappingInput input)
{
std::vector<std::reference_wrapper<KeyMapping>> matches;
Expand Down
3 changes: 2 additions & 1 deletion src/input/mapping.h
Expand Up @@ -45,7 +45,7 @@ struct KeyMapping

bool hasMeta() const;

bool toString(char* pOutStr) const;
std::string toString() const;
};

bool operator==(const KeyMapping& lhs, const KeyMapping& rhs);
Expand All @@ -58,6 +58,7 @@ class KeyMappings
KeyMapping& add(const KeyCombination keys, const KeyFunctionInfo& info, const KeyMappingSlot slot);

nonstd::optional<std::reference_wrapper<KeyMapping>> get(const KeyFunctionInfo& info, const KeyMappingSlot slot);
nonstd::optional<std::reference_wrapper<KeyMapping>> get(const std::string& name, const KeyMappingSlot slot);

/* Finds all mappings with matching meta and input */
std::vector<std::reference_wrapper<KeyMapping>> find(const KEY_CODE meta, const KeyMappingInput input);
Expand Down
15 changes: 7 additions & 8 deletions src/keyedit.cpp
Expand Up @@ -281,8 +281,7 @@ class KeyMapButton : public W_BUTTON

// Select label text and color
PIELIGHT bindingTextColor = WZCOL_FORM_TEXT;
char sPrimaryKey[MAX_STR_LENGTH];
sPrimaryKey[0] = '\0';
std::string sPrimaryKey;
const nonstd::optional<KeyMapping> mapping = targetFunctionData.mappings[static_cast<unsigned int>(slot)];
if (mapping && !mapping->keys.input.isCleared())
{
Expand All @@ -292,14 +291,14 @@ class KeyMapButton : public W_BUTTON
{
bindingTextColor = WZCOL_YELLOW;
}
mapping->toString(sPrimaryKey);
sPrimaryKey = mapping->toString();
}
else
{
sstrcpy(sPrimaryKey, info.type == KeyMappingType::ASSIGNABLE ? getNotBoundLabel().c_str() : getFixedKeyLabel().c_str());
sPrimaryKey = (info.type == KeyMappingType::ASSIGNABLE) ? getNotBoundLabel() : getFixedKeyLabel();
}

wzBindingText.setText(sPrimaryKey, iV_fonts::font_regular);
wzBindingText.setText(WzString::fromUtf8(sPrimaryKey), iV_fonts::font_regular);
wzBindingText.render(xPos, yPos + (h / 2) + 3, bindingTextColor);
}

Expand Down Expand Up @@ -514,10 +513,10 @@ static unsigned int getMaxKeyMapNameWidth(InputManager& inputManager)
if (maxKeyMapNameWidthDirty) {
max = static_cast<int>(iV_GetTextWidth(getNotBoundLabel().c_str(), iV_fonts::font_regular));

char sKey[MAX_STR_LENGTH];
std::string sKey;
for (const KeyMapping& mapping : getVisibleMappings(inputManager)) {
mapping.toString(sKey);
max = MAX(max, static_cast<int>(iV_GetTextWidth(sKey, iV_fonts::font_regular)));
sKey = mapping.toString();
max = MAX(max, static_cast<int>(iV_GetTextWidth(WzString::fromUtf8(sKey), iV_fonts::font_regular)));
}

maxKeyMapNameWidthDirty = false;
Expand Down

0 comments on commit 1111230

Please sign in to comment.