Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion FGEGraphics/ClientSystem/ViewUI2D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
using FGEGraphics.GraphicsHelpers.FontSets;
using FGEGraphics.GraphicsHelpers.Shaders;
using FGEGraphics.UISystem;
using FreneticUtilities.FreneticExtensions;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL4;
Expand Down
24 changes: 24 additions & 0 deletions FGEGraphics/GraphicsHelpers/FontSets/FontSetEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using FGECore.UtilitySystems;
using FGEGraphics.GraphicsHelpers.Shaders;
using OpenTK.Mathematics;
using FGECore.CoreSystems;

namespace FGEGraphics.GraphicsHelpers.FontSets;

Expand Down Expand Up @@ -44,6 +45,9 @@ public class FontSetEngine(GLFontEngine fontEngine)
/// <summary>A list of all currently loaded font sets.</summary>
public Dictionary<(string, int), FontSet> Fonts = [];

/// <summary>A cache of font names and sizes to their closest font set.</summary>
public Dictionary<(string, int), FontSet> ApproximateFonts = [];

/// <summary>Helper function to get a language data.</summary>
public Func<string[], string> GetLanguageHelper;

Expand Down Expand Up @@ -123,4 +127,24 @@ public FontSet GetFont(string fontname, int fontsize)
Fonts.Add((toret.Name, fontsize), toret);
return toret;
}

