From 3ea1429eab34184568468b7902c61539f0d2940f Mon Sep 17 00:00:00 2001 From: Hersi Date: Wed, 27 Apr 2022 00:06:42 +0200 Subject: [PATCH] refactor class.lua (because Local is a bulli) --- scripts/common/class.lua | 30 ++++++------------- scripts/components/pager/containerfield.lua | 2 +- scripts/components/pager/field.lua | 2 +- scripts/components/pager/linkfield.lua | 2 +- scripts/components/pager/page.lua | 2 +- scripts/components/pager/pageview.lua | 2 +- .../fields/boot/checkupdatefield.lua | 2 +- .../titlescreen/fields/boot/dialogfield.lua | 2 +- .../titlescreen/fields/boot/selftestfield.lua | 2 +- .../fields/service/colorgradientfield.lua | 2 +- .../fields/service/inputbuttonfield.lua | 2 +- .../fields/service/inputknobfield.lua | 2 +- .../titlescreen/fields/service/listfield.lua | 2 +- .../fields/service/servicefield.lua | 2 +- .../fields/service/servicelinkfield.lua | 2 +- .../fields/service/updatefield.lua | 2 +- scripts/titlescreen/pages/boot/bootpage.lua | 2 +- .../pages/boot/checkupdatepage.lua | 2 +- .../pages/modeselect/myroompage.lua | 0 .../pages/service/colorcheckpage.lua | 2 +- .../pages/service/inputcheckpage.lua | 2 +- .../pages/service/mainmenupage.lua | 2 +- .../pages/service/screencheckpage.lua | 2 +- .../titlescreen/pages/service/servicepage.lua | 2 +- .../pages/service/versioninfopage.lua | 2 +- 25 files changed, 32 insertions(+), 44 deletions(-) create mode 100644 scripts/titlescreen/pages/modeselect/myroompage.lua diff --git a/scripts/common/class.lua b/scripts/common/class.lua index b147c9e..4c7f409 100644 --- a/scripts/common/class.lua +++ b/scripts/common/class.lua @@ -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) for _, base in ipairs(bases) do local v = base[key] -- try `i'-th superclass @@ -17,26 +5,26 @@ local function search(key, bases) end end ----Declare table as a derived class +---Declare table as polimorphic class ---@generic BaseT, T ---@param cls T # class metatable ---@param o? table # initial parameters ----@param ... BaseT # base class metatables ----@return T # derived class instance -function Inherit(cls, o, ...) +---@param ... BaseT # base class metatables (if any) +---@return T # class instance +function CreateClass(cls, o, ...) o = o or {} local nargs = select("#", ...) local vargs = { select(1, ...) } 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}) for _, base in ipairs(vargs) do o = base:new(o) 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 setmetatable(o, cls) return o diff --git a/scripts/components/pager/containerfield.lua b/scripts/components/pager/containerfield.lua index 12f015a..bf912c0 100644 --- a/scripts/components/pager/containerfield.lua +++ b/scripts/components/pager/containerfield.lua @@ -17,7 +17,7 @@ function ContainerField:new(o) o.content = o.content or {} - local this = Inherit(self, o, Field) + local this = CreateClass(self, o, Field) this:refreshFields() diff --git a/scripts/components/pager/field.lua b/scripts/components/pager/field.lua index a97ff47..14a292a 100644 --- a/scripts/components/pager/field.lua +++ b/scripts/components/pager/field.lua @@ -24,7 +24,7 @@ function Field:new(o) o.aabbW = o.aabbW or 0 o.aabbH = o.aabbH or 0 - return Base(self, o) + return CreateClass(self, o) end ---Get the containing top-level parent page diff --git a/scripts/components/pager/linkfield.lua b/scripts/components/pager/linkfield.lua index 75df179..19c85ba 100644 --- a/scripts/components/pager/linkfield.lua +++ b/scripts/components/pager/linkfield.lua @@ -15,7 +15,7 @@ function LinkField:new(o) o.link = o.link or nil - return Inherit(self, o, Field) + return CreateClass(self, o, Field) end ---@param button integer # options are under the `game` table prefixed with `BUTTON` diff --git a/scripts/components/pager/page.lua b/scripts/components/pager/page.lua index 51001e2..b623603 100644 --- a/scripts/components/pager/page.lua +++ b/scripts/components/pager/page.lua @@ -19,7 +19,7 @@ function Page:new(o) o.content = o.content or {} o.viewHandler = o.viewHandler or nil - return Base(self, o) + return CreateClass(self, o) end ---Add field to page diff --git a/scripts/components/pager/pageview.lua b/scripts/components/pager/pageview.lua index 080a391..9d71a07 100644 --- a/scripts/components/pager/pageview.lua +++ b/scripts/components/pager/pageview.lua @@ -29,7 +29,7 @@ function PageView:new(rootPage) o.pageStack = {} pushStack(o.pageStack, rootPage) - return Base(self, o) + return CreateClass(self, o) end ---Get page from pageStack diff --git a/scripts/titlescreen/fields/boot/checkupdatefield.lua b/scripts/titlescreen/fields/boot/checkupdatefield.lua index 51c81a9..fea7794 100644 --- a/scripts/titlescreen/fields/boot/checkupdatefield.lua +++ b/scripts/titlescreen/fields/boot/checkupdatefield.lua @@ -20,7 +20,7 @@ function CheckUpdateField:new(o) o._timer = o._timer or 0 o.onUpdateAvailable = o.onUpdateAvailable or nil - local this = Inherit(self, o, ServiceField) + local this = CreateClass(self, o, ServiceField) this._url = nil this._version = nil diff --git a/scripts/titlescreen/fields/boot/dialogfield.lua b/scripts/titlescreen/fields/boot/dialogfield.lua index 94cc313..417a065 100644 --- a/scripts/titlescreen/fields/boot/dialogfield.lua +++ b/scripts/titlescreen/fields/boot/dialogfield.lua @@ -43,7 +43,7 @@ function DialogField:new(o) o.aabbW = o.aabbW or self.DEFAULT_WIDTH o.aabbH = o.aabbH or self.DEFAULT_HEIGHT - local this = Inherit(self, o, ContainerField) + local this = CreateClass(self, o, ContainerField) this._symbolMargin = 8 this._symbolSize = 48 diff --git a/scripts/titlescreen/fields/boot/selftestfield.lua b/scripts/titlescreen/fields/boot/selftestfield.lua index c518ed2..87f6bd9 100644 --- a/scripts/titlescreen/fields/boot/selftestfield.lua +++ b/scripts/titlescreen/fields/boot/selftestfield.lua @@ -45,7 +45,7 @@ function SelfTestField:new(o) "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 function SelfTestField:_closeThread() diff --git a/scripts/titlescreen/fields/service/colorgradientfield.lua b/scripts/titlescreen/fields/service/colorgradientfield.lua index c242b3a..2c6366b 100644 --- a/scripts/titlescreen/fields/service/colorgradientfield.lua +++ b/scripts/titlescreen/fields/service/colorgradientfield.lua @@ -18,7 +18,7 @@ function ColorGradientField:new(o) o.value = o.value or {0, 0, 0, 255} - return Inherit(self, o, ServiceField) + return CreateClass(self, o, ServiceField) end ---@param obj? any # message object for the field diff --git a/scripts/titlescreen/fields/service/inputbuttonfield.lua b/scripts/titlescreen/fields/service/inputbuttonfield.lua index c8f1571..a9af384 100644 --- a/scripts/titlescreen/fields/service/inputbuttonfield.lua +++ b/scripts/titlescreen/fields/service/inputbuttonfield.lua @@ -15,7 +15,7 @@ function InputButtonField:new(o) o.button = o.button or nil - return Inherit(self, o, ServiceField) + return CreateClass(self, o, ServiceField) end ---@param obj? any # message object for the field diff --git a/scripts/titlescreen/fields/service/inputknobfield.lua b/scripts/titlescreen/fields/service/inputknobfield.lua index 6a3c43f..2fde1cb 100644 --- a/scripts/titlescreen/fields/service/inputknobfield.lua +++ b/scripts/titlescreen/fields/service/inputknobfield.lua @@ -23,7 +23,7 @@ function InputKnobField:new(o) o.knob = o.knob or nil - return Inherit(self, o, ServiceField) + return CreateClass(self, o, ServiceField) end ---@param obj? any # message object for the field diff --git a/scripts/titlescreen/fields/service/listfield.lua b/scripts/titlescreen/fields/service/listfield.lua index ec330c9..b25de81 100644 --- a/scripts/titlescreen/fields/service/listfield.lua +++ b/scripts/titlescreen/fields/service/listfield.lua @@ -22,7 +22,7 @@ function ListField:new(o) o.selectedIndex = o.selectedIndex or 1 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 minH = this.MARGIN[2] + this.PADDING[2] + this.PADDING[4] + this.MARGIN[4] diff --git a/scripts/titlescreen/fields/service/servicefield.lua b/scripts/titlescreen/fields/service/servicefield.lua index bf99af6..732d9f7 100644 --- a/scripts/titlescreen/fields/service/servicefield.lua +++ b/scripts/titlescreen/fields/service/servicefield.lua @@ -49,7 +49,7 @@ function ServiceField:new(o) o._state = ServiceFieldState.INACTIVE - local this = Inherit(self, o, Field) + local this = CreateClass(self, o, Field) if this.aabbH < h then this.aabbH = h diff --git a/scripts/titlescreen/fields/service/servicelinkfield.lua b/scripts/titlescreen/fields/service/servicelinkfield.lua index 57b211f..b469ba4 100644 --- a/scripts/titlescreen/fields/service/servicelinkfield.lua +++ b/scripts/titlescreen/fields/service/servicelinkfield.lua @@ -13,7 +13,7 @@ local ServiceLinkField = { function ServiceLinkField:new(o) o = o or {} - return Inherit(self, o, ServiceField, LinkField) + return CreateClass(self, o, ServiceField, LinkField) end ---@param deltaTime number # frametime in seconds diff --git a/scripts/titlescreen/fields/service/updatefield.lua b/scripts/titlescreen/fields/service/updatefield.lua index 24d56ee..530faf8 100644 --- a/scripts/titlescreen/fields/service/updatefield.lua +++ b/scripts/titlescreen/fields/service/updatefield.lua @@ -20,7 +20,7 @@ function UpdateField:new(o) o._timer = 0 - return Inherit(self, o, ServiceField) + return CreateClass(self, o, ServiceField) end ---@param obj? any # message object for the field diff --git a/scripts/titlescreen/pages/boot/bootpage.lua b/scripts/titlescreen/pages/boot/bootpage.lua index a311bb4..5634930 100644 --- a/scripts/titlescreen/pages/boot/bootpage.lua +++ b/scripts/titlescreen/pages/boot/bootpage.lua @@ -19,7 +19,7 @@ local BootPage = { function BootPage:new(o) o = o or {} - local this = Inherit(self, o, Page) + local this = CreateClass(self, o, Page) this._networkResult = {} diff --git a/scripts/titlescreen/pages/boot/checkupdatepage.lua b/scripts/titlescreen/pages/boot/checkupdatepage.lua index fab0680..620419f 100644 --- a/scripts/titlescreen/pages/boot/checkupdatepage.lua +++ b/scripts/titlescreen/pages/boot/checkupdatepage.lua @@ -14,7 +14,7 @@ local CheckUpdatePage = { ---@param o? table # initial parameters ---@return CheckUpdatePage function CheckUpdatePage:new(o) - local this = Inherit(self, o, Page) + local this = CreateClass(self, o, Page) local width = DialogField.DEFAULT_WIDTH local height = DialogField.DEFAULT_HEIGHT diff --git a/scripts/titlescreen/pages/modeselect/myroompage.lua b/scripts/titlescreen/pages/modeselect/myroompage.lua new file mode 100644 index 0000000..e69de29 diff --git a/scripts/titlescreen/pages/service/colorcheckpage.lua b/scripts/titlescreen/pages/service/colorcheckpage.lua index c61e95e..1018bbc 100644 --- a/scripts/titlescreen/pages/service/colorcheckpage.lua +++ b/scripts/titlescreen/pages/service/colorcheckpage.lua @@ -30,7 +30,7 @@ function ColorCheckPage:new(o) "BACK BUTTON = EXIT" } - local this = Inherit(self, o, ServicePage) + local this = CreateClass(self, o, ServicePage) local height = self.GRADIENT_SPACING local list = ListField:new() diff --git a/scripts/titlescreen/pages/service/inputcheckpage.lua b/scripts/titlescreen/pages/service/inputcheckpage.lua index 4f41a90..acf25be 100644 --- a/scripts/titlescreen/pages/service/inputcheckpage.lua +++ b/scripts/titlescreen/pages/service/inputcheckpage.lua @@ -18,7 +18,7 @@ function InputCheckPage:new(o) o.title = o.title or "INPUT CHECK" o.footer = o.footer or "BACK BUTTON = EXIT" - local this = Inherit(self, o, ServicePage) + local this = CreateClass(self, o, ServicePage) local list = ListField:new() list:addField(InputButtonField:new{label="START BUTTON", button=game.BUTTON_STA}) diff --git a/scripts/titlescreen/pages/service/mainmenupage.lua b/scripts/titlescreen/pages/service/mainmenupage.lua index f8d364a..61aa6ff 100644 --- a/scripts/titlescreen/pages/service/mainmenupage.lua +++ b/scripts/titlescreen/pages/service/mainmenupage.lua @@ -20,7 +20,7 @@ function MainMenuPage:new(o) o.title = o.title or "MAIN MENU" - local this = Inherit(self, o, ServicePage) + local this = CreateClass(self, o, ServicePage) local list = ListField:new() list:addField(ServiceLinkField:new{label = "INPUT CHECK", link = InputCheckPage:new()}) diff --git a/scripts/titlescreen/pages/service/screencheckpage.lua b/scripts/titlescreen/pages/service/screencheckpage.lua index 11168f7..b70ef71 100644 --- a/scripts/titlescreen/pages/service/screencheckpage.lua +++ b/scripts/titlescreen/pages/service/screencheckpage.lua @@ -28,7 +28,7 @@ function ScreenCheckPage:new(o) "BACK BUTTON = EXIT" } - return Inherit(self, o, ServicePage) + return CreateClass(self, o, ServicePage) end ---@param button integer # options are under the `game` table prefixed with `BUTTON` diff --git a/scripts/titlescreen/pages/service/servicepage.lua b/scripts/titlescreen/pages/service/servicepage.lua index 42bcf8c..1dbf4f4 100644 --- a/scripts/titlescreen/pages/service/servicepage.lua +++ b/scripts/titlescreen/pages/service/servicepage.lua @@ -43,7 +43,7 @@ function ServicePage:new(o) o.selectedIndex = o.selectedIndex or 1 o.footer = o.footer or self.FOOTER - return Inherit(self, o, Page) + return CreateClass(self, o, Page) end ---Refresh content values diff --git a/scripts/titlescreen/pages/service/versioninfopage.lua b/scripts/titlescreen/pages/service/versioninfopage.lua index f8acfd2..e1abe98 100644 --- a/scripts/titlescreen/pages/service/versioninfopage.lua +++ b/scripts/titlescreen/pages/service/versioninfopage.lua @@ -30,7 +30,7 @@ function VersionInfoPage:new(o) } 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")