Skip to content

Commit 1d8041b

Browse files
committed
Replaced IsVisible with Style-Classes for Avalonia
1 parent 7742a2f commit 1d8041b

3 files changed

Lines changed: 87 additions & 14 deletions

File tree

src/CodeBreaker.Avalonia/CodeBreaker.Avalonia/Views/Components/GameResultDisplay.axaml

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,37 @@
1111
<UserControl.Resources>
1212
<Converter:GameStatusToBooleanConverter x:Key="GameStatusToBooleanConverter" />
1313
</UserControl.Resources>
14-
<Grid>
15-
<StackPanel Orientation="Vertical"
16-
IsVisible="{Binding GameStatus, Mode=OneWay, Converter={StaticResource GameStatusToBooleanConverter}, ConverterParameter=Won}">
14+
<UserControl.Styles>
15+
<Style Selector="Grid > StackPanel">
16+
<Setter Property="IsVisible" Value="False" />
17+
</Style>
18+
<Style Selector=".won">
19+
<Style Selector="^ StackPanel#WonPanel">
20+
<Setter Property="IsVisible" Value="True" />
21+
</Style>
22+
<Style Selector="^ StackPanel#LostPanel">
23+
<Setter Property="IsVisible" Value="False" />
24+
</Style>
25+
</Style>
26+
<Style Selector=".lost">
27+
<Style Selector="^ StackPanel#WonPanel">
28+
<Setter Property="IsVisible" Value="False" />
29+
</Style>
30+
<Style Selector="^ StackPanel#LostPanel">
31+
<Setter Property="IsVisible" Value="True" />
32+
</Style>
33+
</Style>
34+
</UserControl.Styles>
35+
<Grid
36+
x:Name="Wrapper"
37+
Classes.won="{Binding GameStatus, Mode=OneWay, Converter={StaticResource GameStatusToBooleanConverter}, ConverterParameter=Won}"
38+
Classes.lost="{Binding GameStatus, Mode=OneWay, Converter={StaticResource GameStatusToBooleanConverter}, ConverterParameter=Lost}">
39+
<StackPanel x:Name="WonPanel" Orientation="Vertical">
1740
<gif:GifImage SourceUri="avares://CodeBreaker.Avalonia/Assets/Animations/WonAnimation_300_opt.gif" AutoStart="True" MaxHeight="300" />
1841
<TextBlock Text="Congratulations - you won the game 🎉🏆" FontSize="20" HorizontalAlignment="Center" Margin="0,50,0,20" />
1942
<Button Content="Great, play again" HorizontalAlignment="Center" Margin="0,0,0,50" Command="{Binding StartGameCommand, Mode=OneWay}" />
2043
</StackPanel>
21-
<StackPanel Orientation="Vertical"
22-
IsVisible="{Binding GameStatus, Mode=OneWay, Converter={StaticResource GameStatusToBooleanConverter}, ConverterParameter=Lost}">
44+
<StackPanel x:Name="LostPanel" Orientation="Vertical">
2345
<gif:GifImage SourceUri="avares://CodeBreaker.Avalonia/Assets/Animations/LostAnimation_300_opt.gif" AutoStart="True" MaxHeight="300" />
2446
<TextBlock Text="Oh no - you lost the game 😑" FontSize="20" HorizontalAlignment="Center" Margin="0,50,0,20" />
2547
<Button Content="Ok, try again" HorizontalAlignment="Center" Margin="0,0,0,50" Command="{Binding StartGameCommand, Mode=OneWay}" />

