Skip to content

Commit

Permalink
Add trait icon lookup, handle image lookup in XAML Designer
Browse files Browse the repository at this point in the history
  • Loading branch information
tylercamp committed Apr 20, 2024
1 parent c52cbe4 commit 6a34bda
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 10 deletions.
14 changes: 9 additions & 5 deletions PalCalc.UI/Model/GraphSharp/BreedingTreeNodeViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TraitViewModel> traits = new ObservableCollection<TraitViewModel>();
public List<TraitViewModel> Traits { get; }

public PalLocationViewModel Location { get; }

public string Gender { get; }
}
}
8 changes: 3 additions & 5 deletions PalCalc.UI/Model/PalIcon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal static class PalIcon
{
private static Dictionary<string, string> 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<Dictionary<string, string>>(reader.ReadToEnd());
Expand All @@ -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");
}
}

Expand Down
26 changes: 26 additions & 0 deletions PalCalc.UI/Model/ResourceLookup.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
47 changes: 47 additions & 0 deletions PalCalc.UI/Model/TraitIcon.cs
Original file line number Diff line number Diff line change
@@ -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<int, ImageSource> images;
public static Dictionary<int, ImageSource> 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;
}
}
}
}
26 changes: 26 additions & 0 deletions PalCalc.UI/ViewModel/MappedViewModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,41 @@ 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<int, Color> RankColors = new Dictionary<int, Color>()
{
{ -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;
}

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);
Expand Down

0 comments on commit 6a34bda

Please sign in to comment.