1
0
mirror of https://github.com/DarthAffe/RGB.NET.git synced 2025-12-13 01:58:30 +00:00

Added change-detection to gradients

This commit is contained in:
Darth Affe 2018-02-03 21:02:36 +01:00
parent 04735cc37d
commit 9563198419
3 changed files with 62 additions and 2 deletions

View File

@ -2,7 +2,10 @@
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using RGB.NET.Core;
@ -36,13 +39,23 @@ namespace RGB.NET.Brushes.Gradients
#endregion
#region Events
/// <inheritdoc />
public event EventHandler GradientChanged;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="AbstractGradient"/> class.
/// </summary>
protected AbstractGradient()
{ }
{
GradientStops.CollectionChanged += GradientCollectionChanged;
PropertyChanged += (sender, args) => OnGradientChanged();
}
/// <summary>
/// Initializes a new instance of the <see cref="AbstractGradient"/> class.
@ -50,6 +63,9 @@ namespace RGB.NET.Brushes.Gradients
/// <param name="gradientStops">The stops with which the gradient should be initialized.</param>
protected AbstractGradient(params GradientStop[] gradientStops)
{
GradientStops.CollectionChanged += GradientCollectionChanged;
PropertyChanged += (sender, args) => OnGradientChanged();
foreach (GradientStop gradientStop in gradientStops)
GradientStops.Add(gradientStop);
}
@ -63,6 +79,9 @@ namespace RGB.NET.Brushes.Gradients
{
this.WrapGradient = wrapGradient;
GradientStops.CollectionChanged += GradientCollectionChanged;
PropertyChanged += (sender, args) => OnGradientChanged();
foreach (GradientStop gradientStop in gradientStops)
GradientStops.Add(gradientStop);
}
@ -106,6 +125,26 @@ namespace RGB.NET.Brushes.Gradients
gradientStop.Offset += 1;
}
/// <summary>
/// Should be called to indicate that the gradient was changed.
/// </summary>
protected void OnGradientChanged() => GradientChanged?.Invoke(this, null);
private void GradientCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.OldItems != null)
foreach (GradientStop gradientStop in e.OldItems)
gradientStop.PropertyChanged -= GradientStopChanged;
if (e.NewItems != null)
foreach (GradientStop gradientStop in e.NewItems)
gradientStop.PropertyChanged += GradientStopChanged;
OnGradientChanged();
}
private void GradientStopChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs) => OnGradientChanged();
#endregion
}
}

View File

@ -1,4 +1,5 @@
using RGB.NET.Core;
using System;
using RGB.NET.Core;
namespace RGB.NET.Brushes.Gradients
{
@ -8,6 +9,11 @@ namespace RGB.NET.Brushes.Gradients
/// </summary>
public interface IGradient : IDecoratable<IGradientDecorator>
{
/// <summary>
/// Occurs if the <see cref="IGradient"/> is changed.
/// </summary>
event EventHandler GradientChanged;
/// <summary>
/// Gets the <see cref="Color"/> of the <see cref="IGradient"/> on the specified offset.
/// </summary>

View File

@ -1,6 +1,7 @@
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
using System;
using RGB.NET.Core;
namespace RGB.NET.Brushes.Gradients
@ -37,6 +38,13 @@ namespace RGB.NET.Brushes.Gradients
#endregion
#region Events
/// <inheritdoc />
public event EventHandler GradientChanged;
#endregion
#region Constructors
/// <summary>
@ -48,6 +56,8 @@ namespace RGB.NET.Brushes.Gradients
{
this.StartHue = startHue;
this.EndHue = endHue;
PropertyChanged += (sender, args) => OnGradientChanged();
}
#endregion
@ -90,6 +100,11 @@ namespace RGB.NET.Brushes.Gradients
}
}
/// <summary>
/// Should be called to indicate that the gradient was changed.
/// </summary>
protected void OnGradientChanged() => GradientChanged?.Invoke(this, null);
#endregion
}
}