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

66 lines
2.0 KiB
Lua
Raw Normal View History

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 _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 ServiceField
---@return SelfTestField
function SelfTestField:new(o)
o = o or {}
o.status = o.status or SelfTestStatusEnum.IDLE
o._timer = 0
return Inherit(self, o, ServiceField)
end
function SelfTestField:drawValue(deltaTime)
gfx.TextAlign(gfx.TEXT_ALIGN_RIGHT | gfx.TEXT_ALIGN_TOP)
gfx.FillColor(table.unpack(self.SERVICE_DEFAULT_FONT_COLOR))
gfx.Text(":", self.valueOffX, 0)
local color, text
if self.status == SelfTestStatusEnum.IDLE then
color = self.SERVICE_DEFAULT_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, self.valueOffX, 0)
end
return SelfTestField