ExperimentalGear/scripts/titlescreen/splash.lua

150 lines
4.3 KiB
Lua
Raw Normal View History

local Util = require("common.util")
2024-02-02 02:50:48 +01:00
local Display = require("scripts.graphics.display")
2022-03-03 03:55:46 +01:00
require "common.globals"
require "common.class"
2024-02-02 02:27:05 +01:00
local PageRegistry = require "api.page.pageregistry"
local Page = require "api.page.page"
local KShootManiaPage = require "titlescreen.splash.kshootmaniapage"
local USCPage = require "titlescreen.splash.uscpage"
local TeamExceedPage = require "titlescreen.splash.teamexceedpage"
local CreditsPage = require "titlescreen.splash.creditspage"
---@class SplashPage : Page
---@field pages Page[]
---@field currentPage integer
---@field _isTransitioning boolean # actively fading between pages
---@field _isFadeOut boolean # fading out between pages
local SplashPage = {
__name = "SplashScreen",
BACKGROUND_COLOR = {255, 255, 255},
FADE_DURATION = 0.25
}
---Create a new SplashScreen instance
---@param params? SplashPage
---@return SplashPage
function SplashPage.new(params)
local self = CreateInstance(SplashPage, params, Page)
self.currentPage = 0 -- start on nil page
self.content = {
2024-02-02 02:27:05 +01:00
PageRegistry:getOrCreatePage(KShootManiaPage),
PageRegistry:getOrCreatePage(USCPage),
PageRegistry:getOrCreatePage(TeamExceedPage),
PageRegistry:getOrCreatePage(CreditsPage),
}
self._isTransitioning = true -- immediately transition to first page
self._isFadeOut = false
-- set callbacks
for index, page in ipairs(self.content) do
if index < #self.content then
page.onInvalidation = function (page_inst)
self._isTransitioning = true
end
else -- last index
page.onInvalidation = function (page_inst)
self:onInvalidation()
end
end
2022-03-03 03:55:46 +01:00
end
return self
2022-03-03 03:55:46 +01:00
end
function SplashPage:init()
self.currentPage = 0
self._isTransitioning = true
for _, page in ipairs(self.content) do
page:init()
end
Page.init(self)
end
2021-12-19 23:16:19 +01:00
function SplashPage:handleButtonInput(button)
if button == game.BUTTON_STA then
game.StopSample(KShootManiaPage.SAMPLE_PATH) -- hack to stop splash screen sample from playing when skipping
self:onInvalidation()
2021-12-20 15:38:54 +01:00
end
2022-03-03 03:55:46 +01:00
end
---Fade between pages
function SplashPage:fadeTransition(deltaTime)
local fadeDuration = self.FADE_DURATION
local fadeColor = self.BACKGROUND_COLOR
2022-03-12 15:18:24 +01:00
-- reset variables on first call
if not self["__fadeStarted"] then
self.__fadeTimer = 0.0
self.__fadeStarted = true
2022-03-03 03:55:46 +01:00
end
local fadeAlpha = 0.0
local halfDuration = fadeDuration / 2
if self.__fadeTimer < halfDuration then
fadeAlpha = math.ceil(Util.lerp(self.__fadeTimer, 0, 0, halfDuration, 255)) -- fade out
self._isFadeOut = true
2022-06-22 03:03:48 +02:00
elseif self.__fadeTimer < fadeDuration then
fadeAlpha = math.ceil(Util.lerp(self.__fadeTimer, halfDuration, 255, fadeDuration, 0)) -- fade in
if self._isFadeOut then -- just switched to fading in, swap to new page
self.currentPage = self.currentPage + 1
end
self._isFadeOut = false
else
self.__fadeStarted = false -- fade done, reset variable
self._isTransitioning = false
self._isFadeOut = false
2022-03-03 03:55:46 +01:00
end
local fillColor = {table.unpack(fadeColor, 1, 3)} -- copy color table
table.insert(fillColor, fadeAlpha) -- add alpha
2022-04-03 03:02:16 +02:00
gfx.BeginPath()
gfx.FillColor(table.unpack(fillColor))
2024-02-02 02:50:48 +01:00
gfx.Rect(0, 0, Display.design.width, Display.design.height)
2022-04-03 03:02:16 +02:00
gfx.Fill()
self.__fadeTimer = self.__fadeTimer + deltaTime
2022-04-03 03:02:16 +02:00
end
function SplashPage:drawBackground(deltaTime)
2022-03-12 15:18:24 +01:00
gfx.BeginPath()
gfx.FillColor(table.unpack(self.BACKGROUND_COLOR))
2024-02-02 02:50:48 +01:00
gfx.Rect(0, 0, Display.design.width, Display.design.height)
2022-03-03 03:55:46 +01:00
gfx.Fill()
end
2022-03-03 03:55:46 +01:00
function SplashPage:drawContent(deltaTime)
if self.content[self.currentPage] then
self.content[self.currentPage]:render(deltaTime)
end
2021-12-19 23:16:19 +01:00
end
function SplashPage:drawForeground(deltaTime)
local textFillColor = {0, 0, 0}
if self.currentPage <= 1 then
textFillColor = {255, 255, 255}
end
if self._isTransitioning then
self:fadeTransition(deltaTime)
2022-03-12 15:18:24 +01:00
end
gfx.LoadSkinFont("segoeui.ttf")
gfx.FillColor(table.unpack(textFillColor))
gfx.TextAlign(gfx.TEXT_ALIGN_LEFT + gfx.TEXT_ALIGN_BOTTOM)
gfx.FontSize(28)
2024-02-02 02:50:48 +01:00
gfx.Text("Press START to skip...", 10, Display.design.height - 10)
2022-03-12 15:18:24 +01:00
end
return SplashPage