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

Added missing files from the last commit

This commit is contained in:
Darth Affe 2016-03-26 19:48:43 +01:00
parent 6f2e77b797
commit 603c4f72fe
4 changed files with 194 additions and 34 deletions

View File

@ -50,8 +50,12 @@
<Compile Include="Devices\Generic\Enums\CorsairAccessMode.cs" />
<Compile Include="Devices\Generic\Enums\CorsairDeviceCaps.cs" />
<Compile Include="Devices\Generic\Enums\CorsairDeviceType.cs" />
<Compile Include="Devices\Generic\ExceptionEventArgs.cs" />
<Compile Include="Devices\Generic\EventArgs\ExceptionEventArgs.cs" />
<Compile Include="Brushes\AbstractBrush.cs" />
<Compile Include="Devices\Generic\EventArgs\LedsUpdatedEventArgs.cs" />
<Compile Include="Devices\Generic\EventArgs\LedsUpdatingEventArgs.cs" />
<Compile Include="Devices\Generic\EventArgs\UpdatedEventArgs.cs" />
<Compile Include="Devices\Generic\EventArgs\UpdatingEventArgs.cs" />
<Compile Include="Devices\Keyboard\Enums\BrushCalculationMode.cs" />
<Compile Include="Gradients\AbstractGradient.cs" />
<Compile Include="Gradients\GradientStop.cs" />

View File

@ -9,6 +9,7 @@ using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using CUE.NET.Devices.Generic.Enums;
using CUE.NET.Devices.Generic.EventArgs;
using CUE.NET.Effects;
using CUE.NET.Native;
@ -73,6 +74,26 @@ namespace CUE.NET.Devices.Generic
/// </summary>
public event ExceptionEventHandler Exception;
/// <summary>
/// Occurs when the device starts updating.
/// </summary>
public event UpdatingEventHandler Updating;
/// <summary>
/// Occurs when the device update is done.
/// </summary>
public event UpdatedEventHandler Updated;
/// <summary>
/// Occurs when the device starts to update the leds.
/// </summary>
public event LedsUpdatingEventHandler LedsUpdating;
/// <summary>
/// Occurs when the device updated the leds.
/// </summary>
public event LedsUpdatedEventHandler LedsUpdated;
#endregion
#region Constructors
@ -158,8 +179,11 @@ namespace CUE.NET.Devices.Generic
/// Performs an update for all dirty keys, or all keys if flushLeds is set to true.
/// </summary>
/// <param name="flushLeds">Specifies whether all keys (including clean ones) should be updated.</param>
public virtual void Update(bool flushLeds = false)
public void Update(bool flushLeds = false)
{
OnUpdating();
DeviceUpdate();
UpdateEffects();
IList<KeyValuePair<int, CorsairLed>> ledsToUpdate = (flushLeds ? Leds : Leds.Where(x => x.Value.IsDirty)).ToList();
@ -168,8 +192,16 @@ namespace CUE.NET.Devices.Generic
led.Update();
UpdateLeds(ledsToUpdate);
OnUpdated();
}
/// <summary>
/// Empty method which can be overwritten to perform device specific updates.
/// </summary>
protected virtual void DeviceUpdate()
{ }
private void UpdateEffects()
{
List<IEffect> effectsToRemove = new List<IEffect>();
@ -198,7 +230,7 @@ namespace CUE.NET.Devices.Generic
effectsToRemove.Add(effect.Effect);
}
// ReSharper disable once CatchAllClause
catch (Exception ex) { ManageException(ex); }
catch (Exception ex) { OnException(ex); }
}
}
@ -212,31 +244,35 @@ namespace CUE.NET.Devices.Generic
/// <param name="effect">The effect to apply.</param>
protected abstract void ApplyEffect(IEffect effect);
private static void UpdateLeds(ICollection<KeyValuePair<int, CorsairLed>> ledsToUpdate)
private void UpdateLeds(ICollection<KeyValuePair<int, CorsairLed>> ledsToUpdate)
{
ledsToUpdate = ledsToUpdate.Where(x => x.Value.Color != Color.Transparent).ToList();
if (!ledsToUpdate.Any())
return; // CUE seems to crash if 'CorsairSetLedsColors' is called with a zero length array
OnLedsUpdating(ledsToUpdate);
int structSize = Marshal.SizeOf(typeof(_CorsairLedColor));
IntPtr ptr = Marshal.AllocHGlobal(structSize * ledsToUpdate.Count);
IntPtr addPtr = new IntPtr(ptr.ToInt64());
foreach (KeyValuePair<int, CorsairLed> led in ledsToUpdate)
if (ledsToUpdate.Any()) // CUE seems to crash if 'CorsairSetLedsColors' is called with a zero length array
{
_CorsairLedColor color = new _CorsairLedColor
int structSize = Marshal.SizeOf(typeof(_CorsairLedColor));
IntPtr ptr = Marshal.AllocHGlobal(structSize * ledsToUpdate.Count);
IntPtr addPtr = new IntPtr(ptr.ToInt64());
foreach (KeyValuePair<int, CorsairLed> led in ledsToUpdate)
{
ledId = led.Key,
r = led.Value.Color.R,
g = led.Value.Color.G,
b = led.Value.Color.B
};
_CorsairLedColor color = new _CorsairLedColor
{
ledId = led.Key,
r = led.Value.Color.R,
g = led.Value.Color.G,
b = led.Value.Color.B
};
Marshal.StructureToPtr(color, addPtr, false);
addPtr = new IntPtr(addPtr.ToInt64() + structSize);
Marshal.StructureToPtr(color, addPtr, false);
addPtr = new IntPtr(addPtr.ToInt64() + structSize);
}
_CUESDK.CorsairSetLedsColors(ledsToUpdate.Count, ptr);
Marshal.FreeHGlobal(ptr);
}
_CUESDK.CorsairSetLedsColors(ledsToUpdate.Count, ptr);
Marshal.FreeHGlobal(ptr);
OnLedsUpdated(ledsToUpdate);
}
/// <summary>
@ -286,16 +322,6 @@ namespace CUE.NET.Devices.Generic
return retVal;
}
/// <summary>
/// Handles the needed event-calls for an exception.
/// </summary>
/// <param name="ex"></param>
/// <exception cref="System.Exception">A delegate callback throws an exception.</exception>
protected void ManageException(Exception ex)
{
Exception?.Invoke(this, new ExceptionEventArgs(ex));
}
/// <summary>
/// Resets all loaded LEDs back to default.
/// </summary>
@ -305,6 +331,85 @@ namespace CUE.NET.Devices.Generic
led.Reset();
}
#region EventCaller
/// <summary>
/// Handles the needed event-calls for an exception.
/// </summary>
/// <param name="ex">The exception previously thrown.</param>
protected virtual void OnException(Exception ex)
{
try
{
Exception?.Invoke(this, new ExceptionEventArgs(ex));
}
catch
{
// Well ... that's not my fault
}
}
/// <summary>
/// Handles the needed event-calls before updating.
/// </summary>
protected virtual void OnUpdating()
{
try
{
Updating?.Invoke(this, new UpdatingEventArgs());
}
catch
{
// Well ... that's not my fault
}
}
/// <summary>
/// Handles the needed event-calls after an update.
/// </summary>
protected virtual void OnUpdated()
{
try
{
Updated?.Invoke(this, new UpdatedEventArgs());
}
catch
{
// Well ... that's not my fault
}
}
/// <summary>
/// Handles the needed event-calls before the leds are updated.
/// </summary>
protected virtual void OnLedsUpdating(ICollection<KeyValuePair<int, CorsairLed>> updatingLeds)
{
try
{
LedsUpdating?.Invoke(this, new LedsUpdatingEventArgs(updatingLeds));
}
catch
{
// Well ... that's not my fault
}
}
/// <summary>
/// Handles the needed event-calls after the leds are updated.
/// </summary>
protected virtual void OnLedsUpdated(IEnumerable<KeyValuePair<int, CorsairLed>> updatedLeds)
{
try
{
LedsUpdated?.Invoke(this, new LedsUpdatedEventArgs(updatedLeds));
}
catch
{
// Well ... that's not my fault
}
}
#endregion
#endregion
}
}

