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

148 lines
4.9 KiB
Lua
Raw Normal View History

require("common.class")
2022-04-05 17:31:02 +02:00
local Dim = require("common.dimensions")
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 = {
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 = {
2022-04-03 22:08:31 +02:00
"A/B BUTTON = SELECT ITEM",
"START BUTTON = EXECUTE",
"BACK BUTTON = EXIT"
}
}
---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 = self.SERVICE_DEFAULT_MARGIN[1]
field.posY = self.SERVICE_DEFAULT_MARGIN[2] + #self.content * (self.SERVICE_DEFAULT_FONT_SIZE + self.SERVICE_DEFAULT_SPACING)
Page.addField(self, field)
end
function ServicePage:refreshFields()
for index, field in ipairs(self.content) do
field.posX = self.SERVICE_DEFAULT_MARGIN[1]
field.posY = self.SERVICE_DEFAULT_MARGIN[2] + (index - 1) * (self.SERVICE_DEFAULT_FONT_SIZE + self.SERVICE_DEFAULT_SPACING)
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)
2022-04-05 17:31:02 +02:00
gfx.Rect(0, 0, Dim.design.width, Dim.design.height)
gfx.Fill()
end
function ServicePage:drawHeader(deltaTime)
local pageTopTitleMargin = self.SERVICE_DEFAULT_FONT_SIZE
local lineHeight = self.SERVICE_DEFAULT_FONT_SIZE + self.SERVICE_DEFAULT_SPACING
gfx.BeginPath()
gfx.FontSize(self.SERVICE_DEFAULT_FONT_SIZE)
gfx.LoadSkinFont(self.SERVICE_DEFAULT_FONT_FACE)
gfx.FillColor(table.unpack(self.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
2022-04-05 17:31:02 +02:00
gfx.Text(line, Dim.design.width / 2, pageTopTitleMargin + (index-1) * lineHeight)
end
elseif type(self.title) == "string" then
2022-04-05 17:31:02 +02:00
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 = self.SERVICE_DEFAULT_MARGIN[4]
local lineHeight = self.SERVICE_DEFAULT_FONT_SIZE + self.SERVICE_DEFAULT_SPACING
gfx.BeginPath()
gfx.FontSize(self.SERVICE_DEFAULT_FONT_SIZE)
gfx.LoadSkinFont(self.SERVICE_DEFAULT_FONT_FACE)
gfx.FillColor(table.unpack(self.SERVICE_DEFAULT_FONT_COLOR))
gfx.TextAlign(gfx.TEXT_ALIGN_CENTER | gfx.TEXT_ALIGN_BOTTOM)
if type(footer) == "table" then
2022-04-05 17:31:02 +02:00
local yFooterBase = Dim.design.height - bottomPageMargin - #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
2022-04-05 17:31:02 +02:00
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
field:deactivate()
-- 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
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