Skip to content

Commit 0b34800

Browse files
committed
Added VisualStateManager to GameResultDisplay
1 parent bc78bd3 commit 0b34800

2 files changed

Lines changed: 55 additions & 9 deletions

File tree

src/Codebreaker.WinUI/Views/Components/GameResultDisplay.xaml

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,42 @@
2121
</animations:ImplicitAnimationSet>
2222
</UserControl.Resources>
2323
<Grid>
24-
<StackPanel Orientation="Vertical"
25-
Visibility="{x:Bind ViewModel.GameStatus, Mode=OneWay, Converter={StaticResource GameStatusToVisibilityConverter}, ConverterParameter=Won, FallbackValue=Collapsed}"
26-
animations:Implicit.ShowAnimations="{StaticResource EntranceAnimation}"
27-
animations:Implicit.HideAnimations="{StaticResource ExitAnimation}">
24+
<VisualStateManager.VisualStateGroups>
25+
<VisualStateGroup>
26+
<VisualState x:Name="Default">
27+
<VisualState.Setters>
28+
<Setter Target="WonPanel.Visibility" Value="Collapsed" />
29+
<Setter Target="LostPanel.Visibility" Value="Collapsed" />
30+
</VisualState.Setters>
31+
</VisualState>
32+
<VisualState x:Name="Won">
33+
<VisualState.Setters>
34+
<Setter Target="WonPanel.Visibility" Value="Visible" />
35+
<Setter Target="LostPanel.Visibility" Value="Collapsed" />
36+
</VisualState.Setters>
37+
</VisualState>
38+
<VisualState x:Name="Lost">
39+
<VisualState.Setters>
40+
<Setter Target="WonPanel.Visibility" Value="Collapsed" />
41+
<Setter Target="LostPanel.Visibility" Value="Visible" />
42+
</VisualState.Setters>
43+
</VisualState>
44+
</VisualStateGroup>
45+
</VisualStateManager.VisualStateGroups>
46+
<StackPanel
47+
x:Name="WonPanel"
48+
Orientation="Vertical"
49+
animations:Implicit.ShowAnimations="{StaticResource EntranceAnimation}"
50+
animations:Implicit.HideAnimations="{StaticResource ExitAnimation}">
2851
<Image Source="ms-appx:///Assets/Animations/WonAnimation_300_opt.gif" MaxHeight="300" />
2952
<TextBlock Text="{cme:ResourceString Name=GamePage_WonMessage}" FontSize="{ThemeResource TextStyleExtraLargeFontSize}" HorizontalAlignment="Center" Margin="0,50,0,20" />
3053
<Button Content="{cme:ResourceString Name=GamePage_WonButtonText}" HorizontalAlignment="Center" Margin="0,0,0,50" Command="{x:Bind ViewModel.StartGameCommand, Mode=OneTime}" />
3154
</StackPanel>
32-
<StackPanel Orientation="Vertical"
33-
Visibility="{x:Bind ViewModel.GameStatus, Mode=OneWay, Converter={StaticResource GameStatusToVisibilityConverter}, ConverterParameter=Lost, FallbackValue=Collapsed}"
34-
animations:Implicit.ShowAnimations="{StaticResource EntranceAnimation}"
35-
animations:Implicit.HideAnimations="{StaticResource ExitAnimation}">
55+
<StackPanel
56+
x:Name="LostPanel"
57+
Orientation="Vertical"
58+
animations:Implicit.ShowAnimations="{StaticResource EntranceAnimation}"
59+
animations:Implicit.HideAnimations="{StaticResource ExitAnimation}">
3660
<Image Source="ms-appx:///Assets/Animations/LostAnimation_300_opt.gif" MaxHeight="300" />
3761
<TextBlock Text="{cme:ResourceString Name=GamePage_LostMessage}" FontSize="{ThemeResource TextStyleExtraLargeFontSize}" HorizontalAlignment="Center" Margin="0,50,0,20" />
3862
<Button Content="{cme:ResourceString Name=GamePage_LostButtonText}" HorizontalAlignment="Center" Margin="0,0,0,50" Command="{x:Bind ViewModel.StartGameCommand, Mode=OneTime}" />

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Codebreaker.ViewModels;
2+
using System.ComponentModel;
23

34
namespace CodeBreaker.WinUI.Views.Components;
45

@@ -7,6 +8,7 @@ public sealed partial class GameResultDisplay : UserControl
78
public GameResultDisplay()
89
{
910
InitializeComponent();
11+
VisualStateManager.GoToState(this, "Default", false);
1012
}
1113

1214
public GamePageViewModel ViewModel
@@ -16,5 +18,25 @@ public GamePageViewModel ViewModel
1618
}
1719

1820
public static readonly DependencyProperty ViewModelProperty =
19-
DependencyProperty.Register(nameof(ViewModel), typeof(GamePageViewModel), typeof(GameResultDisplay), new PropertyMetadata(null));
21+
DependencyProperty.Register(nameof(ViewModel), typeof(GamePageViewModel), typeof(GameResultDisplay), new PropertyMetadata(null, OnViewModelChanged));
22+
23+
private static void OnViewModelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
24+
{
25+
var @this = (GameResultDisplay)d;
26+
@this.ViewModel.PropertyChanged += @this.OnViewModelPropertyChanged;
27+
}
28+
29+
private void OnViewModelPropertyChanged(object? sender, PropertyChangedEventArgs args)
30+
{
31+
if (args.PropertyName != nameof(GamePageViewModel.GameStatus))
32+
return;
33+
34+
var stateName = ViewModel.GameStatus switch
35+
{
36+
GameMode.Won => "Won",
37+
GameMode.Lost => "Lost",
38+
_ => "Default"
39+
};
40+
VisualStateManager.GoToState(this, stateName, true);
41+
}
2042
}

0 commit comments

Comments
 (0)