From 9a9bf52fe95caad2da577201d204d2d6e1e1afbe Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Thu, 22 Dec 2016 12:27:53 +0100 Subject: [PATCH] Added new brush-effect to move gradients --- CUE.NET.csproj | 1 + Effects/MoveGradientEffect.cs | 97 +++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 Effects/MoveGradientEffect.cs diff --git a/CUE.NET.csproj b/CUE.NET.csproj index 65c71c0..59c5f6e 100644 --- a/CUE.NET.csproj +++ b/CUE.NET.csproj @@ -74,6 +74,7 @@ + diff --git a/Effects/MoveGradientEffect.cs b/Effects/MoveGradientEffect.cs new file mode 100644 index 0000000..85f81e5 --- /dev/null +++ b/Effects/MoveGradientEffect.cs @@ -0,0 +1,97 @@ +using CUE.NET.Brushes; +using CUE.NET.Gradients; + +namespace CUE.NET.Effects +{ + /// + /// Represents an effect which allows to move an gradient by modifying his offset. + /// + public class MoveGradientEffect : AbstractBrushEffect + { + #region Properties & Fields + + /// + /// Gets or sets the direction the gradient is moved. + /// True leads to an offset-increment (normaly moving to the right), false to an offset-decrement (normaly moving to the left). + /// + public bool Direction { get; set; } + + /// + /// Gets or sets the speed of the movement in units per second. + /// The meaning of units differs for the different gradients, but 360 units will always be one complete cycle: + /// LinearGradient: 360 unit = 1 offset. + /// RainbowGradient: 1 unit = 1 degree. + /// + public float Speed { get; set; } + + #endregion + + #region Constructors + + /// + /// + /// + /// + /// + public MoveGradientEffect(float speed = 180f, bool direction = true) + { + this.Speed = speed; + this.Direction = direction; + } + + #endregion + + #region Methods + /// + /// Updates the effect. + /// + /// The elapsed time (in seconds) since the last update. + public override void Update(float deltaTime) + { + float movement = Speed * deltaTime; + + if (!Direction) + movement = -movement; + + if (Brush.Gradient is LinearGradient) + { + LinearGradient linearGradient = (LinearGradient)Brush.Gradient; + + movement /= 360f; + + foreach (GradientStop gradientStop in linearGradient.GradientStops) + { + gradientStop.Offset = gradientStop.Offset + movement; + + if (gradientStop.Offset > 1f) + gradientStop.Offset -= 1f; + else if (gradientStop.Offset < 0) + gradientStop.Offset += 1f; + } + } + else if (Brush.Gradient is RainbowGradient) + { + RainbowGradient rainbowGradient = (RainbowGradient)Brush.Gradient; + + // RainbowGradient is calculated inverse but the movement should be the same for all. + movement *= -1; + + rainbowGradient.StartHue += movement; + rainbowGradient.EndHue += movement; + + if (rainbowGradient.StartHue > 360f && rainbowGradient.EndHue > 360f) + { + rainbowGradient.StartHue -= 360f; + rainbowGradient.EndHue -= 360f; + } + else if (rainbowGradient.StartHue < -360f && rainbowGradient.EndHue < -360f) + { + rainbowGradient.StartHue += 360f; + rainbowGradient.EndHue += 360f; + } + } + } + + #endregion + } +}