Files
Mikalai Kryvusha 7b25a861e6 Fit the slider value box to its text instead of a fixed 50px (#127)
* Fit the slider value box to its text instead of a fixed 50px

DrawRangeField drew the number box and the percent label with GUILayout.Width(50), so with a bigger font (4K, custom skin) the value was clipped ("-0.", "0.(") and "100%" wrapped to a second line. The width now fits the longest of the value, the minimum and the maximum in the current style (plus its margin), measured on a sample of zeros so the box does not jump while the slider is dragged, and is never narrower than the old 50.

* Share one width between all slider value boxes so they line up

Per review: every value box and percent label next to a slider now uses one global width, so they stand in one column. It only grows: a value that needs more room bumps it for all of them, up to 10 characters, and it never goes below the old 50. It is reset in ClearCache, when the window opens or closes, so a font or skin change is picked up on the next open.
2026-10-14 19:27:11 +02:00

633 lines
28 KiB
C#

// Made by MarC0 / ManlyMarco
// Copyright 2018 GNU General Public License v3.0
using ConfigurationManager.Utilities;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using BepInEx;
#if IL2CPP
using BepInEx.Unity.IL2CPP.Configuration;
#else
using BepInEx.Configuration;
#endif
using UnityEngine;
namespace ConfigurationManager
{
internal class SettingFieldDrawer
{
private static IEnumerable<KeyCode> _keysToCheck;
public static Dictionary<Type, Action<SettingEntryBase>> SettingDrawHandlers { get; }
private static readonly Dictionary<SettingEntryBase, ComboBox> _comboBoxCache = new Dictionary<SettingEntryBase, ComboBox>();
private static readonly Dictionary<SettingEntryBase, ColorCacheEntry> _colorCache = new Dictionary<SettingEntryBase, ColorCacheEntry>();
private static ConfigurationManager _instance;
private static SettingEntryBase _currentKeyboardShortcutToSet;
public static bool SettingKeyboardShortcut => _currentKeyboardShortcutToSet != null;
static SettingFieldDrawer()
{
SettingDrawHandlers = new Dictionary<Type, Action<SettingEntryBase>>
{
{ typeof(bool), DrawBoolField },
#if IL2CPP
{ typeof(BepInEx.Configuration.KeyboardShortcut), DrawKeyboardShortcutObsolete},
#endif
{ typeof(KeyboardShortcut), DrawKeyboardShortcut},
{ typeof(KeyCode), DrawKeyCode },
{ typeof(Color), DrawColor },
{ typeof(Vector2), DrawVector2 },
{ typeof(Vector3), DrawVector3 },
{ typeof(Vector4), DrawVector4 },
{ typeof(Quaternion), DrawQuaternion },
};
}
public SettingFieldDrawer(ConfigurationManager instance)
{
_instance = instance;
}
public void DrawSettingValue(SettingEntryBase setting)
{
if (setting.CustomDrawer != null)
setting.CustomDrawer(setting is ConfigSettingEntry newSetting ? newSetting.Entry : null);
else if (setting.CustomHotkeyDrawer != null)
{
var isBeingSet = _currentKeyboardShortcutToSet == setting;
var isBeingSetOriginal = isBeingSet;
setting.CustomHotkeyDrawer(setting is ConfigSettingEntry newSetting ? newSetting.Entry : null, ref isBeingSet);
if (isBeingSet != isBeingSetOriginal)
_currentKeyboardShortcutToSet = isBeingSet ? setting : null;
}
else if (setting.ShowRangeAsPercent != null && setting.AcceptableValueRange.Key != null)
DrawRangeField(setting);
else if (setting.AcceptableValues != null)
DrawListField(setting);
else if (DrawFieldBasedOnValueType(setting))
return;
else if (setting.SettingType.IsEnum)
DrawEnumField(setting);
else
DrawUnknownField(setting, _instance.RightColumnWidth);
}
public static void ClearCache()
{
_comboBoxCache.Clear();
_rangeValueWidth = 0;
foreach (var tex in _colorCache)
UnityEngine.Object.Destroy(tex.Value.Tex);
_colorCache.Clear();
}
public static void DrawCenteredLabel(string text, params GUILayoutOption[] options)
{
GUILayout.BeginHorizontal(options);
GUILayout.FlexibleSpace();
GUILayout.Label(text);
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
}
/// <summary>
/// Header style of the current skin: its custom style with the given name if it has one, otherwise a centered copy of its label.
/// Rebuilt only when the skin changes, so the default look builds it once as before.
/// </summary>
private static GUIStyle GetHeaderStyle(string customStyleName, int fontSize, ref GUIStyle style, ref GUISkin styleSkin)
{
var skin = GUI.skin;
if (style == null || styleSkin != skin)
{
style = skin.FindStyle(customStyleName);
if (style == null)
{
style = skin.label.CreateCopy();
style.alignment = TextAnchor.UpperCenter;
style.wordWrap = true;
style.stretchWidth = true;
style.fontSize = fontSize;
}
styleSkin = skin;
}
return style;
}
private static GUIStyle _categoryHeaderSkin;
private static GUISkin _categoryHeaderSkinFor;
public static void DrawCategoryHeader(string text)
{
GUILayout.Label(text, GetHeaderStyle(ConfigurationManager.CategoryHeaderStyleName, 14, ref _categoryHeaderSkin, ref _categoryHeaderSkinFor));
}
private static GUIStyle _pluginHeaderSkin;
private static GUISkin _pluginHeaderSkinFor;
public static bool DrawPluginHeader(GUIContent content, bool isCollapsed)
{
GetHeaderStyle(ConfigurationManager.PluginHeaderStyleName, 15, ref _pluginHeaderSkin, ref _pluginHeaderSkinFor);
if (isCollapsed) content.text += "\n...";
return GUILayout.Button(content, _pluginHeaderSkin, GUILayout.ExpandWidth(true));
}
public static bool DrawCurrentDropdown()
{
if (ComboBox.CurrentDropdownDrawer != null)
{
ComboBox.CurrentDropdownDrawer.Invoke();
ComboBox.CurrentDropdownDrawer = null;
return true;
}
return false;
}
private static void DrawListField(SettingEntryBase setting)
{
var acceptableValues = setting.AcceptableValues;
if (acceptableValues.Length == 0)
throw new ArgumentException("AcceptableValueListAttribute returned an empty list of acceptable values. You need to supply at least 1 option.");
if (!setting.SettingType.IsInstanceOfType(acceptableValues.FirstOrDefault(x => x != null)))
throw new ArgumentException("AcceptableValueListAttribute returned a list with items of type other than the settng type itself.");
if (setting.SettingType == typeof(KeyCode))
DrawKeyCode(setting);
else
DrawComboboxField(setting, acceptableValues, _instance.SettingWindowRect.yMax);
}
private static bool DrawFieldBasedOnValueType(SettingEntryBase setting)
{
if (SettingDrawHandlers.TryGetValue(setting.SettingType, out var drawMethod))
{
drawMethod(setting);
return true;
}
return false;
}
private static void DrawBoolField(SettingEntryBase setting)
{
var boolVal = (bool)setting.Get();
var result = GUILayout.Toggle(boolVal, ImguiUtils.Content(boolVal ? "Enabled" : "Disabled"), GUILayout.ExpandWidth(true));
if (result != boolVal)
setting.Set(result);
}
private static void DrawEnumField(SettingEntryBase setting)
{
if (setting.SettingType.GetCustomAttributes(typeof(FlagsAttribute), false).Any())
DrawFlagsField(setting, Enum.GetValues(setting.SettingType), _instance.RightColumnWidth);
else
DrawComboboxField(setting, Enum.GetValues(setting.SettingType), _instance.SettingWindowRect.yMax);
}
private static void DrawFlagsField(SettingEntryBase setting, IList enumValues, int maxWidth)
{
var currentValue = Convert.ToInt64(setting.Get());
var allValues = enumValues.Cast<Enum>().Select(x => new { name = x.ToString(), val = Convert.ToInt64(x) }).ToArray();
// Vertically stack Horizontal groups of the options to deal with the options taking more width than is available in the window
GUILayout.BeginVertical(GUILayout.MaxWidth(maxWidth));
{
for (var index = 0; index < allValues.Length;)
{
GUILayout.BeginHorizontal();
{
var currentWidth = 0;
for (; index < allValues.Length; index++)
{
var value = allValues[index];
// Skip the 0 / none enum value, just uncheck everything to get 0
if (value.val != 0)
{
// Make sure this horizontal group doesn't extend over window width, if it does then start a new horiz group below
// Count the spacing GUILayout puts between toggles too, or a line can end up wider than maxWidth
var toggleStyle = GUI.skin.toggle;
var textDimension = (int)toggleStyle.CalcSize(new GUIContent(value.name)).x + Mathf.Max(toggleStyle.margin.left, toggleStyle.margin.right);
currentWidth += textDimension;
if (currentWidth > maxWidth)
break;
GUI.changed = false;
var newVal = GUILayout.Toggle((currentValue & value.val) == value.val, value.name,
GUILayout.ExpandWidth(false));
if (GUI.changed)
{
var newValue = newVal ? currentValue | value.val : currentValue & ~value.val;
setting.Set(Enum.ToObject(setting.SettingType, newValue));
}
}
}
}
GUILayout.EndHorizontal();
}
GUI.changed = false;
}
GUILayout.EndVertical();
// Make sure the reset button is properly spaced
GUILayout.FlexibleSpace();
}
private static void DrawComboboxField(SettingEntryBase setting, IList list, float windowYmax)
{
var buttonText = ObjectToGuiContent(setting.Get());
var dispRect = GUILayoutUtility.GetRect(buttonText, GUI.skin.button, GUILayout.ExpandWidth(true));
if (!_comboBoxCache.TryGetValue(setting, out var box))
{
box = new ComboBox(dispRect, buttonText, list.Cast<object>().Select(ObjectToGuiContent).ToArray(), GUI.skin.button, windowYmax);
_comboBoxCache[setting] = box;
}
else
{
box.Rect = dispRect;
box.ButtonContent = buttonText;
}
box.Show(id =>
{
if (id >= 0 && id < list.Count)
setting.Set(list[id]);
});
}
private static GUIContent ObjectToGuiContent(object x)
{
if (x is Enum)
{
var enumType = x.GetType();
var enumMember = enumType.GetMember(x.ToString()).FirstOrDefault();
var attr = enumMember?.GetCustomAttributes(typeof(DescriptionAttribute), false).Cast<DescriptionAttribute>().FirstOrDefault();
if (attr != null)
return new GUIContent(attr.Description);
return new GUIContent(x.ToString().ToProperCase());
}
return new GUIContent(x.ToString());
}
/// <summary>
/// Width shared by every value box and percent label next to a slider, so they line up in one column. It only grows: a value
/// that needs more room bumps it for all of them, up to <see cref="RangeValueMaxChars"/> characters. Reset when the window
/// opens or closes (<see cref="ClearCache"/>), so a font or skin change is picked up on the next open.
/// </summary>
private static float _rangeValueWidth;
private const float RangeValueMinWidth = 50f;
private const int RangeValueMaxChars = 10;
/// <summary>
/// Fits the longest of the given texts in the current style (so a bigger font or a custom skin does not clip the number) and
/// returns the shared width, never narrower than the old fixed 50. Measured on a sample of zeros of that length, not on the
/// value itself, so the box does not change width while the slider is dragged.
/// </summary>
private static float RangeValueWidth(GUIStyle style, params string[] texts)
{
var length = 0;
var negative = false;
foreach (var text in texts)
{
if (text == null) continue;
length = Math.Max(length, text.Length);
negative |= text.StartsWith("-");
}
length = Math.Min(length, RangeValueMaxChars);
var sample = (negative ? "-" : "") + new string('0', Math.Max(1, length - (negative ? 1 : 0)));
// The style's margin is added too: the percent label sits inside a horizontal group, and without it "100%" wrapped.
var fit = style.CalcSize(ImguiUtils.Content(sample)).x + style.margin.horizontal;
_rangeValueWidth = Mathf.Max(_rangeValueWidth, RangeValueMinWidth, fit);
return _rangeValueWidth;
}
private static void DrawRangeField(SettingEntryBase setting)
{
var value = setting.Get();
var converted = (float)Convert.ToDouble(value, CultureInfo.InvariantCulture);
var leftValue = (float)Convert.ToDouble(setting.AcceptableValueRange.Key, CultureInfo.InvariantCulture);
var rightValue = (float)Convert.ToDouble(setting.AcceptableValueRange.Value, CultureInfo.InvariantCulture);
var result = GUILayout.HorizontalSlider(converted, leftValue, rightValue, GUILayout.ExpandWidth(true));
if (Math.Abs(result - converted) > Mathf.Abs(rightValue - leftValue) / 1000)
{
var newValue = Convert.ChangeType(result, setting.SettingType, CultureInfo.InvariantCulture);
setting.Set(newValue);
}
if (setting.ShowRangeAsPercent == true)
{
DrawCenteredLabel(
Mathf.Round(100 * Mathf.Abs(result - leftValue) / Mathf.Abs(rightValue - leftValue)) + "%",
GUILayout.Width(RangeValueWidth(GUI.skin.label, "100%")));
}
else
{
var strVal = Convert.ToString(value, CultureInfo.InvariantCulture).AppendZeroIfFloat(setting.SettingType);
var strMin = Convert.ToString(setting.AcceptableValueRange.Key, CultureInfo.InvariantCulture).AppendZeroIfFloat(setting.SettingType);
var strMax = Convert.ToString(setting.AcceptableValueRange.Value, CultureInfo.InvariantCulture).AppendZeroIfFloat(setting.SettingType);
var strResult = GUILayout.TextField(strVal, GUILayout.Width(RangeValueWidth(GUI.skin.textField, strVal, strMin, strMax)));
if (strResult != strVal)
{
try
{
var resultVal = (float)Convert.ToDouble(strResult, CultureInfo.InvariantCulture);
var clampedResultVal = Mathf.Clamp(resultVal, leftValue, rightValue);
setting.Set(Convert.ChangeType(clampedResultVal, setting.SettingType, CultureInfo.InvariantCulture));
}
catch (FormatException)
{
// Ignore user typing in bad data
}
}
}
}
private void DrawUnknownField(SettingEntryBase setting, int rightColumnWidth)
{
// Try to use user-supplied converters
if (setting.ObjToStr != null && setting.StrToObj != null)
{
var text = setting.ObjToStr(setting.Get()).AppendZeroIfFloat(setting.SettingType);
var result = GUILayout.TextField(text, GUILayout.Width(rightColumnWidth), GUILayout.MaxWidth(rightColumnWidth));
if (result != text)
setting.Set(setting.StrToObj(result));
}
else
{
// Fall back to slow/less reliable method
var rawValue = setting.Get();
var value = rawValue == null ? "NULL" : Convert.ToString(rawValue, CultureInfo.InvariantCulture).AppendZeroIfFloat(setting.SettingType);
if (CanCovert(value, setting.SettingType))
{
var result = GUILayout.TextField(value, GUILayout.Width(rightColumnWidth), GUILayout.MaxWidth(rightColumnWidth));
if (result != value)
setting.Set(Convert.ChangeType(result, setting.SettingType, CultureInfo.InvariantCulture));
}
else
{
GUILayout.TextArea(value, GUILayout.MaxWidth(rightColumnWidth));
}
}
// When using MaxWidth the width will always be less than full window size, use this to fill this gap and push the Reset button to the right edge
GUILayout.FlexibleSpace();
}
private readonly Dictionary<Type, bool> _canCovertCache = new Dictionary<Type, bool>();
private bool CanCovert(string value, Type type)
{
if (_canCovertCache.ContainsKey(type))
return _canCovertCache[type];
try
{
var _ = Convert.ChangeType(value, type);
_canCovertCache[type] = true;
return true;
}
catch
{
_canCovertCache[type] = false;
return false;
}
}
private static void DrawKeyCode(SettingEntryBase setting)
{
if (ReferenceEquals(_currentKeyboardShortcutToSet, setting))
{
GUILayout.Label(ImguiUtils.Content("Press any key"), GUILayout.ExpandWidth(true));
GUIUtility.keyboardControl = -1;
var input = UnityInput.Current;
if (_keysToCheck == null) _keysToCheck = input.SupportedKeyCodes.Except(new[] { KeyCode.Mouse0, KeyCode.None }).ToArray();
foreach (var key in _keysToCheck)
{
if (input.GetKeyUp(key))
{
setting.Set(key);
_currentKeyboardShortcutToSet = null;
break;
}
}
if (GUILayout.Button(ImguiUtils.Content("Cancel"), GUILayout.ExpandWidth(false)))
_currentKeyboardShortcutToSet = null;
}
else
{
var acceptableValues = setting.AcceptableValues?.Length > 1 ? setting.AcceptableValues : Enum.GetValues(setting.SettingType);
DrawComboboxField(setting, acceptableValues, _instance.SettingWindowRect.yMax);
if (GUILayout.Button(new GUIContent("Set...", null, "Set the key by pressing any key on your keyboard."), GUILayout.ExpandWidth(false)))
_currentKeyboardShortcutToSet = setting;
}
}
private static void DrawKeyboardShortcut(SettingEntryBase setting)
{
if (ReferenceEquals(_currentKeyboardShortcutToSet, setting))
{
GUILayout.Label(ImguiUtils.Content("Press any key combination"), GUILayout.ExpandWidth(true));
GUIUtility.keyboardControl = -1;
var input = UnityInput.Current;
if (_keysToCheck == null) _keysToCheck = input.SupportedKeyCodes.Except(new[] { KeyCode.Mouse0, KeyCode.None }).ToArray();
foreach (var key in _keysToCheck)
{
if (input.GetKeyUp(key))
{
setting.Set(new KeyboardShortcut(key, _keysToCheck.Where(input.GetKey).ToArray()));
_currentKeyboardShortcutToSet = null;
break;
}
}
if (GUILayout.Button(ImguiUtils.Content("Cancel"), GUILayout.ExpandWidth(false)))
_currentKeyboardShortcutToSet = null;
}
else
{
if (GUILayout.Button(setting.Get().ToString(), GUILayout.ExpandWidth(true)))
_currentKeyboardShortcutToSet = setting;
if (GUILayout.Button(ImguiUtils.Content("Clear"), GUILayout.ExpandWidth(false)))
{
setting.Set(KeyboardShortcut.Empty);
_currentKeyboardShortcutToSet = null;
}
}
}
#if IL2CPP
private static void DrawKeyboardShortcutObsolete(SettingEntryBase setting)
{
if (ReferenceEquals(_currentKeyboardShortcutToSet, setting))
{
GUILayout.Label(ImguiUtils.Content("Press any key combination"), GUILayout.ExpandWidth(true));
GUIUtility.keyboardControl = -1;
var input = UnityInput.Current;
if (_keysToCheck == null) _keysToCheck = input.SupportedKeyCodes.Except(new[] { KeyCode.Mouse0, KeyCode.None }).ToArray();
foreach (var key in _keysToCheck)
{
if (input.GetKeyUp(key))
{
setting.Set(new BepInEx.Configuration.KeyboardShortcut(key, _keysToCheck.Where(input.GetKey).ToArray()));
_currentKeyboardShortcutToSet = null;
break;
}
}
if (GUILayout.Button(ImguiUtils.Content("Cancel"), GUILayout.ExpandWidth(false)))
_currentKeyboardShortcutToSet = null;
}
else
{
if (GUILayout.Button(setting.Get().ToString(), GUILayout.ExpandWidth(true)))
_currentKeyboardShortcutToSet = setting;
if (GUILayout.Button(ImguiUtils.Content("Clear"), GUILayout.ExpandWidth(false)))
{
setting.Set(BepInEx.Configuration.KeyboardShortcut.Empty);
_currentKeyboardShortcutToSet = null;
}
}
}
#endif
private static void DrawVector2(SettingEntryBase obj)
{
var setting = (Vector2)obj.Get();
var copy = setting;
setting.x = DrawSingleVectorSlider(setting.x, "X");
setting.y = DrawSingleVectorSlider(setting.y, "Y");
if (setting != copy) obj.Set(setting);
}
private static void DrawVector3(SettingEntryBase obj)
{
var setting = (Vector3)obj.Get();
var copy = setting;
setting.x = DrawSingleVectorSlider(setting.x, "X");
setting.y = DrawSingleVectorSlider(setting.y, "Y");
setting.z = DrawSingleVectorSlider(setting.z, "Z");
if (setting != copy) obj.Set(setting);
}
private static void DrawVector4(SettingEntryBase obj)
{
var setting = (Vector4)obj.Get();
var copy = setting;
setting.x = DrawSingleVectorSlider(setting.x, "X");
setting.y = DrawSingleVectorSlider(setting.y, "Y");
setting.z = DrawSingleVectorSlider(setting.z, "Z");
setting.w = DrawSingleVectorSlider(setting.w, "W");
if (setting != copy) obj.Set(setting);
}
private static void DrawQuaternion(SettingEntryBase obj)
{
var setting = (Quaternion)obj.Get();
var copy = setting;
setting.x = DrawSingleVectorSlider(setting.x, "X");
setting.y = DrawSingleVectorSlider(setting.y, "Y");
setting.z = DrawSingleVectorSlider(setting.z, "Z");
setting.w = DrawSingleVectorSlider(setting.w, "W");
if (setting != copy) obj.Set(setting);
}
private static float DrawSingleVectorSlider(float setting, string label)
{
GUILayout.Label(label, GUILayout.ExpandWidth(false));
float.TryParse(GUILayout.TextField(setting.ToString("F", CultureInfo.InvariantCulture), GUILayout.ExpandWidth(true)), NumberStyles.Any, CultureInfo.InvariantCulture, out var x);
return x;
}
private static bool _drawColorHex;
private static void DrawColor(SettingEntryBase obj)
{
var colorValue = (Color)obj.Get();
GUI.changed = false;
if (!_colorCache.TryGetValue(obj, out var cacheEntry))
{
var tex = new Texture2D(100, 20, TextureFormat.ARGB32, false);
cacheEntry = new ColorCacheEntry { Tex = tex, Last = colorValue };
FillTex(colorValue, tex);
_colorCache[obj] = cacheEntry;
}
GUILayout.BeginVertical();
{
GUILayout.BeginHorizontal();
{
GUILayout.Label(cacheEntry.Tex, GUILayout.ExpandWidth(false));
var colorStr = _drawColorHex ? "#" + ColorUtility.ToHtmlStringRGBA(colorValue) : $"{colorValue.r:F2} {colorValue.g:F2} {colorValue.b:F2} {colorValue.a:F2}";
// Let the field shrink below its text width, or a longer (e.g. translated) Reset button is pushed out of the window
var newColorStr = GUILayout.TextField(colorStr, GUILayout.ExpandWidth(true), GUILayout.MinWidth(50));
if (GUI.changed && colorStr != newColorStr)
{
if (_drawColorHex)
{
if (ColorUtility.TryParseHtmlString(newColorStr, out var parsedColor))
colorValue = parsedColor;
}
else
{
var split = newColorStr.Split(' ');
if (split.Length == 4 && float.TryParse(split[0], out var r) && float.TryParse(split[1], out var g) && float.TryParse(split[2], out var b) && float.TryParse(split[3], out var a))
colorValue = new Color(r, g, b, a);
}
}
_drawColorHex = GUILayout.Toggle(_drawColorHex, "Hex", GUILayout.ExpandWidth(false));
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
{
GUILayout.Label("R", GUILayout.ExpandWidth(false));
colorValue.r = GUILayout.HorizontalSlider(colorValue.r, 0f, 1f, GUILayout.ExpandWidth(true));
GUILayout.Label("G", GUILayout.ExpandWidth(false));
colorValue.g = GUILayout.HorizontalSlider(colorValue.g, 0f, 1f, GUILayout.ExpandWidth(true));
GUILayout.Label("B", GUILayout.ExpandWidth(false));
colorValue.b = GUILayout.HorizontalSlider(colorValue.b, 0f, 1f, GUILayout.ExpandWidth(true));
GUILayout.Label("A", GUILayout.ExpandWidth(false));
colorValue.a = GUILayout.HorizontalSlider(colorValue.a, 0f, 1f, GUILayout.ExpandWidth(true));
}
GUILayout.EndHorizontal();
}
GUILayout.EndVertical();
if (colorValue != cacheEntry.Last)
{
obj.Set(colorValue);
FillTex(colorValue, cacheEntry.Tex);
cacheEntry.Last = colorValue;
}
void FillTex(Color color, Texture2D tex)
{
if (color.a < 1f) tex.FillTextureCheckerboard();
tex.FillTexture(color);
}
}
private sealed class ColorCacheEntry
{
public Color Last;
public Texture2D Tex;
}
}
}