refactor class.lua (because Local is a bulli)

This commit is contained in:
Hersi 2022-04-27 00:06:42 +02:00
parent d1458d2ee2
commit 3ea1429eab
25 changed files with 32 additions and 44 deletions

View File

@ -1,15 +1,3 @@
---Declare table as a base class
---@generic T
---@param cls T # class metatable
---@param o? table # initial parameters
---@return T # class instance
function Base(cls, o)
o = o or {}
cls.__index = cls
setmetatable(o, cls)
return o
end
local function search(key, bases) local function search(key, bases)
for _, base in ipairs(bases) do for _, base in ipairs(bases) do
local v = base[key] -- try `i'-th superclass local v = base[key] -- try `i'-th superclass
@ -17,26 +5,26 @@ local function search(key, bases)
end end
end end
---Declare table as a derived class ---Declare table as polimorphic class
---@generic BaseT, T ---@generic BaseT, T
---@param cls T # class metatable ---@param cls T # class metatable
---@param o? table # initial parameters ---@param o? table # initial parameters
---@param ... BaseT # base class metatables ---@param ... BaseT # base class metatables (if any)
---@return T # derived class instance ---@return T # class instance
function Inherit(cls, o, ...) function CreateClass(cls, o, ...)
o = o or {} o = o or {}
local nargs = select("#", ...) local nargs = select("#", ...)
local vargs = { select(1, ...) } local vargs = { select(1, ...) }
cls.__index = cls cls.__index = cls
if nargs > 1 then if nargs == 1 then
local base = vargs[1]
setmetatable(cls, {__index = base})
o = base:new(o)
elseif nargs > 1 then
setmetatable(cls, {__index = function(t, k) return search(k, vargs) end}) setmetatable(cls, {__index = function(t, k) return search(k, vargs) end})
for _, base in ipairs(vargs) do for _, base in ipairs(vargs) do
o = base:new(o) o = base:new(o)
end end
else
local base = assert(vargs[1], "You must at least specify one class to inherit from.")
setmetatable(cls, {__index = base})
o = base:new(o)
end end
setmetatable(o, cls) setmetatable(o, cls)
return o return o

View File

@ -17,7 +17,7 @@ function ContainerField:new(o)
o.content = o.content or {} o.content = o.content or {}
local this = Inherit(self, o, Field) local this = CreateClass(self, o, Field)
this:refreshFields() this:refreshFields()

View File

@ -24,7 +24,7 @@ function Field:new(o)
o.aabbW = o.aabbW or 0 o.aabbW = o.aabbW or 0
o.aabbH = o.aabbH or 0 o.aabbH = o.aabbH or 0
return Base(self, o) return CreateClass(self, o)
end end
---Get the containing top-level parent page ---Get the containing top-level parent page

View File

@ -15,7 +15,7 @@ function LinkField:new(o)
o.link = o.link or nil o.link = o.link or nil
return Inherit(self, o, Field) return CreateClass(self, o, Field)
end end
---@param button integer # options are under the `game` table prefixed with `BUTTON` ---@param button integer # options are under the `game` table prefixed with `BUTTON`

View File

@ -19,7 +19,7 @@ function Page:new(o)
o.content = o.content or {} o.content = o.content or {}
o.viewHandler = o.viewHandler or nil o.viewHandler = o.viewHandler or nil
return Base(self, o) return CreateClass(self, o)
end end
---Add field to page ---Add field to page

View File

@ -29,7 +29,7 @@ function PageView:new(rootPage)
o.pageStack = {} o.pageStack = {}
pushStack(o.pageStack, rootPage) pushStack(o.pageStack, rootPage)
return Base(self, o) return CreateClass(self, o)
end end
---Get page from pageStack ---Get page from pageStack

View File

