#include "qw_inventorymanager.h" #include "qw_globalmetadata.h" #include "view/controls/sceneinventorypanel.h" #include "view/qw_scene.h" #include "models/qw_trigger.h" QWInventoryManager::QWInventoryManager(QWScene *sc) : ptr_scene(sc), index_freepan(0) { // Loading metadata metadata.length_wall = QWGlobalMetadata::valueBy("InventoryManager:length_wall").toInt(); metadata.length_cell = QWGlobalMetadata::valueBy("InventoryManager:length_cell").toInt(); metadata.max_amount = QWGlobalMetadata::valueBy("InventoryManager:max_amount").toInt(); metadata.first_wall = QWGlobalMetadata::valueBy("InventoryManager:first_wall").toInt(); metadata.clr_sel = Qt::GlobalColor(QWGlobalMetadata::valueBy("InventoryManager:clr_sel").toInt()); metadata.clr_sth = QWGlobalMetadata::valueBy("InventoryManager:clr_str").toReal(); item_selected_effect = new QGraphicsColorizeEffect(); item_selected_effect->setColor(metadata.clr_sel); item_selected_effect->setStrength(metadata.clr_sth); } void QWInventoryManager::setInventoryPanel(const std::shared_ptr &panel) noexcept { ptr_panel = panel; } int QWInventoryManager::freePanel() const noexcept { return index_freepan; } void QWInventoryManager::freePanelToRight() { ++index_freepan; Q_ASSERT(index_freepan <= metadata.max_amount); } void QWInventoryManager::freePanelToLeft() { --index_freepan; Q_ASSERT(index_freepan >= 0); } void QWInventoryManager::addInventoryIcon(std::shared_ptr &inventory_icon) { inventory_icon->setParentItem(ptr_panel.get()); list_inventory_icons.append(inventory_icon); inventory_icon->setPos(metadata.first_wall + ((metadata.length_wall + metadata.length_cell) * freePanel()) , 0); freePanelToRight(); } void QWInventoryManager::removeInventoryIcon(std::shared_ptr &inventory_icon) { Index panel = metadata.max_amount + 1; for (Index i = 0; i < list_inventory_icons.size(); ++i) if (inventory_icon == list_inventory_icons[i]) panel = i; Q_ASSERT(panel <= metadata.max_amount); ptr_scene->removeItem(inventory_icon.get()); list_inventory_icons.removeAt(panel); freePanelToLeft(); reorganizeItems(panel); } void QWInventoryManager::reorganizeItems(Index free_panel) { // When we remove item from the center we also should carefully move all the right items to the left for (Index j = free_panel; j < list_inventory_icons.size(); ++j) { const std::shared_ptr &t = list_inventory_icons.at(j); t->moveBy(-(metadata.length_cell + metadata.length_wall), 0); if (j > 0) list_inventory_icons[j-1] = list_inventory_icons[j]; } } void QWInventoryManager::onClicked() { for (auto &tr : list_inventory_icons) if (tr->isUnderMouse()) { tr->setGraphicsEffect(tr->graphicsEffect() ? nullptr : item_selected_effect); tr->activate(); } else { tr->setGraphicsEffect(nullptr); } }