Skip to content

Commit

Permalink
Upgrade to ImGui.NET 1.90.0.1;
Browse files Browse the repository at this point in the history
Removed pre-loaded fonts and FontResources.cs;
Added merging fonts, default font detection, and publically settable glyph ranges for each font;
  • Loading branch information
onepiecefreak3 committed Dec 6, 2023
1 parent 2fa31cb commit 0508d24
Show file tree
Hide file tree
Showing 15 changed files with 423 additions and 213 deletions.
2 changes: 1 addition & 1 deletion ImGui.Forms/Controls/ImageButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected override void UpdateInternal(Rectangle contentRect)

if ((IntPtr)Image != IntPtr.Zero)
{
if ((ImGuiNET.ImGui.ImageButton((IntPtr)Image, GetImageSize()) || IsKeyDown(KeyAction)) && Enabled)
if ((ImGuiNET.ImGui.ImageButton($"##{Id}", (IntPtr)Image, GetImageSize()) || IsKeyDown(KeyAction)) && Enabled)
OnClicked();
}
else
Expand Down
8 changes: 4 additions & 4 deletions ImGui.Forms/Controls/Layouts/TableLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ protected override void UpdateInternal(Rectangle contentRect)
if (contentRect.Height < totalHeight)
childFlags |= ImGuiWindowFlags.AlwaysVerticalScrollbar;

