diff options
| author | Jean-Philippe Bruyère <jp_bruyere@hotmail.com> | 2022-07-17 20:26:28 +0200 |
|---|---|---|
| committer | Jean-Philippe Bruyère <jp_bruyere@hotmail.com> | 2022-07-17 20:26:28 +0200 |
| commit | 9d786ab096ed36ea384e03ee5cea498790e31eec (patch) | |
| tree | adee54b5b90a35238d5794e9ffd3ebcf65798a06 /Samples/AnimTest/Animation | |
| parent | 57daad000aa31c7bcfa1f76b90d40da5a21da041 (diff) | |
wip
Diffstat (limited to 'Samples/AnimTest/Animation')
| -rw-r--r-- | Samples/AnimTest/Animation/AngleAnimation.cs | 19 | ||||
| -rw-r--r-- | Samples/AnimTest/Animation/Animation.cs | 155 | ||||
| -rw-r--r-- | Samples/AnimTest/Animation/AnimationTemplate.cs | 110 | ||||
| -rw-r--r-- | Samples/AnimTest/Animation/BezierPath.cs | 34 | ||||
| -rw-r--r-- | Samples/AnimTest/Animation/FloatAnimation.cs | 83 | ||||
| -rw-r--r-- | Samples/AnimTest/Animation/FloatAnimation2.cs | 60 | ||||
| -rw-r--r-- | Samples/AnimTest/Animation/MathHelper.cs | 16 | ||||
| -rw-r--r-- | Samples/AnimTest/Animation/Path.cs | 51 | ||||
| -rw-r--r-- | Samples/AnimTest/Animation/PathAnimation.cs | 53 | ||||
| -rw-r--r-- | Samples/AnimTest/Animation/ShakeAnimation.cs | 60 |
10 files changed, 641 insertions, 0 deletions
diff --git a/Samples/AnimTest/Animation/AngleAnimation.cs b/Samples/AnimTest/Animation/AngleAnimation.cs new file mode 100644 index 00000000..02faef76 --- /dev/null +++ b/Samples/AnimTest/Animation/AngleAnimation.cs @@ -0,0 +1,19 @@ +// Copyright (c) 2015-2021 Jean-Philippe Bruyère <jp_bruyere@hotmail.com> +// +// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) + +using System; + +namespace vke +{ + public class AngleAnimation : FloatAnimation2 + { + #region CTOR + public AngleAnimation(Object instance, string PropertyName, float Target, int stepCount = 20) : + base (instance,PropertyName,MathHelper.NormalizeAngle(Target),stepCount) { + initialValue = MathHelper.NormalizeAngle (initialValue); + } + #endregion + } +} + diff --git a/Samples/AnimTest/Animation/Animation.cs b/Samples/AnimTest/Animation/Animation.cs new file mode 100644 index 00000000..6ca7af8c --- /dev/null +++ b/Samples/AnimTest/Animation/Animation.cs @@ -0,0 +1,155 @@ +// Copyright (c) 2015-2021 Jean-Philippe Bruyère <jp_bruyere@hotmail.com> +// +// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) + +using System; +using System.Collections.Generic; +using System.Reflection; +using System.Diagnostics; + +namespace vke +{ + public delegate void AnimationEventHandler(Animation a); + + public delegate float GetterDelegate(); + public delegate void SetterDelegate(float value); + + public class Animation + { + public event AnimationEventHandler AnimationFinished; + + public static Random random = new Random (); + public static int DelayMs = 0; + + protected GetterDelegate getValue; + protected SetterDelegate setValue; + + public string propertyName; + + protected Stopwatch timer = new Stopwatch(); + protected int delayStartMs = 0; + /// <summary> + /// Delay before firing ZnimationFinished event. + /// </summary> + protected int delayFinishMs = 0; + public static List<Animation> AnimationList = new List<Animation>(); + public static bool HasAnimations => AnimationList.Count > 0; + //public FieldInfo member; + public Object AnimatedInstance; + + #region CTOR + public Animation (){} + public Animation(Object instance, string _propertyName) + { + propertyName = _propertyName; + AnimatedInstance = instance; + PropertyInfo pi = instance.GetType().GetProperty(propertyName); + try { + getValue = (GetterDelegate)Delegate.CreateDelegate(typeof(GetterDelegate), instance, pi.GetGetMethod()); + setValue = (SetterDelegate)Delegate.CreateDelegate(typeof(SetterDelegate), instance, pi.GetSetMethod()); + } catch (Exception ex) { + Debug.WriteLine (ex.ToString ()); + } + } + #endregion + + public static void StartAnimation(Animation a, int delayMs = 0, AnimationEventHandler OnEnd = null) + { + lock (AnimationList) { + Animation aa = null; + if (Animation.GetAnimation (a.AnimatedInstance, a.propertyName, ref aa)) { + aa.CancelAnimation (); + } + + //a.AnimationFinished += onAnimationFinished; + + a.AnimationFinished += OnEnd; + a.delayStartMs = delayMs + DelayMs; + + + if (a.delayStartMs > 0) + a.timer.Start (); + + AnimationList.Add (a); + } + + } + + static Stack<Animation> anims = new Stack<Animation>(); + static int frame = 0; + public static void ProcessAnimations() + { + frame++; + +// #region FLYING anim +// if (frame % 20 == 0){ +// foreach (Player p in MagicEngine.CurrentEngine.Players) { +// foreach (CardInstance c in p.InPlay.Cards.Where(ci => ci.HasAbility(AbilityEnum.Flying) && ci.z < 0.4f)) { +// +// } +// } +// } +// #endregion + //Stopwatch animationTime = new Stopwatch(); + //animationTime.Start(); + + const int maxAnim = 200000; + int count = 0; + + + lock (AnimationList) { + if (anims.Count == 0) + anims = new Stack<Animation> (AnimationList); + } + + while (anims.Count > 0 && count < maxAnim) { + Animation a = anims.Pop (); + if (a == null) + continue; + if (a.timer.IsRunning) { + if (a.timer.ElapsedMilliseconds > a.delayStartMs) + a.timer.Stop (); + else + continue; + } + + a.Process (); + count++; + } + + //animationTime.Stop(); + //Debug.WriteLine("animation: {0} ticks \t {1} ms ", animationTime.ElapsedTicks,animationTime.ElapsedMilliseconds); + } + public static bool GetAnimation(object instance, string PropertyName, ref Animation a) + { + for (int i = 0; i < AnimationList.Count; i++) { + Animation anim = AnimationList [i]; + if (anim == null) { + continue; + } + if (anim.AnimatedInstance == instance && anim.propertyName == PropertyName) { + a = anim; + return true; + } + } + + return false; + } + public virtual void Process () {} + public void CancelAnimation() + { + //Debug.WriteLine("Cancel anim: " + this.ToString()); + AnimationList.Remove(this); + } + public void RaiseAnimationFinishedEvent() + { + if (AnimationFinished != null) + AnimationFinished (this); + } + + public static void onAnimationFinished(Animation a) + { + Debug.WriteLine ("\t\tAnimation finished: " + a.ToString ()); + } + } +} diff --git a/Samples/AnimTest/Animation/AnimationTemplate.cs b/Samples/AnimTest/Animation/AnimationTemplate.cs new file mode 100644 index 00000000..d5c784c3 --- /dev/null +++ b/Samples/AnimTest/Animation/AnimationTemplate.cs @@ -0,0 +1,110 @@ +// Copyright (c) 2015-2021 Jean-Philippe Bruyère <jp_bruyere@hotmail.com> +// +// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) + +using System; +using MiscUtil; +using System.Reflection; + +namespace vke +{ + public class Animation<T> : Animation + { + public delegate T GetterDelegate(); + public delegate void SetterDelegate(T value); + + public T TargetValue; + T initialValue; + T zero; + public T Step; + public bool Cycle; + + protected GetterDelegate getValue; + protected SetterDelegate setValue; + + #region CTOR + public Animation(Object instance, string _propertyName) + { + propertyName = _propertyName; + AnimatedInstance = instance; + PropertyInfo pi = instance.GetType().GetProperty(propertyName); + getValue = (GetterDelegate)Delegate.CreateDelegate(typeof(GetterDelegate), instance, pi.GetGetMethod()); + setValue = (SetterDelegate)Delegate.CreateDelegate(typeof(SetterDelegate), instance, pi.GetSetMethod()); + } + public Animation(Object instance, string _propertyName, T Target, T step) + { + propertyName = _propertyName; + AnimatedInstance = instance; + PropertyInfo pi = instance.GetType().GetProperty(propertyName); + getValue = (GetterDelegate)Delegate.CreateDelegate(typeof(GetterDelegate), instance, pi.GetGetMethod()); + setValue = (SetterDelegate)Delegate.CreateDelegate(typeof(SetterDelegate), instance, pi.GetSetMethod()); + + TargetValue = Target; + + T value = getValue(); + initialValue = value; + Type t = typeof(T); + + if (t.IsPrimitive) { + Step = (T)Convert.ChangeType (step, t); + zero = (T)Convert.ChangeType (0, t); + }else { + Step = (T)Activator.CreateInstance (typeof(T), new Object[] { step }); + zero = (T)Activator.CreateInstance (typeof(T), 0f); + } + T test = (T)Operator.SubtractAlternative (value, TargetValue); + + if (Operator.LessThan(test, zero)) + { + if (Operator.LessThan (Step, zero)) + Step = Operator.Negate (Step); + } + else if (Operator.GreaterThan(Step, zero)) + Step = Operator.Negate (Step); + } + #endregion + + public override void Process() + { + T value = getValue(); + + //Debug.WriteLine ("Anim: {0} <= {1}", value, this.ToString ()); + + if (Operator.GreaterThan(Step, zero)) + { + value = Operator.Add (value, Step); + setValue(value); + //Debug.WriteLine(value); + if (Operator.GreaterThan(Operator.Subtract(TargetValue, value), zero)) + return; + } + else + { + value = Operator.Add (value, Step); + setValue(value); + + if (Operator.LessThan(Operator.Subtract(TargetValue, value), zero)) + return; + } + + if (Cycle) { + Step = Operator.Negate (Step); + TargetValue = initialValue; + Cycle = false; + return; + } + + setValue(TargetValue); + AnimationList.Remove(this); + + RaiseAnimationFinishedEvent (); + } + + public override string ToString () + { + return string.Format ("{0}:->{1}:{2}",base.ToString(),TargetValue,Step); + } + } + +} + diff --git a/Samples/AnimTest/Animation/BezierPath.cs b/Samples/AnimTest/Animation/BezierPath.cs new file mode 100644 index 00000000..a3126e3e --- /dev/null +++ b/Samples/AnimTest/Animation/BezierPath.cs @@ -0,0 +1,34 @@ +// Copyright (c) 2015-2021 Jean-Philippe Bruyère <jp_bruyere@hotmail.com> +// +// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) + +using System; +using System.Numerics; + +namespace vke +{ + public class BezierPath : Path + { + public Vector3 ControlPointStart; + public Vector3 ControlPointEnd; + + public BezierPath (Vector3 startPos, Vector3 controlPointStart, + Vector3 controlPointEnd, Vector3 endPos) + :base(startPos, endPos) + { + ControlPointStart = controlPointStart; + ControlPointEnd = controlPointEnd; + } + public BezierPath (Vector3 startPos, Vector3 endPos, Vector3 vUp) + :base(startPos, endPos) + { + ControlPointStart = startPos + vUp; + ControlPointEnd = endPos + vUp; + } + public override Vector3 GetStep (float pos) + { + return Path.CalculateBezierPoint (pos, Start, ControlPointStart, ControlPointEnd, End); + } + } +} + diff --git a/Samples/AnimTest/Animation/FloatAnimation.cs b/Samples/AnimTest/Animation/FloatAnimation.cs new file mode 100644 index 00000000..a7e4bf7c --- /dev/null +++ b/Samples/AnimTest/Animation/FloatAnimation.cs @@ -0,0 +1,83 @@ +// Copyright (c) 2015-2021 Jean-Philippe Bruyère <jp_bruyere@hotmail.com> +// +// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) + +using System; + +namespace vke +{ + public class FloatAnimation : Animation + { + + public float TargetValue; + float initialValue; + public float Step; + public bool Cycle; + + #region CTOR + public FloatAnimation(Object instance, string _propertyName, float Target, float step = 0.2f) + : base(instance, _propertyName) + { + + TargetValue = Target; + + float value = getValue(); + initialValue = value; + + Step = step; + + if (value < TargetValue) + { + if (Step < 0) + Step = -Step; + } + else if (Step > 0) + Step = -Step; + } + #endregion + + public override void Process() + { + float value = getValue(); + + //Debug.WriteLine ("Anim: {0} <= {1}", value, this.ToString ()); + + if (Step > 0f) + { + value += Step; + setValue(value); + //Debug.WriteLine(value); + if (TargetValue > value) + return; + } + else + { + value += Step; + setValue(value); + + if (TargetValue < value) + return; + } + + if (Cycle) { + Step = -Step; + TargetValue = initialValue; + Cycle = false; + return; + } + + setValue(TargetValue); + lock(AnimationList) + AnimationList.Remove(this); + + RaiseAnimationFinishedEvent (); + } + + public override string ToString () + { + return string.Format ("{0}:->{1}:{2}",base.ToString(),TargetValue,Step); + } + } + +} + diff --git a/Samples/AnimTest/Animation/FloatAnimation2.cs b/Samples/AnimTest/Animation/FloatAnimation2.cs new file mode 100644 index 00000000..1d5c9193 --- /dev/null +++ b/Samples/AnimTest/Animation/FloatAnimation2.cs @@ -0,0 +1,60 @@ +// Copyright (c) 2015-2021 Jean-Philippe Bruyère <jp_bruyere@hotmail.com> +// +// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) + +using System; + +namespace vke +{ + public class FloatAnimation2 : Animation + { + + public float TargetValue; + protected float initialValue; + int stepCount, currentStep; + + #region CTOR + public FloatAnimation2(Object instance, string _propertyName, float Target, int _stepCount = 20) + : base(instance, _propertyName) + { + + TargetValue = Target; + + float value = getValue(); + initialValue = value; + + stepCount = _stepCount; + } + #endregion + + bool smooth = true; + + float smoothedStep (float step) => (-MathF.Cos (step * MathF.PI) + 1)/2.0f; + + public override void Process() + { + + currentStep++; + + float t = (float)currentStep / (float)stepCount; + if (smooth) + t = smoothedStep (t); + + setValue (initialValue + t * (TargetValue - initialValue)); + + if (currentStep < stepCount) + return; + + setValue(TargetValue); + AnimationList.Remove(this); + RaiseAnimationFinishedEvent (); + } + + public override string ToString () + { + return string.Format ("{0}:->{1}:{2}",base.ToString(),TargetValue,stepCount); + } + } + +} + diff --git a/Samples/AnimTest/Animation/MathHelper.cs b/Samples/AnimTest/Animation/MathHelper.cs new file mode 100644 index 00000000..35a3009f --- /dev/null +++ b/Samples/AnimTest/Animation/MathHelper.cs @@ -0,0 +1,16 @@ +// Copyright (c) 2019-2021 Jean-Philippe Bruyère <jp_bruyere@hotmail.com> +// +// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) +using System; +namespace vke { + public static class MathHelper { + public const float Pi = (float)Math.PI; + public const float TwoPi = (float)Math.PI * 2; + public static float NormalizeAngle (float a) { + float tmp = a % MathHelper.TwoPi; + if (tmp < 0) + tmp += MathHelper.TwoPi; + return tmp; + } + } +} diff --git a/Samples/AnimTest/Animation/Path.cs b/Samples/AnimTest/Animation/Path.cs new file mode 100644 index 00000000..1f04e090 --- /dev/null +++ b/Samples/AnimTest/Animation/Path.cs @@ -0,0 +1,51 @@ +// Copyright (c) 2015-2021 Jean-Philippe Bruyère <jp_bruyere@hotmail.com> +// +// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) +using System.Numerics; + +namespace vke { + public class Path + { + public Vector3 Start; + public Vector3 End; + + float length; + Vector3 vDir; + + public Path(Vector3 startPos, Vector3 endPos){ + Start = startPos; + End = endPos; + initialComputations (); + } + protected void initialComputations(){ + Vector3 vPath = End - Start; + vDir = Vector3.Normalize (vPath); + length = vPath.Length(); + } + /// <summary> + /// Get single step on the path + /// </summary> + /// <returns>return position on the path</returns> + /// <param name="pos">Position expressed as percentage of total length</param> + public virtual Vector3 GetStep(float pos){ + return Start + vDir * length * pos; + } + + public static Vector3 CalculateBezierPoint(float t, Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3) + { + float u = 1 - t; + float tt = t * t; + float uu = u * u; + float uuu = uu * u; + float ttt = tt * t; + + Vector3 p = uuu * p0; + p += 3 * uu * t * p1; + p += 3 * u * tt * p2; + p += ttt * p3; + + return p; + } + } + +} diff --git a/Samples/AnimTest/Animation/PathAnimation.cs b/Samples/AnimTest/Animation/PathAnimation.cs new file mode 100644 index 00000000..54c30e9a --- /dev/null +++ b/Samples/AnimTest/Animation/PathAnimation.cs @@ -0,0 +1,53 @@ +// Copyright (c) 2015-2021 Jean-Philippe Bruyère <jp_bruyere@hotmail.com> +// +// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) + +using System; +using System.Numerics; + +namespace vke +{ + public class PathAnimation : Animation<Vector3> + { + Path path; + int stepCount, currentStep; + + #region CTOR + public PathAnimation(Object instance, string _propertyName, Path _path, int _stepCount = 20) + : base(instance, _propertyName) + { + path = _path; + stepCount = _stepCount; + } + + #endregion + + bool smooth = true; + + float smoothedStep (float step) => (-MathF.Cos (step * MathF.PI) + 1)/2.0f; + + public override void Process() + { + currentStep++; + + float t = (float)currentStep / (float)stepCount; + if (smooth) + t = smoothedStep (t); + + Vector3 pos = path.GetStep (t); + setValue(pos); + + if (currentStep < stepCount) + return; + + AnimationList.Remove(this); + RaiseAnimationFinishedEvent (); + } + + public override string ToString () + { + return string.Format ("{0}:->{1}:{2}",base.ToString(),TargetValue,Step); + } + } +} + diff --git a/Samples/AnimTest/Animation/ShakeAnimation.cs b/Samples/AnimTest/Animation/ShakeAnimation.cs new file mode 100644 index 00000000..37a089bd --- /dev/null +++ b/Samples/AnimTest/Animation/ShakeAnimation.cs @@ -0,0 +1,60 @@ +// Copyright (c) 2015-2021 Jean-Philippe Bruyère <jp_bruyere@hotmail.com> +// +// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) + +using System; + +namespace vke +{ + public class ShakeAnimation : Animation + { + const float stepMin = 0.001f, stepMax = 0.005f; + bool rising = true; + + public float LowBound; + public float HighBound; + + #region CTOR + public ShakeAnimation( + Object instance, + string _propertyName, + float lowBound, float highBound) + : base(instance, _propertyName) + { + + LowBound = Math.Min (lowBound, highBound); + HighBound = Math.Max (lowBound, highBound); + + float value = getValue (); + + if (value > HighBound) + rising = false; + } + #endregion + + public override void Process () + { + float value = getValue (); + float step = stepMin + (float)random.NextDouble () * stepMax; + + if (rising) { + value += step; + if (value > HighBound) { + value = HighBound; + rising = false; + } + } else { + value -= step; + if (value < LowBound) { + value = LowBound; + rising = true; + } else if (value > HighBound) + value -= step * 10f; + } + setValue (value); + } + + } + +} + |