1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-05-10 11:28:44 +02:00

Improved MlemGame class

This commit is contained in:
Ell 2021-12-13 00:39:36 +01:00
parent 60dfbb1ec5
commit ef3fcb2e9c
4 changed files with 59 additions and 18 deletions

View file

@ -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

View file

@ -49,16 +49,6 @@ namespace Demos {
this.GraphicsDeviceManager.PreferredBackBufferHeight = 720;
this.GraphicsDeviceManager.ApplyChanges();
base.LoadContent();
var tex = LoadContent<Texture2D>("Textures/Test");
this.UiSystem.Style = new UntexturedStyle(this.SpriteBatch) {
Font = new GenericSpriteFont(LoadContent<SpriteFont>("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<Texture2D>("Textures/Test");
return new UntexturedStyle(this.SpriteBatch) {
Font = new GenericSpriteFont(LoadContent<SpriteFont>("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);

View file

@ -7,11 +7,19 @@ namespace MLEM.Startup {
public static class CoroutineEvents {
/// <summary>
/// This event is fired in <see cref="MlemGame.Update"/>
/// This event is fired in <see cref="MlemGame.Draw"/>, before <see cref="MlemGame.DoDraw"/> is called.
/// </summary>
public static readonly Event PreUpdate = new Event();
/// <summary>
/// This event is fired in <see cref="MlemGame.Update"/>, after <see cref="MlemGame.DoUpdate"/> is called.
/// </summary>
public static readonly Event Update = new Event();
/// <summary>
/// This event is fired in <see cref="MlemGame.Draw"/>
/// This event is fired in <see cref="MlemGame.Draw"/>, before <see cref="MlemGame.DoDraw"/> is called.
/// </summary>
public static readonly Event PreDraw = new Event();
/// <summary>
/// This event is fired in <see cref="MlemGame.Draw"/>, after <see cref="MlemGame.DoDraw"/> is called.
/// </summary>
public static readonly Event Draw = new Event();

View file

@ -38,15 +38,23 @@ namespace MLEM.Startup {
public UiSystem UiSystem { get; protected set; }
/// <summary>
/// An event that is invoked in <see cref="LoadContent"/>
/// An event that is invoked in <see cref="LoadContent"/>.
/// </summary>
public event GenericCallback OnLoadContent;
/// <summary>
/// An event that is invoked in <see cref="Update"/>
/// An event that is invoked in <see cref="Update"/>, before <see cref="DoUpdate"/> is called.
/// </summary>
public event TimeCallback PreUpdate;
/// <summary>
/// An event that is invoked in <see cref="Update"/>, after <see cref="DoUpdate"/> is called.
/// </summary>
public event TimeCallback OnUpdate;
/// <summary>
/// An event that is invoked in <see cref="Draw"/>
/// An event that is invoked in <see cref="Draw"/>, before <see cref="DoDraw"/> is called.
/// </summary>
public event TimeCallback PreDraw;
/// <summary>
/// An event that is invoked in <see cref="Draw"/>, after <see cref="DoDraw"/> is called.
/// </summary>
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 {
/// <summary>
/// Called when the game should update.
/// Updates the <see cref="T:Microsoft.Xna.Framework.GameComponent" /> instances attached to this game.
/// Override this to update your game.
/// Override <see cref="DoUpdate"/> to update your game.
/// </summary>
/// <param name="gameTime">The elapsed time since the last call to <see cref="M:Microsoft.Xna.Framework.Game.Update(Microsoft.Xna.Framework.GameTime)" />.</param>
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 {
/// <summary>
/// Called when the game should draw a frame.
/// Draws the <see cref="T:Microsoft.Xna.Framework.DrawableGameComponent" /> instances attached to this game.
/// Override this to render your game.
/// Override <see cref="DoDraw"/> to render your game.
/// </summary>
/// <param name="gameTime">A <see cref="T:Microsoft.Xna.Framework.GameTime" /> instance containing the elapsed time since the last call to <see cref="M:Microsoft.Xna.Framework.Game.Draw(Microsoft.Xna.Framework.GameTime)" /> and the total time elapsed since the game started.</param>
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);
}
/// <summary>
/// This method is called in <see cref="LoadContent"/> when the <see cref="UiSystem"/> is initialized.
/// Override this method to easily modify or create a new default <see cref="UiStyle"/> for this game's <see cref="UiSystem"/>.
/// </summary>
/// <param name="batch">The sprite batch to use</param>
/// <returns>The <see cref="UiStyle"/> to use for this game's <see cref="UiSystem"/>.</returns>
protected virtual UiStyle InitializeDefaultUiStyle(SpriteBatch batch) {
return new UntexturedStyle(batch);
}
/// <summary>
/// Static helper method for <see cref="ContentManager.Load{T}"/>.
/// This just invokes the game instance's load method.