Skip to content

Commit

Permalink
Add more key command support;
Browse files Browse the repository at this point in the history
Add support for reacting to key commands in menus;
  • Loading branch information
onepiecefreak3 committed Nov 25, 2023
1 parent ceaeb9d commit 45c9b2a
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 16 deletions.
7 changes: 5 additions & 2 deletions ImGui.Forms/Controls/ArrowButton.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using ImGui.Forms.Controls.Base;
using ImGui.Forms.Models;
using ImGui.Forms.Models.IO;
using ImGui.Forms.Resources;
using ImGuiNET;
using Veldrid;
Expand All @@ -9,6 +10,8 @@ namespace ImGui.Forms.Controls
{
public class ArrowButton : Component
{
public KeyCommand KeyAction { get; set; }

public ImGuiDir Direction { get; set; } = ImGuiDir.None;

#region Events
Expand All @@ -22,7 +25,7 @@ public override Size GetSize()
var height = FontResource.GetCurrentLineHeight();
var padding = ImGuiNET.ImGui.GetStyle().FramePadding;

return new Size((int)Math.Ceiling(height + padding.X*2), (int)Math.Ceiling(height + padding.Y*2));
return new Size((int)Math.Ceiling(height + padding.X * 2), (int)Math.Ceiling(height + padding.Y * 2));
}

protected override void UpdateInternal(Rectangle contentRect)
Expand All @@ -36,7 +39,7 @@ protected override void UpdateInternal(Rectangle contentRect)
ImGuiNET.ImGui.PushStyleColor(ImGuiCol.ButtonActive, 0xFF666666);
}

if (ImGuiNET.ImGui.ArrowButton($"##{Id}", Direction) && Enabled)
if ((ImGuiNET.ImGui.ArrowButton($"##{Id}", Direction) || IsKeyDown(KeyAction)) && Enabled)
OnClicked();

if (!enabled)
Expand Down
2 changes: 1 addition & 1 deletion ImGui.Forms/Controls/Button.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected override void UpdateInternal(Rectangle contentRect)

ApplyStyles(enabled, font);

if (ImGuiNET.ImGui.Button(EscapeText(), new Vector2(contentRect.Width, contentRect.Height)) && Enabled)
if ((ImGuiNET.ImGui.Button(EscapeText(), new Vector2(contentRect.Width, contentRect.Height)) || IsKeyDown(KeyAction)) && Enabled)
OnClicked();

if (Tooltip is { IsEmpty: false } && IsHoveredCore())
Expand Down
25 changes: 16 additions & 9 deletions ImGui.Forms/Controls/Menu/MenuBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,23 @@ public void Update()
ImGuiNET.ImGui.PushFont((ImFontPtr)Font);

// Begin menu bar
if (_isMain) ImGuiNET.ImGui.BeginMainMenuBar();
else ImGuiNET.ImGui.BeginMenuBar();
bool isMenuOpen = _isMain ?
ImGuiNET.ImGui.BeginMainMenuBar() :
ImGuiNET.ImGui.BeginMenuBar();
if (isMenuOpen)
{
// Add content to menu bar
foreach (var child in Items)
child.Update();

// Add content to menu bar
foreach (var child in Items)
child.Update();

// End menu bar
if (_isMain) ImGuiNET.ImGui.EndMainMenuBar();
else ImGuiNET.ImGui.EndMenuBar();
if (_isMain) ImGuiNET.ImGui.EndMainMenuBar();
else ImGuiNET.ImGui.EndMenuBar();
}
else
{
foreach (var child in Items)
child.UpdateEvents();
}

if (Font != null)
ImGuiNET.ImGui.PopFont();
Expand Down
15 changes: 13 additions & 2 deletions ImGui.Forms/Controls/Menu/MenuBarButton.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using ImGui.Forms.Localization;
using ImGui.Forms.Models.IO;
using ImGui.Forms.Resources;

namespace ImGui.Forms.Controls.Menu
Expand All @@ -10,6 +11,8 @@ public class MenuBarButton : MenuBarItem

public LocalizedString Text { get; set; } = string.Empty;

public KeyCommand KeyAction { get; set; }

public override int Height => GetHeight();

