Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions ConfigurationManager.Shared/SettingFieldDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
{
{ typeof(bool), DrawBoolField },
#if IL2CPP
{ typeof(BepInEx.Configuration.KeyboardShortcut), DrawKeyboardShortcutObsolete},

Check warning on line 40 in ConfigurationManager.Shared/SettingFieldDrawer.cs

View workflow job for this annotation

GitHub Actions / build

'KeyboardShortcut' is obsolete: 'Update to BepInEx.Unity.IL2CPP 6.0.0-be.785 and use Unity.IL2CPP.Configuration.KeyboardShortcut instead'
#endif
{ typeof(KeyboardShortcut), DrawKeyboardShortcut},
{ typeof(KeyCode), DrawKeyCode },
Expand Down Expand Up @@ -84,6 +84,7 @@
public static void ClearCache()
{
_comboBoxCache.Clear();
_rangeValueWidth = 0;

foreach (var tex in _colorCache)
UnityEngine.Object.Destroy(tex.Value.Tex);
Expand Down Expand Up @@ -275,6 +276,39 @@
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();
Expand All @@ -293,12 +327,14 @@
{
DrawCenteredLabel(
Mathf.Round(100 * Mathf.Abs(result - leftValue) / Mathf.Abs(rightValue - leftValue)) + "%",
GUILayout.Width(50));
GUILayout.Width(RangeValueWidth(GUI.skin.label, "100%")));
}
else
{
var strVal = Convert.ToString(value, CultureInfo.InvariantCulture).AppendZeroIfFloat(setting.SettingType);
var strResult = GUILayout.TextField(strVal, GUILayout.Width(50));
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)));
Comment thread
MikalaiKryvusha marked this conversation as resolved.
if (strResult != strVal)
{
try
Expand Down Expand Up @@ -445,7 +481,7 @@
{
if (input.GetKeyUp(key))
{
setting.Set(new BepInEx.Configuration.KeyboardShortcut(key, _keysToCheck.Where(input.GetKey).ToArray()));

Check warning on line 484 in ConfigurationManager.Shared/SettingFieldDrawer.cs

View workflow job for this annotation

GitHub Actions / build

'KeyboardShortcut' is obsolete: 'Update to BepInEx.Unity.IL2CPP 6.0.0-be.785 and use Unity.IL2CPP.Configuration.KeyboardShortcut instead'
_currentKeyboardShortcutToSet = null;
break;
}
Expand All @@ -460,7 +496,7 @@

if (GUILayout.Button(ImguiUtils.Content("Clear"), GUILayout.ExpandWidth(false)))
{
setting.Set(BepInEx.Configuration.KeyboardShortcut.Empty);

Check warning on line 499 in ConfigurationManager.Shared/SettingFieldDrawer.cs

View workflow job for this annotation

GitHub Actions / build

'KeyboardShortcut' is obsolete: 'Update to BepInEx.Unity.IL2CPP 6.0.0-be.785 and use Unity.IL2CPP.Configuration.KeyboardShortcut instead'
_currentKeyboardShortcutToSet = null;
}
}
Expand Down
Loading