Some Code

This commit is contained in:
JacobTech 2023-01-05 14:01:32 -05:00
parent 1d3e319638
commit 78a79b4fd7
11 changed files with 243 additions and 203 deletions

View File

@ -1,5 +1,6 @@
namespace GraphicsManager.Enums;
[Flags]
public enum Rendertype
{
Limit = 0b_0001,

View File

@ -10,7 +10,7 @@
<IncludeSymbols>False</IncludeSymbols>
<RepositoryUrl>https://git.jacobtech.com/JacobTech.com/GraphicsManager</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<Version>1.0.0-alpha99999991</Version>
<Version>1.0.0-alpha99999999996</Version>
</PropertyGroup>
<ItemGroup>

View File

@ -86,6 +86,8 @@ public class Font
return fontclass;
}
public static Font MakeFontFromSystem(uint PixelHeight = 20) => new Font() { PixelHeight = PixelHeight};
public static Font MakeFontFromFile(string Font)
{
_ = Font ?? throw new ArgumentNullException(nameof(Font));

View File

@ -23,6 +23,8 @@ public class FlowLayout : IRenderObject, IParent
t.Start();
Controls.ControlAdded += ControlsOnControlAdded;
Controls.ControlRemoved += ControlsOnControlRemoved;
_bounds.MouseEnter += BoundsOnMouseEnter;
_bounds.MouseLeave += BoundsOnMouseLeave;
}
public void ReportSizeUpdate(IRenderObject Control)
@ -45,31 +47,25 @@ public class FlowLayout : IRenderObject, IParent
}
}
lasty = Controls[Controls.Length - 1].Location.Y + Controls[Controls.Length - 1].Size.Y;
isrole = false;
}
private Task ControlsOnControlRemoved()
{
Console.WriteLine("reset");
lasty = 0;
Controls[0].Location = new(0, 0);
lasty += Controls[0].Size.Y;
if (Controls.Length < 1) return Task.CompletedTask;
if (Controls[0].Location.Y > 0) Controls[0].Location = new(0, 0);
for (int i = 1; i < Controls.Length; i++)
{
lasty += Controls[i].Size.Y;
Controls[i].Location = new(0, Controls[Controls.Length - 1].Location.Y + Controls[Controls.Length - 1].Size.Y);
}
return Task.CompletedTask;
}
private int lasty = 0;
private Task ControlsOnControlAdded(IRenderObject arg)
{
if (Controls.Length > 0) arg.Location = new(0, Controls[Controls.Length - 1].Location.Y + Controls[Controls.Length - 1].Size.Y);
else arg.Location = new(0, 0);
lasty += arg.Size.Y;
arg.Size = new(Size.X, arg.Size.Y);
arg.Anchor = ObjectAnchor.Left | ObjectAnchor.Right | ObjectAnchor.Top;
return Task.CompletedTask;
@ -80,26 +76,24 @@ public class FlowLayout : IRenderObject, IParent
if (Clicked is not null) _ = Clicked.Invoke(arg);
return Task.CompletedTask;
}
private Task BoundsOnMouseLeave(IRenderObject arg)
{
inside = false;
if (MouseLeave is not null) _ = MouseLeave.Invoke(this);
return Task.CompletedTask;
}
private bool inside = false;
private Task BoundsOnMouseEnter(IRenderObject arg)
{
inside = true;
if (MouseEnter is not null) _ = MouseEnter.Invoke(this);
return Task.CompletedTask;
}
public ControlList Controls { get; } = new();
public ObjectAnchor Anchor { get => _bounds.Anchor; set => _bounds.Anchor = value; }
public Color4 BackgroundColor
{
get
{
Uniform<Vector4>? u4 = _bounds.Uniforms.Uniform4.Where(u => u.Location == 0).First();
if (u4 is null) u4 = new() { Location = 0, Value = new(1, 1, 0, 1) };
return new Color4(u4.Value.X, u4.Value.X, u4.Value.X, u4.Value.X);
}
set
{
Uniform<Vector4> u4 = _bounds.Uniforms.Uniform4.Where(u => u.Location == 0).First();
if (u4 is not null) _bounds.Uniforms.Uniform4.Remove(u4);
if (u4 is null) u4 = new() { Location = 0 };
u4.Value = new(value.R, value.G, value.B, value.A);
_bounds.Uniforms.Uniform4.Add(u4);
}
}
public Color4 BackgroundColor { get => _bounds.BackgroundColor; set => _bounds.BackgroundColor = value; }
public bool Visible { get => _bounds.Visible; set => _bounds.Visible = value; }
public Vector2i Size
{
@ -174,6 +168,7 @@ public class FlowLayout : IRenderObject, IParent
{
try
{
if (!inside) return;
isrole = true;
dis = obj;
if (scrols.Any())
@ -182,7 +177,7 @@ public class FlowLayout : IRenderObject, IParent
{
scrols.Dequeue().Invoke();
}
Parent!.TryDraw();
}
}
catch (Exception e)
@ -203,17 +198,20 @@ public class FlowLayout : IRenderObject, IParent
{
if (Controls.Length < 1) return;
bool down = dis.OffsetY < 0;//scrole wheel dir
bool moved = false;
if (down && ((Controls[Controls.Length - 1].Location.Y + Controls[Controls.Length - 1].Size.Y) > Size.Y)) //can go down
{
moved = true;
for (int i = 0; i < Controls.Length; i++)
{
Controls[i].Location = new((int)(Controls[i].Location.X),
(int)(Controls[i].Location.Y - (dis.OffsetY * HScrollPixels)));
(int)(Controls[i].Location.Y + (dis.OffsetY * HScrollPixels)));
}
}
if (!down && (Controls[0].Location.Y < 0)) // can go up
{
float newy = Controls[0].Location.Y - (dis.OffsetY * HScrollPixels);
moved = true;
float newy = Controls[0].Location.Y + (dis.OffsetY * HScrollPixels);
if (newy > 0) newy = 0;
Controls[0].Location = new(0, (int)newy);
for (int i = 1; i < Controls.Length; i++)
@ -223,7 +221,10 @@ public class FlowLayout : IRenderObject, IParent
}
}
lasty = Controls[Controls.Length - 1].Location.Y + Controls[Controls.Length - 1].Size.Y;
if (moved)
{
Parent!.TryDraw();
}
}));
}

