added InputCheckPage

added ServiceField class and derived classes
removed redundant function from Field
added a replace method to pageview
This commit is contained in:
Hersi 2022-04-03 04:59:43 +02:00
parent e8f7471663
commit 515d3aa54a
8 changed files with 220 additions and 27 deletions

View File

@ -3,7 +3,6 @@
---@field posX number
---@field posY number
local Field = {
drawCustomFooter = nil, ---@type function void()
handleButtonInput = nil, ---@type function void(integer number)
handleKnobInput = nil, ---@type function void(integer knob, number delta)
}

View File

@ -37,6 +37,17 @@ function PageView:navigate(page)
pushStack(self.pageStack, page)
end
---Replace the current pageStack with a new root page
---@param page Page
function PageView:replace(page)
--clear pageStack
while self.pageStack[1] do
self:back()
end
self:navigate(page)
end
---Navigate to the previous page
function PageView:back()
if not self.pageStack[1] then

View File

@ -4,6 +4,7 @@ local Wallpaper = require("components.wallpaper")
local PageView = require("components.pager.pageview")
local ServicePage = require("titlescreen.service.servicepage")
local Field = require("components.pager.field")
local InputCheckPage = require("titlescreen.service.pages.input_check")
--[[ WIP: REIMPLEMENTATION
local triggerSplashScreen = false
@ -212,16 +213,21 @@ local pages = {
Field:new(),
}
},
input_check = {
page = InputCheckPage:new{title={"MAIN MENU", "INPUT CHECK"}}
}
}
for _, field in ipairs(pages.root.fields) do
pages.root.page:addField(field)
end
local pageview = PageView:new(pages.root.page)
local currentpage = pages.input_check.page
local pageview = PageView:new(currentpage)
local function reset()
pageview = PageView:new(pages.root.page)
pageview = PageView:new(currentpage)
end
local function render(deltaTime)

View File

@ -0,0 +1,29 @@
local dim = require("common.dimensions")
local ServiceField = require("titlescreen.service.servicefield")
---@class InputButtonField: ServiceField
---@field button integer
local InputButtonField = {}
function InputButtonField:new(o)
self.__index = self
setmetatable(self, {__index = ServiceField})
o = ServiceField:new(o)
setmetatable(o, self)
o.button = o.button or nil
return o
end
function InputButtonField:drawValue(deltaTime)
local buttonState = game.GetButton(self.button)
local posX = dim.design.width / 2
self.value = buttonState and "ON" or "OFF"
gfx.BeginPath()
gfx.Text(self.value, posX, 0)
end
return InputButtonField

View File

@ -0,0 +1,57 @@
local dim = require("common.dimensions")
local ServiceField = require("titlescreen.service.servicefield")
---@class InputKnobField: ServiceField
---@field knob integer
local InputKnobField = {}
function InputKnobField:new(o)
self.__index = self
setmetatable(self, {__index = ServiceField})
o = ServiceField:new(o)
setmetatable(o, self)
o.knob = o.knob or nil
return o
end
function InputKnobField:drawValue(deltaTime)
local knobAngle = game.GetKnob(self.knob)
local valuePosX = dim.design.width / 2
local sliderPosX = valuePosX + 64
local sliderWidth = 256
local sliderHeight = ServiceField.SERVICE_DEFAULT_FONT_SIZE
local sliderBgColor = {255, 0, 0, 255}
local sliderFrameColor = ServiceField.SERVICE_DEFAULT_FONT_COLOR
local sliderFrameWidth = 1
local maxValue = 1024
self.value = math.floor(knobAngle * maxValue / (2 * math.pi)) % maxValue
--self.value = knobAngle
--draw value
gfx.BeginPath()
gfx.Text(self.value, valuePosX, 0)
--draw slider
gfx.BeginPath()
gfx.Rect(sliderPosX, 0, sliderWidth, sliderHeight)
gfx.FillColor(table.unpack(sliderBgColor))
gfx.StrokeColor(table.unpack(sliderFrameColor))
gfx.StrokeWidth(sliderFrameWidth)
gfx.Fill()
gfx.Stroke()
local sliderIndicatorX = sliderPosX + self.value * sliderWidth / maxValue
local sliderIndicatorWidth = 4
local sliderIndicatorColor = {0, 255, 0, 255}
--draw indicator
gfx.BeginPath()
gfx.Rect(sliderIndicatorX, sliderFrameWidth, sliderIndicatorWidth, sliderHeight - 2 * sliderFrameWidth)
gfx.FillColor(table.unpack(sliderIndicatorColor))
gfx.Fill()
end
return InputKnobField

