Better Label

This commit is contained in:
JacobTech 2024-04-10 11:19:05 -04:00
parent 771f360a66
commit 46c7244dd1
10 changed files with 203 additions and 392 deletions

View File

@ -3,7 +3,5 @@ namespace GraphicsManager.Enums;
public enum TextLocation
{
TopLeft,
PxLeft,
TrueCenterLeft,
PostiveTureCenterLeft
LineCenter
}

View File

@ -10,7 +10,7 @@
<IncludeSymbols>False</IncludeSymbols>
<RepositoryUrl>https://git.jacobtech.com/JacobTech.com/GraphicsManager</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<Version>1.0.9-alpha07</Version>
<Version>1.0.9-alpha29</Version>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
@ -33,7 +33,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="OpenTK" Version="4.7.1" />
<PackageReference Include="OpenTK" Version="4.8.2" />
<PackageReference Include="SixLabors.ImageSharp" Version="2.1.7" />
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta15" />
<PackageReference Include="SpaceWizards.SharpFont" Version="1.0.1" />

View File

@ -34,5 +34,6 @@ public interface IRenderObject
public event Func<IRenderObject, Task>? Clicked;
public event Func<IRenderObject, Task>? MouseLeave;
public event Func<IRenderObject, Task>? MouseEnter;
public event Func<IRenderObject, Task>? SizeChanged;
public event Func<IRenderObject, Task>? WindowLoaded;
}

View File

