|
1 | 1 | using Codebreaker.ViewModels; |
| 2 | +using CommunityToolkit.Mvvm.Messaging; |
| 3 | +using Microsoft.UI.Xaml.Media.Animation; |
| 4 | +using Microsoft.UI.Xaml.Shapes; |
2 | 5 |
|
3 | 6 | namespace CodeBreaker.WinUI.Views.Pages; |
4 | 7 |
|
5 | 8 | /// <summary> |
6 | 9 | /// An empty page that can be used on its own or navigated to within a Frame. |
7 | 10 | /// </summary> |
8 | | -public sealed partial class GamePage : Page |
| 11 | +public sealed partial class GamePage : Page, IRecipient<GameMoveMessage> |
9 | 12 | { |
10 | 13 | public GamePageViewModel ViewModel { get; } |
11 | 14 |
|
12 | 15 | public GamePage() |
13 | 16 | { |
14 | 17 | ViewModel = App.GetService<GamePageViewModel>(); |
15 | 18 | 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 | + }); |
16 | 44 | } |
17 | 45 | } |
0 commit comments