XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEEDToolBox

【WinUI】XFE精英危险工具箱

公开
关注 0 Fork 0 Star 0
UTF-8
using Microsoft.UI.Xaml.Automation;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Imaging;
using Windows.System;

namespace XFEEDToolBox.Controls;

/// <summary>One original game image per grade; transparent selected images never overlay an unset image.</summary>
public sealed class EngineeringGradePicker : UserControl
{
    public static readonly DependencyProperty ValueProperty=DependencyProperty.Register(nameof(Value),typeof(double),typeof(EngineeringGradePicker),new PropertyMetadata(-1d,Changed));
    public double Value{get=>(double)GetValue(ValueProperty);set=>SetValue(ValueProperty,value);}
    private readonly Image[] images=new Image[5];
    private readonly BitmapImage selected=new(new Uri("ms-appx:///Assets/GameIcons/EngineeringGradeSelected.png"));
    private readonly BitmapImage empty=new(new Uri("ms-appx:///Assets/GameIcons/EngineeringGradeEmpty.png"));
    public EngineeringGradePicker()
    {
        var panel=new StackPanel{Orientation=Orientation.Horizontal,Spacing=2};
        for(var i=0;i<5;i++)
        {
            // Both original PNGs occupy the same rectangle. Uniform sizing
            // would let their different source aspect ratios change ActualHeight
            // and round their centres to different physical pixels at 150% DPI.
            var grade=i+1;var icon=new Image{Width=20,Height=22,Stretch=Stretch.Fill};images[i]=icon;
            var button=new Button{Content=icon,Width=28,Height=32,MinWidth=0,MinHeight=0,Padding=new(4),BorderThickness=new(0),
                Background=new SolidColorBrush(Microsoft.UI.Colors.Transparent),HorizontalContentAlignment=HorizontalAlignment.Center,VerticalContentAlignment=VerticalAlignment.Center};
            AutomationProperties.SetName(button,$"工程等级 G{grade}");ToolTipService.SetToolTip(button,$"G{grade}");
            button.Click+=(_,_)=>Value=grade;panel.Children.Add(button);
        }
        Content=panel;KeyDown+=HandleKey;Update();
    }
    private static void Changed(DependencyObject sender,DependencyPropertyChangedEventArgs args)=>((EngineeringGradePicker)sender).Update();
    private void Update()
    {
        var grade=double.IsFinite(Value)?Math.Clamp((int)Math.Round(Value),0,5):0;
        for(var i=0;i<images.Length;i++)images[i].Source=i<grade?selected:empty;
    }
    private void HandleKey(object sender,KeyRoutedEventArgs args)
    {
        var grade=double.IsFinite(Value)?Math.Clamp((int)Math.Round(Value),0,5):0;
        var next=args.Key switch{VirtualKey.Left or VirtualKey.Down=>Math.Max(1,grade-1),VirtualKey.Right or VirtualKey.Up=>Math.Min(5,grade+1),VirtualKey.Home=>1,VirtualKey.End=>5,_=>0};
        if(next==0||!IsEnabled)return;Value=next;args.Handled=true;
    }
}