PlayerInventoryUI: add equipment slot overlay

This commit is contained in:
Van
2026-04-19 20:31:52 +03:00
parent 89bc05d2cb
commit f34c12c59f
5 changed files with 84 additions and 41 deletions

View File

@@ -1,2 +1,2 @@
name = smc__ui__player_inventory
depends = smc__core__formspec
depends = smc__core__formspec, smc__core__tilesheet

View File

@@ -449,4 +449,64 @@ PlayerInventoryUI:registerOnFormFieldActionMatch('.+::craft_item', function(play
end
end)
-- We only allow items to be placed in equipment slot
-- that have groups that match the slot name and index.
core.register_allow_player_inventory_action(function(player, action, inventory, inventoryInfo)
if action == 'move' then
-- Checking if a slot is an equipment slot.
local equipGroup = string.match(inventoryInfo.to_list, 'equip_%S+')
if equipGroup == nil then
return
end
-- We receive the item that the player wants to place in the equipment slot.
local itemStack = inventory:get_stack(inventoryInfo.from_list, inventoryInfo.from_index)
assert(itemStack ~= nil)
if core.get_item_group(itemStack:get_name(), equipGroup) == inventoryInfo.to_index then
return
else
return 0
end
elseif action == 'put' then
-- Checking if a slot is an equipment slot.
local equipGroup = string.match(inventoryInfo.listname, 'equip_%S+')
if equipGroup == nil then
return
end
-- We receive the item that the player wants to place in the equipment slot.
local itemStack = inventory:get_stack(inventoryInfo.listname, inventoryInfo.index)
assert(itemStack ~= nil)
if core.get_item_group(itemStack:get_name(), equipGroup) == inventoryInfo.index then
return
else
return 0
end
end
end)
-- 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)
local playerName = player:get_player_name()
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
PlayerInventoryUI:showForm(playerName)
end
elseif action == 'put' then
local equipGroup = string.match(inventoryInfo.listname, 'equip_%S+')
if equipGroup ~= nil then
PlayerInventoryUI:showForm(playerName)
end
elseif action == 'take' then
local equipGroup = string.match(inventoryInfo.listname, 'equip_%S+')
if equipGroup ~= nil then
PlayerInventoryUI:showForm(playerName)
end
end
end)
return PlayerInventoryUI

View File

@@ -1,4 +1,6 @@
local Formspec = Smc.requireFormspec()
local Formspec, TileSheet = Smc.requireFormspec(), Smc.requireTileSheet()
local slotsOverlayTileSheet = TileSheet:new('player_inventory_equip_tab_armor_slots_overlay_sheet.png', 16, 16)
---@param self PlayerInventoryUI
---@param playerName string
@@ -20,45 +22,26 @@ local function getEquipTab(self, playerName, tabType)
:list('current_player', 'equip_legs', equipListsX, 3.5, 2, 1)
:list('current_player', 'equip_feet', equipListsX, 4.75, 2, 1)
local player = core.get_player_by_name(playerName)
assert(player)
local inv = player:get_inventory()
-- If the equipment slots are empty, then we apply an overlay.
if inv:get_stack('equip_head', 2):is_empty() then
EquipTab:image(equipListsX+1.25, 1, 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))
end
if inv:get_stack('equip_legs', 2):is_empty() then
EquipTab:image(equipListsX+1.25, 3.5, 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))
end
return EquipTab
end
-- We only allow items to be placed in equipment slot
-- that have groups that match the slot name and index.
core.register_allow_player_inventory_action(function(player, action, inventory, inventoryInfo)
if action == 'move' then
-- Checking if a slot is an equipment slot.
local equipGroup = string.match(inventoryInfo.to_list, 'equip_%S+')
if equipGroup == nil then
return
end
-- We receive the item that the player wants to place in the equipment slot.
local itemStack = inventory:get_stack(inventoryInfo.from_list, inventoryInfo.from_index)
assert(itemStack ~= nil)
if core.get_item_group(itemStack:get_name(), equipGroup) == inventoryInfo.to_index then
return
else
return 0
end
elseif action == 'put' then
-- Checking if a slot is an equipment slot.
local equipGroup = string.match(inventoryInfo.listname, 'equip_%S+')
if equipGroup == nil then
return
end
-- We receive the item that the player wants to place in the equipment slot.
local itemStack = inventory:get_stack(inventoryInfo.listname, inventoryInfo.index)
assert(itemStack ~= nil)
if core.get_item_group(itemStack:get_name(), equipGroup) == inventoryInfo.index then
return
else
return 0
end
end
end)
return getEquipTab

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB