Skip to content

Commit

Permalink
Graphics: Add texture fade function
Browse files Browse the repository at this point in the history
  • Loading branch information
Beyley committed Dec 21, 2022
1 parent 52ae1c4 commit c2560fd
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 19 deletions.
79 changes: 62 additions & 17 deletions Furball.Engine/Engine/Graphics/Drawables/TexturedDrawable.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@

using System;
using System.Drawing;
using System.Numerics;
using Furball.Engine.Engine.Graphics.Drawables.Managers;
using Furball.Engine.Engine.Graphics.Drawables.Tweens;
using Furball.Engine.Engine.Graphics.Drawables.Tweens.TweenTypes;
using Furball.Vixie;
using JetBrains.Annotations;
using Color=Furball.Vixie.Backends.Shared.Color;

namespace Furball.Engine.Engine.Graphics.Drawables;
namespace Furball.Engine.Engine.Graphics.Drawables;

/// <summary>
/// A Basic Managed Drawable that just Draws a Texture to the Screen,
Expand All @@ -23,7 +27,19 @@ public class TexturedDrawable : Drawable {
/// Unprocessed Size of the Drawable in Pixels
/// <remarks>This variable does not get changed as the DrawableManager translates the Drawable to be Scaled to be properly visible on all resolutions</remarks>
/// </summary>
public override Vector2 Size => this.Cropping == null ? new Vector2(this.Texture.Size.X, this.Texture.Size.Y) * this.Scale : new Vector2(this.Cropping.Value.Width, this.Cropping.Value.Height) * this.Scale;
public override Vector2 Size => this.Cropping == null
? new Vector2(this.Texture.Size.X, this.Texture.Size.Y) * this.Scale
: new Vector2(this.Cropping.Value.Width, this.Cropping.Value.Height) * this.Scale;

private FloatTween _fadeTextureTween = null;
private Texture _fadeTextureTo;
public void FadeTexture(Texture tex, double time, Easing easing = Easing.None) {
if (tex.Size != this.Texture.Size)
throw new Exception("Sizes of the fading textures must match!");

this._fadeTextureTween = new FloatTween(TweenType.Fade, 0, 1, FurballGame.Time, FurballGame.Time + time, easing);
this._fadeTextureTo = tex;
}

/// <summary>
/// TexturedDrawable Constructor
Expand All @@ -35,6 +51,7 @@ public TexturedDrawable(Texture texture, Vector2 position) {

this.Texture = texture;
}

/// <summary>
/// TexturedDrawable Constructor that allows for Cropping
/// </summary>
Expand All @@ -50,19 +67,47 @@ public TexturedDrawable(Texture texture, Vector2 position, Rectangle cropping, f
this.Texture = texture;
}

public override void Update(double time) {
base.Update(time);

//Update the tween with the current time of the drawable
this._fadeTextureTween?.Update(this.DrawableTime);

//If the tween is terminated, then we are fully faded to the new texture
if (this._fadeTextureTween is {
Terminated: true
}) {
this.Texture = this._fadeTextureTo;
this._fadeTextureTween = null;
this._fadeTextureTo = null;
}
}

public override void Draw(double time, DrawableBatch batch, DrawableManagerArgs args) {
if(this.Cropping != null)
batch.Draw(
this.Texture,
args.Position,
args.Scale,
args.Rotation,
args.Color,
this.Cropping.Value,
args.Effects,
this.RotationOrigin
);
else
batch.Draw(this.Texture, args.Position, args.Scale, args.Rotation, args.Color, args.Effects, this.RotationOrigin);
if (this.Cropping != null) {
if (this._fadeTextureTween != null) {
float progress = this._fadeTextureTween.GetCurrent();

Color orig = args.Color with {
Af = args.Color.Af * (1f - progress)
};

batch.Draw(this._fadeTextureTo, args.Position, args.Scale, args.Rotation, args.Color, this.Cropping.Value, args.Effects, this.RotationOrigin);
batch.Draw(this.Texture, args.Position, args.Scale, args.Rotation, orig, this.Cropping.Value, args.Effects, this.RotationOrigin);
} else
batch.Draw(this.Texture, args.Position, args.Scale, args.Rotation, args.Color, this.Cropping.Value, args.Effects, this.RotationOrigin);
} else {
if (this._fadeTextureTween != null) {
float progress = this._fadeTextureTween.GetCurrent();

Color orig = args.Color with {
Af = args.Color.Af * (1f - progress)
};

batch.Draw(this._fadeTextureTo, args.Position, args.Scale, args.Rotation, args.Color, args.Effects, this.RotationOrigin);
batch.Draw(this.Texture, args.Position, args.Scale, args.Rotation, orig, args.Effects, this.RotationOrigin);
} else
batch.Draw(this.Texture, args.Position, args.Scale, args.Rotation, args.Color, args.Effects, this.RotationOrigin);
}
}
}
}
1 change: 1 addition & 0 deletions Furball.Game/FurballTestGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ protected override void InitializeLocalizations() {
LocalizationManager.AddDefaultTranslation(LocalizationStrings.GMLTest, "GML Test");
LocalizationManager.AddDefaultTranslation(LocalizationStrings.TexturePackerTest, "Texture Packer Test");
LocalizationManager.AddDefaultTranslation(LocalizationStrings.BoxBlurTest, "Box Blur Test");
LocalizationManager.AddDefaultTranslation(LocalizationStrings.TextureFadeTest, "Texture Fade Test");

LocalizationManager.AddDefaultTranslation(LocalizationStrings.ChooseScreen, "Choose Screen");
}
Expand Down
1 change: 1 addition & 0 deletions Furball.Game/LocalizationStrings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ public enum LocalizationStrings {
GMLTest,
TexturePackerTest,
BoxBlurTest,
TextureFadeTest
}
3 changes: 2 additions & 1 deletion Furball.Game/Screens/ScreenSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public override void Initialize() {
(LocalizationStrings.GraphTest, typeof(GraphTest)),
(LocalizationStrings.GMLTest, typeof(GMLTest)),
(LocalizationStrings.TexturePackerTest, typeof(TexturePackerTest)),
(LocalizationStrings.BoxBlurTest, typeof(BoxBlurTest))
(LocalizationStrings.BoxBlurTest, typeof(BoxBlurTest)),
(LocalizationStrings.TextureFadeTest, typeof(TextureFadeTest))
};