View File

@ -0,0 +1,34 @@
local dim = require("common.dimensions")
local ServicePage = require("titlescreen.service.servicepage")
local InputButtonField = require("titlescreen.service.fields.input_button_field")
local InputKnobField = require("titlescreen.service.fields.input_knob_field")
---@class InputCheckPage: ServicePage
local InputCheckPage = {}
---Create a new InputCheckPage instance
---
---Inherits from ServicePage
---@return InputCheckPage
function InputCheckPage:new(o)
self.__index = self
setmetatable(self, {__index = ServicePage})
o = ServicePage:new(o)
setmetatable(o, self)
o.content = o.content or {}
o:addField(InputButtonField:new{label="START BUTTON", button=game.BUTTON_STA})
o:addField(InputButtonField:new{label="A BUTTON", button=game.BUTTON_BTA})
o:addField(InputButtonField:new{label="B BUTTON", button=game.BUTTON_BTB})
o:addField(InputButtonField:new{label="C BUTTON", button=game.BUTTON_BTC})
o:addField(InputButtonField:new{label="D BUTTON", button=game.BUTTON_BTD})
o:addField(InputButtonField:new{label="FX L BUTTON", button=game.BUTTON_FXL})
o:addField(InputButtonField:new{label="FX R BUTTON", button=game.BUTTON_FXR})
o:addField(InputKnobField:new{label="ANALOG VOLUME L", knob=0})
o:addField(InputKnobField:new{label="ANALOG VOLUME R", knob=1})
return o
end
return InputCheckPage

View File

@ -0,0 +1,50 @@
local dim = require("common.dimensions")
local Field = require("components.pager.field")
---@class ServiceField: Field
---@field label string
---@field value any
---@field footer string|string[]
local ServiceField = {
SERVICE_DEFAULT_FONT_SIZE = 24,
SERVICE_DEFAULT_FONT_FACE = "dfmarugoth.ttf",
SERVICE_DEFAULT_FONT_COLOR = {255, 255, 255, 255}, --{r, g, b, a}
}
function ServiceField:new(o)
self.__index = self
setmetatable(self, {__index = Field})
o = Field:new(o)
setmetatable(o, self)
o.label = o.label or "<UNDEFINED>"
o.value = o.value or nil
o.footer = o.footer or nil
return o
end
function ServiceField:drawValue(deltaTime)
gfx.Save()
gfx.Text("N/A", dim.design.width / 2, 0)
gfx.Restore()
end
function ServiceField:render(deltaTime)
gfx.Save()
gfx.Translate(self.posX, self.posY)
gfx.FontSize(ServiceField.SERVICE_DEFAULT_FONT_SIZE)
gfx.LoadSkinFont(ServiceField.SERVICE_DEFAULT_FONT_FACE)
gfx.FillColor(table.unpack(ServiceField.SERVICE_DEFAULT_FONT_COLOR))
gfx.TextAlign(gfx.TEXT_ALIGN_LEFT | gfx.TEXT_ALIGN_TOP)
gfx.BeginPath()
gfx.Text(self.label, 0, 0)
self:drawValue(deltaTime)
gfx.Restore()
end
return ServiceField

View File

