Skip to content

Commit

Permalink
Refactor font registration and usage;
Browse files Browse the repository at this point in the history
Cleanup constructors and properties of components to make more sense;
Bump to minro 1.1.0;
  • Loading branch information
onepiecefreak3 committed Sep 29, 2024
1 parent 5f59108 commit 96273f8
Show file tree
Hide file tree
Showing 36 changed files with 541 additions and 322 deletions.
4 changes: 2 additions & 2 deletions ImGui.Forms/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ public class Application

public static Application Instance { get; private set; }

public static FontFactory FontFactory { get; } = new();

#endregion

#region Properties
Expand Down Expand Up @@ -191,6 +189,8 @@ private bool UpdateFrame(CommandList cl)
ImageFactory.FreeTextures();
IdFactory.FreeIds();

//FontFactory.InitializeFonts(ImGuiNET.ImGui.GetIO(), _executionContext.Renderer);

// Snapshot current machine state
var snapshot = _executionContext.Window.PumpEvents();

Expand Down
12 changes: 10 additions & 2 deletions ImGui.Forms/Controls/ArrowButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using ImGui.Forms.Controls.Base;
using ImGui.Forms.Models;
using ImGui.Forms.Models.IO;
using ImGui.Forms.Resources;
using ImGuiNET;
using Veldrid;

Expand All @@ -13,16 +12,25 @@ public class ArrowButton : Component
private const int ButtonSizeX_ = 11;
private const int ButtonSizeY_ = 13;

#region Properties

public KeyCommand KeyAction { get; set; }

public ImGuiDir Direction { get; set; } = ImGuiDir.None;
public ImGuiDir Direction { get; set; }

#endregion

#region Events

public event EventHandler Clicked;

#endregion

public ArrowButton(ImGuiDir direction = ImGuiDir.None)
{
Direction = direction;
}

public override Size GetSize()
{
var padding = ImGuiNET.ImGui.GetStyle().FramePadding;
Expand Down
15 changes: 11 additions & 4 deletions ImGui.Forms/Controls/Button.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,16 @@ public class Button : Component

#endregion

public Button(LocalizedString text = default)
{
Text = text;
}

public override Size GetSize()
{
ApplyStyles(Enabled, Font);

var textSize = FontResource.MeasureText(EscapeText());
var textSize = TextMeasurer.MeasureText(EscapeText());
SizeValue width = Width.IsContentAligned ? (int)Math.Ceiling(textSize.X) + (int)Padding.X * 2 : Width;
var height = (int)Math.Ceiling(textSize.Y) + (int)Padding.Y * 2;

Expand Down Expand Up @@ -75,8 +80,9 @@ private void ApplyStyles(bool enabled, FontResource font)
ImGuiNET.ImGui.PushStyleColor(ImGuiCol.ButtonActive, ImGuiNET.ImGui.GetColorU32(ImGuiCol.TextDisabled));
}

if (font != null)
ImGuiNET.ImGui.PushFont((ImFontPtr)Font);
ImFontPtr? fontPtr = font?.GetPointer();
if (fontPtr != null)
ImGuiNET.ImGui.PushFont(fontPtr.Value);

ImGuiNET.ImGui.PushStyleVar(ImGuiStyleVar.FramePadding, Padding);
}
Expand All @@ -85,7 +91,8 @@ private void RemoveStyles(bool enabled, FontResource font)
{
ImGuiNET.ImGui.PopStyleVar();

if (font != null)
ImFontPtr? fontPtr = font?.GetPointer();
if (fontPtr != null)
ImGuiNET.ImGui.PopFont();

if (!enabled)
Expand Down
11 changes: 10 additions & 1 deletion ImGui.Forms/Controls/CheckBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class CheckBox : Component
{
private bool _checked;

#region Properties

public LocalizedString Text { get; set; }

public LocalizedString Tooltip { get; set; }
Expand All @@ -26,15 +28,22 @@ public bool Checked
}
}

#endregion

#region Events

public event EventHandler CheckChanged;

#endregion

public CheckBox(LocalizedString text = default)
{
Text = text;
}

public override Size GetSize()
{
var size = FontResource.MeasureText(Text);
var size = TextMeasurer.MeasureText(Text);
return new Size((int)(Math.Ceiling(size.X) + 21 + ImGuiNET.ImGui.GetStyle().ItemInnerSpacing.X), (int)Math.Max(Math.Ceiling(size.Y), 21));
}

