1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-12-25 01:39:23 +01:00

cleaned up the demos a bit to allow better usage on mobile

This commit is contained in:
Ellpeck 2020-07-17 14:56:22 +02:00
parent ec2416719e
commit e427305490
6 changed files with 48 additions and 25 deletions

View file

@ -6,12 +6,16 @@ using MLEM.Animations;
using MLEM.Misc;
using MLEM.Startup;
using MLEM.Textures;
using MLEM.Ui;
using MLEM.Ui.Elements;
namespace Demos {
public class AnimationDemo : Demo {
private SpriteAnimationGroup group;
private Direction2 facing = Direction2.Down;
private Group buttons;
private bool stop;
public AnimationDemo(MlemGame game) : base(game) {
}
@ -48,26 +52,38 @@ namespace Demos {
// you can also add a priority to an animation in the group (10 in this case, which is higher than the default of 0)
// if two animations' playing conditions are both true, then the one with the higher priority will be picked to play
// in this instance, a standing "animation" is displayed when we're facing down and also holding the space key
this.group.Add(new SpriteAnimation(1F, atlas[0, 0]) {Name = "DownStanding"}, () => this.facing == Direction2.Down && this.InputHandler.IsKeyDown(Keys.Space), 10);
this.group.Add(new SpriteAnimation(1F, atlas[0, 0]) {Name = "DownStanding"}, () => this.facing == Direction2.Down && this.stop, 10);
this.group.Add(new SpriteAnimation(1F, atlas[1, 0]) {Name = "UpStanding"}, () => this.facing == Direction2.Up && this.stop, 10);
this.group.Add(new SpriteAnimation(1F, atlas[2, 0]) {Name = "LeftStanding"}, () => this.facing == Direction2.Left && this.stop, 10);
this.group.Add(new SpriteAnimation(1F, atlas[3, 0]) {Name = "RightStanding"}, () => this.facing == Direction2.Right && this.stop, 10);
// you can also add a callback to see when the animation used changes
this.group.OnAnimationChanged += (anim, newAnim) => {
Console.WriteLine("Changing anim from " + (anim?.Name ?? "None") + " to " + (newAnim?.Name ?? "None"));
};
// some ui elements to interact with the character
this.buttons = new Group(Anchor.TopCenter, Vector2.One) {SetWidthBasedOnChildren = true};
this.UiRoot.AddChild(this.buttons);
this.buttons.AddChild(new Button(Anchor.AutoInlineIgnoreOverflow, new Vector2(10), "^") {
OnPressed = e => this.facing = Direction2.Up
});
this.buttons.AddChild(new Button(Anchor.AutoInlineIgnoreOverflow, new Vector2(10), "v") {
OnPressed = e => this.facing = Direction2.Down
});
this.buttons.AddChild(new Button(Anchor.AutoInlineIgnoreOverflow, new Vector2(10), "<") {
OnPressed = e => this.facing = Direction2.Left
});
this.buttons.AddChild(new Button(Anchor.AutoInlineIgnoreOverflow, new Vector2(10), ">") {
OnPressed = e => this.facing = Direction2.Right
});
this.buttons.AddChild(new Button(Anchor.AutoCenter, new Vector2(30, 10), "Stand/Walk") {
OnPressed = e => this.stop = !this.stop
});
}
public override void Update(GameTime gameTime) {
base.Update(gameTime);
if (this.InputHandler.IsKeyDown(Keys.Down))
this.facing = Direction2.Down;
else if (this.InputHandler.IsKeyDown(Keys.Up))
this.facing = Direction2.Up;
else if (this.InputHandler.IsKeyDown(Keys.Left))
this.facing = Direction2.Left;
else if (this.InputHandler.IsKeyDown(Keys.Right))
this.facing = Direction2.Right;
// update the animation group
// if not using a group, just update the animation itself here
this.group.Update(gameTime);
@ -85,5 +101,9 @@ namespace Demos {
base.DoDraw(gameTime);
}
public override void Clear() {
this.UiRoot.RemoveChild(this.buttons);
}
}
}

View file

@ -3,6 +3,7 @@ using Microsoft.Xna.Framework.Graphics;
using MLEM.Input;
using MLEM.Startup;
using MLEM.Ui;
using MLEM.Ui.Elements;
namespace Demos {
public class Demo {
@ -12,6 +13,7 @@ namespace Demos {
public GraphicsDevice GraphicsDevice => this.Game.GraphicsDevice;
public InputHandler InputHandler => this.Game.InputHandler;
public UiSystem UiSystem => this.Game.UiSystem;
public Element UiRoot => this.UiSystem.Get("DemoUi").Element;
public Demo(MlemGame game) {
this.Game = game;

View file

@ -29,7 +29,7 @@ namespace Demos {
OnPressed = e => this.current = (this.current + 1) % Easings.Length
});
this.group.AddChild(new Paragraph(Anchor.AutoCenter, 1, p => EasingFields[this.current].Name, true));
this.UiSystem.Get("DemoUi").Element.AddChild(this.group);
this.UiRoot.AddChild(this.group);
}
public override void Clear() {

View file

@ -21,10 +21,10 @@ namespace Demos {
static GameImpl() {
Demos.Add("Ui", ("An in-depth demonstration of the MLEM.Ui package and its abilities", game => new UiDemo(game)));
Demos.Add("Easings", ("An example of MLEM's Easings class, an adaptation of easings.net", game => new EasingsDemo(game)));
Demos.Add("Pathfinding", ("An example of MLEM's A* pathfinding implementation in 2D", game => new PathfindingDemo(game)));
Demos.Add("Animation and Texture Atlas", ("An example of UniformTextureAtlases, SpriteAnimations and SpriteAnimationGroups", game => new AnimationDemo(game)));
Demos.Add("Auto Tiling", ("A demonstration of the AutoTiling class that MLEM provides", game => new AutoTilingDemo(game)));
Demos.Add("Pathfinding", ("An example of MLEM's A* pathfinding implementation in 2D", game => new PathfindingDemo(game)));
Demos.Add("Easings", ("An example of MLEM's Easings class, an adaptation of easings.net", game => new EasingsDemo(game)));
}
public GameImpl() {

View file

@ -8,6 +8,8 @@ using MLEM.Input;
using MLEM.Misc;
using MLEM.Pathfinding;
using MLEM.Startup;
using MLEM.Ui;
using MLEM.Ui.Elements;
namespace Demos {
public class PathfindingDemo : Demo {
@ -15,6 +17,7 @@ namespace Demos {
private bool[,] world;
private AStar2 pathfinder;
private List<Point> path;
private Button regenerateButton;
public PathfindingDemo(MlemGame game) : base(game) {
}
@ -62,19 +65,13 @@ namespace Demos {
}
public override void LoadContent() {
base.LoadContent();
this.regenerateButton = new Button(Anchor.TopCenter, new Vector2(30, 10), "Regenerate") {
OnPressed = e => this.Init()
};
this.UiRoot.AddChild(this.regenerateButton);
this.Init();
}
public override void Update(GameTime gameTime) {
base.Update(gameTime);
// when pressing the left mouse button, generate a new world and find a new path
if (this.InputHandler.IsMouseButtonPressed(MouseButton.Left)) {
this.Init();
}
}
public override void DoDraw(GameTime gameTime) {
this.GraphicsDevice.Clear(Color.White);
@ -102,5 +99,9 @@ namespace Demos {
base.DoDraw(gameTime);
}
public override void Clear() {
this.UiRoot.RemoveChild(this.regenerateButton);
}
}
}

View file

@ -61,7 +61,7 @@ namespace Demos {
this.root = new Panel(Anchor.Center, new Vector2(80, 100), Vector2.Zero, false, true, new Point(5, 10));
this.root.ScrollBar.SmoothScrolling = true;
// add the root to the demos' ui
this.UiSystem.Get("DemoUi").Element.AddChild(this.root);
this.UiRoot.AddChild(this.root);
this.root.AddChild(new Paragraph(Anchor.AutoLeft, 1, "This is a small demo for MLEM.Ui, a user interface library that is part of the MLEM Library by Ellpeck for MonoGame."));
var image = this.root.AddChild(new Image(Anchor.AutoCenter, new Vector2(50, 50), new TextureRegion(this.testTexture, 0, 0, 8, 8)) {IsHidden = true, Padding = new Vector2(3)});