Added pokemon exception handling around effect- and brush-updates to prevent issues with wrong implementations.

This commit is contained in:
DarthAffe committed 2015-10-10 12:29:35 +02:00
1 parent 920821deee
commit 2590c4ad2a
5 files changed
+65 -17

No files matched your search

+11
View File
@@ -39,6 +39,12 @@ namespace CUE.NET.Devices.Generic
#endregion
#region Events
public event OnExceptionEventHandler OnException;
#endregion
#region Constructors
protected AbstractCueDevice(IDeviceInfo info)
@@ -142,6 +148,11 @@ namespace CUE.NET.Devices.Generic
Marshal.FreeHGlobal(ptr);
}
protected void ManageException(Exception ex)
{
OnException?.Invoke(this, new OnExceptionEventArgs(ex));
}
#endregion
}
}
+22
View File
@@ -0,0 +1,22 @@
using System;
namespace CUE.NET.Devices.Generic
{
public class OnExceptionEventArgs : EventArgs
{
#region Properties & Fields
public Exception Exception { get; }
#endregion
#region Constructors
public OnExceptionEventArgs(Exception exception)
{
this.Exception = exception;
}
#endregion
}
}
+7 -1
View File
@@ -1,15 +1,21 @@
using CUE.NET.Devices.Generic.Enums;
using CUE.NET.Devices.Generic;
using CUE.NET.Devices.Generic.Enums;
namespace CUE.NET.Devices
{
public delegate void OnExceptionEventHandler(object sender, OnExceptionEventArgs args);
public interface ICueDevice
{
IDeviceInfo DeviceInfo { get; }
UpdateMode UpdateMode { get; set; }
float UpdateFrequency { get; set; }
event OnExceptionEventHandler OnException;
void Update(bool flushLeds = false);
}
}
+24 -16
View File
@@ -97,22 +97,26 @@ namespace CUE.NET.Devices.Keyboard
long currentTicks = DateTime.Now.Ticks;
foreach (EffectTimeContainer effect in _effects)
{
float deltaTime;
if (effect.TicksAtLastUpdate < 0)
try
{
float deltaTime;
if (effect.TicksAtLastUpdate < 0)
{
effect.TicksAtLastUpdate = currentTicks;
deltaTime = 0f;
}
else
deltaTime = (currentTicks - effect.TicksAtLastUpdate) / 10000000f;
effect.TicksAtLastUpdate = currentTicks;
deltaTime = 0f;
effect.Effect.Update(deltaTime);
ApplyBrush((effect.Effect.KeyList ?? this).ToList(), effect.Effect.EffectBrush);
if (effect.Effect.IsDone)
effectsToRemove.Add(effect.Effect);
}
else
deltaTime = (currentTicks - effect.TicksAtLastUpdate) / 10000000f;
effect.TicksAtLastUpdate = currentTicks;
effect.Effect.Update(deltaTime);
ApplyBrush((effect.Effect.KeyList ?? this).ToList(), effect.Effect.EffectBrush);
if (effect.Effect.IsDone)
effectsToRemove.Add(effect.Effect);
catch (Exception ex) { ManageException(ex); }
}
}
@@ -136,9 +140,13 @@ namespace CUE.NET.Devices.Keyboard
// ReSharper disable once MemberCanBeMadeStatic.Local - idc
private void ApplyBrush(ICollection<CorsairKey> keys, IBrush brush)
{
RectangleF brushRectangle = RectangleHelper.CreateRectangleFromRectangles(keys.Select(x => x.KeyRectangle));
foreach (CorsairKey key in keys)
key.Led.Color = brush.GetColorAtPoint(brushRectangle, key.KeyRectangle.GetCenter());
try
{
RectangleF brushRectangle = RectangleHelper.CreateRectangleFromRectangles(keys.Select(x => x.KeyRectangle));
foreach (CorsairKey key in keys)
key.Led.Color = brush.GetColorAtPoint(brushRectangle, key.KeyRectangle.GetCenter());
}
catch (Exception ex) { ManageException(ex); }
}
#endregion