From 278d9aa7eb084bd6f93e6ac1fd84033dc316ab5e Mon Sep 17 00:00:00 2001 From: Thorbjørn Lindeijer Date: Thu, 15 Feb 2024 14:15:27 +0100 Subject: Fix handling of non-consecutive emote IDs Previous code was assuming there would be no gaps in the emote IDs. Also cleaned up some confusion where the "emote ID" being passed around in the code was often offset by 1. Now it is only offset in communication with tmwAthena and when saving the shortcuts. --- src/gui/emotepopup.cpp | 96 ++++++++++++++++-------------- src/gui/emotepopup.h | 14 ++--- src/gui/widgets/emoteshortcutcontainer.cpp | 55 +++++++---------- src/gui/widgets/emoteshortcutcontainer.h | 8 +-- src/gui/windowmenu.cpp | 6 +- 5 files changed, 82 insertions(+), 97 deletions(-) (limited to 'src/gui') diff --git a/src/gui/emotepopup.cpp b/src/gui/emotepopup.cpp index 104f1283..b732705e 100644 --- a/src/gui/emotepopup.cpp +++ b/src/gui/emotepopup.cpp @@ -26,16 +26,12 @@ #include "configuration.h" #include "emoteshortcut.h" #include "graphics.h" -#include "localplayer.h" #include "log.h" #include "resources/emotedb.h" #include "resources/image.h" -#include "resources/iteminfo.h" #include "resources/theme.h" -#include "utils/dtor.h" - #include #include @@ -46,12 +42,6 @@ static const int MAX_COLUMNS = 6; EmotePopup::EmotePopup() { - // Setup emote sprites - for (int i = 0; i <= EmoteDB::getLast(); ++i) - { - mEmotes.push_back(EmoteDB::get(i)->sprite); - } - mSelectionImage = Theme::getImageFromTheme("selection.png"); if (!mSelectionImage) logger->error("Unable to load selection.png"); @@ -72,8 +62,7 @@ void EmotePopup::draw(gcn::Graphics *graphics) { Popup::draw(graphics); - const int emoteCount = mEmotes.size(); - const int emotesLeft = mEmotes.size() % mColumnCount; + const int emoteCount = EmoteDB::getEmoteCount(); for (int i = 0; i < emoteCount ; i++) { @@ -84,8 +73,11 @@ void EmotePopup::draw(gcn::Graphics *graphics) int emoteY = 4 + row * gridHeight; // Center the last row when there are less emotes than columns - if (emotesLeft > 0 && row == mRowCount - 1) + if (row == mRowCount - 1) + { + const int emotesLeft = emoteCount % mColumnCount; emoteX += (mColumnCount - emotesLeft) * gridWidth / 2; + } // Draw selection image below hovered item if (i == mHoveredEmoteIndex) @@ -95,10 +87,17 @@ void EmotePopup::draw(gcn::Graphics *graphics) } // Draw emote icon - mEmotes[i]->draw(static_cast(graphics), emoteX, emoteY); + EmoteDB::getByIndex(i).sprite->draw(static_cast(graphics), emoteX, emoteY); } } +void EmotePopup::mouseExited(gcn::MouseEvent &event) +{ + Popup::mouseExited(event); + + mHoveredEmoteIndex = -1; +} + void EmotePopup::mousePressed(gcn::MouseEvent &event) { if (event.getButton() != gcn::MouseEvent::LEFT) @@ -107,8 +106,10 @@ void EmotePopup::mousePressed(gcn::MouseEvent &event) const int index = getIndexAt(event.getX(), event.getY()); if (index != -1) { - setSelectedEmoteIndex(index); - emoteShortcut->setEmoteSelected(index + 1); + const int emoteId = EmoteDB::getByIndex(index).id; + + setSelectedEmoteId(emoteId); + emoteShortcut->setEmoteSelected(emoteId); } } @@ -119,41 +120,45 @@ void EmotePopup::mouseMoved(gcn::MouseEvent &event) mHoveredEmoteIndex = getIndexAt(event.getX(), event.getY()); } -int EmotePopup::getSelectedEmote() const +int EmotePopup::getSelectedEmoteId() const { - return 1 + mSelectedEmoteIndex; + return mSelectedEmoteId; } -void EmotePopup::setSelectedEmoteIndex(int index) +void EmotePopup::setSelectedEmoteId(int emoteId) { - if (index == mSelectedEmoteIndex) + if (emoteId == mSelectedEmoteId) return; - mSelectedEmoteIndex = index; + mSelectedEmoteId = emoteId; distributeValueChangedEvent(); } int EmotePopup::getIndexAt(int x, int y) const { - const int emotesLeft = mEmotes.size() % mColumnCount; + if (mColumnCount <= 0) + return -1; + + // Take into account the border + x -= 2; + y -= 4; + const int row = y / gridHeight; - int column; // Take into account that the last row is centered - if (emotesLeft > 0 && row == mRowCount - 1) - { - int emotesMissing = mColumnCount - emotesLeft; - column = std::min((x - emotesMissing * gridWidth / 2) / gridWidth, - emotesLeft - 1); - } - else + if (row == mRowCount - 1) { - column = std::min(x / gridWidth, mColumnCount - 1); + const int emotesLeft = EmoteDB::getEmoteCount() % mColumnCount; + const int emotesMissing = mColumnCount - emotesLeft; + x -= emotesMissing * gridWidth / 2; + if (x < 0) + return -1; } - int index = column + (row * mColumnCount); + const int column = std::min(x / gridWidth, mColumnCount - 1); + const int index = column + (row * mColumnCount); - if ((unsigned) index < mEmotes.size()) + if (index >= 0 && index < EmoteDB::getEmoteCount()) return index; return -1; @@ -161,27 +166,28 @@ int EmotePopup::getIndexAt(int x, int y) const void EmotePopup::recalculateSize() { - const unsigned emoteCount = mEmotes.size(); + const int emoteCount = EmoteDB::getEmoteCount(); - mRowCount = emoteCount / MAX_COLUMNS; - if (emoteCount % MAX_COLUMNS > 0) - ++mRowCount; + if (emoteCount > 0) { + mRowCount = emoteCount / MAX_COLUMNS; + if (emoteCount % MAX_COLUMNS > 0) + ++mRowCount; - mColumnCount = emoteCount / mRowCount; - if (emoteCount % mRowCount > 0) - ++mColumnCount; + mColumnCount = std::min(MAX_COLUMNS, emoteCount); + } else { + mRowCount = 0; + mColumnCount = 0; + } setContentSize(mColumnCount * gridWidth, mRowCount * gridHeight); } void EmotePopup::distributeValueChangedEvent() { - gcn::SelectionEvent event(this); - Listeners::const_iterator i_end = mListeners.end(); - Listeners::const_iterator i; + const gcn::SelectionEvent event(this); - for (i = mListeners.begin(); i != i_end; ++i) + for (auto &listener : mListeners) { - (*i)->valueChanged(event); + listener->valueChanged(event); } } diff --git a/src/gui/emotepopup.h b/src/gui/emotepopup.h index ee905cd6..ef3fffed 100644 --- a/src/gui/emotepopup.h +++ b/src/gui/emotepopup.h @@ -28,9 +28,7 @@ #include #include -#include -class ImageSprite; class Image; namespace gcn { @@ -57,13 +55,14 @@ class EmotePopup : public Popup */ void draw(gcn::Graphics *graphics) override; + void mouseExited(gcn::MouseEvent &event) override; void mousePressed(gcn::MouseEvent &event) override; void mouseMoved(gcn::MouseEvent &event) override; /** * Returns the selected emote. */ - int getSelectedEmote() const; + int getSelectedEmoteId() const; /** * Adds a listener to the list that's notified each time a change to @@ -87,7 +86,7 @@ class EmotePopup : public Popup /** * Sets the index of the currently selected emote. */ - void setSelectedEmoteIndex(int index); + void setSelectedEmoteId(int emoteId); /** * Returns the index at the specified coordinates. Returns -1 when @@ -105,17 +104,14 @@ class EmotePopup : public Popup */ void distributeValueChangedEvent(); - std::vector mEmotes; Image *mSelectionImage; - int mSelectedEmoteIndex = -1; + int mSelectedEmoteId = -1; int mHoveredEmoteIndex = -1; int mRowCount = 1; int mColumnCount = 1; - using Listeners = std::list; - - Listeners mListeners; + std::list mListeners; static const int gridWidth; static const int gridHeight; diff --git a/src/gui/widgets/emoteshortcutcontainer.cpp b/src/gui/widgets/emoteshortcutcontainer.cpp index b719bc5f..f4a5449f 100644 --- a/src/gui/widgets/emoteshortcutcontainer.cpp +++ b/src/gui/widgets/emoteshortcutcontainer.cpp @@ -43,13 +43,7 @@ EmoteShortcutContainer::EmoteShortcutContainer() mBackgroundImg->setAlpha(config.getFloatValue("guialpha")); - // Setup emote sprites - for (int i = 0; i <= EmoteDB::getLast(); i++) - { - mEmoteImg.push_back(EmoteDB::get(i)->sprite); - } - - mMaxItems = std::min(EmoteDB::getLast(), MAX_ITEMS); + mMaxItems = std::min(EmoteDB::getEmoteCount(), MAX_ITEMS); mBoxHeight = mBackgroundImg->getHeight(); mBoxWidth = mBackgroundImg->getWidth(); @@ -81,28 +75,26 @@ void EmoteShortcutContainer::draw(gcn::Graphics *graphics) // Draw emote keyboard shortcut. const char *key = SDL_GetKeyName( - (SDL_Scancode) keyboard.getKeyValue(keyboard.KEY_EMOTE_1 + i)); + (SDL_Scancode) keyboard.getKeyValue(KeyboardConfig::KEY_EMOTE_1 + i)); graphics->setColor(Theme::getThemeColor(Theme::TEXT)); g->drawText(key, emoteX + 2, emoteY + 2, gcn::Graphics::LEFT); int emoteId = emoteShortcut->getEmote(i); - if (emoteId > 0 && emoteId <= EmoteDB::getLast() + 1) + if (emoteId != -1) { - mEmoteImg[emoteId - 1]->draw(g, emoteX + 2, emoteY + 10); + EmoteDB::get(emoteId).sprite->draw(g, emoteX + 2, emoteY + 10); } } - if (mEmoteMoved) + if (mEmoteMoved != -1) { // Draw the emote image being dragged by the cursor. - const ImageSprite* sprite = mEmoteImg[mEmoteMoved - 1]; - if (sprite) - { - const int tPosX = mCursorPosX - (sprite->getWidth() / 2); - const int tPosY = mCursorPosY - (sprite->getHeight() / 2); + const ImageSprite *sprite = EmoteDB::get(mEmoteMoved).sprite; - sprite->draw(g, tPosX, tPosY); - } + const int tPosX = mCursorPosX - (sprite->getWidth() / 2); + const int tPosY = mCursorPosY - (sprite->getHeight() / 2); + + sprite->draw(g, tPosX, tPosY); } } @@ -110,22 +102,20 @@ void EmoteShortcutContainer::mouseDragged(gcn::MouseEvent &event) { if (event.getButton() == gcn::MouseEvent::LEFT) { - if (!mEmoteMoved && mEmoteClicked) + if (mEmoteMoved == -1 && mEmoteClicked) { const int index = getIndexFromGrid(event.getX(), event.getY()); - if (index == -1) return; const int emoteId = emoteShortcut->getEmote(index); - - if (emoteId) + if (emoteId != -1) { mEmoteMoved = emoteId; emoteShortcut->removeEmote(index); } } - if (mEmoteMoved) + if (mEmoteMoved != -1) { mCursorPosX = event.getX(); mCursorPosY = event.getY(); @@ -144,9 +134,9 @@ void EmoteShortcutContainer::mousePressed(gcn::MouseEvent &event) if (emoteShortcut->isEmoteSelected()) { emoteShortcut->setEmote(index); - emoteShortcut->setEmoteSelected(0); + emoteShortcut->setEmoteSelected(-1); } - else if (emoteShortcut->getEmote(index)) + else if (emoteShortcut->getEmote(index) != -1) { mEmoteClicked = true; } @@ -159,26 +149,25 @@ void EmoteShortcutContainer::mouseReleased(gcn::MouseEvent &event) const int index = getIndexFromGrid(event.getX(), event.getY()); if (emoteShortcut->isEmoteSelected()) - emoteShortcut->setEmoteSelected(0); + emoteShortcut->setEmoteSelected(-1); if (index == -1) { - mEmoteMoved = 0; + mEmoteMoved = -1; return; } - if (mEmoteMoved) + if (mEmoteMoved != -1) { emoteShortcut->setEmotes(index, mEmoteMoved); - mEmoteMoved = 0; + mEmoteMoved = -1; } - else if (emoteShortcut->getEmote(index) && mEmoteClicked) + else if (mEmoteClicked) { - emoteShortcut->useEmote(index + 1); + emoteShortcut->useEmote(index); } - if (mEmoteClicked) - mEmoteClicked = false; + mEmoteClicked = false; } } diff --git a/src/gui/widgets/emoteshortcutcontainer.h b/src/gui/widgets/emoteshortcutcontainer.h index 209a3725..ecd41736 100644 --- a/src/gui/widgets/emoteshortcutcontainer.h +++ b/src/gui/widgets/emoteshortcutcontainer.h @@ -24,10 +24,6 @@ #include "gui/widgets/shortcutcontainer.h" -#include - -class ImageSprite; - /** * An emote shortcut container. Used to quickly use emoticons. * @@ -61,10 +57,8 @@ class EmoteShortcutContainer : public ShortcutContainer void mouseReleased(gcn::MouseEvent &event) override; private: - std::vector mEmoteImg; - bool mEmoteClicked = false; - int mEmoteMoved = 0; + int mEmoteMoved = -1; }; #endif diff --git a/src/gui/windowmenu.cpp b/src/gui/windowmenu.cpp index aff7c5c5..01194c0b 100644 --- a/src/gui/windowmenu.cpp +++ b/src/gui/windowmenu.cpp @@ -153,9 +153,9 @@ void WindowMenu::valueChanged(const gcn::SelectionEvent &event) { if (event.getSource() == mEmotePopup) { - int emote = mEmotePopup->getSelectedEmote(); - if (emote) - Net::getPlayerHandler()->emote(emote); + int emoteId = mEmotePopup->getSelectedEmoteId(); + if (emoteId != -1) + Net::getPlayerHandler()->emote(emoteId); windowContainer->scheduleDelete(mEmotePopup); mEmotePopup = nullptr; -- cgit v1.2.3-70-g09d2