From f679f6590477297827429adbe41f9cf22c01fafc Mon Sep 17 00:00:00 2001 From: matt-edmondson Date: Wed, 20 Mar 2024 14:40:20 +1100 Subject: [PATCH] Hoist popup functionality out into its own base class so that it can be used by other projects wanting modal popups --- ImGuiWidgets/PopupInput.cs | 35 +++++++--------------- ImGuiWidgets/PopupModal.cs | 60 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 25 deletions(-) create mode 100644 ImGuiWidgets/PopupModal.cs diff --git a/ImGuiWidgets/PopupInput.cs b/ImGuiWidgets/PopupInput.cs index 1705c4b..e180389 100644 --- a/ImGuiWidgets/PopupInput.cs +++ b/ImGuiWidgets/PopupInput.cs @@ -7,20 +7,11 @@ namespace ktsu.io.ImGuiWidgets; /// /// The type of the input value. /// The type of the derived class for CRTP. -public abstract class PopupInput where TDerived : PopupInput, new() +public abstract class PopupInput : PopupModal where TDerived : PopupInput, new() { private TInput? cachedValue; - - private string Title { get; set; } = string.Empty; - private string Label { get; set; } = string.Empty; private Action OnConfirm { get; set; } = null!; - private bool WasOpen { get; set; } - - /// - /// Gets the id of the popup window. - /// - /// The id of the popup window. - protected string PopupName => $"{Title}###PopupInput{Title}"; + private string Label { get; set; } = string.Empty; /// /// Open the popup and set the title, label, and default value. @@ -31,21 +22,25 @@ namespace ktsu.io.ImGuiWidgets; /// A callback to handle the new input value. public void Open(string title, string label, TInput defaultValue, Action onConfirm) { - Title = title; Label = label; OnConfirm = onConfirm; cachedValue = defaultValue; - ImGui.OpenPopup(PopupName); + base.Open(title); } + /// + /// Dont use this method, use the other Open method + /// + public override void Open(string title) => throw new InvalidOperationException("Use the other Open method."); + /// /// Show the popup if it is open. /// /// True if the popup is open. - 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(); @@ -67,17 +62,7 @@ public bool ShowIfOpen() OnConfirm(cachedValue); ImGui.CloseCurrentPopup(); } - - if (ImGui.IsKeyPressed(ImGuiKey.Escape)) - { - ImGui.CloseCurrentPopup(); - } - - ImGui.EndPopup(); } - - WasOpen = result; - return result; } /// diff --git a/ImGuiWidgets/PopupModal.cs b/ImGuiWidgets/PopupModal.cs new file mode 100644 index 0000000..6edbfae --- /dev/null +++ b/ImGuiWidgets/PopupModal.cs @@ -0,0 +1,60 @@ +namespace ktsu.io.ImGuiWidgets; + +using ImGuiNET; + +/// +/// Base class for a modal popup window. +/// +public abstract class PopupModal +{ + private string Title { get; set; } = string.Empty; + + /// + /// Returns false if this is the first frame teh popup is shown, true on subsequent frames. + /// + protected bool WasOpen { get; set; } + + /// + /// Gets the id of the popup window. + /// + /// The id of the popup window. + protected string PopupName => $"{Title}###PopupInput{Title}"; + + /// + /// Open the popup and set the title. + /// + /// The title of the popup window. + public virtual void Open(string title) + { + Title = title; + ImGui.OpenPopup(PopupName); + } + + /// + /// Show the content of the popup. + /// + public abstract void ShowContent(); + + /// + /// Show the popup if it is open. + /// + /// True if the popup is open. + 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; + } +}