PlayerInventory & PlayerInventoryUI: adding explanatory annotations and comments to the code. #34
This commit is contained in:
@@ -3,6 +3,8 @@ local PrimitiveCraftHandler = Smc.requireCraftSystemPrimitiveCraftHandler()
|
||||
---@type PlayerInventoryUI
|
||||
local PlayerInventoryUI = Mod.getInfo('smc__ui__player_inventory').require('PlayerInventoryUI')
|
||||
|
||||
-- TODO: перенести обработки в PlayerUI
|
||||
|
||||
-- Handling the button click for selecting an item for crafting.
|
||||
-- Opens the item description and allows you to craft the selected item.
|
||||
PlayerInventoryUI:registerOnFormFieldActionMatch('.+::%S+::item_button',function(player, fieldName, fieldValue)
|
||||
|
||||
@@ -4,10 +4,9 @@ local BaseUI, Formspec = Smc.requireBaseUI(), Smc.requireFormspec()
|
||||
|
||||
local require = Mod.getInfo('smc__ui__player_inventory').require
|
||||
|
||||
---@alias PlayerInventoryUI.TabType 'left'|'right'
|
||||
---@alias PlayerInventoryUI.TabType 'left'|'right'
|
||||
---@alias PlayerInventoryUI.getTabFunc fun(self:PlayerInventoryUI,playerName:string,tabType:PlayerInventoryUI.TabType):Formspec
|
||||
|
||||
---@alias PlayerInventoryUI.TabName 'select_tab_menu'|'bag_tab'|'craft_tab'|'equip_tab'|'ref_tab'
|
||||
---@alias PlayerInventoryUI.TabName 'select_tab_menu'|'bag_tab'|'craft_tab'|'equip_tab'|'ref_tab'
|
||||
|
||||
---@type PlayerInventoryUI.getTabFunc
|
||||
local getBagTab = require('PlayerInventoryUI.getBagTab')
|
||||
@@ -54,45 +53,45 @@ local tabButtonTextures = {
|
||||
}
|
||||
}
|
||||
|
||||
---@param tabName PlayerInventoryUI.TabName
|
||||
local function getTab(tabName)
|
||||
if tabName == nil then
|
||||
---@type PlayerInventoryUI.TabName
|
||||
tabName = 'select_tab_menu'
|
||||
end
|
||||
|
||||
local tab = tabs[tabName]
|
||||
assert(tab ~= nil, ('The tab with the `%s` does not exist.'):format(tabName))
|
||||
|
||||
return tab
|
||||
end
|
||||
|
||||
---Returns the formspec definition for the compact (single tab)
|
||||
---version of the player's inventory.
|
||||
---@param self PlayerInventoryUI
|
||||
---@param playerName string
|
||||
---@return string
|
||||
local function getCompactForm(self, playerName)
|
||||
-- We geet the name of the selected (active) tab.
|
||||
-- The left tab is considered the primary tab (shown in the compact version).
|
||||
--
|
||||
-- If the tab is not specified, the equipment tab will be opened by default.
|
||||
---@type PlayerInventoryUI.TabName
|
||||
local leftTabName = self:getContex(playerName, 'left_tab_name') or 'craft_tab'
|
||||
local leftTabName = self:getContex(playerName, 'left_tab_name') or 'equip_tab'
|
||||
|
||||
-- TODO: убрать, если будет не нужно
|
||||
local leftTabIsBlock = self:getContex(playerName, 'left_tab_is_block')
|
||||
|
||||
-- Create an instance of the inventory form.
|
||||
local InventoryForm = Formspec:new({
|
||||
size = {x=14, y=12.5},
|
||||
position = {x=0.5, y=0.55},
|
||||
noPrepend = true,
|
||||
})
|
||||
|
||||
-- We get a table for textures for tab selection buttons according to the active tab.
|
||||
local leftTabButtonTextures = tabButtonTextures[leftTabName]
|
||||
|
||||
InventoryForm:
|
||||
-- Removing the inventory background color:
|
||||
bgcolor('#00000000', 'neither'):
|
||||
-- A permanent part of the inventory:
|
||||
background(0, 0.5, 14, 12.5, 'player_inventory_compact_middle_background.png', false):
|
||||
listColors('#00000000', '#e1b69150'):
|
||||
imageButton(13, 9.75, 0.5, 1, 'player_inventory_compact_mode_button_on.png', 'change_compact_mode'):
|
||||
list('current_player', 'main', 1.5,9.75, 9,1):
|
||||
list('current_player', 'main', 1.5,11.25, 9,1, 9):
|
||||
-- Inventory mode switch button (compact/full):
|
||||
imageButton(13, 9.75, 0.5, 1, 'player_inventory_compact_mode_button_on.png', 'change_compact_mode'):
|
||||
-- Tab:
|
||||
container(tabs[leftTabName](self, playerName, 'left'), 1.5, 0.5):
|
||||
-- Selection buttons for the left panel:
|
||||
-- Selection buttons for the (left) panel:
|
||||
imageButton(1.5,0,2,0.5, leftTabButtonTextures[1],'select_left_craft_tab', core.colorize('#e1b691','craft')):
|
||||
imageButton(3.5,0,2,0.5, leftTabButtonTextures[2], 'select_left_equip_tab', core.colorize('#e1b691','equip')):
|
||||
imageButton(5.5,0,2,0.5, leftTabButtonTextures[3], 'select_left_bag_tab', core.colorize('#e1b691','bags')):
|
||||
@@ -101,34 +100,50 @@ local function getCompactForm(self, playerName)
|
||||
return InventoryForm:toString()
|
||||
end
|
||||
|
||||
---Returns the formspec definition for the full (two tab)
|
||||
---version of the player's inventory.
|
||||
---@param self PlayerInventoryUI
|
||||
---@param playerName string
|
||||
---@return string
|
||||
local function getFullForm(self, playerName)
|
||||
-- We get the names of the active tabs (left and right).
|
||||
-- The tabs are identical, but when switching the inventory
|
||||
-- to compact mode, the left tab becomes the primary one.
|
||||
--
|
||||
-- The default values are:
|
||||
-- * left tab - equipment;
|
||||
-- * right tab - crafting menu.
|
||||
---@type PlayerInventoryUI.TabName
|
||||
local leftTabName = self:getContex(playerName, 'left_tab_name') or 'craft_tab'
|
||||
---@type PlayerInventoryUI.TabName
|
||||
local rightTabName = self:getContex(playerName, 'right_tab_name') or 'craft_tab'
|
||||
|
||||
-- TODO: убрать, если будет не нужно
|
||||
local leftTabIsBlock = self:getContex(playerName, 'left_tab_is_block')
|
||||
local rightTabIsBlock = self:getContex(playerName, 'right_tab_is_block')
|
||||
|
||||
-- Create an instance of the inventory form.
|
||||
local InventoryForm = Formspec:new({
|
||||
size = {x=22, y=12.5},
|
||||
position = {x=0.5, y=0.55},
|
||||
noPrepend = true,
|
||||
})
|
||||
|
||||
-- We get texture tables for the tab selection buttons depending on the active tabs.
|
||||
local leftTabButtonTextures = tabButtonTextures[leftTabName]
|
||||
local rightTabButtonTextures = tabButtonTextures[rightTabName]
|
||||
|
||||
InventoryForm:
|
||||
-- Removing the inventory background color:
|
||||
bgcolor('#00000000', 'neither'):
|
||||
-- A permanent part of the inventory:
|
||||
background(0, 9.5, 22, 3, 'player_inventory_middle_background.png', false):
|
||||
listColors('#00000000', '#e1b69150'):
|
||||
imageButton(17, 9.75, 0.5, 1, 'player_inventory_compact_mode_button_off.png', 'change_compact_mode'):
|
||||
list('current_player', 'main', 5.5,9.75, 9,1):
|
||||
list('current_player', 'main', 5.5,11.25, 9,1, 9):
|
||||
-- Inventory mode switch button (compact/full):
|
||||
imageButton(17, 9.75, 0.5, 1, 'player_inventory_compact_mode_button_off.png', 'change_compact_mode'):
|
||||
-- Tabs:
|
||||
container(tabs[leftTabName](self, playerName, 'left'), 0, 0.5):
|
||||
container(tabs[rightTabName](self, playerName, 'right'), 11, 0.5):
|
||||
-- Selection buttons for the left panel:
|
||||
@@ -136,6 +151,7 @@ local function getFullForm(self, playerName)
|
||||
imageButton(2,0,2,0.5, leftTabButtonTextures[2], 'select_left_equip_tab', core.colorize('#e1b691','equip')):
|
||||
imageButton(4,0,2,0.5, leftTabButtonTextures[3], 'select_left_bag_tab', core.colorize('#e1b691','bags')):
|
||||
imageButton(6,0,2,0.5, leftTabButtonTextures[4], 'select_left_ref_tab', core.colorize('#e1b691','refs')):
|
||||
-- Selection buttons for the right panel:
|
||||
imageButton(11,0,2,0.5, rightTabButtonTextures[1],'select_right_craft_tab', core.colorize('#e1b691','craft')):
|
||||
imageButton(13,0,2,0.5, rightTabButtonTextures[2], 'select_right_equip_tab', core.colorize('#e1b691','equip')):
|
||||
imageButton(15,0,2,0.5, rightTabButtonTextures[3], 'select_right_bag_tab', core.colorize('#e1b691','bags')):
|
||||
@@ -144,23 +160,37 @@ local function getFullForm(self, playerName)
|
||||
return InventoryForm:toString()
|
||||
end
|
||||
|
||||
---Returns the inventory formspec definition
|
||||
---depending on the selected inventory mod (compact/full).
|
||||
---
|
||||
---The selected inventory mode is contained in the context of the `PlayerInventoryUI` UI class.
|
||||
---@param self PlayerInventoryUI
|
||||
---@param playerName string
|
||||
---@return string
|
||||
local function getFormSpecString(self, playerName)
|
||||
-- We get the selected inventory mode.
|
||||
-- * `true` - compact mode
|
||||
-- * `false` - full mode
|
||||
---@type boolean
|
||||
local compactMode = self:getContex(playerName, 'compact_mode')
|
||||
|
||||
-- If the mode is not selected...
|
||||
if compactMode == nil then
|
||||
-- ...by default it will be `true` (compact mode).
|
||||
compactMode = true
|
||||
end
|
||||
|
||||
-- If compact mode is selected...
|
||||
if compactMode then
|
||||
-- ...return the compact inventory definition.
|
||||
return getCompactForm(self, playerName)
|
||||
else
|
||||
-- ...else we return the full version of the inventory.
|
||||
return getFullForm(self, playerName)
|
||||
end
|
||||
end
|
||||
|
||||
---Context keys for the `PlayerInventoryUI` class.
|
||||
---@alias PlayerInventoryUI.ContexKey
|
||||
--- 'compact_mode'|
|
||||
---
|
||||
@@ -186,16 +216,23 @@ end
|
||||
---@field setContex fun(self:PlayerInventoryUI,player:string|Player,key:PlayerInventoryUI.ContexKey,value:any)
|
||||
local PlayerInventoryUI = BaseUI:new('', getFormSpecString, false)
|
||||
|
||||
-- We are overriding the `BaseUI.showForm`.
|
||||
local showForm = PlayerInventoryUI.showForm
|
||||
|
||||
function PlayerInventoryUI:showForm(playerName)
|
||||
-- We get a player by his name.
|
||||
local player = core.get_player_by_name(playerName)
|
||||
assert(player ~= nil)
|
||||
|
||||
-- We overwrite the player's inventory definition so that the updated inventory
|
||||
-- is shown to the player when they press the "open inventory" key.
|
||||
player:set_inventory_formspec(PlayerInventoryUI:getFormSpecString(playerName))
|
||||
|
||||
-- We call the standard `BaseUI.showForm` function for the standard operation.
|
||||
return showForm(self, playerName)
|
||||
end
|
||||
|
||||
-- Handling the press of the inventory mode change button.
|
||||
PlayerInventoryUI:registerOnFormFieldAction('change_compact_mode', function(player, fieldName, fieldValue)
|
||||
local playerName = player:get_player_name()
|
||||
|
||||
@@ -208,6 +245,7 @@ PlayerInventoryUI:registerOnFormFieldAction('change_compact_mode', function(play
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
end)
|
||||
|
||||
-- Handling the left inventory tab selection button press.
|
||||
PlayerInventoryUI:registerOnFormFieldActionMatch('select_left_%S+', function(player, fieldName, fieldValue)
|
||||
local tabName = stringMatch(fieldName, 'select_%S+_(%S+_%S+)')
|
||||
assert(tabName ~= nil)
|
||||
@@ -218,13 +256,7 @@ PlayerInventoryUI:registerOnFormFieldActionMatch('select_left_%S+', function(pla
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
end)
|
||||
|
||||
PlayerInventoryUI:registerOnFormFieldAction('close_right_tab', function(player, fieldName, fieldValue)
|
||||
local playerName = player:get_player_name()
|
||||
|
||||
PlayerInventoryUI:setContex(playerName, 'right_tab_name', 'select_tab_menu')
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
end)
|
||||
|
||||
-- Handling the right inventory tab selection button press.
|
||||
PlayerInventoryUI:registerOnFormFieldActionMatch('select_right_%S+', function(player, fieldName, fieldValue)
|
||||
local tabName = stringMatch(fieldName, 'select_%S+_(%S+_%S+)')
|
||||
assert(tabName ~= nil)
|
||||
@@ -235,6 +267,7 @@ PlayerInventoryUI:registerOnFormFieldActionMatch('select_right_%S+', function(pl
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
end)
|
||||
|
||||
-- Handling the click of the button to turning back through the crafting menu pages.
|
||||
PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::page_back', function(player, fieldName, fieldValue)
|
||||
local tabType = stringMatch(fieldName, '(%S+)::page_back')
|
||||
assert(tabType == 'left' or tabType == 'right')
|
||||
@@ -251,6 +284,7 @@ PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::page_back', function(play
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
end)
|
||||
|
||||
-- Handling the click of the button to turning forward through the crafting menu pages.
|
||||
PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::page_forward', function(player, fieldName, fieldValue)
|
||||
local tabType = stringMatch(fieldName, '(%S+)::page_forward')
|
||||
assert(tabType == 'left' or tabType == 'right')
|
||||
@@ -267,6 +301,7 @@ PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::page_forward', function(p
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
end)
|
||||
|
||||
-- Processing closing of the selected item group in the crafting menu (returning to the main list of crafts).
|
||||
PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::close_group', function(player, fieldName, fieldValue)
|
||||
local tabType = stringMatch(fieldName, '(%S+)::close_group')
|
||||
assert(tabType == 'left' or tabType == 'right')
|
||||
|
||||
Reference in New Issue
Block a user