From 7dfd12cfc2a9b38a5f70cda43a3617b598abc4c3 Mon Sep 17 00:00:00 2001 From: Tyler Camp Date: Sat, 20 Apr 2024 14:40:01 -0400 Subject: [PATCH] Add TraitCollectionView --- .../GraphSharp/BreedingTreeNodeViewModel.cs | 6 ++ PalCalc.UI/PalCalc.UI.csproj.user | 6 ++ PalCalc.UI/View/BreedingResultView.xaml | 30 ++++--- PalCalc.UI/View/TraitCollectionView.xaml | 11 +++ PalCalc.UI/View/TraitCollectionView.xaml.cs | 78 ++++++++++++++++++ PalCalc.UI/View/TraitView.xaml | 9 ++- .../ViewModel/TraitCollectionViewModel.cs | 79 +++++++++++++++++++ 7 files changed, 199 insertions(+), 20 deletions(-) create mode 100644 PalCalc.UI/View/TraitCollectionView.xaml create mode 100644 PalCalc.UI/View/TraitCollectionView.xaml.cs create mode 100644 PalCalc.UI/ViewModel/TraitCollectionViewModel.cs diff --git a/PalCalc.UI/Model/GraphSharp/BreedingTreeNodeViewModel.cs b/PalCalc.UI/Model/GraphSharp/BreedingTreeNodeViewModel.cs index c8e5d623..4f2cce6d 100644 --- a/PalCalc.UI/Model/GraphSharp/BreedingTreeNodeViewModel.cs +++ b/PalCalc.UI/Model/GraphSharp/BreedingTreeNodeViewModel.cs @@ -8,6 +8,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Windows; namespace PalCalc.UI.Model { @@ -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(); } @@ -29,6 +31,10 @@ public BreedingTreeNodeViewModel(IBreedingTreeNode node) public List Traits { get; } + public TraitCollectionViewModel TraitCollection { get; } + + public Visibility TraitsVisibility => Traits.Count > 0 ? Visibility.Visible : Visibility.Collapsed; + public PalLocationViewModel Location { get; } public string Gender { get; } diff --git a/PalCalc.UI/PalCalc.UI.csproj.user b/PalCalc.UI/PalCalc.UI.csproj.user index 449bc61d..3ad8effc 100644 --- a/PalCalc.UI/PalCalc.UI.csproj.user +++ b/PalCalc.UI/PalCalc.UI.csproj.user @@ -28,6 +28,9 @@ Code + + Code + Code @@ -57,6 +60,9 @@ Designer + + Designer + Designer diff --git a/PalCalc.UI/View/BreedingResultView.xaml b/PalCalc.UI/View/BreedingResultView.xaml index 279e07a3..e31e7f25 100644 --- a/PalCalc.UI/View/BreedingResultView.xaml +++ b/PalCalc.UI/View/BreedingResultView.xaml @@ -15,23 +15,21 @@ > - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + diff --git a/PalCalc.UI/View/TraitCollectionView.xaml b/PalCalc.UI/View/TraitCollectionView.xaml new file mode 100644 index 00000000..8d66de68 --- /dev/null +++ b/PalCalc.UI/View/TraitCollectionView.xaml @@ -0,0 +1,11 @@ + + diff --git a/PalCalc.UI/View/TraitCollectionView.xaml.cs b/PalCalc.UI/View/TraitCollectionView.xaml.cs new file mode 100644 index 00000000..42137c83 --- /dev/null +++ b/PalCalc.UI/View/TraitCollectionView.xaml.cs @@ -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 +{ + /// + /// Interaction logic for TraitCollectionView.xaml + /// + 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); + } + } + } +} diff --git a/PalCalc.UI/View/TraitView.xaml b/PalCalc.UI/View/TraitView.xaml index 4ec403d6..b57a941a 100644 --- a/PalCalc.UI/View/TraitView.xaml +++ b/PalCalc.UI/View/TraitView.xaml @@ -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}"> @@ -24,15 +25,15 @@ - + - - + + - + diff --git a/PalCalc.UI/ViewModel/TraitCollectionViewModel.cs b/PalCalc.UI/ViewModel/TraitCollectionViewModel.cs new file mode 100644 index 00000000..1b90bacf --- /dev/null +++ b/PalCalc.UI/ViewModel/TraitCollectionViewModel.cs @@ -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() + { + new TraitViewModel(), + new TraitViewModel(), + new TraitViewModel(), + }) + { + } + + public TraitCollectionViewModel(List 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 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 RowDefinitions { get; } = new List(); + public List ColumnDefinitions { get; } = new List(); + + 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; + } + } +}