2 Commits

10 changed files with 122 additions and 72 deletions

View File

@@ -1,4 +1,4 @@
local modInfo = Mod.getInfo('smc__core__player')
local require = modInfo.require
return require('Player')
return require('Player')

View File

@@ -3,23 +3,43 @@ local PrimitiveCraftHandler = Smc.requireCraftSystemPrimitiveCraftHandler()
---@type PlayerInventoryUI
local PlayerInventoryUI = Mod.getInfo('smc__ui__player_inventory').require('PlayerInventoryUI')
PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::%S+::item_button',function(player, fieldName, fieldValue)
local craftName, paneType = string.match(fieldName, '(%S+)::(%S+)::item_button')
-- 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)
local craftName, paneType = string.match(fieldName, '(.+)::(%S+)::item_button')
assert(craftName ~= nil)
assert(paneType == 'left' or paneType == 'right')
local playerName = player:get_player_name()
PlayerInventoryUI:setContex(playerName, paneType..'_pane_craft_selected_craft', craftName)
player:set_inventory_formspec(PlayerInventoryUI:getFormSpecString(playerName))
PlayerInventoryUI:showForm(playerName)
end)
PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::craft_item', function(player, fieldName, fieldValue)
-- Handling the click of the crafting group selection button.
-- Causes grouped items to be expanded into a separate list.
PlayerInventoryUI:registerOnFormFieldActionMatch('.+::%S+::group_button',function(player, fieldName, fieldValue)
local groupName, paneType = string.match(fieldName, '(.+)::(%S+)::group_button')
assert(groupName ~= nil)
assert(paneType == 'left' or paneType == 'right')
local playerName = player:get_player_name()
PlayerInventoryUI:setContex(playerName, paneType..'_pane_craft_selected_group', groupName)
PlayerInventoryUI:showForm(playerName)
end)
Smc.registerGlobalStepForEachPlayer(1, function(deltaTime, player)
local playerName = player:get_player_name()
if Smc.getActiveFormspec(playerName) == '' then
player:set_inventory_formspec(PlayerInventoryUI:getFormSpecString(playerName))
end
end)
PlayerInventoryUI:registerOnFormFieldActionMatch('.+::craft_item', function(player, fieldName, fieldValue)
---@type InvRef
local inv = player:get_inventory()
local playerName = player:get_player_name()
local paneType = string.match(fieldName, '(%S+)::craft_item')
local paneType = string.match(fieldName, '(.+)::craft_item')
assert(paneType == 'left' or paneType == 'right')
local craftName = PlayerInventoryUI:getContex(playerName, paneType..'_pane_craft_selected_craft')

View File

@@ -170,34 +170,28 @@ function table.merge(t1, t2, recursively)
return result
end
---Adds key-value from `table2` into `table1` only if that key does not exists in `table1`.
---Values that are tables are copied by `table.copy()`.
---If `recursively` is true, the function will be applied recursively for values
---that are tables in both `table1` and `table2`.
---
---
---@overload fun(t1:table, t2:table):table
---
---@generic T: table, U: table
---@param t1 T
---@param t2 U
---@param recursively boolean Default: false. Go recursively if both values are tables.
---
---Original source: [Voxrame](https://github.com/Voxrame/Voxrame/blob/d5255d466ca502267023c146696a655cf09595b3/helpers/src/lua_ext/table.lua#L161)
---@return T|U
function table.join(t1, t2, recursively)
recursively = recursively or false
for key, value in pairs(t2) do
if not t1[key] then
t1[key] = type(value) == 'table' and tableCopy(value) or value
else
if recursively and type(t1[key]) == 'table' and type(value) == 'table' then
table.join(t1[key], value, recursively)
end
end
--- Appends a second list to the end of the first list,
--- thereby concatenating the two lists together.
--- @generic t
--- @param t1 t[]
--- @param t2 t[]
--- @return t[]
function table.join(t1, t2)
ensureArgType(t1, 'table', 1, 'table.join')
ensureArgType(t2, 'table', 2, 'table.join')
local result = {}
for i, v in ipairs(t1) do
result[i] = v
end
return t1
local offset = #t1
for i, v in ipairs(t2) do
result[offset + i] = v
end
return result
end
--- Returns an array containing only unique values of the table `t`.

View File

@@ -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)

View File

@@ -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')

View File

@@ -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()

View File

@@ -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')

View File

@@ -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')

View File

@@ -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'):

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB