Skip to content

Commit

Permalink
Make IdFactory static;
Browse files Browse the repository at this point in the history
Refactor member visibilities in Form;
Bump to version 1.1.3;
  • Loading branch information
onepiecefreak3 committed Sep 29, 2024
1 parent d9dc8d9 commit 310ae02
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 58 deletions.
15 changes: 4 additions & 11 deletions ImGui.Forms/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,13 @@ public class Application

#region Properties

#region Factories

public IdFactory IdFactory { get; private set; }

internal ImageFactory ImageFactory { get; private set; }

#endregion

public Form MainForm => _executionContext.MainForm;

internal Sdl2Window Window => _executionContext.Window;

public ILocalizer Localizer { get; }
internal ILocalizer Localizer { get; }

internal ImageFactory ImageFactory { get; private set; }

#endregion

Expand Down Expand Up @@ -124,8 +118,7 @@ private void CreateApplication(Form form)
CreateWindow(form, out var window, out var gd);

_executionContext = new ExecutionContext(form, gd, window);

IdFactory = new IdFactory();

ImageFactory = new ImageFactory(gd, _executionContext.Renderer);

FontFactory.Initialize(ImGuiNET.ImGui.GetIO(), _executionContext.Renderer);
Expand Down
3 changes: 2 additions & 1 deletion ImGui.Forms/Controls/Base/Component.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using ImGui.Forms.Factories;
using ImGui.Forms.Models;
using ImGui.Forms.Models.IO;
using ImGuiNET;
Expand All @@ -16,7 +17,7 @@ public abstract class Component
/// <summary>
/// The Id for this component.
/// </summary>
public int Id => Application.Instance.IdFactory.Get(this);
public int Id => IdFactory.Get(this);

