102 lines
2.4 KiB
C#
102 lines
2.4 KiB
C#
|
|
using System;
|
|
using Quik.CommandMachine;
|
|
|
|
namespace Quik.Controls
|
|
{
|
|
/// <summary>
|
|
/// Bases for all UI elements.
|
|
/// </summary>
|
|
public abstract class UIBase
|
|
{
|
|
private QVec2 size;
|
|
|
|
public UIBase? Parent { get; protected set; }
|
|
public string? Id { get; set; }
|
|
public QRectangle Bounds
|
|
{
|
|
get => new QRectangle(Position + Size, Position);
|
|
set
|
|
{
|
|
Size = value.Size;
|
|
Position = value.Min;
|
|
}
|
|
}
|
|
|
|
public QVec2 Position { get; set; }
|
|
|
|
public QVec2 Size
|
|
{
|
|
get => size;
|
|
set
|
|
{
|
|
QVec2 oldSize = size;
|
|
size = value;
|
|
|
|
OnResized(this, new ResizedEventArgs(size, oldSize));
|
|
}
|
|
}
|
|
|
|
public QRectangle AbsoluteBounds
|
|
{
|
|
get
|
|
{
|
|
if (Parent == null)
|
|
{
|
|
return Bounds;
|
|
}
|
|
else
|
|
{
|
|
return new QRectangle(Bounds.Max + Parent.Position, Bounds.Min + Parent.Position);
|
|
}
|
|
}
|
|
}
|
|
|
|
public QVec2 MaximumSize { get; set; } = new QVec2(-1, -1);
|
|
public QVec2 MinimumSize { get; set; } = new QVec2(-1, -1);
|
|
|
|
public bool IsMaximumSizeSet => MaximumSize != new QVec2(-1, -1);
|
|
public bool IsMinimumSizeSet => MinimumSize != new QVec2(-1, -1);
|
|
|
|
public virtual void NotifyEvent(object? sender, EventArgs args)
|
|
{
|
|
}
|
|
|
|
protected virtual void PaintBegin(CommandList cmd)
|
|
{
|
|
cmd.PushViewport();
|
|
cmd.StoreViewport(AbsoluteBounds);
|
|
cmd.PushZ();
|
|
}
|
|
|
|
protected virtual void PaintEnd(CommandList cmd)
|
|
{
|
|
cmd.PopViewport();
|
|
}
|
|
|
|
public void Paint(CommandList cmd)
|
|
{
|
|
PaintBegin(cmd);
|
|
PaintEnd(cmd);
|
|
}
|
|
|
|
public event EventHandler<ResizedEventArgs>? Resized;
|
|
|
|
public virtual void OnResized(object sender, ResizedEventArgs ea)
|
|
{
|
|
Resized?.Invoke(sender, ea);
|
|
}
|
|
}
|
|
|
|
public class ResizedEventArgs : EventArgs
|
|
{
|
|
public QVec2 NewSize { get; }
|
|
public QVec2 OldSize { get; }
|
|
|
|
public ResizedEventArgs(QVec2 newSize, QVec2 oldSize)
|
|
{
|
|
NewSize = newSize;
|
|
OldSize = oldSize;
|
|
}
|
|
}
|
|
} |