240 lines
6.7 KiB
C#
Executable File
240 lines
6.7 KiB
C#
Executable File
using System.Timers;
|
|
using GraphicsManager.Enums;
|
|
using GraphicsManager.Interfaces;
|
|
using GraphicsManager.Objects.Core;
|
|
using OpenTK.Graphics.GL;
|
|
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;
|
|
private Label _watermark;
|
|
public ContextMenu? ContextMenu { get => _bounds.ContextMenu; set => _bounds.ContextMenu = value; }
|
|
public Textbox()
|
|
{
|
|
_bounds = new RoundedRectangle();
|
|
_inside = new RoundedRectangle();
|
|
_label = new Label();
|
|
_watermark = new()
|
|
{
|
|
Color = new(128, 128, 128, 255)
|
|
};
|
|
|
|
_bounds.MouseEnter += BoundsOnMouseEnter;
|
|
_bounds.MouseLeave += BoundsOnMouseLeave;
|
|
}
|
|
|
|
private int currentc = 0;
|
|
|
|
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
|
|
{
|
|
if (!string.IsNullOrEmpty(value))
|
|
{
|
|
if (!_label.Visible) _label.Visible = true;
|
|
if (_watermark.Visible) _watermark.Visible = false;
|
|
}
|
|
else
|
|
{
|
|
if (_label.Visible) _label.Visible = false;
|
|
if (!_watermark.Visible) _watermark.Visible = true;
|
|
}
|
|
_label.Text = value;
|
|
}
|
|
}
|
|
|
|
public Font WatermarkFont { get => _watermark!.Font; set => _watermark.Font = value; }
|
|
public string WatermarkText
|
|
{
|
|
get
|
|
{
|
|
return _watermark.Text;
|
|
}
|
|
set
|
|
{
|
|
_watermark.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 + Border + (((Size.Y - (Radius * 2)) / 2) - (_label.Size.Y / 2)));
|
|
_watermark.Location = _label.Location;
|
|
_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; internal set => _bounds.Distance = value; }
|
|
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 Color4 TextColor { get => _label.Color; set => _label.Color = value; }
|
|
public Color4 WatermarkColor { get => _watermark.Color; set => _watermark.Color = value; }
|
|
|
|
public bool Visible
|
|
{
|
|
get => _bounds.Visible;
|
|
set
|
|
{
|
|
_bounds.Visible = value;
|
|
_inside.Visible = value;
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
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();
|
|
_watermark.Clean();
|
|
}
|
|
|
|
public void Draw()
|
|
{
|
|
if (!Visible || !Loaded) return;
|
|
_bounds.Draw();
|
|
_inside.Draw();
|
|
if (!string.IsNullOrEmpty(_label.Text)) _label.Draw();
|
|
else _watermark.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);
|
|
_watermark.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;
|
|
|
|
public void UnFocus()
|
|
{
|
|
use = false;
|
|
if (Window is not null && Window.focused == this)
|
|
Window.focused = null;
|
|
}
|
|
public 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(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 (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(OpenTK.Windowing.Common.MouseButtonEventArgs e)
|
|
{
|
|
if (Visible &&
|
|
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;
|
|
Focus();
|
|
if (Clicked is not null) Clicked.Invoke(this);
|
|
}
|
|
else use = false;
|
|
}
|
|
}
|