/// <summary>
/// Declares if a component is visible and should be drawn with its content.
Expand Down
3 changes: 2 additions & 1 deletion ImGui.Forms/Controls/Lists/ImageList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using ImGui.Forms.Controls.Base;
using ImGui.Forms.Controls.Layouts;
using ImGui.Forms.Extensions;
using ImGui.Forms.Factories;
using ImGui.Forms.Models;
using ImGui.Forms.Resources;
using ImGui.Forms.Support;
Expand Down Expand Up @@ -71,7 +72,7 @@ protected override void UpdateInternal(Rectangle contentRect)
for (var i = 0; i < localItems.Length; i++)
{
var item = localItems[i];
var itemId = Application.Instance.IdFactory.Get(item);
var itemId = IdFactory.Get(item);

var itemPos = new Vector2(Padding.X, i * itemHeight + Padding.Y + i * ItemPadding);
var contentItemPos = contentPos + itemPos;
Expand Down
3 changes: 2 additions & 1 deletion ImGui.Forms/Controls/Menu/ContextMenu.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using ImGui.Forms.Factories;
using ImGuiNET;

namespace ImGui.Forms.Controls.Menu
Expand All @@ -11,7 +12,7 @@ public class ContextMenu
/// <summary>
/// The Id for this component.
/// </summary>
public int Id => Application.Instance.IdFactory.Get(this);
public int Id => IdFactory.Get(this);

public IList<MenuBarItem> Items { get; } = new List<MenuBarItem>();

Expand Down
5 changes: 3 additions & 2 deletions ImGui.Forms/Controls/Menu/MenuBarItem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ImGui.Forms.Models.IO;
using ImGui.Forms.Factories;
using ImGui.Forms.Models.IO;

namespace ImGui.Forms.Controls.Menu
{
Expand All @@ -7,7 +8,7 @@ public abstract class MenuBarItem
/// <summary>
/// The Id for this component.
/// </summary>
protected int Id => Application.Instance.IdFactory.Get(this);
protected int Id => IdFactory.Get(this);

/// <summary>
/// The height of the menu bar item.
Expand Down
3 changes: 2 additions & 1 deletion ImGui.Forms/Controls/TabControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Numerics;
using System.Threading.Tasks;
using ImGui.Forms.Controls.Base;
using ImGui.Forms.Factories;
using ImGui.Forms.Models;
using ImGuiNET;
using Veldrid;
Expand Down Expand Up @@ -64,7 +65,7 @@ protected override async void UpdateInternal(Rectangle contentRect)
if (wasManuallyChanged && _selectedPageTemp == page) pageFlags |= ImGuiTabItemFlags.SetSelected;
if (!Enabled && _selectedPage == page) pageFlags |= ImGuiTabItemFlags.SetSelected;

ImGuiNET.ImGui.PushID(Application.Instance.IdFactory.Get(page));
ImGuiNET.ImGui.PushID(IdFactory.Get(page));

var stillOpen = true;
if (ImGuiNET.ImGui.BeginTabItem(page.Title, ref stillOpen, pageFlags))
Expand Down
3 changes: 2 additions & 1 deletion ImGui.Forms/Controls/Tree/TreeView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Numerics;
using ImGui.Forms.Controls.Base;
using ImGui.Forms.Controls.Menu;
using ImGui.Forms.Factories;
using ImGui.Forms.Models.IO;
using ImGuiNET;
using Veldrid;
Expand Down Expand Up @@ -111,7 +112,7 @@ private bool UpdateNodes(IList<TreeNode<TNodeData>> nodes, ref bool nodeHovered)
if (SelectedNode == node) flags |= ImGuiTreeNodeFlags.Selected;

// Add node
int nodeId = Application.Instance.IdFactory.Get(node);
int nodeId = IdFactory.Get(node);

ImGuiNET.ImGui.PushID(nodeId);
ImGuiNET.ImGui.SetNextItemOpen(node.IsExpanded);
Expand Down
45 changes: 19 additions & 26 deletions ImGui.Forms/Factories/IdFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,29 @@

namespace ImGui.Forms.Factories
{
public class IdFactory
public static class IdFactory
{
private readonly Random _random;
private readonly HashSet<int> _ids;
private readonly IDictionary<object, (int, bool)> _idDictionary;
private static readonly Random _random = new();
private static readonly HashSet<int> _ids = new();
private static readonly Dictionary<object, (int, bool)> _idDictionary = new();

internal IdFactory()
public static int Get(object item)
{
_random = new Random();
_ids = new HashSet<int>();
_idDictionary = new Dictionary<object, (int, bool)>();
if (_idDictionary.ContainsKey(item))
{
_idDictionary[item] = (_idDictionary[item].Item1, true);
return _idDictionary[item].Item1;
}

int id = Create();

_ids.Add(id);
_idDictionary[item] = (id, true);

return id;
}

internal void FreeIds()
internal static void FreeIds()
{
// Remove unused Id's
var objToDelete = new List<object>();
Expand All @@ -37,23 +46,7 @@ internal void FreeIds()
_idDictionary.Remove(obj);
}

public int Get(object item)
{
if (_idDictionary.ContainsKey(item))
{
_idDictionary[item] = (_idDictionary[item].Item1, true);
return _idDictionary[item].Item1;
}

var id = Create();

_ids.Add(id);
_idDictionary[item] = (id, true);

return id;
}

private int Create()
private static int Create()
{
var id = _random.Next(int.MaxValue);
while (_ids.Contains(id))
Expand Down
31 changes: 18 additions & 13 deletions ImGui.Forms/Form.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using Veldrid.Sdl2;
using static System.Net.Mime.MediaTypeNames;

namespace ImGui.Forms
{
Expand All @@ -35,9 +34,9 @@ public abstract class Form
public bool AllowDragDrop { get; set; }

/// <summary>
/// Sets the applications icon.
/// Gets and sets the applications icon.
/// </summary>
/// <remarks>The icons dimensions need to be a power of 2 (eg. 32, 64, 128, etc)</remarks>
/// <remarks>The icon dimensions need to be a power of 2 (eg. 32, 64, 128, etc)</remarks>
public Image<Rgba32> Icon
{
get => _icon;
Expand All @@ -48,11 +47,15 @@ protected set
}
}

public MainMenuBar MainMenuBar { get; protected set; }
public MainMenuBar MenuBar { get; protected set; }

public Component Content { get; protected set; }

public Vector2 Padding => Style.GetStyleVector2(ImGuiStyleVar.WindowPadding);
public Vector2 Padding
{
get => Style.GetStyleVector2(ImGuiStyleVar.WindowPadding);
set => Style.SetStyle(ImGuiStyleVar.WindowPadding, value);
}

public FontResource DefaultFont { get; set; }

Expand All @@ -67,15 +70,15 @@ protected set

#endregion

public void PushModal(Modal modal)
internal void PushModal(Modal modal)
{
if (_modals.Count > 0)
_modals.Last().ChildModal = modal;

_modals.Add(modal);
}

public void PopModal()
internal void PopModal()
{
if (_modals.Count <= 0)
return;
Expand All @@ -91,7 +94,7 @@ public bool HasOpenModals()
return _modals.Count > 0;
}

public void Update()
internal void Update()
{
// Set icon
if (_setIcon)
Expand Down Expand Up @@ -121,8 +124,8 @@ public void Update()
ImGuiNET.ImGui.PushFont(fontPtr.Value);

// Add menu bar
MainMenuBar?.Update();
var menuHeight = MainMenuBar?.Height ?? 0;
MenuBar?.Update();
var menuHeight = MenuBar?.Height ?? 0;

// Add form controls
ImGuiNET.ImGui.PushStyleVar(ImGuiStyleVar.WindowPadding, Padding);
Expand Down Expand Up @@ -162,17 +165,19 @@ internal bool HasBlockingModals()

internal void OnResized()
{
Resized?.Invoke(this, new EventArgs());
Resized?.Invoke(this, EventArgs.Empty);
}

internal void OnLoad()
{
Load?.Invoke(this, new EventArgs());
Load?.Invoke(this, EventArgs.Empty);
}

internal async Task OnClosing(ClosingEventArgs e)
{
if (Closing == null) return;
if (Closing == null)
return;

await Closing?.Invoke(this, e);
}

Expand Down
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.1.2</version>
<version>1.1.3</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 310ae02

Please sign in to comment.