# HG changeset patch # User Jason Maltzen # Date 2023-12-03 06:01:47 # Node ID 3737f942b229ded8343659e8b1e7440897657b5c # Parent 70b1de28b2a296d25a7897fab0c2fc3497de069f Simulator view now uses a new recipe view element instead of the old content element. diff --git a/ViewModels/SimulatorViewModel.cs b/ViewModels/SimulatorViewModel.cs --- a/ViewModels/SimulatorViewModel.cs +++ b/ViewModels/SimulatorViewModel.cs @@ -29,8 +29,8 @@ namespace DesertPaintCodex.ViewModels } // Stealing the recipe view from the paint generator - private GeneratorRecipe? _existingRecipe; - public GeneratorRecipe? ExistingRecipe { get => _existingRecipe; private set => this.RaiseAndSetIfChanged(ref _existingRecipe, value); } + private PaintRecipe? _existingRecipe; + public PaintRecipe? ExistingRecipe { get => _existingRecipe; private set => this.RaiseAndSetIfChanged(ref _existingRecipe, value); } private bool _hasMissingReactions; public bool HasMissingReactions @@ -171,19 +171,9 @@ namespace DesertPaintCodex.ViewModels { System.Diagnostics.Debug.WriteLine($"Setting existing recipe for {colorName}."); - GeneratorRecipe? existingRecipe = ExistingRecipe; - if (existingRecipe == null) - { - _tempColor.Set(colorName, _currentRecipe.ReactedColor); - existingRecipe = new GeneratorRecipe(_tempColor); - } - else - { - existingRecipe.Color.Set(colorName, _currentRecipe.ReactedColor); - } - existingRecipe.DraftRecipe(paintRecipe); + PaintRecipe? existingRecipe = ExistingRecipe; ExistingRecipe = null; - ExistingRecipe = existingRecipe; + ExistingRecipe = _currentRecipe; } else { diff --git a/Views/RecipeView.axaml b/Views/RecipeView.axaml new file mode 100644 --- /dev/null +++ b/Views/RecipeView.axaml @@ -0,0 +1,27 @@ + + + + + + + + + + + Black + Cost: 0 + + None + + + + diff --git a/Views/RecipeView.axaml.cs b/Views/RecipeView.axaml.cs new file mode 100644 --- /dev/null +++ b/Views/RecipeView.axaml.cs @@ -0,0 +1,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 ColorProperty = AvaloniaProperty.RegisterDirect(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 IngredientsProperty = + AvaloniaProperty.Register(nameof(Ingredients)); + private string _ingredients = string.Empty; + public string Ingredients { get => _ingredients; set => this.SetAndRaise(IngredientsProperty, ref _ingredients, value); } + + public static readonly DirectProperty RecipeProperty = + AvaloniaProperty.RegisterDirect(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("ColorSwatch"); + _ingredientList = this.FindControl("Ingredients"); + _colorName = this.FindControl("ColorName"); + _cost = this.FindControl("Cost"); + ColorProperty.Changed.AddClassHandler((x, _) => x.UpdateColor()); + RecipeProperty.Changed.AddClassHandler((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(); + } + + } +} diff --git a/Views/SimulatorView.axaml b/Views/SimulatorView.axaml --- a/Views/SimulatorView.axaml +++ b/Views/SimulatorView.axaml @@ -46,7 +46,7 @@ CURRENT RECIPE - +