Skip to content

Commit

Permalink
Add TraitCollectionView
Browse files Browse the repository at this point in the history
  • Loading branch information
tylercamp committed Apr 20, 2024
1 parent 763909e commit 7dfd12c
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 20 deletions.
6 changes: 6 additions & 0 deletions PalCalc.UI/Model/GraphSharp/BreedingTreeNodeViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace PalCalc.UI.Model
{
Expand All @@ -19,6 +20,7 @@ public BreedingTreeNodeViewModel(IBreedingTreeNode node)
Value = node;
Pal = new PalViewModel(node.PalRef.Pal);
Traits = node.PalRef.Traits.Select(t => new TraitViewModel(t)).ToList();
TraitCollection = new TraitCollectionViewModel(Traits);
Location = new PalLocationViewModel(node.PalRef.Location);
Gender = node.PalRef.Gender.ToString();
}
Expand All @@ -29,6 +31,10 @@ public BreedingTreeNodeViewModel(IBreedingTreeNode node)

public List<TraitViewModel> Traits { get; }

public TraitCollectionViewModel TraitCollection { get; }

public Visibility TraitsVisibility => Traits.Count > 0 ? Visibility.Visible : Visibility.Collapsed;

public PalLocationViewModel Location { get; }

public string Gender { get; }
Expand Down
6 changes: 6 additions & 0 deletions PalCalc.UI/PalCalc.UI.csproj.user
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
<Compile Update="View\SolverControlsView.xaml.cs">
<SubType>Code</SubType>
</Compile>
<Compile Update="View\TraitCollectionView.xaml.cs">
<SubType>Code</SubType>
</Compile>
<Compile Update="View\TraitView.xaml.cs">
<SubType>Code</SubType>
</Compile>
Expand Down Expand Up @@ -57,6 +60,9 @@
<Page Update="View\SolverControlsView.xaml">
<SubType>Designer</SubType>
</Page>
<Page Update="View\TraitCollectionView.xaml">
<SubType>Designer</SubType>
</Page>
<Page Update="View\TraitView.xaml">
<SubType>Designer</SubType>
</Page>
Expand Down
30 changes: 14 additions & 16 deletions PalCalc.UI/View/BreedingResultView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,21 @@
>
<UserControl.Resources>
<DataTemplate x:Key="DemoTemplate" DataType="{x:Type m:BreedingTreeNodeViewModel}">
<StackPanel Orientation="Horizontal" Margin="5">
<Border Width="50" Height="50" CornerRadius="25" BorderThickness="1" BorderBrush="White" Margin="0,0,5,0">
<Border.Background>
<ImageBrush Stretch="Fill" ImageSource="{Binding Pal.Icon}" />
</Border.Background>
</Border>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Gender}" Foreground="White" />
<TextBlock Text="{Binding Location.Description}" Foreground="White" />
<ItemsControl ItemsSource="{Binding Traits}" Background="Black" Foreground="Black" BorderThickness="0" Margin="0,0,0,0">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="vm:TraitViewModel">
<local:TraitView DataContext="{Binding}" MinWidth="100" Margin="0,2,0,0" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<StackPanel Orientation="Vertical" Margin="5" MinWidth="150">
<StackPanel Orientation="Horizontal">
<Border Width="50" Height="50" CornerRadius="25" BorderThickness="1" BorderBrush="White" Margin="0,0,5,0">
<Border.Background>
<ImageBrush Stretch="Fill" ImageSource="{Binding Pal.Icon}" />
</Border.Background>
</Border>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Gender}" Foreground="White" />
<TextBlock Text="{Binding Location.Description}" Foreground="White" />
</StackPanel>
</StackPanel>
<Grid Visibility="{Binding TraitsVisibility}">
<local:TraitCollectionView Margin="0,5,0,0" DataContext="{Binding TraitCollection}" Width="Auto" Background="Black" />
</Grid>
</StackPanel>
</DataTemplate>

