Groups can now be used for filtering in InventoryWidget.

This commit is contained in:
Quentin Bazin 2020-03-29 13:51:58 +02:00
parent 1347f81619
commit 786aaff198
10 changed files with 44 additions and 20 deletions

View File

@ -112,11 +112,12 @@ mod:block {
source = "block",
block = {x = pos.x, y = pos.y, z = pos.z},
offset = 2,
count = 1
count = 1,
},
size = {x = 1, y = 1},
filter = "group:om_fuel",
shift_destination = "inv_main,inv_hotbar",
}

View File

@ -38,3 +38,10 @@ void AbstractInventoryWidget::setShiftDestination(const std::string &shiftDestin
}
}
bool AbstractInventoryWidget::doItemMatchFilter(const Item &item) {
if (!m_filter.empty() && item.id() != 0)
return item.hasGroup(m_filter);
return true;
}

View File

@ -40,8 +40,14 @@ class AbstractInventoryWidget : public Widget {
void setShiftDestination(const std::string &shiftDestination);
void setShiftDestination(const std::vector<std::string> &shiftDestination) { m_shiftDestination = shiftDestination; }
const std::string &filter() const { return m_filter; }
void setFilter(const std::string &filter) { m_filter = filter; }
bool doItemMatchFilter(const Item &item);
protected:
std::vector<std::string> m_shiftDestination;
std::string m_filter;
};
#endif // ABSTRACTINVENTORYWIDGET_HPP_

View File

