From bbafab8e00b5a77c00a1ca857d09fd1f2445d529 Mon Sep 17 00:00:00 2001 From: Vanic Date: Mon, 27 Apr 2026 19:36:16 +0300 Subject: [PATCH] `PlayerInventoryUI`: Organizing the equipment and bag tabs. Creating a bag mechanic. #34 --- mods/CORE/Player/src/Player.lua | 3 + .../src/PlayerInventoryUI.lua | 106 ++++++++++++++++++ .../src/PlayerInventoryUI/getBagTab.lua | 68 ++++++++++- .../src/PlayerInventoryUI/getEquipTab.lua | 25 +++-- .../player_inventory_equip_tab_background.png | Bin 101860 -> 101860 bytes ...ry_equip_tab_armor_slots_overlay_sheet.png | Bin 4341 -> 4341 bytes 6 files changed, 191 insertions(+), 11 deletions(-) diff --git a/mods/CORE/Player/src/Player.lua b/mods/CORE/Player/src/Player.lua index b2be420..8920f01 100644 --- a/mods/CORE/Player/src/Player.lua +++ b/mods/CORE/Player/src/Player.lua @@ -15,6 +15,9 @@ core.register_on_joinplayer(function(player) inv:set_size('equip_legs', 2) inv:set_size('equip_feet', 2) inv:set_size('listring_equip', 1) -- Slot for correct listring. + inv:set_size('equip_back', 1) + inv:set_size('equip_left_thigh', 1) + inv:set_size('equip_right_thigh', 1) end) core.register_chatcommand('show_inv', diff --git a/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI.lua b/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI.lua index 0a41a5a..4acb57d 100644 --- a/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI.lua +++ b/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI.lua @@ -555,6 +555,111 @@ core.register_allow_player_inventory_action(function(player, action, inventory, end end) +---Returns the name of the inventory list that will contain the items in the bag. +---@param equipList string +---@return string +local function getBagInventoryListName(equipList) + local bagInventoryList = string.match(equipList, 'equip_(%S+)') + assert(bagInventoryList ~= nil) + + return 'inventory_'..bagInventoryList +end + +---Creates a corresponding inventory list by getting it from the item's metadata. +---@param inventory InvRef +---@param equipList string +---@param bagItem ItemStack +local function onBagEquipped(inventory, equipList, bagItem) + local bagInventoryListName = getBagInventoryListName(equipList) + local bagInventoryList = Smc.getItemInventoryList(bagItem) or {} + + inventory:set_size(bagInventoryListName, Smc.getItemInventoryListSize(bagItem)) + inventory:set_list(bagInventoryListName, bagInventoryList) +end + +---Saves the inventory list corresponding to the equipped bag to the item's metadata. +---@param inventory InvRef +---@param equipList string +---@param bagItem ItemStack +local function onBagUnequipped(inventory, equipList, bagItem) + local bagInventoryListName = getBagInventoryListName(equipList) + local bagInventoryList = inventory:get_list(bagInventoryListName) + + Smc.setItemInventoryList(bagItem, bagInventoryList) + + inventory:set_size(bagInventoryListName, 0) +end + +---Checks if a slot is a bag slot by matching the inventory list name. +---@param equipGroup string +---@return boolean +local function isBagSlot(equipGroup) + if equipGroup == 'equip_back' or equipGroup == 'equip_left_thigh' or equipGroup == 'equip_right_thigh' then + return true + else + return false + end +end + +---Handling the movement of bags slots in the player's inventory. +---@param inventory InvRef +---@param inventoryInfo table +local function onBagSlotMove(inventory, inventoryInfo) + -- We get the copy items currently at the destination and source positions. + local newItem = inventory:get_stack(inventoryInfo.to_list, inventoryInfo.to_index) + local oldItem = inventory:get_stack(inventoryInfo.from_list, inventoryInfo.from_index) + + -- Handling the case where both slots are bag slots. + if isBagSlot(inventoryInfo.to_list) and isBagSlot(inventoryInfo.from_list) then + -- If the target slot is empty (we put a bag from one slot to another), + -- then we unequip the bag and equip it again + if oldItem:is_empty() then + onBagUnequipped(inventory, inventoryInfo.from_list, newItem) + onBagEquipped(inventory, inventoryInfo.to_list, newItem) + -- If a slot is occupied, we swap bags. + -- We unequip them, then equip them in their correct slots. + else + onBagUnequipped(inventory, inventoryInfo.to_list, oldItem) + onBagUnequipped(inventory, inventoryInfo.from_list, newItem) + onBagEquipped(inventory, inventoryInfo.from_list, oldItem) + onBagEquipped(inventory, inventoryInfo.to_list, newItem) + end + + -- Handling the case where only the target slot is a bag slot + -- (equipping a bag from other inventory slots). + elseif isBagSlot(inventoryInfo.to_list) and not isBagSlot(inventoryInfo.from_list) then + -- If the target slot is empty (we put a bag from one slot to another), + -- then we just equip the bag. + if oldItem:is_empty() then + onBagEquipped(inventory, inventoryInfo.to_list, newItem) + -- Else we swap the bags. + -- We unequip the old bag and equip the new one. + else + onBagUnequipped(inventory, inventoryInfo.to_list, oldItem) + onBagEquipped(inventory, inventoryInfo.to_list, newItem) + end + + -- Handling the case where a bag is moved from a bag slot to a non-bag slot. + elseif not isBagSlot(inventoryInfo.to_list) and isBagSlot(inventoryInfo.from_list) then + -- If the target slot is empty, + -- then we just unequip the bag. + if oldItem:is_empty() then + onBagUnequipped(inventory, inventoryInfo.from_list, newItem) + end + -- The case where the slot is occupied should not be handling here. + -- It is handling in the previous else-if block. + end + + -- We are updating the item stacks in the inventory, + -- since we were previously working with copies + -- (we need to save the items with new metadata). + inventory:set_stack(inventoryInfo.to_list, inventoryInfo.to_index, newItem) + inventory:set_stack(inventoryInfo.from_list, inventoryInfo.from_index, oldItem) +end + +-- TODO: скорее всего будет проблема со сваппингом сумок из сундуков +-- Возможно стоит запретить свапать сумки + -- Update the interface if the player has moved something from the equipment slots -- (this enables and disables the slot overlay). core.register_on_player_inventory_action(function(player, action, inventory, inventoryInfo) @@ -562,6 +667,7 @@ core.register_on_player_inventory_action(function(player, action, inventory, inv if action == 'move' then local equipGroup = string.match(inventoryInfo.to_list, 'equip_%S+') or string.match(inventoryInfo.from_list, 'equip_%S+') if equipGroup ~= nil then + onBagSlotMove(inventory, inventoryInfo) PlayerInventoryUI:showForm(playerName) end elseif action == 'put' then diff --git a/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/getBagTab.lua b/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/getBagTab.lua index ea6039d..9d524bb 100644 --- a/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/getBagTab.lua +++ b/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/getBagTab.lua @@ -6,10 +6,76 @@ local Formspec = Smc.requireFormspec() ---@return Formspec local function getBagTab(self, playerName, tabType) local BagTab = Formspec:new() + local BagLists = Formspec:new() + local player = core.get_player_by_name(playerName) + assert(player ~= nil) + local inventory = player:get_inventory() + local inventoryBackSize = inventory:get_size('inventory_back') or 0 + local inventoryLeftThighSize = inventory:get_size('inventory_left_thigh') or 0 + local inventoryRightThighSize = inventory:get_size('inventory_right_thigh') or 0 + + local containerHeight = 0.0 + + if inventoryBackSize > 0 then + local bag = inventory:get_stack('equip_back', 1) + + if not bag:is_empty() then + local background = Smc.getItemInventoryListBackground(bag) + local lines = math.ceil(inventoryBackSize/8) + + BagLists + :image(0.5, 0, 9.875, (lines)*1.25+1, background) + :itemImage(0.5625, 0.0625, 1, 1, bag:get_name()) + :label(1.7625, 0.5, bag:get_definition()._title or '') + :list('current_player', 'inventory_back', 0.5625, 1.25, 8, 4) + + containerHeight = containerHeight + lines * 1.25 + 1 + end + end + + if inventoryLeftThighSize > 0 then + local bag = inventory:get_stack('equip_left_thigh', 1) + + if not bag:is_empty() then + local background = Smc.getItemInventoryListBackground(bag) + local lines = math.ceil(inventoryLeftThighSize/8) + + BagLists + :image(0.5, containerHeight, 9.875, (lines)*1.25+1, background) + :itemImage(0.5625, containerHeight + 0.0625, 1, 1, bag:get_name()) + :label(1.7625, containerHeight + 0.5, bag:get_definition()._title or '') + :list('current_player', 'inventory_left_thigh', 0.5625, containerHeight + 1.25, 8, 4) + + containerHeight = containerHeight + lines * 1.25 + 1 + end + end + + if inventoryRightThighSize > 0 then + local bag = inventory:get_stack('equip_right_thigh', 1) + + if not bag:is_empty() then + local background = Smc.getItemInventoryListBackground(bag) + local lines = math.ceil(inventoryRightThighSize/8) + + BagLists + :image(0.5, containerHeight, 9.875, (lines)*1.25+1, background) + :itemImage(0.5625, containerHeight + 0.0625, 1, 1, bag:get_name()) + :label(1.7625, containerHeight + 0.5, bag:get_definition()._title or '') + :list('current_player', 'inventory_right_thigh', 0.5625, containerHeight + 1.25, 8, 4) + + containerHeight = containerHeight + lines * 1.25 + 1 + end + end BagTab :image(0, 0, 11, 9, 'player_inventory_tab_background.png') - :label(2,2, 'BAG PANE') + :scrollContainer(BagLists, 0, 0.5, 10.5, 8, 'bag_tab_scrollbar', 'vertical') + + if containerHeight > 8 then + BagTab + :scrollbarOptions({min = 0, max = math.round((containerHeight-8)*10+10)}) + :scrollbar(10.5, 0.5, 0.25, 8, 'vertical', 'bag_tab_scrollbar') + end return BagTab end diff --git a/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/getEquipTab.lua b/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/getEquipTab.lua index 87eb595..d24cfd7 100644 --- a/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/getEquipTab.lua +++ b/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/getEquipTab.lua @@ -15,12 +15,17 @@ local function getEquipTab(self, playerName, tabType) EquipTab :image(0, 0, 11, 9, 'player_inventory_equip_tab_background.png') -- Equipment slots: - :label(equipListsX, 0.75, 'cloth') - :label(equipListsX+1.25, 0.75, 'armor') - :list('current_player', 'equip_head', equipListsX, 1, 2, 1) - :list('current_player', 'equip_torso', equipListsX, 2.25, 2, 1) - :list('current_player', 'equip_legs', equipListsX, 3.5, 2, 1) - :list('current_player', 'equip_feet', equipListsX, 4.75, 2, 1) + :list('current_player', 'equip_head', 56/16, 45/16, 1, 1) + :list('current_player', 'equip_head', 44/16, 33/16, 1, 1, 1) + :list('current_player', 'equip_torso', 104/16, 45/16, 1, 1) + :list('current_player', 'equip_torso', 116/16, 33/16, 1, 1, 1) + :list('current_player', 'equip_legs', 56/16, 65/16, 1, 1) + :list('current_player', 'equip_legs', 44/16, 77/16, 1, 1, 1) + :list('current_player', 'equip_feet', 104/16, 65/16, 1, 1) + :list('current_player', 'equip_feet', 116/16, 77/16, 1, 1, 1) + :list('current_player', 'equip_left_thigh', 60/16, 97/16, 1, 1) + :list('current_player', 'equip_back', 80/16, 97/16, 1, 1) + :list('current_player', 'equip_right_thigh', 100/16, 97/16, 1, 1) -- Hidden equipment slot for correct listing -- (distribute equipment across its slots while holding shift): :list('current_player', 'listring_equip', 0, 0, 1, 1) @@ -46,16 +51,16 @@ local function getEquipTab(self, playerName, tabType) -- If the equipment slots are empty, then we apply an overlay. -- TODO: оверлей одежды if inv:get_stack('equip_head', 2):is_empty() then - EquipTab:image(equipListsX+1.25, 1, 1, 1, slotsOverlayTileSheet:t(0,0)) + EquipTab:image(44/16, 33/16, 1, 1, slotsOverlayTileSheet:t(0,0)) end if inv:get_stack('equip_torso', 2):is_empty() then - EquipTab:image(equipListsX+1.25, 2.25, 1, 1, slotsOverlayTileSheet:t(0,1)) + EquipTab:image(116/16, 33/16, 1, 1, slotsOverlayTileSheet:t(0,1)) end if inv:get_stack('equip_legs', 2):is_empty() then - EquipTab:image(equipListsX+1.25, 3.5, 1, 1, slotsOverlayTileSheet:t(0,2)) + EquipTab:image(44/16, 77/16, 1, 1, slotsOverlayTileSheet:t(0,2)) end if inv:get_stack('equip_feet', 2):is_empty() then - EquipTab:image(equipListsX+1.25, 4.75, 1, 1, slotsOverlayTileSheet:t(0,3)) + EquipTab:image(116/16, 77/16, 1, 1, slotsOverlayTileSheet:t(0,3)) end return EquipTab diff --git a/mods/UI/PlayerInventoryUI/textures/player_inventory_equip_tab_background.png b/mods/UI/PlayerInventoryUI/textures/player_inventory_equip_tab_background.png index 9973b6d167687829d2032a0874d182ce6194f491..ec549bbb23fadf47a7fe5c55564f9f3b6bcdcfbc 100644 GIT binary patch delta 2045 zcmZ`)eQZ-z6o0qx?IYcm7U-mI2#=(5oph{&HWX5IRWMsJNHv-a6PGQPILVSujnHL7 z20A5Z$gXdptm{nwFivqJ+`j2E1EnNtG9}A~BSsR*Kmr&;bkTnlVm#;F`__rGNndZz z*FEQVe&@XF!-dy}3kQxCyB(Vg275;Kv3K{@=rgz9bY_E|LZz+MSE5g6s%=7axIoK% zJg)5>ifZ=MjQD*YWK4@rs5GZ)=hE}}<7v=L(Oh5hct7!Lw$v3R^vJaq?LGBnu6P4v zUIUcP1TOfhj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N%rEW_8z`-hc@IVtwpJ_D01Ch+h3ev9IsqDQ9k|MgTf-#l}DI^L;qxUT}{Bv?P zp^&S%ne9JK_5|qX*eT7puf8&r`A$oj_Y<^BwN`i_L^AIQIilMVADJx^ZZ!slA#U(1q-Gl!HsKT69^fOabrwQ?5<2Ou z2Iuvd#e}tRof5$7<1o48EipEr%S1-mLYAETdWz0Bv=oPM=Qv_Wd~xpDT`I5!o*Y=l zD2&YzQ`=vLz_RfrU(M{FNTtUi=`fnj^TZg-leE@{uAp%zADMjvY(3>7rjbi(Xb%?| zg5p4{Cwf}DQ!dEq>>|{mAJmPeweJG*HZr)xMVQU=<3h7Wv}HiweS9;QJWfY`k5py< zs-z7dBJ}LF)MX>r@7F1vugbCnd&X0%pL_T4oTL=x&ff$oJMS{W-q4W zYK)`1=0@=mH4&wE+kh(t?iQO&Kn@oGb{Jv7%^95L$|bvv=rehJzih=Y2#b$%2Gik{ zMVa_fY|t*<-hn$_Gnv{SA6+YX{L9gf*+)^T1V&`6%a_(>DiK?}I*a_T5^n&}E3G~_ zHG{~Y-g8hdPP{6cS`K#;xT_YW0>1&SJbnhW7qeeL8x5l?=gG?jkgt(d+%xlvaR**D z*J~02Kc!S!`M3&y=7OmWm0BCCA#!wFz5fuFI|)Ygw8A@LrIJ1vo(sC2Hfd_RsF$g8 zi2&1xxI^x)yGwMtKuZE?6}UB{lM$I(D!NvhdDmJW+N4@j+gwf`eY>(7+p`sR>jH7rs~2Q_2FX2=pyz($Khk`(`~&&{{WOk0TciL diff --git a/mods/UI/PlayerInventoryUI/textures/sheets/player_inventory_equip_tab_armor_slots_overlay_sheet.png b/mods/UI/PlayerInventoryUI/textures/sheets/player_inventory_equip_tab_armor_slots_overlay_sheet.png index 472d81e1d8a812554a26e97c16481b3e6ddac395..d87df827227b7ae4b5a4332edec4ab9ac3ca7474 100644 GIT binary patch delta 644 zcmY*X&r1SP5Pr37?kc;AY-|1~kq#|H34xaalc+u+$0PSv699rGe|t%Ux8 z?w-7eN9m?`QcMJ0df&5=bzt5MGvD{ko5vgH#<}MQZ996b-?zIN@8woAP1A*Y^$b7L zwyKFl!ezn?!$4sDeqYAnaOlb;a43OXrXUvz1(r^ynPpjQFc>i3_gOq1XS3O?G@VYr zgPausNv;ZZKA&g3UQfpPe9lIrk#tp6nW88wDr+Q1qd_KXwOXvx=}0owuorfseb%Y5 zh~8&7N;Fgi+^Z+~X-LmbCKHK4o6K<>nTjb3n6ck%Hf0He0_|lD!-W(-3Z)K!DVNKv zR;w}M(HIbPU6(xItKr=&-WCpCu~?LS+qOxYKbk3CQnfv*p@BO<8yvhO^u_YLv1vQ> z2mYuWQ7JKnn$f|jUx^;hwal3x+E9kEVs4u74u PpDwhnF0U^>&MS9+0$!}r delta 87 zcmeyW_*HR2m$