diff --git a/mods/CORE/Player/init.lua b/mods/CORE/Player/init.lua index a587138..d0996ea 100644 --- a/mods/CORE/Player/init.lua +++ b/mods/CORE/Player/init.lua @@ -1,4 +1,4 @@ local modInfo = Mod.getInfo('smc__core__player') local require = modInfo.require -return require('Player') \ No newline at end of file +return require('Player') diff --git a/mods/CORE/Player/src/Player/inventory.lua b/mods/CORE/Player/src/Player/inventory.lua index a7c8dc1..98df051 100644 --- a/mods/CORE/Player/src/Player/inventory.lua +++ b/mods/CORE/Player/src/Player/inventory.lua @@ -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') diff --git a/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI.lua b/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI.lua index 7eb5564..bbe7a23 100644 --- a/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI.lua +++ b/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI.lua @@ -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) diff --git a/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/getBagPane.lua b/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/getBagPane.lua index 13ba4fc..8832fe1 100644 --- a/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/getBagPane.lua +++ b/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/getBagPane.lua @@ -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') diff --git a/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/getCraftPane.lua b/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/getCraftPane.lua index 6cf457c..d400d1f 100644 --- a/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/getCraftPane.lua +++ b/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/getCraftPane.lua @@ -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() diff --git a/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/getEquipPane.lua b/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/getEquipPane.lua index 19f1730..7b27753 100644 --- a/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/getEquipPane.lua +++ b/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/getEquipPane.lua @@ -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') diff --git a/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/getRefPane.lua b/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/getRefPane.lua index 674b1a9..58d32a0 100644 --- a/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/getRefPane.lua +++ b/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/getRefPane.lua @@ -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') diff --git a/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/getSelectPaneMenu.lua b/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/getSelectPaneMenu.lua index bbc485c..47dc57f 100644 --- a/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/getSelectPaneMenu.lua +++ b/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/getSelectPaneMenu.lua @@ -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'): diff --git a/mods/UI/PlayerInventoryUI/textures/player_inventory_craft_pane_close_group_button_background.png b/mods/UI/PlayerInventoryUI/textures/player_inventory_craft_pane_close_group_button_background.png new file mode 100644 index 0000000..645f586 Binary files /dev/null and b/mods/UI/PlayerInventoryUI/textures/player_inventory_craft_pane_close_group_button_background.png differ