diff options
author | Thorbjørn Lindeijer <bjorn@lindeijer.nl> | 2025-03-08 01:07:24 +0100 |
---|---|---|
committer | Thorbjørn Lindeijer <bjorn@lindeijer.nl> | 2025-03-11 09:52:12 +0000 |
commit | bde9d2d83dc703dd961861c3e6705bca28303cdb (patch) | |
tree | 3f9913970f491334840a516850f4519c61a0c87e /src | |
parent | b69210ddbf1e989e0093d40c8a5c1a6d9bb16a50 (diff) | |
download | mana-bde9d2d83dc703dd961861c3e6705bca28303cdb.tar.gz mana-bde9d2d83dc703dd961861c3e6705bca28303cdb.tar.bz2 mana-bde9d2d83dc703dd961861c3e6705bca28303cdb.tar.xz mana-bde9d2d83dc703dd961861c3e6705bca28303cdb.zip |
Implemented replacement of key names in NPC dialog
Now when NPCs text includes "###MoveUp;", it will get replaced by "Up",
or whatever key is currently bound to that action.
For compatibility reasons the key name can optionally have a "key"
prefix, for example "###keyMoveUp;".
Closes #73
Diffstat (limited to 'src')
-rw-r--r-- | src/gui/npcdialog.cpp | 1 | ||||
-rw-r--r-- | src/gui/widgets/browserbox.cpp | 36 | ||||
-rw-r--r-- | src/gui/widgets/browserbox.h | 6 | ||||
-rw-r--r-- | src/keyboardconfig.cpp | 31 | ||||
-rw-r--r-- | src/keyboardconfig.h | 5 |
5 files changed, 66 insertions, 13 deletions
diff --git a/src/gui/npcdialog.cpp b/src/gui/npcdialog.cpp index 18b3ff1b..1df40d4f 100644 --- a/src/gui/npcdialog.cpp +++ b/src/gui/npcdialog.cpp @@ -89,6 +89,7 @@ NpcDialog::NpcDialog(int npcId) mTextBox->setWrapIndent(15); mTextBox->setFrameSize(2); mTextBox->setLinkHandler(mItemLinkHandler.get()); + mTextBox->setEnableKeys(true); mScrollArea = new ScrollArea(mTextBox); mScrollArea->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER); diff --git a/src/gui/widgets/browserbox.cpp b/src/gui/widgets/browserbox.cpp index d27e843c..fd777133 100644 --- a/src/gui/widgets/browserbox.cpp +++ b/src/gui/widgets/browserbox.cpp @@ -26,6 +26,7 @@ #include "gui/truetypefont.h" #include "gui/widgets/linkhandler.h" +#include "keyboardconfig.h" #include "resources/itemdb.h" #include "resources/iteminfo.h" #include "resources/theme.h" @@ -38,6 +39,38 @@ #include <algorithm> +/** + * Check for key replacements in format "###key;" + */ +static void replaceKeys(std::string &text) +{ + auto keyStart = text.find("###"); + + while (keyStart != std::string::npos) + { + const auto keyEnd = text.find(";", keyStart + 3); + if (keyEnd == std::string::npos) + break; + + std::string_view key(text.data() + keyStart + 3, keyEnd - keyStart - 3); + + // Remove "key" prefix + if (key.size() > 3 && key.substr(0, 3) == "key") + key.remove_prefix(3); + + const auto keyName = keyboard.getKeyName(key); + if (!keyName.empty()) + { + text.replace(keyStart, keyEnd - keyStart + 1, keyName); + keyStart = text.find("###", keyStart + keyName.size()); + } + else + { + keyStart = text.find("###", keyEnd + 1); + } + } +} + struct LayoutContext { LayoutContext(gcn::Font *font); @@ -136,6 +169,9 @@ void BrowserBox::addRow(std::string_view row) newRow.text = row; } + if (mEnableKeys) + replaceKeys(newRow.text); + // Layout the newly added row LayoutContext context(getFont()); context.y = getHeight(); diff --git a/src/gui/widgets/browserbox.h b/src/gui/widgets/browserbox.h index d327b533..7066585d 100644 --- a/src/gui/widgets/browserbox.h +++ b/src/gui/widgets/browserbox.h @@ -117,6 +117,11 @@ class BrowserBox : public gcn::Widget, void disableLinksAndUserColors() { mUseLinksAndUserColors = false; } /** + * Enable or disable the replacement of keys. + */ + void setEnableKeys(bool enable) { mEnableKeys = enable; } + + /** * Adds one or more text rows to the browser, separated by '\n'. */ void addRows(std::string_view rows); @@ -196,6 +201,7 @@ class BrowserBox : public gcn::Widget, bool mShadows = false; bool mOutline = false; bool mUseLinksAndUserColors = true; + bool mEnableKeys = false; std::optional<BrowserLink> mHoveredLink; unsigned int mMaxRows = 0; int mLastLayoutWidth = 0; diff --git a/src/keyboardconfig.cpp b/src/keyboardconfig.cpp index c4d7a26d..ec5b5e37 100644 --- a/src/keyboardconfig.cpp +++ b/src/keyboardconfig.cpp @@ -153,24 +153,21 @@ void KeyboardConfig::store() void KeyboardConfig::makeDefault() { for (auto &key : mKey) - { key.value = key.defaultValue; - } } bool KeyboardConfig::hasConflicts() { - int i, j; /** * No need to parse the square matrix: only check one triangle * that's enough to detect conflicts */ - for (i = 0; i < KEY_TOTAL; i++) + for (int i = 0; i < KEY_TOTAL; i++) { if (mKey[i].value == KEY_NO_VALUE) continue; - for (j = i, j++; j < KEY_TOTAL; j++) + for (int j = i + 1; j < KEY_TOTAL; j++) { if (mKey[j].value == KEY_NO_VALUE) continue; @@ -220,25 +217,33 @@ const std::string &KeyboardConfig::getKeyCaption(int index) const int KeyboardConfig::getKeyIndex(SDL_Keycode keyValue) const { for (int i = 0; i < KEY_TOTAL; i++) - { if (keyValue == mKey[i].value) - { return i; - } - } + return KEY_NO_VALUE; } +std::string_view KeyboardConfig::getKeyName(std::string_view configName) const +{ + for (auto key : mKey) + { + if (configName == key.configField) + { + if (key.value == KEY_NO_VALUE) + return {}; + return SDL_GetKeyName(key.value); + } + } + + return {}; +} int KeyboardConfig::getKeyEmoteOffset(SDL_Keycode keyValue) const { for (int i = KEY_EMOTE_1; i <= KEY_EMOTE_12; i++) - { if (keyValue == mKey[i].value) - { return i - KEY_EMOTE_1; - } - } + return -1; } diff --git a/src/keyboardconfig.h b/src/keyboardconfig.h index 94b51aff..6fc79ced 100644 --- a/src/keyboardconfig.h +++ b/src/keyboardconfig.h @@ -101,6 +101,11 @@ class KeyboardConfig int getKeyIndex(SDL_Keycode keyValue) const; /** + * Get the key name by providing the keys config name. + */ + std::string_view getKeyName(std::string_view configName) const; + + /** * Get the key function index for an emote by providing the offset value. */ int getKeyEmoteOffset(SDL_Keycode keyValue) const; |