From 6a34bda91807d46ca340ede7bdc8f5f07f2c2a14 Mon Sep 17 00:00:00 2001 From: Tyler Camp Date: Sat, 20 Apr 2024 11:43:05 -0400 Subject: [PATCH] Add trait icon lookup, handle image lookup in XAML Designer --- .../GraphSharp/BreedingTreeNodeViewModel.cs | 14 ++++-- PalCalc.UI/Model/PalIcon.cs | 8 ++-- PalCalc.UI/Model/ResourceLookup.cs | 26 ++++++++++ PalCalc.UI/Model/TraitIcon.cs | 47 +++++++++++++++++++ PalCalc.UI/ViewModel/MappedViewModels.cs | 26 ++++++++++ 5 files changed, 111 insertions(+), 10 deletions(-) create mode 100644 PalCalc.UI/Model/ResourceLookup.cs create mode 100644 PalCalc.UI/Model/TraitIcon.cs diff --git a/PalCalc.UI/Model/GraphSharp/BreedingTreeNodeViewModel.cs b/PalCalc.UI/Model/GraphSharp/BreedingTreeNodeViewModel.cs index 99a998e7..c8e5d623 100644 --- a/PalCalc.UI/Model/GraphSharp/BreedingTreeNodeViewModel.cs +++ b/PalCalc.UI/Model/GraphSharp/BreedingTreeNodeViewModel.cs @@ -18,15 +18,19 @@ public BreedingTreeNodeViewModel(IBreedingTreeNode node) { Value = node; Pal = new PalViewModel(node.PalRef.Pal); + Traits = node.PalRef.Traits.Select(t => new TraitViewModel(t)).ToList(); + Location = new PalLocationViewModel(node.PalRef.Location); + Gender = node.PalRef.Gender.ToString(); } public PalViewModel Pal { get; } - [ObservableProperty] - [NotifyPropertyChangedFor(nameof(Traits))] - private IBreedingTreeNode value = null; + public IBreedingTreeNode Value { get; } - [ObservableProperty] - private ObservableCollection traits = new ObservableCollection(); + public List Traits { get; } + + public PalLocationViewModel Location { get; } + + public string Gender { get; } } } diff --git a/PalCalc.UI/Model/PalIcon.cs b/PalCalc.UI/Model/PalIcon.cs index 5ef67dde..6d0b65b1 100644 --- a/PalCalc.UI/Model/PalIcon.cs +++ b/PalCalc.UI/Model/PalIcon.cs @@ -17,7 +17,7 @@ internal static class PalIcon { private static Dictionary GetIconOverrides() { - using (var stream = Application.GetResourceStream(new Uri("/Resources/PalIconOverride.json", UriKind.Relative)).Stream) + using (var stream = ResourceLookup.Get("PalIconOverride.json")) using (var reader = new StreamReader(stream)) { return JsonConvert.DeserializeObject>(reader.ReadToEnd()); @@ -28,15 +28,13 @@ private static Stream IconStream(string iconName) { try { - var uri = new Uri($"/Resources/Pals/{iconName}", UriKind.Relative); - return Application.GetResourceStream(uri).Stream; + return ResourceLookup.Get($"Pals/{iconName}"); } catch (IOException) { // fallback for pals with missing icons // TODO - log - var uri = new Uri("/Resources/Pals/Human.png", UriKind.Relative); - return Application.GetResourceStream(uri).Stream; + return ResourceLookup.Get("Pals/Human.png"); } } diff --git a/PalCalc.UI/Model/ResourceLookup.cs b/PalCalc.UI/Model/ResourceLookup.cs new file mode 100644 index 00000000..bdde1e72 --- /dev/null +++ b/PalCalc.UI/Model/ResourceLookup.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; + +namespace PalCalc.UI.Model +{ + internal static class ResourceLookup + { + public static Stream Get(string pathInResources) + { + if (DesignerProperties.GetIsInDesignMode(new DependencyObject())) + { + return File.OpenRead($"PalCalc.UI/Resources/{pathInResources}"); + } + else + { + return Application.GetResourceStream(new Uri($"/Resources/{pathInResources}", UriKind.Relative)).Stream; + } + } + } +} diff --git a/PalCalc.UI/Model/TraitIcon.cs b/PalCalc.UI/Model/TraitIcon.cs new file mode 100644 index 00000000..26306d99 --- /dev/null +++ b/PalCalc.UI/Model/TraitIcon.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Media; +using System.Windows.Media.Imaging; + +namespace PalCalc.UI.Model +{ + internal static class TraitIcon + { + private static Dictionary images; + public static Dictionary Images + { + get + { + if (images == null) + { + Stream IconStream(string iconName) => ResourceLookup.Get($"TraitRank/{iconName}"); + ImageSource IconImage(string iconName) + { + var source = new BitmapImage(); + source.BeginInit(); + source.StreamSource = IconStream(iconName); + source.EndInit(); + return source; + } + + Images.Add(-3, IconImage("Passive_Negative_3_icon.png")); + Images.Add(-2, IconImage("Passive_Negative_2_icon.png")); + Images.Add(-1, IconImage("Passive_Negative_1_icon.png")); + + Images.Add(0, IconImage("Passive_Positive_1_icon.png")); + + Images.Add(1, IconImage("Passive_Positive_1_icon.png")); + Images.Add(2, IconImage("Passive_Positive_2_icon.png")); + Images.Add(3, IconImage("Passive_Positive_3_icon.png")); + } + + return images; + } + } + } +} diff --git a/PalCalc.UI/ViewModel/MappedViewModels.cs b/PalCalc.UI/ViewModel/MappedViewModels.cs index b2c4ae85..91d7a5e2 100644 --- a/PalCalc.UI/ViewModel/MappedViewModels.cs +++ b/PalCalc.UI/ViewModel/MappedViewModels.cs @@ -73,8 +73,31 @@ public PalViewModel(Pal pal) public override int GetHashCode() => ModelObject.GetHashCode(); } + public class PalLocationViewModel + { + public PalLocationViewModel(IPalRefLocation location) + { + ModelObject = location; + } + + public IPalRefLocation ModelObject { get; } + + public string Description => ModelObject.ToString(); + } + public class TraitViewModel { + public static Dictionary RankColors = new Dictionary() + { + { -3, new Color() { R = 247, G = 63, B = 63, A = 255 } }, + { -2, new Color() { R = 247, G = 63, B = 63, A = 255 } }, + { -1, new Color() { R = 247, G = 63, B = 63, A = 255 } }, + { 0, new Color() { R = 230, G = 231, B = 223, A = 255 } }, + { 1, new Color() { R = 230, G = 231, B = 223, A = 255 } }, + { 2, new Color() { R = 255, G = 221, B = 0, A = 255 } }, + { 3, new Color() { R = 255, G = 221, B = 0, A = 255 } }, + }; + public TraitViewModel(Trait trait) { ModelObject = trait; @@ -82,6 +105,9 @@ public TraitViewModel(Trait trait) public Trait ModelObject { get; } + public ImageSource RankIcon => TraitIcon.Images[ModelObject.Rank]; + public Color RankColor => RankColors[ModelObject.Rank]; + public string Name => ModelObject?.Name ?? "None"; public override bool Equals(object obj) => ModelObject.Equals((obj as TraitViewModel)?.ModelObject);