JacobTech cd290e6fd0 New function for TabControl
A TabPage can also be a FlowLayout
2023-01-06 11:25:58 -05:00

313 lines
9.8 KiB
C#

using GraphicsManager.Enums;
using GraphicsManager.Interfaces;
using GraphicsManager.Objects.Core;
using OpenTK.Mathematics;
using OpenTK.Windowing.Common;
using SixLabors.ImageSharp.Processing;
namespace GraphicsManager.Objects;
public class TabControl : IRenderObject, IParent
{
public Rectangle _bounds;
public TabControl()
{
_bounds = new();
}
private ControlList Buttonts = new();
private ControlList Controls = new();
public void AddPage(string Title, UserControl page)
{
RoundedButton tmp;
uint loc = (uint)Buttonts.Length;
int locc = 0, loccc = 0;
if (loc != 0)
{
locc = Buttonts[loc - 1].Location.X;
loccc = Buttonts[loc - 1].Size.X;
}
Buttonts.Add(tmp = new RoundedButton()
{
Location = new( locc+ loccc + (TabSpace * (int)loc) + Border, Border),
Font = TitleFont,
Text = Title,
Tag = loc,
BorderColor = this.BorderColor,
InsideColor = this.InsideColor,
FontColor = this.TextColor
});
if (loc == PageIndex) tmp.BorderColor = this.SelectedColor;
tmp.Clicked += TmpOnClicked;
tmp.Size = new(tmp._label.Size.X + ((TextBorder + 4) * 2), tmp._label.Size.Y + ((TextBorder + 4) * 2));
page.Anchor = ObjectAnchor.All;
page.Location = new(Border, tmp.Location.Y + tmp.Size.Y + TabSpace);
page.Size = new(Size.X - Border - Border, Size.Y - page.Location.Y - Border);
if (PageIndex != loc) page.Visible = false;
Controls.Add(page);
}
public void AddPage(string Title, FlowLayout page)
{
RoundedButton tmp;
uint loc = (uint)Buttonts.Length;
int locc = 0, loccc = 0;
if (loc != 0)
{
locc = Buttonts[loc - 1].Location.X;
loccc = Buttonts[loc - 1].Size.X;
}
Buttonts.Add(tmp = new RoundedButton()
{
Location = new( locc+ loccc + (TabSpace * (int)loc) + Border, Border),
Font = TitleFont,
Text = Title,
Tag = loc,
BorderColor = this.BorderColor,
InsideColor = this.InsideColor,
FontColor = this.TextColor
});
if (loc == PageIndex) tmp.BorderColor = this.SelectedColor;
tmp.Clicked += TmpOnClicked;
tmp.Size = new(tmp._label.Size.X + ((TextBorder + 4) * 2), tmp._label.Size.Y + ((TextBorder + 4) * 2));
page.Anchor = ObjectAnchor.All;
page.Location = new(Border, tmp.Location.Y + tmp.Size.Y + TabSpace);
page.Size = new(Size.X - Border - Border, Size.Y - page.Location.Y - Border);
if (PageIndex != loc) page.Visible = false;
Controls.Add(page);
}
private Task TmpOnClicked(IRenderObject arg)
{
PageIndex = (uint)arg.Tag!;
return Task.CompletedTask;
}
public Color4 TextColor { get; set; } = new(255, 255, 255, 255);
public Color4 BorderColor { get; set; } = new(40, 40, 40, 255);
public Color4 SelectedColor { get; set; } = Color4.DarkCyan;
public Color4 InsideColor { get; set; } = new(0, 0, 0, 255);
public Color4 BackgroundColor { get => _bounds.BackgroundColor; set => _bounds.BackgroundColor = value; }
public int TextBorder { get; set; } = 5;
public int TabSpace { get; set; } = 5;
public int Border { get; set; } = 10;
public IParent? Parent { get; private set; } = null;
public Font TitleFont { get; set; } = Label.DefaultFont;
public Window? Window { get; private set; } = null;
public bool Visible { get => _bounds.Visible; set => _bounds.Visible = value; }
public ObjectAnchor Anchor { get => _bounds.Anchor; set => _bounds.Anchor = value; }
public object? Tag { get => _bounds.Tag; set => _bounds.Tag = value; }
public Vector2i Size { get => _bounds.Size; set => _bounds.Size = value; }
public Vector2i Location { get => _bounds.Location; set => _bounds.Location = value; }
public ContextMenu? ContextMenu { get => _bounds.ContextMenu; set => _bounds.ContextMenu = value; }
public Vector2i Distance { get => _bounds.Distance; }
public Vector2 LocationAsFloat { get => _bounds.LocationAsFloat; }
public Vector2 SizeAsFloat { get => _bounds.SizeAsFloat; }
private uint pgi = 0;
public uint PageIndex
{
get
{
return pgi;
}
set
{
pgi = value;
res = true;
for (int i = 0; i < Buttonts.Length; i++)
((RoundedButton)Buttonts[i]).BorderColor = BorderColor;
((RoundedButton)Buttonts[value]).BorderColor = SelectedColor;
if (Parent is not null) Parent.TryDraw();
}
}
public Vector2i Position => Location;
public bool Loaded { get; private set; } = false;
public void TryDraw()
{
if (!res) Parent!.TryDraw();
}
public void ReportSizeUpdate(IRenderObject Control)
{
}
public void ParentResize(ResizeEventArgs e)
{
res = true;
if (e.Width == 0 && e.Height == 0) return;
if (Controls.Length >= PageIndex) return;
if (!Controls[PageIndex].Loaded) return;
bool top = (Controls[PageIndex].Anchor & ObjectAnchor.Top) == ObjectAnchor.Top;
bool left = (Controls[PageIndex].Anchor & ObjectAnchor.Left) == ObjectAnchor.Left;
bool right = (Controls[PageIndex].Anchor & ObjectAnchor.Right) == ObjectAnchor.Right;
bool bottom = (Controls[PageIndex].Anchor & ObjectAnchor.Bottom) == ObjectAnchor.Bottom;
if (!top && !bottom) { Controls[PageIndex].Anchor |= ObjectAnchor.Top; top = true; }
if (!left && !right) { Controls[PageIndex].Anchor |= ObjectAnchor.Left; left = true; }
int lx = (left ? Controls[PageIndex].Location.X : Size.X - Controls[PageIndex].Distance.X - Controls[PageIndex].Size.X);
int ly = (top ? Controls[PageIndex].Location.Y : Size.Y - Controls[PageIndex].Distance.Y - Controls[PageIndex].Size.Y);
int sy = (bottom ? Size.Y - Controls[PageIndex].Distance.Y - ly : Controls[PageIndex].Size.Y);
int sx = (right ? Size.X - Controls[PageIndex].Distance.X - lx : Controls[PageIndex].Size.X);
Controls[PageIndex].Size = new(sx, sy);
Controls[PageIndex].Location = new(lx, ly);
if (Controls[PageIndex] is IParent parent)
{
parent.ParentResize(e);
}
Parent!.TryDraw();
res = false;
}
private bool res = false;
public void LoadToParent(IParent Parent, Window Window)
{
if (Loaded) return;
this.Parent = Parent;
this.Window = Window;
res = true;
Loaded = true;
_bounds.LoadToParent(Parent, Window);
for (int i = 0; i < Controls.Length; i++)
{
Controls[i].LoadToParent(this, Window);
}
for (int i = 0; i < Buttonts.Length; i++)
{
Buttonts[i].LoadToParent(this, Window);
}
res = false;
if (WindowLoaded is not null) WindowLoaded.Invoke(this);
}
public void Draw()
{
if (Loaded && Visible)
{
_bounds.Draw();
if (!(Controls.Length >= (PageIndex))) return;
if (!Controls[PageIndex].Loaded) return;
Controls[PageIndex].Draw();
for (int i = 0; i < Buttonts.Length; i++)
Buttonts[i].Draw();
}
}
public void Clean()
{
for (int i = 0; i < Controls.Length; i++)
{
Controls[i].Clean();
}
for (int i = 0; i < Buttonts.Length; i++)
{
Buttonts[i].Clean();
}
_bounds.Clean();
}
public event Func<IRenderObject, Task>? Clicked;
public event Func<IRenderObject, Task>? MouseLeave;
public event Func<IRenderObject, Task>? MouseEnter;
public event Func<IRenderObject, Task>? WindowLoaded;
#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 float IntToFloat(float p, bool Invert = false)
{
p += (Invert ? Location.Y : Location.X);
IParent? tempp = Parent;
while (tempp is not null)
{
p += (Invert ? tempp.Position.Y : tempp.Position.X);
tempp = tempp.Parent;
}
float Size = (Invert ? Window!.Size.Y : Window!.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 FloatToInt(float p, bool Invert = false)
{
p += (Invert ? LocationAsFloat.Y : LocationAsFloat.X);
IParent? tempp = Parent;
while (tempp is not null)
{
p += (Invert ? tempp.LocationAsFloat.Y : tempp.LocationAsFloat.X);
tempp = tempp.Parent;
}
float Size = (Invert ? Window!.Size.Y : Window!.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
}