PlayerInventoryUI: Adding basic panes switching functionality. #34

This commit is contained in:
Van
2026-03-19 21:57:43 +03:00
parent 338cc7a36b
commit e98935cf6f
7 changed files with 94 additions and 23 deletions

View File

@@ -2,13 +2,16 @@ local BaseUI, Formspec = Smc.requireBaseUI(), Smc.requireFormspec()
local require = Mod.getInfo('smc__ui__player_inventory').require
---@alias PlayerInventoryUI.getPaneFunc fun(self:PlayerInventoryUI,playerName:string):string
---@alias PlayerInventoryUI.PaneType 'left'|'right'
---@alias PlayerInventoryUI.getPaneFunc fun(self:PlayerInventoryUI,playerName:string,paneType:PlayerInventoryUI.PaneType):Formspec
---@alias PlayerInventoryUI.PaneName 'select_pane_menu'|'craft_pane'|'equip_pane'|'ref_pane'
---@alias PlayerInventoryUI.PaneName 'select_pane_menu'|'bag_pane'|'craft_pane'|'equip_pane'|'ref_pane'
---@type PlayerInventoryUI.getPaneFunc
local getSelectPaneMenu = require('PlayerInventoryUI.getSelectPaneMenu')
---@type PlayerInventoryUI.getPaneFunc
local getBagPane = require('PlayerInventoryUI.getBagPane')
---@type PlayerInventoryUI.getPaneFunc
local getCraftPane = require('PlayerInventoryUI.getCraftPane')
---@type PlayerInventoryUI.getPaneFunc
local getEquipPane = require('PlayerInventoryUI.getEquipPane')
@@ -18,6 +21,7 @@ local getRefPane = require('PlayerInventoryUI.getRefPane')
---@type table<PlayerInventoryUI.PaneName, PlayerInventoryUI.getPaneFunc>
local panes = {
['select_pane_menu'] = getSelectPaneMenu,
['bag_pane'] = getBagPane,
['craft_pane'] = getCraftPane,
['equip_pane'] = getEquipPane,
['ref_pane'] = getRefPane,
@@ -41,9 +45,9 @@ end
---@return string
local function getFormSpecString(self, playerName)
---@type PlayerInventoryUI.PaneName
local leftPaneName = self:getContex(playerName, 'left_pane_name')
local leftPaneName = self:getContex(playerName, 'left_pane_name') or 'select_pane_menu'
---@type PlayerInventoryUI.PaneName
local rightPaneName = self:getContex(playerName, 'right_pane_name')
local rightPaneName = self:getContex(playerName, 'right_pane_name') or 'select_pane_menu'
local leftPaneIsBlock = self:getContex(playerName, 'left_pane_is_block')
local rightPaneIsBlock = self:getContex(playerName, 'right_pane_is_block')
@@ -54,13 +58,14 @@ local function getFormSpecString(self, playerName)
noPrepend = true,
})
InventoryForm:
--bgcolor('#00000000'):
listColors('#00000000', '#C4C4C433'):
list('current_player', 'main', 5.5,9.5, 9,2):
image(0, 0, 11, 9, 'player_inventory_background_block.png'):
image(11, 0, 11, 9, 'player_inventory_background_block.png'):
background(0, 9, 22, 3.5, 'player_inventory_middle_background.png', false):
container(panes[leftPaneName](self, playerName, 'left'), 0, 0):
container(panes[rightPaneName](self, playerName, 'right'), 11, 0):
button(10.5, 0, .5, .5, 'close_left_pane', 'x'):
button(21.5, 0, .5, .5, 'close_right_pane', 'x')
@@ -93,7 +98,40 @@ function PlayerInventoryUI:closeForm(playerName)
end--]]
PlayerInventoryUI:registerOnFormFieldAction('close_left_pane', function(player, fieldName, fieldValue)
core.log(fieldName)
local playerName = player:get_player_name()
PlayerInventoryUI:setContex(playerName, 'left_pane_name', 'select_pane_menu')
PlayerInventoryUI:showForm(playerName)
end)
PlayerInventoryUI:registerOnFormFieldActionMatch('select_left_%S+', function(player, fieldName, fieldValue)
local paneName = string.match(fieldName, 'select_%S+_(%S+_%S+)')
assert(paneName ~= nil)
local playerName = player:get_player_name()
PlayerInventoryUI:setContex(playerName, 'left_pane_name', paneName)
PlayerInventoryUI:showForm(playerName)
player:set_inventory_formspec(PlayerInventoryUI:getFormSpecString(playerName))
end)
PlayerInventoryUI:registerOnFormFieldAction('close_right_pane', function(player, fieldName, fieldValue)
local playerName = player:get_player_name()
PlayerInventoryUI:setContex(playerName, 'right_pane_name', 'select_pane_menu')
PlayerInventoryUI:showForm(playerName)
player:set_inventory_formspec(PlayerInventoryUI:getFormSpecString(playerName))
end)
PlayerInventoryUI:registerOnFormFieldActionMatch('select_right_%S+', function(player, fieldName, fieldValue)
local paneName = string.match(fieldName, 'select_%S+_(%S+_%S+)')
assert(paneName ~= nil)
local playerName = player:get_player_name()
PlayerInventoryUI:setContex(playerName, 'right_pane_name', paneName)
PlayerInventoryUI:showForm(playerName)
player:set_inventory_formspec(PlayerInventoryUI:getFormSpecString(playerName))
end)
return PlayerInventoryUI

