Title screen menu component for the Transposer game. Draws a pixel-art play button with hover outline, handles mute toggling, and fades into the gameplay scene set in GameplayScene, reusing the last gameplay frame as the fade-in background.
namespace Sandbox.Transposer;
[Title( "Transposer Platform Menu" )]
public sealed class PlatformMenu : Component
{
[Property] public SceneFile GameplayScene { get; set; }
private readonly TitleScene _title = new();
private readonly Game.Overlay _overlay = new();
private ScoreText _playLabel;
private PixelRect _playBounds;
private LastGameplayFrame _lastGameFrame;
private bool _launching;
protected override void OnEnabled()
{
Mouse.Visibility = MouseVisibility.Visible;
Mouse.CursorType = "arrow";
AudioManager.MusicPitch = 1f;
AudioManager.FadeInMusic( 1f );
}
protected override void OnStart()
{
SpriteManager.LoadAll();
var screen = PixelScreen.Instance;
_playLabel = new ScoreText( "play", _title, 0, 0, "font" );
_playLabel.PixelX = (screen.PixelWidth - _playLabel.Hitbox.Width) / 2;
_playLabel.PixelY = 19;
_playBounds = new PixelRect( _playLabel.PixelX - 10, _playLabel.PixelY - 5,
_playLabel.Hitbox.Width + 20, _playLabel.Hitbox.Height + 10 );
_title.Activate( _playBounds.YMax + 7 );
_lastGameFrame = LastGameplayFrame.Get( Scene );
if ( _lastGameFrame.Pixels?.Length == screen.PixelWidth * screen.PixelHeight )
{
screen.TransitionRect = _lastGameFrame.Bounds;
screen.TransitionBackground = _lastGameFrame.Background;
_title.SetFadeToBlackCallback( RevealMenu );
_title.FadeToBlack( TransposerScene.SceneFadeTime );
}
else
{
RevealMenu();
}
}
protected override void OnUpdate()
{
Mouse.Visibility = MouseVisibility.Visible;
bool overlayOpen = _overlay.IsOpen;
if ( !overlayOpen )
{
Mouse.CursorType = "arrow";
if ( Input.Pressed( "Mute" ) )
AudioManager.ToggleMute();
}
AudioManager.Tick( Time.Delta );
var screen = PixelScreen.Instance;
if ( _lastGameFrame.Pixels is { } outgoing )
{
outgoing.CopyTo( screen.GetPixelsWritable(), 0 );
screen.MarkDirty();
_title.HandleFading( Time.Delta );
return;
}
_title.UpdateScene( Time.Delta );
bool hovered = !overlayOpen && !Input.UsingController && _playBounds.Contains( screen.GetMouseScreenPosition() );
DrawPlayButton( screen, hovered );
if ( AudioManager.IsMuted )
_title.DrawMuteIcon();
if ( !_launching && !overlayOpen && (Input.Pressed( "Start" ) || (hovered && Input.Pressed( "MenuClick" ))) )
{
_launching = true;
_title.SetFadeToBlackCallback( Play );
_title.FadeToBlack( TransposerScene.SceneFadeTime );
}
_title.HandleFading( Time.Delta );
}
private void RevealMenu()
{
_lastGameFrame.Release();
PixelScreen.Instance.TransitionRect = null;
PixelScreen.Instance.TransitionBackground = null;
_launching = false;
_title.FadeIn( TransposerScene.SceneFadeTime );
}
private void DrawPlayButton( PixelScreen screen, bool hovered )
{
// Inset the visible frame inside the larger click target.
int left = _playBounds.XMin + 4;
int right = _playBounds.XMax - 5;
int bottom = _playBounds.YMin + 2;
int top = _playBounds.YMax - 3;
Color32 edge = new( 0, 0, 0, (byte)(hovered ? 160 : 76) );
for ( int x = left + 1; x < right; x++ )
{
screen.AddPixel( x, bottom, edge );
screen.AddPixel( x, top, edge );
}
for ( int y = bottom + 1; y < top; y++ )
{
screen.AddPixel( left, y, edge );
screen.AddPixel( right, y, edge );
}
_playLabel.Draw();
}
private void Play()
{
var load = new SceneLoadOptions { ShowLoadingScreen = false };
if ( !load.SetScene( GameplayScene ) || !Game.ChangeScene( load ) )
{
Log.Warning( "Could not open the Transposer gameplay scene." );
RevealMenu();
return;
}
var game = Game.ActiveScene.GetAllComponents<TransposerGame>().Single();
game.InitialScene = SceneType.Game;
game.PixelScreen.ApplyPixelFade( 1f );
}
protected override void OnDisabled()
{
AudioManager.StopMusic();
Mouse.CursorType = null;
Mouse.Visibility = MouseVisibility.Auto;
}
}