diff --git a/Demos/AnimationDemo.cs b/Demos/AnimationDemo.cs index d394575..a9a4179 100644 --- a/Demos/AnimationDemo.cs +++ b/Demos/AnimationDemo.cs @@ -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); + } + } } \ No newline at end of file diff --git a/Demos/Demo.cs b/Demos/Demo.cs index be050da..6132aa6 100644 --- a/Demos/Demo.cs +++ b/Demos/Demo.cs @@ -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; diff --git a/Demos/EasingsDemo.cs b/Demos/EasingsDemo.cs index 94649d0..3600019 100644 --- a/Demos/EasingsDemo.cs +++ b/Demos/EasingsDemo.cs @@ -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() { diff --git a/Demos/GameImpl.cs b/Demos/GameImpl.cs index b123257..bb855da 100644 --- a/Demos/GameImpl.cs +++ b/Demos/GameImpl.cs @@ -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() { diff --git a/Demos/PathfindingDemo.cs b/Demos/PathfindingDemo.cs index 0b01f09..5f4d05b 100644 --- a/Demos/PathfindingDemo.cs +++ b/Demos/PathfindingDemo.cs @@ -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 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); + } + } } \ No newline at end of file diff --git a/Demos/UiDemo.cs b/Demos/UiDemo.cs index ee70b48..5d8f241 100644 --- a/Demos/UiDemo.cs +++ b/Demos/UiDemo.cs @@ -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)});