View File

@@ -0,0 +1,17 @@
local Formspec = Smc.requireFormspec()
local BagPane = Formspec:new()
---@param self PlayerInventoryUI
---@param playerName string
---@param paneType PlayerInventoryUI.PaneType
---@return Formspec
local function getBagPane(self, playerName, paneType)
BagPane:
image(0, 0, 11, 9, 'player_inventory_background_block.png^[colorize:#BF59EE:120'):
label(2,2, 'BAG PANE')
return BagPane
end
return getBagPane

View File

@@ -4,11 +4,14 @@ local CraftPane = Formspec:new()
---@param self PlayerInventoryUI
---@param playerName string
---@return string
local function getCraftPane(self, playerName)
---@param paneType PlayerInventoryUI.PaneType
---@return Formspec
local function getCraftPane(self, playerName, paneType)
CraftPane:
image(0, 0, 11, 9, 'player_inventory_background_block.png^[colorize:#E7CC35:120'):
label(2,2, 'CRAFTING PANE')
return CraftPane:toString()
return CraftPane
end
return getCraftPane

View File

@@ -4,11 +4,14 @@ local EquipPane = Formspec:new()
---@param self PlayerInventoryUI
---@param playerName string
---@return string
local function getEquipPane(self, playerName)
---@param paneType PlayerInventoryUI.PaneType
---@return Formspec
local function getEquipPane(self, playerName, paneType)
EquipPane:
image(0, 0, 11, 9, 'player_inventory_background_block.png^[colorize:#72FAC6:120'):
label(2,2, 'EQUIPMENT PANE')
return EquipPane:toString()
return EquipPane
end
return getEquipPane

View File

@@ -4,11 +4,14 @@ local RefPane = Formspec:new()
---@param self PlayerInventoryUI
---@param playerName string
---@return string
local function getRefPane(self, playerName)
---@param paneType PlayerInventoryUI.PaneType
---@return Formspec
local function getRefPane(self, playerName, paneType)
RefPane:
image(0, 0, 11, 9, 'player_inventory_background_block.png^[colorize:#8072FA:120'):
label(2,2, 'REFERENCE PANE')
return RefPane:toString()
return RefPane
end
return getRefPane

View File

@@ -4,11 +4,18 @@ local selectPaneMenu = Formspec:new()
---@param self PlayerInventoryUI
---@param playerName string
---@return string
local function getSelectPaneMenu(self, playerName)
---@param paneType PlayerInventoryUI.PaneType
---@return Formspec
local function getSelectPaneMenu(self, playerName, paneType)
selectPaneMenu:
image(0, 0, 11, 9, 'player_inventory_background_block.png^[colorize:#FA8072:120'):
button(3.5,2.5,2,2,'select_'..paneType..'_bag_pane', 'bag'):
button(5.5,2.5,2,2,'select_'..paneType..'_craft_pane', 'craft'):
button(3.5,4.5,2,2,'select_'..paneType..'_equip_pane', 'equip'):
button(5.5,4.5,2,2,'select_'..paneType..'_ref_pane', 'ref')
return selectPaneMenu:toString()
return selectPaneMenu
end
return getSelectPaneMenu
return getSelectPaneMenu