-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hoist popup functionality out into its own base class so that it can …
…be used by other projects wanting modal popups
- Loading branch information
1 parent
b1b49c2
commit f679f65
Showing
2 changed files
with
70 additions
and
25 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
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,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; | ||
} | ||
} |