PageRenderTime 186ms CodeModel.GetById 36ms RepoModel.GetById 7ms app.codeStats 0ms

/Hearthstone Deck Tracker/Controls/ManaCostBar.xaml.cs

https://github.com/anonymous555/Hearthstone-Deck-Tracker
C# | 143 lines | 120 code | 19 blank | 4 comment | 23 complexity | 45af43458c58706b21fabd54a5478d2c MD5 | raw file
  1. using System;
  2. using System.Threading.Tasks;
  3. using System.Windows;
  4. using System.Windows.Shapes;
  5. namespace Hearthstone_Deck_Tracker
  6. {
  7. /// <summary>
  8. /// Interaction logic for MultiProgressBar.xaml
  9. /// </summary>
  10. public partial class ManaCostBar
  11. {
  12. private readonly Rectangle[] _bars;
  13. private readonly double[] _previousBarHeights;
  14. private bool _cancelCurrentAnimation;
  15. private bool _isAnimationRunning;
  16. private double[] _nextAnimation;
  17. public ManaCostBar()
  18. {
  19. InitializeComponent();
  20. _previousBarHeights = new[] {0.0, 0.0, 0.0};
  21. _bars = new[] {WeaponsRect, SpellsRect, MinionsRect};
  22. AnimationDuration = 300;
  23. FrameDelay = 20;
  24. _nextAnimation = new double[3];
  25. _isAnimationRunning = false;
  26. McbToolTip.SetValue(DataContextProperty, this);
  27. }
  28. public int AnimationDuration { get; set; }
  29. public int FrameDelay { get; set; }
  30. public void SetTooltipValues(int weapons, int spells, int minions)
  31. {
  32. McbToolTip.TextblockWeapons.Text = "Weapons: " + weapons;
  33. McbToolTip.TextblockSpells.Text = "Spells: " + spells;
  34. McbToolTip.TextblockMinions.Text = "Minions: " + minions;
  35. }
  36. public void SetValues(double weapons, double spells, double minions, int count)
  37. {
  38. LabelCount.Content = count;
  39. _nextAnimation = new[] {ActualHeight * weapons / 100, ActualHeight * spells / 100, ActualHeight * minions / 100};
  40. if(!_isAnimationRunning)
  41. Animate();
  42. else
  43. _cancelCurrentAnimation = true;
  44. }
  45. private bool AnimateBar(Rectangle bar, double from, double to)
  46. {
  47. if(to > from)
  48. {
  49. if(bar.Height < to)
  50. {
  51. bar.Height += (Math.Abs(from - to) * FrameDelay) / AnimationDuration;
  52. if(bar.Height > to)
  53. {
  54. bar.Height = to;
  55. return true;
  56. }
  57. }
  58. else
  59. {
  60. bar.Height = to;
  61. return true;
  62. }
  63. }
  64. else if(to < from)
  65. {
  66. if(bar.Height > to)
  67. {
  68. var newHeight = bar.Height - (Math.Abs(from - to) * FrameDelay) / AnimationDuration;
  69. if(newHeight < to || newHeight < 0)
  70. {
  71. bar.Height = to;
  72. return true;
  73. }
  74. bar.Height -= (Math.Abs(from - to) * FrameDelay) / AnimationDuration;
  75. }
  76. else
  77. {
  78. bar.Height = to;
  79. return true;
  80. }
  81. }
  82. else
  83. return true;
  84. return false;
  85. }
  86. private async void Animate()
  87. {
  88. _isAnimationRunning = true;
  89. while(_nextAnimation != null)
  90. {
  91. var targetValues = _nextAnimation;
  92. _nextAnimation = null;
  93. bool[] done = {false, false, false};
  94. if(double.IsNaN(targetValues[0]) || targetValues[0] < 0)
  95. targetValues[0] = 0.0;
  96. if(double.IsNaN(targetValues[1]) || targetValues[1] < 0)
  97. targetValues[1] = 0.0;
  98. if(double.IsNaN(targetValues[2]) || targetValues[2] < 0)
  99. targetValues[2] = 0.0;
  100. while(!done[0] || !done[1] || !done[2])
  101. {
  102. if(_cancelCurrentAnimation)
  103. break;
  104. //minions first, weapons last
  105. if(!done[2])
  106. done[2] = AnimateBar(_bars[2], _previousBarHeights[2], targetValues[2]);
  107. if(!done[1])
  108. done[1] = AnimateBar(_bars[1], _previousBarHeights[1], targetValues[1]);
  109. if(!done[0])
  110. done[0] = AnimateBar(_bars[0], _previousBarHeights[0], targetValues[0]);
  111. var offset = _bars[0].ActualHeight + _bars[1].ActualHeight + _bars[2].ActualHeight - 24;
  112. if(offset < -4) offset = -4;
  113. LabelCount.Margin = new Thickness(0, 0, 0, offset);
  114. await Task.Delay(FrameDelay);
  115. }
  116. _cancelCurrentAnimation = false;
  117. _previousBarHeights[0] = _bars[0].Height;
  118. _previousBarHeights[1] = _bars[1].Height;
  119. _previousBarHeights[2] = _bars[2].Height;
  120. }
  121. _isAnimationRunning = false;
  122. }
  123. }
  124. }