[AbstractInventoryWidget] Now supports multiple destinations.

This commit is contained in:
Quentin Bazin 2020-03-01 12:55:20 +01:00
parent c092e53116
commit 908bb10b9b
10 changed files with 78 additions and 29 deletions

View File

@ -0,0 +1,40 @@
/*
* =====================================================================================
*
* OpenMiner
*
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <openminer@unarelith.net>
* Copyright (C) 2019-2020 the OpenMiner contributors (see CONTRIBUTORS.md)
*
* This file is part of OpenMiner.
*
* OpenMiner is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* OpenMiner is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with OpenMiner; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* =====================================================================================
*/
#include <sstream>
#include "AbstractInventoryWidget.hpp"
void AbstractInventoryWidget::setShiftDestination(const std::string &shiftDestination) {
m_shiftDestination.clear();
std::stringstream stream(shiftDestination);
std::string item;
while (std::getline(stream, item, ',')) {
m_shiftDestination.emplace_back(item);
}
}

View File

@ -33,14 +33,14 @@ class AbstractInventoryWidget : public Widget {
public:
AbstractInventoryWidget(Widget *parent) : Widget(parent) {}
virtual void sendItemStackToDest(const ItemWidget *itemStack, AbstractInventoryWidget *dest) = 0;
virtual bool sendItemStackToDest(const ItemWidget *itemStack, AbstractInventoryWidget *dest) = 0;
virtual bool receiveItemStack(const ItemWidget *itemStack) = 0;
const std::string &shiftDestination() const { return m_shiftDestination; }
void setShiftDestination(const std::string &shiftDestination) { m_shiftDestination = shiftDestination; }
const std::vector<std::string> &shiftDestination() const { return m_shiftDestination; }
void setShiftDestination(const std::string &shiftDestination);
private:
std::string m_shiftDestination;
std::vector<std::string> m_shiftDestination;
};
#endif // ABSTRACTINVENTORYWIDGET_HPP_

View File

@ -80,12 +80,15 @@ void CraftingWidget::update() {
}
}
void CraftingWidget::sendItemStackToDest(const ItemWidget *itemStack, AbstractInventoryWidget *dest) {
bool CraftingWidget::sendItemStackToDest(const ItemWidget *itemStack, AbstractInventoryWidget *dest) {
if (m_currentInventoryWidget && dest->receiveItemStack(itemStack)) {
m_currentInventoryWidget->inventory()->clearStack(itemStack->x(), itemStack->y());
m_currentInventoryWidget->update();
m_currentInventoryWidget->sendUpdatePacket();
return true;
}
return false;
}
bool CraftingWidget::receiveItemStack(const ItemWidget *itemStack) {

View File

@ -41,7 +41,7 @@ class CraftingWidget : public AbstractInventoryWidget {
void update() override;
void sendItemStackToDest(const ItemWidget *itemStack, AbstractInventoryWidget *dest) override;
bool sendItemStackToDest(const ItemWidget *itemStack, AbstractInventoryWidget *dest) override;
bool receiveItemStack(const ItemWidget *itemStack) override;
const ItemWidget *currentItemWidget() const { return m_craftingResultInventoryWidget.currentItemWidget() ? m_craftingResultInventoryWidget.currentItemWidget() : m_craftingInventoryWidget.currentItemWidget(); }

View File

@ -86,12 +86,15 @@ void InventoryWidget::update() {
it.update();
}
void InventoryWidget::sendItemStackToDest(const ItemWidget *itemStack, AbstractInventoryWidget *dest) {
bool InventoryWidget::sendItemStackToDest(const ItemWidget *itemStack, AbstractInventoryWidget *dest) {
if (dest->receiveItemStack(itemStack)) {
m_inventory->clearStack(itemStack->x(), itemStack->y());
update();
sendUpdatePacket();
return true;
}
return false;
}
bool InventoryWidget::receiveItemStack(const ItemWidget *itemStack) {

View File

@ -45,7 +45,7 @@ class InventoryWidget : public AbstractInventoryWidget {
void update() override;
void sendItemStackToDest(const ItemWidget *itemStack, AbstractInventoryWidget *dest) override;
bool sendItemStackToDest(const ItemWidget *itemStack, AbstractInventoryWidget *dest) override;
bool receiveItemStack(const ItemWidget *itemStack) override;
void sendUpdatePacket();

View File

@ -74,24 +74,27 @@ void LuaGUIState::onEvent(const SDL_Event &event) {
if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT
&& m_currentInventoryWidget && !m_currentInventoryWidget->shiftDestination().empty()
&& m_mouseItemWidget.currentItemWidget() && gk::GamePad::isKeyPressed(GameKey::Shift)) {
AbstractInventoryWidget *dest = nullptr;
for (const std::string &shiftDestination : m_currentInventoryWidget->shiftDestination()) {
AbstractInventoryWidget *dest = nullptr;
auto it = m_craftingWidgets.find(m_currentInventoryWidget->shiftDestination());
if (it != m_craftingWidgets.end())
dest = &it->second;
if (!dest) {
auto it = m_inventoryWidgets.find(m_currentInventoryWidget->shiftDestination());
if (it != m_inventoryWidgets.end())
auto it = m_craftingWidgets.find(shiftDestination);
if (it != m_craftingWidgets.end())
dest = &it->second;
}
if (!dest) {
DEBUG("ERROR: Destination not found: '" + m_currentInventoryWidget->shiftDestination() + "'");
return;
}
if (!dest) {
auto it = m_inventoryWidgets.find(shiftDestination);
if (it != m_inventoryWidgets.end())
dest = &it->second;
}
m_currentInventoryWidget->sendItemStackToDest(m_mouseItemWidget.currentItemWidget(), dest);
if (!dest) {
DEBUG("WARNING: Destination not found: '" + shiftDestination + "'");
return;
}
if (m_currentInventoryWidget->sendItemStackToDest(m_mouseItemWidget.currentItemWidget(), dest))
break;
}
}
else {
for (auto &it : m_inventoryWidgets)

View File

@ -85,7 +85,7 @@ mod:block {
offset = 0,
count = 1,
shift_destination = "inv_main",
shift_destination = "inv_main,inv_hotbar",
}
gui:inventory {
@ -99,7 +99,7 @@ mod:block {
offset = 1,
count = 1,
shift_destination = "inv_main",
shift_destination = "inv_main,inv_hotbar",
}
gui:inventory {
@ -113,7 +113,7 @@ mod:block {
offset = 2,
count = 1,
shift_destination = "inv_main",
shift_destination = "inv_main,inv_hotbar",
}
gui:inventory {
@ -128,7 +128,7 @@ mod:block {
offset = 9,
count = 9 * 3,
shift_destination = "inv_input",
shift_destination = "inv_input,inv_fuel",
}
gui:inventory {
@ -143,7 +143,7 @@ mod:block {
offset = 0,
count = 9,
shift_destination = "inv_input",
shift_destination = "inv_input,inv_fuel",
}
gui:image {

View File

@ -123,7 +123,7 @@ function show_inventory(client, screen_width, screen_height, gui_scale)
inventory = "temp",
size = 2,
shift_destination = "inv_main",
shift_destination = "inv_main,inv_hotbar",
}
gui:show(client)

View File

@ -79,7 +79,7 @@ mod:block {
inventory = "block",
block = {x = pos.x, y = pos.y, z = pos.z},
shift_destination = "inv_main",
shift_destination = "inv_main,inv_hotbar",
}
gui:image {