Files
@ abff9027c612
Branch filter:
Location: ATITD-Tools/Desert-Paint-Codex/Views/RecipeView.axaml.cs
abff9027c612
3.9 KiB
text/x-csharp
Make the download URL a link in the README.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.Media;
using DesertPaintCodex.Models;
using DesertPaintCodex.Services;
using System.Text;
namespace DesertPaintCodex.Views
{
public class RecipeView : UserControl
{
private static readonly SolidColorBrush NoColor = new();
public static readonly DirectProperty<RecipeView, PaintColor?> ColorProperty = AvaloniaProperty.RegisterDirect<RecipeView, PaintColor?>(nameof(Color),
o => o.Color,
(o, v) => o.Color = v);
private PaintColor? _color;
public PaintColor? Color { get => _color; set => SetAndRaise(ColorProperty, ref _color, value); }
public static readonly StyledProperty<string> IngredientsProperty =
AvaloniaProperty.Register<RecipeView, string>(nameof(Ingredients));
private string _ingredients = string.Empty;
public string Ingredients { get => _ingredients; set => this.SetAndRaise(IngredientsProperty, ref _ingredients, value); }
public static readonly DirectProperty<RecipeView, PaintRecipe?> RecipeProperty =
AvaloniaProperty.RegisterDirect<RecipeView, PaintRecipe?>(nameof(Recipe),
o => o.Recipe,
(o,v) => o.Recipe = v);
private PaintRecipe? _recipe;
public PaintRecipe? Recipe
{
get => _recipe;
set => this.SetAndRaise(RecipeProperty, ref _recipe, value);
}
private readonly Border _colorSwatch;
private readonly TextBlock _colorName;
private readonly TextBlock _ingredientList;
private readonly TextBlock _cost;
public RecipeView()
{
InitializeComponent();
_colorSwatch = this.FindControl<Border>("ColorSwatch");
_ingredientList = this.FindControl<TextBlock>("Ingredients");
_colorName = this.FindControl<TextBlock>("ColorName");
_cost = this.FindControl<TextBlock>("Cost");
ColorProperty.Changed.AddClassHandler<RecipeView>((x, _) => x.UpdateColor());
RecipeProperty.Changed.AddClassHandler<RecipeView>((x, _) => x.UpdateRecipe());
UpdateRecipe();
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
private void UpdateColor()
{
UpdateColorSwatch();
UpdateColorName();
}
private void UpdateColorSwatch()
{
_colorSwatch.Background = Color == null ? NoColor : new SolidColorBrush(new Color(0xFF, Color.Red, Color.Green, Color.Blue));
}
private void UpdateColorName()
{
_colorName.Text = Color == null ? "[Unknown]" : PaletteService.FindNearest(Color);
}
private void UpdateIngredients()
{
string ingredients = string.Empty;
if (Recipe != null)
{
StringBuilder sb = new();
for (int i = 0; i < Recipe.Reagents.Count; i++)
{
sb.Append(Recipe.Reagents[i].Quantity);
sb.Append(' ');
sb.Append(Recipe.Reagents[i].Name);
if (i != Recipe.Reagents.Count - 1)
{
sb.Append(", ");
}
}
ingredients = sb.ToString();
}
_ingredientList.Text = ingredients;
}
private void UpdateCost()
{
_cost.Text = Recipe == null ? "Cost: 0" : $"Cost: {Recipe.Cost}";
}
private void UpdateRecipe()
{
if (Recipe != null)
{
// Update components from recipe
Color = Recipe.ReactedColor;
}
else
{
Color = null;
}
UpdateIngredients();
UpdateCost();
}
}
}
|