Expand Down
8 changes: 6 additions & 2 deletions ImGui.Forms/Controls/ComboBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class ComboBox<TItem> : Component
{
private string _input = string.Empty;

#region Properties

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

public ComboBoxItem<TItem> SelectedItem { get; set; }
Expand All @@ -33,6 +35,8 @@ public class ComboBox<TItem> : Component
/// </summary>
public uint MaxShowItems { get; set; } = 10;

#endregion

#region Events

public event EventHandler SelectedItemChanged;
Expand All @@ -41,11 +45,11 @@ public class ComboBox<TItem> : Component

public override Size GetSize()
{
var maxWidth = Items.Select(x => FontResource.GetCurrentLineWidth(x.Name)).DefaultIfEmpty(0).Max() + (int)ImGuiNET.ImGui.GetStyle().ItemInnerSpacing.X * 2;
var maxWidth = Items.Select(x => TextMeasurer.GetCurrentLineWidth(x.Name)).DefaultIfEmpty(0).Max() + (int)ImGuiNET.ImGui.GetStyle().ItemInnerSpacing.X * 2;
var arrowWidth = 20;

SizeValue width = Width.IsContentAligned ? maxWidth + arrowWidth : Width;
var height = FontResource.GetCurrentLineHeight() + (int)ImGuiNET.ImGui.GetStyle().ItemInnerSpacing.Y * 2;
var height = TextMeasurer.GetCurrentLineHeight() + (int)ImGuiNET.ImGui.GetStyle().ItemInnerSpacing.Y * 2;

return new Size(width, height);
}
Expand Down
8 changes: 6 additions & 2 deletions ImGui.Forms/Controls/DropDownBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ namespace ImGui.Forms.Controls
{
public class DropDownBox<TItem> : Component
{
#region Properties

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

public ComboBoxItem<TItem> SelectedItem { get; set; }

public SizeValue Width { get; set; } = SizeValue.Content;

#endregion

#region Events

public event EventHandler SelectedItemChanged;
Expand All @@ -25,11 +29,11 @@ public class DropDownBox<TItem> : Component

public override Size GetSize()
{
var maxWidth = Items.Select(x => FontResource.GetCurrentLineWidth(x.Name)).DefaultIfEmpty(0).Max() + (int)ImGuiNET.ImGui.GetStyle().ItemInnerSpacing.X * 2;
var maxWidth = Items.Select(x => TextMeasurer.GetCurrentLineWidth(x.Name)).DefaultIfEmpty(0).Max() + (int)ImGuiNET.ImGui.GetStyle().ItemInnerSpacing.X * 2;
var arrowWidth = 20;

SizeValue width = Width.IsContentAligned ? maxWidth + arrowWidth : Width;
var height = FontResource.GetCurrentLineHeight() + (int)ImGuiNET.ImGui.GetStyle().ItemInnerSpacing.Y * 2;
var height = TextMeasurer.GetCurrentLineHeight() + (int)ImGuiNET.ImGui.GetStyle().ItemInnerSpacing.Y * 2;

return new Size(width, height);
}
Expand Down
12 changes: 11 additions & 1 deletion ImGui.Forms/Controls/Expander.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace ImGui.Forms.Controls
{
public class Expander : Component
{
#region Properties

public LocalizedString Caption { get; set; }

public Component Content { get; set; }
Expand All @@ -18,12 +20,20 @@ public class Expander : Component

public bool Expanded { get; set; }

#endregion

#region Events

public event EventHandler ExpandedChanged;

#endregion

public Expander(Component content, LocalizedString caption = default)
{
Content = content;
Caption = caption;
}

public override Size GetSize()
{
return new Size(1f, (int)(GetHeaderHeight() + (Expanded ? ImGuiNET.ImGui.GetStyle().ItemSpacing.X + ContentHeight : 0)));
Expand Down Expand Up @@ -73,7 +83,7 @@ private int GetContentPosY()

private int GetHeaderHeight()
{
var size = FontResource.MeasureText(Caption);
var size = TextMeasurer.MeasureText(Caption);
var framePadding = ImGuiNET.ImGui.GetStyle().FramePadding;
return (int)Math.Ceiling(size.Y + framePadding.Y * 2);
}
Expand Down
10 changes: 10 additions & 0 deletions ImGui.Forms/Controls/ImageButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class ImageButton : Component
{
private ThemedImageResource _baseImg;

#region Properties

public LocalizedString? Tooltip { get; set; }

public KeyCommand KeyAction { get; set; }
Expand All @@ -27,16 +29,24 @@ public ThemedImageResource Image
_baseImg = value;
}
}

public Vector2 ImageSize { get; set; } = Vector2.Zero;

public Vector2 Padding { get; set; } = new(2, 2);

#endregion

#region Events

public event EventHandler Clicked;

#endregion

public ImageButton(ThemedImageResource image = default)
{
Image = image;
}

public override Size GetSize()
{
var size = GetImageSize();
Expand Down
22 changes: 16 additions & 6 deletions ImGui.Forms/Controls/Label.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Drawing;
using System.Numerics;
using ImGui.Forms.Controls.Base;
using ImGui.Forms.Extensions;
using ImGui.Forms.Localization;
using ImGui.Forms.Models;
using ImGui.Forms.Resources;
Expand All @@ -14,6 +13,8 @@ namespace ImGui.Forms.Controls
{
public class Label : Component
{
#region Properties

public LocalizedString Text { get; set; }

public FontResource Font { get; set; }
Expand All @@ -24,6 +25,13 @@ public class Label : Component

public SizeValue Width { get; set; } = SizeValue.Content;

#endregion

public Label(LocalizedString text = default)
{
Text = text;
}

public override Size GetSize()
{
ApplyStyles();
Expand All @@ -34,7 +42,7 @@ public override Size GetSize()
var textSize = Vector2.Zero;
foreach (var line in lines)
{
var lineSize = FontResource.MeasureText(line, true);
var lineSize = TextMeasurer.MeasureText(line, true);
textSize = new Vector2(Math.Max(textSize.X, lineSize.X), textSize.Y + lineSize.Y);
}
SizeValue width = Width.IsContentAligned ? (int)Math.Ceiling(textSize.X) : Width;
Expand All @@ -56,7 +64,7 @@ protected override void UpdateInternal(Rectangle contentRect)
{
ImGuiNET.ImGui.GetWindowDrawList().AddText(pos, ImGuiNET.ImGui.GetColorU32(ImGuiCol.Text), line);

var lineSize = FontResource.MeasureText(line, true);
var lineSize = TextMeasurer.MeasureText(line, true);
pos += new Vector2(0, lineSize.Y + LineDistance);
}

Expand All @@ -68,13 +76,15 @@ protected override void ApplyStyles()
if (!TextColor.IsEmpty)
ImGuiNET.ImGui.PushStyleColor(ImGuiCol.Text, TextColor.ToUInt32());

if (Font != null)
ImGuiNET.ImGui.PushFont((ImFontPtr)Font);
ImFontPtr? fontPtr = Font?.GetPointer();
if (fontPtr != null)
ImGuiNET.ImGui.PushFont(fontPtr.Value);
}

protected override void RemoveStyles()
{
if (Font != null)
ImFontPtr? fontPtr = Font?.GetPointer();
if (fontPtr != null)
ImGuiNET.ImGui.PopFont();

if (!TextColor.IsEmpty)
Expand Down
5 changes: 5 additions & 0 deletions ImGui.Forms/Controls/Layouts/StackLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class StackLayout : Component
private readonly TableLayout _tableLayout;
private Size _size = Size.Parent;

#region Properties

public IList<StackItem> Items { get; }

public Alignment Alignment { get; set; } = Alignment.Vertical;
Expand Down Expand Up @@ -54,6 +56,8 @@ public Size Size
}
}

#endregion

public StackLayout()
{
var itemList = new ObservableList<StackItem>();
Expand All @@ -63,6 +67,7 @@ public StackLayout()
itemList.ItemInserted += (s, e) => InsertItem(e.Item, e.Index);

Items = itemList;

_tableLayout = new TableLayout();
}

Expand Down
3 changes: 1 addition & 2 deletions ImGui.Forms/Controls/Layouts/Stacktem.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using ImGui.Forms.Controls.Base;
using ImGui.Forms.Controls.Base;

namespace ImGui.Forms.Controls.Layouts
{
Expand Down
4 changes: 4 additions & 0 deletions ImGui.Forms/Controls/Layouts/ZLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ namespace ImGui.Forms.Controls.Layouts
{
public class ZLayout : Component
{
#region Properties

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

public Vector2 ItemSpacing { get; set; }

public Size Size { get; set; } = Size.Parent;

#endregion

public override Size GetSize()
{
return Size;
Expand Down
Loading

0 comments on commit 96273f8

Please sign in to comment.