@ -172,6 +172,37 @@ public class Texture
};
}
internal static Character GetChar(FontInteraction l, char charter, IGLFWGraphicsContext con)
{
try
{
if (!Label._characters[con].ContainsKey(l)) Label._characters[con].Add(l, new Dictionary<uint, Character>());
if (Label._characters[con][l].ContainsKey(charter)) return Label._characters[con][l][(ushort)charter];
for (int i = 0; i < l.CurrentFonts.Length; i++)
{
try
{
l.CurrentFonts[i].Face.SetPixelSizes(0, l.PixelHeight);
l.CurrentFonts[i].Face.SelectCharmap(Encoding.Unicode);
}
catch
{
continue;
}
ushort temp = (charter);
if (l.CurrentFonts[i].Face.GetCharIndex(temp) == 0) continue;
return GetChar(l.CurrentFonts[i].Face, charter);
}
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
return GetChar(l.CurrentFonts[0].Face, (char)1);
}
internal static Character GetChar(FontInteraction l, char charter)
{
try

View File

@ -158,7 +158,7 @@ public class FlowLayout : ParentBase, IFlow
if (index < Controls.Length)
{
arg.Location = Controls[index].Location;
Controls[index].Location = new(arg.Location.X, arg.Location.Y + arg.Size.Y, arg.Location.Z);
Controls[index].Location = new(arg.Location.X, Controls[index].Location.Y + arg.Size.Y, arg.Location.Z);
for (int i = index + 1; i < Controls.Length; i++)
{
Controls[i].Location = new(0, Controls[i - 1].Location.Y + Controls[i - 1].Size.Y, arg.Location.Z);

View File

@ -32,6 +32,67 @@ public class Label : ILabel
public Vector2 LocationAsFloat { get { return laf; } }
public Vector2 SizeAsFloat { get { return saf; } }
private Vector2i? msize = null;
public Vector2i? MaxSize
{
get => msize;
set
{
msize = value;
text_Calculated = string.Empty;
float max_x = 0, lines = 1, char_x = 0F;
for (int i = 0; i < Text.Length; i++)
{
char c;
if (PasswordChar is null)
c = Text[i];
else
c = PasswordChar.Value;
bool n = (c == '\n');
if (n)
{
lines++;
text_Calculated += c;
char_x = 0f;
}
else
{
Character ch;
if (Window is not null) ch = Texture.GetChar(Font, c, Window.Context);
else ch = Texture.GetChar(Font, c);
if (i > 0 && text[i-1] == ' ')
{
int addc = 0;
float word_char_x = char_x;
while (MaxSize is not null)
{
if (addc + i == Text.Length) break;
if (text[addc + i] == ' ') break;
Character ch2;
if (Window is not null) ch2 = Texture.GetChar(Font, text[addc + i], Window.Context);
else ch2 = Texture.GetChar(Font, c);
word_char_x += (ch2.Advance >> 6) * Scale;
if (word_char_x > MaxSize.Value.X)
{
char_x = 0f;
lines++;
text_Calculated += '\n';
break;
}
addc++;
}
}
text_Calculated += c;
float w = ch.Size.X * Scale;
float xrel = char_x + ch.Bearing.X * Scale;
char_x += (ch.Advance >> 6) * Scale;
if ((xrel + w) >= max_x) max_x = (xrel + w);
}
}
Size = new((int)max_x, (int)(lines * LineHeight) + (int)(lines * Font.ExtraLinePixels));
}
}
private char? pc;
public char? PasswordChar
{
@ -56,6 +117,7 @@ public class Label : ILabel
public static readonly Dictionary<IGLFWGraphicsContext, Dictionary<FontInteraction, Dictionary<uint, Character>>> _characters = new();
private string text = string.Empty;
private string text_Calculated = string.Empty;
public int VAO { get; private set; }
public void Focus()
@ -69,36 +131,30 @@ public class Label : ILabel
public int VBO { get; private set; }
public Vector2 DIR { get; set; } = new Vector2(1f, 0f);
public int TrueHeight;
public int PostiveTrueHeight;
public Vector2i GetSizeOfChar(int Index)
{
float addy = Font.PixelHeight * Scale, addx = 0F, char_x = 0F;
for (int i = 0; i < Index + 1; i++)
float mx = 0, my = 0;
char character;
if (PasswordChar is null)
character = Text[Index];
else
character = PasswordChar.Value;
if (character == '\n')
{
char character;
if (PasswordChar is null)
character = Text[i];
else
character = PasswordChar.Value;
if (character == '\n')
{
char_x = 0f;
addy += Font.PixelHeight * Scale;
addy += Font.ExtraLinePixels;
continue;
}
Character cha = Texture.GetChar(Font, character);
float w = cha.Size.X * Scale;
float xrel = char_x + cha.Bearing.X * Scale;
char_x += (cha.Advance >> 6) * Scale;
if ((xrel + w) >= addx) addx = (xrel + w);
mx = 0f;
my += Font.PixelHeight * Scale;
}
return new((int)addx, (int)addy);
else
{
Character cha;
if (Window is not null) cha = Texture.GetChar(Font, character, Window.Context);
else cha = Texture.GetChar(Font, character);
mx = cha.Size.X * Scale;
my = cha.Size.Y * Scale;
}
return new((int)mx, (int)my);
}
public string Text
@ -108,40 +164,61 @@ public class Label : ILabel
{
if (value is null) value = string.Empty;
text = value;
int line = 0;
double nl = 0;
double addy = 0f, addy2 =0f, addx = 0F, char_x = 0F;
for (int i = 0; i < value.Length; i++)
text_Calculated = string.Empty;
float max_x = 0, lines = 1, char_x = 0F;
for (int i = 0; i < Text.Length; i++)
{
char character;
char c;
if (PasswordChar is null)
character = value[i];
c = Text[i];
else
character = PasswordChar.Value;
c = PasswordChar.Value;
bool n = (c == '\n');
if (character == '\n')
if (n)
{
lines++;
char_x = 0f;
nl = addy;
line++;
continue;
text_Calculated += c;
}
Character cha = Texture.GetChar(Font, character);
double w = cha.Size.X * Scale;
double xrel = char_x + cha.Bearing.X * Scale;
double yrel = ((cha.Size.Y - cha.Bearing.Y) * Scale) + (Font.PixelHeight * Scale);
yrel += nl;
char_x += (cha.Advance >> 6) * Scale;
if ((xrel + w) >= addx) addx = (xrel + w);
if (yrel > addy) addy = yrel;
if (line == 0)
else
{
if (addy2 < cha.Bearing.Y) addy2 = cha.Bearing.Y;
Character ch;
if (Window is not null) ch = Texture.GetChar(Font, c, Window.Context);
else ch = Texture.GetChar(Font, c);
if (MaxSize is not null && i > 0 && text[i-1] == ' ')
{
int addc = 0;
float word_char_x = char_x;
while (true)
{
if (addc + i == Text.Length) break;
if (text[addc + i] == ' ') break;
Character ch2;
if (Window is not null) ch2 = Texture.GetChar(Font, text[addc + i], Window.Context);
else ch2 = Texture.GetChar(Font, c);
word_char_x += (ch2.Advance >> 6) * Scale;
if (word_char_x > MaxSize.Value.X)
{
char_x = 0f;
lines++;
text_Calculated += '\n';
break;
}
addc++;
}
}
text_Calculated += c;
float w = ch.Size.X * Scale;
float xrel = char_x + ch.Bearing.X * Scale;
char_x += (ch.Advance >> 6) * Scale;
if ((xrel + w) >= max_x) max_x = (xrel + w);
}
}
Size = new((int)addx, (int)addy + (int)(line * Font.ExtraLinePixels));
PostiveTrueHeight = (int)addy2;
TrueHeight = (int)(addy - (Font.PixelHeight - addy2));
Size = new((int)max_x, (int)(lines * LineHeight) + (int)(lines * Font.ExtraLinePixels));
if (Loaded)
{
@ -169,6 +246,13 @@ public class Label : ILabel
}
}
}
public uint LineHeight
{
get
{
return (uint)((Font.PixelHeight * (Font.CurrentFonts[0].Face.Height / (float)Font.CurrentFonts[0].Face.UnitsPerEM)) * Scale);
}
}
public Shader Shader { get; set; } = null!;
public FontInteraction Font { get; }
public float Scale { get; set; } = 1.0f;
@ -204,38 +288,33 @@ public class Label : ILabel
public Vector2i GetCharLocation(int index)
{
int line = 0;
double nl = 0;
double addy = 0f, addy2 =0f, addx = 0F, char_x = 0F;
float max_x = 0, lines = 0, char_x = 0F;
for (int i = 0; i < index; i++)
{
char character;
char c;
if (PasswordChar is null)
character = Text[i];
c = text_Calculated[i];
else
character = PasswordChar.Value;
c = PasswordChar.Value;
bool n = (c == '\n');
if (character == '\n')
if (n)
{
lines++;
char_x = 0f;
nl = addy;
line++;
continue;
}
Character cha = Texture.GetChar(Font, character);
double w = cha.Size.X * Scale;
double xrel = char_x + cha.Bearing.X * Scale;
double yrel = ((cha.Size.Y - cha.Bearing.Y) * Scale) + (Font.PixelHeight * Scale);
yrel += nl;
char_x += (cha.Advance >> 6) * Scale;
if ((xrel + w) >= addx) addx = (xrel + w);
if (yrel > addy) addy = yrel;
if (line == 0)
else
{
if (addy2 < cha.Bearing.Y) addy2 = cha.Bearing.Y;
Character ch;
if (Window is not null) ch = Texture.GetChar(Font, c, Window.Context);
else ch = Texture.GetChar(Font, c);
float w = ch.Size.X * Scale;
float xrel = char_x + ch.Bearing.X * Scale;
char_x += (ch.Advance >> 6) * Scale;
max_x = (xrel + w);
}
}
return new((int)addx, (int)(line * Font.ExtraLinePixels) + (int)(Font.PixelHeight * line));
return new((int)max_x, (int)(lines * LineHeight) + (int)(lines * Font.ExtraLinePixels));
}
public void Clean()
@ -260,6 +339,8 @@ public class Label : ILabel
Visible = false;
}
public event Func<IRenderObject, Task>? SizeChanged;
public void Draw(int x, int y, int ww, int hh)
{
if (Visible && Loaded && this.Font is not null)
@ -296,23 +377,22 @@ public class Label : ILabel
}));
float hhh = 0f;
for (int i = 0; i < Text.Length; i++)
for (int i = 0; i < text_Calculated.Length; i++)
{
char c;
if (PasswordChar is null)
c = Text[i];
c = text_Calculated[i];
else
c = PasswordChar.Value;
bool n = (c == '\n');
if (!_characters[Window!.Context][Font].ContainsKey(c) && !n)
{
Texture f = Texture.TextureForChar(Window!.Context, Font, c, Shader);
_ = Texture.TextureForChar(Window!.Context, Font, c, Shader);
}
int maxx = 0;
if (n)
{
hhh += Font.PixelHeight * Scale;
hhh += LineHeight;
hhh += Font.ExtraLinePixels;
char_x = 0f;
}
@ -320,6 +400,7 @@ public class Label : ILabel
{
if (!_characters[Window!.Context][Font].ContainsKey(c)) continue;
Character ch = _characters[Window!.Context][Font][c];
float w = ch.Size.X * Scale;
float h = ch.Size.Y * Scale;
float xrel = char_x + ch.Bearing.X * Scale;

View File

@ -220,6 +220,8 @@ public class RainbowLabel : ILabel
}
public Vector2i ScissorLocation { get; private set; }
public event Func<IRenderObject, Task>? SizeChanged;
public void Draw(int x, int y, int ww, int hh)
{
if (Visible && Loaded && this.Font is not null)

View File

@ -293,6 +293,8 @@ public class Rectangle : ITextureObject
}
}
public event Func<IRenderObject, Task>? SizeChanged;
public ushort[] Indexs { get; set; } = new ushort[6] { 0, 1, 3, 1, 2, 3 };
public BufferUsageHint Hint { get; set; } = BufferUsageHint.StaticDraw;
public event Func<IRenderObject, Task>? Clicked;
@ -658,6 +660,7 @@ public class Rectangle : ITextureObject
}
Points = temp;
if (SizeChanged is not null) SizeChanged.Invoke(this);
}
}

