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;
|
2020-10-24 22:10:34 +02:00
|
|
|
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") {
|
2020-10-24 22:10:34 +02:00
|
|
|
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));
|
2020-07-17 14:56:22 +02:00
|
|
|
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;
|
2020-10-24 22:10:34 +02:00
|
|
|
|
|
|
|
// 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++) {
|
2020-10-24 22:10:34 +02:00
|
|
|
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);
|
|
|
|
}
|
2020-10-24 22:10:34 +02:00
|
|
|
|
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|