1
0
mirror of https://github.com/DarthAffe/CUE.NET.git synced 2025-12-12 16:58:29 +00:00

Fixed update mechanism

This commit is contained in:
Darth Affe 2016-03-26 20:37:10 +01:00
parent 603c4f72fe
commit dbdc1c22e8
6 changed files with 53 additions and 22 deletions

View File

@ -56,6 +56,7 @@
<Compile Include="Devices\Generic\EventArgs\LedsUpdatingEventArgs.cs" />
<Compile Include="Devices\Generic\EventArgs\UpdatedEventArgs.cs" />
<Compile Include="Devices\Generic\EventArgs\UpdatingEventArgs.cs" />
<Compile Include="Devices\Generic\LedUpateRequest.cs" />
<Compile Include="Devices\Keyboard\Enums\BrushCalculationMode.cs" />
<Compile Include="Gradients\AbstractGradient.cs" />
<Compile Include="Gradients\GradientStop.cs" />

View File

@ -186,7 +186,7 @@ namespace CUE.NET.Devices.Generic
DeviceUpdate();
UpdateEffects();
IList<KeyValuePair<int, CorsairLed>> ledsToUpdate = (flushLeds ? Leds : Leds.Where(x => x.Value.IsDirty)).ToList();
ICollection<LedUpateRequest> ledsToUpdate = (flushLeds ? Leds : Leds.Where(x => x.Value.IsDirty)).Select(x => new LedUpateRequest(x.Key, x.Value.RequestedColor)).ToList();
foreach (CorsairLed led in Leds.Values)
led.Update();
@ -244,35 +244,35 @@ namespace CUE.NET.Devices.Generic
/// <param name="effect">The effect to apply.</param>
protected abstract void ApplyEffect(IEffect effect);
private void UpdateLeds(ICollection<KeyValuePair<int, CorsairLed>> ledsToUpdate)
private void UpdateLeds(ICollection<LedUpateRequest> updateRequests)
{
ledsToUpdate = ledsToUpdate.Where(x => x.Value.Color != Color.Transparent).ToList();
updateRequests = updateRequests.Where(x => x.Color != Color.Transparent).ToList();
OnLedsUpdating(ledsToUpdate);
OnLedsUpdating(updateRequests);
if (ledsToUpdate.Any()) // CUE seems to crash if 'CorsairSetLedsColors' is called with a zero length array
if (updateRequests.Any()) // CUE seems to crash if 'CorsairSetLedsColors' is called with a zero length array
{
int structSize = Marshal.SizeOf(typeof(_CorsairLedColor));
IntPtr ptr = Marshal.AllocHGlobal(structSize * ledsToUpdate.Count);
IntPtr ptr = Marshal.AllocHGlobal(structSize * updateRequests.Count);
IntPtr addPtr = new IntPtr(ptr.ToInt64());
foreach (KeyValuePair<int, CorsairLed> led in ledsToUpdate)
foreach (LedUpateRequest ledUpdateRequest in updateRequests)
{
_CorsairLedColor color = new _CorsairLedColor
{
ledId = led.Key,
r = led.Value.Color.R,
g = led.Value.Color.G,
b = led.Value.Color.B
ledId = ledUpdateRequest.LedId,
r = ledUpdateRequest.Color.R,
g = ledUpdateRequest.Color.G,
b = ledUpdateRequest.Color.B
};
Marshal.StructureToPtr(color, addPtr, false);
addPtr = new IntPtr(addPtr.ToInt64() + structSize);
}
_CUESDK.CorsairSetLedsColors(ledsToUpdate.Count, ptr);
_CUESDK.CorsairSetLedsColors(updateRequests.Count, ptr);
Marshal.FreeHGlobal(ptr);
}
OnLedsUpdated(ledsToUpdate);
OnLedsUpdated(updateRequests);
}
/// <summary>
@ -382,7 +382,7 @@ namespace CUE.NET.Devices.Generic
/// <summary>
/// Handles the needed event-calls before the leds are updated.
/// </summary>
protected virtual void OnLedsUpdating(ICollection<KeyValuePair<int, CorsairLed>> updatingLeds)
protected virtual void OnLedsUpdating(ICollection<LedUpateRequest> updatingLeds)
{
try
{
@ -397,7 +397,7 @@ namespace CUE.NET.Devices.Generic
/// <summary>
/// Handles the needed event-calls after the leds are updated.
/// </summary>
protected virtual void OnLedsUpdated(IEnumerable<KeyValuePair<int, CorsairLed>> updatedLeds)
protected virtual void OnLedsUpdated(IEnumerable<LedUpateRequest> updatedLeds)
{
try
{

View File

@ -1,4 +1,5 @@
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedAutoPropertyAccessor.Global
using System.Collections.Generic;
@ -8,15 +9,15 @@ namespace CUE.NET.Devices.Generic.EventArgs
{
#region Properties & Fields
public IEnumerable<KeyValuePair<int, CorsairLed>> UpdatedLeds { get; private set; }
public IEnumerable<LedUpateRequest> UpdatedLeds { get; }
#endregion
#region Constructors
public LedsUpdatedEventArgs(IEnumerable<KeyValuePair<int, CorsairLed>> updatedLeds)
public LedsUpdatedEventArgs(IEnumerable<LedUpateRequest> updatedLeds)
{
this.UpdatedLeds = new List<KeyValuePair<int, CorsairLed>>(updatedLeds); // Copy this - we don't want anyone to change the original led list.
this.UpdatedLeds = updatedLeds;
}
#endregion

View File

@ -1,4 +1,5 @@
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedAutoPropertyAccessor.Global
using System.Collections.Generic;
@ -8,15 +9,15 @@ namespace CUE.NET.Devices.Generic.EventArgs
{
#region Properties & Fields
public ICollection<KeyValuePair<int, CorsairLed>> UpdatingLeds { get; private set; }
public ICollection<LedUpateRequest> UpdatingLeds { get; }
#endregion
#region Constructors
public LedsUpdatingEventArgs(ICollection<KeyValuePair<int, CorsairLed>> updatingLeds)
public LedsUpdatingEventArgs(ICollection<LedUpateRequest> updatingLeds)
{
this.UpdatingLeds = updatingLeds; // No copy here - before the update changing this is ok.
this.UpdatingLeds = updatingLeds;
}
#endregion

View File

@ -0,0 +1,28 @@
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
using System.Drawing;
namespace CUE.NET.Devices.Generic
{
public class LedUpateRequest
{
#region Properties & Fields
public int LedId { get; }
public Color Color { get; set; }
#endregion
#region Constructors
public LedUpateRequest(int ledId, Color color)
{
this.LedId = ledId;
this.Color = color;
}
#endregion
}
}

View File

@ -183,7 +183,7 @@ namespace CUE.NET.Devices.Keyboard
brushRectangle.X = 0;
brushRectangle.Y = 0;
foreach (CorsairKey key in keys)
key.Led.Color = brush.GetColorAtPoint(KeyboardRectangle, key.KeyRectangle.GetCenter(offsetX, offsetY));
key.Led.Color = brush.GetColorAtPoint(brushRectangle, key.KeyRectangle.GetCenter(offsetX, offsetY));
break;
case BrushCalculationMode.Absolute:
foreach (CorsairKey key in keys)