Skip to content

Commit

Permalink
refactor in progress to use T3Ui
Browse files Browse the repository at this point in the history
may want to move DrawSettings into T3Ui as well, possibly in a separate class for "settings" specific functions (would need to move over DrawSettingsTable)
  • Loading branch information
domportera committed Oct 7, 2022
1 parent 203d307 commit 7a85fd7
Show file tree
Hide file tree
Showing 5 changed files with 362 additions and 241 deletions.
64 changes: 64 additions & 0 deletions T3/Gui/SettingsUi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using ImGuiNET;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using T3.Gui.Windows;

namespace t3.Gui
{
public static class SettingsUi
{
/// <summary>
/// Draws a table of <see cref="UIControlledSetting"/>s
/// </summary>
/// <param name="tableID">Unique identifier for your table - will not be displayed</param>
/// <param name="settings">Settings to display</param>
/// <returns>Returns true if a setting has been modified</returns>
public static bool DrawSettingsTable(string tableID, UIControlledSetting[] settings)
{
ImGui.NewLine();
bool changed = false;
if (ImGui.BeginTable(tableID, 2, ImGuiTableFlags.BordersInnerH | ImGuiTableFlags.SizingFixedSame | ImGuiTableFlags.PadOuterX))
{
foreach (UIControlledSetting setting in settings)
{
ImGui.TableNextRow();
ImGui.TableNextColumn();
ImGui.Indent();
ImGui.Text(setting.uniqueLabel);
ImGui.Unindent();
ImGui.TableNextColumn();
bool valueChanged = DrawSetting(setting);
changed |= valueChanged;
}
}

ImGui.EndTable();
ImGui.NewLine();

return changed;
}

public static bool DrawSettings(UIControlledSetting[] settings)
{
ImGui.NewLine();

bool changed = false;

foreach (var setting in settings)
{
changed |= DrawSetting(setting);
}

ImGui.NewLine();
return changed;
}

public static bool DrawSetting(UIControlledSetting setting)
{
return setting.DrawGUIControl();
}
}
}
32 changes: 23 additions & 9 deletions T3/Gui/Styling/CustomComponents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -473,21 +473,23 @@ public static bool DrawSegmentedToggle(ref int currentIndex, List<string> option
}


public static bool FloatValueEdit(string label, ref float value, float min= float.NegativeInfinity, float max= float.PositiveInfinity, float scale= 0)
public static bool FloatValueEdit(string label, ref float value, float min = float.NegativeInfinity, float max = float.PositiveInfinity, float scale = 0.01f, bool clamp = false)
{
var labelSize = ImGui.CalcTextSize(label);
const float leftPadding = 200;
var p = ImGui.GetCursorPos();
ImGui.SetCursorPosX( MathF.Max(leftPadding - labelSize.X,0)+10);
ImGui.SetCursorPosX(MathF.Max(leftPadding - labelSize.X, 0) + 10);
ImGui.AlignTextToFramePadding();
ImGui.TextUnformatted(label);

string cleanedLabel = label.Split(_idSpecifier)[0];
ImGui.TextUnformatted(cleanedLabel);
ImGui.SetCursorPos(p);

ImGui.SameLine();
ImGui.SetCursorPosX(leftPadding + 20);
var size = new Vector2(150, ImGui.GetFrameHeight());
ImGui.PushID(label);
var result = SingleValueEdit.Draw(ref value, size, min, max);
var result = SingleValueEdit.Draw(ref value, size, min, max, clamp, scale);
var modified = (result & InputEditStateFlags.Modified) != InputEditStateFlags.Nothing;
ImGui.PopID();
return modified;
Expand All @@ -500,7 +502,10 @@ public static bool IntValueEdit(string label, ref int value, int min = int.MinVa
var p = ImGui.GetCursorPos();
ImGui.SetCursorPosX(MathF.Max(leftPadding - labelSize.X, 0) + 10);
ImGui.AlignTextToFramePadding();
ImGui.TextUnformatted(label);

string cleanedLabel = label.Split(_idSpecifier)[0];
ImGui.TextUnformatted(cleanedLabel);

ImGui.SetCursorPos(p);

ImGui.SameLine();
Expand All @@ -521,7 +526,10 @@ public static bool StringValueEdit(string label, ref string value)
var p = ImGui.GetCursorPos();
ImGui.SetCursorPosX( MathF.Max(leftPadding - labelSize.X,0)+10);
ImGui.AlignTextToFramePadding();
ImGui.TextUnformatted(label);

string cleanedLabel = label.Split(_idSpecifier)[0];
ImGui.TextUnformatted(cleanedLabel);

ImGui.SetCursorPos(p);

ImGui.SameLine();
Expand All @@ -541,7 +549,10 @@ public static bool DrawEnumSelector<T>(ref int index, string label)
var p = ImGui.GetCursorPos();
ImGui.SetCursorPosX(MathF.Max(200 - labelSize.X, 0) + 10);
ImGui.AlignTextToFramePadding();
ImGui.TextUnformatted(label);

string cleanedLabel = label.Split(_idSpecifier)[0];
ImGui.TextUnformatted(cleanedLabel);

ImGui.SetCursorPos(p);

// Dropdown
Expand All @@ -567,6 +578,9 @@ public static bool DrawEnumSelector<T>(ref int index, string label)
var modified = ImGui.Combo($"##dropDown{enumType}{label}", ref index, valueNames, valueNames.Length, valueNames.Length);
return modified;
}


private const string _idSpecifier = "##";
}

