Files @ 70b1de28b2a2
Branch filter:

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

Jason Maltzen
Re-enable the ability to save debug screenshots based on a setting value to help debug reaction capturing. Update the README to correctly reflect the debug.screenshot setting name, location of the settings file, and removal of the old debug menu.
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);
            }
        }
    }
}