diff --git a/ImGui.Forms/Controls/Menu/MenuBarButton.cs b/ImGui.Forms/Controls/Menu/MenuBarButton.cs index 512938d..35405db 100644 --- a/ImGui.Forms/Controls/Menu/MenuBarButton.cs +++ b/ImGui.Forms/Controls/Menu/MenuBarButton.cs @@ -7,8 +7,6 @@ namespace ImGui.Forms.Controls.Menu { public class MenuBarButton : MenuBarItem { - public bool Enabled { get; set; } = true; - public LocalizedString Text { get; set; } = string.Empty; public KeyCommand KeyAction { get; set; } diff --git a/ImGui.Forms/Controls/Menu/MenuBarCheckBox.cs b/ImGui.Forms/Controls/Menu/MenuBarCheckBox.cs index 88a39f5..074a93c 100644 --- a/ImGui.Forms/Controls/Menu/MenuBarCheckBox.cs +++ b/ImGui.Forms/Controls/Menu/MenuBarCheckBox.cs @@ -8,8 +8,6 @@ public class MenuBarCheckBox : MenuBarItem { private bool _checked; - public bool Enabled { get; set; } = true; - public LocalizedString Text { get; set; } = string.Empty; public override int Height => GetHeight(); diff --git a/ImGui.Forms/Controls/Menu/MenuBarItem.cs b/ImGui.Forms/Controls/Menu/MenuBarItem.cs index 0d8eb7b..3d76cd8 100644 --- a/ImGui.Forms/Controls/Menu/MenuBarItem.cs +++ b/ImGui.Forms/Controls/Menu/MenuBarItem.cs @@ -14,6 +14,11 @@ public abstract class MenuBarItem /// public abstract int Height { get; } + /// + /// Determines, if the menu item is enabled. + /// + public virtual bool Enabled { get; set; } = true; + /// /// Updates the menu bar component to ImGui. /// diff --git a/ImGui.Forms/Controls/Menu/MenuBarMenu.cs b/ImGui.Forms/Controls/Menu/MenuBarMenu.cs index e7d7904..7533454 100644 --- a/ImGui.Forms/Controls/Menu/MenuBarMenu.cs +++ b/ImGui.Forms/Controls/Menu/MenuBarMenu.cs @@ -8,8 +8,6 @@ namespace ImGui.Forms.Controls.Menu { public class MenuBarMenu : MenuBarItem { - public bool Enabled { get; set; } = true; - public LocalizedString Text { get; set; } = string.Empty; public Vector2 Padding { get; set; } = new Vector2(8, 8); diff --git a/ImGui.Forms/Controls/Menu/MenuBarRadio.cs b/ImGui.Forms/Controls/Menu/MenuBarRadio.cs index 77e95a5..1f8baae 100644 --- a/ImGui.Forms/Controls/Menu/MenuBarRadio.cs +++ b/ImGui.Forms/Controls/Menu/MenuBarRadio.cs @@ -21,6 +21,12 @@ public LocalizedString Text public MenuBarCheckBox SelectedItem { get; private set; } + public override bool Enabled + { + get => _radioMenu.Enabled; + set => _radioMenu.Enabled = value; + } + #region Events public event EventHandler SelectedItemChanged; diff --git a/ImGui.Forms/Controls/TabControl.cs b/ImGui.Forms/Controls/TabControl.cs index ed3241d..4c86f7d 100644 --- a/ImGui.Forms/Controls/TabControl.cs +++ b/ImGui.Forms/Controls/TabControl.cs @@ -25,6 +25,9 @@ public TabPage SelectedPage get => _selectedPageTemp ?? _selectedPage; set { + if (!Enabled) + return; + _selectedTabPageCount = 2; _selectedPageTemp = value; } @@ -50,57 +53,68 @@ protected override async void UpdateInternal(Rectangle contentRect) if (ImGuiNET.ImGui.BeginTabBar($"{Id}", ImGuiTabBarFlags.None)) { var toRemovePages = new HashSet(); - foreach (var page in Pages.ToArray()) + foreach (TabPage page in Pages.ToArray()) { var pageFlags = ImGuiTabItemFlags.None; if (page.HasChanges) pageFlags |= ImGuiTabItemFlags.UnsavedDocument; if (wasManuallyChanged && _selectedPageTemp == page) pageFlags |= ImGuiTabItemFlags.SetSelected; + if (!Enabled && _selectedPage == page) pageFlags |= ImGuiTabItemFlags.SetSelected; ImGuiNET.ImGui.PushID(Application.Instance.IdFactory.Get(page)); var stillOpen = true; - if (ImGuiNET.ImGui.BeginTabItem(page.Title, ref stillOpen, pageFlags) && !wasManuallyChanged) + if (ImGuiNET.ImGui.BeginTabItem(page.Title, ref stillOpen, pageFlags)) { - // Check selected page status - var wasChanged = _selectedPage != page; + if (!wasManuallyChanged) + { + // Check selected page status + var wasChanged = _selectedPage != page; - if (wasChanged) - _selectedPage?.Content?.SetTabInactiveInternal(); + if (wasChanged && Enabled) + { + _selectedPage?.Content?.SetTabInactiveInternal(); + _selectedPage = page; + } - _selectedPageTemp = null; - _selectedPage = page; + _selectedPageTemp = null; - if (wasChanged) - OnSelectedPageChanged(); + if (wasChanged && Enabled) + OnSelectedPageChanged(); - // Remove tab page on middle mouse click - if (ImGuiNET.ImGui.IsItemHovered() && ImGuiNET.ImGui.IsMouseClicked(ImGuiMouseButton.Middle)) - toRemovePages.Add(page); + // Remove tab page on middle mouse click + if (ImGuiNET.ImGui.IsItemHovered() && ImGuiNET.ImGui.IsMouseClicked(ImGuiMouseButton.Middle) && Enabled) + toRemovePages.Add(page); - // Draw content of tab page - var yPos = (int)ImGuiNET.ImGui.GetCursorPosY(); + // Draw content of tab page + var yPos = (int)ImGuiNET.ImGui.GetCursorPosY(); - var pageWidth = page.Content.GetWidth(contentRect.Width); - var pageHeight = page.Content.GetHeight(contentRect.Height - yPos); + var pageWidth = page.Content.GetWidth(contentRect.Width); + var pageHeight = page.Content.GetHeight(contentRect.Height - yPos); - if (ImGuiNET.ImGui.BeginChild($"##{Id}-in", new Vector2(pageWidth, pageHeight), ImGuiChildFlags.None, ImGuiWindowFlags.None)) - page.Content.Update(new Rectangle(contentRect.X, contentRect.Y + yPos, pageWidth, pageHeight)); + if (ImGuiNET.ImGui.BeginChild($"##{Id}-in", new Vector2(pageWidth, pageHeight), ImGuiChildFlags.None, ImGuiWindowFlags.None)) + page.Content.Update(new Rectangle(contentRect.X, contentRect.Y + yPos, pageWidth, pageHeight)); - ImGuiNET.ImGui.EndChild(); + ImGuiNET.ImGui.EndChild(); - ImGuiNET.ImGui.EndTabItem(); + ImGuiNET.ImGui.EndTabItem(); + } + } + else + { + // If tab could not be rendered by ImGui, but it should still be shown, set it to inactive + page.Content?.SetTabInactiveInternal(); } ImGuiNET.ImGui.PopID(); - if (!stillOpen) + if (!stillOpen && Enabled) toRemovePages.Add(page); } ImGuiNET.ImGui.EndTabBar(); // Handle pages to remove asynchronously - foreach (var toRemove in toRemovePages) + foreach (TabPage toRemove in toRemovePages) await RemovePageInternal(toRemove); } } @@ -111,6 +125,9 @@ protected override async void UpdateInternal(Rectangle contentRect) /// the to add. public void AddPage(TabPage page) { + if (!Enabled) + return; + _pages.Add(page); } @@ -121,6 +138,9 @@ public void AddPage(TabPage page) /// Does not invoke and . public void RemovePage(TabPage page) { + if (!Enabled) + return; + if (_selectedPage == page) _selectedPage = null; @@ -143,7 +163,7 @@ private async Task RemovePageInternal(TabPage page) private void OnSelectedPageChanged() { - SelectedPageChanged?.Invoke(this, new EventArgs()); + SelectedPageChanged?.Invoke(this, EventArgs.Empty); } private async Task OnPageRemoving(TabPage page) diff --git a/ImGui.Forms/Controls/TabPage.cs b/ImGui.Forms/Controls/TabPage.cs index fdd44d2..c31135b 100644 --- a/ImGui.Forms/Controls/TabPage.cs +++ b/ImGui.Forms/Controls/TabPage.cs @@ -8,8 +8,8 @@ public class TabPage public LocalizedString Title { get; set; } public Component Content { get; } - - public bool HasChanges { get; set; } = false; + + public bool HasChanges { get; set; } public TabPage(Component content) { diff --git a/ImGui.Forms/Controls/Tree/TreeView.cs b/ImGui.Forms/Controls/Tree/TreeView.cs index 8013e74..0a84a91 100644 --- a/ImGui.Forms/Controls/Tree/TreeView.cs +++ b/ImGui.Forms/Controls/Tree/TreeView.cs @@ -4,7 +4,6 @@ using System.Numerics; using ImGui.Forms.Controls.Base; using ImGui.Forms.Controls.Menu; -using ImGui.Forms.Extensions; using ImGui.Forms.Models.IO; using ImGuiNET; using Veldrid; @@ -102,7 +101,8 @@ private bool UpdateNodes(IList> nodes, ref bool nodeHovered) var isAnyNodeFocused = false; foreach (var node in nodes.ToArray()) { - var flags = ImGuiTreeNodeFlags.OpenOnDoubleClick | ImGuiTreeNodeFlags.OpenOnArrow | ImGuiTreeNodeFlags.FramePadding | ImGuiTreeNodeFlags.SpanAvailWidth; + ImGuiTreeNodeFlags flags = ImGuiTreeNodeFlags.FramePadding | ImGuiTreeNodeFlags.SpanAvailWidth; + if (Enabled) flags |= ImGuiTreeNodeFlags.OpenOnDoubleClick | ImGuiTreeNodeFlags.OpenOnArrow; if (node.Nodes.Count <= 0) flags |= ImGuiTreeNodeFlags.Leaf; if (SelectedNode == node) flags |= ImGuiTreeNodeFlags.Selected; @@ -127,7 +127,7 @@ private bool UpdateNodes(IList> nodes, ref bool nodeHovered) ImGuiNET.ImGui.PopStyleVar(2); bool changedExpansion = expanded != node.IsExpanded; - if (changedExpansion) + if (changedExpansion && Enabled) node.IsExpanded = expanded; if (node.Font != null) @@ -142,7 +142,7 @@ private bool UpdateNodes(IList> nodes, ref bool nodeHovered) nodeHovered |= ImGuiNET.ImGui.IsItemHovered(); // Change selected node, if expansion of node did not change, and mouse is over node - if (!changedExpansion && IsTreeNodeClicked() && SelectedNode != node) + if (!changedExpansion && IsTreeNodeClicked() && SelectedNode != node && Enabled) SelectedNode = node; // Add context, only if mouse is over a tree node @@ -150,13 +150,11 @@ private bool UpdateNodes(IList> nodes, ref bool nodeHovered) ContextMenu?.Update(); // Add children nodes, if parent is expanded - if (!node.IsExpanded) - continue; - - if (node.Nodes.Count > 0) + if (node.IsExpanded && node.Nodes.Count > 0) isAnyNodeFocused |= UpdateNodes(node.Nodes, ref nodeHovered); - ImGuiNET.ImGui.TreePop(); + if (expanded) + ImGuiNET.ImGui.TreePop(); } return isAnyNodeFocused; @@ -183,9 +181,9 @@ private void ChangeSelectedNodeOnArrowKey() private readonly IList _pressedArrows = new List(2); private void UpdateArrowKeyState() { - if (IsKeyDown(PreviousNodeKey) && !_pressedArrows.Contains(PreviousNodeKey)) + if (IsKeyDown(PreviousNodeKey) && !_pressedArrows.Contains(PreviousNodeKey) && Enabled) _pressedArrows.Add(PreviousNodeKey); - if (IsKeyDown(NextNodeKey) && !_pressedArrows.Contains(NextNodeKey)) + if (IsKeyDown(NextNodeKey) && !_pressedArrows.Contains(NextNodeKey) && Enabled) _pressedArrows.Add(NextNodeKey); if (IsKeyUp(PreviousNodeKey)) _pressedArrows.Remove(PreviousNodeKey); diff --git a/ImGui.Forms/ImGui.Forms.nuspec b/ImGui.Forms/ImGui.Forms.nuspec index f9c1f7d..b49f4eb 100644 --- a/ImGui.Forms/ImGui.Forms.nuspec +++ b/ImGui.Forms/ImGui.Forms.nuspec @@ -2,12 +2,12 @@ Imgui.Forms - 1.0.58 + 1.0.59 A WinForms-inspired object-oriented framework around Dear ImGui (https://github.com/ocornut/imgui) onepiecefreak onepiecefreak - Copyright (c) 2021 onepiecefreak + Copyright (c) 2021-2024 onepiecefreak https://github.com/FanTranslatorsInternational/ImGui.Forms ImGui;Forms;ImGui.NET;GUI