mirror of
https://github.com/DarthAffe/RGB.NET.git
synced 2025-12-12 17:48:31 +00:00
Improved custom update data to be easier to use
This commit is contained in:
parent
dc34535cea
commit
9a5fe75b60
43
RGB.NET.Core/Extensions/CustomUpdateDataExtension.cs
Normal file
43
RGB.NET.Core/Extensions/CustomUpdateDataExtension.cs
Normal file
@ -0,0 +1,43 @@
|
||||
namespace RGB.NET.Core;
|
||||
|
||||
/// <summary>
|
||||
/// Offers some extensions for easier use of <see cref="CustomUpdateData"/>.
|
||||
/// </summary>
|
||||
public static class CustomUpdateDataExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// Sets the <see cref="CustomUpdateDataIndex.FLUSH_LEDS"/>-Parameter to the given value.
|
||||
/// </summary>
|
||||
/// <param name="customUpdateData">The update-data to modify.</param>
|
||||
/// <param name="value">The value to set.</param>
|
||||
/// <returns>The modified update-data.</returns>
|
||||
public static CustomUpdateData FlushLeds(this CustomUpdateData customUpdateData, bool value = true)
|
||||
{
|
||||
customUpdateData[CustomUpdateDataIndex.FLUSH_LEDS] = value;
|
||||
return customUpdateData;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the <see cref="CustomUpdateDataIndex.RENDER"/>-Parameter to the given value.
|
||||
/// </summary>
|
||||
/// <param name="customUpdateData">The update-data to modify.</param>
|
||||
/// <param name="value">The value to set.</param>
|
||||
/// <returns>The modified update-data.</returns>
|
||||
public static CustomUpdateData Render(this CustomUpdateData customUpdateData, bool value = true)
|
||||
{
|
||||
customUpdateData[CustomUpdateDataIndex.RENDER] = value;
|
||||
return customUpdateData;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the <see cref="CustomUpdateDataIndex.UPDATE_DEVICES"/>-Parameter to the given value.
|
||||
/// </summary>
|
||||
/// <param name="customUpdateData">The update-data to modify.</param>
|
||||
/// <param name="value">The value to set.</param>
|
||||
/// <returns>The modified update-data.</returns>
|
||||
public static CustomUpdateData UpdateDevices(this CustomUpdateData customUpdateData, bool value = true)
|
||||
{
|
||||
customUpdateData[CustomUpdateDataIndex.UPDATE_DEVICES] = value;
|
||||
return customUpdateData;
|
||||
}
|
||||
}
|
||||
@ -132,7 +132,7 @@ public sealed class RGBSurface : AbstractBindable, IDisposable
|
||||
/// Perform a full update for all devices. Updates only dirty <see cref="Led"/> by default, or all <see cref="Led"/>, if flushLeds is set to true.
|
||||
/// </summary>
|
||||
/// <param name="flushLeds">Specifies whether all <see cref="Led"/>, (including clean ones) should be updated.</param>
|
||||
public void Update(bool flushLeds = false) => Update(null, new CustomUpdateData(("flushLeds", flushLeds)));
|
||||
public void Update(bool flushLeds = false) => Update(null, new CustomUpdateData((CustomUpdateDataIndex.FLUSH_LEDS, flushLeds)));
|
||||
|
||||
private void Update(object? updateTrigger, CustomUpdateData customData) => Update(updateTrigger as IUpdateTrigger, customData);
|
||||
|
||||
@ -140,9 +140,9 @@ public sealed class RGBSurface : AbstractBindable, IDisposable
|
||||
{
|
||||
try
|
||||
{
|
||||
bool flushLeds = customData["flushLeds"] as bool? ?? false;
|
||||
bool render = customData["render"] as bool? ?? true;
|
||||
bool updateDevices = customData["updateDevices"] as bool? ?? true;
|
||||
bool flushLeds = customData[CustomUpdateDataIndex.FLUSH_LEDS] as bool? ?? false;
|
||||
bool render = customData[CustomUpdateDataIndex.RENDER] as bool? ?? true;
|
||||
bool updateDevices = customData[CustomUpdateDataIndex.UPDATE_DEVICES] as bool? ?? true;
|
||||
|
||||
lock (UpdateTriggers)
|
||||
lock (Devices)
|
||||
|
||||
@ -2,6 +2,30 @@
|
||||
|
||||
namespace RGB.NET.Core;
|
||||
|
||||
/// <summary>
|
||||
/// Represents an index used to identify data in the <see cref="CustomUpdateData"/>.
|
||||
/// </summary>
|
||||
public static class CustomUpdateDataIndex
|
||||
{
|
||||
/// <summary>
|
||||
/// Checked by the <see cref="RGBSurface"/> to see if all LEDs needs to be flushed even if they aren't changed in this update.
|
||||
/// default: false
|
||||
/// </summary>
|
||||
public const string FLUSH_LEDS = "flushLeds";
|
||||
|
||||
/// <summary>
|
||||
/// Checked by the <see cref="RGBSurface"/> to see if the surface should be rendered in this update.
|
||||
/// default: true
|
||||
/// </summary>
|
||||
public const string RENDER = "render";
|
||||
|
||||
/// <summary>
|
||||
/// Checked by the <see cref="RGBSurface"/> to see if devies should be updated after rendering.
|
||||
/// default: true
|
||||
/// </summary>
|
||||
public const string UPDATE_DEVICES = "updateDevices";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents a set of custom data, each indexed by a string-key.
|
||||
/// </summary>
|
||||
@ -47,4 +71,4 @@ public class CustomUpdateData
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@ -19,6 +19,8 @@ public sealed class ManualUpdateTrigger : AbstractUpdateTrigger
|
||||
private CancellationTokenSource? UpdateTokenSource { get; set; }
|
||||
private CancellationToken UpdateToken { get; set; }
|
||||
|
||||
private CustomUpdateData? _customUpdateData;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the time it took the last update-loop cycle to run.
|
||||
/// </summary>
|
||||
@ -71,7 +73,11 @@ public sealed class ManualUpdateTrigger : AbstractUpdateTrigger
|
||||
/// <summary>
|
||||
/// Triggers an update.
|
||||
/// </summary>
|
||||
public void TriggerUpdate() => _mutex.Set();
|
||||
public void TriggerUpdate(CustomUpdateData? updateData = null)
|
||||
{
|
||||
_customUpdateData = updateData;
|
||||
_mutex.Set();
|
||||
}
|
||||
|
||||
private void UpdateLoop()
|
||||
{
|
||||
@ -82,7 +88,7 @@ public sealed class ManualUpdateTrigger : AbstractUpdateTrigger
|
||||
if (_mutex.WaitOne(100))
|
||||
{
|
||||
long preUpdateTicks = Stopwatch.GetTimestamp();
|
||||
OnUpdate();
|
||||
OnUpdate(_customUpdateData);
|
||||
LastUpdateTime = ((Stopwatch.GetTimestamp() - preUpdateTicks) / 10000.0);
|
||||
}
|
||||
}
|
||||
|
||||
@ -17,6 +17,8 @@ public class TimerUpdateTrigger : AbstractUpdateTrigger
|
||||
|
||||
private readonly object _lock = new();
|
||||
|
||||
private readonly CustomUpdateData? _customUpdateData;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the update loop of this trigger.
|
||||
/// </summary>
|
||||
@ -62,6 +64,19 @@ public class TimerUpdateTrigger : AbstractUpdateTrigger
|
||||
Start();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="TimerUpdateTrigger"/> class.
|
||||
/// </summary>
|
||||
/// <param name="autostart">A value indicating if the trigger should automatically <see cref="Start"/> right after construction.</param>
|
||||
public TimerUpdateTrigger(CustomUpdateData? customUpdateData, bool autostart = true)
|
||||
{
|
||||
this._customUpdateData = customUpdateData;
|
||||
|
||||
if (autostart)
|
||||
// ReSharper disable once VirtualMemberCallInConstructor - HACK DarthAffe 01.06.2021: I've no idea how to correctly handle that case, for now just disable it
|
||||
Start();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
@ -118,7 +133,7 @@ public class TimerUpdateTrigger : AbstractUpdateTrigger
|
||||
{
|
||||
long preUpdateTicks = Stopwatch.GetTimestamp();
|
||||
|
||||
OnUpdate();
|
||||
OnUpdate(_customUpdateData);
|
||||
|
||||
if (UpdateFrequency > 0)
|
||||
{
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user