|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq.Expressions; |
| 4 | +using System.Reflection; |
| 5 | +using System.Text; |
| 6 | + |
| 7 | +namespace SharpEngine.Core.Utils.Tween |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Represents a single step in a tween animation sequence. |
| 11 | + /// </summary> |
| 12 | + /// <remarks>A TweenStep typically encapsulates the logic for updating a portion of an animation over |
| 13 | + /// time. Instances of this class are commonly used within tweening frameworks to manage incremental changes to |
| 14 | + /// animated properties.</remarks> |
| 15 | + public class TweenStep |
| 16 | + { |
| 17 | + /// <summary> |
| 18 | + /// Duration of the step |
| 19 | + /// </summary> |
| 20 | + public float CurrentDuration { get; set; } |
| 21 | + |
| 22 | + private readonly List<TweenData<float, float>> _floatTweens = []; |
| 23 | + private readonly List<TweenData<int, int>> _intTweens = []; |
| 24 | + private readonly List<TweenData<Color, ColorStep>> _colorTweens = []; |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Initializes a new instance of the TweenStep class with the specified duration. |
| 28 | + /// </summary> |
| 29 | + /// <param name="duration">The duration, in seconds, for this tween step. Must be greater than or equal to zero.</param> |
| 30 | + public TweenStep(float duration) |
| 31 | + { |
| 32 | + CurrentDuration = duration; |
| 33 | + } |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Add a float tween to the step |
| 37 | + /// </summary> |
| 38 | + /// <param name="entity">Entity</param> |
| 39 | + /// <param name="property">Property which be modified</param> |
| 40 | + /// <param name="to">Target Value</param> |
| 41 | + /// <param name="duration">Duration of the tween</param> |
| 42 | + /// <param name="from">Source Value</param> |
| 43 | + /// <returns>Tween Step</returns> |
| 44 | + public TweenStep Float(object entity, Expression<Func<object, float>> property, float to, float duration, float? from = null) |
| 45 | + { |
| 46 | + _floatTweens.Add(new TweenData<float, float>(entity, property, from ?? 0, to, duration, from is not null)); |
| 47 | + return this; |
| 48 | + } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Add an int tween to the step |
| 52 | + /// </summary> |
| 53 | + /// <param name="entity">Entity</param> |
| 54 | + /// <param name="property">Property which be modified</param> |
| 55 | + /// <param name="to">Target Value</param> |
| 56 | + /// <param name="duration">Duration of the tween</param> |
| 57 | + /// <param name="from">Source Value</param> |
| 58 | + /// <returns>Tween Step</returns> |
| 59 | + public TweenStep Int(object entity, Expression<Func<object, int>> property, int to, float duration, int? from = null) |
| 60 | + { |
| 61 | + _intTweens.Add(new TweenData<int, int>(entity, property, from ?? 0, to, duration, from is not null)); |
| 62 | + return this; |
| 63 | + } |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Animates a color property of the specified entity from a starting value to a target value over the given |
| 67 | + /// duration. |
| 68 | + /// </summary> |
| 69 | + /// <param name="entity">The entity whose color property will be animated.</param> |
| 70 | + /// <param name="property">An expression that selects the color property of the entity to animate.</param> |
| 71 | + /// <param name="to">The target color value to animate to.</param> |
| 72 | + /// <param name="duration">The duration, in seconds, over which the animation occurs. Must be greater than zero.</param> |
| 73 | + /// <param name="from">The initial color value to animate from. If null, the current value of the property is used.</param> |
| 74 | + /// <returns>The current <see cref="TweenStep"/> instance, allowing for method chaining.</returns> |
| 75 | + public TweenStep Color(object entity, Expression<Func<object, Color>> property, Color to, float duration, Color? from = null) |
| 76 | + { |
| 77 | + _colorTweens.Add(new TweenData<Color, ColorStep>(entity, property, from ?? Utils.Color.White, to, duration, from is not null)); |
| 78 | + return this; |
| 79 | + } |
| 80 | + |
| 81 | + /// <summary> |
| 82 | + /// Update the tween step |
| 83 | + /// </summary> |
| 84 | + /// <param name="deltaTime">Delta time</param> |
| 85 | + /// <returns>If step ended</returns> |
| 86 | + public bool Update(float deltaTime) |
| 87 | + { |
| 88 | + CurrentDuration -= deltaTime; |
| 89 | + foreach (var floatTween in _floatTweens) |
| 90 | + floatTween.Update(deltaTime); |
| 91 | + foreach (var intTween in _intTweens) |
| 92 | + intTween.Update(deltaTime); |
| 93 | + foreach (var colorTween in _colorTweens) |
| 94 | + colorTween.Update(deltaTime); |
| 95 | + return CurrentDuration <= 0f; |
| 96 | + } |
| 97 | + |
| 98 | + internal void Launch() |
| 99 | + { |
| 100 | + foreach (var floatTween in _floatTweens) |
| 101 | + floatTween.Launch(); |
| 102 | + foreach (var intTween in _intTweens) |
| 103 | + intTween.Launch(); |
| 104 | + foreach (var colorTween in _colorTweens) |
| 105 | + colorTween.Launch(); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments