ExperimentalGear/scripts/titlescreen/service/servicepage.lua

124 lines
4.1 KiB
Lua

local dim = require("common.dimensions")
local Page = require("components.pager.page")
---@class ServicePage: Page
---@field title string
---@field selectedIdx integer
---@field footer string[]
local ServicePage = {
SERVICE_DEFAULT_FONT_SIZE = 24,
SERVICE_DEFAULT_FONT_FACE = "dfmarugoth.ttf",
SERVICE_DEFAULT_FONT_COLOR = {255, 255, 255, 255}, --{r, g, b, a}
SERVICE_DEFAULT_MARGIN = {92, 128, 0, 56}, --{left, top, right, bottom}
SERVICE_DEFAULT_SPACING = 4,
SERVICE_DEFAULT_FOOTER = {
"BT-A/BT-B = UP/DOWN",
"START = SELECT",
"BACK = RETURN TO LAST PAGE"
}
}
---Create a new ServicePage instance
---
---Inherits from Page
---@return ServicePage
function ServicePage:new(o)
self.__index = self
setmetatable(self, {__index = Page})
o = Page:new(o)
setmetatable(o, self)
o.title = o.title or ""
o.selectedIdx = o.selectedIdx or 0
o.footer = o.footer or self.SERVICE_DEFAULT_FOOTER
return o
end
---Add field to page
---@param field Field
function ServicePage:addField(field)
field.posX = ServicePage.SERVICE_DEFAULT_MARGIN[1]
field.posY = ServicePage.SERVICE_DEFAULT_MARGIN[2] + #self.content * (ServicePage.SERVICE_DEFAULT_FONT_SIZE + ServicePage.SERVICE_DEFAULT_SPACING)
Page.addField(self, field)
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)
gfx.BeginPath()
gfx.FontSize(ServicePage.SERVICE_DEFAULT_FONT_SIZE)
gfx.LoadSkinFont(ServicePage.SERVICE_DEFAULT_FONT_FACE)
gfx.FillColor(table.unpack(ServicePage.SERVICE_DEFAULT_FONT_COLOR))
gfx.TextAlign(gfx.TEXT_ALIGN_CENTER | gfx.TEXT_ALIGN_TOP)
gfx.Text(self.title, dim.design.width / 2, ServicePage.SERVICE_DEFAULT_FONT_SIZE)
end
function ServicePage:drawFooter(deltaTime)
if self.content[selectedIndex] and self.content[selectedIndex].drawCustomFooter then
self.content[selectedIndex].drawCustomFooter(deltaTime)
else
local bottomPageMargin = ServicePage.SERVICE_DEFAULT_MARGIN[4]
local lineHeight = ServicePage.SERVICE_DEFAULT_FONT_SIZE + ServicePage.SERVICE_DEFAULT_SPACING
gfx.BeginPath()
gfx.FontSize(ServicePage.SERVICE_DEFAULT_FONT_SIZE)
gfx.LoadSkinFont(ServicePage.SERVICE_DEFAULT_FONT_FACE)
gfx.FillColor(table.unpack(ServicePage.SERVICE_DEFAULT_FONT_COLOR))
gfx.TextAlign(gfx.TEXT_ALIGN_CENTER | gfx.TEXT_ALIGN_BOTTOM)
if type(self.footer) == "table" then
for index, line in ipairs(self.footer) do
local yFooterBase = dim.design.height - bottomPageMargin - #self.footer * lineHeight
gfx.Text(line, 1080 / 2, yFooterBase + (index-1) * lineHeight)
end
elseif type(self.footer) == "string" then
local yFooterBase = dim.design.height - bottomPageMargin
gfx.Text(self.footer, 1080 / 2, yFooterBase)
end
end
end
---Handle controller button input
---@param button integer
function ServicePage:handleButtonInput(button)
local stop_processing = false
if self.content[selectedIndex] and self.content[selectedIndex].handleButtonInput then
stop_processing = self.content[selectedIndex].handleButtonInput(button)
end
-- default behaviour
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.selectedIdx = (self.selectedIdx - 1 + direction) % #self.content + 1
end
end
---Handle controller knob input
---@param knob integer
---@param delta number
function ServicePage:handleKnobInput(knob, delta)
if self.content[selectedIndex] and self.content[selectedIndex].handleKnobInput then
self.content[selectedIndex].handleKnobInput(knob, delta)
end
end
return ServicePage