ExperimentalGear/scripts/titlescreen/pages/service/servicepage.lua

144 lines
4.3 KiB
Lua

require("common.class")
local Dim = require("common.dimensions")
local Util = require("common.util")
local Page = require("components.pager.page")
local ServiceField = require("titlescreen.fields.service.servicefield")
---@class ServicePage: Page
---@field title string|string[]
---@field selectedIndex integer
---@field footer string[]
---@field content ServiceField[]
local ServicePage = {
__tostring = function() return "ServicePage" end,
FONT_SIZE = ServiceField.FONT_SIZE,
FONT_FACE = ServiceField.FONT_FACE,
FONT_COLOR = ServiceField.FONT_COLOR,
PADDING = {92, 128, 0, 56}, --{left, top, right, bottom}
FOOTER = {
"A/B BUTTON = SELECT ITEM",
"START BUTTON = EXECUTE",
"BACK BUTTON = EXIT"
},
FOOTER_SPACING = ServiceField.FONT_SIZE
+ ServiceField.MARGIN[2]
+ ServiceField.MARGIN[4],
}
---Create a new ServicePage instance
---
---Inherits from Page
---@return ServicePage
function ServicePage:new(o)
o = o or {}
o.title = o.title or ""
o.selectedIndex = o.selectedIndex or 1
o.footer = o.footer or self.FOOTER
return Inherit(self, o, Page)
end
function ServicePage:refreshFields()
for index, field in ipairs(self.content) do
if index == self.selectedIndex then
field:focus()
else
field:deactivate()
end
end
Page.refreshFields(self)
end
function ServicePage:drawBackground(deltaTime)
gfx.BeginPath()
gfx.FillColor(0, 0, 0)
gfx.Rect(0, 0, Dim.design.width, Dim.design.height)
gfx.Fill()
end
function ServicePage:drawHeader(deltaTime)
local pageTitleTopPadding = self.FONT_SIZE
local lineHeight = self.FOOTER_SPACING
gfx.BeginPath()
gfx.FontSize(self.FONT_SIZE)
gfx.LoadSkinFont(self.FONT_FACE)
gfx.FillColor(table.unpack(self.FONT_COLOR))
gfx.TextAlign(gfx.TEXT_ALIGN_CENTER | gfx.TEXT_ALIGN_TOP)
if type(self.title) == "table" then
for index, line in ipairs(self.title) do
gfx.Text(line, Dim.design.width / 2, pageTitleTopPadding + (index-1) * lineHeight)
end
elseif type(self.title) == "string" then
gfx.Text(self.title, Dim.design.width / 2, pageTitleTopPadding)
end
end
function ServicePage:drawFooter(deltaTime)
local footer = self.content[self.selectedIndex] and self.content[self.selectedIndex].footer or self.footer
local pageFooterBottomPadding = self.PADDING[4]
local lineHeight = self.FOOTER_SPACING
gfx.BeginPath()
gfx.FontSize(self.FONT_SIZE)
gfx.LoadSkinFont(self.FONT_FACE)
gfx.FillColor(table.unpack(self.FONT_COLOR))
gfx.TextAlign(gfx.TEXT_ALIGN_CENTER | gfx.TEXT_ALIGN_BOTTOM)
local yFooterBase
if type(footer) == "table" then
yFooterBase = Dim.design.height - pageFooterBottomPadding - #footer * lineHeight
for index, line in ipairs(footer) do
gfx.Text(line, Dim.design.width / 2, yFooterBase + (index-1) * lineHeight)
end
elseif type(footer) == "string" then
yFooterBase = Dim.design.height - pageFooterBottomPadding
gfx.Text(footer, Dim.design.width / 2, yFooterBase)
end
end
---Handle controller button input
---@param button integer
function ServicePage:handleButtonInput(button)
local stop_processing = false
local field = self.content[self.selectedIndex]
if field and field.handleButtonInput then
stop_processing = field:handleButtonInput(button)
end
-- default behaviour
field:deactivate()
if not stop_processing then
local direction = 0
if button == game.BUTTON_BCK then
if self.viewHandler then
self.viewHandler:back()
end
return
end
if button == game.BUTTON_BTA then
direction = -1
elseif button == game.BUTTON_BTB then
direction = 1
end
self.selectedIndex = Util.modIndex(self.selectedIndex + direction, #self.content)
end
field = self.content[self.selectedIndex]
field:focus()
end
---Handle controller knob input
---@param knob integer
---@param delta number
function ServicePage:handleKnobInput(knob, delta)
if self.content[self.selectedIndex] and self.content[self.selectedIndex].handleKnobInput then
self.content[self.selectedIndex].handleKnobInput(knob, delta)
end
end
return ServicePage