Animation.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace ImpulseVision
  7. {
  8. public class Animation
  9. {
  10. public string ID { get; set; }
  11. public float Value;
  12. public float StartValue;
  13. private float targetValue;
  14. public float TargetValue
  15. {
  16. get => targetValue;
  17. set
  18. {
  19. targetValue = value;
  20. Reverse = value < Value ? true : false;
  21. }
  22. }
  23. public float Volume;
  24. public bool Reverse = false;
  25. public AnimationStatus Status { get; set; }
  26. public enum AnimationStatus
  27. {
  28. Requested,
  29. Active,
  30. Completed
  31. }
  32. private float p15, p30, p70, p85;
  33. public int StepDivider = 11;
  34. private float Step()
  35. {
  36. float basicStep = Math.Abs(Volume) / StepDivider; // Math.Abs - превращает числа 0< в >0
  37. float resultStep = 0;
  38. if (Reverse == false)
  39. {
  40. if (Value <= p15 || Value >= p85)
  41. {
  42. resultStep = basicStep / 3.5f;
  43. }
  44. else if (Value <= p30 || Value >= p70)
  45. {
  46. resultStep = basicStep / 2f;
  47. }
  48. else if (Value > p30 && Value < p70)
  49. {
  50. resultStep = basicStep;
  51. }
  52. }
  53. else
  54. {
  55. if (Value >= p15 || Value <= p85)
  56. {
  57. resultStep = basicStep / 3.5f;
  58. }
  59. else if (Value >= p30 || Value <= p70)
  60. {
  61. resultStep = basicStep / 2f;
  62. }
  63. else if (Value < p30 && Value > p70)
  64. {
  65. resultStep = basicStep;
  66. }
  67. }
  68. return Math.Abs(resultStep);
  69. }
  70. private float ValueByPercent(float Percent)
  71. {
  72. float COEFF = Percent / 100;
  73. float VolumeInPercent = Volume * COEFF;
  74. float ValueInPercent = StartValue + VolumeInPercent;
  75. return ValueInPercent;
  76. }
  77. public delegate void ControlMethod();
  78. private ControlMethod InvalidateControl;
  79. public void UpdateFrame()
  80. {
  81. Status = AnimationStatus.Active;
  82. if (Reverse == false)
  83. {
  84. if (Value <= targetValue)
  85. {
  86. Value += Step();
  87. if (Value >= targetValue)
  88. {
  89. Value = targetValue;
  90. Status = AnimationStatus.Completed;
  91. }
  92. }
  93. }
  94. else
  95. {
  96. if (Value >= targetValue)
  97. {
  98. Value -= Step();
  99. if (Value <= targetValue)
  100. {
  101. Value = targetValue;
  102. Status = AnimationStatus.Completed;
  103. }
  104. }
  105. }
  106. InvalidateControl.Invoke();
  107. }
  108. public Animation() { }
  109. public Animation(string ID, ControlMethod InvalidateControl, float Value, float TargetValue)
  110. {
  111. this.ID = ID;
  112. this.InvalidateControl = InvalidateControl;
  113. this.Value = Value;
  114. this.TargetValue = TargetValue;
  115. StartValue = Value;
  116. Volume = TargetValue - Value;
  117. p15 = ValueByPercent(15);
  118. p30 = ValueByPercent(30);
  119. p70 = ValueByPercent(70);
  120. p85 = ValueByPercent(85);
  121. }
  122. }
  123. }