Expand Down
11 changes: 11 additions & 0 deletions PalCalc.UI/View/TraitCollectionView.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Grid x:Class="PalCalc.UI.View.TraitCollectionView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:PalCalc.UI.View"
xmlns:vm="clr-namespace:PalCalc.UI.ViewModel"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance vm:TraitCollectionViewModel, IsDesignTimeCreatable=True}"
d:DesignHeight="450" d:DesignWidth="800">
</Grid>
78 changes: 78 additions & 0 deletions PalCalc.UI/View/TraitCollectionView.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using PalCalc.UI.ViewModel;
using QuickGraph;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace PalCalc.UI.View
{
/// <summary>
/// Interaction logic for TraitCollectionView.xaml
/// </summary>
public partial class TraitCollectionView : Grid
{
public TraitCollectionView()
{
InitializeComponent();

DataContextChanged += TraitCollectionView_DataContextChanged;
}

protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
{
base.OnRenderSizeChanged(sizeInfo);

var vm = DataContext as TraitCollectionViewModel;
if (vm == null) return;

foreach (var child in Children)
{
var traitView = child as TraitView;
if (traitView == null) continue;

// force child sizing (otherwise the '*' column sizing doesn't stay proportional)
traitView.Width = (ActualWidth - vm.Spacing) / 2;
}
}

private void TraitCollectionView_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
var newModel = e.NewValue as TraitCollectionViewModel;
if (newModel == null) return;

Children.Clear();

ColumnDefinitions.Clear();
foreach (var cdef in newModel.ColumnDefinitions)
ColumnDefinitions.Add(cdef);

RowDefinitions.Clear();
foreach (var rdef in newModel.RowDefinitions)
RowDefinitions.Add(rdef);



foreach (var vm in newModel.Traits)
{
var traitView = new TraitView();
traitView.DataContext = vm;

Grid.SetRow(traitView, newModel.RowIndexOf(vm));
Grid.SetColumn(traitView, newModel.ColumnIndexOf(vm));

Children.Add(traitView);
}
}
}
}
9 changes: 5 additions & 4 deletions PalCalc.UI/View/TraitView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:PalCalc.UI.View"
xmlns:vm="clr-namespace:PalCalc.UI.ViewModel"
d:Width="40"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance vm:TraitViewModel, IsDesignTimeCreatable=True}">
<Grid>
Expand All @@ -24,15 +25,15 @@
</Border>
<Grid Grid.Column="1" Margin="5,2,2,2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="5" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>

<Viewbox StretchDirection="DownOnly" Stretch="Uniform">
<TextBlock Grid.Column="0" Text="{Binding Name}" Height="14" FontSize="10" VerticalAlignment="Center" Foreground="White" />
<Viewbox Grid.Column="0" StretchDirection="DownOnly" Stretch="Uniform" HorizontalAlignment="Left">
<TextBlock Text="{Binding Name}" Height="14" FontSize="10" VerticalAlignment="Center" Foreground="White" />
</Viewbox>
<Grid Grid.Column="2" Margin="10,0,0,0">
<Grid Grid.Column="2">
<Rectangle Width="12" Height="12">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding RankColor}" />
Expand Down
79 changes: 79 additions & 0 deletions PalCalc.UI/ViewModel/TraitCollectionViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.Eventing.Reader;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace PalCalc.UI.ViewModel
{
public class TraitCollectionViewModel
{
// for XAML designer view
public TraitCollectionViewModel() : this(
new List<TraitViewModel>()
{
new TraitViewModel(),
new TraitViewModel(),
new TraitViewModel(),
})
{
}

public TraitCollectionViewModel(List<TraitViewModel> traits)
{
Traits = traits;

RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
for (int i = 1; i < NumRows; i++)
{
if (i % 2 == 1)
{
RowDefinitions.Add(new RowDefinition() { Height = new GridLength(Spacing) });
}
else
{
RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
}
}

ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });
ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(Spacing) });
ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });
}

public List<TraitViewModel> Traits { get; }

public int Spacing => 3;

public int EntriesPerRow => 2;

public int NumRows
{
get
{
if (Traits.Count <= 2) return 1;
else return 3;
}
}

public List<RowDefinition> RowDefinitions { get; } = new List<RowDefinition>();
public List<ColumnDefinition> ColumnDefinitions { get; } = new List<ColumnDefinition>();

public int RowIndexOf(TraitViewModel trait)
{
var mainRow = Traits.IndexOf(trait) / EntriesPerRow;
if (mainRow == 0) return mainRow;
else return mainRow + 1;
}

public int ColumnIndexOf(TraitViewModel trait)
{
var mainColumn = Traits.IndexOf(trait) % EntriesPerRow;
if (mainColumn == 0) return mainColumn;
else return mainColumn + 1;
}
}
}

0 comments on commit 7dfd12c

Please sign in to comment.