ExperimentalGear/scripts/titlescreen/boot/checkupdatefield.lua

73 lines
2.0 KiB
Lua
Raw Normal View History

2022-04-24 01:39:48 +02:00
require("common.class")
local Util = require("common.util")
local ServiceField = require("api.page.servicefield")
2022-04-24 01:39:48 +02:00
---@class CheckUpdateField: ServiceField
---@field onUpdateAvailable nil|fun(url: string, version: string)
---@field _timer number
local CheckUpdateField = {
__name = "CheckUpdateField",
PROGRESS_FREQ = 3, -- seconds
CHECK_UPDATE_TIMEOUT = 5, -- seconds
2022-04-24 01:39:48 +02:00
}
---Create a new CheckUpdateField instance
---@param params? CheckUpdateField # initial parameters
2022-04-24 01:39:48 +02:00
---@return CheckUpdateField
function CheckUpdateField.new(params)
params = params or {}
2022-04-24 01:39:48 +02:00
params.onUpdateAvailable = params.onUpdateAvailable or nil
2022-04-24 01:39:48 +02:00
local self = CreateInstance(CheckUpdateField, params, ServiceField)
2022-04-24 01:39:48 +02:00
self._timer = 0
2022-04-24 01:39:48 +02:00
self._url = nil
self._version = nil
self._onUpdateAvailableFired = false
return self
2022-04-24 01:39:48 +02:00
end
function CheckUpdateField:drawLabel(deltaTime)
local text = self.label
local progress = math.ceil(Util.lerp(self._timer % self.PROGRESS_FREQ,
0, 0, self.PROGRESS_FREQ, 4
))
text = text .. string.rep(".", progress)
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
function CheckUpdateField:drawValue(deltaTime)
end
function CheckUpdateField:tick(deltaTime)
if not self._onUpdateAvailableFired then
if self._timer > self.CHECK_UPDATE_TIMEOUT then
self._url, self._version = game.UpdateAvailable()
-- self._url = "" -- debug code to force onUpdateAvailable()
if self._url then
self.onUpdateAvailable(self._url, self._version)
self._onUpdateAvailableFired = true
else
self:getParentPage():onInvalidation() -- Exit out of bootscreen
end
2022-04-24 01:39:48 +02:00
end
end
self._timer = self._timer + deltaTime
end
function CheckUpdateField:render(deltaTime)
self:tick(deltaTime)
ServiceField.render(self, deltaTime)
end
return CheckUpdateField