2019-09-01 11:55:41 +02:00
using System ;
using System.Collections.Generic ;
2020-05-14 15:42:40 +02:00
using System.Linq ;
2019-09-01 11:55:41 +02:00
using Microsoft.Xna.Framework ;
using Microsoft.Xna.Framework.Graphics ;
using MLEM.Font ;
using MLEM.Startup ;
using MLEM.Textures ;
using MLEM.Ui ;
using MLEM.Ui.Elements ;
using MLEM.Ui.Style ;
namespace Demos {
public class GameImpl : MlemGame {
2020-05-14 15:42:40 +02:00
private static readonly Dictionary < string , ( string , Func < MlemGame , Demo > ) > Demos = new Dictionary < string , ( string , Func < MlemGame , Demo > ) > ( ) ;
2019-09-01 11:55:41 +02:00
private Demo activeDemo ;
2020-02-20 17:57:14 +01:00
private double fpsTime ;
private int lastFps ;
private int fpsCounter ;
2021-12-12 12:32:09 +01:00
private UiMetrics cumulativeMetrics ;
private TimeSpan secondCounter ;
2019-09-01 11:55:41 +02:00
static GameImpl ( ) {
2022-06-15 11:38:11 +02:00
GameImpl . Demos . Add ( "Ui" , ( "An in-depth demonstration of the MLEM.Ui package and its abilities" , game = > new UiDemo ( game ) ) ) ;
2022-06-17 21:47:40 +02:00
GameImpl . Demos . Add ( "Text Formatting" , ( "A demonstration of MLEM's text formatting system" , game = > new TextFormattingDemo ( game ) ) ) ;
2022-06-15 11:38:11 +02:00
GameImpl . Demos . Add ( "Easings" , ( "An example of MLEM's Easings class, an adaptation of easings.net" , game = > new EasingsDemo ( game ) ) ) ;
GameImpl . Demos . Add ( "Pathfinding" , ( "An example of MLEM's A* pathfinding implementation in 2D" , game = > new PathfindingDemo ( game ) ) ) ;
GameImpl . Demos . Add ( "Animation and Texture Atlas" , ( "An example of UniformTextureAtlases, SpriteAnimations and SpriteAnimationGroups" , game = > new AnimationDemo ( game ) ) ) ;
GameImpl . Demos . Add ( "Auto Tiling" , ( "A demonstration of the AutoTiling class that MLEM provides" , game = > new AutoTilingDemo ( game ) ) ) ;
2019-09-01 11:55:41 +02:00
}
public GameImpl ( ) {
this . IsMouseVisible = true ;
2021-12-12 12:32:09 +01:00
// print out ui metrics every second
this . OnDraw + = ( g , time ) = > {
this . cumulativeMetrics + = this . UiSystem . Metrics ;
this . secondCounter + = time . ElapsedGameTime ;
if ( this . secondCounter . TotalSeconds > = 1 ) {
this . secondCounter - = TimeSpan . FromSeconds ( 1 ) ;
Console . WriteLine ( $"Metrics/s: {this.cumulativeMetrics}" ) ;
this . cumulativeMetrics = new UiMetrics ( ) ;
}
} ;
2019-09-01 11:55:41 +02:00
}
protected override void LoadContent ( ) {
base . LoadContent ( ) ;
this . UiSystem . AutoScaleReferenceSize = new Point ( 1280 , 720 ) ;
this . UiSystem . AutoScaleWithScreen = true ;
this . UiSystem . GlobalScale = 5 ;
2020-05-14 15:42:40 +02:00
var ui = new Group ( Anchor . TopLeft , Vector2 . One , false ) ;
this . UiSystem . Add ( "DemoUi" , ui ) ;
var selection = ui . AddChild ( new Panel ( Anchor . Center , new Vector2 ( 100 , 80 ) , Vector2 . Zero , true ) ) ;
2019-09-01 11:55:41 +02:00
2020-05-14 15:42:40 +02:00
var backButton = ui . AddChild ( new Button ( Anchor . TopRight , new Vector2 ( 30 , 10 ) , "Back" ) {
2019-09-01 11:55:41 +02:00
OnPressed = e = > {
this . activeDemo . Clear ( ) ;
this . activeDemo = null ;
2020-05-14 15:42:40 +02:00
selection . IsHidden = false ;
e . IsHidden = true ;
selection . Root . SelectElement ( selection . GetChildren ( ) . First ( c = > c . CanBeSelected ) ) ;
} ,
IsHidden = true
} ) ;
2019-09-01 11:55:41 +02:00
2022-03-07 12:00:33 +01:00
selection . AddChild ( new Paragraph ( Anchor . AutoLeft , 1 , "Select the demo you want to see below using your mouse, touch input, your keyboard or a controller. Check the demos' <l https://github.com/Ellpeck/MLEM/tree/main/Demos>source code</l> for more in-depth explanations of their functionality or the <l https://mlem.ellpeck.de/>website</l> for tutorials and API documentation." ) ) ;
2019-09-01 11:55:41 +02:00
selection . AddChild ( new VerticalSpace ( 5 ) ) ;
2022-06-15 11:38:11 +02:00
foreach ( var demo in GameImpl . Demos ) {
2020-05-14 15:42:40 +02:00
selection . AddChild ( new Button ( Anchor . AutoCenter , new Vector2 ( 1 , 10 ) , demo . Key , demo . Value . Item1 ) {
2019-09-01 11:55:41 +02:00
OnPressed = e = > {
2020-05-14 15:42:40 +02:00
selection . IsHidden = true ;
backButton . IsHidden = false ;
backButton . Root . SelectElement ( backButton ) ;
2019-09-01 11:55:41 +02:00
2020-05-14 15:42:40 +02:00
this . activeDemo = demo . Value . Item2 . Invoke ( this ) ;
2019-09-01 11:55:41 +02:00
this . activeDemo . LoadContent ( ) ;
} ,
2024-04-10 17:58:01 +02:00
PositionOffset = new Vector2 ( 0 , 1 ) ,
Tooltip = { DisplayInAutoNavMode = true }
2019-09-01 11:55:41 +02:00
} ) ;
}
2020-05-14 15:42:40 +02:00
ui . AddChild ( new Paragraph ( Anchor . TopLeft , 1 , p = > "FPS: " + this . lastFps ) ) ;
2019-09-01 11:55:41 +02:00
}
2021-12-13 00:39:36 +01:00
protected override UiStyle InitializeDefaultUiStyle ( SpriteBatch batch ) {
2022-06-15 11:38:11 +02:00
var tex = MlemGame . LoadContent < Texture2D > ( "Textures/Test" ) ;
2021-12-13 00:39:36 +01:00
return new UntexturedStyle ( this . SpriteBatch ) {
2022-06-15 11:38:11 +02:00
Font = new GenericSpriteFont ( MlemGame . LoadContent < SpriteFont > ( "Fonts/TestFont" ) ) ,
2021-12-13 00:39:36 +01:00
TextScale = 0.1F ,
PanelTexture = new NinePatch ( new TextureRegion ( tex , 0 , 8 , 24 , 24 ) , 8 ) ,
ButtonTexture = new NinePatch ( new TextureRegion ( tex , 24 , 8 , 16 , 16 ) , 4 ) ,
2022-11-03 18:16:59 +01:00
ScrollBarBackground = new NinePatch ( new TextureRegion ( tex , 12 , 0 , 4 , 8 ) , 0.25F , paddingPercent : true ) ,
2022-12-18 13:01:19 +01:00
ScrollBarScrollerTexture = new NinePatch ( new TextureRegion ( tex , 8 , 0 , 4 , 8 ) , 0.25F , paddingPercent : true )
2021-12-13 00:39:36 +01:00
} ;
}
2019-09-01 11:55:41 +02:00
protected override void DoDraw ( GameTime gameTime ) {
if ( this . activeDemo ! = null ) {
this . activeDemo . DoDraw ( gameTime ) ;
} else {
this . GraphicsDevice . Clear ( Color . CornflowerBlue ) ;
}
base . DoDraw ( gameTime ) ;
2020-02-20 17:57:14 +01:00
this . fpsCounter + + ;
this . fpsTime + = gameTime . ElapsedGameTime . TotalSeconds ;
if ( this . fpsTime > = 1 ) {
this . fpsTime - = 1 ;
this . lastFps = this . fpsCounter ;
this . fpsCounter = 0 ;
}
2019-09-01 11:55:41 +02:00
}
2019-12-05 17:35:24 +01:00
protected override void DoUpdate ( GameTime gameTime ) {
base . DoUpdate ( gameTime ) ;
2019-09-01 11:55:41 +02:00
if ( this . activeDemo ! = null )
this . activeDemo . Update ( gameTime ) ;
}
}
2022-06-17 18:23:47 +02:00
}