@ -20,7 +20,7 @@ function CheckUpdateField:new(o)
o._timer = o._timer or 0 o._timer = o._timer or 0
o.onUpdateAvailable = o.onUpdateAvailable or nil o.onUpdateAvailable = o.onUpdateAvailable or nil
local this = Inherit(self, o, ServiceField) local this = CreateClass(self, o, ServiceField)
this._url = nil this._url = nil
this._version = nil this._version = nil

View File

@ -43,7 +43,7 @@ function DialogField:new(o)
o.aabbW = o.aabbW or self.DEFAULT_WIDTH o.aabbW = o.aabbW or self.DEFAULT_WIDTH
o.aabbH = o.aabbH or self.DEFAULT_HEIGHT o.aabbH = o.aabbH or self.DEFAULT_HEIGHT
local this = Inherit(self, o, ContainerField) local this = CreateClass(self, o, ContainerField)
this._symbolMargin = 8 this._symbolMargin = 8
this._symbolSize = 48 this._symbolSize = 48

View File

@ -45,7 +45,7 @@ function SelfTestField:new(o)
"Failed to construct SelfTestField, checkTask is mandatory when onStatusChange is defined!\n" .. debug.traceback() "Failed to construct SelfTestField, checkTask is mandatory when onStatusChange is defined!\n" .. debug.traceback()
) )
return Inherit(self, o, ServiceField) return CreateClass(self, o, ServiceField)
end end
function SelfTestField:_closeThread() function SelfTestField:_closeThread()

View File

@ -18,7 +18,7 @@ function ColorGradientField:new(o)
o.value = o.value or {0, 0, 0, 255} o.value = o.value or {0, 0, 0, 255}
return Inherit(self, o, ServiceField) return CreateClass(self, o, ServiceField)
end end
---@param obj? any # message object for the field ---@param obj? any # message object for the field

View File

@ -15,7 +15,7 @@ function InputButtonField:new(o)
o.button = o.button or nil o.button = o.button or nil
return Inherit(self, o, ServiceField) return CreateClass(self, o, ServiceField)
end end
---@param obj? any # message object for the field ---@param obj? any # message object for the field

View File

@ -23,7 +23,7 @@ function InputKnobField:new(o)
o.knob = o.knob or nil o.knob = o.knob or nil
return Inherit(self, o, ServiceField) return CreateClass(self, o, ServiceField)
end end
---@param obj? any # message object for the field ---@param obj? any # message object for the field

View File

@ -22,7 +22,7 @@ function ListField:new(o)
o.selectedIndex = o.selectedIndex or 1 o.selectedIndex = o.selectedIndex or 1
o.locked = o.locked or false o.locked = o.locked or false
local this = Inherit(self, o, ContainerField, ServiceField) local this = CreateClass(self, o, ContainerField, ServiceField)
local minW = this.MARGIN[1] + this.PADDING[1] + this.PADDING[3] + this.MARGIN[3] local minW = this.MARGIN[1] + this.PADDING[1] + this.PADDING[3] + this.MARGIN[3]
local minH = this.MARGIN[2] + this.PADDING[2] + this.PADDING[4] + this.MARGIN[4] local minH = this.MARGIN[2] + this.PADDING[2] + this.PADDING[4] + this.MARGIN[4]

View File

@ -49,7 +49,7 @@ function ServiceField:new(o)
o._state = ServiceFieldState.INACTIVE o._state = ServiceFieldState.INACTIVE
local this = Inherit(self, o, Field) local this = CreateClass(self, o, Field)
if this.aabbH < h then if this.aabbH < h then
this.aabbH = h this.aabbH = h

View File

