Dashboard/Quik/Controls/Control.cs

110 lines
3.0 KiB
C#
Raw Normal View History

2024-05-16 21:58:22 +02:00
using System;
using Quik.CommandMachine;
namespace Quik.Controls
{
public abstract class Control : UIBase
{
private readonly CommandList drawCommands = new CommandList();
public Style Style { get; set; } = new Style();
public float Padding
{
get => (float)(Style["padding"] ?? 0.0f);
set => Style["padding"] = value;
}
2024-06-09 21:06:13 +02:00
public bool IsVisualsValid { get; private set; } = false;
public bool IsLayoutValid { get; private set; } = false;
protected bool IsLayoutSuspended { get; private set; } = false;
2024-05-16 21:58:22 +02:00
public void InvalidateVisual()
{
IsVisualsValid = false;
OnVisualsInvalidated(this, EventArgs.Empty);
}
2024-06-09 21:06:13 +02:00
public void InvalidateLayout()
{
IsLayoutValid = false;
OnLayoutInvalidated(this, EventArgs.Empty);
}
public void SuspendLayout()
{
IsLayoutSuspended = true;
}
public void ResumeLayout()
{
IsLayoutSuspended = false;
InvalidateLayout();
}
2024-05-16 21:58:22 +02:00
protected abstract void ValidateVisual(CommandList cmd);
2024-06-09 21:06:13 +02:00
protected abstract void ValidateLayout();
2024-05-16 21:58:22 +02:00
protected override void PaintBegin(CommandList cmd)
{
base.PaintBegin(cmd);
2024-06-09 21:06:13 +02:00
if (!IsLayoutValid && !IsLayoutSuspended)
{
ValidateLayout();
OnLayoutValidated(this, EventArgs.Empty);
IsLayoutValid = true;
InvalidateVisual();
}
2024-05-16 21:58:22 +02:00
if (!IsVisualsValid)
{
ValidateVisual(drawCommands);
OnVisualsValidated(this, EventArgs.Empty);
IsVisualsValid = true;
}
cmd.PushStyle(Style);
cmd.PushViewport();
cmd.StoreViewport(AbsoluteBounds);
cmd.Splice(drawCommands);
cmd.PopViewport();
cmd.PopStyle();
}
public event EventHandler StyleChanged;
public event EventHandler VisualsInvalidated;
public event EventHandler VisualsValidated;
2024-06-09 21:06:13 +02:00
public event EventHandler LayoutInvalidated;
public event EventHandler LayoutValidated;
2024-05-16 21:58:22 +02:00
protected virtual void OnStyleChanged(object sender, EventArgs ea)
{
StyleChanged?.Invoke(sender, ea);
2024-06-09 21:06:13 +02:00
InvalidateLayout();
2024-05-16 21:58:22 +02:00
}
protected virtual void OnVisualsInvalidated(object sender, EventArgs ea)
{
VisualsInvalidated?.Invoke(sender, ea);
}
protected virtual void OnVisualsValidated(object sender, EventArgs ea)
{
VisualsValidated?.Invoke(sender, ea);
}
2024-06-09 21:06:13 +02:00
protected virtual void OnLayoutInvalidated(object sender, EventArgs ea)
{
LayoutInvalidated?.Invoke(sender, ea);
}
protected virtual void OnLayoutValidated(object sender, EventArgs ea)
{
LayoutValidated?.Invoke(sender, ea);
}
2024-05-16 21:58:22 +02:00
}
}