Added MainMenuPage

Added LinkField and LinkServiceField
Added state handling functions to Field
Added Page refreshFields to ServicePage
This commit is contained in:
Hersi 2022-04-03 19:51:56 +02:00
parent 297cd161bd
commit 6e42ccafc6
9 changed files with 169 additions and 24 deletions

View File

@ -28,6 +28,12 @@ function Field:new(o)
return o
end
function Field:activate() end
function Field:focus() end
function Field:deactivate() end
function Field:render(deltaTime)
gfx.Save()

View File

@ -0,0 +1,31 @@
require("common.class")
local Field = require("components.pager.field")
---@class LinkField: Field
---@field link Page
local LinkField = {}
---Create a new Field instance
---@param o table
---@return LinkField
function LinkField:new(o)
o = Inherit(self, Field, o)
o.link = o.link or nil
return o
end
function LinkField:handleButtonInput(button)
if button == game.BUTTON_STA then
if self.parent and self.parent.viewHandler then
self.parent.viewHandler:navigate(self.link)
else
game.Log("LinkField can't access PageView instance to navigate", game.LOGGER_ERROR)
end
end
return false
end
return LinkField

View File

@ -2,9 +2,7 @@ local Dim = require("common.dimensions")
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")
local MainMenuPage = require("titlescreen.service.pages.main_menu")
--[[ WIP: REIMPLEMENTATION
local triggerSplashScreen = false
@ -204,25 +202,7 @@ local rootMenu = {
local index = 1
]]
local pages = {
root = {
page = ServicePage:new{title="MAIN MENU"},
fields = {
Field:new(),
Field:new(),
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 currentpage = pages.input_check.page
local currentpage = MainMenuPage:new()
local pageview = PageView:new(currentpage)

View File

@ -14,6 +14,12 @@ function InputButtonField:new(o)
return o
end
function InputButtonField:activate() end
function InputButtonField:focus() end
function InputButtonField:deactivate() end
function InputButtonField:drawValue(deltaTime)
local buttonState = game.GetButton(self.button)
local posX = dim.design.width / 2

View File

@ -14,6 +14,12 @@ function InputKnobField:new(o)
return o
end
function InputKnobField:activate() end
function InputKnobField:focus() end
function InputKnobField:deactivate() end
function InputKnobField:drawValue(deltaTime)
local knobAngle = game.GetKnob(self.knob)
local valuePosX = dim.design.width / 2

View File

@ -0,0 +1,37 @@
require("common.class")
local ServiceField = require("titlescreen.service.servicefield")
---@class LinkServiceField: ServiceField
local LinkServiceField = {}
---Create a new Field instance
---@param o ServiceField
---@return LinkServiceField
function LinkServiceField:new(o)
o = Inherit(self, ServiceField, o)
o.value = o.value or nil
return o
end
function LinkServiceField:drawValue(deltaTime) end
function LinkServiceField:handleButtonInput(button)
if not self.value then
game.Log("LinkServiceField (" .. self.label .. ") does not have a valid link", game.LOGGER_ERROR)
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)
self.parent.viewHandler:navigate(self.value)
else
game.Log("LinkServiceField (" .. self.label .. ") can't access PageView instance to navigate", game.LOGGER_ERROR)
end
end
return false
end
return LinkServiceField

View File

@ -0,0 +1,32 @@
require("common.class")
local ServicePage = require("titlescreen.service.servicepage")
local InputCheckPage = require("titlescreen.service.pages.input_check")
local LinkServiceField = require("titlescreen.service.fields.link_service_field")
---@class MainMenuPage: ServicePage
local MainMenuPage = {}
---Create a new MainMenuPage instance
---
---Inherits from ServicePage
---@param o ServicePage
---@return MainMenuPage
function MainMenuPage:new(o)
o = Inherit(self, ServicePage, o)
o.title = o.title or "MAIN MENU"
o:addField(LinkServiceField:new{
label = "INPUT CHECK",
value = InputCheckPage:new{
title = {o.title, "INPUT CHECK"}
}
})
o:refreshFields()
return o
end
return MainMenuPage

View File

@ -2,10 +2,18 @@ require("common.class")
local dim = require("common.dimensions")
local Field = require("components.pager.field")
---@class ServiceFieldState
ServiceFieldState = {
INACTIVE = 0,
FOCUSED = 1,
ACTIVE = 2
}
---@class ServiceField: Field
---@field label string
---@field value any
---@field footer string|string[]
---@field _state ServiceFieldState
local ServiceField = {
SERVICE_DEFAULT_FONT_SIZE = 24,
SERVICE_DEFAULT_FONT_FACE = "dfmarugoth.ttf",
@ -19,11 +27,26 @@ function ServiceField:new(o)
o.value = o.value or nil
o.footer = o.footer or nil
o._state = ServiceFieldState.INACTIVE
return o
end
function ServiceField:activate()
self._state = ServiceFieldState.ACTIVE
end
function ServiceField:focus()
self._state = ServiceFieldState.FOCUSED
end
function ServiceField:deactivate()
self._state = ServiceFieldState.INACTIVE
end
function ServiceField:drawValue(deltaTime)
gfx.Save()
gfx.FillColor(table.unpack(ServiceField.SERVICE_DEFAULT_FONT_COLOR))
gfx.Text("N/A", dim.design.width / 2, 0)
gfx.Restore()
end
@ -35,11 +58,17 @@ function ServiceField:render(deltaTime)
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)
if self._state == ServiceFieldState.FOCUSED then
gfx.FillColor(255, 0, 0, 255)
else
gfx.FillColor(table.unpack(ServiceField.SERVICE_DEFAULT_FONT_COLOR))
end
gfx.BeginPath()
gfx.Text(self.label, 0, 0)
self:drawValue(deltaTime)
gfx.Restore()

View File

@ -43,6 +43,19 @@ function ServicePage:addField(field)
Page.addField(self, field)
end
function ServicePage:refreshFields()
for index, field in ipairs(self.content) do
field.posX = ServicePage.SERVICE_DEFAULT_MARGIN[1]
field.posY = ServicePage.SERVICE_DEFAULT_MARGIN[2] + (index - 1) * (ServicePage.SERVICE_DEFAULT_FONT_SIZE + ServicePage.SERVICE_DEFAULT_SPACING)
if index == self.selectedIndex then
field:focus()
else
field:deactivate()
end
end
Page.refreshFields(self)
end
function ServicePage:drawBackground(deltaTime)
gfx.BeginPath()
gfx.FillColor(0, 0, 0)
@ -97,6 +110,8 @@ function ServicePage:handleButtonInput(button)
stop_processing = field:handleButtonInput(button)
end
field:deactivate()
-- default behaviour
if not stop_processing then
local direction = 0
@ -116,6 +131,9 @@ function ServicePage:handleButtonInput(button)
self.selectedIndex = (self.selectedIndex - 1 + direction) % #self.content + 1
end
field = self.content[self.selectedIndex]
field:focus()
end
---Handle controller knob input