From ef3fcb2e9cba9a1a4cf055344005ce08656604a2 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Mon, 13 Dec 2021 00:39:36 +0100 Subject: [PATCH] Improved MlemGame class --- CHANGELOG.md | 5 +++++ Demos/GameImpl.cs | 22 ++++++++++--------- MLEM.Startup/CoroutineEvents.cs | 12 +++++++++-- MLEM.Startup/MlemGame.cs | 38 +++++++++++++++++++++++++++------ 4 files changed, 59 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ec005e..af8a4a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -72,6 +72,11 @@ Improvements Removals - Marked features related to Lidgren.Network as obsolete +### MLEM.Startup +Additions +- Added virtual InitializeDefaultUiStyle to MlemGame +- Added PreDraw and PreUpdate events and coroutine events + ## 5.1.0 ### MLEM Additions diff --git a/Demos/GameImpl.cs b/Demos/GameImpl.cs index 52fd436..1f5417b 100644 --- a/Demos/GameImpl.cs +++ b/Demos/GameImpl.cs @@ -49,16 +49,6 @@ namespace Demos { this.GraphicsDeviceManager.PreferredBackBufferHeight = 720; this.GraphicsDeviceManager.ApplyChanges(); base.LoadContent(); - - var tex = LoadContent("Textures/Test"); - this.UiSystem.Style = new UntexturedStyle(this.SpriteBatch) { - Font = new GenericSpriteFont(LoadContent("Fonts/TestFont")), - 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), - ScrollBarBackground = new NinePatch(new TextureRegion(tex, 12, 0, 4, 8), 1, 1, 2, 2), - ScrollBarScrollerTexture = new NinePatch(new TextureRegion(tex, 8, 0, 4, 8), 1, 1, 2, 2) - }; this.UiSystem.AutoScaleReferenceSize = new Point(1280, 720); this.UiSystem.AutoScaleWithScreen = true; this.UiSystem.GlobalScale = 5; @@ -97,6 +87,18 @@ namespace Demos { ui.AddChild(new Paragraph(Anchor.TopLeft, 1, p => "FPS: " + this.lastFps)); } + protected override UiStyle InitializeDefaultUiStyle(SpriteBatch batch) { + var tex = LoadContent("Textures/Test"); + return new UntexturedStyle(this.SpriteBatch) { + Font = new GenericSpriteFont(LoadContent("Fonts/TestFont")), + 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), + ScrollBarBackground = new NinePatch(new TextureRegion(tex, 12, 0, 4, 8), 1, 1, 2, 2), + ScrollBarScrollerTexture = new NinePatch(new TextureRegion(tex, 8, 0, 4, 8), 1, 1, 2, 2) + }; + } + protected override void DoDraw(GameTime gameTime) { if (this.activeDemo != null) { this.activeDemo.DoDraw(gameTime); diff --git a/MLEM.Startup/CoroutineEvents.cs b/MLEM.Startup/CoroutineEvents.cs index 64788e1..cc75412 100644 --- a/MLEM.Startup/CoroutineEvents.cs +++ b/MLEM.Startup/CoroutineEvents.cs @@ -7,11 +7,19 @@ namespace MLEM.Startup { public static class CoroutineEvents { /// - /// This event is fired in + /// This event is fired in , before is called. + /// + public static readonly Event PreUpdate = new Event(); + /// + /// This event is fired in , after is called. /// public static readonly Event Update = new Event(); /// - /// This event is fired in + /// This event is fired in , before is called. + /// + public static readonly Event PreDraw = new Event(); + /// + /// This event is fired in , after is called. /// public static readonly Event Draw = new Event(); diff --git a/MLEM.Startup/MlemGame.cs b/MLEM.Startup/MlemGame.cs index 71848cf..ed83878 100644 --- a/MLEM.Startup/MlemGame.cs +++ b/MLEM.Startup/MlemGame.cs @@ -38,15 +38,23 @@ namespace MLEM.Startup { public UiSystem UiSystem { get; protected set; } /// - /// An event that is invoked in + /// An event that is invoked in . /// public event GenericCallback OnLoadContent; /// - /// An event that is invoked in + /// An event that is invoked in , before is called. + /// + public event TimeCallback PreUpdate; + /// + /// An event that is invoked in , after is called. /// public event TimeCallback OnUpdate; /// - /// An event that is invoked in + /// An event that is invoked in , before is called. + /// + public event TimeCallback PreDraw; + /// + /// An event that is invoked in , after is called. /// public event TimeCallback OnDraw; @@ -74,7 +82,7 @@ namespace MLEM.Startup { this.SpriteBatch = new SpriteBatch(this.GraphicsDevice); this.InputHandler = new InputHandler(this); this.Components.Add(this.InputHandler); - this.UiSystem = new UiSystem(this, new UntexturedStyle(this.SpriteBatch), this.InputHandler); + this.UiSystem = new UiSystem(this, this.InitializeDefaultUiStyle(this.SpriteBatch), this.InputHandler); this.Components.Add(this.UiSystem); this.OnLoadContent?.Invoke(this); } @@ -82,11 +90,15 @@ namespace MLEM.Startup { /// /// Called when the game should update. /// Updates the instances attached to this game. - /// Override this to update your game. + /// Override to update your game. /// /// The elapsed time since the last call to . protected sealed override void Update(GameTime gameTime) { + this.PreUpdate?.Invoke(this, gameTime); + CoroutineHandler.RaiseEvent(CoroutineEvents.PreUpdate); + this.DoUpdate(gameTime); + this.OnUpdate?.Invoke(this, gameTime); CoroutineHandler.Tick(gameTime.ElapsedGameTime.TotalSeconds); CoroutineHandler.RaiseEvent(CoroutineEvents.Update); @@ -95,13 +107,17 @@ namespace MLEM.Startup { /// /// Called when the game should draw a frame. /// Draws the instances attached to this game. - /// Override this to render your game. + /// Override to render your game. /// /// A instance containing the elapsed time since the last call to and the total time elapsed since the game started. protected sealed override void Draw(GameTime gameTime) { + this.PreDraw?.Invoke(this, gameTime); + CoroutineHandler.RaiseEvent(CoroutineEvents.PreDraw); + this.UiSystem.DrawEarly(gameTime, this.SpriteBatch); this.DoDraw(gameTime); this.UiSystem.Draw(gameTime, this.SpriteBatch); + this.OnDraw?.Invoke(this, gameTime); CoroutineHandler.RaiseEvent(CoroutineEvents.Draw); } @@ -124,6 +140,16 @@ namespace MLEM.Startup { base.Update(gameTime); } + /// + /// This method is called in when the is initialized. + /// Override this method to easily modify or create a new default for this game's . + /// + /// The sprite batch to use + /// The to use for this game's . + protected virtual UiStyle InitializeDefaultUiStyle(SpriteBatch batch) { + return new UntexturedStyle(batch); + } + /// /// Static helper method for . /// This just invokes the game instance's load method.