summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorThorbjørn Lindeijer <bjorn@lindeijer.nl>2025-05-09 09:55:39 +0200
committerThorbjørn Lindeijer <bjorn@lindeijer.nl>2025-05-25 19:24:29 +0000
commite35fd4085a9ca3044fa415e79490d6cf192a4ba4 (patch)
treec250ac06ae8b14f286de3918bc078e3463bddffc /src
parent6154a8f70daa6c21b70e07b192e5eee73d667239 (diff)
downloadmana-master.tar.gz
mana-master.tar.bz2
mana-master.tar.xz
mana-master.zip
Fixed tooltip visibility logic for shortcut and inventory windowsHEADmaster
* When hovering an empty box in the shortcut window, the tooltip of a previously hovered non-empty box would stay visible. * When hovering to the right of a row of inventory items, the tooltip for the left-most item on the next row would be displayed. Also added some padding to shift the text and item icon a little in the shortcut window when using the Jewelry theme.
Diffstat (limited to 'src')
-rw-r--r--src/gui/widgets/itemcontainer.cpp15
-rw-r--r--src/gui/widgets/itemshortcutcontainer.cpp47
-rw-r--r--src/gui/widgets/itemshortcutcontainer.h2
-rw-r--r--src/itemshortcut.cpp7
-rw-r--r--src/itemshortcut.h2
-rw-r--r--src/net/tmwa/buysellhandler.cpp2
6 files changed, 44 insertions, 31 deletions
diff --git a/src/gui/widgets/itemcontainer.cpp b/src/gui/widgets/itemcontainer.cpp
index 57e8a973..d0d24c51 100644
--- a/src/gui/widgets/itemcontainer.cpp
+++ b/src/gui/widgets/itemcontainer.cpp
@@ -354,9 +354,7 @@ void ItemContainer::mouseReleased(gcn::MouseEvent &event)
// Show ItemTooltip
void ItemContainer::mouseMoved(gcn::MouseEvent &event)
{
- Item *item = getItemAt(getSlotIndex(event.getX(), event.getY()));
-
- if (item)
+ if (Item *item = getItemAt(getSlotIndex(event.getX(), event.getY())))
{
mItemPopup->setItem(item->getInfo());
mItemPopup->position(viewport->getMouseX(), viewport->getMouseY());
@@ -394,12 +392,17 @@ void ItemContainer::adjustHeight()
int ItemContainer::getSlotIndex(int x, int y) const
{
+ if (x >= getWidth() || y >= getHeight())
+ return Inventory::NO_SLOT_INDEX;
+
auto &slotSkin = gui->getTheme()->getSkin(SkinType::ItemSlot);
+ const auto row = y / slotSkin.height;
+ const auto column = x / slotSkin.width;
- if (x < getWidth() && y < getHeight())
- return (y / slotSkin.height) * mGridColumns + (x / slotSkin.width);
+ if (row < 0 || row >= mGridRows || column < 0 || column >= mGridColumns)
+ return Inventory::NO_SLOT_INDEX;
- return Inventory::NO_SLOT_INDEX;
+ return (row * mGridColumns) + column;
}
void ItemContainer::keyAction()
diff --git a/src/gui/widgets/itemshortcutcontainer.cpp b/src/gui/widgets/itemshortcutcontainer.cpp
index b7924a4b..b47fa29d 100644
--- a/src/gui/widgets/itemshortcutcontainer.cpp
+++ b/src/gui/widgets/itemshortcutcontainer.cpp
@@ -49,6 +49,7 @@ void ItemShortcutContainer::draw(gcn::Graphics *graphics)
{
auto *g = static_cast<Graphics*>(graphics);
auto theme = gui->getTheme();
+ auto &skin = theme->getSkin(SkinType::ShortcutBox);
graphics->setFont(getFont());
@@ -57,21 +58,22 @@ void ItemShortcutContainer::draw(gcn::Graphics *graphics)
WidgetState state;
state.x = (i % mGridWidth) * mBoxWidth;
state.y = (i / mGridWidth) * mBoxHeight;
- theme->drawSkin(g, SkinType::ShortcutBox, state);
+ skin.draw(g, state);
// Draw item keyboard shortcut.
const char *key = SDL_GetKeyName(
keyboard.getKeyValue(KeyboardConfig::KEY_SHORTCUT_1 + i));
graphics->setColor(Theme::getThemeColor(Theme::TEXT));
- g->drawText(key, state.x + 2, state.y + 2, gcn::Graphics::LEFT);
+ g->drawText(key,
+ state.x + skin.padding + 2,
+ state.y + skin.padding + 2,
+ gcn::Graphics::LEFT);
- if (itemShortcut->getItem(i) < 0)
+ const int itemId = itemShortcut->getItem(i);
+ if (itemId < 0)
continue;
- Item *item =
- PlayerInfo::getInventory()->findItem(itemShortcut->getItem(i));
-
- if (item)
+ if (Item *item = PlayerInfo::getInventory()->findItem(itemId))
{
// Draw item icon.
if (Image *image = item->getImage())
@@ -83,11 +85,13 @@ void ItemShortcutContainer::draw(gcn::Graphics *graphics)
caption = "Eq.";
image->setAlpha(1.0f);
- g->drawImage(image, state.x, state.y);
+ g->drawImage(image, state.x + skin.padding, state.y + skin.padding);
if (item->isEquipped())
g->setColor(Theme::getThemeColor(Theme::ITEM_EQUIPPED));
- g->drawText(caption, state.x + mBoxWidth / 2,
- state.y + mBoxHeight - 14, gcn::Graphics::CENTER);
+ g->drawText(caption,
+ state.x + mBoxWidth / 2,
+ state.y + mBoxHeight - 14,
+ gcn::Graphics::CENTER);
}
}
}
@@ -200,15 +204,7 @@ void ItemShortcutContainer::mouseReleased(gcn::MouseEvent &event)
// Show ItemTooltip
void ItemShortcutContainer::mouseMoved(gcn::MouseEvent &event)
{
- const int index = getIndexFromGrid(event.getX(), event.getY());
- if (index == -1)
- return;
-
- const int itemId = itemShortcut->getItem(index);
- if (itemId < 0)
- return;
-
- if (Item *item = PlayerInfo::getInventory()->findItem(itemId))
+ if (Item *item = getItemAt(event.getX(), event.getY()))
{
mItemPopup->setItem(item->getInfo());
mItemPopup->position(viewport->getMouseX(), viewport->getMouseY());
@@ -219,6 +215,19 @@ void ItemShortcutContainer::mouseMoved(gcn::MouseEvent &event)
}
}
+Item *ItemShortcutContainer::getItemAt(int x, int y) const
+{
+ const int index = getIndexFromGrid(x, y);
+ if (index == -1)
+ return nullptr;
+
+ const int itemId = itemShortcut->getItem(index);
+ if (itemId < 0)
+ return nullptr;
+
+ return PlayerInfo::getInventory()->findItem(itemId);
+}
+
// Hide ItemTooltip
void ItemShortcutContainer::mouseExited(gcn::MouseEvent &event)
{
diff --git a/src/gui/widgets/itemshortcutcontainer.h b/src/gui/widgets/itemshortcutcontainer.h
index 63d9e0ef..a01857db 100644
--- a/src/gui/widgets/itemshortcutcontainer.h
+++ b/src/gui/widgets/itemshortcutcontainer.h
@@ -67,6 +67,8 @@ class ItemShortcutContainer : public ShortcutContainer
void mouseExited(gcn::MouseEvent &event) override;
void mouseMoved(gcn::MouseEvent &event) override;
+ Item *getItemAt(int x, int y) const;
+
bool mItemClicked = false;
Item *mItemMoved = nullptr;
diff --git a/src/itemshortcut.cpp b/src/itemshortcut.cpp
index 643a594f..4d2cec8a 100644
--- a/src/itemshortcut.cpp
+++ b/src/itemshortcut.cpp
@@ -29,8 +29,7 @@
ItemShortcut *itemShortcut;
-ItemShortcut::ItemShortcut():
- mItemSelected(-1)
+ItemShortcut::ItemShortcut()
{
load();
}
@@ -42,8 +41,8 @@ ItemShortcut::~ItemShortcut()
void ItemShortcut::load()
{
- for (int i = 0; i < SHORTCUT_ITEMS; i++)
- mItems[i] = -1;
+ for (int &item : mItems)
+ item = -1;
for (auto &shortcut : config.itemShortcuts)
{
diff --git a/src/itemshortcut.h b/src/itemshortcut.h
index fcec3d7d..0849a8d1 100644
--- a/src/itemshortcut.h
+++ b/src/itemshortcut.h
@@ -109,7 +109,7 @@ class ItemShortcut
void save();
int mItems[SHORTCUT_ITEMS]; /**< The items stored. */
- int mItemSelected; /**< The item held by cursor. */
+ int mItemSelected = -1; /**< The item held by cursor. */
};
diff --git a/src/net/tmwa/buysellhandler.cpp b/src/net/tmwa/buysellhandler.cpp
index 1fdf1ffe..d7acd674 100644
--- a/src/net/tmwa/buysellhandler.cpp
+++ b/src/net/tmwa/buysellhandler.cpp
@@ -99,7 +99,7 @@ void BuySellHandler::handleMessage(MessageIn &msg)
Item *item = PlayerInfo::getInventory()->getItem(index);
- if (item && !(item->isEquipped()))
+ if (item && !item->isEquipped())
dialog->addItem(item, value);
}
}