View File

@ -3,12 +3,12 @@
using System;
namespace CUE.NET.Devices.Generic
namespace CUE.NET.Devices.Generic.EventArgs
{
/// <summary>
/// Represents the information supplied with an Exception-event.
/// </summary>
public class ExceptionEventArgs : EventArgs
public class ExceptionEventArgs : System.EventArgs
{
#region Properties & Fields

View File

@ -1,8 +1,8 @@
// ReSharper disable UnusedMemberInSuper.Global
// ReSharper disable UnusedMember.Global
using CUE.NET.Devices.Generic;
using CUE.NET.Devices.Generic.Enums;
using CUE.NET.Devices.Generic.EventArgs;
namespace CUE.NET.Devices
{
@ -13,6 +13,34 @@ namespace CUE.NET.Devices
/// <param name="args">The arguments provided by the event.</param>
public delegate void ExceptionEventHandler(object sender, ExceptionEventArgs args);
/// <summary>
/// Represents the event-handler of the Updating-event.
/// </summary>
/// <param name="sender">The sender of the event.</param>
/// <param name="args">The arguments provided by the event.</param>
public delegate void UpdatingEventHandler(object sender, UpdatingEventArgs args);
/// <summary>
/// Represents the event-handler of the Updated-event.
/// </summary>
/// <param name="sender">The sender of the event.</param>
/// <param name="args">The arguments provided by the event.</param>
public delegate void UpdatedEventHandler(object sender, UpdatedEventArgs args);
/// <summary>
/// Represents the event-handler of the LedsUpdating-event.
/// </summary>
/// <param name="sender">The sender of the event.</param>
/// <param name="args">The arguments provided by the event.</param>
public delegate void LedsUpdatingEventHandler(object sender, LedsUpdatingEventArgs args);
/// <summary>
/// Represents the event-handler of the LedsUpdated-event.
/// </summary>
/// <param name="sender">The sender of the event.</param>
/// <param name="args">The arguments provided by the event.</param>
public delegate void LedsUpdatedEventHandler(object sender, LedsUpdatedEventArgs args);
/// <summary>
/// Represents a generic cue device.
/// </summary>
@ -33,12 +61,35 @@ namespace CUE.NET.Devices
/// </summary>
float UpdateFrequency { get; set; }
// ReSharper disable once EventNeverSubscribedTo.Global
// ReSharper disable EventNeverSubscribedTo.Global
/// <summary>
/// Occurs when a catched exception is thrown inside the device.
/// </summary>
event ExceptionEventHandler Exception;
/// <summary>
/// Occurs when the device starts updating.
/// </summary>
event UpdatingEventHandler Updating;
/// <summary>
/// Occurs when the device update is done.
/// </summary>
event UpdatedEventHandler Updated;
/// <summary>
/// Occurs when the device starts to update the leds.
/// </summary>
event LedsUpdatingEventHandler LedsUpdating;
/// <summary>
/// Occurs when the device updated the leds.
/// </summary>
event LedsUpdatedEventHandler LedsUpdated;
// ReSharper restore EventNeverSubscribedTo.Global
/// <summary>
/// Perform an update for all dirty keys, or all keys if flushLeds is set to true.
/// </summary>