mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-26 06:28:35 +01:00
added an extension method to auto-reverse an easing function
This commit is contained in:
parent
cdc08c5cf2
commit
3005b3edcb
2 changed files with 26 additions and 3 deletions
|
@ -17,6 +17,7 @@ namespace Demos {
|
||||||
.Select(f => (Easings.Easing) f.GetValue(null)).ToArray();
|
.Select(f => (Easings.Easing) f.GetValue(null)).ToArray();
|
||||||
private Group group;
|
private Group group;
|
||||||
private int current;
|
private int current;
|
||||||
|
private float progress;
|
||||||
|
|
||||||
public EasingsDemo(MlemGame game) : base(game) {
|
public EasingsDemo(MlemGame game) : base(game) {
|
||||||
}
|
}
|
||||||
|
@ -26,7 +27,10 @@ namespace Demos {
|
||||||
|
|
||||||
this.group = new Group(Anchor.TopCenter, Vector2.One) {SetWidthBasedOnChildren = true};
|
this.group = new Group(Anchor.TopCenter, Vector2.One) {SetWidthBasedOnChildren = true};
|
||||||
this.group.AddChild(new Button(Anchor.AutoCenter, new Vector2(30, 10), "Next") {
|
this.group.AddChild(new Button(Anchor.AutoCenter, new Vector2(30, 10), "Next") {
|
||||||
OnPressed = e => this.current = (this.current + 1) % Easings.Length
|
OnPressed = e => {
|
||||||
|
this.current = (this.current + 1) % Easings.Length;
|
||||||
|
this.progress = 0;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
this.group.AddChild(new Paragraph(Anchor.AutoCenter, 1, p => EasingFields[this.current].Name, true));
|
this.group.AddChild(new Paragraph(Anchor.AutoCenter, 1, p => EasingFields[this.current].Name, true));
|
||||||
this.UiRoot.AddChild(this.group);
|
this.UiRoot.AddChild(this.group);
|
||||||
|
@ -42,11 +46,20 @@ namespace Demos {
|
||||||
|
|
||||||
this.SpriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp);
|
this.SpriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp);
|
||||||
var view = this.GraphicsDevice.Viewport;
|
var view = this.GraphicsDevice.Viewport;
|
||||||
var easing = Easings[this.current].ScaleInput(0, view.Width).ScaleOutput(-view.Height / 3, view.Height / 3);
|
|
||||||
|
// graph the easing function
|
||||||
|
var graphEase = Easings[this.current].ScaleInput(0, view.Width).ScaleOutput(-view.Height / 3, view.Height / 3);
|
||||||
for (var x = 0; x < view.Width; x++) {
|
for (var x = 0; x < view.Width; x++) {
|
||||||
var area = new RectangleF(x - 2, view.Height / 2 - easing(x) - 2, 4, 4);
|
var area = new RectangleF(x - 2, view.Height / 2 - graphEase(x) - 2, 4, 4);
|
||||||
this.SpriteBatch.Draw(this.SpriteBatch.GetBlankTexture(), area, Color.Green);
|
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);
|
||||||
|
|
||||||
this.SpriteBatch.End();
|
this.SpriteBatch.End();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -100,6 +100,16 @@ namespace MLEM.Misc {
|
||||||
return p => easing(p) * (max - min) + min;
|
return p => easing(p) * (max - min) + min;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Causes the easing functino to play fully, and then play fully in reverse, in the span between an input of 0 and 1.
|
||||||
|
/// In some places, this behavior is also called "auto-reversing".
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="easing">The easing function to play in reverse as well</param>
|
||||||
|
/// <returns>An auto-reversing version of the easing function</returns>
|
||||||
|
public static Easing AndReverse(this Easing easing) {
|
||||||
|
return p => p <= 0.5F ? easing(p * 2) : easing(1 - (p - 0.5F) * 2);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A delegate method used by <see cref="Easings"/>.
|
/// A delegate method used by <see cref="Easings"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
Loading…
Reference in a new issue