Skip to content

Commit 908bb10

Browse files
committed
[AbstractInventoryWidget] Now supports multiple destinations.
1 parent c092e53 commit 908bb10

10 files changed

+78
-29
lines changed

Diff for: client/source/gui/AbstractInventoryWidget.cpp

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* =====================================================================================
3+
*
4+
* OpenMiner
5+
*
6+
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <[email protected]>
7+
* Copyright (C) 2019-2020 the OpenMiner contributors (see CONTRIBUTORS.md)
8+
*
9+
* This file is part of OpenMiner.
10+
*
11+
* OpenMiner is free software; you can redistribute it and/or
12+
* modify it under the terms of the GNU Lesser General Public
13+
* License as published by the Free Software Foundation; either
14+
* version 2.1 of the License, or (at your option) any later version.
15+
*
16+
* OpenMiner is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19+
* Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License
22+
* along with OpenMiner; if not, write to the Free Software Foundation, Inc.,
23+
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24+
*
25+
* =====================================================================================
26+
*/
27+
#include <sstream>
28+
29+
#include "AbstractInventoryWidget.hpp"
30+
31+
void AbstractInventoryWidget::setShiftDestination(const std::string &shiftDestination) {
32+
m_shiftDestination.clear();
33+
34+
std::stringstream stream(shiftDestination);
35+
std::string item;
36+
while (std::getline(stream, item, ',')) {
37+
m_shiftDestination.emplace_back(item);
38+
}
39+
}
40+

Diff for: client/source/gui/AbstractInventoryWidget.hpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ class AbstractInventoryWidget : public Widget {
3333
public:
3434
AbstractInventoryWidget(Widget *parent) : Widget(parent) {}
3535

36-
virtual void sendItemStackToDest(const ItemWidget *itemStack, AbstractInventoryWidget *dest) = 0;
36+
virtual bool sendItemStackToDest(const ItemWidget *itemStack, AbstractInventoryWidget *dest) = 0;
3737
virtual bool receiveItemStack(const ItemWidget *itemStack) = 0;
3838

39-
const std::string &shiftDestination() const { return m_shiftDestination; }
40-
void setShiftDestination(const std::string &shiftDestination) { m_shiftDestination = shiftDestination; }
39+
const std::vector<std::string> &shiftDestination() const { return m_shiftDestination; }
40+
void setShiftDestination(const std::string &shiftDestination);
4141

4242
private:
43-
std::string m_shiftDestination;
43+
std::vector<std::string> m_shiftDestination;
4444
};
4545

4646
#endif // ABSTRACTINVENTORYWIDGET_HPP_

Diff for: client/source/gui/CraftingWidget.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,15 @@ void CraftingWidget::update() {
8080
}
8181
}
8282