View File

@ -4,6 +4,8 @@ using GraphicsManager.Objects.Core;
using GraphicsManager.Structs;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Mathematics;
using OpenTK.Windowing.Common;
using OpenTK.Windowing.GraphicsLibraryFramework;
using SharpFont;
using Encoding = SharpFont.Encoding;
@ -19,7 +21,18 @@ public class Label : IRenderObject
public Vector2 LocationAsFloat { get { return laf; } }
public Vector2 SizeAsFloat { get { return saf; } }
private char? pc = null;
public char? PasswordChar
{
get => pc;
set
{
pc = value;
if (Parent is not null) Parent.TryDraw();
}
}
private bool _Visible = true;
public bool Visible
{
get => _Visible;
@ -40,11 +53,17 @@ public class Label : IRenderObject
get => text;
set
{
if (value is null) value = string.Empty;
text = value;
if (!_characters.ContainsKey(Font)) _characters.Add(Font, new Dictionary<uint, Character>());
float addy = Font.PixelHeight * Scale, addx = 0F, char_x = 0F, hhh = 0F;
foreach (char character in value)
for (int i = 0; i < value.Length; i++)
{
char character;
if (PasswordChar is null)
character = value[i];
else
character = PasswordChar.Value;
if (!_characters[Font].ContainsKey(character))
{
var f = Texture.TextureForChar(Font, character, Font.Faces.ToArray());
@ -61,7 +80,7 @@ public class Label : IRenderObject
char_x += (_characters[Font][character].Advance >> 6) * Scale;
if (xrel + w > addx) addx = xrel + w;
}
Size = new((int)addx, (int)addy);
if (Loaded)
{
@ -73,7 +92,7 @@ public class Label : IRenderObject
public Shader Shader { get; set; } = DefaultTextShader;
public Font Font { get; set; } = DefaultFont;
public float Scale { get; set; } = 1.0f;
public Vector4 Color { get; set; } = new Vector4(1, 1, 1, 1);
public Color4 Color { get; set; } = new Color4(255, 255, 255, 255);
public Vector2i Distance { get; private set; }
private Vector2i loc_ = new();
private Vector2i locc_ = new();
@ -87,6 +106,7 @@ public class Label : IRenderObject
{
loc_ = value;
if (Window is null || Parent is null) return;
Console.WriteLine(value.X);
locc_ = new((int)Window.FloatToInt(Parent.IntToFloat(value.X)), (int)Window.FloatToInt(Parent.IntToFloat(value.Y + (int)Font.PixelHeight, true), true));
if (Window.CanControleUpdate && Loaded) Parent!.TryDraw();
}
@ -122,38 +142,42 @@ public class Label : IRenderObject
GL.ActiveTexture(TextureUnit.Texture0);
float hhh = 0f;
foreach (char c in Text)
for (int i = 0; i < Text.Length; i++)
{
if (!_characters[Font].ContainsKey(c)) break;
Character ch = _characters[Font][c];
int maxx = 0;
int maxy = (int)Font.PixelHeight;
if (c == '\n')
{
hhh += Font.PixelHeight;
char_x = 0f;
maxy += (int)Font.PixelHeight;
}
char c;
if (PasswordChar is null)
c = Text[i];
else
{
float w = ch.Size.X * Scale;
float h = ch.Size.Y * Scale;
float xrel = char_x + ch.Bearing.X * Scale;
float yrel = (ch.Size.Y - ch.Bearing.Y) * Scale;
yrel += hhh;
char_x += (ch.Advance >> 6) * Scale;
if (xrel + w > maxx) maxx = (int)(xrel + w);
Matrix4 scaleM = Matrix4.CreateScale(new Vector3(w, h, 1.0f));
Matrix4 transRelM = Matrix4.CreateTranslation(new Vector3(xrel, yrel, 0.0f));
c = PasswordChar.Value;
if (!_characters[Font].ContainsKey(c)) break;
Character ch = _characters[Font][c];
int maxx = 0;
int maxy = (int)Font.PixelHeight;
if (c == '\n')
{
hhh += Font.PixelHeight;
char_x = 0f;
maxy += (int)Font.PixelHeight;
}
else
{
float w = ch.Size.X * Scale;
float h = ch.Size.Y * Scale;
float xrel = char_x + ch.Bearing.X * Scale;
float yrel = (ch.Size.Y - ch.Bearing.Y) * Scale;
yrel += hhh;
char_x += (ch.Advance >> 6) * Scale;
if (xrel + w > maxx) maxx = (int)(xrel + w);
Matrix4 scaleM = Matrix4.CreateScale(new Vector3(w, h, 1.0f));
Matrix4 transRelM = Matrix4.CreateTranslation(new Vector3(xrel, yrel, 0.0f));
Matrix4 modelM = scaleM * transRelM * rotateM * transOriginM;
GL.UniformMatrix4(0, false, ref modelM);
Matrix4 modelM = scaleM * transRelM * rotateM * transOriginM;
GL.UniformMatrix4(0, false, ref modelM);
ch.Texture.Use();
GL.DrawArrays(PrimitiveType.Triangles, 0, 6);
}
ch.Texture.Use();
GL.DrawArrays(PrimitiveType.Triangles, 0, 6);
}
}
GL.Disable(EnableCap.Blend);
@ -167,6 +191,8 @@ public class Label : IRenderObject
if (!_characters.ContainsKey(Font)) _characters.Add(Font, new Dictionary<uint, Character>());
Parent = window;
Window = win;
Window.MouseMove += WindowOnMouseMove;
Window.MouseDown += WindowOnMouseDown;
GL.PixelStore(PixelStoreParameter.UnpackAlignment, 1);
GL.PixelStore(PixelStoreParameter.UnpackAlignment, 4);
float[] vquad =
@ -196,9 +222,33 @@ public class Label : IRenderObject
Loaded = true;
Text = Text;
Location = Location;
Distance = new(window.Size.X - Size.X - Location.X, window.Size.Y - Size.Y - Location.Y);
Distance = new(Parent.Size.X - Size.X - Location.X, Parent.Size.Y - Size.Y - Location.Y);
if (WindowLoaded is not null) WindowLoaded.Invoke(this);
}
private void WindowOnMouseDown(MouseButtonEventArgs obj)
{
if (mouseinside && obj.Button == MouseButton.Button1 && Clicked is not null) _ = Clicked.Invoke(this);
}
private bool mouseinside = false;
private void WindowOnMouseMove(MouseMoveEventArgs obj)
{
if (Parent?.IntToFloat(Location.X) <= Window?.IntToFloat((float)Parent?.MousePosition.X!) &&
Parent?.IntToFloat(Size.X + Location.X) >= Window?.IntToFloat((float)Parent?.MousePosition.X!) &&
Parent?.IntToFloat(Location.Y + Size.Y, true) <= Window?.IntToFloat((float)Parent?.MousePosition.Y!, true) &&
Parent?.IntToFloat(Location.Y, true) >= Window?.IntToFloat((float)Parent?.MousePosition.Y!, true))
{
if (MouseEnter is not null && !mouseinside) _ = MouseEnter.Invoke(this);
mouseinside = true;
}
else
{
if (MouseLeave is not null && mouseinside) _ = MouseLeave.Invoke(this);
mouseinside = false;
}
}
public event Func<IRenderObject, Task>? Clicked;
public event Func<IRenderObject, Task>? WindowLoaded;
public event Func<IRenderObject, Task>? MouseEnter;

View File

@ -3,6 +3,7 @@ using GraphicsManager.Interfaces;
using GraphicsManager.Objects.Core;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Mathematics;
using OpenTK.Windowing.Common;
using OpenTK.Windowing.GraphicsLibraryFramework;
namespace GraphicsManager.Objects;
@ -32,9 +33,8 @@ public class Rectangle : ITextureObject
}
}
public Uniforms Uniforms { get; } = new() { Uniform4 = new() { new() { Location = 0, Value = new(0,0,0,1) } } };
public Color4 BackgroundColor { get; set; } = new(0, 0, 0, 255);
//public bool Visible { get; set; } = true;
private bool _Visible = true;
public bool Visible
{
@ -52,22 +52,7 @@ public class Rectangle : ITextureObject
{
if (Texture is not null) Texture.Use();
Shader.Use();
for (int i = 0; i < Uniforms.Uniform4.Count; i++)
{
GL.Uniform4(Uniforms.Uniform4[i].Location, Uniforms.Uniform4[i].Value);
}
for (int i = 0; i < Uniforms.Uniform3.Count; i++)
{
GL.Uniform3(Uniforms.Uniform3[i].Location, Uniforms.Uniform3[i].Value);
}
for (int i = 0; i < Uniforms.Uniform2.Count; i++)
{
GL.Uniform2(Uniforms.Uniform2[i].Location, Uniforms.Uniform2[i].Value);
}
for (int i = 0; i < Uniforms.Uniform1.Count; i++)
{
GL.Uniform1(Uniforms.Uniform1[i].Location, Uniforms.Uniform1[i].Value);
}
GL.Uniform4(0, BackgroundColor);
if (Texture is not null)
{
GL.Enable(EnableCap.Blend);
@ -119,24 +104,36 @@ public class Rectangle : ITextureObject
GL.BufferData(BufferTarget.ElementArrayBuffer, Indexs.Length * sizeof(uint), Indexs, Hint);
Loaded = true;
Window.MouseDown += Window_MouseDown;
Window.MouseMove += WindowOnMouseMove;
Location = Location;
Distance = new(Parent.Size.X - Size.X - Location.X, Parent.Size.Y - Size.Y - Location.Y);
if (WindowLoaded is not null) WindowLoaded.Invoke(this);
}
private bool mouseinside = false;
private void WindowOnMouseMove(MouseMoveEventArgs e)
{
if (Parent?.IntToFloat(Location.X) <= Window?.IntToFloat((float)Parent?.MousePosition.X!) &&
Parent?.IntToFloat(Size.X + Location.X) >= Window?.IntToFloat((float)Parent?.MousePosition.X!) &&
Parent?.IntToFloat(Location.Y + Size.Y, true) <= Window?.IntToFloat((float)Parent?.MousePosition.Y!, true) &&
Parent?.IntToFloat(Location.Y, true) >= Window?.IntToFloat((float)Parent?.MousePosition.Y!, true))
{
if (MouseEnter is not null && !mouseinside) _ = MouseEnter.Invoke(this);
mouseinside = true;
}
else
{
if (MouseLeave is not null && mouseinside) _ = MouseLeave.Invoke(this);
mouseinside = false;
}
}
public IParent? Parent { get; private set; }
public Window? Window { get; private set; }
private void Window_MouseDown(OpenTK.Windowing.Common.MouseButtonEventArgs e)
{
if (e.Button == MouseButton.Button1 &&
Parent?.IntToFloat(Location.X) <= Parent?.IntToFloat((int)Parent?.MousePosition.X!) &&
Parent?.IntToFloat(Size.X + Location.X) >= Parent?.IntToFloat((int)Parent?.MousePosition.X!) &&
Parent?.IntToFloat(Location.Y + Size.Y, true) <= Parent?.IntToFloat((int)Parent?.MousePosition.Y!, true) &&
Parent?.IntToFloat(Location.Y, true) >= Parent?.IntToFloat((int)Parent?.MousePosition.Y!, true))
{
if (Clicked is not null) Clicked.Invoke(this);
}
if (mouseinside && e.Button == MouseButton.Button1 && Clicked is not null) _ = Clicked.Invoke(this);
}
~Rectangle()
@ -167,7 +164,7 @@ public class Rectangle : ITextureObject
{
if (Loaded)
{
Distance = new(Parent!.Size.X - Size.X - Location.X, Parent.Size.Y - Size.Y - Location.Y);
//Distance = new(Parent!.Size.X - Size.X - Location.X, Parent.Size.Y - Size.Y - Location.Y);
int add = 3;
if (Texture is not null) add = 5;
GL.BindBuffer(BufferTarget.ArrayBuffer, BufferObject);

View File

@ -16,6 +16,20 @@ public class RoundedButton : IRenderObject
_bounds = new RoundedRectangle();
_inside = new RoundedRectangle();
_label = new Label();
_bounds.MouseEnter += BoundsOnMouseEnter;
_bounds.MouseLeave += BoundsOnMouseLeave;
}
private Task BoundsOnMouseLeave(IRenderObject arg)
{
if (MouseLeave is not null) _ = MouseLeave.Invoke(this);
return Task.CompletedTask;
}
private Task BoundsOnMouseEnter(IRenderObject arg)
{
if (MouseEnter is not null) _ = MouseEnter.Invoke(this);
return Task.CompletedTask;
}
public event Func<IRenderObject, Task>? WindowLoaded;
@ -56,41 +70,8 @@ public class RoundedButton : IRenderObject
public Vector2i Distance { get => _bounds.Distance; }
public IParent? Parent { get; private set; } = null;
public Window? Window { get; private set; } = null;
public Color4 InsideColor
{
get
{
Uniform<Vector4>? u4 = _inside.Uniforms.Uniform4.Where(u => u.Location == 0).First();
if (u4 is null) u4 = new() { Location = 0, Value = new(1, 1, 0, 1) };
return new Color4(u4.Value.X, u4.Value.X, u4.Value.X, u4.Value.X);
}
set
{
Uniform<Vector4> u4 = _inside.Uniforms.Uniform4.Where(u => u.Location == 0).First();
if (u4 is not null) _inside.Uniforms.Uniform4.Remove(u4);
if (u4 is null) u4 = new() { Location = 0 };
u4.Value = new(value.R, value.G, value.B, value.A);
_inside.Uniforms.Uniform4.Add(u4);
}
}
public Color4 BorderColor
{
get
{
Uniform<Vector4> u4 = _bounds.Uniforms.Uniform4.Where(u => u.Location == 0).First();
if (u4 is null) u4 = new() { Location = 0, Value = new(1, 1, 0, 1) };
return new Color4(u4.Value.X, u4.Value.X, u4.Value.X, u4.Value.X);
}
set
{
Uniform<Vector4> u4 = _bounds.Uniforms.Uniform4.Where(u => u.Location == 0).First();
if (u4 is not null) _bounds.Uniforms.Uniform4.Remove(u4);
if (u4 is null) u4 = new() { Location = 0 };
u4.Value = new(value.R, value.G, value.B, value.A);
_bounds.Uniforms.Uniform4.Add(u4);
}
}
public Color4 InsideColor { get => _inside.BackgroundColor; set => _inside.BackgroundColor = value; }
public Color4 BorderColor { get => _bounds.BackgroundColor; set => _bounds.BackgroundColor = value; }
public bool Visible
{
get => _bounds.Visible;

View File

@ -5,6 +5,7 @@ using OpenTK.Graphics.OpenGL4;
using OpenTK.Mathematics;
using OpenTK.Windowing.GraphicsLibraryFramework;
using System.Runtime.Intrinsics.X86;
using OpenTK.Windowing.Common;
namespace GraphicsManager.Objects;
@ -25,7 +26,7 @@ public class RoundedRectangle : IRenderObject
public event Func<IRenderObject, Task>? MouseLeave;
public object? Tag { get; set; } = null;
public Uniforms Uniforms { get; set; } = new() { Uniform4 = new() { new() { Location = 0, Value = new(0,0,0,1) } } };
public Color4 BackgroundColor { get; set; } = new(0, 0, 0, 255);
public int Radius
{
get
@ -110,18 +111,7 @@ public class RoundedRectangle : IRenderObject
GL.Hint(HintTarget.PolygonSmoothHint, HintMode.Nicest);
GL.Hint(HintTarget.PointSmoothHint, HintMode.Nicest);
Shader.Use();
for (int i = 0; i < Uniforms.Uniform4.Count; i++)
{
GL.Uniform4(Uniforms.Uniform4[i].Location, Uniforms.Uniform4[i].Value);
}
for (int i = 0; i < Uniforms.Uniform3.Count; i++)
{
GL.Uniform3(Uniforms.Uniform3[i].Location, Uniforms.Uniform3[i].Value);
}
for (int i = 0; i < Uniforms.Uniform2.Count; i++)
{
GL.Uniform2(Uniforms.Uniform2[i].Location, Uniforms.Uniform2[i].Value);
}
GL.Uniform4(0, BackgroundColor);
GL.BindVertexArray(ArrayObject);
GL.DrawElements(PrimitiveType.Triangles, Indexs.Length, DrawElementsType.UnsignedInt, 0);
@ -159,24 +149,36 @@ public class RoundedRectangle : IRenderObject
GL.BufferData(BufferTarget.ElementArrayBuffer, Indexs.Length * sizeof(uint), Indexs, Hint);
Loaded = true;
Window.MouseDown += Window_MouseDown;
Window.MouseMove += WindowOnMouseMove;
Location = Location;
Distance = new(Parent.Size.X - Size.X - Location.X, Parent.Size.Y - Size.Y - Location.Y);
if (WindowLoaded is not null) WindowLoaded.Invoke(this);
}
private bool mouseinside = false;
private void WindowOnMouseMove(MouseMoveEventArgs obj)
{
if (Parent?.IntToFloat(Location.X) <= Window?.IntToFloat((float)Parent?.MousePosition.X!) &&
Parent?.IntToFloat(Size.X + Location.X) >= Window?.IntToFloat((float)Parent?.MousePosition.X!) &&
Parent?.IntToFloat(Location.Y + Size.Y, true) <= Window?.IntToFloat((float)Parent?.MousePosition.Y!, true) &&
Parent?.IntToFloat(Location.Y, true) >= Window?.IntToFloat((float)Parent?.MousePosition.Y!, true))
{
if (MouseEnter is not null && !mouseinside) _ = MouseEnter.Invoke(this);
mouseinside = true;
}
else
{
if (MouseLeave is not null && mouseinside) _ = MouseLeave.Invoke(this);
mouseinside = false;
}
}
public IParent? Parent { get; private set; }
public Window? Window { get; private set; }
private void Window_MouseDown(OpenTK.Windowing.Common.MouseButtonEventArgs e)
{
if (e.Button == MouseButton.Button1 &&
Parent?.IntToFloat(Location.X) <= Parent?.IntToFloat((int)Parent?.MousePosition.X!) &&
Parent?.IntToFloat(Size.X + Location.X) >= Parent?.IntToFloat((int)Parent?.MousePosition.X!) &&
Parent?.IntToFloat(Location.Y + Size.Y, true) <= Parent?.IntToFloat((int)Parent?.MousePosition.Y!, true) &&
Parent?.IntToFloat(Location.Y, true) >= Parent?.IntToFloat((int)Parent?.MousePosition.Y!, true))
{
if (Clicked is not null) Clicked.Invoke(this);
}
if (mouseinside && e.Button == MouseButton.Button1 && Clicked is not null) _ = Clicked.Invoke(this);
}
~RoundedRectangle()
@ -205,7 +207,7 @@ public class RoundedRectangle : IRenderObject
{
if (Loaded)
{
Distance = new(Parent!.Size.X - Size.X - Location.X, Parent.Size.Y - Size.Y - Location.Y);
//Distance = new(Parent!.Size.X - Size.X - Location.X, Parent.Size.Y - Size.Y - Location.Y);
int add = 3;
GL.BindBuffer(BufferTarget.ArrayBuffer, BufferObject);
GL.BindVertexArray(ArrayObject);

View File

@ -16,6 +16,8 @@ public class Textbox : IRenderObject
_bounds = new RoundedRectangle();
_inside = new RoundedRectangle();
_label = new Label();
_bounds.MouseEnter += BoundsOnMouseEnter;
_bounds.MouseLeave += BoundsOnMouseLeave;
}
public event Func<IRenderObject, Task>? WindowLoaded;
@ -28,6 +30,7 @@ public class Textbox : IRenderObject
public ObjectAnchor Anchor { get => _bounds.Anchor; set { _bounds.Anchor = value; _inside.Anchor = value; _label.Anchor = value; } }
public Font Font { get => _label.Font; set => _label.Font = value; }
public string Text { get => _label.Text; set => _label.Text = value; }
public char? PasswordChar { get => _label.PasswordChar; set => _label.PasswordChar = value; }
public bool Loaded { get; private set; } = false;
public Vector2i Size
{
@ -55,47 +58,26 @@ public class Textbox : IRenderObject
public Vector2i Distance { get => _bounds.Distance; }
public IParent? Parent { get; private set; } = null;
public Window? Window { get; private set; } = null;
public Color4 InsideColor
{
get
{
Uniform<Vector4>? u4 = _inside.Uniforms.Uniform4.Where(u => u.Location == 0).First();
if (u4 is null) u4 = new() { Location = 0, Value = new(1, 1, 0, 1) };
return new Color4(u4.Value.X, u4.Value.X, u4.Value.X, u4.Value.X);
}
set
{
Uniform<Vector4> u4 = _inside.Uniforms.Uniform4.Where(u => u.Location == 0).First();
if (u4 is not null) _inside.Uniforms.Uniform4.Remove(u4);
if (u4 is null) u4 = new() { Location = 0 };
u4.Value = new(value.R, value.G, value.B, value.A);
_inside.Uniforms.Uniform4.Add(u4);
}
}
public Color4 BorderColor
{
get
{
Uniform<Vector4> u4 = _bounds.Uniforms.Uniform4.Where(u => u.Location == 0).First();
if (u4 is null) u4 = new() { Location = 0, Value = new(1, 1, 0, 1) };
return new Color4(u4.Value.X, u4.Value.X, u4.Value.X, u4.Value.X);
}
set
{
Uniform<Vector4> u4 = _bounds.Uniforms.Uniform4.Where(u => u.Location == 0).First();
if (u4 is not null) _bounds.Uniforms.Uniform4.Remove(u4);
if (u4 is null) u4 = new() { Location = 0 };
u4.Value = new(value.R, value.G, value.B, value.A);
_bounds.Uniforms.Uniform4.Add(u4);
}
}
public Color4 InsideColor { get => _inside.BackgroundColor; set => _inside.BackgroundColor = value; }
public Color4 BorderColor { get => _bounds.BackgroundColor; set => _bounds.BackgroundColor = value; }
public bool Visible
{
get => _bounds.Visible;
set => _bounds.Visible = value;
}
public event Func<IRenderObject, Task>? Clicked;
private Task BoundsOnMouseLeave(IRenderObject arg)
{
if (MouseLeave is not null) _ = MouseLeave.Invoke(this);
return Task.CompletedTask;
}
private Task BoundsOnMouseEnter(IRenderObject arg)
{
if (MouseEnter is not null) _ = MouseEnter.Invoke(this);
return Task.CompletedTask;
}
public void Clean()
{
@ -135,6 +117,7 @@ public class Textbox : IRenderObject
}
private bool use = false;
public event Func<KeyboardKeyEventArgs, Task>? KeyPress;
private void Window_KeyDown(OpenTK.Windowing.Common.KeyboardKeyEventArgs obj)
{
if (!use) return;
@ -144,15 +127,17 @@ public class Textbox : IRenderObject
if (!(Text.Length > 0)) return;
Text = Text.Remove(Text.Length - 1, 1);
}
if (KeyPress is not null) _ = KeyPress.Invoke(obj);
}
private void Window_MouseDown(OpenTK.Windowing.Common.MouseButtonEventArgs e)
{
if (e.Button == MouseButton.Button1 &&
Parent?.IntToFloat(Location.X) <= Parent?.IntToFloat((int)Parent?.MousePosition.X!) &&
Parent?.IntToFloat(Size.X + Location.X) >= Parent?.IntToFloat((int)Parent?.MousePosition.X!) &&
Parent?.IntToFloat(Location.Y + Size.Y, true) <= Parent?.IntToFloat((int)Parent?.MousePosition.Y!, true) &&
Parent?.IntToFloat(Location.Y, true) >= Parent?.IntToFloat((int)Parent?.MousePosition.Y!, true))
Parent?.IntToFloat(Location.X) <= Window?.IntToFloat((int)Window?.MousePosition.X!) &&
Parent?.IntToFloat(Size.X + Location.X) >= Window?.IntToFloat((int)Window?.MousePosition.X!) &&
Parent?.IntToFloat(Location.Y + Size.Y, true) <= Window?.IntToFloat((int)Window?.MousePosition.Y!, true) &&
Parent?.IntToFloat(Location.Y, true) >= Window?.IntToFloat((int)Window?.MousePosition.Y!, true))
{
use = true;
if (Clicked is not null) Clicked.Invoke(this);

View File

@ -14,6 +14,8 @@ public class UserControl : IRenderObject, IParent
{
_bounds = new Rectangle();
_bounds.Clicked += _bounds_Clicked;
_bounds.MouseEnter += BoundsOnMouseEnter;
_bounds.MouseLeave += BoundsOnMouseLeave;
}
public void TryDraw()
@ -29,23 +31,9 @@ public class UserControl : IRenderObject, IParent
public ControlList Controls { get; } = new();
public ObjectAnchor Anchor { get => _bounds.Anchor; set => _bounds.Anchor = value; }
public Color4 BackgroundColor
{
get
{
Uniform<Vector4>? u4 = _bounds.Uniforms.Uniform4.Where(u => u.Location == 0).First();
if (u4 is null) u4 = new() { Location = 0, Value = new(1, 1, 0, 1) };
return new Color4(u4.Value.X, u4.Value.X, u4.Value.X, u4.Value.X);
}
set
{
Uniform<Vector4> u4 = _bounds.Uniforms.Uniform4.Where(u => u.Location == 0).First();
if (u4 is not null) _bounds.Uniforms.Uniform4.Remove(u4);
if (u4 is null) u4 = new() { Location = 0 };
u4.Value = new(value.R, value.G, value.B, value.A);
_bounds.Uniforms.Uniform4.Add(u4);
}
}
public Color4 BackgroundColor { get => _bounds.BackgroundColor; set => _bounds.BackgroundColor = value; }
public bool Visible { get => _bounds.Visible; set => _bounds.Visible = value; }
private bool res = false;
public Vector2i Size
@ -142,6 +130,17 @@ public class UserControl : IRenderObject, IParent
}
_bounds.Clean();
}
private Task BoundsOnMouseLeave(IRenderObject arg)
{
if (MouseLeave is not null) _ = MouseLeave.Invoke(this);
return Task.CompletedTask;
}
private Task BoundsOnMouseEnter(IRenderObject arg)
{
if (MouseEnter is not null) _ = MouseEnter.Invoke(this);
return Task.CompletedTask;
}
public void ParentResize(ResizeEventArgs e)
{

View File

@ -65,7 +65,7 @@ public class Window : NativeWindow , IParent
public float IntToFloat(float p, bool Invert = false)
{
float Size = (Invert ? this.Size.Y : this.Size.X);
double Size = (Invert ? this.Size.Y : this.Size.X);
double half = Math.Round((double)Size / (double)2, 1);
double Per = Math.Round((double)1 / half, 15);
if (p == half) return 0.0f;
@ -83,7 +83,7 @@ public class Window : NativeWindow , IParent
public float FloatToInt(float p, bool Invert = false)
{
float Size = (Invert ? this.Size.Y : this.Size.X);
double Size = (Invert ? this.Size.Y : this.Size.X);
double half = Math.Round((double)Size / (double)2, 15);
if (p == 0) return (int)half;
if (Invert)
@ -177,6 +177,7 @@ public class Window : NativeWindow , IParent
public void StartRender()
{
Context.MakeCurrent();
initthread = Thread.CurrentThread.ManagedThreadId;
ProcessEvents();
DrawFrame();
if (WindowLoaded is not null) WindowLoaded.Invoke(this);
@ -185,6 +186,10 @@ public class Window : NativeWindow , IParent
ProcessEvents();
bool u = (Rendertype & Rendertype.ControlUpdates) == Rendertype.ControlUpdates;
if (!u) DrawFrame();
if (invokes.Any())
{
for (int i = 0; i < invokes.Count; i++) invokes.Dequeue().Invoke();
}
Thread.Sleep(8);
}
}
@ -194,6 +199,23 @@ public class Window : NativeWindow , IParent
{
if (!res) DrawFrame();
}
private int initthread = 0;
public bool InvokeRequired
{
get
{
return initthread != Thread.CurrentThread.ManagedThreadId;
}
}
private Queue<Action> invokes = new();
public void Invoke(Action A)
{
invokes.Enqueue(A);
}
public void DrawFrame()
{