Skip to content

Commit 1040ee9

Browse files
committed
Added connected animation to game page
1 parent 205cd97 commit 1040ee9

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

src/Codebreaker.WinUI/Views/Components/PegSelectionComponent.xaml.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
using Codebreaker.ViewModels;
2+
using CommunityToolkit.Mvvm.Messaging;
3+
using Microsoft.UI.Xaml.Media.Animation;
24

35
namespace CodeBreaker.WinUI.Views.Components;
46

5-
internal sealed partial class PegSelectionComponent : UserControl
7+
internal sealed partial class PegSelectionComponent : UserControl, IRecipient<GameMoveMessage>
68
{
79
public PegSelectionComponent()
810
{
911
InitializeComponent();
12+
WeakReferenceMessenger.Default.Register(this);
13+
WeakReferenceMessenger.Default.UnregisterAllOnUnloaded(this);
1014
}
1115

1216
public GamePageViewModel ViewModel
@@ -22,4 +26,13 @@ public GamePageViewModel ViewModel
2226
public static readonly DependencyProperty ViewModelProperty =
2327
DependencyProperty.Register("ViewModel", typeof(GamePageViewModel), typeof(PegSelectionComponent), new PropertyMetadata(null));
2428

29+
public void Receive(GameMoveMessage message)
30+
{
31+
if (message.GameMoveValue is not GameMoveValue.Started)
32+
return;
33+
34+
var animationService = ConnectedAnimationService.GetForCurrentView();
35+
this.FindItemsOfType<ComboBox>(this)
36+
.Foreach((comboBox, i) => animationService.PrepareToAnimate($"guess{i}", comboBox));
37+
}
2538
}
Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,45 @@
11
using Codebreaker.ViewModels;
2+
using CommunityToolkit.Mvvm.Messaging;
3+
using Microsoft.UI.Xaml.Media.Animation;
4+
using Microsoft.UI.Xaml.Shapes;
25

36
namespace CodeBreaker.WinUI.Views.Pages;
47

58
/// <summary>
69
/// An empty page that can be used on its own or navigated to within a Frame.
710
/// </summary>
8-
public sealed partial class GamePage : Page
11+
public sealed partial class GamePage : Page, IRecipient<GameMoveMessage>
912
{
1013
public GamePageViewModel ViewModel { get; }
1114

1215
public GamePage()
1316
{
1417
ViewModel = App.GetService<GamePageViewModel>();
1518
InitializeComponent();
19+
WeakReferenceMessenger.Default.Register(this);
20+
WeakReferenceMessenger.Default.UnregisterAllOnUnloaded(this);
21+
}
22+
23+
public void Receive(GameMoveMessage message)
24+
{
25+
if (message.GameMoveValue is not GameMoveValue.Completed)
26+
return;
27+
28+
var selectionAndKeyPegs = message.SelectionAndKeyPegs ?? throw new InvalidOperationException();
29+
var animationService = ConnectedAnimationService.GetForCurrentView();
30+
animationService.DefaultDuration = TimeSpan.FromMilliseconds(500);
31+
var container = listGameMoves.ItemContainerGenerator.ContainerFromItem(selectionAndKeyPegs);
32+
this.FindItemsOfType<Ellipse>(container)
33+
.Foreach((ellipse, i) =>
34+
{
35+
ConnectedAnimation? animation = animationService.GetAnimation($"guess{i}");
36+
37+
// No animation found for this ellipxe -> the ellipse is most likely a key-peg
38+
if (animation is null)
39+
return;
40+
41+
animation.Configuration = new BasicConnectedAnimationConfiguration();
42+
animation.TryStart(ellipse);
43+
});
1644
}
1745
}

0 commit comments

Comments
 (0)