Implement INotifyPropertyChanged for brushes.
This commit is contained in:
@@ -1,27 +1,111 @@
|
|||||||
|
using System.Collections.Specialized;
|
||||||
|
using System.ComponentModel;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.Numerics;
|
using System.Runtime.CompilerServices;
|
||||||
|
|
||||||
namespace Dashboard.Drawing
|
namespace Dashboard.Drawing
|
||||||
{
|
{
|
||||||
public abstract class Brush
|
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 class SolidColorBrush(Color color) : Brush
|
public sealed class SolidColorBrush(Color color) : Brush
|
||||||
{
|
{
|
||||||
public Color Color { get; } = color;
|
public Color Color { get; } = color;
|
||||||
}
|
}
|
||||||
|
|
||||||
public class ImageBrush(Image image) : Brush
|
public class ImageBrush(Image image) : Brush
|
||||||
{
|
{
|
||||||
public Image Image { get; set; } = image;
|
public Image Image
|
||||||
public Box2d TextureCoordinates { get; set; } = new Box2d(0, 0, 1, 1);
|
{
|
||||||
|
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 class NinePatchImageBrush(Image image) : Brush
|
||||||
{
|
{
|
||||||
public Image Image { get; set; } = image;
|
public Image Image
|
||||||
public Box2d CenterCoordinates { get; set; } = new Box2d(0, 0, 1, 1);
|
{
|
||||||
public Vector4 Extents { get; set; } = Vector4.Zero;
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user