From 082ceec5e2e1442f00f71a5e28a46157cbe8adc7 Mon Sep 17 00:00:00 2001 From: yyc12345 Date: Mon, 8 Sep 2025 16:28:08 +0800 Subject: [PATCH] feat: finish color picker --- HFUTCourseSimulation/Widget/ColorPicker.xaml | 2 +- .../Widget/ColorPicker.xaml.cs | 81 ++++++++++++++++++- 2 files changed, 81 insertions(+), 2 deletions(-) diff --git a/HFUTCourseSimulation/Widget/ColorPicker.xaml b/HFUTCourseSimulation/Widget/ColorPicker.xaml index 37c8c14..6b100e7 100644 --- a/HFUTCourseSimulation/Widget/ColorPicker.xaml +++ b/HFUTCourseSimulation/Widget/ColorPicker.xaml @@ -41,7 +41,7 @@ - + diff --git a/HFUTCourseSimulation/Widget/ColorPicker.xaml.cs b/HFUTCourseSimulation/Widget/ColorPicker.xaml.cs index 6080bd6..1c39d34 100644 --- a/HFUTCourseSimulation/Widget/ColorPicker.xaml.cs +++ b/HFUTCourseSimulation/Widget/ColorPicker.xaml.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows; @@ -25,8 +26,11 @@ namespace HFUTCourseSimulation.Widget { private Color _foregroundBackup, _backgroundBackup; private void uiMainWindow_Loaded(object sender, RoutedEventArgs e) { - // Set context and backup color for restoring + // Build color list + BuildColorList(); + // Set context uiPicker.DataContext = CurrentColor; + // Backup color for restoring _foregroundBackup = CurrentColor.Foreground; _backgroundBackup = CurrentColor.Background; } @@ -35,6 +39,78 @@ namespace HFUTCourseSimulation.Widget { uiPicker.DataContext = null; } + #region Color Box Stuff + + private Border CreateColorBox(Util.ColorPair c, string name) { + var colorBox = new Border(); + colorBox.Width = 100; + colorBox.Height = 100; + colorBox.Background = new SolidColorBrush(c.Background); + colorBox.BorderBrush = new SolidColorBrush(Colors.Black); + colorBox.BorderThickness = new Thickness(1); + colorBox.CornerRadius = new CornerRadius(5); + colorBox.Margin = new Thickness(5); + colorBox.Cursor = Cursors.Hand; + + var colorText = new TextBlock(); + colorText.Text = name; + colorText.Foreground = new SolidColorBrush(c.Foreground); + colorText.HorizontalAlignment = HorizontalAlignment.Center; + colorText.VerticalAlignment = VerticalAlignment.Center; + colorBox.Child = colorText; + + return colorBox; + } + + private class ColorPropertyWrapper { + public ColorPropertyWrapper(PropertyInfo prop) { + _prop = prop; + } + + public string GetName() => _prop.Name; + public Util.ColorPair GetColor() { + var value = _prop.GetValue(null); + if (value is Util.ColorPair color) { + return color; + } else { + return new Util.ColorPair(); + } + } + + private PropertyInfo _prop; + } + + private IEnumerable FilterColorProperties(Type t) { + return t.GetProperties(BindingFlags.Public | BindingFlags.Static) + .Where((prop) => prop.PropertyType == typeof(Util.ColorPair)) + .Select((prop) => new ColorPropertyWrapper(prop)); + } + + private Dictionary _knownColorPresets = new Dictionary(); + + private void BuildColorList() { + _knownColorPresets .Clear(); + + uiMdColorPresetList.Children.Clear(); + foreach (var prop in FilterColorProperties(typeof(Util.ColorPreset.MdColors))) { + var box = CreateColorBox(prop.GetColor(), prop.GetName()); + box.MouseDown += ColorBox_Click; + uiMdColorPresetList.Children.Add(box); + _knownColorPresets.Add(box, prop.GetColor()); + } + } + + private void ColorBox_Click(object sender, MouseButtonEventArgs e) { + if (_knownColorPresets.TryGetValue(sender, out var value)) { + CurrentColor.Foreground = value.Foreground; + CurrentColor.Background = value.Background; + } + } + + #endregion + + #region Ui Handlers + private void uiForegroundPicker_MouseDown(object sender, MouseButtonEventArgs e) { var color = Util.Win32Dialog.PickColor(); if (color is null) return; @@ -56,5 +132,8 @@ namespace HFUTCourseSimulation.Widget { CurrentColor.Background = _backgroundBackup; this.Close(); } + + #endregion + } }