/// <summary>Returns the closest <see cref="FontSet"/> from the given font name and size.</summary>
/// <param name="fontname">The name of the font.</param>
/// <param name="fontsize">The size of the font.</param>
public FontSet GetApproximateFont(string fontname, int fontsize)
{
if (ApproximateFonts.TryGetValue((fontname, fontsize), out FontSet found))
{
return found;
}
IEnumerable<KeyValuePair<(string, int), FontSet>> fontVariants = Fonts.Where(pair => pair.Value.Name == fontname);
if (!fontVariants.Any())
{
return null;
}
IEnumerable<KeyValuePair<(string, int), FontSet>> fittingFonts = fontVariants.Where(pair => pair.Key.Item2 <= fontsize);
((string, int) _, FontSet font) = fittingFonts.Any() ? fittingFonts.MinBy(pair => fontsize - pair.Key.Item2) : fontVariants.MinBy(pair => Math.Abs(fontsize - pair.Key.Item2));
ApproximateFonts[(fontname, fontsize)] = font;
return font;
}
}
2 changes: 1 addition & 1 deletion FGEGraphics/UISystem/UI3DSubEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class UI3DSubEngine : UIElement
/// <summary>Constructs a new 3D sub-engine.</summary>
/// <param name="layout">The layout of the element.</param>
/// <param name="alphaBack">Whether to have an alpha background.</param>
public UI3DSubEngine(UILayout layout, bool alphaBack) : base(UIStyling.Empty, layout)
public UI3DSubEngine(UILayout layout, bool alphaBack) : base(null, layout)
{
SubEngine = new GameEngine3D
{
Expand Down
22 changes: 11 additions & 11 deletions FGEGraphics/UISystem/UIBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,37 +40,37 @@ public class UIBox : UIElement
/// <param name="text">Text to display inside the box.</param>
public UIBox(UIStyling styling, UILayout layout, string text = null) : base(styling, layout)
{
AddChild(Label = new UILabel(text, styling.Bind(this), new UILayout().SetAnchor(UIAnchor.CENTER)) { IsEnabled = false });
AddChild(Label = new UILabel(text, styling?.Bind(this), new UILayout().SetAnchor(UIAnchor.CENTER)) { IsEnabled = false });
}

/// <inheritdoc/>
public override void Render(double delta, UIStyle style)
{
Vector3 rotation = new(-0.5f, -0.5f, Rotation);
bool any = style.DropShadowLength > 0 || style.BorderColor.A > 0 || style.BaseColor.A > 0;
bool any = style.ShadowSize > 0 || style.Stroke.A > 0 || style.Fill.A > 0;
if (any)
{
View.Engine.Textures.White.Bind();
if (style.DropShadowLength > 0)
if (style.ShadowSize > 0)
{
Renderer2D.SetColor(new Color4F(0, 0, 0, 0.5f));
View.Rendering.RenderRectangle(View.UIContext, X, Y, X + Width + style.DropShadowLength, Y + Height + style.DropShadowLength, rotation);
View.Rendering.RenderRectangle(View.UIContext, X, Y, X + Width + style.ShadowSize, Y + Height + style.ShadowSize, rotation);
}
if (style.BorderColor.A > 0 && style.BorderThickness > 0)
if (style.Stroke.A > 0 && style.StrokeWeight > 0)
{
Renderer2D.SetColor(style.BorderColor);
Renderer2D.SetColor(style.Stroke);
View.Rendering.RenderRectangle(View.UIContext, X, Y, X + Width, Y + Height, rotation);
}
if (style.BaseColor.A > 0)
if (style.Fill.A > 0)
{
Renderer2D.SetColor(style.BaseColor);
View.Rendering.RenderRectangle(View.UIContext, X + style.BorderThickness, Y + style.BorderThickness, X + Width - style.BorderThickness, Y + Height - style.BorderThickness, rotation);
Renderer2D.SetColor(style.Fill);
View.Rendering.RenderRectangle(View.UIContext, X + style.StrokeWeight, Y + style.StrokeWeight, X + Width - style.StrokeWeight, Y + Height - style.StrokeWeight, rotation);
}
Renderer2D.SetColor(Color4F.White);
}
if (style.BaseTexture is not null)
if (style.Texture is not null)
{
style.BaseTexture.Bind();
style.Texture.Bind();
float ymin = Flip ? Y + Height : Y;
float ymax = Flip ? Y : Y + Height;
View.Rendering.RenderRectangle(View.UIContext, X, ymin, X + Width, ymax, rotation);
Expand Down
6 changes: 3 additions & 3 deletions FGEGraphics/UISystem/UIDropdown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ public struct InternalData()
public UIDropdown(int boxPadding, int listSpacing, UIStyling buttonStyling, UIStyling boxStyling, UILayout layout, string text = null, UIElement layer = null) : base(buttonStyling, layout)
{
PlaceholderInfo = text ?? "null";
AddChild(Button = new UIBox(buttonStyling, layout.AtOrigin(), text) { OnClick = Open });
Box = new UIBox(boxStyling, layout.AtOrigin());
AddChild(Button = new UIBox(buttonStyling, layout.Container(), text) { OnClick = Open });
Box = new UIBox(boxStyling, layout.Container());
Box.AddChild(Entries = new UIListGroup(listSpacing, new UILayout().SetAnchor(UIAnchor.TOP_CENTER).SetPosition(0, boxPadding)));
Box.Layout.SetHeight(() => Entries.Layout.Height + boxPadding * 2);
Internal.Layer = layer ?? this;
Expand Down Expand Up @@ -133,7 +133,7 @@ public void DeselectChoice()
public void AddChoice(UIElement choice, Func<string> label)
{
// TODO: configurable appearance
UIStyle containerStyle = Entries.Items.Count % 2 == 0 ? UIStyle.Empty : new UIStyle { BaseColor = new(0, 0, 0, 0.25f) };
UIStyling containerStyle = Entries.Items.Count % 2 == 0 ? null : new UIStyling { Fill = new Color4F(0, 0, 0, 0.25f) };
UIBox container = new(containerStyle, new UILayout().SetSize(() => Box.Width, () => choice.Height));
choice.Layout.SetAnchor(UIAnchor.TOP_CENTER);
container.AddChild(choice);
Expand Down
29 changes: 16 additions & 13 deletions FGEGraphics/UISystem/UIElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public abstract class UIElement
/// <summary>The parent element, <c>null</c> if this element is the root or hasn't been added as a child.</summary>
public UIElement Parent;

/// <summary>Gets the UI view this element is attached to.</summary>
/// <summary>The UI view this element is attached to.</summary>
public ViewUI2D View;

/// <summary>Styling logic for this element.</summary>
Expand Down Expand Up @@ -99,7 +99,7 @@ public abstract class UIElement
public object Tag = null;

/// <summary>Whether this element should render itself. If <c>false</c>, <see cref="Render(double, UIStyle)"/> may be called manually.</summary>
public bool RenderSelf = true;
public UIRenderMode RenderMode = UIRenderMode.FULL;

/// <summary>The debug name of this element.</summary>
public virtual string Name { get; set; } = null;
Expand Down Expand Up @@ -173,9 +173,6 @@ public struct ElementInternalData()

/// <summary>The current style of this element.</summary>
public UIStyle Style = UIStyle.Empty;

/// <summary>Styles registered on this element.</summary>
public HashSet<UIStyle> Styles = [];
}

/// <summary>Data internal to a <see cref="UIElement"/> instance.</summary>
Expand Down Expand Up @@ -373,15 +370,15 @@ public void SetStyle(UIStyle style)
{
return;
}
ElementInternal.Styles.Add(style);
ElementInternal.Style = style;
StyleChanged(previousStyle, ElementInternal.Style = style);
OnStyleChange?.Invoke(previousStyle, style);
}

/// <summary>If a <see cref="Styling"/> is present, attempts to update the current style.</summary>
public void UpdateStyle()
{
SetStyle(Styling.Get(this));
SetStyle(Styling?.Get(this) ?? UIStyle.Empty);
}

/// <summary>
Expand Down Expand Up @@ -637,13 +634,19 @@ public virtual void Render(double delta, UIStyle style)
public virtual void RenderAll(double delta)
{
GraphicsUtil.CheckError("UIElement - PreRender");
Render(delta);
if (RenderMode == UIRenderMode.FULL)
{
Render(delta);
}
GraphicsUtil.CheckError("UIElement - PostRenderSelf", this);
foreach (UIElement child in ElementInternal.Children)
if (RenderMode != UIRenderMode.NONE)
{
if (child.IsValid && child.RenderSelf)
foreach (UIElement child in ElementInternal.Children)
{
child.RenderAll(delta);
if (child.IsValid)
{
child.RenderAll(delta);
}
}
}
GraphicsUtil.CheckError("UIElement - PostRenderAll", this);
Expand Down Expand Up @@ -791,11 +794,11 @@ public List<string> GetBaseDebugInfo(string baseColor, bool detailed)
{
info.Add(transforms);
info.Add($"^7Enabled: ^{(IsEnabled ? "2" : "1")}{IsEnabled}^&, ^7Hovered: ^{(IsHovered ? "2" : "1")}{IsHovered}^&, ^7Pressed: ^{(IsPressed ? "2" : "1")}{IsPressed}^&, ^7Selected: ^{(IsFocused ? "2" : "1")}{IsFocused}");
if (ElementInternal.Styles.Count > 0)
/*if (ElementInternal.Styles.Count > 0)
{
List<string> styleNames = [.. ElementInternal.Styles.Select(style => style.Name is not null ? $"^{(style == ElementInternal.Style ? "3" : "7")}{style.Name}" : "^1unnamed")];
info.Add($"^7Styles: {string.Join("^&, ", styleNames)}");
}
}*/
}
return info;
}
Expand Down
2 changes: 1 addition & 1 deletion FGEGraphics/UISystem/UIGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class UIGroup : UIElement

/// <summary>Constructs a new group.</summary>
/// <param name="layout">The layout of the element.</param>
public UIGroup(UILayout layout) : base(UIStyling.Empty, layout)
public UIGroup(UILayout layout) : base(null, layout)
{
IsEnabled = false;
ScaleSize = false;
Expand Down
2 changes: 1 addition & 1 deletion FGEGraphics/UISystem/UIImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace FGEGraphics.UISystem;
/// <remarks>Constructs an image.</remarks>
/// <param name="image">The image to display.</param>
/// <param name="layout">The layout of the element.</param>
public class UIImage(Texture image, UILayout layout) : UIElement(UIStyling.Empty, layout)
public class UIImage(Texture image, UILayout layout) : UIElement(null, layout)
{
/// <inheritdoc/>
public override string Name => $"Image \"{Image.Name}\"";
Expand Down
2 changes: 1 addition & 1 deletion FGEGraphics/UISystem/UIInputBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace FGEGraphics.UISystem;
/// <param name="fonts">The font to use.</param>
/// <param name="pos">The position of the element.</param>
// TODO: Remove
public class UIInputBox(string text, string info, FontSet fonts, UILayout pos) : UIElement(UIStyling.Empty, pos.Height <= 0 ? pos.SetHeight(fonts.Height) : pos)
public class UIInputBox(string text, string info, FontSet fonts, UILayout pos) : UIElement(null, pos.Height <= 0 ? pos.SetHeight(fonts.Height) : pos)
{
/// <summary>The current text in this input box.</summary>
public string Text = text;
Expand Down
Loading