Files
hk4e-gm-clone/csharp/UI/Ui.cs
T

174 lines
5.9 KiB
C#

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using MaterialDesignThemes.Wpf;
namespace Hk4eGm.UI;
/// <summary>绝对坐标页面构建工具:坐标沿用原程序 1400x1000(减侧栏 60 / 标题栏 32)。</summary>
public static class Ui
{
public const double OffsetX = 60;
public const double OffsetY = 32;
public static readonly Brush TextBrush = new SolidColorBrush(Color.FromRgb(0xEE, 0xEE, 0xEE));
public static readonly Brush DimBrush = new SolidColorBrush(Color.FromRgb(0xA6, 0xA6, 0xA6));
public static readonly Brush FaintBrush = new SolidColorBrush(Color.FromRgb(0x7A, 0x7A, 0x7A));
static Ui()
{
TextBrush.Freeze();
DimBrush.Freeze();
FaintBrush.Freeze();
}
public static Canvas NewCanvas()
=> new() { Background = Brushes.Transparent, ClipToBounds = true };
public static TextBlock Label(Canvas canvas, string text, double x, double y,
double size = 14, bool bold = false, Brush? brush = null, double width = 0, double height = 0)
{
var tb = new TextBlock
{
Text = text,
FontSize = size,
FontWeight = bold ? FontWeights.Bold : FontWeights.Normal,
Foreground = brush ?? TextBrush,
TextWrapping = TextWrapping.Wrap,
Width = width > 0 ? width : double.NaN,
Height = height > 0 ? height : double.NaN,
};
Canvas.SetLeft(tb, x - OffsetX);
Canvas.SetTop(tb, y - OffsetY);
canvas.Children.Add(tb);
return tb;
}
public static Border Card(Canvas canvas, double x, double y, double w, double h)
{
var border = new Border
{
Width = w,
Height = h,
CornerRadius = new CornerRadius(8),
Background = new SolidColorBrush(Color.FromRgb(0x39, 0x39, 0x39)),
BorderBrush = new SolidColorBrush(Color.FromRgb(0x46, 0x46, 0x46)),
BorderThickness = new Thickness(1),
};
Canvas.SetLeft(border, x - OffsetX);
Canvas.SetTop(border, y - OffsetY);
canvas.Children.Add(border);
Panel.SetZIndex(border, -1);
return border;
}
public static Button Button(Canvas canvas, string text, double x, double y, double w, double h,
RoutedEventHandler? onClick, bool primary = false, bool danger = false, bool flat = false)
{
var styleKey = flat ? "MaterialDesignFlatButton"
: primary ? "MaterialDesignFilledButton"
: "MaterialDesignOutlinedButton";
var btn = new Button
{
Content = text,
Width = w,
Height = h,
Style = (Style)Application.Current.FindResource(styleKey),
FontSize = 14,
Padding = new Thickness(0),
};
ButtonAssist.SetCornerRadius(btn, new CornerRadius(16));
if (primary)
{
btn.Background = new SolidColorBrush(Color.FromRgb(0x61, 0x4B, 0x56));
btn.Foreground = Brushes.White;
btn.BorderBrush = new SolidColorBrush(Color.FromRgb(0xB4, 0x8F, 0xA3));
}
else if (danger)
{
btn.Background = new SolidColorBrush(Color.FromRgb(0x6F, 0x47, 0x54));
btn.Foreground = Brushes.White;
}
if (onClick != null) btn.Click += onClick;
Canvas.SetLeft(btn, x - OffsetX);
Canvas.SetTop(btn, y - OffsetY);
canvas.Children.Add(btn);
return btn;
}
public static TextBox Field(Canvas canvas, double x, double y, double w, double h,
string? hint = null, string text = "")
{
var tb = new TextBox
{
Width = w,
Height = h,
Text = text,
FontSize = 14,
Style = (Style)Application.Current.FindResource("MaterialDesignFloatingHintTextBox"),
};
if (!string.IsNullOrEmpty(hint)) HintAssist.SetHint(tb, hint);
Canvas.SetLeft(tb, x - OffsetX);
Canvas.SetTop(tb, y - OffsetY);
canvas.Children.Add(tb);
return tb;
}
public static ComboBox Combo(Canvas canvas, double x, double y, double w, double h,
IEnumerable<object>? items = null, string? hint = null, int selected = 0)
{
var cb = new ComboBox
{
Width = w,
Height = h,
FontSize = 14,
Style = (Style)Application.Current.FindResource("MaterialDesignFloatingHintComboBox"),
};
if (!string.IsNullOrEmpty(hint)) HintAssist.SetHint(cb, hint);
if (items != null)
foreach (var it in items) cb.Items.Add(it);
if (cb.Items.Count > 0 && selected < cb.Items.Count) cb.SelectedIndex = selected;
Canvas.SetLeft(cb, x - OffsetX);
Canvas.SetTop(cb, y - OffsetY);
canvas.Children.Add(cb);
return cb;
}
public static Slider Slider(Canvas canvas, double x, double y, double w, double h,
double min, double max, double value)
{
var slider = new Slider
{
Width = w,
Height = h,
Minimum = min,
Maximum = max,
Value = value,
IsSnapToTickEnabled = true,
TickFrequency = 1,
Foreground = new SolidColorBrush(Color.FromRgb(0x80, 0xB9, 0xEE)),
};
Canvas.SetLeft(slider, x - OffsetX);
Canvas.SetTop(slider, y - OffsetY);
canvas.Children.Add(slider);
return slider;
}
public static ListBox List(Canvas canvas, double x, double y, double w, double h)
{
var lb = new ListBox
{
Width = w,
Height = h,
FontSize = 14,
Background = Brushes.Transparent,
BorderThickness = new Thickness(0),
Style = (Style)Application.Current.FindResource("MaterialDesignListBox"),
};
Canvas.SetLeft(lb, x - OffsetX);
Canvas.SetTop(lb, y - OffsetY);
canvas.Children.Add(lb);
return lb;
}
}