2023-10-16 22:38:57 -04:00

304 lines
8.5 KiB
C#
Executable File

using GraphicsManager.Enums;
using GraphicsManager.Interfaces;
using GraphicsManager.Objects.Core;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Mathematics;
using OpenTK.Windowing.Common;
using OpenTK.Windowing.Common.Input;
using OpenTK.Windowing.GraphicsLibraryFramework;
namespace GraphicsManager.Objects;
public class TexturedTextBox : Rectangle
{
private Label _label;
private Label _watermark;
public ContextMenu? ContextMenu { get; set; }
public TextLocation TextLocation { get; set; } = TextLocation.TopLeft;
public TexturedTextBox(Texture texture, FontFamily LabelFam, FontFamily WaterFam)
:base(texture)
{
base.HoverMouse = MouseCursor.IBeam;
_label = new Label(LabelFam)
{
HoverMouse = MouseCursor.IBeam
};
TextureDisplay = TextureDisplay.HorizontalCenter;
_watermark = new(WaterFam)
{
Color = new(128, 128, 128, 255),
HoverMouse = MouseCursor.IBeam
};
}
public override MouseCursor HoverMouse
{
get => base.HoverMouse;
set
{
_watermark.HoverMouse = value;
_label.HoverMouse = value;
base.HoverMouse = value;
}
}
public int Border { get; set; } = 2;
public TexturedTextBox(Texture texture, FontInteraction LabelFam, FontInteraction WaterFam)
:base(texture)
{
base.HoverMouse = MouseCursor.IBeam;
_label = new Label(LabelFam)
{
HoverMouse = MouseCursor.IBeam
};
TextureDisplay = TextureDisplay.HorizontalCenter;
_watermark = new(WaterFam)
{
Color = new(128, 128, 128, 255),
HoverMouse = MouseCursor.IBeam
};
}
public FontInteraction Font { get => _label.Font; }
public string Text
{
get => _label.Text;
set
{
int old = _label.TrueHeight;
_label.Text = value;
if (!string.IsNullOrEmpty(value))
{
bool f = false;
if (!_label.Visible)
{
f = true;
_label.Visible = true;
_label.Location = TextLocation switch
{
TextLocation.TrueCenterLeft => new(Location.X + Border + 5, Location.Y + ((Size.Y - _label.TrueHeight) / 2) - (_label.Size.Y - _label.TrueHeight), Location.Z),
TextLocation.PostiveTureCenterLeft => new(Location.X + Border + 5, Location.Y + ((Size.Y - _label.PostiveTrueHeight) / 2) - _label.Size.Y + _label.TrueHeight, Location.Z),
TextLocation.PxLeft => new(Location.X + Border + 5, Location.Y + ((Size.Y - _label.Size.Y) / 2), Location.Z),
_ => new(Location.X + Border + 5, Location.Y + Border + 5, Location.Z)
};
_watermark.Location = _label.Location;
}
if (_watermark.Visible) _watermark.Visible = false;
if (!f && TextLocation == TextLocation.TrueCenterLeft && old != _label.TrueHeight)
{
_label.Location = new(Location.X + Border + 5, Location.Y + ((Size.Y - _label.TrueHeight) / 2) - (_label.Size.Y - _label.TrueHeight), Location.Z);
_watermark.Location = _label.Location;
}
}
else
{
if (_label.Visible) _label.Visible = false;
if (!_watermark.Visible)
{
_watermark.Visible = true;
_watermark.Location = TextLocation switch
{
TextLocation.TrueCenterLeft => new(Location.X + Border + 5, Location.Y + ((Size.Y - _watermark.TrueHeight) / 2) - (_watermark.Size.Y - _watermark.TrueHeight), Location.Z),
TextLocation.PostiveTureCenterLeft => new(Location.X + Border + 5, Location.Y + ((Size.Y - _label.PostiveTrueHeight) / 2) - _label.Size.Y + _label.TrueHeight, Location.Z),
TextLocation.PxLeft => new(Location.X + Border + 5, Location.Y + ((Size.Y - _watermark.Size.Y) / 2), Location.Z),
_ => new(Location.X + Border + 5, Location.Y + Border + 5, Location.Z)
};
_label.Location = _label.Location;
}
}
}
}
public FontInteraction WatermarkFont { get => _watermark.Font; }
public string WatermarkText
{
get
{
return _watermark.Text;
}
set
{
_watermark.Text = value;
}
}
public char? PasswordChar { get => _label.PasswordChar; set => _label.PasswordChar = value; }
public override Vector2i Size
{
get
{
return base.Size;
}
set
{
if (base.Size == value) return;
base.Size = value;
}
}
public override Vector3i Location {
get => base.Location;
set
{
Vector3i diff = base.Location - value;
base.Location = value;
if (_watermark.Visible && !string.IsNullOrEmpty(_watermark.Text))
{
_watermark.Location = TextLocation switch
{
TextLocation.TrueCenterLeft => new(value.X + Border + 5, value.Y + ((Size.Y - _watermark.TrueHeight) / 2) - (_watermark.Size.Y - _watermark.TrueHeight), Location.Z),
TextLocation.PostiveTureCenterLeft => new(value.X + Border + 5, value.Y + ((Size.Y - _label.PostiveTrueHeight) / 2) - _label.Size.Y + _label.TrueHeight, Location.Z),
TextLocation.PxLeft => new(value.X + Border + 5, value.Y + ((Size.Y - _watermark.Size.Y) / 2), Location.Z),
_ => new(value.X + Border + 5, value.Y + Border + 5, Location.Z)
};
_label.Location = _watermark.Location;
}
else
{
_label.Location = TextLocation switch
{
TextLocation.TrueCenterLeft => new(value.X + Border + 5, value.Y + ((Size.Y - _label.TrueHeight) / 2) - (_label.Size.Y - _label.TrueHeight), Location.Z),
TextLocation.PostiveTureCenterLeft => new(value.X + Border + 5, value.Y + ((Size.Y - _label.PostiveTrueHeight) / 2) - _label.Size.Y + _label.TrueHeight, Location.Z),
TextLocation.PxLeft => new(value.X + Border + 5, value.Y + ((Size.Y - _label.Size.Y) / 2), Location.Z),
_ => new(value.X + Border + 5, value.Y + Border + 5, Location.Z)
};
_watermark.Location = _label.Location;
}
}
}
public Color4 TextColor { get => _label.Color; set => _label.Color = value; }
public Color4 WatermarkColor { get => _watermark.Color; set => _watermark.Color = value; }
public override bool Visible
{
get => base.Visible;
set
{
if (value == base.Visible) return;
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;
}
base.Visible = value;
}
}
public override void Clean()
{
_label.Clean();
_watermark.Clean();
base.Clean();
}
public override void Draw(int x, int y, int w, int h)
{
if (!Visible || !Loaded) return;
int nx = x, ny = y, nw = w, nh = h;
if (Location.X + Border > nw)
return;
else
{
nx += (Location.X + Border);
nw -= (Location.X + Border);
if (Size.X - Border < nw)
nw = Size.X - Border;
}
if (Location.Y + Border > nh)
return;
else
{
ny += (Location.Y + Border);
nh -= (Location.Y + Border);
if (Size.Y - Border < nh)
nh = Size.Y - Border;
}
if (nh < 1 || nw < 1) return;
GL.Scissor(nx,ny,nw,nh);
base.Draw(nx,ny,nw,nh);
if (!string.IsNullOrEmpty(_label.Text)) _label.Draw(nx,ny,nw,nh);
else _watermark.Draw(nx,ny,nw,nh);
}
public override void LoadToParent(IParent parent, IWindow window)
{
if (Loaded) return;
window.MouseDown += Window_MouseDown;
window.KeyDown += Window_KeyDown;
window.TextInput += WindowOnTextInput;
if (!window.Context.IsCurrent) window.Context.MakeCurrent();
_label.LoadToParent(parent, window);
_watermark.LoadToParent(parent, window);
base.LoadToParent(parent, window);
}
private void WindowOnTextInput(TextInputEventArgs obj)
{
if (!use) return;
Text += obj.AsString;
}
private bool use;
public event Func<KeyboardKeyEventArgs, Task>? KeyPress;
public override void UnFocus()
{
use = false;
if (Window is not null && Window.focused == this)
Window.focused = null;
}
public override 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(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(MouseButtonEventArgs e)
{
if (MouseInside && e.Button == MouseButton.Button1)
{
use = true;
Focus();
}
else use = false;
}
}