forked from tooll3/t3
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
203d307
commit 7a85fd7
Showing
5 changed files
with
362 additions
and
241 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
Oops, something went wrong.