PlayerInventory & PlayerInventoryUI: Completion of work on a sane version of the crafting menu. #34
This commit is contained in:
@@ -83,6 +83,9 @@ end
|
||||
---
|
||||
--- 'left_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'|
|
||||
--- 'right_pane_craft_page_number'|
|
||||
--- 'left_pane_craft_scroll'|
|
||||
@@ -95,11 +98,20 @@ end
|
||||
---@field setContex fun(self:PlayerInventoryUI,player:string|Player,key:PlayerInventoryUI.ContexKey,value:any)
|
||||
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)
|
||||
local playerName = player:get_player_name()
|
||||
|
||||
PlayerInventoryUI:setContex(playerName, 'left_pane_name', 'select_pane_menu')
|
||||
player:set_inventory_formspec(PlayerInventoryUI:getFormSpecString(playerName))
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
end)
|
||||
|
||||
@@ -110,7 +122,6 @@ PlayerInventoryUI:registerOnFormFieldActionMatch('select_left_%S+', function(pla
|
||||
local playerName = player:get_player_name()
|
||||
|
||||
PlayerInventoryUI:setContex(playerName, 'left_pane_name', paneName)
|
||||
player:set_inventory_formspec(PlayerInventoryUI:getFormSpecString(playerName))
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
end)
|
||||
|
||||
@@ -118,7 +129,6 @@ PlayerInventoryUI:registerOnFormFieldAction('close_right_pane', function(player,
|
||||
local playerName = player:get_player_name()
|
||||
|
||||
PlayerInventoryUI:setContex(playerName, 'right_pane_name', 'select_pane_menu')
|
||||
player:set_inventory_formspec(PlayerInventoryUI:getFormSpecString(playerName))
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
end)
|
||||
|
||||
@@ -129,7 +139,6 @@ PlayerInventoryUI:registerOnFormFieldActionMatch('select_right_%S+', function(pl
|
||||
local playerName = player:get_player_name()
|
||||
|
||||
PlayerInventoryUI:setContex(playerName, 'right_pane_name', paneName)
|
||||
player:set_inventory_formspec(PlayerInventoryUI:getFormSpecString(playerName))
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
end)
|
||||
|
||||
@@ -146,7 +155,6 @@ PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::page_back', function(play
|
||||
end
|
||||
|
||||
PlayerInventoryUI:setContex(playerName, paneType..'_pane_craft_page_number', pageNumber)
|
||||
player:set_inventory_formspec(PlayerInventoryUI:getFormSpecString(playerName))
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
end)
|
||||
|
||||
@@ -163,7 +171,15 @@ PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::page_forward', function(p
|
||||
end
|
||||
|
||||
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)
|
||||
end)
|
||||
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
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)
|
||||
local BagPane = Formspec:new()
|
||||
|
||||
BagPane:
|
||||
image(0, 0, 11, 9, 'player_inventory_pane_background.png'):
|
||||
label(2,2, 'BAG PANE')
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
local mathFloor = math.floor
|
||||
|
||||
---Number of crafting recipes on one menu page.
|
||||
local PAGE_NUMBER_ITEMS = 24
|
||||
|
||||
@@ -5,8 +7,6 @@ local PrimitiveCraftHandler = Smc.requireCraftSystemPrimitiveCraftHandler()
|
||||
|
||||
local Formspec = Smc.requireFormspec()
|
||||
|
||||
local CraftPane = Formspec:new()
|
||||
|
||||
---@param self PlayerInventoryUI
|
||||
---@param playerName string
|
||||
---@param paneType PlayerInventoryUI.PaneType
|
||||
@@ -16,24 +16,24 @@ local function getCraftPane(self, playerName, paneType)
|
||||
error('Incorrect argument `paneType`: '..paneType)
|
||||
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 learnedCrafts = PrimitiveCraftHandler:getLearnedCrafts(playerName)
|
||||
---@type CraftSystem.PrimitiveCraft[]
|
||||
local crafts = {}
|
||||
|
||||
local craftsListForm = Formspec:new()
|
||||
|
||||
for _, craft in pairs(learnedCrafts) do
|
||||
table.insert(crafts, craft)
|
||||
local crafts
|
||||
if selectedGroup == nil then
|
||||
---@type (CraftSystem.PrimitiveCraft[]|CraftSystem.PrimitiveCraft)[]
|
||||
crafts = PrimitiveCraftHandler:getLearnedCrafts(playerName)
|
||||
else
|
||||
---@type CraftSystem.PrimitiveCraft[]
|
||||
crafts = PrimitiveCraftHandler:getCraftsByGroup(selectedGroup)
|
||||
end
|
||||
|
||||
table.sort(crafts, function(a, b)
|
||||
if a.output < b.output then
|
||||
return true
|
||||
end
|
||||
end)
|
||||
local craftsListForm = Formspec:new()
|
||||
|
||||
---Maximum number of crafting recipe pages
|
||||
---@type integer
|
||||
@@ -44,24 +44,45 @@ local function getCraftPane(self, playerName, paneType)
|
||||
self:setContex(playerName, paneType..'_pane_craft_page_number', pageNumber)
|
||||
end
|
||||
|
||||
local registeredItems = core.registered_items
|
||||
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
|
||||
local craft = crafts[i]
|
||||
if craft == nil then
|
||||
break
|
||||
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:
|
||||
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
|
||||
local k = time%#craft+1
|
||||
craft = craft[k]
|
||||
assert(craft ~= nil)
|
||||
|
||||
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
|
||||
|
||||
craftsListForm:
|
||||
itemImage(w+0.0625, h+0.0625, 1, 1, craft.output)
|
||||
|
||||
w = w + 1.125
|
||||
|
||||
if w >= 4 then
|
||||
@@ -71,13 +92,13 @@ local function getCraftPane(self, playerName, paneType)
|
||||
end
|
||||
|
||||
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):
|
||||
button(1.75, 7.75, 1, 0.5, paneType..'::page_back', '<'):
|
||||
button(2.75, 7.75, 1, 0.5, paneType..'::page_forward', '>'):
|
||||
label(2.5,8.5, ('%s/%s'):format(pageNumber, pagesMax))
|
||||
|
||||
if selectedCraft ~= nil then
|
||||
if selectedCraft ~= '' then
|
||||
local craft = PrimitiveCraftHandler:getCraftByName(selectedCraft)
|
||||
local item = ItemStack(craft.output)
|
||||
local itemDef = item:get_definition()
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
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)
|
||||
local EquipPane = Formspec:new()
|
||||
|
||||
EquipPane:
|
||||
image(0, 0, 11, 9, 'player_inventory_pane_background.png'):
|
||||
label(2,2, 'EQUIPMENT PANE')
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
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)
|
||||
local RefPane = Formspec:new()
|
||||
|
||||
RefPane:
|
||||
image(0, 0, 11, 9, 'player_inventory_pane_background.png'):
|
||||
label(2,2, 'REFERENCE PANE')
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
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)
|
||||
local selectPaneMenu = Formspec:new()
|
||||
|
||||
selectPaneMenu:
|
||||
image(0, 0, 11, 9, 'player_inventory_pane_background.png'):
|
||||
|
||||
Reference in New Issue
Block a user