diff options
author | Thorbjørn Lindeijer <bjorn@lindeijer.nl> | 2024-02-15 14:15:27 +0100 |
---|---|---|
committer | Thorbjørn Lindeijer <bjorn@lindeijer.nl> | 2024-02-15 14:15:34 +0100 |
commit | 278d9aa7eb084bd6f93e6ac1fd84033dc316ab5e (patch) | |
tree | e1a095b48e34b844405801f7240501fe89579b38 /src/gui/emotepopup.cpp | |
parent | 2779eb792a04667958feab9b433c1c82392c0bc3 (diff) | |
download | mana-278d9aa7eb084bd6f93e6ac1fd84033dc316ab5e.tar.gz mana-278d9aa7eb084bd6f93e6ac1fd84033dc316ab5e.tar.bz2 mana-278d9aa7eb084bd6f93e6ac1fd84033dc316ab5e.tar.xz mana-278d9aa7eb084bd6f93e6ac1fd84033dc316ab5e.zip |
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.
Diffstat (limited to 'src/gui/emotepopup.cpp')
-rw-r--r-- | src/gui/emotepopup.cpp | 96 |
1 files changed, 51 insertions, 45 deletions
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 <guichan/mouseinput.hpp> #include <guichan/selectionlistener.hpp> @@ -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*>(graphics), emoteX, emoteY); + EmoteDB::getByIndex(i).sprite->draw(static_cast<Graphics*>(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); } } |