-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
57 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System; | ||
using System.Windows.Controls; | ||
using System.Windows.Threading; | ||
|
||
namespace Artorio | ||
{ | ||
internal class CountTextAnimation | ||
{ | ||
private TextBlock textBlock; | ||
private DispatcherTimer timer; | ||
private float targetValue; | ||
private float currentValue; | ||
private float timeLerp; | ||
private string displayText; | ||
|
||
private const float timeSpeed = 0.03f; | ||
|
||
public CountTextAnimation(TextBlock textBlock, Dispatcher dispatcher, string displayText) | ||
{ | ||
timeLerp = 1f; | ||
this.displayText = displayText; | ||
this.textBlock = textBlock; | ||
timer = new DispatcherTimer(TimeSpan.FromSeconds(0.01), DispatcherPriority.Background, Tick, dispatcher); | ||
} | ||
|
||
public void SetTarget(int value, string displayText = null) | ||
{ | ||
if (displayText != null) | ||
this.displayText = displayText; | ||
|
||
targetValue = value; | ||
timeLerp = 0f; | ||
timer.Start(); | ||
} | ||
|
||
private void Tick(object s, EventArgs e) | ||
{ | ||
if (timeLerp >= 1f) | ||
{ | ||
timer.Stop(); | ||
return; | ||
} | ||
|
||
timeLerp += timeSpeed; | ||
|
||
if (timeLerp > 1f) | ||
timeLerp = 1f; | ||
|
||
currentValue += timeLerp * (targetValue - currentValue); | ||
textBlock.Text = displayText.Replace("#", Math.Round(currentValue).ToString()); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters