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

148 lines
4.7 KiB
Lua
Raw Normal View History

require("common.class")
2022-04-05 17:31:02 +02:00
local Dim = require("common.dimensions")
local Util = require("common.util")
local Page = require("components.pager.page")
2022-04-05 17:31:02 +02:00
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, --{r, g, b, a}
PADDING = {92, 128, 0, 56}, --{left, top, right, bottom}
PAGE_PADDING = {16, 16, 16, 16}, --{left, top, right, bottom}
FOOTER = {
2022-04-03 22:08:31 +02:00
"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
---@param o? table # initial parameters
---@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
---Refresh content values
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
---@param deltaTime number # frametime in seconds
function ServicePage:drawBackground(deltaTime)
gfx.BeginPath()
gfx.FillColor(0, 0, 0)
2022-04-05 17:31:02 +02:00
gfx.Rect(0, 0, Dim.design.width, Dim.design.height)
gfx.Fill()
end
---@param deltaTime number # frametime in seconds
function ServicePage:drawHeader(deltaTime)
local pageTitleTopPadding = self.PAGE_PADDING[2]
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
---@param deltaTime number # frametime in seconds
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
2022-04-05 17:31:02 +02:00
gfx.Text(line, Dim.design.width / 2, yFooterBase + (index-1) * lineHeight)
end
elseif type(footer) == "string" then
yFooterBase = Dim.design.height - pageFooterBottomPadding
2022-04-05 17:31:02 +02:00
gfx.Text(footer, Dim.design.width / 2, yFooterBase)
end
end
---@param button integer # options are under the `game` table prefixed with `BUTTON`
function ServicePage:handleButtonInput(button)
local field = self.content[self.selectedIndex]
if field and field.handleButtonInput then
-- if the field indicates that the button input has been handled in a
-- way that requires no further processing, return from this function
if field:handleButtonInput(button) then
return
end
end
-- default behaviour
field:deactivate()
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)
field = self.content[self.selectedIndex]
field:focus({direction = direction}) -- send direction as the message
end
---@param knob integer # `0` = Left, `1` = Right
---@param delta number # in radians, `-2*pi` to `0` (turning CCW) and `0` to `2*pi` (turning CW)
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
2022-04-05 23:53:19 +02:00
return ServicePage