From 45c9b2aeebf38ba2fbcdf420d0a5964bdf81c442 Mon Sep 17 00:00:00 2001 From: onepiecefreak3 Date: Sat, 25 Nov 2023 23:58:50 +0100 Subject: [PATCH] Add more key command support; Add support for reacting to key commands in menus; --- ImGui.Forms/Controls/ArrowButton.cs | 7 +++- ImGui.Forms/Controls/Button.cs | 2 +- ImGui.Forms/Controls/Menu/MenuBar.cs | 25 +++++++----- ImGui.Forms/Controls/Menu/MenuBarButton.cs | 15 +++++++- ImGui.Forms/Controls/Menu/MenuBarCheckBox.cs | 4 ++ ImGui.Forms/Controls/Menu/MenuBarItem.cs | 40 +++++++++++++++++++- ImGui.Forms/Controls/Menu/MenuBarMenu.cs | 9 +++++ ImGui.Forms/Controls/Menu/MenuBarRadio.cs | 5 +++ ImGui.Forms/Controls/Menu/MenuBarSplitter.cs | 4 ++ ImGui.Forms/ImGui.Forms.nuspec | 2 +- 10 files changed, 97 insertions(+), 16 deletions(-) diff --git a/ImGui.Forms/Controls/ArrowButton.cs b/ImGui.Forms/Controls/ArrowButton.cs index 2828b0d..ed4d63f 100644 --- a/ImGui.Forms/Controls/ArrowButton.cs +++ b/ImGui.Forms/Controls/ArrowButton.cs @@ -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; @@ -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 @@ -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) @@ -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) diff --git a/ImGui.Forms/Controls/Button.cs b/ImGui.Forms/Controls/Button.cs index 04a82ff..8a9dac8 100644 --- a/ImGui.Forms/Controls/Button.cs +++ b/ImGui.Forms/Controls/Button.cs @@ -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()) diff --git a/ImGui.Forms/Controls/Menu/MenuBar.cs b/ImGui.Forms/Controls/Menu/MenuBar.cs index a8f6d2b..42e2479 100644 --- a/ImGui.Forms/Controls/Menu/MenuBar.cs +++ b/ImGui.Forms/Controls/Menu/MenuBar.cs @@ -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(); diff --git a/ImGui.Forms/Controls/Menu/MenuBarButton.cs b/ImGui.Forms/Controls/Menu/MenuBarButton.cs index 9368b55..512938d 100644 --- a/ImGui.Forms/Controls/Menu/MenuBarButton.cs +++ b/ImGui.Forms/Controls/Menu/MenuBarButton.cs @@ -1,5 +1,6 @@ using System; using ImGui.Forms.Localization; +using ImGui.Forms.Models.IO; using ImGui.Forms.Resources; namespace ImGui.Forms.Controls.Menu @@ -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 @@ -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() diff --git a/ImGui.Forms/Controls/Menu/MenuBarCheckBox.cs b/ImGui.Forms/Controls/Menu/MenuBarCheckBox.cs index 76bc485..88a39f5 100644 --- a/ImGui.Forms/Controls/Menu/MenuBarCheckBox.cs +++ b/ImGui.Forms/Controls/Menu/MenuBarCheckBox.cs @@ -38,6 +38,10 @@ protected override void UpdateInternal() Checked = !Checked; } + protected override void UpdateEventsInternal() + { + } + private int GetHeight() { ApplyStyles(); diff --git a/ImGui.Forms/Controls/Menu/MenuBarItem.cs b/ImGui.Forms/Controls/Menu/MenuBarItem.cs index 2002311..0d8eb7b 100644 --- a/ImGui.Forms/Controls/Menu/MenuBarItem.cs +++ b/ImGui.Forms/Controls/Menu/MenuBarItem.cs @@ -1,4 +1,6 @@ -namespace ImGui.Forms.Controls.Menu +using ImGui.Forms.Models.IO; + +namespace ImGui.Forms.Controls.Menu { public abstract class MenuBarItem { @@ -26,11 +28,25 @@ public void Update() ImGuiNET.ImGui.PopID(); } + internal void UpdateEvents() + { + ImGuiNET.ImGui.PushID(Id); + + UpdateEventsInternal(); + + ImGuiNET.ImGui.PopID(); + } + /// /// Updates the component. /// protected abstract void UpdateInternal(); + /// + /// Executed behaviour based on application events. + /// + protected abstract void UpdateEventsInternal(); + /// /// Applies any styles specific to this component, before is invoked. /// @@ -40,5 +56,27 @@ protected virtual void ApplyStyles() { } /// Removes any styles specific to this component, after is invoked. /// 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; + } } } diff --git a/ImGui.Forms/Controls/Menu/MenuBarMenu.cs b/ImGui.Forms/Controls/Menu/MenuBarMenu.cs index 1bb755e..e7d7904 100644 --- a/ImGui.Forms/Controls/Menu/MenuBarMenu.cs +++ b/ImGui.Forms/Controls/Menu/MenuBarMenu.cs @@ -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(); diff --git a/ImGui.Forms/Controls/Menu/MenuBarRadio.cs b/ImGui.Forms/Controls/Menu/MenuBarRadio.cs index d65ff34..77e95a5 100644 --- a/ImGui.Forms/Controls/Menu/MenuBarRadio.cs +++ b/ImGui.Forms/Controls/Menu/MenuBarRadio.cs @@ -46,6 +46,11 @@ protected override void UpdateInternal() _radioMenu.Update(); } + protected override void UpdateEventsInternal() + { + _radioMenu.UpdateEvents(); + } + private int GetHeight() { ApplyStyles(); diff --git a/ImGui.Forms/Controls/Menu/MenuBarSplitter.cs b/ImGui.Forms/Controls/Menu/MenuBarSplitter.cs index 2c65523..3e61d70 100644 --- a/ImGui.Forms/Controls/Menu/MenuBarSplitter.cs +++ b/ImGui.Forms/Controls/Menu/MenuBarSplitter.cs @@ -8,5 +8,9 @@ protected override void UpdateInternal() { ImGuiNET.ImGui.Separator(); } + + protected override void UpdateEventsInternal() + { + } } } diff --git a/ImGui.Forms/ImGui.Forms.nuspec b/ImGui.Forms/ImGui.Forms.nuspec index 0ca4b18..d45281f 100644 --- a/ImGui.Forms/ImGui.Forms.nuspec +++ b/ImGui.Forms/ImGui.Forms.nuspec @@ -2,7 +2,7 @@ Imgui.Forms - 1.0.42 + 1.0.43 A WinForms-inspired object-oriented framework around Dear ImGui (https://github.com/ocornut/imgui) onepiecefreak