3 Commits

10 changed files with 194 additions and 8 deletions

View File

@@ -296,7 +296,7 @@ end
---@param w number ---@param w number
---@param h number ---@param h number
---@param textureName string ---@param textureName string
---@param middle boolean? Makes the image render in 9-sliced mode and defines the middle rect. ---@param middle string? Makes the image render in 9-sliced mode and defines the middle rect.
---@return Formspec ---@return Formspec
function Formspec:image(x, y, w, h, textureName, middle) function Formspec:image(x, y, w, h, textureName, middle)
local element = ('image[%s,%s;%s,%s;%s;%s]'):format( local element = ('image[%s,%s;%s,%s;%s;%s]'):format(
@@ -441,7 +441,7 @@ end
--- * All numbers in middle are integers. --- * All numbers in middle are integers.
---@return Formspec ---@return Formspec
function Formspec:background9(x, y, w, h, textureName, autoClip, middle) function Formspec:background9(x, y, w, h, textureName, autoClip, middle)
local element = ('background9[%s,%s;%s,%s;%s;%s]'):format( local element = ('background9[%s,%s;%s,%s;%s;%s;%s]'):format(
x, y, w, h, textureName, autoClip, middle) x, y, w, h, textureName, autoClip, middle)
self:addElement(element) self:addElement(element)

View File

@@ -1,23 +1,83 @@
local BaseUI, Formspec = Smc.requireBaseUI(), Smc.requireFormspec() local BaseUI, Formspec = Smc.requireBaseUI(), Smc.requireFormspec()
---@param self BaseUI local require = Mod.getInfo('smc__ui__player_inventory').require
---@alias PlayerInventoryUI.PaneType 'left'|'right'
---@alias PlayerInventoryUI.getPaneFunc fun(self:PlayerInventoryUI,playerName:string,paneType:PlayerInventoryUI.PaneType):Formspec
---@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')
---@type PlayerInventoryUI.getPaneFunc
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,
}
---@param paneName PlayerInventoryUI.PaneName
local function getPane(paneName)
if paneName == nil then
---@type PlayerInventoryUI.PaneName
paneName = 'select_pane_menu'
end
local pane = panes[paneName]
assert(pane ~= nil, ('The pane with the `%s` does not exist.'):format(paneName))
return pane
end
---@param self PlayerInventoryUI
---@param playerName string ---@param playerName string
---@return string ---@return string
local function getFormSpecString(self, playerName) local function getFormSpecString(self, playerName)
---@type PlayerInventoryUI.PaneName
local leftPaneName = self:getContex(playerName, 'left_pane_name') or 'select_pane_menu'
---@type PlayerInventoryUI.PaneName
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')
local InventoryForm = Formspec:new({ local InventoryForm = Formspec:new({
size = {x=24, y=8.5}, size = {x=22, y=12},
position = {x=0.5, y=0.6}, position = {x=0.5, y=0.5},
noPrepend = true,
}) })
InventoryForm: InventoryForm:
list('current_player', 'main', 12,0.5, 9,2) --bgcolor('#00000000'):
listColors('#00000000', '#C4C4C433'):
list('current_player', 'main', 5.5,9.5, 9,2):
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')
return InventoryForm:toString() return InventoryForm:toString()
end end
---@alias PlayerInventoryUI.ContexKey 'left_pane_name'|'right_pane_name'|'left_pane_is_block'|'right_pane_is_block'
---@class PlayerInventoryUI : BaseUI ---@class PlayerInventoryUI : BaseUI
---@field getFormSpecString fun(self:BaseUI,playerName:string):string ---@field getFormSpecString fun(self:PlayerInventoryUI,playerName:string):string
---@field showForm fun(self:BaseUI,playerName:string):string ---@field showForm fun(self:PlayerInventoryUI,playerName:string):string
---@field getContex fun(self:PlayerInventoryUI,player:string|Player,key:PlayerInventoryUI.ContexKey):any
local PlayerInventoryUI = BaseUI:new('', getFormSpecString, false) local PlayerInventoryUI = BaseUI:new('', getFormSpecString, false)
--[[ --[[
@@ -37,4 +97,41 @@ function PlayerInventoryUI:closeForm(playerName)
core.close_formspec(playerName, '') core.close_formspec(playerName, '')
end--]] end--]]
PlayerInventoryUI:registerOnFormFieldAction('close_left_pane', function(player, fieldName, fieldValue)
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 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

@@ -0,0 +1,17 @@
local Formspec = Smc.requireFormspec()
local CraftPane = Formspec:new()
---@param self PlayerInventoryUI
---@param playerName string
---@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
end
return getCraftPane

View File

@@ -0,0 +1,17 @@
local Formspec = Smc.requireFormspec()
local EquipPane = Formspec:new()
---@param self PlayerInventoryUI
---@param playerName string
---@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
end
return getEquipPane

View File

@@ -0,0 +1,17 @@
local Formspec = Smc.requireFormspec()
local RefPane = Formspec:new()
---@param self PlayerInventoryUI
---@param playerName string
---@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
end
return getRefPane

View File

@@ -0,0 +1,21 @@
local Formspec = Smc.requireFormspec()
local selectPaneMenu = Formspec:new()
---@param self PlayerInventoryUI
---@param playerName string
---@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
end
return getSelectPaneMenu

Binary file not shown.

After

Width:  |  Height:  |  Size: 266 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 199 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 77 KiB