src/CodeBreaker.Avalonia/CodeBreaker.Avalonia/Views/Pages/GamePage.axaml

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,48 @@
1414
<UserControl.Resources>
1515
<converter:GameStatusToBooleanConverter x:Key="GameStatusToBooleanConverter" />
1616
</UserControl.Resources>
17+
<UserControl.Styles>
18+
<Style Selector=".start">
19+
<Style Selector="^ Grid#StartGameArea">
20+
<Setter Property="IsVisible" Value="True" />
21+
</Style>
22+
<Style Selector="^ components|PegSelectionView#PegSelectionArea">
23+
<Setter Property="IsVisible" Value="False" />
24+
</Style>
25+
<Style Selector="^ ScrollViewer#PegScrollViewer">
26+
<Setter Property="IsVisible" Value="False" />
27+
</Style>
28+
</Style>
29+
<Style Selector=".playing">
30+
<Style Selector="^ Grid#StartGameArea">
31+
<Setter Property="IsVisible" Value="False" />
32+
</Style>
33+
<Style Selector="^ components|PegSelectionView#PegSelectionArea">
34+
<Setter Property="IsVisible" Value="True" />
35+
</Style>
36+
<Style Selector="^ ScrollViewer#PegScrollViewer">
37+
<Setter Property="IsVisible" Value="True" />
38+
</Style>
39+
</Style>
40+
<Style Selector=".finished">
41+
<Style Selector="^ Grid#StartGameArea">
42+
<Setter Property="IsVisible" Value="False" />
43+
</Style>
44+
<Style Selector="^ components|PegSelectionView#PegSelectionArea">
45+
<Setter Property="IsVisible" Value="False" />
46+
</Style>
47+
<Style Selector="^ ScrollViewer#PegScrollViewer">
48+
<Setter Property="IsVisible" Value="True" />
49+
</Style>
50+
</Style>
51+
</UserControl.Styles>
1752
<Grid RowDefinitions="Auto,*,Auto">
1853
<components:GameResultDisplay Grid.Row="0" />
19-
<Grid Grid.Row="0"
54+
<Grid x:Name="StartGameArea"
55+
Grid.Row="0"
2056
ColumnDefinitions="3*,1*"
2157
RowDefinitions="Auto,Auto"
22-
Margin="8"
23-
IsVisible="{Binding GameStatus, Mode=OneWay, Converter={StaticResource GameStatusToBooleanConverter}, ConverterParameter=Start}">
58+
Margin="8">
2459
<StackPanel
2560
Grid.Column="0" Grid.Row="0"
2661
Grid.RowSpan="{OnFormFactor 2, Mobile=1}" Grid.ColumnSpan="{OnFormFactor 1, Mobile=2}"
@@ -43,14 +78,13 @@
4378
</Button>
4479
</Grid>
4580
<components:PegSelectionView
81+
x:Name="PegSelectionArea"
4682
Grid.Row="0"
47-
IsVisible="{Binding GameStatus, Mode=OneWay, Converter={StaticResource GameStatusToBooleanConverter}, ConverterParameter=Cancelable}"
4883
Margin="{OnFormFactor '55,0,0,15', Mobile='0,0,0,15'}" />
4984
<ScrollViewer
50-
x:Name="pegScrollViewer"
85+
x:Name="PegScrollViewer"
5186
Padding="0,0,0,15"
52-
Grid.Row="1"
53-
IsVisible="{Binding GameStatus, Mode=OneWay, ConverterParameter=Running}">
87+
Grid.Row="1">
5488
<ItemsControl
5589
ItemsSource="{Binding GameMoves, Mode=OneWay}"
5690
ItemTemplate="{StaticResource PegsTemplate}">

src/CodeBreaker.Avalonia/CodeBreaker.Avalonia/Views/Pages/GamePage.axaml.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public GamePage()
1616
_navigationService = App.GetService<INavigationService>();
1717
InitializeComponent();
1818
WeakReferenceMessenger.Default.Register(this);
19+
ViewModel.PropertyChanged += ViewModel_PropertyChanged;
20+
Classes.Add("start");
1921
}
2022

2123
protected override void OnUnloaded(RoutedEventArgs e)
@@ -30,8 +32,23 @@ public void Receive(GameMoveMessage message)
3032
if (message.GameMoveValue is not GameMoveValue.Completed)
3133
return;
3234

33-
pegScrollViewer.UpdateLayout();
34-
pegScrollViewer.ScrollToEnd();
35+
PegScrollViewer.UpdateLayout();
36+
PegScrollViewer.ScrollToEnd();
37+
}
38+
39+
private void ViewModel_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
40+
{
41+
if (e.PropertyName != nameof(GamePageViewModel.GameStatus))
42+
return;
43+
44+
var stateName = ViewModel.GameStatus switch
45+
{
46+
GameMode.Started or GameMode.MoveSet => "playing",
47+
GameMode.Won or GameMode.Lost => "finished",
48+
_ => "start",
49+
};
50+
Classes.Clear();
51+
Classes.Add(stateName);
3552
}
3653

3754
private void ToTestPageButtonClicked(object? sender, RoutedEventArgs e)

0 commit comments

Comments
 (0)