Skip to content

Commit

Permalink
Hoist popup functionality out into its own base class so that it can …
Browse files Browse the repository at this point in the history
…be used by other projects wanting modal popups
  • Loading branch information
matt-edmondson committed Mar 20, 2024
1 parent b1b49c2 commit f679f65
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 25 deletions.
35 changes: 10 additions & 25 deletions ImGuiWidgets/PopupInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,11 @@ namespace ktsu.io.ImGuiWidgets;
/// </summary>
/// <typeparam name="TInput">The type of the input value.</typeparam>
/// <typeparam name="TDerived">The type of the derived class for CRTP.</typeparam>
public abstract class PopupInput<TInput, TDerived> where TDerived : PopupInput<TInput, TDerived>, new()
public abstract class PopupInput<TInput, TDerived> : PopupModal where TDerived : PopupInput<TInput, TDerived>, new()
{
private TInput? cachedValue;

private string Title { get; set; } = string.Empty;
private string Label { get; set; } = string.Empty;
private Action<TInput> OnConfirm { get; set; } = null!;
private bool WasOpen { get; set; }

/// <summary>
/// Gets the id of the popup window.
/// </summary>
/// <returns>The id of the popup window.</returns>
protected string PopupName => $"{Title}###PopupInput{Title}";
private string Label { get; set; } = string.Empty;

/// <summary>
/// Open the popup and set the title, label, and default value.
Expand All @@ -31,21 +22,25 @@ namespace ktsu.io.ImGuiWidgets;
/// <param name="onConfirm">A callback to handle the new input value.</param>
public void Open(string title, string label, TInput defaultValue, Action<TInput> onConfirm)
{
Title = title;
Label = label;
OnConfirm = onConfirm;
cachedValue = defaultValue;
ImGui.OpenPopup(PopupName);
base.Open(title);
}

/// <summary>
/// Dont use this method, use the other Open method
/// </summary>
public override void Open(string title) => throw new InvalidOperationException("Use the other Open method.");

/// <summary>
/// Show the popup if it is open.
/// </summary>
/// <returns>True if the popup is open.</returns>
public bool ShowIfOpen()
public override void ShowContent()
{
bool result = ImGui.IsPopupOpen(PopupName);
if (cachedValue is not null && ImGui.BeginPopupModal(PopupName, ref result, ImGuiWindowFlags.AlwaysAutoResize))
if (cachedValue is not null)
{
ImGui.TextUnformatted(Label);
ImGui.NewLine();
Expand All @@ -67,17 +62,7 @@ public bool ShowIfOpen()
OnConfirm(cachedValue);
ImGui.CloseCurrentPopup();
}

if (ImGui.IsKeyPressed(ImGuiKey.Escape))
{
ImGui.CloseCurrentPopup();
}

ImGui.EndPopup();
}

WasOpen = result;
return result;
}

/// <summary>
Expand Down
60 changes: 60 additions & 0 deletions ImGuiWidgets/PopupModal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
namespace ktsu.io.ImGuiWidgets;

using ImGuiNET;

/// <summary>
/// Base class for a modal popup window.
/// </summary>
public abstract class PopupModal
{
private string Title { get; set; } = string.Empty;

/// <summary>
/// Returns false if this is the first frame teh popup is shown, true on subsequent frames.
/// </summary>
protected bool WasOpen { get; set; }

/// <summary>
/// Gets the id of the popup window.
/// </summary>
/// <returns>The id of the popup window.</returns>
protected string PopupName => $"{Title}###PopupInput{Title}";

/// <summary>
/// Open the popup and set the title.
/// </summary>
/// <param name="title">The title of the popup window.</param>
public virtual void Open(string title)
{
Title = title;
ImGui.OpenPopup(PopupName);
}

/// <summary>
/// Show the content of the popup.
/// </summary>
public abstract void ShowContent();

/// <summary>
/// Show the popup if it is open.
/// </summary>
/// <returns>True if the popup is open.</returns>
public bool ShowIfOpen()
{
bool result = ImGui.IsPopupOpen(PopupName);
if (ImGui.BeginPopupModal(PopupName, ref result, ImGuiWindowFlags.AlwaysAutoResize))
{
ShowContent();

if (ImGui.IsKeyPressed(ImGuiKey.Escape))
{
ImGui.CloseCurrentPopup();
}

ImGui.EndPopup();
}

WasOpen = result;
return result;
}
}

0 comments on commit f679f65

Please sign in to comment.