From c36fb36afba47f52c1f81f1df61eb31838134cd2 Mon Sep 17 00:00:00 2001 From: Ľubomír Remák Date: Thu, 6 Sep 2018 19:38:16 +0200 Subject: MUTATIONOFJB: Add support for 'look' action on inventory items. --- engines/mutationofjb/widgets/inventorywidget.cpp | 64 +++++++++++++++++++++--- 1 file changed, 57 insertions(+), 7 deletions(-) (limited to 'engines/mutationofjb/widgets/inventorywidget.cpp') diff --git a/engines/mutationofjb/widgets/inventorywidget.cpp b/engines/mutationofjb/widgets/inventorywidget.cpp index 46bfab9c34..851f8af9d0 100644 --- a/engines/mutationofjb/widgets/inventorywidget.cpp +++ b/engines/mutationofjb/widgets/inventorywidget.cpp @@ -23,11 +23,14 @@ #include "mutationofjb/widgets/inventorywidget.h" #include "mutationofjb/game.h" #include "mutationofjb/gamedata.h" -#include "mutationofjb/gui.h" +#include "mutationofjb/gamescreen.h" #include "mutationofjb/inventory.h" + #include "common/str.h" #include "common/rect.h" #include "common/util.h" +#include "common/events.h" + #include "graphics/managed_surface.h" namespace MutationOfJB { @@ -41,18 +44,18 @@ enum { INVENTORY_ITEMS_LINES = 5 }; -InventoryWidget::InventoryWidget(Gui &gui, Gui::InventoryMap &inventoryMap, const Common::Array &inventorySurfaces) : +InventoryWidget::InventoryWidget(GuiScreen &gui, const Common::Array &inventorySurfaces) : Widget(gui, Common::Rect(INVENTORY_START_X, INVENTORY_START_Y, INVENTORY_START_X + Inventory::VISIBLE_ITEMS * INVENTORY_ITEM_WIDTH, INVENTORY_START_Y + INVENTORY_ITEM_HEIGHT)), - _inventoryMap(inventoryMap), - _surfaces(inventorySurfaces) {} + _surfaces(inventorySurfaces), + _callback(nullptr), + _hoveredItemPos(-1) {} void InventoryWidget::drawInventoryItem(Graphics::ManagedSurface &surface, const Common::String &item, int pos) { - Gui::InventoryMap::iterator it = _inventoryMap.find(item); - if (it == _inventoryMap.end()) { + const int index = _gui.getGame().getAssets().getInventoryItemDefList().findItemIndex(item); + if (index == -1) { return; } - const int index = it->_value; const int surfaceNo = index / (INVENTORY_ITEMS_LINES * INVENTORY_ITEMS_PER_LINE); const int indexInSurface = index % (INVENTORY_ITEMS_LINES * INVENTORY_ITEMS_PER_LINE); const int itemX = indexInSurface % INVENTORY_ITEMS_PER_LINE; @@ -72,4 +75,51 @@ void InventoryWidget::draw(Graphics::ManagedSurface &surface) { } } +void InventoryWidget::handleEvent(const Common::Event &event) { + if (!_callback) + return; + + Inventory &inventory = _gui.getGame().getGameData().getInventory(); + const int numItems = inventory.getItems().size(); + + switch (event.type) { + case Common::EVENT_LBUTTONDOWN: { + const int16 x = event.mouse.x; + const int16 y = event.mouse.y; + if (_area.contains(x, y)) { + int itemPos = (x - INVENTORY_START_X) / INVENTORY_ITEM_WIDTH; + if (itemPos < numItems) { + _callback->onInventoryItemClicked(this, itemPos); + } + } + break; + } + case Common::EVENT_MOUSEMOVE: { + const int16 x = event.mouse.x; + const int16 y = event.mouse.y; + + int newHoveredItemPos = -1; + if (_area.contains(x, y)) { + int itemPos = (x - INVENTORY_START_X) / INVENTORY_ITEM_WIDTH; + if (itemPos < numItems) { + newHoveredItemPos = itemPos; + + if (_hoveredItemPos != newHoveredItemPos) { + _callback->onInventoryItemHovered(this, itemPos); + } + } + } + + if (newHoveredItemPos == -1 && _hoveredItemPos != -1) { + _callback->onInventoryItemHovered(this, -1); + } + + _hoveredItemPos = newHoveredItemPos; + break; + } + default: + break; + } +} + } -- cgit v1.2.3