mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-05 19:11:50 +00:00
* implement a bit * change shift to ctrl * add enhancement #2 * add ctrl-interaction with content access * fix libinventory.move bug (none checking) * fix p.5 * move shareinv logic into inventory_utils * add inventory utils * small refactor * fix iframe.src = "" --------- Co-authored-by: MihailRis <mexaruc@gmail.com>
This commit is contained in:
parent
f8e4e2759b
commit
3b3bf8e878
9 changed files with 84 additions and 37 deletions
|
|
@ -106,6 +106,7 @@ std::shared_ptr<UINode> HudElement::getNode() const {
|
|||
std::shared_ptr<InventoryView> Hud::createContentAccess() {
|
||||
auto& content = frontend.getLevel().content;
|
||||
auto& indices = *content.getIndices();
|
||||
auto& input = this->input;
|
||||
auto inventory = player.getInventory();
|
||||
|
||||
size_t itemsCount = indices.items.count();
|
||||
|
|
@ -115,12 +116,19 @@ std::shared_ptr<InventoryView> Hud::createContentAccess() {
|
|||
}
|
||||
|
||||
SlotLayout slotLayout(-1, glm::vec2(), false, true, nullptr,
|
||||
[inventory, &indices](uint, ItemStack& item) {
|
||||
[inventory, &indices, &input](uint, ItemStack& item) {
|
||||
auto copy = ItemStack(item);
|
||||
if (input.pressed(Keycode::LEFT_CONTROL)) {
|
||||
copy.maximizeCount(indices);
|
||||
}
|
||||
inventory->move(copy, indices);
|
||||
},
|
||||
[this, inventory](uint, ItemStack& item) {
|
||||
inventory->getSlot(player.getChosenSlot()).set(item);
|
||||
[this, inventory, &indices, &input](uint, ItemStack& item) {
|
||||
auto copy = ItemStack(item);
|
||||
if (input.pressed(Keycode::LEFT_CONTROL)) {
|
||||
copy.maximizeCount(indices);
|
||||
}
|
||||
inventory->getSlot(player.getChosenSlot()).set(copy);
|
||||
});
|
||||
|
||||
InventoryBuilder builder(gui);
|
||||
|
|
|
|||
|
|
@ -366,28 +366,12 @@ void SlotView::performLeftClick(ItemStack& stack, ItemStack& grabbed) {
|
|||
);
|
||||
return;
|
||||
}
|
||||
|
||||
auto indices = *content->getIndices();
|
||||
if (!layout.itemSource && stack.accepts(grabbed) && layout.placing) {
|
||||
action = InteractionAction::PUT;
|
||||
stack.move(grabbed, *content->getIndices());
|
||||
stack.move(grabbed, indices);
|
||||
} else {
|
||||
if (layout.itemSource) {
|
||||
if (grabbed.isEmpty()) {
|
||||
action = InteractionAction::TAKE;
|
||||
grabbed.set(stack);
|
||||
} else {
|
||||
action = InteractionAction::PUT;
|
||||
grabbed.clear();
|
||||
}
|
||||
} else if (grabbed.isEmpty()) {
|
||||
if (layout.taking) {
|
||||
action = InteractionAction::TAKE;
|
||||
std::swap(grabbed, stack);
|
||||
}
|
||||
} else if (layout.taking && layout.placing) {
|
||||
action = InteractionAction::PUT;
|
||||
std::swap(grabbed, stack);
|
||||
}
|
||||
actIfCannotPut(stack, grabbed, action);
|
||||
}
|
||||
|
||||
if (action != InteractionAction::UNDEFINED) {
|
||||
|
|
@ -400,6 +384,39 @@ void SlotView::performLeftClick(ItemStack& stack, ItemStack& grabbed) {
|
|||
}
|
||||
}
|
||||
|
||||
void SlotView::actIfCannotPut(ItemStack& stack, ItemStack& grabbed, InteractionAction& action) {
|
||||
const auto& input = gui.getInput();
|
||||
auto indices = *content->getIndices();
|
||||
if (layout.itemSource) {
|
||||
if (grabbed.isEmpty()) {
|
||||
action = InteractionAction::TAKE;
|
||||
grabbed.set(stack);
|
||||
if (input.pressed(Keycode::LEFT_CONTROL)) {
|
||||
grabbed.maximizeCount(*content->getIndices());
|
||||
}
|
||||
} else {
|
||||
if (grabbed.accepts(stack)) {
|
||||
auto& def = indices.items.require(stack.getItemId());
|
||||
if (grabbed.getCount() < def.stackSize) {
|
||||
action = InteractionAction::TAKE;
|
||||
grabbed.setCount(grabbed.getCount() + 1);
|
||||
}
|
||||
} else {
|
||||
action = InteractionAction::PUT;
|
||||
grabbed.clear();
|
||||
}
|
||||
}
|
||||
} else if (grabbed.isEmpty()) {
|
||||
if (layout.taking) {
|
||||
action = InteractionAction::TAKE;
|
||||
std::swap(grabbed, stack);
|
||||
}
|
||||
} else if (layout.taking && layout.placing) {
|
||||
action = InteractionAction::PUT;
|
||||
std::swap(grabbed, stack);
|
||||
}
|
||||
}
|
||||
|
||||
void SlotView::performRightClick(ItemStack& stack, ItemStack& grabbed) {
|
||||
if (layout.rightClick) {
|
||||
layout.rightClick(inventoryId, stack);
|
||||
|
|
|
|||
|
|
@ -123,6 +123,8 @@ namespace gui {
|
|||
SHARE = 2,
|
||||
UNDEFINED,
|
||||
};
|
||||
private:
|
||||
void actIfCannotPut(ItemStack& stack, ItemStack& grabbed, InteractionAction& action);
|
||||
};
|
||||
|
||||
class InventoryView final : public gui::Container {
|
||||
|
|
|
|||
|
|
@ -48,6 +48,14 @@ void ItemStack::setCount(itemcount_t count) {
|
|||
}
|
||||
}
|
||||
|
||||
void ItemStack::maximizeCount(const ContentIndices& indices) {
|
||||
auto& def = indices.items.require(getItemId());
|
||||
this->count = def.stackSize;
|
||||
if (count == 0) {
|
||||
clear();
|
||||
}
|
||||
}
|
||||
|
||||
void ItemStack::setField(std::string_view name, dv::value value) {
|
||||
if (fields == nullptr) {
|
||||
if (value == nullptr) {
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ public:
|
|||
void set(const ItemStack& item);
|
||||
void set(ItemStack&& item);
|
||||
void setCount(itemcount_t count);
|
||||
void maximizeCount(const ContentIndices& indices);
|
||||
|
||||
/// @brief Set a field in the item stack data.
|
||||
void setField(std::string_view name, dv::value value);
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
#include "items/Inventories.hpp"
|
||||
#include "items/ItemStack.hpp"
|
||||
#include "logic/BlocksController.hpp"
|
||||
#include "logic/scripting/lua/lua_util.hpp"
|
||||
#include "world/Level.hpp"
|
||||
#include "api_lua.hpp"
|
||||
|
||||
|
|
@ -180,7 +181,7 @@ static int l_move(lua::State* L) {
|
|||
validate_slotid(slotAid, invA);
|
||||
|
||||
auto invBid = lua::tointeger(L, 3);
|
||||
auto slotBid = lua::isnil(L, 4) ? -1 : lua::tointeger(L, 4);
|
||||
auto slotBid = lua::isnoneornil(L, 4) ? -1 : lua::tointeger(L, 4);
|
||||
auto& invB = get_inventory(invBid, 3);
|
||||
auto& slot = invA.getSlot(slotAid);
|
||||
if (slotBid == -1) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue