PlayerInventory & PlayerInventoryUI: Completion of work on a sane version of the crafting menu. #34

This commit is contained in:
Van
2026-04-03 21:45:24 +03:00
parent 91603e21f8
commit 427c674103
9 changed files with 102 additions and 46 deletions

View File

@@ -3,23 +3,43 @@ local PrimitiveCraftHandler = Smc.requireCraftSystemPrimitiveCraftHandler()
---@type PlayerInventoryUI ---@type PlayerInventoryUI
local PlayerInventoryUI = Mod.getInfo('smc__ui__player_inventory').require('PlayerInventoryUI') local PlayerInventoryUI = Mod.getInfo('smc__ui__player_inventory').require('PlayerInventoryUI')
PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::%S+::item_button',function(player, fieldName, fieldValue) -- Handling the button click for selecting an item for crafting.
local craftName, paneType = string.match(fieldName, '(%S+)::(%S+)::item_button') -- Opens the item description and allows you to craft the selected item.
PlayerInventoryUI:registerOnFormFieldActionMatch('.+::%S+::item_button',function(player, fieldName, fieldValue)
local craftName, paneType = string.match(fieldName, '(.+)::(%S+)::item_button')
assert(craftName ~= nil) assert(craftName ~= nil)
assert(paneType == 'left' or paneType == 'right') assert(paneType == 'left' or paneType == 'right')
local playerName = player:get_player_name() local playerName = player:get_player_name()
PlayerInventoryUI:setContex(playerName, paneType..'_pane_craft_selected_craft', craftName) PlayerInventoryUI:setContex(playerName, paneType..'_pane_craft_selected_craft', craftName)
player:set_inventory_formspec(PlayerInventoryUI:getFormSpecString(playerName))
PlayerInventoryUI:showForm(playerName) PlayerInventoryUI:showForm(playerName)
end) end)
PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::craft_item', function(player, fieldName, fieldValue) -- Handling the click of the crafting group selection button.
-- Causes grouped items to be expanded into a separate list.
PlayerInventoryUI:registerOnFormFieldActionMatch('.+::%S+::group_button',function(player, fieldName, fieldValue)
local groupName, paneType = string.match(fieldName, '(.+)::(%S+)::group_button')
assert(groupName ~= nil)
assert(paneType == 'left' or paneType == 'right')
local playerName = player:get_player_name()
PlayerInventoryUI:setContex(playerName, paneType..'_pane_craft_selected_group', groupName)
PlayerInventoryUI:showForm(playerName)
end)
Smc.registerGlobalStepForEachPlayer(1, function(deltaTime, player)
local playerName = player:get_player_name()
if Smc.getActiveFormspec(playerName) == '' then
player:set_inventory_formspec(PlayerInventoryUI:getFormSpecString(playerName))
end
end)
PlayerInventoryUI:registerOnFormFieldActionMatch('.+::craft_item', function(player, fieldName, fieldValue)
---@type InvRef ---@type InvRef
local inv = player:get_inventory() local inv = player:get_inventory()
local playerName = player:get_player_name() local playerName = player:get_player_name()
local paneType = string.match(fieldName, '(%S+)::craft_item') local paneType = string.match(fieldName, '(.+)::craft_item')
assert(paneType == 'left' or paneType == 'right') assert(paneType == 'left' or paneType == 'right')
local craftName = PlayerInventoryUI:getContex(playerName, paneType..'_pane_craft_selected_craft') local craftName = PlayerInventoryUI:getContex(playerName, paneType..'_pane_craft_selected_craft')

View File

@@ -83,6 +83,9 @@ end
--- ---
--- 'left_pane_craft_selected_craft'| --- 'left_pane_craft_selected_craft'|
--- 'right_pane_craft_selected_craft'| --- 'right_pane_craft_selected_craft'|
--- 'left_pane_craft_selected_group'|
--- 'right_pane_craft_selected_group'|
--- 'group_image_number'|
--- 'left_pane_craft_page_number'| --- 'left_pane_craft_page_number'|
--- 'right_pane_craft_page_number'| --- 'right_pane_craft_page_number'|
--- 'left_pane_craft_scroll'| --- 'left_pane_craft_scroll'|
@@ -95,11 +98,20 @@ end
---@field setContex fun(self:PlayerInventoryUI,player:string|Player,key:PlayerInventoryUI.ContexKey,value:any) ---@field setContex fun(self:PlayerInventoryUI,player:string|Player,key:PlayerInventoryUI.ContexKey,value:any)
local PlayerInventoryUI = BaseUI:new('', getFormSpecString, false) local PlayerInventoryUI = BaseUI:new('', getFormSpecString, false)
local showForm = PlayerInventoryUI.showForm
function PlayerInventoryUI:showForm(playerName)
local player = core.get_player_by_name(playerName)
assert(player ~= nil)
player:set_inventory_formspec(PlayerInventoryUI:getFormSpecString(playerName))
return showForm(self, playerName)
end
PlayerInventoryUI:registerOnFormFieldAction('close_left_pane', function(player, fieldName, fieldValue) PlayerInventoryUI:registerOnFormFieldAction('close_left_pane', function(player, fieldName, fieldValue)
local playerName = player:get_player_name() local playerName = player:get_player_name()
PlayerInventoryUI:setContex(playerName, 'left_pane_name', 'select_pane_menu') PlayerInventoryUI:setContex(playerName, 'left_pane_name', 'select_pane_menu')
player:set_inventory_formspec(PlayerInventoryUI:getFormSpecString(playerName))
PlayerInventoryUI:showForm(playerName) PlayerInventoryUI:showForm(playerName)
end) end)
@@ -110,7 +122,6 @@ PlayerInventoryUI:registerOnFormFieldActionMatch('select_left_%S+', function(pla
local playerName = player:get_player_name() local playerName = player:get_player_name()
PlayerInventoryUI:setContex(playerName, 'left_pane_name', paneName) PlayerInventoryUI:setContex(playerName, 'left_pane_name', paneName)
player:set_inventory_formspec(PlayerInventoryUI:getFormSpecString(playerName))
PlayerInventoryUI:showForm(playerName) PlayerInventoryUI:showForm(playerName)
end) end)
@@ -118,7 +129,6 @@ PlayerInventoryUI:registerOnFormFieldAction('close_right_pane', function(player,
local playerName = player:get_player_name() local playerName = player:get_player_name()
PlayerInventoryUI:setContex(playerName, 'right_pane_name', 'select_pane_menu') PlayerInventoryUI:setContex(playerName, 'right_pane_name', 'select_pane_menu')
player:set_inventory_formspec(PlayerInventoryUI:getFormSpecString(playerName))
PlayerInventoryUI:showForm(playerName) PlayerInventoryUI:showForm(playerName)
end) end)
@@ -129,7 +139,6 @@ PlayerInventoryUI:registerOnFormFieldActionMatch('select_right_%S+', function(pl
local playerName = player:get_player_name() local playerName = player:get_player_name()
PlayerInventoryUI:setContex(playerName, 'right_pane_name', paneName) PlayerInventoryUI:setContex(playerName, 'right_pane_name', paneName)
player:set_inventory_formspec(PlayerInventoryUI:getFormSpecString(playerName))
PlayerInventoryUI:showForm(playerName) PlayerInventoryUI:showForm(playerName)
end) end)
@@ -146,7 +155,6 @@ PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::page_back', function(play
end end
PlayerInventoryUI:setContex(playerName, paneType..'_pane_craft_page_number', pageNumber) PlayerInventoryUI:setContex(playerName, paneType..'_pane_craft_page_number', pageNumber)
player:set_inventory_formspec(PlayerInventoryUI:getFormSpecString(playerName))
PlayerInventoryUI:showForm(playerName) PlayerInventoryUI:showForm(playerName)
end) end)
@@ -163,7 +171,15 @@ PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::page_forward', function(p
end end
PlayerInventoryUI:setContex(playerName, paneType..'_pane_craft_page_number', pageNumber) PlayerInventoryUI:setContex(playerName, paneType..'_pane_craft_page_number', pageNumber)
player:set_inventory_formspec(PlayerInventoryUI:getFormSpecString(playerName)) PlayerInventoryUI:showForm(playerName)
end)
PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::close_group', function(player, fieldName, fieldValue)
local paneType = stringMatch(fieldName, '(%S+)::close_group')
assert(paneType == 'left' or paneType == 'right')
local playerName = player:get_player_name()
PlayerInventoryUI:setContex(playerName, paneType..'_pane_craft_selected_group', nil)
PlayerInventoryUI:showForm(playerName) PlayerInventoryUI:showForm(playerName)
end) end)

