Skip to content

Commit

Permalink
additional settings improvements to make them easy to read while maki…
Browse files Browse the repository at this point in the history
…ng use of T3 UI

also some style changes i neglected before
  • Loading branch information
domportera committed Oct 8, 2022
1 parent 7ac3bf9 commit d92d2b3
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 69 deletions.
35 changes: 25 additions & 10 deletions T3/Gui/SettingsUi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
using T3.Gui.Windows;
Expand All @@ -27,11 +28,26 @@ public static bool DrawSettingsTable(string tableID, UIControlledSetting[] setti
ImGui.TableNextRow();
ImGui.TableNextColumn();
ImGui.Indent();
ImGui.Text(setting.uniqueLabel);
ImGui.Unindent();
ImGui.TableNextColumn();
bool valueChanged = DrawSetting(setting);
changed |= valueChanged;

if (setting.DrawOnLeft)
{
var valueChanged = setting.DrawGUIControl(true);
changed |= valueChanged;
ImGui.SameLine();
ImGui.Dummy(_leftCheckboxSpacing);
ImGui.SameLine();
ImGui.Text(setting.CleanLabel);
ImGui.Unindent();
}
else
{
ImGui.Text(setting.CleanLabel);
ImGui.Unindent();
ImGui.TableNextColumn();
var valueChanged = setting.DrawGUIControl(true);
changed |= valueChanged;
}

}
}

Expand All @@ -54,16 +70,15 @@ public static bool DrawSettings(UIControlledSetting[] settings)

foreach (var setting in settings)
{
changed |= DrawSetting(setting);
ImGui.Text(setting.CleanLabel);
ImGui.SameLine();
changed |= setting.DrawGUIControl(true);
}

ImGui.NewLine();
return changed;
}

public static bool DrawSetting(UIControlledSetting setting)
{
return setting.DrawGUIControl();
}
private static readonly Vector2 _leftCheckboxSpacing = new Vector2(0f, 20f);
}
}
37 changes: 30 additions & 7 deletions T3/Gui/Styling/CustomComponents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -487,10 +487,19 @@ public static bool FloatValueEdit(string label, ref float value, float min = flo

ImGui.SameLine();
ImGui.SetCursorPosX(leftPadding + 20);
var size = new Vector2(150, ImGui.GetFrameHeight());
var modified = DrawSingleValueEdit(cleanedLabel, ref value, min, max, clamp, scale);

return modified;
}

public static bool FloatValueEditInPlace(string label, ref float value, float min = float.NegativeInfinity, float max = float.PositiveInfinity, float scale = 0.01f, bool clamp = false)
{
string cleanedLabel = label.Split(_idSpecifier)[0];
ImGui.TextUnformatted(cleanedLabel);
ImGui.SameLine();
ImGui.PushID(label);
var result = SingleValueEdit.Draw(ref value, size, min, max, clamp, scale);
var modified = (result & InputEditStateFlags.Modified) != InputEditStateFlags.Nothing;
var modified = DrawSingleValueEdit(cleanedLabel, ref value, min, max, clamp, scale);

ImGui.PopID();
return modified;
}
Expand All @@ -510,12 +519,27 @@ public static bool IntValueEdit(string label, ref int value, int min = int.MinVa

ImGui.SameLine();
ImGui.SetCursorPosX(leftPadding + 20);

var modified = DrawSingleValueEdit(cleanedLabel, ref value, min, max, true, scale);
return modified;
}

public static bool DrawSingleValueEdit(string label, ref int value, int min = int.MinValue, int max = int.MaxValue, bool clamp = false, float scale = 1f)
{
ImGui.PushID(label);
var size = new Vector2(150, ImGui.GetFrameHeight());
var result = SingleValueEdit.Draw(ref value, size, min, max, clamp, scale);
ImGui.PopID();
return (result & InputEditStateFlags.Modified) != InputEditStateFlags.Nothing;
}

public static bool DrawSingleValueEdit(string label, ref float value, float min = float.MinValue, float max = float.MaxValue, bool clamp = false, float scale = 1f)
{
ImGui.PushID(label);
var result = SingleValueEdit.Draw(ref value, size, min, max, true, scale);
var modified = (result & InputEditStateFlags.Modified) != InputEditStateFlags.Nothing;
var size = new Vector2(150, ImGui.GetFrameHeight());
var result = SingleValueEdit.Draw(ref value, size, min, max, clamp, scale);
ImGui.PopID();
return modified;
return (result & InputEditStateFlags.Modified) != InputEditStateFlags.Nothing;
}

