Files
Dashboard/Dashboard.Common/Drawing/Brush.cs
T

112 lines
2.8 KiB
C#

using System.Collections.Specialized;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.CompilerServices;
namespace Dashboard.Drawing
{
public abstract class Brush : INotifyPropertyChanged, ICloneable
{
public event PropertyChangedEventHandler? PropertyChanged;
public virtual object Clone()
{
return MemberwiseClone();
}
protected void SetField<T>(ref T field, T value, [CallerMemberName] string property = "")
{
field = value;
OnPropertyChanged(property);
}
protected virtual void OnPropertyChanged(string property)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
}
public sealed class SolidColorBrush(Color color) : Brush
{
public Color Color { get; } = color;
}
public class ImageBrush(Image image) : Brush
{
public Image Image
{
get;
set => SetField(ref field, value);
} = image;
public Box2d TextureCoordinates
{
get;
set => SetField(ref field, value);
} = new Box2d(0, 0, 1, 1);
}
public class NinePatchImageBrush(Image image) : Brush
{
public Image Image
{
get;
set => SetField(ref field, value);
} = image;
public Box2d CenterCoordinates
{
get;
set => SetField(ref field, value);
} = new Box2d(0, 0, 1, 1);
public Extents Extents
{
get;
set => SetField(ref field, value);
} = Extents.None;
}
public class GradientBrush : Brush
{
public Gradient Gradient
{
get;
set
{
Unsubscribe(field);
Subscribe(value);
SetField(ref field, value);
}
}
public GradientBrush(Gradient gradient)
{
Gradient = gradient;
}
private void Subscribe(Gradient gradient)
{
gradient.PropertyChanged += GradientPropertyChanged;
gradient.CollectionChanged += GradientCollectionChanged;
}
private void Unsubscribe(Gradient? gradient)
{
gradient?.PropertyChanged -= GradientPropertyChanged;
gradient?.CollectionChanged -= GradientCollectionChanged;
}
private void GradientCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
OnPropertyChanged(nameof(Gradient));
}
private void GradientPropertyChanged(object? sender, PropertyChangedEventArgs e)
{
OnPropertyChanged(nameof(Gradient));
}
}
}