View File

@@ -1,12 +1,12 @@
local Formspec = Smc.requireFormspec() local Formspec = Smc.requireFormspec()
local BagPane = Formspec:new()
---@param self PlayerInventoryUI ---@param self PlayerInventoryUI
---@param playerName string ---@param playerName string
---@param paneType PlayerInventoryUI.PaneType ---@param paneType PlayerInventoryUI.PaneType
---@return Formspec ---@return Formspec
local function getBagPane(self, playerName, paneType) local function getBagPane(self, playerName, paneType)
local BagPane = Formspec:new()
BagPane: BagPane:
image(0, 0, 11, 9, 'player_inventory_pane_background.png'): image(0, 0, 11, 9, 'player_inventory_pane_background.png'):
label(2,2, 'BAG PANE') label(2,2, 'BAG PANE')

View File

@@ -1,3 +1,5 @@
local mathFloor = math.floor
---Number of crafting recipes on one menu page. ---Number of crafting recipes on one menu page.
local PAGE_NUMBER_ITEMS = 24 local PAGE_NUMBER_ITEMS = 24
@@ -5,8 +7,6 @@ local PrimitiveCraftHandler = Smc.requireCraftSystemPrimitiveCraftHandler()
local Formspec = Smc.requireFormspec() local Formspec = Smc.requireFormspec()
local CraftPane = Formspec:new()
---@param self PlayerInventoryUI ---@param self PlayerInventoryUI
---@param playerName string ---@param playerName string
---@param paneType PlayerInventoryUI.PaneType ---@param paneType PlayerInventoryUI.PaneType
@@ -16,24 +16,24 @@ local function getCraftPane(self, playerName, paneType)
error('Incorrect argument `paneType`: '..paneType) error('Incorrect argument `paneType`: '..paneType)
end end
local selectedCraft = self:getContex(playerName, paneType..'_pane_craft_selected_craft') local CraftPane = Formspec:new()
---@type string
local selectedCraft = self:getContex(playerName, paneType..'_pane_craft_selected_craft') or ''
---@type CraftSystem.CraftGroup?
local selectedGroup = self:getContex(playerName, paneType..'_pane_craft_selected_group')
local pageNumber = self:getContex(playerName, paneType..'_pane_craft_page_number') or 1 local pageNumber = self:getContex(playerName, paneType..'_pane_craft_page_number') or 1
local learnedCrafts = PrimitiveCraftHandler:getLearnedCrafts(playerName) local crafts
---@type CraftSystem.PrimitiveCraft[] if selectedGroup == nil then
local crafts = {} ---@type (CraftSystem.PrimitiveCraft[]|CraftSystem.PrimitiveCraft)[]
crafts = PrimitiveCraftHandler:getLearnedCrafts(playerName)
local craftsListForm = Formspec:new() else
---@type CraftSystem.PrimitiveCraft[]
for _, craft in pairs(learnedCrafts) do crafts = PrimitiveCraftHandler:getCraftsByGroup(selectedGroup)
table.insert(crafts, craft)
end end
table.sort(crafts, function(a, b) local craftsListForm = Formspec:new()
if a.output < b.output then
return true
end
end)
---Maximum number of crafting recipe pages ---Maximum number of crafting recipe pages
---@type integer ---@type integer
@@ -44,24 +44,45 @@ local function getCraftPane(self, playerName, paneType)
self:setContex(playerName, paneType..'_pane_craft_page_number', pageNumber) self:setContex(playerName, paneType..'_pane_craft_page_number', pageNumber)
end end
local registeredItems = core.registered_items
local w, h = 0.0, 0.0 local w, h = 0.0, 0.0
if selectedGroup ~= nil then
craftsListForm:
imageButton(0, 0, 1.125, 1.125, 'player_inventory_craft_pane_close_group_button_background.png', paneType..'::close_group')
w = 1.125
end
local time = mathFloor(core.get_us_time()/1000000)
for i = PAGE_NUMBER_ITEMS*pageNumber-PAGE_NUMBER_ITEMS+1, PAGE_NUMBER_ITEMS*pageNumber do for i = PAGE_NUMBER_ITEMS*pageNumber-PAGE_NUMBER_ITEMS+1, PAGE_NUMBER_ITEMS*pageNumber do
local craft = crafts[i] local craft = crafts[i]
if craft == nil then if craft == nil then
break break
end end
if craft.name == selectedCraft then
---@type ItemStackString?
local output = craft.output
if output ~= nil then
if craft.name == selectedCraft then
craftsListForm:
imageButton(w, h, 1.125, 1.125, 'player_inventory_craft_pane_button_pressed_background.png', craft.name..'::'..paneType..'::item_button')
else
craftsListForm:
imageButton(w, h, 1.125, 1.125, 'player_inventory_craft_pane_button_background.png', craft.name..'::'..paneType..'::item_button')
end
craftsListForm: craftsListForm:
imageButton(w, h, 1.125, 1.125, 'player_inventory_craft_pane_button_pressed_background.png', craft.name..'::'..paneType..'::item_button') itemImage(w+0.0625, h+0.0625, 1, 1, output)
else else
local k = time%#craft+1
craft = craft[k]
assert(craft ~= nil)
craftsListForm: craftsListForm:
imageButton(w, h, 1.125, 1.125, 'player_inventory_craft_pane_button_background.png', craft.name..'::'..paneType..'::item_button') imageButton(w, h, 1.125, 1.125, 'player_inventory_craft_pane_button_background.png', craft.group..'::'..paneType..'::group_button'):
itemImage(w+0.0625, h+0.0625, 1, 1, craft.output)
end end
craftsListForm:
itemImage(w+0.0625, h+0.0625, 1, 1, craft.output)
w = w + 1.125 w = w + 1.125
if w >= 4 then if w >= 4 then
@@ -71,13 +92,13 @@ local function getCraftPane(self, playerName, paneType)
end end
CraftPane: CraftPane:
image(0, 0, 11, 9, 'player_inventory_craft_pane_background.png'): background(0, 0, 11, 9, 'player_inventory_craft_pane_background.png'):
container(craftsListForm, 0.5, 0.5): container(craftsListForm, 0.5, 0.5):
button(1.75, 7.75, 1, 0.5, paneType..'::page_back', '<'): button(1.75, 7.75, 1, 0.5, paneType..'::page_back', '<'):
button(2.75, 7.75, 1, 0.5, paneType..'::page_forward', '>'): button(2.75, 7.75, 1, 0.5, paneType..'::page_forward', '>'):
label(2.5,8.5, ('%s/%s'):format(pageNumber, pagesMax)) label(2.5,8.5, ('%s/%s'):format(pageNumber, pagesMax))
if selectedCraft ~= nil then if selectedCraft ~= '' then
local craft = PrimitiveCraftHandler:getCraftByName(selectedCraft) local craft = PrimitiveCraftHandler:getCraftByName(selectedCraft)
local item = ItemStack(craft.output) local item = ItemStack(craft.output)
local itemDef = item:get_definition() local itemDef = item:get_definition()

