From a8315676c7cec4eed5be68a525b19954dd09c0a1 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 30 Oct 2026 01:40:54 +0000 Subject: [PATCH 1/3] Value buttons for discrete numeric values: Range step, and numeric Values lists Per UX feedback: - "Range: [20-120, by 20]" allows only 20,40,...,120 (int or float). - An all-numeric "Values: [8,9,10,12,16]" lists the allowed values themselves (text labels still map to 0,1,2... in a dropdown). - When there are 21 or fewer allowed values, the detail pane shows a wrapping grid of buttons instead of the text box; the selected one is highlighted, and Apply & Save writes it. Typed values (when there are more than 21) are validated against the step / list. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01KaEsnLMDyw72GZ8a6ZxAGq --- src/BmsConfig/Core/ValueConverter.cs | 111 ++++++++++++++++++- src/BmsConfig/MainWindow.xaml | 3 + src/BmsConfig/MainWindow.xaml.cs | 34 +++++- src/BmsConfig/Themes/Styles.xaml | 35 ++++++ tests/BmsConfig.Tests/ValueConverterTests.cs | 59 ++++++++++ 5 files changed, 235 insertions(+), 7 deletions(-) diff --git a/src/BmsConfig/Core/ValueConverter.cs b/src/BmsConfig/Core/ValueConverter.cs index 58b0470..6173f77 100644 --- a/src/BmsConfig/Core/ValueConverter.cs +++ b/src/BmsConfig/Core/ValueConverter.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Globalization; +using System.Linq; using System.Text.RegularExpressions; namespace BmsConfig.Core @@ -46,14 +47,21 @@ public static string DisplayName(ParamType type) } /// - /// Constraints parsed from a param's description text: "Range: [0-100]", "Range: [0-]", "Values: [Low,Medium,High]". + /// Constraints parsed from a param's description text: + /// "Range: [0-100]", "Range: [0-]", "Range: [20-120, by 20]", + /// "Values: [Low,Medium,High]" (labels for 0,1,2...), "Values: [8,9,10,12,16]" (the allowed numbers). /// public sealed class ParamConstraints { + /// Discrete values are offered as buttons, when there are at most this many. + public const int MaxChoices = 21; + static readonly Regex s_range = new Regex( - @"Range:\s*\[\s*(?[+-]?\d+(?:\.\d+)?)?\s*(?:-|\.\.|,)\s*(?[+-]?\d+(?:\.\d+)?)?\s*\]", + @"Range:\s*\[\s*(?[+-]?\d+(?:\.\d+)?)?\s*(?:-|\.\.|,)\s*(?[+-]?\d+(?:\.\d+)?)?\s*(?:,\s*by\s+(?\+?\d+(?:\.\d+)?)\s*)?\]", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant); + static readonly Regex s_number = new Regex(@"^[+-]?(\d+\.?\d*|\.\d+)$", RegexOptions.CultureInvariant); + static readonly Regex s_enum = new Regex( @"Values:\s*\[(?[^\]]*)\]", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant); @@ -62,7 +70,12 @@ public sealed class ParamConstraints public double? Min { get; private set; } public double? Max { get; private set; } + /// From "Range: [min-max, by step]": allowed values are min, min+step, min+2*step... (up to max). + public double? Step { get; private set; } + /// From "Values: [Low,Medium,High]": labels for the integer values 0,1,2... public IReadOnlyList EnumLabels { get; private set; } + /// From an all-numeric "Values: [8,9,10,12,16]": the only allowed values. + public IReadOnlyList NumericValues { get; private set; } public bool HasRange { get { return Min.HasValue || Max.HasValue; } } public bool IsEnum { get { return EnumLabels != null && EnumLabels.Count > 0; } } @@ -81,6 +94,12 @@ public static ParamConstraints Parse(string description) c.Min = double.Parse(m.Groups["min"].Value, CultureInfo.InvariantCulture); if (m.Groups["max"].Success) c.Max = double.Parse(m.Groups["max"].Value, CultureInfo.InvariantCulture); + + // A step needs a starting point; a zero step is meaningless. + double step; + if (m.Groups["step"].Success && c.Min.HasValue + && double.TryParse(m.Groups["step"].Value, NumberStyles.Float, CultureInfo.InvariantCulture, out step) && step > 0) + c.Step = step; } m = s_enum.Match(description); @@ -90,12 +109,83 @@ public static ParamConstraints Parse(string description) foreach (string s in m.Groups["labels"].Value.Split(',')) labels.Add(s.Trim()); if (labels.Count > 0 && !(labels.Count == 1 && labels[0].Length == 0)) - c.EnumLabels = labels; + { + // All numbers -> the allowed values themselves; otherwise labels for 0,1,2... + if (labels.TrueForAll(l => s_number.IsMatch(l))) + c.NumericValues = labels.ConvertAll(l => double.Parse(l, NumberStyles.Float, CultureInfo.InvariantCulture)); + else + c.EnumLabels = labels; + } } return c; } + /// + /// The discrete values allowed for an int or float param (from "Values: [numbers]", or a Range with a + /// step), as cfg tokens -- or null if there are none, or more than MaxChoices. + /// + public IReadOnlyList GetChoices(ParamType type) + { + if (type != ParamType.Int && type != ParamType.Float) + return null; + + List values = new List(); + if (NumericValues != null) + { + values.AddRange(NumericValues); + } + else if (Step.HasValue && Max.HasValue) + { + for (int i = 0; i <= MaxChoices; i++) + { + double v = Math.Round(Min.Value + i * Step.Value, 9); + if (v > Max.Value + 1e-9) + break; + values.Add(v); + } + } + + if (values.Count == 0 || values.Count > MaxChoices) + return null; + + List tokens = new List(); + foreach (double v in values) + { + if (type == ParamType.Int && v != Math.Floor(v)) + continue; // eg. "Values: [1.5]" on an int param + string token = ValueConverter.FormatNumber(type, v); + if (!tokens.Contains(token)) + tokens.Add(token); + } + return tokens.Count > 0 ? tokens : null; + } + + /// Is the value one of the NumericValues, and on the Step grid (if those apply)? + public bool IsAllowedValue(double value, out string error) + { + error = null; + const double tolerance = 1e-9; + + if (NumericValues != null && !NumericValues.Any(v => Math.Abs(v - value) <= tolerance * Math.Max(1, Math.Abs(v)))) + { + error = "Value must be one of: " + string.Join(", ", NumericValues.Select(v => v.ToString(CultureInfo.InvariantCulture))) + "."; + return false; + } + + if (Step.HasValue) + { + double steps = (value - Min.Value) / Step.Value; + if (Math.Abs(steps - Math.Round(steps)) > 1e-6) + { + error = "Value must be a multiple of " + Step.Value.ToString(CultureInfo.InvariantCulture) + + " from " + Min.Value.ToString(CultureInfo.InvariantCulture) + ". " + Describe(); + return false; + } + } + return true; + } + public string Describe() { if (!HasRange) @@ -103,9 +193,10 @@ public string Describe() string min = Min.HasValue ? Min.Value.ToString(CultureInfo.InvariantCulture) : ""; string max = Max.HasValue ? Max.Value.ToString(CultureInfo.InvariantCulture) : ""; + string step = Step.HasValue ? ", by " + Step.Value.ToString(CultureInfo.InvariantCulture) : ""; if (Max.HasValue) - return "Range: [" + min + " to " + max + "]"; - return Min == 0 ? "Range: [0 or greater]" : "Range: [" + min + " or greater]"; + return "Range: [" + min + " to " + max + step + "]"; + return Min == 0 ? "Range: [0 or greater" + step + "]" : "Range: [" + min + " or greater" + step + "]"; } } @@ -426,7 +517,15 @@ static bool CheckRange(double value, ParamConstraints constraints, out string er error = "Value is out of range. " + constraints.Describe(); return false; } - return true; + return constraints.IsAllowedValue(value, out error); + } + + /// Formats a number as a cfg token: integers in decimal; floats with at least one decimal place. + public static string FormatNumber(ParamType type, double value) + { + if (type == ParamType.Int) + return ((long)Math.Round(value)).ToString(CultureInfo.InvariantCulture); + return Math.Round(value, 9).ToString("0.0########", CultureInfo.InvariantCulture); } static bool HasWhitespace(string s) diff --git a/src/BmsConfig/MainWindow.xaml b/src/BmsConfig/MainWindow.xaml index 455fb91..edb92a1 100644 --- a/src/BmsConfig/MainWindow.xaml +++ b/src/BmsConfig/MainWindow.xaml @@ -225,6 +225,9 @@ + + + diff --git a/src/BmsConfig/MainWindow.xaml.cs b/src/BmsConfig/MainWindow.xaml.cs index 5ae382c..60ed8be 100644 --- a/src/BmsConfig/MainWindow.xaml.cs +++ b/src/BmsConfig/MainWindow.xaml.cs @@ -469,6 +469,8 @@ void ShowDetail(ParamItem item) string typeText = "Type: " + ParamTypes.DisplayName(entry.Type); if (c.IsEnum) typeText += " Values: " + string.Join(", ", c.EnumLabels.Select((l, i) => i + " = " + l)); + else if (c.NumericValues != null) + typeText += " Values: " + string.Join(", ", c.NumericValues.Select(v => ValueConverter.FormatNumber(entry.Type, v))); else if (c.HasRange) typeText += " " + c.Describe(); if (entry.Category == ConfigSet.UnknownCategory && entry.BmsValue == null) @@ -501,9 +503,25 @@ void ShowDetail(ParamItem item) _suppressEvents = true; bool isBool = entry.Type == ParamType.Bool; bool isEnum = entry.Type == ParamType.Int && c.IsEnum; + IReadOnlyList choices = (!isBool && !isEnum) ? c.GetChoices(entry.Type) : null; + bool isChoice = choices != null; BoolEditor.Visibility = isBool ? Visibility.Visible : Visibility.Collapsed; EnumEditor.Visibility = isEnum ? Visibility.Visible : Visibility.Collapsed; - TextEditor.Visibility = (!isBool && !isEnum) ? Visibility.Visible : Visibility.Collapsed; + ChoiceEditor.Visibility = isChoice ? Visibility.Visible : Visibility.Collapsed; + TextEditor.Visibility = (!isBool && !isEnum && !isChoice) ? Visibility.Visible : Visibility.Collapsed; + + ChoiceEditor.Children.Clear(); + if (isChoice) + { + Style style = (Style)FindResource("ChoiceButton"); + foreach (string token in choices) + { + RadioButton button = new RadioButton { Content = token, Tag = token, GroupName = "ChoiceValue", Style = style }; + button.Click += (s, e) => OnEditorChoiceChanged(); + ChoiceEditor.Children.Add(button); + } + SelectChoice(entry.Type, current); // off-grid values (eg. hand-edited) leave no button selected + } if (isBool) { @@ -612,10 +630,19 @@ string EditorInput return BoolOn.IsChecked == true ? "1" : BoolOff.IsChecked == true ? "0" : null; if (EnumEditor.Visibility == Visibility.Visible) return EnumEditor.SelectedIndex >= 0 ? EnumEditor.SelectedIndex.ToString() : null; + if (ChoiceEditor.Visibility == Visibility.Visible) + return ChoiceEditor.Children.OfType().Where(b => b.IsChecked == true).Select(b => (string)b.Tag).FirstOrDefault(); return TextEditor.Text; } } + /// Checks the value button equivalent to the given cfg value (eg. "1" matches "1.0"), if any. + void SelectChoice(ParamType type, string value) + { + foreach (RadioButton button in ChoiceEditor.Children.OfType()) + button.IsChecked = value != null && ValueConverter.AreEquivalent(type, (string)button.Tag, value); + } + void OnApplyClick(object sender, RoutedEventArgs e) { string input = EditorInput; @@ -654,6 +681,11 @@ void OnFillDefaultClick(object sender, RoutedEventArgs e) long? n = ValueConverter.ParseCfgInt(entry.BmsValue); EnumEditor.SelectedIndex = (n.HasValue && n.Value >= 0 && n.Value < EnumEditor.Items.Count) ? (int)n.Value : -1; } + else if (ChoiceEditor.Visibility == Visibility.Visible) + { + SelectChoice(entry.Type, entry.BmsValue); + OnEditorChoiceChanged(); + } else { TextEditor.Text = ValueConverter.ToEditText(entry.Type, entry.BmsValue); diff --git a/src/BmsConfig/Themes/Styles.xaml b/src/BmsConfig/Themes/Styles.xaml index 33bd3f7..f39fdc0 100644 --- a/src/BmsConfig/Themes/Styles.xaml +++ b/src/BmsConfig/Themes/Styles.xaml @@ -129,6 +129,41 @@ + + + + + +