Skip to content

Commit 1111230

Browse files
past-duepull[bot]
authored andcommittedApr 20, 2024
KeyMappings tweaks
1 parent a2114a9 commit 1111230

File tree

3 files changed

+21
-18
lines changed

3 files changed

+21
-18
lines changed
 

‎src/input/mapping.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ bool KeyMapping::hasMeta() const
101101
return keys.meta != KEY_CODE::KEY_IGNORE;
102102
}
103103

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

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

128-
sprintf(pOutStr, "%s %s", asciiMeta, asciiSub);
127+
return astringf("%s %s", asciiMeta, asciiSub);
129128
}
130129
else
131130
{
132-
sprintf(pOutStr, "%s", asciiSub);
131+
return std::string(asciiSub);
133132
}
134-
return true;
135133
}
136134

137135

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

188-
nonstd::optional<std::reference_wrapper<KeyMapping>> KeyMappings::get(const KeyFunctionInfo& info, const KeyMappingSlot slot)
186+
nonstd::optional<std::reference_wrapper<KeyMapping>> KeyMappings::get(const std::string& name, const KeyMappingSlot slot)
189187
{
190-
auto mapping = std::find_if(keyMappings.begin(), keyMappings.end(), [&info, slot](const KeyMapping& mapping) {
191-
return mapping.info.name == info.name && mapping.slot == slot;
188+
auto mapping = std::find_if(keyMappings.begin(), keyMappings.end(), [&name, slot](const KeyMapping& mapping) {
189+
return mapping.info.name == name && mapping.slot == slot;
192190
});
193191
if (mapping != keyMappings.end())
194192
{
@@ -198,6 +196,11 @@ nonstd::optional<std::reference_wrapper<KeyMapping>> KeyMappings::get(const KeyF
198196
return nonstd::nullopt;
199197
}
200198

199+
nonstd::optional<std::reference_wrapper<KeyMapping>> KeyMappings::get(const KeyFunctionInfo& info, const KeyMappingSlot slot)
200+
{
201+
return get(info.name, slot);
202+
}
203+
201204
std::vector<std::reference_wrapper<KeyMapping>> KeyMappings::find(const KEY_CODE meta, const KeyMappingInput input)
202205
{
203206
std::vector<std::reference_wrapper<KeyMapping>> matches;

‎src/input/mapping.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ struct KeyMapping
4545

4646
bool hasMeta() const;
4747

48-
bool toString(char* pOutStr) const;
48+
std::string toString() const;
4949
};
5050

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

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

6263
/* Finds all mappings with matching meta and input */
6364
std::vector<std::reference_wrapper<KeyMapping>> find(const KEY_CODE meta, const KeyMappingInput input);

‎src/keyedit.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,7 @@ class KeyMapButton : public W_BUTTON
281281

282282
// Select label text and color
283283
PIELIGHT bindingTextColor = WZCOL_FORM_TEXT;
284-
char sPrimaryKey[MAX_STR_LENGTH];
285-
sPrimaryKey[0] = '\0';
284+
std::string sPrimaryKey;
286285
const nonstd::optional<KeyMapping> mapping = targetFunctionData.mappings[static_cast<unsigned int>(slot)];
287286
if (mapping && !mapping->keys.input.isCleared())
288287
{
@@ -292,14 +291,14 @@ class KeyMapButton : public W_BUTTON
292291
{
293292
bindingTextColor = WZCOL_YELLOW;
294293
}
295-
mapping->toString(sPrimaryKey);
294+
sPrimaryKey = mapping->toString();
296295
}
297296
else
298297
{
299-
sstrcpy(sPrimaryKey, info.type == KeyMappingType::ASSIGNABLE ? getNotBoundLabel().c_str() : getFixedKeyLabel().c_str());
298+
sPrimaryKey = (info.type == KeyMappingType::ASSIGNABLE) ? getNotBoundLabel() : getFixedKeyLabel();
300299
}
301300

302-
wzBindingText.setText(sPrimaryKey, iV_fonts::font_regular);
301+
wzBindingText.setText(WzString::fromUtf8(sPrimaryKey), iV_fonts::font_regular);
303302
wzBindingText.render(xPos, yPos + (h / 2) + 3, bindingTextColor);
304303
}
305304

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

517-
char sKey[MAX_STR_LENGTH];
516+
std::string sKey;
518517
for (const KeyMapping& mapping : getVisibleMappings(inputManager)) {
519-
mapping.toString(sKey);
520-
max = MAX(max, static_cast<int>(iV_GetTextWidth(sKey, iV_fonts::font_regular)));
518+
sKey = mapping.toString();
519+
max = MAX(max, static_cast<int>(iV_GetTextWidth(WzString::fromUtf8(sKey), iV_fonts::font_regular)));
521520
}
522521

523522
maxKeyMapNameWidthDirty = false;

0 commit comments

Comments
 (0)