if (ImGuiNET.ImGui.BeginChild($"{Id}", contentRect.Size, false, childFlags))
if (ImGuiNET.ImGui.BeginChild($"{Id}", contentRect.Size, ImGuiChildFlags.None, childFlags))
{
var (x, y) = GetInitPoint(localWidths, localHeights, contentRect);
ImGuiNET.ImGui.SetCursorPosX(x);
ImGuiNET.ImGui.SetCursorPosY(y);

if (ImGuiNET.ImGui.BeginChild($"{Id}-in", new Vector2(totalWidth, totalHeight), false, ImGuiWindowFlags.NoScrollbar))
if (ImGuiNET.ImGui.BeginChild($"{Id}-in", new Vector2(totalWidth, totalHeight), ImGuiChildFlags.None, ImGuiWindowFlags.NoScrollbar))
{
(x, y) = (0, 0);
var origX = x;
Expand Down Expand Up @@ -155,13 +155,13 @@ protected override void UpdateInternal(Rectangle contentRect)
ImGuiNET.ImGui.SetCursorPosX(x);
ImGuiNET.ImGui.SetCursorPosY(y);

if (ImGuiNET.ImGui.BeginChild($"{Id}-{r}-{c}", new Vector2(cellWidth, cellHeight), false, ImGuiWindowFlags.NoScrollbar))
if (ImGuiNET.ImGui.BeginChild($"{Id}-{r}-{c}", new Vector2(cellWidth, cellHeight), ImGuiChildFlags.None, ImGuiWindowFlags.NoScrollbar))
{
// Draw cell content container
ImGuiNET.ImGui.SetCursorPosX(xAdjust);
ImGuiNET.ImGui.SetCursorPosY(yAdjust);

if (ImGuiNET.ImGui.BeginChild($"{Id}-{r}-{c}-content", new Vector2(cellInternalWidth, cellInternalHeight), false, ImGuiWindowFlags.NoScrollbar))
if (ImGuiNET.ImGui.BeginChild($"{Id}-{r}-{c}-content", new Vector2(cellInternalWidth, cellInternalHeight), ImGuiChildFlags.None, ImGuiWindowFlags.NoScrollbar))
{
// Draw component
cell.Content?.Update(new Rectangle((int)(contentRect.X + x + xAdjust - sx), (int)(contentRect.Y + y + yAdjust - sy), cellInternalWidth, cellInternalHeight));
Expand Down
5 changes: 3 additions & 2 deletions ImGui.Forms/Controls/Lists/BaseList.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.Layouts;
using ImGuiNET;
using Veldrid;
using Size = ImGui.Forms.Models.Size;

Expand Down Expand Up @@ -63,7 +64,7 @@ protected override void UpdateInternal(Rectangle contentRect)
var listDimension = localItems.Sum(i => GetDimension(i, contentRect)) + Math.Max(0, localItems.Length - 1) * ItemSpacing + (int)(GetPadding() * 2);
var scrollableDimension = GetScrollableDimension(contentRect);

if (ImGuiNET.ImGui.BeginChild($"{Id}", contentRect.Size, false))
if (ImGuiNET.ImGui.BeginChild($"{Id}", contentRect.Size, ImGuiChildFlags.None))
{
if (_scrollToLast)
{
Expand Down Expand Up @@ -136,7 +137,7 @@ private void SetScroll(float scroll)
{
scroll = Math.Max(0, scroll);

if(Alignment == Alignment.Vertical)
if (Alignment == Alignment.Vertical)
ImGuiNET.ImGui.SetScrollY(scroll);
else
ImGuiNET.ImGui.SetScrollX(scroll);
Expand Down
4 changes: 2 additions & 2 deletions ImGui.Forms/Controls/Lists/ImageList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ protected override void UpdateInternal(Rectangle contentRect)
{
ImageListItem selectedItem = null;

if (ImGuiNET.ImGui.BeginChild($"##{Id}_out", new Vector2(contentRect.Width, contentRect.Height), false))
if (ImGuiNET.ImGui.BeginChild($"##{Id}_out", new Vector2(contentRect.Width, contentRect.Height), ImGuiChildFlags.None))
{
var textHeight = FontResource.GetCurrentLineHeight(Font);

Expand All @@ -66,7 +66,7 @@ protected override void UpdateInternal(Rectangle contentRect)

var localItems = Items.ToArray();

if (ImGuiNET.ImGui.BeginChild($"##{Id}_in", new Vector2(contentRect.Width, Padding.Y * 2 + localItems.Length * (itemHeight + ItemPadding)), false, ImGuiWindowFlags.NoScrollbar))
if (ImGuiNET.ImGui.BeginChild($"##{Id}_in", new Vector2(contentRect.Width, Padding.Y * 2 + localItems.Length * (itemHeight + ItemPadding)), ImGuiChildFlags.None, ImGuiWindowFlags.NoScrollbar))
{
for (var i = 0; i < localItems.Length; i++)
{
Expand Down
24 changes: 23 additions & 1 deletion ImGui.Forms/Controls/MultiLineTextBox.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 ImGuiNET;
using Veldrid;

namespace ImGui.Forms.Controls
Expand All @@ -24,6 +25,11 @@ public string Text
}
}

/// <summary>
/// Marks the input as read-only.
/// </summary>
public bool IsReadOnly { get; set; }

/// <summary>
/// Get or set the max count of characters allowed in the text box.
/// </summary>
Expand All @@ -41,8 +47,24 @@ public string Text

protected override void UpdateInternal(Rectangle contentRect)
{
if (ImGuiNET.ImGui.InputTextMultiline($"##{Id}", ref _text, MaxCharacters, contentRect.Size))
var enabled = Enabled;
var isReadonly = IsReadOnly;

var flags = ImGuiInputTextFlags.None;
if (isReadonly || !enabled) flags |= ImGuiInputTextFlags.ReadOnly;

if (isReadonly || !enabled)
{
ImGuiNET.ImGui.PushStyleColor(ImGuiCol.FrameBg, 0xFF666666);
ImGuiNET.ImGui.PushStyleColor(ImGuiCol.FrameBgActive, 0xFF666666);
ImGuiNET.ImGui.PushStyleColor(ImGuiCol.FrameBgHovered, 0xFF666666);
}

if (ImGuiNET.ImGui.InputTextMultiline($"##{Id}", ref _text, MaxCharacters, contentRect.Size, flags))
OnTextChanged();

if (isReadonly || !enabled)
ImGuiNET.ImGui.PopStyleColor(3);
}

private void OnTextChanged()
Expand Down
2 changes: 1 addition & 1 deletion ImGui.Forms/Controls/Tree/TreeView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected override void UpdateInternal(Rectangle contentRect)
return;

var anyNodeHovered = false;
if (ImGuiNET.ImGui.BeginChild($"{Id}", new Vector2(contentRect.Width, contentRect.Height), false, ImGuiWindowFlags.HorizontalScrollbar))
if (ImGuiNET.ImGui.BeginChild($"{Id}", new Vector2(contentRect.Width, contentRect.Height), ImGuiChildFlags.None, ImGuiWindowFlags.HorizontalScrollbar))
{
UpdateNodes(Nodes, ref anyNodeHovered);
}
Expand Down
79 changes: 73 additions & 6 deletions ImGui.Forms/Factories/FontFactory.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using ImGui.Forms.Models;
using ImGui.Forms.Resources;
using ImGui.Forms.Support.Veldrid.ImGui;
using ImGuiNET;
Expand All @@ -26,17 +29,17 @@ internal FontFactory()

#region Registration

public void RegisterFromFile(string ttfPath, int size)
public void RegisterFromFile(string ttfPath, int size, FontGlyphRange glyphRanges = FontGlyphRange.All)
{
if (IsInitialized)
throw new InvalidOperationException("Can not register new fonts after application started.");

// If font not tracked, add it
if (!_discCache.ContainsKey((ttfPath, size)))
_discCache[(ttfPath, size)] = new FontResource(ttfPath, size);
_discCache[(ttfPath, size)] = new FontResource(ttfPath, size, glyphRanges);
}

public void RegisterFromResource(Assembly assembly, string resourceName, int size)
public void RegisterFromResource(Assembly assembly, string resourceName, int size, FontGlyphRange glyphRanges = FontGlyphRange.All)
{
if (IsInitialized)
throw new InvalidOperationException("Can not register new fonts after application started.");
Expand All @@ -62,7 +65,7 @@ public void RegisterFromResource(Assembly assembly, string resourceName, int siz
return;

// Otherwise add it to cache
_discCache[(fontPath, size)] = new FontResource(fontPath, size, true);
_discCache[(fontPath, size)] = new FontResource(fontPath, size, glyphRanges, true);
}

#endregion
Expand Down Expand Up @@ -93,18 +96,82 @@ public FontResource Get(Assembly assembly, string resourceName, int size)

#region Initialization

internal void Initialize(ImGuiIOPtr io, ImGuiRenderer controller)
internal unsafe void Initialize(ImGuiIOPtr io, ImGuiRenderer controller)
{
_io = io;
_controller = controller;

// Initialize default font
var defaultFont = InitializeDefaultFont();

// Initialize fonts
var config = ImGuiNative.ImFontConfig_ImFontConfig();
config->MergeMode = 1;

foreach (var discFont in _discCache)
discFont.Value.Initialize(_io.Fonts.AddFontFromFileTTF(discFont.Key.Item1, discFont.Key.Item2));
{
if (discFont.Value == defaultFont)
continue;

var ranges = GetGlyphRanges(discFont.Value.GlyphRanges);

var loadedFont = _io.Fonts.AddFontFromFileTTF(discFont.Key.Item1, discFont.Key.Item2, config, ranges.Data);
discFont.Value.Initialize(loadedFont);
}

_controller.RecreateFontDeviceTexture();
}

private unsafe FontResource InitializeDefaultFont()
{
_io.Fonts.Clear();

ImFontPtr defaultFontPtr;

var defaultFont = _discCache.Values.FirstOrDefault(f => f.GlyphRanges.HasFlag(FontGlyphRange.Default));
if (defaultFont == null)
{
defaultFontPtr = _io.Fonts.AddFontDefault().NativePtr;
}
else
{
defaultFontPtr = _io.Fonts.AddFontFromFileTTF(defaultFont.Path, defaultFont.Size, null, GetGlyphRanges(defaultFont.GlyphRanges).Data);
defaultFont.Initialize(defaultFontPtr);
}

var config = ImGuiNative.ImFontConfig_ImFontConfig();
config->DstFont = defaultFontPtr;

return defaultFont;
}

private unsafe ImVector GetGlyphRanges(FontGlyphRange rangeFlags)
{
var builder = new ImFontGlyphRangesBuilderPtr(ImGuiNative.ImFontGlyphRangesBuilder_ImFontGlyphRangesBuilder());
builder.Clear();

if (rangeFlags.HasFlag(FontGlyphRange.Default))
builder.AddRanges(_io.Fonts.GetGlyphRangesDefault());
if (rangeFlags.HasFlag(FontGlyphRange.Cyrillic))
builder.AddRanges(_io.Fonts.GetGlyphRangesCyrillic());
if (rangeFlags.HasFlag(FontGlyphRange.Chinese))
builder.AddRanges(_io.Fonts.GetGlyphRangesChineseFull());
if (rangeFlags.HasFlag(FontGlyphRange.Japanese))
builder.AddRanges(_io.Fonts.GetGlyphRangesJapanese());
if (rangeFlags.HasFlag(FontGlyphRange.Greek))
builder.AddRanges(_io.Fonts.GetGlyphRangesGreek());
if (rangeFlags.HasFlag(FontGlyphRange.Korean))
builder.AddRanges(_io.Fonts.GetGlyphRangesKorean());
if (rangeFlags.HasFlag(FontGlyphRange.Thai))
builder.AddRanges(_io.Fonts.GetGlyphRangesThai());
if (rangeFlags.HasFlag(FontGlyphRange.Vietnamese))
builder.AddRanges(_io.Fonts.GetGlyphRangesVietnamese());

builder.BuildRanges(out var ranges);

return ranges;
}

#endregion

public void Dispose()
Expand Down
10 changes: 1 addition & 9 deletions ImGui.Forms/ImGui.Forms.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,10 @@
</PropertyGroup>

<ItemGroup>
<None Remove="Resources\Fonts\arial.ttf" />
<None Remove="Resources\Fonts\roboto.ttf" />
<None Remove="Resources\Images\error.png" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Resources\Fonts\arial.ttf">
<LogicalName>arial.ttf</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="Resources\Fonts\roboto.ttf">
<LogicalName>roboto.ttf</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="Resources\Images\error.png">
<LogicalName>error.png</LogicalName>
</EmbeddedResource>
Expand Down Expand Up @@ -78,7 +70,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="ImGui.NET-newest" Version="1.82.0" />
<PackageReference Include="ImGui.NET" Version="1.90.0.1" />
<PackageReference Include="System.Drawing.Common" Version="5.0.3" />
<PackageReference Include="Veldrid.StartupUtilities" Version="4.9.0" />
</ItemGroup>
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,7 +2,7 @@
<package >
<metadata>
<id>Imgui.Forms</id>
<version>1.0.45</version>
<version>1.0.46</version>
<description>A WinForms-inspired object-oriented framework around Dear ImGui (https://github.com/ocornut/imgui)</description>

<authors>onepiecefreak</authors>
Expand All @@ -19,7 +19,7 @@

<dependencies>
<group targetFramework="netstandard2.1">
<dependency id="ImGui.NET-newest" version="1.82.0" />
<dependency id="ImGui.NET" version="1.90.0.1" />
<dependency id="System.Drawing.Common" version="5.0.3" />
<dependency id="Veldrid.StartupUtilities" version="4.9.0" />
</group>
Expand Down
19 changes: 19 additions & 0 deletions ImGui.Forms/Models/FontGlyphRange.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;

namespace ImGui.Forms.Models
{
[Flags]
public enum FontGlyphRange
{
Default = 1 << 0,
Cyrillic = 1 << 1,
Chinese = 1 << 2,
Japanese = 1 << 3,
Greek = 1 << 4,
Korean = 1 << 5,
Thai = 1 << 6,
Vietnamese = 1 << 7,

All = 255,
}
}
22 changes: 16 additions & 6 deletions ImGui.Forms/Resources/FontResource.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Numerics;
using ImGui.Forms.Models;
using ImGuiNET;

namespace ImGui.Forms.Resources
Expand All @@ -12,20 +13,29 @@ namespace ImGui.Forms.Resources
public class FontResource : IDisposable
{
private ImFontPtr _ptr;

private readonly string _path;

private readonly bool _temporary;

/// <summary>
/// Supported glyphs.
/// </summary>
public FontGlyphRange GlyphRanges { get; }

/// <summary>
/// The physical path to the font.
/// </summary>
public string Path { get; }
/// <summary>
/// The size of the font.
/// </summary>
public int Size { get; }

internal FontResource(string path, int size, bool temporary = false)
internal FontResource(string path, int size, FontGlyphRange glyphRanges, bool temporary = false)
{
_path = path;
Path = path;
Size = size;
_temporary = temporary;
GlyphRanges = glyphRanges;
}

internal void Initialize(ImFontPtr ptr)
Expand All @@ -42,8 +52,8 @@ protected virtual void Dispose(bool disposing)
if (!_temporary)
return;

if (File.Exists(_path))
File.Delete(_path);
if (File.Exists(Path))
File.Delete(Path);
}

/// <summary>
Expand Down
Loading

0 comments on commit 0508d24

Please sign in to comment.