2022-04-11 18:47:26 +02:00
|
|
|
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
|
2022-04-15 04:54:30 +02:00
|
|
|
---@field callback nil|fun(obj: any): SelfTestStatusEnum
|
2022-04-11 18:47:26 +02:00
|
|
|
---@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},
|
2022-04-15 04:54:30 +02:00
|
|
|
SELFTEST_INPROGRESS_FREQ = 1 / 20, --20Hz
|
2022-04-11 18:47:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
---Create a new SelfTestField instance
|
2022-04-15 04:54:30 +02:00
|
|
|
---@param o? table
|
2022-04-11 18:47:26 +02:00
|
|
|
---@return SelfTestField
|
|
|
|
function SelfTestField:new(o)
|
|
|
|
o = o or {}
|
|
|
|
|
|
|
|
o.status = o.status or SelfTestStatusEnum.IDLE
|
2022-04-15 04:54:30 +02:00
|
|
|
o.callback = o.callback or nil
|
2022-04-11 18:47:26 +02:00
|
|
|
o._timer = 0
|
|
|
|
|
|
|
|
return Inherit(self, o, ServiceField)
|
|
|
|
end
|
|
|
|
|
2022-04-15 04:54:30 +02:00
|
|
|
function SelfTestField:activate(obj)
|
|
|
|
if self.callback then
|
|
|
|
self.status = self.callback(obj) or SelfTestStatusEnum.INPROGRESS
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-11 18:47:26 +02:00
|
|
|
function SelfTestField:drawValue(deltaTime)
|
2022-04-15 04:54:30 +02:00
|
|
|
gfx.Translate(self.VALUE_OFFSETX, 0)
|
|
|
|
|
2022-04-11 18:47:26 +02:00
|
|
|
gfx.TextAlign(gfx.TEXT_ALIGN_RIGHT | gfx.TEXT_ALIGN_TOP)
|
2022-04-15 04:54:30 +02:00
|
|
|
gfx.FillColor(table.unpack(self.FONT_COLOR))
|
|
|
|
gfx.Text(": ", 0, 0)
|
2022-04-11 18:47:26 +02:00
|
|
|
|
|
|
|
local color, text
|
|
|
|
if self.status == SelfTestStatusEnum.IDLE then
|
2022-04-15 04:54:30 +02:00
|
|
|
color = self.FONT_COLOR
|
2022-04-11 18:47:26 +02:00
|
|
|
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
|
2022-04-15 04:54:30 +02:00
|
|
|
|
2022-04-11 18:47:26 +02:00
|
|
|
gfx.TextAlign(gfx.TEXT_ALIGN_LEFT | gfx.TEXT_ALIGN_TOP)
|
|
|
|
gfx.FillColor(table.unpack(color))
|
2022-04-15 04:54:30 +02:00
|
|
|
gfx.Text(text, 0, 0)
|
2022-04-11 18:47:26 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
return SelfTestField
|