public static class InputWithTypeAheadSearch
Expand Down Expand Up @@ -665,6 +679,6 @@ public static bool Draw(string id, ref string text, IOrderedEnumerable<string> i

private static List<string> _lastResults = new List<string>();
private static int _selectedResultIndex = 0;
private static bool _isSearchResultWindowOpen;
private static bool _isSearchResultWindowOpen;
}
}
89 changes: 89 additions & 0 deletions T3/Gui/UiHelpers/UIControlledSetting.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using ImGuiNET;
using System;
using T3.Gui.UiHelpers;

namespace T3.Gui.Windows
{
public class UIControlledSetting
{
/// <summary>
/// The generated unique label
/// </summary>
public string uniqueLabel { get; private set; }

/// <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; }

private string tooltip;
private string additionalNotes;
private Func<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;

/// <summary>
/// For the sake of simple use of the optional parameters and populating/maintaining many settings, the recommended way to call this constructor is:
/// <code>
/// new UIControlledSetting
/// (
/// label: "My Setting",
/// tooltip: "The global scale of all rendered UI in the application",
/// guiFunc: (string guiLabel) => CustomComponents.FloatValueEdit(guiLabel, ref UserSettings.Config.UiScaleFactor, 0.01f, 0.5f, 3f),
/// OnValueChanged: () => //your action
/// );
/// </code>
/// </summary>
/// <param name="label">The label to display next to the gui control</param>
/// <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="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)
{
cleanLabel = label;
uniqueLabel = $"{label}##{countForUniqueID--}";

this.guiFunc = () => guiFunc(uniqueLabel);
this.tooltip = tooltip;
this.additionalNotes = additionalNotes;
this.OnValueChanged = OnValueChanged;
}

/// <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()
{
//if (!string.IsNullOrEmpty(tooltip))
//{
// if (ImGui.IsItemHovered(ImGuiHoveredFlags.AllowWhenDisabled))
// {
// ImGui.SetTooltip(tooltip);
// }
//}

var changed = guiFunc.Invoke();

if (!string.IsNullOrEmpty(tooltip))
{
CustomComponents.TooltipForLastItem(tooltip, additionalNotes);
}

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

return changed;
}
}
}
Loading

0 comments on commit 7a85fd7

Please sign in to comment.