@ -1,15 +1,16 @@
local dim = require("common.dimensions")
local Page = require("components.pager.page")
local ServiceField = require("titlescreen.service.servicefield")
---@class ServicePage: Page
---@field title string
---@field title string|string[]
---@field selectedIdx integer
---@field footer string[]
---@field content ServiceField[]
local ServicePage = {
SERVICE_DEFAULT_FONT_SIZE = 24,
SERVICE_DEFAULT_FONT_FACE = "dfmarugoth.ttf",
SERVICE_DEFAULT_FONT_COLOR = {255, 255, 255, 255}, --{r, g, b, a}
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,
SERVICE_DEFAULT_MARGIN = {92, 128, 0, 56}, --{left, top, right, bottom}
SERVICE_DEFAULT_SPACING = 4,
SERVICE_DEFAULT_FOOTER = {
@ -52,18 +53,25 @@ function ServicePage:drawBackground(deltaTime)
end
function ServicePage:drawHeader(deltaTime)
local pageTopTitleMargin = ServicePage.SERVICE_DEFAULT_FONT_SIZE
local lineHeight = ServicePage.SERVICE_DEFAULT_FONT_SIZE + ServicePage.SERVICE_DEFAULT_SPACING
gfx.BeginPath()
gfx.FontSize(ServicePage.SERVICE_DEFAULT_FONT_SIZE)
gfx.LoadSkinFont(ServicePage.SERVICE_DEFAULT_FONT_FACE)
gfx.FillColor(table.unpack(ServicePage.SERVICE_DEFAULT_FONT_COLOR))
gfx.TextAlign(gfx.TEXT_ALIGN_CENTER | gfx.TEXT_ALIGN_TOP)
gfx.Text(self.title, dim.design.width / 2, ServicePage.SERVICE_DEFAULT_FONT_SIZE)
if type(self.title) == "table" then
for index, line in ipairs(self.title) do
gfx.Text(line, dim.design.width / 2, pageTopTitleMargin + (index-1) * lineHeight)
end
elseif type(self.title) == "string" then
gfx.Text(self.title, dim.design.width / 2, pageTopTitleMargin)
end
end
function ServicePage:drawFooter(deltaTime)
if self.content[selectedIndex] and self.content[selectedIndex].drawCustomFooter then
self.content[selectedIndex].drawCustomFooter(deltaTime)
else
local footer = self.content[selectedIndex] and self.content[selectedIndex].footer or self.footer
local bottomPageMargin = ServicePage.SERVICE_DEFAULT_MARGIN[4]
local lineHeight = ServicePage.SERVICE_DEFAULT_FONT_SIZE + ServicePage.SERVICE_DEFAULT_SPACING
gfx.BeginPath()
@ -71,15 +79,14 @@ function ServicePage:drawFooter(deltaTime)
gfx.LoadSkinFont(ServicePage.SERVICE_DEFAULT_FONT_FACE)
gfx.FillColor(table.unpack(ServicePage.SERVICE_DEFAULT_FONT_COLOR))
gfx.TextAlign(gfx.TEXT_ALIGN_CENTER | gfx.TEXT_ALIGN_BOTTOM)
if type(self.footer) == "table" then
for index, line in ipairs(self.footer) do
local yFooterBase = dim.design.height - bottomPageMargin - #self.footer * lineHeight
gfx.Text(line, 1080 / 2, yFooterBase + (index-1) * lineHeight)
if type(footer) == "table" then
local yFooterBase = dim.design.height - bottomPageMargin - #footer * lineHeight
for index, line in ipairs(footer) do
gfx.Text(line, dim.design.width / 2, yFooterBase + (index-1) * lineHeight)
end
elseif type(self.footer) == "string" then
elseif type(footer) == "string" then
local yFooterBase = dim.design.height - bottomPageMargin
gfx.Text(self.footer, 1080 / 2, yFooterBase)
end
gfx.Text(footer, dim.design.width / 2, yFooterBase)
end
end