@@ -1,3 +1,5 @@
local MAX_NODE_TAB_DISTANCE = 20
local stringMatch = string.match
local stringMatch = string.match
local BaseUI , Formspec = Smc.requireBaseUI ( ) , Smc.requireFormspec ( )
local BaseUI , Formspec = Smc.requireBaseUI ( ) , Smc.requireFormspec ( )
@@ -9,7 +11,7 @@ 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.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 'bag_tab'|'craft_tab'|'equip_tab'|'ref_tab'|'node_tab'
---@type PlayerInventoryUI.getTabFunc
---@type PlayerInventoryUI.getTabFunc
local getBagTab = require ( ' PlayerInventoryUI.getBagTab ' )
local getBagTab = require ( ' PlayerInventoryUI.getBagTab ' )
@@ -34,21 +36,32 @@ local tabButtonTextures = {
selectTabButtonTexture ,
selectTabButtonTexture ,
unselectTabButtonTexture ,
unselectTabButtonTexture ,
unselectTabButtonTexture ,
unselectTabButtonTexture ,
unselectTabButtonTexture ,
unselectTabButtonTexture
unselectTabButtonTexture
} ,
} ,
[ ' equip_tab ' ] = {
[ ' equip_tab ' ] = {
unselectTabButtonTexture ,
unselectTabButtonTexture ,
selectTabButtonTexture ,
selectTabButtonTexture ,
unselectTabButtonTexture ,
unselectTabButtonTexture ,
unselectTabButtonTexture ,
unselectTabButtonTexture
unselectTabButtonTexture
} ,
} ,
[ ' bag_tab ' ] = {
[ ' bag_tab ' ] = {
unselectTabButtonTexture ,
unselectTabButtonTexture ,
unselectTabButtonTexture ,
unselectTabButtonTexture ,
selectTabButtonTexture ,
selectTabButtonTexture ,
unselectTabButtonTexture ,
unselectTabButtonTexture
unselectTabButtonTexture
} ,
} ,
[ ' ref_tab ' ] = {
[ ' ref_tab ' ] = {
unselectTabButtonTexture ,
unselectTabButtonTexture ,
unselectTabButtonTexture ,
selectTabButtonTexture ,
unselectTabButtonTexture
} ,
[ ' node_tab ' ] = {
unselectTabButtonTexture ,
unselectTabButtonTexture ,
unselectTabButtonTexture ,
unselectTabButtonTexture ,
unselectTabButtonTexture ,
unselectTabButtonTexture ,
unselectTabButtonTexture ,
@@ -65,12 +78,16 @@ local function getCompactForm(self, playerName)
-- We geet the name of the selected (active) tab.
-- We geet the name of the selected (active) tab.
-- The left tab is considered the primary tab (shown in the compact version).
-- 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.
-- If the tab is not specified, the crafting tab will be opened by default.
---@type PlayerInventoryUI.TabName
---@type PlayerInventoryUI.TabName
local leftTabName = self : getContex ( playerName , ' left_tab_name ' ) or ' equip _tab'
local leftTabName = self : getContex ( playerName , ' left_tab_name ' ) or ' craft _tab'
-- TODO: убрать, если будет не нужно
local leftTab
local leftTabIsBlock = self : getContex ( player Name , ' left_tab_is_block ' )
if leftTabName == ' node_tab ' then
leftTab = self : getContex ( playerName , ' node_tab ' )
else
leftTab = tabs [ leftTabName ]
end
local InventoryForm = Formspec : new ( {
local InventoryForm = Formspec : new ( {
size = { x = 14 , y = 12.5 } ,
size = { x = 14 , y = 12.5 } ,
@@ -94,13 +111,18 @@ local function getCompactForm(self, playerName)
-- Inventory mode switch button (compact/full):
-- Inventory mode switch button (compact/full):
: imageButton ( 12.75 , 10 , 1 , 0.5 , ' player_inventory_compact_mode_button_on.png ' , ' change_compact_mode ' )
: imageButton ( 12.75 , 10 , 1 , 0.5 , ' player_inventory_compact_mode_button_on.png ' , ' change_compact_mode ' )
-- Tab:
-- Tab:
: container ( tabs [ leftTabName ] ( self , playerName , ' left ' ) , 1.5 , 0.5 )
: container ( leftTab ( 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 ( 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 ( 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 ' ) )
: imageButton ( 5.5 , 0 , 2 , 0.5 , leftTabButtonTextures [ 3 ] , ' select_left_bag_tab ' , core.colorize ( ' #e1b691 ' , ' bags ' ) )
: imageButton ( 7.5 , 0 , 2 , 0.5 , leftTabButtonTextures [ 4 ] , ' select_left_ref_tab ' , core.colorize ( ' #e1b691 ' , ' refs ' ) )
: imageButton ( 7.5 , 0 , 2 , 0.5 , leftTabButtonTextures [ 4 ] , ' select_left_ref_tab ' , core.colorize ( ' #e1b691 ' , ' refs ' ) )
if self : getContex ( playerName , ' node_tab ' ) ~= nil then
InventoryForm
: imageButton ( 9.5 , 0 , 2 , 0.5 , leftTabButtonTextures [ 5 ] , ' select_left_node_tab ' , core.colorize ( ' #e1b691 ' , ' node ' ) )
end
return InventoryForm : toString ( )
return InventoryForm : toString ( )
end
end
@@ -115,16 +137,26 @@ local function getFullForm(self, playerName)
-- to compact mode, the left tab becomes the primary one.
-- to compact mode, the left tab becomes the primary one.
--
--
-- The default values are:
-- The default values are:
-- * left tab - equip ment ;
-- * left tab - crafting menu ;
-- * right tab - crafting menu .
-- * right tab - equip ment .
---@type PlayerInventoryUI.TabName
---@type PlayerInventoryUI.TabName
local leftTabName = self : getContex ( playerName , ' left_tab_name ' ) or ' equip _tab'
local leftTabName = self : getContex ( playerName , ' left_tab_name ' ) or ' craft _tab'
---@type PlayerInventoryUI.TabName
---@type PlayerInventoryUI.TabName
local rightTabName = self : getContex ( playerName , ' right_tab_name ' ) or ' craft _tab'
local rightTabName = self : getContex ( playerName , ' right_tab_name ' ) or ' equip _tab'
-- TODO: убрать, если будет не нужно
local leftTab
local leftTabIsBlock = self : getContex ( player Name , ' left_tab_is_block ' )
if leftTabName == ' node_tab ' then
local rightTabIsBlock = self : getContex ( playerName , ' right_tab_is_block ' )
leftTab = self : getContex ( playerName , ' node_tab ' )
else
leftTab = tabs [ leftTabName ]
end
local rightTab
if rightTabName == ' node_tab ' then
rightTab = self : getContex ( playerName , ' node_tab ' )
else
rightTab = tabs [ rightTabName ]
end
local InventoryForm = Formspec : new ( {
local InventoryForm = Formspec : new ( {
size = { x = 22 , y = 12.5 } ,
size = { x = 22 , y = 12.5 } ,
@@ -149,8 +181,8 @@ local function getFullForm(self, playerName)
-- Inventory mode switch button (compact/full):
-- Inventory mode switch button (compact/full):
: imageButton ( 16.75 , 10 , 1 , 0.5 , ' player_inventory_compact_mode_button_off.png ' , ' change_compact_mode ' )
: imageButton ( 16.75 , 10 , 1 , 0.5 , ' player_inventory_compact_mode_button_off.png ' , ' change_compact_mode ' )
-- Tabs:
-- Tabs:
: container ( tabs [ leftTabName ] ( self , playerName , ' left ' ) , 0 , 0.5 )
: container ( leftTab ( self , playerName , ' left ' ) , 0 , 0.5 )
: container ( tabs [ rightTabName ] ( self , playerName , ' right ' ) , 11 , 0.5 )
: container ( rightTab ( self , playerName , ' right ' ) , 11 , 0.5 )
-- Selection buttons for the left panel:
-- Selection buttons for the left panel:
: imageButton ( 0 , 0 , 2 , 0.5 , leftTabButtonTextures [ 1 ] , ' select_left_craft_tab ' , core.colorize ( ' #e1b691 ' , ' craft ' ) )
: imageButton ( 0 , 0 , 2 , 0.5 , leftTabButtonTextures [ 1 ] , ' select_left_craft_tab ' , core.colorize ( ' #e1b691 ' , ' craft ' ) )
: imageButton ( 2 , 0 , 2 , 0.5 , leftTabButtonTextures [ 2 ] , ' select_left_equip_tab ' , core.colorize ( ' #e1b691 ' , ' equip ' ) )
: imageButton ( 2 , 0 , 2 , 0.5 , leftTabButtonTextures [ 2 ] , ' select_left_equip_tab ' , core.colorize ( ' #e1b691 ' , ' equip ' ) )
@@ -162,6 +194,12 @@ local function getFullForm(self, playerName)
: imageButton ( 15 , 0 , 2 , 0.5 , rightTabButtonTextures [ 3 ] , ' select_right_bag_tab ' , core.colorize ( ' #e1b691 ' , ' bags ' ) )
: imageButton ( 15 , 0 , 2 , 0.5 , rightTabButtonTextures [ 3 ] , ' select_right_bag_tab ' , core.colorize ( ' #e1b691 ' , ' bags ' ) )
: imageButton ( 17 , 0 , 2 , 0.5 , rightTabButtonTextures [ 4 ] , ' select_right_ref_tab ' , core.colorize ( ' #e1b691 ' , ' refs ' ) )
: imageButton ( 17 , 0 , 2 , 0.5 , rightTabButtonTextures [ 4 ] , ' select_right_ref_tab ' , core.colorize ( ' #e1b691 ' , ' refs ' ) )
if self : getContex ( playerName , ' node_tab ' ) ~= nil then
InventoryForm
: imageButton ( 8 , 0 , 2 , 0.5 , leftTabButtonTextures [ 5 ] , ' select_left_node_tab ' , core.colorize ( ' #e1b691 ' , ' node ' ) )
: imageButton ( 19 , 0 , 2 , 0.5 , rightTabButtonTextures [ 5 ] , ' select_right_node_tab ' , core.colorize ( ' #e1b691 ' , ' node ' ) )
end
return InventoryForm : toString ( )
return InventoryForm : toString ( )
end
end
@@ -194,11 +232,11 @@ end
---Context keys for the `PlayerInventoryUI` class.
---Context keys for the `PlayerInventoryUI` class.
---@alias PlayerInventoryUI.ContexKey
---@alias PlayerInventoryUI.ContexKey
--- 'compact_mode'|
--- 'compact_mode'|
--- 'node_tab_pos'|
--- 'node_tab'|
---
---
--- 'left_tab_name'|
--- 'left_tab_name'|
--- 'right_tab_name'|
--- 'right_tab_name'|
--- 'left_tab_is_block'|
--- 'right_tab_is_block'|
---
---
--- 'left_tab_craft_search_field_value'|
--- 'left_tab_craft_search_field_value'|
--- 'right_tab_craft_search_field_value'|
--- 'right_tab_craft_search_field_value'|
@@ -233,6 +271,48 @@ function PlayerInventoryUI:showForm(playerName)
return showForm ( self , playerName )
return showForm ( self , playerName )
end
end
---Clears all data on the node's inventory tab.
---@param player Player
local function closeNodeTab ( player )
local playerName = player : get_player_name ( )
PlayerInventoryUI : setContex ( playerName , ' node_tab_pos ' , nil )
PlayerInventoryUI : setContex ( playerName , ' node_tab ' , nil )
---@type PlayerInventoryUI.TabName
local leftTabName = PlayerInventoryUI : getContex ( playerName , ' left_tab_name ' ) or ' craft_tab '
---@type PlayerInventoryUI.TabName
local rightTabName = PlayerInventoryUI : getContex ( playerName , ' right_tab_name ' ) or ' equip_tab '
if leftTabName == ' node_tab ' then
PlayerInventoryUI : setContex ( playerName , ' left_tab_name ' , nil )
end
if rightTabName == ' node_tab ' then
PlayerInventoryUI : setContex ( playerName , ' right_tab_name ' , nil )
end
player : set_inventory_formspec ( PlayerInventoryUI : getFormSpecString ( playerName ) )
end
-- When closing the form, we clear all data related to the node's inventory tab.
PlayerInventoryUI : registerOnFormQuit ( closeNodeTab )
Smc.registerGlobalStepForEachPlayer ( 0 , function ( deltaTime , player )
local playerName = player : get_player_name ( )
if Smc.getActiveFormspec ( playerName ) == ' ' then
local nodePos = PlayerInventoryUI : getContex ( playerName , ' node_tab_pos ' )
if nodePos == nil then
return
end
local playerPos = player : get_pos ( )
local distance = playerPos : subtract ( nodePos )
if distance : length ( ) > MAX_NODE_TAB_DISTANCE then
closeNodeTab ( player )
end
end
end )
-- Handling the press of the inventory mode change button.
-- Handling the press of the inventory mode change button.
PlayerInventoryUI : registerOnFormFieldAction ( ' change_compact_mode ' , function ( player , fieldName , fieldValue )
PlayerInventoryUI : registerOnFormFieldAction ( ' change_compact_mode ' , function ( player , fieldName , fieldValue )
local playerName = player : get_player_name ( )
local playerName = player : get_player_name ( )
@@ -709,7 +789,7 @@ core.register_allow_player_inventory_action(function(player, action, inventory,
return
return
end
end
elseif action == ' put ' then
elseif action == ' put ' and isBagInventoryList ( inventoryInfo.listname ) then
local bagItem = getBagSlotListName ( inventory , inventoryInfo.listname )
local bagItem = getBagSlotListName ( inventory , inventoryInfo.listname )
local movableItemName = inventoryInfo.stack
local movableItemName = inventoryInfo.stack