@ -13,7 +13,7 @@ local ServiceLinkField = {
function ServiceLinkField:new(o) function ServiceLinkField:new(o)
o = o or {} o = o or {}
return Inherit(self, o, ServiceField, LinkField) return CreateClass(self, o, ServiceField, LinkField)
end end
---@param deltaTime number # frametime in seconds ---@param deltaTime number # frametime in seconds

View File

@ -20,7 +20,7 @@ function UpdateField:new(o)
o._timer = 0 o._timer = 0
return Inherit(self, o, ServiceField) return CreateClass(self, o, ServiceField)
end end
---@param obj? any # message object for the field ---@param obj? any # message object for the field

View File

@ -19,7 +19,7 @@ local BootPage = {
function BootPage:new(o) function BootPage:new(o)
o = o or {} o = o or {}
local this = Inherit(self, o, Page) local this = CreateClass(self, o, Page)
this._networkResult = {} this._networkResult = {}

View File

@ -14,7 +14,7 @@ local CheckUpdatePage = {
---@param o? table # initial parameters ---@param o? table # initial parameters
---@return CheckUpdatePage ---@return CheckUpdatePage
function CheckUpdatePage:new(o) function CheckUpdatePage:new(o)
local this = Inherit(self, o, Page) local this = CreateClass(self, o, Page)
local width = DialogField.DEFAULT_WIDTH local width = DialogField.DEFAULT_WIDTH
local height = DialogField.DEFAULT_HEIGHT local height = DialogField.DEFAULT_HEIGHT

View File

@ -30,7 +30,7 @@ function ColorCheckPage:new(o)
"BACK BUTTON = EXIT" "BACK BUTTON = EXIT"
} }
local this = Inherit(self, o, ServicePage) local this = CreateClass(self, o, ServicePage)
local height = self.GRADIENT_SPACING local height = self.GRADIENT_SPACING
local list = ListField:new() local list = ListField:new()

View File

@ -18,7 +18,7 @@ function InputCheckPage:new(o)
o.title = o.title or "INPUT CHECK" o.title = o.title or "INPUT CHECK"
o.footer = o.footer or "BACK BUTTON = EXIT" o.footer = o.footer or "BACK BUTTON = EXIT"
local this = Inherit(self, o, ServicePage) local this = CreateClass(self, o, ServicePage)
local list = ListField:new() local list = ListField:new()
list:addField(InputButtonField:new{label="START BUTTON", button=game.BUTTON_STA}) list:addField(InputButtonField:new{label="START BUTTON", button=game.BUTTON_STA})

View File

@ -20,7 +20,7 @@ function MainMenuPage:new(o)
o.title = o.title or "MAIN MENU" o.title = o.title or "MAIN MENU"
local this = Inherit(self, o, ServicePage) local this = CreateClass(self, o, ServicePage)
local list = ListField:new() local list = ListField:new()
list:addField(ServiceLinkField:new{label = "INPUT CHECK", link = InputCheckPage:new()}) list:addField(ServiceLinkField:new{label = "INPUT CHECK", link = InputCheckPage:new()})

View File

@ -28,7 +28,7 @@ function ScreenCheckPage:new(o)
"BACK BUTTON = EXIT" "BACK BUTTON = EXIT"
} }
return Inherit(self, o, ServicePage) return CreateClass(self, o, ServicePage)
end end
---@param button integer # options are under the `game` table prefixed with `BUTTON` ---@param button integer # options are under the `game` table prefixed with `BUTTON`

View File

@ -43,7 +43,7 @@ function ServicePage:new(o)
o.selectedIndex = o.selectedIndex or 1 o.selectedIndex = o.selectedIndex or 1
o.footer = o.footer or self.FOOTER o.footer = o.footer or self.FOOTER
return Inherit(self, o, Page) return CreateClass(self, o, Page)
end end
---Refresh content values ---Refresh content values

View File

@ -30,7 +30,7 @@ function VersionInfoPage:new(o)
} }
o.selectedIndex = o.selectedIndex or 1 o.selectedIndex = o.selectedIndex or 1
local this = Inherit(self, o, ServicePage) local this = CreateClass(self, o, ServicePage)
local logStr = ReadGameFile("log_usc-game.exe.txt") or ReadGameFile("log_usc-game.txt") local logStr = ReadGameFile("log_usc-game.exe.txt") or ReadGameFile("log_usc-game.txt")