@ -80,7 +80,7 @@ void InventoryWidget::update() {
}
bool InventoryWidget::sendItemStackToDest(const ItemWidget *itemStack, AbstractInventoryWidget *dest) {
if (dest->receiveItemStack(itemStack, this)) {
if (dest->doItemMatchFilter(itemStack->stack().item()) && dest->receiveItemStack(itemStack, this)) {
if (dest != this)
m_inventory->clearStack(itemStack->x(), itemStack->y());

View File

@ -76,24 +76,28 @@ void MouseItemWidget::onEvent(const SDL_Event &event) {
void MouseItemWidget::leftClickBehaviour() {
if (m_currentInventoryWidget && m_currentInventoryWidget->currentItemWidget() && m_currentInventoryWidget->inventory()) {
ItemWidget *currentItemWidget = m_currentInventoryWidget->currentItemWidget();
if (!m_currentInventoryWidget->inventory()->isUnlimited())
swapItems(*currentItemWidget, m_currentInventoryWidget->isReadOnly());
else if (getStack().amount() == 0 && currentItemWidget->stack().amount() != 0)
setStack(currentItemWidget->stack().item().stringID(), 64);
if (m_currentInventoryWidget->doItemMatchFilter(m_inventory.getStack(0, 0).item())) {
ItemWidget *currentItemWidget = m_currentInventoryWidget->currentItemWidget();
if (!m_currentInventoryWidget->inventory()->isUnlimited())
swapItems(*currentItemWidget, m_currentInventoryWidget->isReadOnly());
else if (getStack().amount() == 0 && currentItemWidget->stack().amount() != 0)
setStack(currentItemWidget->stack().item().stringID(), 64);
m_currentInventoryWidget->sendUpdatePacket();
m_currentInventoryWidget->sendUpdatePacket();
}
}
}
void MouseItemWidget::rightClickBehaviour() {
if (m_currentInventoryWidget && m_currentInventoryWidget->currentItemWidget() && m_currentInventoryWidget->inventory()) {
if (!m_currentInventoryWidget->isReadOnly()) {
ItemWidget *currentItemWidget = m_currentInventoryWidget->currentItemWidget();
if (!m_currentInventoryWidget->inventory()->isUnlimited())
putItem(*currentItemWidget);
else if (getStack().amount() == 0 && currentItemWidget->stack().amount() != 0)
setStack(currentItemWidget->stack().item().stringID(), 1);
if (m_currentInventoryWidget->doItemMatchFilter(m_inventory.getStack(0, 0).item())) {
ItemWidget *currentItemWidget = m_currentInventoryWidget->currentItemWidget();
if (!m_currentInventoryWidget->inventory()->isUnlimited())
putItem(*currentItemWidget);
else if (getStack().amount() == 0 && currentItemWidget->stack().amount() != 0)
setStack(currentItemWidget->stack().item().stringID(), 1);
}
m_currentInventoryWidget->sendUpdatePacket();
}
@ -167,7 +171,8 @@ void MouseItemWidget::draggingBehaviour(ItemWidget *newItemWidget) {
void MouseItemWidget::updateCurrentItem(ItemWidget *currentItemWidget) {
if (currentItemWidget) {
if (m_isDragging && currentItemWidget != m_currentItemWidget)
bool doItemMatchFilter = !m_currentInventoryWidget || m_currentInventoryWidget->doItemMatchFilter(m_draggedStack.item());
if (m_isDragging && currentItemWidget != m_currentItemWidget && doItemMatchFilter)
draggingBehaviour(currentItemWidget);
m_currentItemWidget = (currentItemWidget->stack().item().id()) ? currentItemWidget : nullptr;

View File

@ -144,8 +144,8 @@ void LuaGUIState::update() {
}
}
m_mouseItemWidget.updateCurrentItem(currentItemWidget);
m_mouseItemWidget.setCurrentInventoryWidget(m_currentInventoryWidget);
m_mouseItemWidget.updateCurrentItem(currentItemWidget);
}
void LuaGUIState::draw(gk::RenderTarget &target, gk::RenderStates states) const {
@ -222,10 +222,10 @@ void LuaGUIState::loadTextButton(const std::string &, s32 x, s32 y, sf::Packet &
}
void LuaGUIState::loadInventoryWidget(const std::string &name, s32 x, s32 y, sf::Packet &packet) {
std::string inventory, shiftDestination;
std::string inventory, shiftDestination, filter;
u16 width, height;
u16 offset, count;
packet >> width >> height >> shiftDestination >> offset >> count >> inventory;
packet >> width >> height >> shiftDestination >> offset >> count >> inventory >> filter;
Inventory *widgetInventory = nullptr;
if (inventory == "player") {
@ -270,6 +270,7 @@ void LuaGUIState::loadInventoryWidget(const std::string &name, s32 x, s32 y, sf:
inventoryWidget.setPosition(x, y);
inventoryWidget.init(*widgetInventory, offset, count);
inventoryWidget.setShiftDestination(shiftDestination);
inventoryWidget.setFilter(filter);
}
else {
DEBUG("ERROR: Inventory widget '" + name + "' is invalid");

View File

@ -44,7 +44,7 @@ class Inventory : public ISerializable {
ItemStack &getStackRef(u16 x, u16 y) { return m_items.at(x + y * m_width); }
void setStack(u16 x, u16 y, const std::string &stringID, u16 amount = 1);
bool addStack(const std::string &stringID, u16 amount = 1, u16 offset = 0, u16 size = 0);
bool addStack2(const std::string &stringID, u16 amount = 1);
bool addStack2(const std::string &stringID, u16 amount = 1); // Needed for Lua
void clearStack(u16 x, u16 y);
void serialize(sf::Packet &packet) const override;

View File

@ -31,7 +31,8 @@
void InventoryWidgetDef::serialize(sf::Packet &packet) const {
WidgetDef::serialize(packet);
packet << m_width << m_height << m_shiftDestination << m_offset << m_count << m_inventory;
packet << m_width << m_height << m_shiftDestination << m_offset << m_count
<< m_inventory << m_filter;
if (m_inventory == "player")
packet << m_player << m_inventoryName;
@ -47,6 +48,7 @@ void InventoryWidgetDef::loadFromLuaTable(const sol::table &table) {
loadInventory(table);
m_shiftDestination = table["shift_destination"].get_or<std::string>("");
m_filter = table["filter"].get_or<std::string>("");
sol::optional<sol::table> size = table["size"];
if (size != sol::nullopt) {

View File

@ -54,6 +54,8 @@ class InventoryWidgetDef : public WidgetDef {
u16 m_offset = 0;
u16 m_count = 0;
std::string m_filter;
};
#endif // INVENTORYWIDGETDEF_HPP_

View File

@ -46,7 +46,7 @@ void LuaItemLoader::loadItem(const sol::table &table) const {
if (groupsObject.get_type() == sol::type::table) {
sol::table groupsTable = groupsObject.as<sol::table>();
for (auto &groupObject : groupsTable) {
item.addGroup(groupObject.first.as<std::string>(), groupObject.second.as<u16>());
item.addGroup("group:" + groupObject.first.as<std::string>(), groupObject.second.as<u16>());
}
}
else