2023-01-05 14:01:32 -05:00

148 lines
4.9 KiB
C#
Executable File

using GraphicsManager.Enums;
using GraphicsManager.Interfaces;
using GraphicsManager.Objects.Core;
using OpenTK.Mathematics;
using OpenTK.Windowing.Common;
using OpenTK.Windowing.GraphicsLibraryFramework;
namespace GraphicsManager.Objects;
public class Textbox : IRenderObject
{
private RoundedRectangle _bounds, _inside;
private Label _label;
public Textbox()
{
_bounds = new RoundedRectangle();
_inside = new RoundedRectangle();
_label = new Label();
_bounds.MouseEnter += BoundsOnMouseEnter;
_bounds.MouseLeave += BoundsOnMouseLeave;
}
public event Func<IRenderObject, Task>? WindowLoaded;
public event Func<IRenderObject, Task>? MouseEnter;
public event Func<IRenderObject, Task>? MouseLeave;
public object? Tag { get; set; } = null;
public int Radius { get => _bounds.Radius; set { _bounds.Radius = value; _inside.Radius = value; } }
public int Border { get; set; } = 2;
public int Smoothness { get => _bounds.Smoothness; set { _bounds.Smoothness = value; _inside.Smoothness = value; } }
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
{
get
{
return _bounds.Size;
}
set
{
_bounds.Size = value;
_inside.Size = new(value.X - (Border * 2), value.Y - (Border * 2));
}
}
public Vector2i Location {
get => _bounds.Location;
set
{
_bounds.Location = value;
_label.Location = new(value.X + Border + 5, Location.Y + Border + (((Size.Y - (Radius * 2)) / 2) - (_label.Size.Y / 2)));
_inside.Location = new(value.X + Border, value.Y + Border);
}
}
public Vector2 SizeAsFloat { get => _bounds.SizeAsFloat; }
public Vector2 LocationAsFloat { get => _bounds.LocationAsFloat; }
public Vector2i Distance { get => _bounds.Distance; }
public IParent? Parent { get; private set; } = null;
public Window? Window { get; private set; } = null;
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()
{
_bounds.Clean();
_inside.Clean();
_label.Clean();
}
public void Draw()
{
if (!Visible || !Loaded) return;
_bounds.Draw();
_inside.Draw();
_label.Draw();
}
public void LoadToParent(IParent Parent, Window Window)
{
if (Loaded) return;
this.Parent = Parent;
this.Window = Window;
this.Window.MouseDown += Window_MouseDown;
this.Window.KeyDown += Window_KeyDown;
this.Window.TextInput += WindowOnTextInput;
Loaded = true;
_bounds.LoadToParent(Parent, Window);
_inside.LoadToParent(Parent, Window);
_label.LoadToParent(Parent, Window);
Location = Location;
if (WindowLoaded is not null) WindowLoaded.Invoke(this);
}
private void WindowOnTextInput(TextInputEventArgs obj)
{
if (!use) return;
Text += obj.AsString;
}
private bool use = false;
public event Func<KeyboardKeyEventArgs, Task>? KeyPress;
private void Window_KeyDown(OpenTK.Windowing.Common.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 (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) <= 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);
}
else use = false;
}
}