//------------------------------------------------------------------------------// bool InventoryReceiver::addItemAtLocation(InventoryItem& item, int x, int y) { if (itemWillFitAtLocation(item, x, y)) { InventoryReceiver* old_receiver = dynamic_cast<InventoryReceiver*>(item.getParent()); if (old_receiver) old_receiver->removeItem(item); item.setLocationOnReceiver(x, y); writeItemToContentMap(item); addChild(&item); // set position and size. This ensures the items visually match the // logical content map. item.setPosition(UVector2(UDim(static_cast<float>(x) / contentWidth(), 0), UDim(static_cast<float>(y) / contentHeight(), 0))); item.setSize(USize( UDim(static_cast<float>(item.contentWidth()) / contentWidth(), 0), UDim(static_cast<float>(item.contentHeight()) / contentHeight(), 0))); return true; } return false; }
//------------------------------------------------------------------------------// bool InventoryReceiver::itemWillFitAtLocation(const InventoryItem& item, int x, int y) { if (x < 0 || y < 0) return false; if (x + item.contentWidth() > d_content.width() || y + item.contentHeight() > d_content.height()) return false; const bool already_attached = this == item.getParent(); // if item is already attatched erase its data from the content map so the // test result is reliable. if (already_attached) eraseItemFromContentMap(item); bool result = true; for (int item_y = 0; item_y < item.contentHeight() && result; ++item_y) { for (int item_x = 0; item_x < item.contentWidth() && result; ++item_x) { if (d_content.elementAtLocation(item_x + x, item_y + y) && item.isSolidAtLocation(item_x, item_y)) result = false; } } // re-write item into content map if we erased it earlier. if (already_attached) writeItemToContentMap(item); return result; }
//------------------------------------------------------------------------------// void InventoryReceiver::removeItem(InventoryItem& item) { if (item.getParent() != this || item.locationOnReceiverX() == -1 || item.locationOnReceiverY() == -1) return; eraseItemFromContentMap(item); item.setLocationOnReceiver(-1, -1); removeChild(&item); }