View File

@@ -1,12 +1,12 @@
local Formspec = Smc.requireFormspec() local Formspec = Smc.requireFormspec()
local EquipPane = Formspec:new()
---@param self PlayerInventoryUI ---@param self PlayerInventoryUI
---@param playerName string ---@param playerName string
---@param paneType PlayerInventoryUI.PaneType ---@param paneType PlayerInventoryUI.PaneType
---@return Formspec ---@return Formspec
local function getEquipPane(self, playerName, paneType) local function getEquipPane(self, playerName, paneType)
local EquipPane = Formspec:new()
EquipPane: EquipPane:
image(0, 0, 11, 9, 'player_inventory_pane_background.png'): image(0, 0, 11, 9, 'player_inventory_pane_background.png'):
label(2,2, 'EQUIPMENT PANE') label(2,2, 'EQUIPMENT PANE')

View File

@@ -1,12 +1,12 @@
local Formspec = Smc.requireFormspec() local Formspec = Smc.requireFormspec()
local RefPane = Formspec:new()
---@param self PlayerInventoryUI ---@param self PlayerInventoryUI
---@param playerName string ---@param playerName string
---@param paneType PlayerInventoryUI.PaneType ---@param paneType PlayerInventoryUI.PaneType
---@return Formspec ---@return Formspec
local function getRefPane(self, playerName, paneType) local function getRefPane(self, playerName, paneType)
local RefPane = Formspec:new()
RefPane: RefPane:
image(0, 0, 11, 9, 'player_inventory_pane_background.png'): image(0, 0, 11, 9, 'player_inventory_pane_background.png'):
label(2,2, 'REFERENCE PANE') label(2,2, 'REFERENCE PANE')

View File

@@ -1,12 +1,11 @@
local Formspec = Smc.requireFormspec() local Formspec = Smc.requireFormspec()
local selectPaneMenu = Formspec:new()
---@param self PlayerInventoryUI ---@param self PlayerInventoryUI
---@param playerName string ---@param playerName string
---@param paneType PlayerInventoryUI.PaneType ---@param paneType PlayerInventoryUI.PaneType
---@return Formspec ---@return Formspec
local function getSelectPaneMenu(self, playerName, paneType) local function getSelectPaneMenu(self, playerName, paneType)
local selectPaneMenu = Formspec:new()
selectPaneMenu: selectPaneMenu:
image(0, 0, 11, 9, 'player_inventory_pane_background.png'): image(0, 0, 11, 9, 'player_inventory_pane_background.png'):

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB