Better Code?

This commit is contained in:
JacobTech 2024-04-27 17:18:55 -04:00
parent 06bc325fbe
commit 31d20221a6
12 changed files with 72 additions and 97 deletions

View File

@ -47,6 +47,8 @@ public class FPSWindow : GameWindow , IWindow
ClientRectangle = new Box2i(num, num2, num + Size.X, num2 + Size.Y);
}
public Vector2i CS { get; private set; } = new(0, 0);
public Matrix4 WindowSizeMatrix { get; private set; }

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-alpha66</Version>
<Version>1.0.9-alpha81</Version>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">

View File

@ -1,6 +1,7 @@
using GraphicsManager.Enums;
using GraphicsManager.Objects;
using OpenTK.Mathematics;
using OpenTK.Windowing.Common;
using OpenTK.Windowing.Common.Input;
namespace GraphicsManager.Interfaces;
@ -21,6 +22,8 @@ public interface IRenderObject
public void Clean();
public void Focus();
public void UnFocus();
public void SendKeyEvent(KeyboardKeyEventArgs KeyArgs);
public void SendClipEvent(string Text);
public Vector2i Size { get; set; }
public Vector3i Location { get; set; }
public Vector2 SizeAsFloat { get; }

View File

@ -11,7 +11,7 @@ public interface IWindow : IParent
public bool ShowMissingChar { get; }
public Matrix4 WindowSizeMatrix { get; }
public Vector2i ClientSize { get; }
public Vector2i CS { get; }
public event Action<MouseButtonEventArgs> MouseDown;

View File

@ -83,7 +83,7 @@ public abstract class ParentBase : Rectangle, IParent
nh = Size.Y;
}
if (nw == 0 || nh == 0) return;
GL.Scissor(nx, Window!.ClientSize.Y - ny - nh, nw, nh);
GL.Scissor(nx, Window!.CS.Y - ny - nh, nw, nh);
base.Draw(nx,ny,nw,nh);
IEnumerable<IRenderObject> needload = Controls.Where(a => a.Loaded == false);
@ -100,7 +100,7 @@ public abstract class ParentBase : Rectangle, IParent
for (int i = 0; i < Controls.Length; i++)
{
if (Controls[i].Location.X > Size.X || Controls[i].Location.Y > Size.Y) continue;
GL.Scissor(nx, Window!.ClientSize.Y - ny - nh, nw, nh);
GL.Scissor(nx, Window!.CS.Y - ny - nh, nw, nh);
Controls[i].Draw(nx, ny, nw, nh);
}
}

View File

@ -114,6 +114,12 @@ public class Shader : IDisposable
GL.UseProgram(Handle);
GL.UniformMatrix4(_uniformLocations[name], true, ref data);
}
public void SetMatrixF4(string name, Matrix4 data)
{
GL.UseProgram(Handle);
GL.UniformMatrix4(_uniformLocations[name], false, ref data);
}
public void Use()
{

View File

@ -226,7 +226,7 @@ public class FlowLayout : ParentBase, IFlow
}
Timer t;
private int dist = 0;
private int dist;
private void WindowOnMouseWheel(MouseWheelEventArgs obj)
{
@ -245,7 +245,7 @@ public class FlowLayout : ParentBase, IFlow
public uint HScrollPixels { get; set; } = 30;
private uint ScrollValueBackEnd { get; set; } = 0;
private uint ScrollValueBackEnd { get; set; }
public event Func<bool, uint, uint, Task>? FlowUpdate;
@ -283,7 +283,7 @@ public class FlowLayout : ParentBase, IFlow
}
}
private uint ss = 0;
private uint ss;
public uint ScrollSum
{
@ -293,7 +293,7 @@ public class FlowLayout : ParentBase, IFlow
ss = value;
}
}
private bool scroleip = false;
private bool scroleip;
private void TOnElapsed(object? sender, ElapsedEventArgs e)
{
try
@ -301,7 +301,7 @@ public class FlowLayout : ParentBase, IFlow
if (!scroleip && dist != 0)
{
scroleip = true;
var n = ScrollValue - dist;
long n = ScrollValue - dist;
if (n < 0) n = 0;
if (n > MaxScrollValue) n = MaxScrollValue;
Window!.Invoke(() =>ScrollValue = (uint)n);

View File

@ -314,7 +314,17 @@ public class Label : ILabel
max_x = (xrel + w);
}
}
return new((int)max_x, (int)(lines * LineHeight) + (int)(lines * Font.ExtraLinePixels));
return new((int)char_x, (int)(lines * LineHeight) + (int)(lines * Font.ExtraLinePixels));
}
public virtual void SendKeyEvent(KeyboardKeyEventArgs KeyArgs)
{
}
public virtual void SendClipEvent(string ClipString)
{
}
public void Clean()
@ -349,11 +359,9 @@ public class Label : ILabel
Shader.Use();
GL.Enable(EnableCap.Blend);
GL.Uniform4(Shader.GetUniformLocation("textColor"), Color);
Matrix4 projectionM = Matrix4.CreateOrthographicOffCenter(0, Window!.Size.X, Window!.Size.Y, 0, -1.0f, 1.0f);
projectionM = this.Window.WindowSizeMatrix;
GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);
GL.BlendFunc(0, BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
GL.UniformMatrix4(1, false, ref projectionM);
Shader.SetMatrixF4("projection", Window.WindowSizeMatrix);
GL.BindVertexArray(VAO);

View File

@ -302,6 +302,16 @@ public class RainbowLabel : ILabel
}
}
}
}
public virtual void SendKeyEvent(KeyboardKeyEventArgs KeyArgs)
{
}
public virtual void SendClipEvent(string ClipString)
{
}
public void ForceDistanceUpdate()
{

View File

@ -93,6 +93,15 @@ public class Rectangle : ITextureObject
}
public Action? OnDrawAction;
public virtual void SendKeyEvent(KeyboardKeyEventArgs KeyArgs)
{
}
public virtual void SendClipEvent(string ClipString)
{
}
//public Action? OnDrawExitAction;
public virtual void Draw(int x, int y, int w, int h)