83-
void CraftingWidget::sendItemStackToDest(const ItemWidget *itemStack, AbstractInventoryWidget *dest) {
83+
bool CraftingWidget::sendItemStackToDest(const ItemWidget *itemStack, AbstractInventoryWidget *dest) {
8484
if (m_currentInventoryWidget && dest->receiveItemStack(itemStack)) {
8585
m_currentInventoryWidget->inventory()->clearStack(itemStack->x(), itemStack->y());
8686
m_currentInventoryWidget->update();
8787
m_currentInventoryWidget->sendUpdatePacket();
88+
return true;
8889
}
90+
91+
return false;
8992
}
9093

9194
bool CraftingWidget::receiveItemStack(const ItemWidget *itemStack) {

Diff for: client/source/gui/CraftingWidget.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class CraftingWidget : public AbstractInventoryWidget {
4141

4242
void update() override;
4343

44-
void sendItemStackToDest(const ItemWidget *itemStack, AbstractInventoryWidget *dest) override;
44+
bool sendItemStackToDest(const ItemWidget *itemStack, AbstractInventoryWidget *dest) override;
4545
bool receiveItemStack(const ItemWidget *itemStack) override;
4646

4747
const ItemWidget *currentItemWidget() const { return m_craftingResultInventoryWidget.currentItemWidget() ? m_craftingResultInventoryWidget.currentItemWidget() : m_craftingInventoryWidget.currentItemWidget(); }

Diff for: client/source/gui/InventoryWidget.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,15 @@ void InventoryWidget::update() {
8686
it.update();
8787
}
8888

89-
void InventoryWidget::sendItemStackToDest(const ItemWidget *itemStack, AbstractInventoryWidget *dest) {
89+
bool InventoryWidget::sendItemStackToDest(const ItemWidget *itemStack, AbstractInventoryWidget *dest) {
9090
if (dest->receiveItemStack(itemStack)) {
9191
m_inventory->clearStack(itemStack->x(), itemStack->y());
9292
update();
9393
sendUpdatePacket();
94+
return true;
9495
}
96+
97+
return false;
9598
}
9699

97100
bool InventoryWidget::receiveItemStack(const ItemWidget *itemStack) {

Diff for: client/source/gui/InventoryWidget.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class InventoryWidget : public AbstractInventoryWidget {
4545

4646
void update() override;
4747

48-
void sendItemStackToDest(const ItemWidget *itemStack, AbstractInventoryWidget *dest) override;
48+
bool sendItemStackToDest(const ItemWidget *itemStack, AbstractInventoryWidget *dest) override;
4949
bool receiveItemStack(const ItemWidget *itemStack) override;
5050

5151
void sendUpdatePacket();

Diff for: client/source/states/LuaGUIState.cpp

+17-14
Original file line numberDiff line numberDiff line change
@@ -74,24 +74,27 @@ void LuaGUIState::onEvent(const SDL_Event &event) {
7474
if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT
7575
&& m_currentInventoryWidget && !m_currentInventoryWidget->shiftDestination().empty()
7676
&& m_mouseItemWidget.currentItemWidget() && gk::GamePad::isKeyPressed(GameKey::Shift)) {
77-
AbstractInventoryWidget *dest = nullptr;
77+
for (const std::string &shiftDestination : m_currentInventoryWidget->shiftDestination()) {
78+
AbstractInventoryWidget *dest = nullptr;
7879

79-
auto it = m_craftingWidgets.find(m_currentInventoryWidget->shiftDestination());
80-
if (it != m_craftingWidgets.end())
81-
dest = &it->second;
82-
83-
if (!dest) {
84-
auto it = m_inventoryWidgets.find(m_currentInventoryWidget->shiftDestination());
85-
if (it != m_inventoryWidgets.end())
80+
auto it = m_craftingWidgets.find(shiftDestination);
81+
if (it != m_craftingWidgets.end())
8682
dest = &it->second;
87-
}
8883

89-
if (!dest) {
90-
DEBUG("ERROR: Destination not found: '" + m_currentInventoryWidget->shiftDestination() + "'");
91-
return;
92-
}
84+
if (!dest) {
85+
auto it = m_inventoryWidgets.find(shiftDestination);
86+
if (it != m_inventoryWidgets.end())
87+
dest = &it->second;
88+
}
89+
90+
if (!dest) {
91+
DEBUG("WARNING: Destination not found: '" + shiftDestination + "'");
92+
return;
93+
}
9394

94-
m_currentInventoryWidget->sendItemStackToDest(m_mouseItemWidget.currentItemWidget(), dest);
95+
if (m_currentInventoryWidget->sendItemStackToDest(m_mouseItemWidget.currentItemWidget(), dest))
96+
break;
97+
}
9598
}
9699
else {
97100
for (auto &it : m_inventoryWidgets)

Diff for: mods/default/furnace.lua

+5-5
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ mod:block {
8585
offset = 0,
8686
count = 1,
8787

88-
shift_destination = "inv_main",
88+
shift_destination = "inv_main,inv_hotbar",
8989
}
9090

9191
gui:inventory {
@@ -99,7 +99,7 @@ mod:block {
9999
offset = 1,
100100
count = 1,
101101

102-
shift_destination = "inv_main",
102+
shift_destination = "inv_main,inv_hotbar",
103103
}
104104

105105
gui:inventory {
@@ -113,7 +113,7 @@ mod:block {
113113
offset = 2,
114114
count = 1,
115115

116-
shift_destination = "inv_main",
116+
shift_destination = "inv_main,inv_hotbar",
117117
}
118118

119119
gui:inventory {
@@ -128,7 +128,7 @@ mod:block {
128128
offset = 9,
129129
count = 9 * 3,
130130

131-
shift_destination = "inv_input",
131+
shift_destination = "inv_input,inv_fuel",
132132
}
133133

134134
gui:inventory {
@@ -143,7 +143,7 @@ mod:block {
143143
offset = 0,
144144
count = 9,
145145

146-
shift_destination = "inv_input",
146+
shift_destination = "inv_input,inv_fuel",
147147
}
148148

149149
gui:image {

Diff for: mods/default/init.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ function show_inventory(client, screen_width, screen_height, gui_scale)
123123
inventory = "temp",
124124
size = 2,
125125

126-
shift_destination = "inv_main",
126+
shift_destination = "inv_main,inv_hotbar",
127127
}
128128

129129
gui:show(client)

Diff for: mods/default/workbench.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ mod:block {
7979
inventory = "block",
8080
block = {x = pos.x, y = pos.y, z = pos.z},
8181

82-
shift_destination = "inv_main",
82+
shift_destination = "inv_main,inv_hotbar",
8383
}
8484

8585
gui:image {

0 commit comments

Comments
 (0)