Skip to content

Commit

Permalink
Allow more disablement of controls;
Browse files Browse the repository at this point in the history
  • Loading branch information
onepiecefreak3 committed Jul 30, 2024
1 parent aa35079 commit 2edbe25
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 45 deletions.
2 changes: 0 additions & 2 deletions ImGui.Forms/Controls/Menu/MenuBarButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
2 changes: 0 additions & 2 deletions ImGui.Forms/Controls/Menu/MenuBarCheckBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 5 additions & 0 deletions ImGui.Forms/Controls/Menu/MenuBarItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ public abstract class MenuBarItem
/// </summary>
public abstract int Height { get; }

/// <summary>
/// Determines, if the menu item is enabled.
/// </summary>
public virtual bool Enabled { get; set; } = true;

/// <summary>
/// Updates the menu bar component to ImGui.
/// </summary>
Expand Down
2 changes: 0 additions & 2 deletions ImGui.Forms/Controls/Menu/MenuBarMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions ImGui.Forms/Controls/Menu/MenuBarRadio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
68 changes: 44 additions & 24 deletions ImGui.Forms/Controls/TabControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public TabPage SelectedPage
get => _selectedPageTemp ?? _selectedPage;
set
{
if (!Enabled)
return;

_selectedTabPageCount = 2;
_selectedPageTemp = value;
}
Expand All @@ -50,57 +53,68 @@ protected override async void UpdateInternal(Rectangle contentRect)
if (ImGuiNET.ImGui.BeginTabBar($"{Id}", ImGuiTabBarFlags.None))
{
var toRemovePages = new HashSet<TabPage>();
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);
}
}
Expand All @@ -111,6 +125,9 @@ protected override async void UpdateInternal(Rectangle contentRect)
/// <param name="page">the <see cref="TabPage"/> to add.</param>
public void AddPage(TabPage page)
{
if (!Enabled)
return;

_pages.Add(page);
}

Expand All @@ -121,6 +138,9 @@ public void AddPage(TabPage page)
/// <remarks>Does not invoke <see cref="PageRemoving"/> and <see cref="PageRemoved"/>.</remarks>
public void RemovePage(TabPage page)
{
if (!Enabled)
return;

if (_selectedPage == page)
_selectedPage = null;

Expand All @@ -143,7 +163,7 @@ private async Task<bool> RemovePageInternal(TabPage page)

private void OnSelectedPageChanged()
{
SelectedPageChanged?.Invoke(this, new EventArgs());
SelectedPageChanged?.Invoke(this, EventArgs.Empty);
}

private async Task<bool> OnPageRemoving(TabPage page)
Expand Down
4 changes: 2 additions & 2 deletions ImGui.Forms/Controls/TabPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
20 changes: 9 additions & 11 deletions ImGui.Forms/Controls/Tree/TreeView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -102,7 +101,8 @@ private bool UpdateNodes(IList<TreeNode<TNodeData>> 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;

Expand All @@ -127,7 +127,7 @@ private bool UpdateNodes(IList<TreeNode<TNodeData>> 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)
Expand All @@ -142,21 +142,19 @@ private bool UpdateNodes(IList<TreeNode<TNodeData>> 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
if (SelectedNode == node)
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;
Expand All @@ -183,9 +181,9 @@ private void ChangeSelectedNodeOnArrowKey()
private readonly IList<KeyCommand> _pressedArrows = new List<KeyCommand>(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);
Expand Down
4 changes: 2 additions & 2 deletions ImGui.Forms/ImGui.Forms.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
<package >
<metadata>
<id>Imgui.Forms</id>
<version>1.0.58</version>
<version>1.0.59</version>
<description>A WinForms-inspired object-oriented framework around Dear ImGui (https://github.com/ocornut/imgui)</description>

<authors>onepiecefreak</authors>
<owners>onepiecefreak</owners>
<copyright>Copyright (c) 2021 onepiecefreak</copyright>
<copyright>Copyright (c) 2021-2024 onepiecefreak</copyright>

<projectUrl>https://github.com/FanTranslatorsInternational/ImGui.Forms</projectUrl>
<tags>ImGui;Forms;ImGui.NET;GUI</tags>
Expand Down

0 comments on commit 2edbe25

Please sign in to comment.