add __tostring fields to classes

This commit is contained in:
Hersi 2022-04-05 23:53:19 +02:00
parent 83c3504e7d
commit df66b1592b
16 changed files with 69 additions and 37 deletions

View File

@ -5,6 +5,7 @@ require("common.class")
---@field posX number
---@field posY number
local Field = {
__tostring = function () return "Field" end,
---@type nil|fun(button: integer): boolean
---returns true if further button input processing should be stopped, otherwise false
handleButtonInput = nil,
@ -69,4 +70,4 @@ function Field:render(deltaTime)
gfx.Restore()
end
return Field
return Field

View File

@ -3,7 +3,9 @@ local Field = require("components.pager.field")
---@class LinkField: Field
---@field link Page
local LinkField = {}
local LinkField = {
__tostring = function () return "LinkField" end
}
---Create a new Field instance
---@param o table
@ -19,13 +21,16 @@ end
function LinkField:handleButtonInput(button)
if button == game.BUTTON_STA then
if self.parent and self.parent.viewHandler then
game.Log(self .. " navigate(" .. self.link .. ") called", game.LOGGER_INFO)
self.parent.viewHandler:navigate(self.link)
else
game.Log("LinkField can't access PageView instance to navigate", game.LOGGER_ERROR)
game.Log(self .. " can't access " ..
(self.parent and self.parent.viewHandler or "PageView") ..
" instance to navigate", game.LOGGER_ERROR)
end
end
return false
end
return LinkField
return LinkField

View File

@ -4,6 +4,7 @@ require("common.class")
---@field content Field[]
---@field viewHandler nil|PageView
local Page = {
__tostring = function () return "Page" end,
drawBackground = nil, ---@type nil|fun(deltaTime: number)
drawHeader = nil, ---@type nil|fun(deltaTime: number)
drawFooter = nil, ---@type nil|fun(deltaTime: number)
@ -80,4 +81,4 @@ function Page:render(deltaTime)
end
end
return Page
return Page

View File

@ -2,7 +2,9 @@ require("common.class")
---@class PageView
---@field pageStack Page[]
local PageView = {}
local PageView = {
__tostring = function () return "PageView" end
}
local function pushStack(t, o)
table.insert(t, 1, o)
@ -68,4 +70,4 @@ function PageView:render(deltaTime)
end
end
return PageView
return PageView

View File

@ -5,6 +5,7 @@ local ServiceField = require("titlescreen.fields.service.servicefield")
---@class ColorGradientField: ServiceField
---@field height number
local ColorGradientField = {
__tostring = function () return "ColorGradientField" end,
GRADIENT_X_OFFSET = 128,
GRADIENT_WIDTH = 512,
GRADIENT_STEPS = 32
@ -37,4 +38,4 @@ function ColorGradientField:drawValue(deltaTime)
end
end
return ColorGradientField
return ColorGradientField

View File

@ -4,7 +4,9 @@ local ServiceField = require("titlescreen.fields.service.servicefield")
---@class InputButtonField: ServiceField
---@field button integer
local InputButtonField = {}
local InputButtonField = {
__tostring = function () return "InputButtonField" end,
}
function InputButtonField:new(o)
o = Inherit(self, ServiceField, o)
@ -30,4 +32,4 @@ function InputButtonField:drawValue(deltaTime)
gfx.Text(self.value, posX, 0)
end
return InputButtonField
return InputButtonField

View File

@ -4,7 +4,9 @@ local ServiceField = require("titlescreen.fields.service.servicefield")
---@class InputKnobField: ServiceField
---@field knob integer
local InputKnobField = {}
local InputKnobField = {
__tostring = function () return "InputKnobField" end,
}
function InputKnobField:new(o)
o = Inherit(self, ServiceField, o)
@ -58,4 +60,4 @@ function InputKnobField:drawValue(deltaTime)
end
return InputKnobField
return InputKnobField

View File

@ -1,13 +1,15 @@
require("common.class")
local ServiceField = require("titlescreen.fields.service.servicefield")
---@class LinkServiceField: ServiceField
local LinkServiceField = {}
---@class LinkField: ServiceField
local LinkField = {
__tostring = function () return "LinkField" end,
}
---Create a new Field instance
---@param o ServiceField
---@return LinkServiceField
function LinkServiceField:new(o)
function LinkField:new(o)
o = Inherit(self, ServiceField, o)
o.value = o.value or nil
@ -15,23 +17,26 @@ function LinkServiceField:new(o)
return o
end
function LinkServiceField:drawValue(deltaTime) end
function LinkField:drawValue(deltaTime) end
function LinkServiceField:handleButtonInput(button)
function LinkField:handleButtonInput(button)
if not self.value then
game.Log("LinkServiceField (" .. self.label .. ") does not have a valid link", game.LOGGER_ERROR)
game.Log(self .. " (" .. self.label .. ") does not have a valid link", game.LOGGER_ERROR)
return false
end
if button == game.BUTTON_STA then
if self.parent and self.parent.viewHandler then
game.Log("LinkServiceField (" .. self.label .. ") navigate() called", game.LOGGER_INFO)
game.Log(self .. " (" .. self.label .. ") navigate(" .. self.value .. ") called", game.LOGGER_INFO)
self.parent.viewHandler:navigate(self.value)
else
game.Log("LinkServiceField (" .. self.label .. ") can't access PageView instance to navigate", game.LOGGER_ERROR)
game.Log(self .. "can't access " ..
(self.parent and self.parent.viewHandler or "PageView") ..
" instance to navigate", game.LOGGER_ERROR)
end
end
return false
end
return LinkServiceField
return LinkField

View File

@ -15,6 +15,7 @@ ServiceFieldState = {
---@field footer string|string[]
---@field _state ServiceFieldState
local ServiceField = {
__tostring = function () return "ServiceField" end,
SERVICE_DEFAULT_FONT_SIZE = 24,
SERVICE_DEFAULT_FONT_FACE = "dfmarugoth.ttf",
SERVICE_DEFAULT_FONT_COLOR = {255, 255, 255, 255}, --{r, g, b, a}
@ -79,4 +80,4 @@ function ServiceField:render(deltaTime)
gfx.Restore()
end
return ServiceField
return ServiceField

View File

@ -5,6 +5,7 @@ local ServiceField = require("titlescreen.fields.service.servicefield")
---@class UpdateField: ServiceField
---@field _timer number
local UpdateField = {
__tostring = function () return "UpdateField" end,
UPDATE_FLICKER_TIME = 0.5,
UPDATE_FLICKER_COLORS = {
{255, 0, 0, 255},
@ -53,4 +54,4 @@ function UpdateField:drawValue(deltaTime)
end
return UpdateField
return UpdateField

View File

@ -4,6 +4,8 @@ local ColorGradientField = require("titlescreen.fields.service.colorgradientfiel
---@class ColorCheckPage: ServicePage
local ColorCheckPage = {
__tostring = function () return "ColorCheckPage" end,
SERVICE_DEFAULT_SPACING = 8,
LINE_COLOR = {255, 255, 255, 255},
LINE_WIDTH = 4,
@ -85,4 +87,4 @@ function ColorCheckPage:drawBackground(deltaTime)
gfx.Restore()
end
return ColorCheckPage
return ColorCheckPage

View File

@ -4,7 +4,9 @@ local InputButtonField = require("titlescreen.fields.service.inputbuttonfield")
local InputKnobField = require("titlescreen.fields.service.inputknobfield")
---@class InputCheckPage: ServicePage
local InputCheckPage = {}
local InputCheckPage = {
__tostring = function () return "InputCheckPage" end,
}
---Create a new InputCheckPage instance
---
@ -47,4 +49,4 @@ function InputCheckPage:handleButtonInput(button)
end
end
return InputCheckPage
return InputCheckPage

View File

@ -4,11 +4,13 @@ local InputCheckPage = require("titlescreen.pages.service.inputcheckpage")
local ScreenCheckPage = require("titlescreen.pages.service.screencheckpage")
local ColorCheckPage = require("titlescreen.pages.service.colorcheckpage")
local VersionInfoPage = require("titlescreen.pages.service.versioninfopage")
local LinkField = require("titlescreen.fields.service.linkfield")
local ServiceLinkField = require("titlescreen.fields.service.linkfield")
---@class MainMenuPage: ServicePage
local MainMenuPage = {}
local MainMenuPage = {
__tostring = function () return "MainMenuPage" end,
}
---Create a new MainMenuPage instance
---
@ -20,14 +22,14 @@ function MainMenuPage:new(o)
o.title = o.title or "MAIN MENU"
o:addField(LinkField:new{
o:addField(ServiceLinkField:new{
label = "INPUT CHECK",
value = InputCheckPage:new{
title = "INPUT CHECK",
footer = "BACK BUTTON = EXIT"
}
})
o:addField(LinkField:new{
o:addField(ServiceLinkField:new{
label = "SCREEN CHECK",
value = ScreenCheckPage:new{
title = "SCREEN CHECK",
@ -37,7 +39,7 @@ function MainMenuPage:new(o)
}
}
})
o:addField(LinkField:new{
o:addField(ServiceLinkField:new{
label = "COLOR CHECK",
value = ColorCheckPage:new{
title = "COLOR CHECK",
@ -47,7 +49,7 @@ function MainMenuPage:new(o)
}
}
})
o:addField(LinkField:new{
o:addField(ServiceLinkField:new{
label = "VERSION INFORMATION",
value = VersionInfoPage:new{
title = "VERSION INFORMATION",
@ -63,4 +65,4 @@ function MainMenuPage:new(o)
return o
end
return MainMenuPage
return MainMenuPage

View File

@ -4,6 +4,8 @@ local ServicePage = require("titlescreen.pages.service.servicepage")
---@class ScreenCheckPage: ServicePage
local ScreenCheckPage = {
__tostring = function () return "ScreenCheckPage" end,
BG_COLOR = {255, 255, 255, 255},
STROKE_COLOR = {255, 0, 0, 255},
SQUARE_BG_COLOR = {128, 128, 128, 255},
@ -99,4 +101,4 @@ function ScreenCheckPage:drawBackground(deltaTime)
gfx.Stroke()
end
return ScreenCheckPage
return ScreenCheckPage

View File

@ -9,6 +9,7 @@ local ServiceField = require("titlescreen.fields.service.servicefield")
---@field footer string[]
---@field content ServiceField[]
local ServicePage = {
__tostring = function () return "ServicePage" end,
SERVICE_DEFAULT_FONT_SIZE = ServiceField.SERVICE_DEFAULT_FONT_SIZE,
SERVICE_DEFAULT_FONT_FACE = ServiceField.SERVICE_DEFAULT_FONT_FACE,
SERVICE_DEFAULT_FONT_COLOR = ServiceField.SERVICE_DEFAULT_FONT_COLOR,
@ -145,4 +146,4 @@ function ServicePage:handleKnobInput(knob, delta)
end
end
return ServicePage
return ServicePage

View File

@ -12,7 +12,9 @@ local function getGameLogValue(prefix, str)
end
---@class VersionInfoPage: ServicePage
local VersionInfoPage = {}
local VersionInfoPage = {
__tostring = function () return "VersionInfoPage" end,
}
---Create a new VersionInfoPage instance
---
@ -62,4 +64,4 @@ function VersionInfoPage:handleButtonInput(button)
end
end
return VersionInfoPage
return VersionInfoPage