public static bool StringValueEdit(string label, ref string value)
Expand Down Expand Up @@ -579,7 +603,6 @@ public static bool DrawEnumSelector<T>(ref int index, string label)
return modified;
}


private const string _idSpecifier = "##";
}

Expand Down
50 changes: 29 additions & 21 deletions T3/Gui/UiHelpers/UIControlledSetting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,24 @@ public class UIControlledSetting
/// <summary>
/// The generated unique label
/// </summary>
public string uniqueLabel { get; private set; }
private string _uniqueLabel;
private string _hiddenUniqueLabel;

/// <summary>
/// The label provided in the constructor
/// Unused by this class and here for convenience, but often best left ignored
/// </summary>
public string cleanLabel { get; private set; }
public string CleanLabel { get; private set; }
public bool DrawOnLeft { get; private set; }

private string tooltip;
private string additionalNotes;
private Func<bool> guiFunc;
private Action OnValueChanged;
private string _tooltip;
private string _additionalNotes;
private Func<string, bool> _guiFunc;
private Action _OnValueChanged;

// Cheaper than GUIDs
// In case we want to have the same variable be changeable with different UI controls
// or if multiple settings have the same label
static ushort countForUniqueID = ushort.MaxValue;
static ushort _countForUniqueID = ushort.MaxValue;

/// <summary>
/// For the sake of simple use of the optional parameters and populating/maintaining many settings, the recommended way to call this constructor is:
Expand All @@ -43,42 +44,49 @@ public class UIControlledSetting
/// <param name="guiFunc">The <see cref="ImGuiNET"/> - based function that draws the setting control and
/// returns true if the control was changed. The input to this function see is a unique ID based on the label provided</param>
/// <param name="tooltip">Tooltip displayed when hovering over the control</param>
/// <param name="additionalNotes">Additional notes displayed alongside the tooltip</param>
/// <param name="additionalNotes">Additional notes displayed alongside the tooltip [Not currently in use, but will be once T3 tooltips are integrated]</param>
/// <param name="OnValueChanged">An action performed when the value is changed</param>
public UIControlledSetting(string label, Func<string, bool> guiFunc, string tooltip = null, string additionalNotes = null, Action OnValueChanged = null)
public UIControlledSetting(string label, Func<string, bool> guiFunc, string tooltip = null, string additionalNotes = null, bool drawOnLeft = false, Action OnValueChanged = null)
{
cleanLabel = label;
uniqueLabel = $"{label}##{countForUniqueID--}";
CleanLabel = label;
_hiddenUniqueLabel = $"##{label}{_countForUniqueID--}";
_uniqueLabel = $"{label}##{_countForUniqueID--}";

this.guiFunc = () => guiFunc(uniqueLabel);
this.tooltip = tooltip;
this.additionalNotes = additionalNotes;
this.OnValueChanged = OnValueChanged;
_guiFunc = guiFunc;
_tooltip = tooltip;
_additionalNotes = additionalNotes;
_OnValueChanged = OnValueChanged;
DrawOnLeft = drawOnLeft;
}

/// <summary>
/// Draws the GUI for this setting using the Func provided in its constructor
/// </summary>
/// <returns>True if changed, false if unchanged.
/// If an Action was provided in constructor, it will be executed when value is changed. </returns>
public bool DrawGUIControl()
public bool DrawGUIControl(bool hideLabel)
{
if (!string.IsNullOrEmpty(tooltip))
if (!string.IsNullOrEmpty(_tooltip))
{
if (ImGui.IsItemHovered(ImGuiHoveredFlags.AllowWhenDisabled))
{
ImGui.SetTooltip(tooltip);
ImGui.SetTooltip(_tooltip);
}
}

var changed = guiFunc.Invoke();
var changed = DrawCommand(hideLabel);

if(changed)
{
OnValueChanged?.Invoke();
_OnValueChanged?.Invoke();
}

return changed;
}

bool DrawCommand(bool hideLabel)
{
return _guiFunc.Invoke(hideLabel ? _hiddenUniqueLabel : _uniqueLabel);
}
}
}
Loading

0 comments on commit d92d2b3

Please sign in to comment.