130 lines
4.5 KiB
Lua
130 lines
4.5 KiB
Lua
require("common.class")
|
|
local dim = require("common.dimensions")
|
|
local Page = require("components.pager.page")
|
|
local ServiceField = require("titlescreen.service.servicefield")
|
|
|
|
---@class ServicePage: Page
|
|
---@field title string|string[]
|
|
---@field selectedIndex integer
|
|
---@field footer string[]
|
|
---@field content ServiceField[]
|
|
local ServicePage = {
|
|
SERVICE_DEFAULT_FONT_SIZE = ServiceField.SERVICE_DEFAULT_FONT_SIZE,
|
|
SERVICE_DEFAULT_FONT_FACE = ServiceField.SERVICE_DEFAULT_FONT_FACE,
|
|
SERVICE_DEFAULT_FONT_COLOR = ServiceField.SERVICE_DEFAULT_FONT_COLOR,
|
|
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)
|
|
o = Inherit(self, Page, o)
|
|
|
|
o.title = o.title or ""
|
|
o.selectedIndex = o.selectedIndex or 1
|
|
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)
|
|
local pageTopTitleMargin = ServicePage.SERVICE_DEFAULT_FONT_SIZE
|
|
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_TOP)
|
|
if type(self.title) == "table" then
|
|
for index, line in ipairs(self.title) do
|
|
gfx.Text(line, dim.design.width / 2, pageTopTitleMargin + (index-1) * lineHeight)
|
|
end
|
|
elseif type(self.title) == "string" then
|
|
gfx.Text(self.title, dim.design.width / 2, pageTopTitleMargin)
|
|
end
|
|
end
|
|
|
|
function ServicePage:drawFooter(deltaTime)
|
|
local footer = self.content[self.selectedIndex] and self.content[self.selectedIndex].footer or self.footer
|
|
|
|
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(footer) == "table" then
|
|
local yFooterBase = dim.design.height - bottomPageMargin - #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
|
|
local yFooterBase = dim.design.height - bottomPageMargin
|
|
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
|
|
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 = (self.selectedIndex - 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[self.selectedIndex] and self.content[self.selectedIndex].handleKnobInput then
|
|
self.content[self.selectedIndex].handleKnobInput(knob, delta)
|
|
end
|
|
end
|
|
|
|
return ServicePage |