1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-25 22:18:34 +01:00
MLEM/Demos/EasingsDemo.cs

66 lines
2.6 KiB
C#
Raw Normal View History

2020-07-16 00:45:25 +02:00
using System.Linq;
using System.Reflection;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MLEM.Extensions;
using MLEM.Misc;
using MLEM.Startup;
using MLEM.Ui;
using MLEM.Ui.Elements;
namespace Demos {
public class EasingsDemo : Demo {
private static readonly FieldInfo[] EasingFields = typeof(Easings)
.GetFields(BindingFlags.Public | BindingFlags.Static).ToArray();
private static readonly Easings.Easing[] Easings = EasingFields
.Select(f => (Easings.Easing) f.GetValue(null)).ToArray();
private Group group;
private int current;
private float progress;
2020-07-16 00:45:25 +02:00
2021-12-28 14:56:11 +01:00
public EasingsDemo(MlemGame game) : base(game) {}
2020-07-16 00:45:25 +02:00
public override void LoadContent() {
base.LoadContent();
2021-04-02 17:12:27 +02:00
this.group = new Group(Anchor.TopCenter, Vector2.One) {CanBeMoused = false};
2020-07-16 00:45:25 +02:00
this.group.AddChild(new Button(Anchor.AutoCenter, new Vector2(30, 10), "Next") {
OnPressed = e => {
this.current = (this.current + 1) % Easings.Length;
this.progress = 0;
}
2020-07-16 00:45:25 +02:00
});
this.group.AddChild(new Paragraph(Anchor.AutoCenter, 1, p => EasingFields[this.current].Name, true));
this.UiRoot.AddChild(this.group);
2020-07-16 00:45:25 +02:00
}
public override void Clear() {
this.group.Parent.RemoveChild(this.group);
}
public override void DoDraw(GameTime time) {
this.GraphicsDevice.Clear(Color.CornflowerBlue);
base.DoDraw(time);
this.SpriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp);
var view = this.GraphicsDevice.Viewport;
// graph the easing function
var graphEase = Easings[this.current].ScaleInput(0, view.Width).ScaleOutput(-view.Height / 3, view.Height / 3);
2020-07-16 00:45:25 +02:00
for (var x = 0; x < view.Width; x++) {
var area = new RectangleF(x - 2, view.Height / 2 - graphEase(x) - 2, 4, 4);
2020-07-16 00:45:25 +02:00
this.SpriteBatch.Draw(this.SpriteBatch.GetBlankTexture(), area, Color.Green);
}
// draw a little dot to show what it would look like
this.progress = (this.progress + (float) time.ElapsedGameTime.TotalSeconds / 2) % 1;
var dotEase = Easings[this.current].AndReverse().ScaleOutput(0, view.Height / 4);
var pos = new RectangleF(view.Width / 2 - 4, view.Height - 20 - dotEase(this.progress), 8, 8);
this.SpriteBatch.Draw(this.SpriteBatch.GetBlankTexture(), pos, Color.Red);
2020-07-16 00:45:25 +02:00
this.SpriteBatch.End();
}
}
}