1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-12-27 10:39:24 +01:00
MLEM/Demos/EasingsDemo.cs

67 lines
2.7 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.Graphics;
using MLEM.Maths;
2020-07-16 00:45:25 +02:00
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 = EasingsDemo.EasingFields
2020-07-16 00:45:25 +02:00
.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) % EasingsDemo.Easings.Length;
this.progress = 0;
}
2020-07-16 00:45:25 +02:00
});
this.group.AddChild(new Paragraph(Anchor.AutoCenter, 1, p => EasingsDemo.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);
2022-08-20 11:39:28 +02:00
this.SpriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, DepthStencilState.None, RasterizerState.CullCounterClockwise);
2020-07-16 00:45:25 +02:00
var view = this.GraphicsDevice.Viewport;
// graph the easing function
var graphEase = EasingsDemo.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 = EasingsDemo.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();
}
}
2022-06-17 18:23:47 +02:00
}