ExperimentalGear/scripts/titlescreen/fields/boot/selftestfield.lua

77 lines
2.2 KiB
Lua

require("common.class")
local Util = require("common.util")
local ServiceField = require("titlescreen.fields.service.servicefield")
---@class SelfTestStatusEnum
SelfTestStatusEnum = {
IDLE = 1,
INPROGRESS = 2,
OK = 3,
PASS = 4,
ERROR = 5
}
---@class SelfTestField: ServiceField
---@field status SelfTestStatusEnum
---@field callback nil|fun(obj: any): SelfTestStatusEnum
---@field _timer number
local SelfTestField = {
SELFTEST_COLOR_INPROGRESS = {255, 255, 255, 255},
SELFTEST_COLOR_OK = {0, 255, 0, 255},
SELFTEST_COLOR_PASS = {255, 255, 0, 255},
SELFTEST_COLOR_ERROR = {255, 0, 0, 255},
SELFTEST_INPROGRESS_FREQ = 1 / 20, --20Hz
}
---Create a new SelfTestField instance
---@param o? table
---@return SelfTestField
function SelfTestField:new(o)
o = o or {}
o.status = o.status or SelfTestStatusEnum.IDLE
o.callback = o.callback or nil
o._timer = 0
return Inherit(self, o, ServiceField)
end
function SelfTestField:activate(obj)
if self.callback then
self.status = self.callback(obj) or SelfTestStatusEnum.INPROGRESS
end
end
function SelfTestField:drawValue(deltaTime)
gfx.Translate(self.VALUE_OFFSETX, 0)
gfx.TextAlign(gfx.TEXT_ALIGN_RIGHT | gfx.TEXT_ALIGN_TOP)
gfx.FillColor(table.unpack(self.FONT_COLOR))
gfx.Text(": ", 0, 0)
local color, text
if self.status == SelfTestStatusEnum.IDLE then
color = self.FONT_COLOR
text = ""
elseif self.status == SelfTestStatusEnum.INPROGRESS then
self._timer = self._timer + deltaTime
local progress = math.ceil(Util.lerp(self._timer % 1, 0, 0, 1, 4))
color = self.SELFTEST_COLOR_INPROGRESS
text = string.rep(".", progress)
elseif self.status == SelfTestStatusEnum.OK then
color = self.SELFTEST_COLOR_OK
text = "OK"
elseif self.status == SelfTestStatusEnum.PASS then
color = self.SELFTEST_COLOR_PASS
text = "PASS"
elseif self.status == SelfTestStatusEnum.ERROR then
color = self.SELFTEST_COLOR_ERROR
text = "ERROR"
end
gfx.TextAlign(gfx.TEXT_ALIGN_LEFT | gfx.TEXT_ALIGN_TOP)
gfx.FillColor(table.unpack(color))
gfx.Text(text, 0, 0)
end
return SelfTestField