Files @ 6a6817b17a06
Branch filter:

Location: ATITD-Tools/Desert-Paint-Codex/Views/PaintSwatchView.axaml.cs - annotation

Jason Maltzen
Simulator view updates: new warning when the recipe is below minimum concentration. Add the missing reactions to the warning about missing reactions. Show the current saved recipe for a color, and allow replacing/saving the current simulated recipe as the recipe for that color.
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.Media;
using DesertPaintCodex.Models;
using DesertPaintCodex.Services;


namespace DesertPaintCodex.Views
{
    public class PaintSwatchView : UserControl
    {
        private static readonly SolidColorBrush NoColor = new();
        public static readonly DirectProperty<PaintSwatchView, PaintColor?> ColorProperty = AvaloniaProperty.RegisterDirect<PaintSwatchView, 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<bool> ShowNameProperty = AvaloniaProperty.Register<PaintSwatchView, bool>(nameof(ShowName));
        public bool ShowName { get => GetValue(ShowNameProperty); set => SetValue(ShowNameProperty, value); }

        private readonly Border    _colorSwatch;
        private readonly TextBlock _hexCode;
        private readonly TextBlock _colorName;

        
        public PaintSwatchView()
        {
            InitializeComponent();
            _colorSwatch = this.FindControl<Border>("ColorSwatch");
            _hexCode = this.FindControl<TextBlock>("HexCode");
            _colorName = this.FindControl<TextBlock>("ColorName");
            ColorProperty.Changed.AddClassHandler<PaintSwatchView>((x, _) => x.UpdateColor());
            ShowNameProperty.Changed.AddClassHandler<PaintSwatchView>((x, _) => x.UpdateColorName());
            UpdateColor();
        }

        private void InitializeComponent()
        {
            AvaloniaXamlLoader.Load(this);
        }

        private void UpdateColor()
        {
            UpdateColorSwatch();
            UpdateHexCode();
            UpdateColorName();
        }

        private void UpdateColorSwatch()
        {
            _colorSwatch.Background = Color == null ? NoColor : new SolidColorBrush(new Color(0xFF, Color.Red, Color.Green, Color.Blue));
        }
        
        private void UpdateHexCode()
        {
            _hexCode.Text = Color?.ToHexString() ?? string.Empty;
        }
        
        private void UpdateColorName()
        {
            if (!ShowName)
            {
                _colorName.IsVisible = false;
            }
            else
            {
                _colorName.IsVisible = true;
                _colorName.Text = Color == null ? "[Unknown]" : PaletteService.FindNearest(Color);
            }
        }
    }
}