ExperimentalGear/scripts/titlescreen/common/page/servicefield.lua

93 lines
2.5 KiB
Lua

require("common.class")
local Display = require("scripts.graphics.display")
local Field = require("api.page.field")
---@class ServiceField: Field
---@field label string
---@field value any
---@field footer string|string[]
---@field FONT_SIZE number
---@field FONT_FACE string
---@field FONT_COLOR integer[] # {r, g, b, a}
---@field FONT_ACTIVE_COLOR integer[] # {r, g, b, a}
---@field FONT_FOCUSED_COLOR integer[] # {r, g, b, a}
---@field MARGIN number[] # {left, top, right, bottom}
---@field VALUE_OFFSETX number
local ServiceField = {
__name = "ServiceField",
FONT_SIZE = 24,
FONT_FACE = "dfmarugoth.ttf",
FONT_COLOR = {255, 255, 255, 255},
FONT_ACTIVE_COLOR = {0, 255, 0, 255},
FONT_FOCUSED_COLOR = {255, 0, 0, 255},
MARGIN = {0, 0, 0, 0},
VALUE_OFFSETX = 500
}
---Create a new ServiceField instance
---@param params? ServiceField # initial parameters
---@return ServiceField
function ServiceField.new(params)
params = params or {}
local h = ServiceField.FONT_SIZE + ServiceField.MARGIN[2] + ServiceField.MARGIN[4]
params.aabbH = params.aabbH or h
params.aabbW = params.aabbW or Display.design.width --:shrug:
params.label = params.label or "<UNDEFINED>"
params.value = params.value or nil
params.footer = params.footer or nil
local self = CreateInstance(ServiceField, params, Field)
if self.aabbH < h then
self.aabbH = h
end
return self
end
---@param deltaTime number # frametime in seconds
function ServiceField:drawLabel(deltaTime)
local color
if self.focused then
color = self.FONT_FOCUSED_COLOR
else
color = self.FONT_COLOR
end
gfx.FontSize(self.FONT_SIZE)
gfx.LoadSkinFont(self.FONT_FACE)
gfx.TextAlign(gfx.TEXT_ALIGN_LEFT | gfx.TEXT_ALIGN_TOP)
gfx.FillColor(table.unpack(color))
gfx.Text(string.upper(self.label), 0, 0)
end
---@param deltaTime number # frametime in seconds
function ServiceField:drawValue(deltaTime)
local text
if type(self.value) == "string" then
text = string.upper(self.value)
else
text = "N/A"
end
gfx.Translate(self.VALUE_OFFSETX, 0)
gfx.FontSize(self.FONT_SIZE)
gfx.LoadSkinFont(self.FONT_FACE)
gfx.TextAlign(gfx.TEXT_ALIGN_LEFT | gfx.TEXT_ALIGN_TOP)
gfx.FillColor(table.unpack(self.FONT_COLOR))
gfx.Text(text, 0, 0)
end
---@param deltaTime number # frametime in seconds
function ServiceField:drawContent(deltaTime)
gfx.Translate(self.MARGIN[1], self.MARGIN[2])
self:drawLabel(deltaTime)
self:drawValue(deltaTime)
end
return ServiceField