View File

@ -1,307 +0,0 @@
using GraphicsManager.Enums;
using GraphicsManager.Interfaces;
using GraphicsManager.Objects.Core;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Mathematics;
using OpenTK.Windowing.Common;
using OpenTK.Windowing.Common.Input;
using OpenTK.Windowing.GraphicsLibraryFramework;
namespace GraphicsManager.Objects;
public class TexturedTextBox : Rectangle
{
private Label _label;
private Label _watermark;
public TextLocation TextLocation { get; set; } = TextLocation.TopLeft;
public TexturedTextBox(Texture texture, FontFamily LabelFam, FontFamily WaterFam)
:base(texture)
{
base.HoverMouse = MouseCursor.IBeam;
_label = new Label(LabelFam)
{
HoverMouse = MouseCursor.IBeam,
IgnoreHover = true
};
TextureDisplay = TextureDisplay.HorizontalCenter;
_watermark = new(WaterFam)
{
Color = new(128, 128, 128, 255),
HoverMouse = MouseCursor.IBeam,
IgnoreHover = true
};
}
public override MouseCursor HoverMouse
{
get => base.HoverMouse;
set
{
_watermark.HoverMouse = value;
_label.HoverMouse = value;
base.HoverMouse = value;
}
}
public int Border { get; set; } = 2;
public TexturedTextBox(Texture texture, FontInteraction LabelFam, FontInteraction WaterFam)
:base(texture)
{
base.HoverMouse = MouseCursor.IBeam;
_label = new Label(LabelFam)
{
HoverMouse = MouseCursor.IBeam,
IgnoreHover = true
};
TextureDisplay = TextureDisplay.HorizontalCenter;
_watermark = new(WaterFam)
{
Color = new(128, 128, 128, 255),
HoverMouse = MouseCursor.IBeam,
IgnoreHover = true
};
}
public FontInteraction Font { get => _label.Font; }
public string Text
{
get => _label.Text;
set
{
int old = _label.TrueHeight;
_label.Text = value;
if (!string.IsNullOrEmpty(value))
{
bool f = false;
if (!_label.Visible)
{
f = true;
_label.Visible = true;
_label.Location = TextLocation switch
{
TextLocation.TrueCenterLeft => new(Location.X + Border + 5, Location.Y + ((Size.Y - _label.TrueHeight) / 2) - (_label.Size.Y - _label.TrueHeight), Location.Z),
TextLocation.PostiveTureCenterLeft => new(Location.X + Border + 5, Location.Y + ((Size.Y - _label.PostiveTrueHeight) / 2) - _label.Size.Y + _label.TrueHeight, Location.Z),
TextLocation.PxLeft => new(Location.X + Border + 5, Location.Y + ((Size.Y - _label.Size.Y) / 2), Location.Z),
_ => new(Location.X + Border + 5, Location.Y + Border + 5, Location.Z)
};
_watermark.Location = _label.Location;
}
if (_watermark.Visible) _watermark.Visible = false;
if (!f && TextLocation == TextLocation.TrueCenterLeft && old != _label.TrueHeight)
{
_label.Location = new(Location.X + Border + 5, Location.Y + ((Size.Y - _label.TrueHeight) / 2) - (_label.Size.Y - _label.TrueHeight), Location.Z);
_watermark.Location = _label.Location;
}
}
else
{
if (_label.Visible) _label.Visible = false;
if (!_watermark.Visible)
{
_watermark.Visible = true;
_watermark.Location = TextLocation switch
{
TextLocation.TrueCenterLeft => new(Location.X + Border + 5, Location.Y + ((Size.Y - _watermark.TrueHeight) / 2) - (_watermark.Size.Y - _watermark.TrueHeight), Location.Z),
TextLocation.PostiveTureCenterLeft => new(Location.X + Border + 5, Location.Y + ((Size.Y - _label.PostiveTrueHeight) / 2) - _label.Size.Y + _label.TrueHeight, Location.Z),
TextLocation.PxLeft => new(Location.X + Border + 5, Location.Y + ((Size.Y - _watermark.Size.Y) / 2), Location.Z),
_ => new(Location.X + Border + 5, Location.Y + Border + 5, Location.Z)
};
_label.Location = _label.Location;
}
}
}
}
public FontInteraction WatermarkFont { get => _watermark.Font; }
public string WatermarkText
{
get
{
return _watermark.Text;
}
set
{
_watermark.Text = value;
}
}
public char? PasswordChar { get => _label.PasswordChar; set => _label.PasswordChar = value; }
public override Vector2i Size
{
get
{
return base.Size;
}
set
{
if (base.Size == value) return;
base.Size = value;
}
}
public override Vector3i Location {
get => base.Location;
set
{
Vector3i diff = base.Location - value;
base.Location = value;
if (_watermark.Visible && !string.IsNullOrEmpty(_watermark.Text))
{
_watermark.Location = TextLocation switch
{
TextLocation.TrueCenterLeft => new(value.X + Border + 5, value.Y + ((Size.Y - _watermark.TrueHeight) / 2) - (_watermark.Size.Y - _watermark.TrueHeight), Location.Z),
TextLocation.PostiveTureCenterLeft => new(value.X + Border + 5, value.Y + ((Size.Y - _label.PostiveTrueHeight) / 2) - _label.Size.Y + _label.TrueHeight, Location.Z),
TextLocation.PxLeft => new(value.X + Border + 5, value.Y + ((Size.Y - _watermark.Size.Y) / 2), Location.Z),
_ => new(value.X + Border + 5, value.Y + Border + 5, Location.Z)
};
_label.Location = _watermark.Location;
}
else
{
_label.Location = TextLocation switch
{
TextLocation.TrueCenterLeft => new(value.X + Border + 5, value.Y + ((Size.Y - _label.TrueHeight) / 2) - (_label.Size.Y - _label.TrueHeight), Location.Z),
TextLocation.PostiveTureCenterLeft => new(value.X + Border + 5, value.Y + ((Size.Y - _label.PostiveTrueHeight) / 2) - _label.Size.Y + _label.TrueHeight, Location.Z),
TextLocation.PxLeft => new(value.X + Border + 5, value.Y + ((Size.Y - _label.Size.Y) / 2), Location.Z),
_ => new(value.X + Border + 5, value.Y + Border + 5, Location.Z)
};
_watermark.Location = _label.Location;
}
}
}
public Color4 TextColor { get => _label.Color; set => _label.Color = value; }
public Color4 WatermarkColor { get => _watermark.Color; set => _watermark.Color = value; }
public override bool Visible
{
get => base.Visible;
set
{
if (value == base.Visible) return;
if (value)
{
if (!string.IsNullOrEmpty(_label.Text))
{
_label.Visible = true;
_watermark.Visible = false;
}
else
{
_label.Visible = false;
_watermark.Visible = true;
}
}
else
{
_label.Visible = value;
_watermark.Visible = value;
}
base.Visible = value;
}
}
public override void Clean()
{
_label.Clean();
_watermark.Clean();
base.Clean();
}
public override void Draw(int x, int y, int w, int h)
{
if (!Visible || !Loaded) return;
int nx = x, ny = y, nw = w, nh = h;
if (Location.X > nw)
return;
else
{
nx += Location.X;
nw -= Location.X;
if (Size.X < nw)
nw = Size.X;
}
if (Location.Y > nh)
return;
else
{
ny += Location.Y;
nh -= Location.Y;
if (Size.Y < nh)
nh = Size.Y;
}
if (nw == 0 || nh == 0) return;
GL.Scissor(nx, Window!.Size.Y - ny - nh, nw, nh);
base.Draw(nx,ny,nw,nh);
GL.Scissor(nx, Window!.Size.Y - ny - nh, nw, nh);
if (!string.IsNullOrEmpty(_label.Text)) _label.Draw(nx,ny,nw,nh);
else _watermark.Draw(nx,ny,nw,nh);
}
public override void LoadToParent(IParent parent, IWindow window)
{
if (Loaded) return;
window.MouseDown += Window_MouseDown;
window.KeyDown += Window_KeyDown;
window.TextInput += WindowOnTextInput;
if (!window.Context.IsCurrent) window.Context.MakeCurrent();
_label.LoadToParent(parent, window);
_watermark.LoadToParent(parent, window);
base.LoadToParent(parent, window);
}
private void WindowOnTextInput(TextInputEventArgs obj)
{
if (!use) return;
Text += obj.AsString;
}
private bool use;
public event Func<KeyboardKeyEventArgs, Task>? KeyPress;
public override void UnFocus()
{
use = false;
if (Window is not null && Window.focused == this)
Window.focused = null;
}
public override void Focus()
{
if (Window is not null)
{
if (Window.focused is not null)
{
Window.focused.UnFocus();
}
Window.focused = this;
use = true;
}
}
private void Window_KeyDown(KeyboardKeyEventArgs obj)
{
if (!use) return;
if (obj.Key == Keys.CapsLock || obj.Key == Keys.Menu || obj.Key == Keys.LeftSuper || obj.Key == Keys.RightSuper || obj.Key == Keys.End || obj.Key == Keys.Home || obj.Key == Keys.PageDown || obj.Key == Keys.PageUp || obj.Key == Keys.Insert || obj.Key == Keys.Up || obj.Key == Keys.Down || obj.Key == Keys.Left || obj.Key == Keys.Right) return;
if (obj.Key == Keys.Backspace || obj.Key == Keys.Delete)
{
if (!(Text.Length > 0)) return;
Text = Text.Remove(Text.Length - 1, 1);
}
if (obj.Key == Keys.V && obj.Control && Window is not null) Text += Window.ClipboardString;
if (KeyPress is not null) _ = KeyPress.Invoke(obj);
}
private void Window_MouseDown(MouseButtonEventArgs e)
{
if (MouseInside && e.Button == MouseButton.Button1)
{
use = true;
Focus();
}
else use = false;
}
}

View File

@ -245,6 +245,9 @@ public class Window : NativeWindow , IWindow
public void ParentResize(ResizeEventArgs e)
{
base.OnResize(e);
if (e.Width == 0 && e.Height == 0 && WindowState != WindowState.Fullscreen) return;
GL.Viewport(0, 0, e.Width, e.Height);
Matrix4.CreateOrthographicOffCenter(0.0f, 800.0f, 0.0f, 600.0f, 0.1f, 100.0f);
@ -285,13 +288,12 @@ public class Window : NativeWindow , IWindow
public void ProcessEvent()
{
ProcessEvents();
ProcessEvents(10);
}
public void StartRender()
{
Context.MakeCurrent();
initthread = Thread.CurrentThread.ManagedThreadId;
//GL.Enable(EnableCap.DepthTest);
MouseMove += WhenMouseMove;
GLFW.PollEvents();
DrawFrame();