Binary file not shown.

After

Width:  |  Height:  |  Size: 393 B

View File

@ -76,6 +76,15 @@ public class Window : NativeWindow , IWindow
last = WindowState;
WindowSizeMatrix = Matrix4.CreateOrthographicOffCenter(0.0f, Size.X, Size.Y, 0, 1, -1);
KeyDown += OnKeyDownn;
TextInput += OnTextInputEvent;
}
private void OnTextInputEvent(TextInputEventArgs obj)
{
if (focused is not null)
{
focused.SendClipEvent(obj.AsString);
}
}
private WindowState last;
@ -84,6 +93,10 @@ public class Window : NativeWindow , IWindow
public bool CustomF11 = true;
private unsafe void OnKeyDownn(KeyboardKeyEventArgs obj)
{
if (focused is not null)
{
focused.SendKeyEvent(obj);
}
if (obj.Key != Keys.F11 || WindowBorder == WindowBorder.Fixed) return;
if (CustomF11)
{
@ -136,91 +149,14 @@ public class Window : NativeWindow , IWindow
public Color4 BackgroundColor { get; set; } = new Color4(0, 0, 0, 255);
public ControlList Controls { get; } = new();
#region Cool Math Things
public float[] RctToFloat(int x, int y, int Width, int Height, bool hasTexture = false, float z = 0.0f)
{
if (hasTexture)
{
return new float[20] {
IntToFloat(x + Width), IntToFloat(y, true), z, 1.0f, 1.0f,// top r
IntToFloat(x + Width), IntToFloat(y + Height, true), z, 1.0f, 0.0f,//b r
IntToFloat(x), IntToFloat(y + Height, true), z, 0.0f, 0.0f,//bot l
IntToFloat(x), IntToFloat(y, true), z, 0.0f, 1.0f// top l
};
}
else
{
return new float[12] {
IntToFloat(x + Width), IntToFloat(y, true), z,// top r
IntToFloat(x + Width), IntToFloat(y + Height, true), z, //b r
IntToFloat(x), IntToFloat(y + Height, true), z, //bot l
IntToFloat(x), IntToFloat(y, true), z,// top l
};
}
}
public Vector3 PointToVector(float x, float y, float z = 0.0f)
{
return new Vector3(IntToFloat(x), IntToFloat(y, true), z);
}
public Vector2i CS { get; private set; } = new(0, 0);
public float IntToFloat(float p, bool Invert = false)
{
float 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;
if (Invert)
{
if (p > half) return (float)(((double)(p - half) * Per) * -1);
else return (float)(1 - (p * Per));
}
else
{
if (p > half) return (float)((double)(p - half) * Per);
else return (float)((1 - (p * Per)) * -1);
}
}
public float IntToWindow(float p, bool Y = false)
{
return p;
}
public float FloatToInt(float p, bool Invert = false)
{
float 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)
{
if (p < 0)
{
p *= -1;
p++;
return (float)(half * p);
}
else
{
return (float)(half - (p * half));
}
}
else
{
if (p < 0)
{
p *= -1;
p++;
return (float)(Size - (half * p));
}
else
{
return (float)(p * half + half);
}
}
}
#endregion
public void ForceUpdate(ResizeEventArgs e)
{
BlockDraw = true;
@ -237,7 +173,7 @@ public class Window : NativeWindow , IWindow
int ly = (top ? Controls[i].Location.Y : Size.Y - Controls[i].Distance.Y - Controls[i].Size.Y);
int sy = (bottom ? Size.Y - Controls[i].Distance.Y - ly : Controls[i].Size.Y);
int sx = (right ? Size.X - Controls[i].Distance.X - lx : Controls[i].Size.X);
if (Controls[i].Size.X != sx || Controls[i].Size.Y != sy) Controls[i].Size = new(sx, sy);
Controls[i].Size = new(sx, sy);
Controls[i].Location = new(lx, ly, Controls[i].Location.Z);
if (Controls[i] is IParent parent)
{
@ -256,7 +192,8 @@ public class Window : NativeWindow , IWindow
if (e.Width == 0 && e.Height == 0 && WindowState != WindowState.Fullscreen) return;
WindowSizeMatrix = Matrix4.CreateOrthographicOffCenter(0.0f, e.Width, e.Height, 0, 1, -1);
GL.Viewport(0, 0, e.Width, e.Height);
GL.Viewport(0, 0, e.Width, e.Height);
CS = e.Size;
ForceUpdate(e);
}
@ -442,13 +379,13 @@ public class Window : NativeWindow , IWindow
}
BlockDraw = false;
}
GL.Scissor(0,0,Size.X, Size.Y);
GL.Scissor(0,0,CS.X, CS.Y);
GL.Clear(ClearBufferMask.ColorBufferBit);
for (int i = 0; i < Controls.Length; i++)
{
if (!Controls[i].Loaded) continue;
GL.Scissor(0, 0, Size.X, Size.Y);
Controls[i].Draw(0,0,Size.X, ClientSize.Y);
GL.Scissor(0, 0, CS.X, CS.Y);
Controls[i].Draw(0,0,CS.X, CS.Y);
}
Context.SwapBuffers();