Files @ 7117d2e703c8
Branch filter:

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

Malkyne
Updated scanner for Tale 10, and fixed several bugs.
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);
            }
        }
    }
}