Skip to content

Commit

Permalink
add light animation to progress bar when install game
Browse files Browse the repository at this point in the history
  • Loading branch information
Scighost committed Jul 25, 2023
1 parent 29c1383 commit 3c78330
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Starward/Pages/DownloadGamePage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,11 @@
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>

<ProgressBar MinHeight="20"
<ProgressBar Name="ProgressBar_Download"
MinHeight="20"
Background="#40101010"
CornerRadius="4"
Loaded="ProgressBar_Download_Loaded"
Value="{x:Bind ProgressValue}">
<ProgressBar.Resources>
<x:Double x:Key="ProgressBarTrackHeight">20</x:Double>
Expand Down
58 changes: 58 additions & 0 deletions Starward/Pages/DownloadGamePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
using CommunityToolkit.Mvvm.Input;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.UI;
using Microsoft.UI.Composition;
using Microsoft.UI.Dispatching;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Hosting;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Imaging;
using Microsoft.Windows.AppLifecycle;
Expand All @@ -16,6 +19,7 @@
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Numerics;
using System.Security.Principal;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -240,6 +244,54 @@ private async Task GetBgAsync()
}
}

AmbientLight ambientLight;
PointLight pointLight;
Vector3KeyFrameAnimation pointLightAnimation;


private async void ProgressBar_Download_Loaded(object sender, RoutedEventArgs e)
{
await Task.Delay(1000);
var visual = ElementCompositionPreview.GetElementVisual(ProgressBar_Download);
var com = visual.Compositor;
ambientLight = com.CreateAmbientLight();
ambientLight.Color = Colors.White;
ambientLight.Intensity = 1f;
ambientLight.Targets.Add(visual);

float width = (float)ProgressBar_Download.ActualWidth;
float height = (float)ProgressBar_Download.ActualHeight;

pointLight = com.CreatePointLight();
pointLight.Color = Colors.White;
pointLight.CoordinateSpace = visual;
pointLight.Intensity = 0.8f;
pointLight.Targets.Add(visual);
pointLight.MaxAttenuationCutoff = height * 2;
pointLight.Offset = new Vector3(100, 10, 20);

pointLightAnimation = com.CreateVector3KeyFrameAnimation();
pointLightAnimation.Duration = TimeSpan.FromSeconds(2);
pointLightAnimation.InsertKeyFrame(0.00f, new Vector3(-height * 2, height / 2, height));
pointLightAnimation.InsertKeyFrame(0.25f, new Vector3(-height * 2, height / 2, height));
pointLightAnimation.InsertKeyFrame(0.75f, new Vector3(width + height * 2, height / 2, height), com.CreateLinearEasingFunction());
pointLightAnimation.InsertKeyFrame(1.00f, new Vector3(width + height * 2, height / 2, height), com.CreateLinearEasingFunction());
pointLightAnimation.IterationBehavior = AnimationIterationBehavior.Forever;
StartProgressAnimation();
}


private void StartProgressAnimation()
{
pointLight.StartAnimation(nameof(pointLight.Offset), pointLightAnimation);
}


private void StopProgressAnimation()
{
pointLight.Offset = new Vector3(-1000, 0, 0);
pointLight.StopAnimation(nameof(pointLight.Offset));
}



Expand Down Expand Up @@ -411,6 +463,7 @@ private async void ShowErrorMessage()
ActionButtonIcon = StartIcon;
// 重试
ActionButtonText = Lang.DownloadGamePage_Retry;
StopProgressAnimation();

var dialog = new ContentDialog()
{
Expand Down Expand Up @@ -501,6 +554,7 @@ private void FinishTask()
ActionButtonText = Lang.DownloadGamePage_Finished;
IsProgressStateVisible = false;
ProgressValue = 100;
StopProgressAnimation();
}


Expand Down Expand Up @@ -683,17 +737,21 @@ private async Task ClickActionButtonAsync()
StateText = Lang.DownloadGamePage_DownloadPaused;
SpeedText = null;
RemainTimeText = null;
StopProgressAnimation();
}
if (state is DownloadGameService.DownloadGameState.Prepared)
{
StartProgressAnimation();
_ = DownloadAsync();
}
if (state is DownloadGameService.DownloadGameState.Verified && repairMode)
{
StartProgressAnimation();
_ = DownloadAsync();
}
if (state is DownloadGameService.DownloadGameState.Error)
{
StartProgressAnimation();
_ = PrepareForDownloadAsync();
}
}
Expand Down

0 comments on commit 3c78330

Please sign in to comment.