Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-edmondson committed Feb 28, 2024
2 parents 73802c6 + ae4fa38 commit af96558
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -397,5 +397,6 @@ FodyWeavers.xsd
# JetBrains Rider
*.sln.iml

# Custom medmondson
# Custom entries by https://github.com/ktsu-io
.project.props.default
.project.props.default-winforms
2 changes: 1 addition & 1 deletion ImGuiWidgets/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net7.0</TargetFramework>
<UseWindowsForms>false</UseWindowsForms>
<VersionPrefix>1.0.0</VersionPrefix>
<VersionSuffix>alpha.6</VersionSuffix>
<VersionSuffix>alpha.8</VersionSuffix>
<PackageDescription>A library for custom widgets using ImGui.NET.</PackageDescription>
</PropertyGroup>

Expand Down
136 changes: 136 additions & 0 deletions ImGuiWidgets/PopupInput.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
namespace ktsu.io.ImGuiWidgets;

using ImGuiNET;

/// <summary>
/// Base class for a popup input window.
/// </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()
{
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}";

/// <summary>
/// Open the popup and set the title, label, and default value.
/// </summary>
/// <param name="title">The title of the popup window.</param>
/// <param name="label">The label of the input field.</param>
/// <param name="defaultValue">The default value of the input field.</param>
/// <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);
}

/// <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 (cachedValue is not null && ImGui.BeginPopupModal(PopupName, ref result, ImGuiWindowFlags.AlwaysAutoResize))
{
ImGui.TextUnformatted(Label);
ImGui.NewLine();

if (!WasOpen && !ImGui.IsItemFocused())
{
ImGui.SetKeyboardFocusHere();
}

if (ShowEdit(ref cachedValue))
{
OnConfirm(cachedValue);
ImGui.CloseCurrentPopup();
}

ImGui.SameLine();
if (ImGui.Button($"OK###{PopupName}_OK"))
{
OnConfirm(cachedValue);
ImGui.CloseCurrentPopup();
}

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

ImGui.EndPopup();
}

WasOpen = result;
return result;
}

/// <summary>
/// Show the input field for the derived class.
/// </summary>
/// <param name="value">The input value.</param>
/// <returns>True if the input field is changed.</returns>
protected abstract bool ShowEdit(ref TInput value);
}

/// <summary>
/// A popup input window for strings.
/// </summary>
public class PopupInputString : PopupInput<string, PopupInputString>
{
/// <summary>
/// Show the input field for strings.
/// </summary>
/// <param name="value">The input value.</param>
/// <returns>True if Enter is pressed.</returns>
protected override bool ShowEdit(ref string value) => ImGui.InputText($"###{PopupName}_INPUT", ref value, 100, ImGuiInputTextFlags.EnterReturnsTrue);
}

/// <summary>
/// A popup input window for integers.
/// </summary>
public class PopupInputInt : PopupInput<int, PopupInputInt>
{
/// <summary>
/// Show the input field for integers.
/// </summary>
/// <param name="value">The input value.</param>
/// <returns>False</returns>
protected override bool ShowEdit(ref int value)
{
ImGui.InputInt("##Input", ref value);
return false;
}
}

/// <summary>
/// A popup input window for floats.
/// </summary>
public class PopupInputFloat : PopupInput<float, PopupInputFloat>
{
/// <summary>
/// Show the input field for floats.
/// </summary>
/// <param name="value">The input value.</param>
/// <returns>False</returns>
protected override bool ShowEdit(ref float value)
{
ImGui.InputFloat("##Input", ref value);
return false;
}
}
4 changes: 1 addition & 3 deletions ImGuiWidgetsDemo/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<Project>
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />
<PropertyGroup>
<TargetFramework>net7.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<EnableWindowsTargeting>true</EnableWindowsTargeting>
<TargetFramework>net7.0</TargetFramework>
<OutputType>Exe</OutputType>
<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down
12 changes: 12 additions & 0 deletions ImGuiWidgetsDemo/ImGuiWidgetsDemo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,24 @@ private static void Main(string[] args)
}

private static float value = 0.5f;

private readonly PopupInputString popupInputString = new();
private string inputString = "String Input Popup";
private void Tick(float dt)
{
float ms = dt * 1000;
Knob.Draw("DT", ref ms, 0, 10, 150f);
ImGui.SameLine();
Knob.Draw("Value", ref value, 0f, 1f, 150f);
if (ImGui.Button(inputString))
{
popupInputString.Open("Enter a string", "Enter", "Yeet", (string result) =>
{
inputString = result;
});
}

popupInputString.ShowIfOpen();
}

private void ShowMenu()
Expand Down
2 changes: 1 addition & 1 deletion ImGuiWidgetsDemo/ImGuiWidgetsDemo.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<PackageReference Include="ktsu.io.ImGuiApp" Version="1.0.0-alpha.6" />
<PackageReference Include="ktsu.io.ImGuiApp" Version="1.0.0-alpha.7" />
</ItemGroup>

<ItemGroup>
Expand Down
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# ImGuiWidgets

Knob Widget
A library for custom widgets using ImGui.NET.

Includes:

Rotary Knob

Resizable Layout Dividers

Input Popups
- String
- Integer
- Float

0 comments on commit af96558

Please sign in to comment.