this.Manager.Add(this._topText = new TextDrawable(new Vector2(1280f / 2f, 40), FurballGame.DefaultFont, LocalizationManager.GetLocalizedString(LocalizationStrings.ChooseScreen), 48) {
Expand Down
40 changes: 40 additions & 0 deletions Furball.Game/Screens/Tests/TextureFadeTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Numerics;
using Furball.Engine;
using Furball.Engine.Engine.Graphics;
using Furball.Engine.Engine.Graphics.Drawables;
using Furball.Engine.Engine.Graphics.Drawables.UiElements;
using Furball.Engine.Engine.Input.Events;
using Furball.Vixie;
using Furball.Vixie.Backends.Shared;
using Furball.Vixie.Backends.Shared.TextureEffects.Blur;

namespace Furball.Game.Screens.Tests;

public class TextureFadeTest : TestScreen {
private TexturedDrawable _textureDrawable;
private Texture _source;
private BoxBlurTextureEffect _blur;
private Texture _dest;

public override void Initialize() {
base.Initialize();

this._source = ContentManager.LoadTextureFromFileCached("inviswater.png");
this._blur = FurballGame.Instance.WindowManager.GraphicsBackend.CreateBoxBlurTextureEffect(this._source);
this._dest = new Texture(this._blur.Texture);
this._blur.UpdateTexture();

this.Manager.Add(this._textureDrawable = new TexturedDrawable(this._source, new Vector2(10)));

this.Manager.Add(
new DrawableButton(new Vector2(10), FurballGame.DefaultFont, 24, "Fade texture", Color.Blue, Color.White, Color.Black, Vector2.Zero, FadeTexture) {
ScreenOriginType = OriginType.TopRight,
OriginType = OriginType.TopRight
}
);
}

private void FadeTexture(object sender, MouseButtonEventArgs e) {
this._textureDrawable.FadeTexture(this._textureDrawable.Texture == this._source ? this._dest : this._source, 1000);
}
}

0 comments on commit c2560fd

Please sign in to comment.