#region Events
Expand All @@ -21,9 +24,17 @@ public class MenuBarButton : MenuBarItem
protected override void UpdateInternal()
{
// Add menu button
if (ImGuiNET.ImGui.MenuItem(Text, Enabled))
if (ImGuiNET.ImGui.MenuItem(Text, Enabled) || IsKeyDown(KeyAction))
// Execute click event, if set
Clicked?.Invoke(this, EventArgs.Empty);
}

protected override void UpdateEventsInternal()
{
// Add menu button
if (IsKeyDown(KeyAction))
// Execute click event, if set
Clicked?.Invoke(this, new EventArgs());
Clicked?.Invoke(this, EventArgs.Empty);
}

private int GetHeight()
Expand Down
4 changes: 4 additions & 0 deletions ImGui.Forms/Controls/Menu/MenuBarCheckBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ protected override void UpdateInternal()
Checked = !Checked;
}

protected override void UpdateEventsInternal()
{
}

private int GetHeight()
{
ApplyStyles();
Expand Down
40 changes: 39 additions & 1 deletion ImGui.Forms/Controls/Menu/MenuBarItem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace ImGui.Forms.Controls.Menu
using ImGui.Forms.Models.IO;

namespace ImGui.Forms.Controls.Menu
{
public abstract class MenuBarItem
{
Expand Down Expand Up @@ -26,11 +28,25 @@ public void Update()
ImGuiNET.ImGui.PopID();
}

internal void UpdateEvents()
{
ImGuiNET.ImGui.PushID(Id);

UpdateEventsInternal();

ImGuiNET.ImGui.PopID();
}

/// <summary>
/// Updates the component.
/// </summary>
protected abstract void UpdateInternal();

/// <summary>
/// Executed behaviour based on application events.
/// </summary>
protected abstract void UpdateEventsInternal();

/// <summary>
/// Applies any styles specific to this component, before <see cref="UpdateInternal"/> is invoked.
/// </summary>
Expand All @@ -40,5 +56,27 @@ protected virtual void ApplyStyles() { }
/// Removes any styles specific to this component, after <see cref="UpdateInternal"/> is invoked.
/// </summary>
protected virtual void RemoveStyles() { }

protected bool IsKeyDown(KeyCommand keyDown)
{
if (keyDown == default)
return false;

if (!Application.Instance.TryGetKeyDownCommand(out KeyCommand internalKeyDown))
return false;

return keyDown == internalKeyDown;
}

protected bool IsKeyUp(KeyCommand keyUp)
{
if (keyUp == default)
return false;

if (!Application.Instance.TryGetKeyDownCommand(out KeyCommand internalKeyUp))
return false;

return keyUp == internalKeyUp;
}
}
}
9 changes: 9 additions & 0 deletions ImGui.Forms/Controls/Menu/MenuBarMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,19 @@ protected override void UpdateInternal()

ImGuiNET.ImGui.EndMenu();
}
else
UpdateEventsInternal();


ImGuiNET.ImGui.PopStyleVar(2);
}

protected override void UpdateEventsInternal()
{
foreach (var item in Items)
item.UpdateEvents();
}

private int GetHeight()
{
ApplyStyles();
Expand Down
5 changes: 5 additions & 0 deletions ImGui.Forms/Controls/Menu/MenuBarRadio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ protected override void UpdateInternal()
_radioMenu.Update();
}

protected override void UpdateEventsInternal()
{
_radioMenu.UpdateEvents();
}

private int GetHeight()
{
ApplyStyles();
Expand Down
4 changes: 4 additions & 0 deletions ImGui.Forms/Controls/Menu/MenuBarSplitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@ protected override void UpdateInternal()
{
ImGuiNET.ImGui.Separator();
}

protected override void UpdateEventsInternal()
{
}
}
}
2 changes: 1 addition & 1 deletion ImGui.Forms/ImGui.Forms.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>Imgui.Forms</id>
<version>1.0.42</version>
<version>1.0.43</version>
<description>A WinForms-inspired object-oriented framework around Dear ImGui (https://github.com/ocornut/imgui)</description>

<authors>onepiecefreak</authors>
Expand Down

0 comments on commit 45c9b2a

Please sign in to comment.