Refactored brushes to perform a multi-pass rendering.

This allows effects to change the calculated values.
This commit is contained in:
DarthAffe committed 2016-08-21 18:14:31 +02:00
1 parent ba80ef9848
commit 8d32c73b6d
15 files changed
+159 -97

No files matched your search

+38 -2
View File
@@ -1,6 +1,9 @@
// ReSharper disable VirtualMemberNeverOverriden.Global
// ReSharper disable MemberCanBePrivate.Global
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using CUE.NET.Devices.Keyboard.Enums;
using CUE.NET.Effects;
using CUE.NET.Helper;
@@ -29,6 +32,16 @@ namespace CUE.NET.Brushes
/// </summary>
public float Opacity { get; set; }
/// <summary>
/// Gets the Rectangle used in the last render pass.
/// </summary>
public RectangleF RenderedRectangle { get; protected set; }
/// <summary>
/// Gets a dictionary containing all colors for points calculated in the last render pass.
/// </summary>
public Dictionary<BrushRenderTarget, Color> RenderedTargets { get; } = new Dictionary<BrushRenderTarget, Color>();
#endregion
#region Constructors
@@ -48,13 +61,36 @@ namespace CUE.NET.Brushes
#region Methods
/// <summary>
/// Performas the render pass of the brush and calculates the raw colors for all requested points.
/// </summary>
/// <param name="rectangle">The rectangle in which the brush should be drawn.</param>
/// <param name="renderTargets">The targets (keys/points) of which the color should be calculated.</param>
public virtual void PerformRender(RectangleF rectangle, IEnumerable<BrushRenderTarget> renderTargets)
{
RenderedRectangle = rectangle;
RenderedTargets.Clear();
foreach (BrushRenderTarget point in renderTargets)
RenderedTargets[point] = GetColorAtPoint(rectangle, point);
}
/// <summary>
/// Performs the finalize pass of the brush and calculates the final colors for all previously calculated points.
/// </summary>
public virtual void PerformFinalize()
{
List<BrushRenderTarget> renderTargets = RenderedTargets.Keys.ToList();
foreach (BrushRenderTarget renderTarget in renderTargets)
RenderedTargets[renderTarget] = FinalizeColor(RenderedTargets[renderTarget]);
}
/// <summary>
/// Gets the color at an specific point assuming the brush is drawn into the given rectangle.
/// </summary>
/// <param name="rectangle">The rectangle in which the brush should be drawn.</param>
/// <param name="point">The point from which the color should be taken.</param>
/// <param name="renderTarget">The target (key/point) from which the color should be taken.</param>
/// <returns>The color at the specified point.</returns>
public abstract Color GetColorAtPoint(RectangleF rectangle, PointF point);
protected abstract Color GetColorAtPoint(RectangleF rectangle, BrushRenderTarget renderTarget);
/// <summary>
/// Finalizes the color by appliing the overall brightness and opacity.<br/>
+29
View File
@@ -0,0 +1,29 @@
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedAutoPropertyAccessor.Global
using System.Drawing;
using CUE.NET.Devices.Keyboard.Enums;
namespace CUE.NET.Brushes
{
public class BrushRenderTarget
{
#region Properties & Fields
public CorsairKeyboardKeyId Key { get; }
public PointF Point { get; }
#endregion
#region Constructors
public BrushRenderTarget(CorsairKeyboardKeyId key, PointF point)
{
this.Point = point;
this.Key = key;
}
#endregion
}
}
+19 -4
View File
@@ -1,5 +1,6 @@
// ReSharper disable UnusedMemberInSuper.Global
using System.Collections.Generic;
using System.Drawing;
using CUE.NET.Devices.Keyboard.Enums;
using CUE.NET.Effects;
@@ -27,11 +28,25 @@ namespace CUE.NET.Brushes
float Opacity { get; set; }
/// <summary>
/// Gets the color at an specific point assuming the brush is drawn into the given rectangle.
/// Gets the Rectangle used in the last render pass.
/// </summary>
RectangleF RenderedRectangle { get; }
/// <summary>
/// Gets a dictionary containing all colors for points calculated in the last render pass.
/// </summary>
Dictionary<BrushRenderTarget, Color> RenderedTargets { get; }
/// <summary>
/// Performas the render pass of the brush and calculates the raw colors for all requested points.
/// </summary>
/// <param name="rectangle">The rectangle in which the brush should be drawn.</param>
/// <param name="point">The point from which the color should be taken.</param>
/// <returns>The color at the specified point.</returns>
Color GetColorAtPoint(RectangleF rectangle, PointF point);
/// <param name="renderTargets">The targets (keys/points) of which the color should be calculated.</param>
void PerformRender(RectangleF rectangle, IEnumerable<BrushRenderTarget> renderTargets);
/// <summary>
/// Performs the finalize pass of the brush and calculates the final colors for all previously calculated points.
/// </summary>
void PerformFinalize();
}
}
+7 -7
View File
@@ -61,12 +61,12 @@ namespace CUE.NET.Brushes
#region Methods
/// <summary>
/// Gets the color at an specific point getting the color of the key at the given point.
/// Gets the color at an specific point assuming the brush is drawn into the given rectangle.
/// </summary>
/// <param name="rectangle">The rectangle in which the brush should be drawn.</param>
/// <param name="point">The point from which the color should be taken.</param>
/// <returns>The color of the key at the specified point.</returns>
public override Color GetColorAtPoint(RectangleF rectangle, PointF point)
/// <param name="renderTarget">The target (key/point) from which the color should be taken.</param>
/// <returns>The color at the specified point.</returns>
protected override Color GetColorAtPoint(RectangleF rectangle, BrushRenderTarget renderTarget)
{
if (Image == null || Image.Width == 0 || Image.Height == 0)
return Color.Transparent;
@@ -75,13 +75,13 @@ namespace CUE.NET.Brushes
float scaleX = Image.Width / rectangle.Width;
float scaleY = Image.Height / rectangle.Height;
int x = (int)(point.X * scaleX);
int y = (int)(point.Y * scaleY);
int x = (int)(renderTarget.Point.X * scaleX);
int y = (int)(renderTarget.Point.Y * scaleY);
x = Math.Max(0, Math.Min(x, Image.Width - 1));
y = Math.Max(0, Math.Min(y, Image.Height - 1));
return FinalizeColor(Image.GetPixel(x, y));
return Image.GetPixel(x, y);
}
#endregion
+4 -4
View File
@@ -74,17 +74,17 @@ namespace CUE.NET.Brushes
/// Gets the color at an specific point assuming the brush is drawn into the given rectangle.
/// </summary>
/// <param name="rectangle">The rectangle in which the brush should be drawn.</param>
/// <param name="point">The point from which the color should be taken.</param>
/// <param name="renderTarget">The target (key/point) from which the color should be taken.</param>
/// <returns>The color at the specified point.</returns>
public override Color GetColorAtPoint(RectangleF rectangle, PointF point)
protected override Color GetColorAtPoint(RectangleF rectangle, BrushRenderTarget renderTarget)
{
if (Gradient == null) return Color.Transparent;
PointF startPoint = new PointF(StartPoint.X * rectangle.Width, StartPoint.Y * rectangle.Height);
PointF endPoint = new PointF(EndPoint.X * rectangle.Width, EndPoint.Y * rectangle.Height);
float offset = GradientHelper.CalculateLinearGradientOffset(startPoint, endPoint, point);
return FinalizeColor(Gradient.GetColor(offset));
float offset = GradientHelper.CalculateLinearGradientOffset(startPoint, endPoint, renderTarget.Point);
return Gradient.GetColor(offset);
}
#endregion
+6 -9
View File
@@ -34,21 +34,18 @@ namespace CUE.NET.Brushes
#region Methods
/// <summary>
/// Gets the color at an specific point getting the color of the key at the given point.
/// Gets the color at an specific point assuming the brush is drawn into the given rectangle.
/// </summary>
/// <param name="rectangle">The rectangle in which the brush should be drawn.</param>
/// <param name="point">The point from which the color should be taken.</param>
/// <returns>The color of the key at the specified point.</returns>
public override Color GetColorAtPoint(RectangleF rectangle, PointF point)
/// <param name="renderTarget">The target (key/point) from which the color should be taken.</param>
/// <returns>The color at the specified point.</returns>
protected override Color GetColorAtPoint(RectangleF rectangle, BrushRenderTarget renderTarget)
{
CorsairKey key = CueSDK.KeyboardSDK[point];
CorsairKey key = CueSDK.KeyboardSDK[renderTarget.Key];
if (key == null) return Color.Transparent;
Color color;
if (!_keyLights.TryGetValue(key.KeyId, out color))
return Color.Transparent;
return FinalizeColor(color);
return !_keyLights.TryGetValue(key.KeyId, out color) ? Color.Transparent : color;
}
#endregion
+4 -4
View File
@@ -66,9 +66,9 @@ namespace CUE.NET.Brushes
/// Gets the color at an specific point assuming the brush is drawn into the given rectangle.
/// </summary>
/// <param name="rectangle">The rectangle in which the brush should be drawn.</param>
/// <param name="point">The point from which the color should be taken.</param>
/// <param name="renderTarget">The target (key/point) from which the color should be taken.</param>
/// <returns>The color at the specified point.</returns>
public override Color GetColorAtPoint(RectangleF rectangle, PointF point)
protected override Color GetColorAtPoint(RectangleF rectangle, BrushRenderTarget renderTarget)
{
if(Gradient == null) return Color.Transparent;
@@ -81,9 +81,9 @@ namespace CUE.NET.Brushes
GradientHelper.CalculateDistance(new PointF(rectangle.X, rectangle.Y + rectangle.Height), centerPoint)),
GradientHelper.CalculateDistance(new PointF(rectangle.X + rectangle.Width, rectangle.Y + rectangle.Height), centerPoint));
float distance = GradientHelper.CalculateDistance(point, centerPoint);
float distance = GradientHelper.CalculateDistance(renderTarget.Point, centerPoint);
float offset = distance / refDistance;
return FinalizeColor(Gradient.GetColor(offset));
return Gradient.GetColor(offset);
}
#endregion
+6 -6
View File
@@ -24,14 +24,14 @@ namespace CUE.NET.Brushes
#region Methods
/// <summary>
/// Gets a random color.
/// Gets the color at an specific point assuming the brush is drawn into the given rectangle.
/// </summary>
/// <param name="rectangle">This value isn't used.</param>
/// <param name="point">This value isn't used.</param>
/// <returns>A random color.</returns>
public override Color GetColorAtPoint(RectangleF rectangle, PointF point)
/// <param name="rectangle">The rectangle in which the brush should be drawn.</param>
/// <param name="renderTarget">The target (key/point) from which the color should be taken.</param>
/// <returns>The color at the specified point.</returns>
protected override Color GetColorAtPoint(RectangleF rectangle, BrushRenderTarget renderTarget)
{
return FinalizeColor(ColorHelper.ColorFromHSV((float)_random.NextDouble() * 360f, 1, 1));
return ColorHelper.ColorFromHSV((float)_random.NextDouble() * 360f, 1, 1);
}
#endregion
+6 -6
View File
@@ -37,14 +37,14 @@ namespace CUE.NET.Brushes
#region Methods
/// <summary>
/// Returns the <see cref="Color" /> of the brush.
/// Gets the color at an specific point assuming the brush is drawn into the given rectangle.
/// </summary>
/// <param name="rectangle">This value isn't used.</param>
/// <param name="point">This value isn't used.</param>
/// <returns>The <see cref="Color" /> of the brush.</returns>
public override Color GetColorAtPoint(RectangleF rectangle, PointF point)
/// <param name="rectangle">The rectangle in which the brush should be drawn.</param>
/// <param name="renderTarget">The target (key/point) from which the color should be taken.</param>
/// <returns>The color at the specified point.</returns>
protected override Color GetColorAtPoint(RectangleF rectangle, BrushRenderTarget renderTarget)
{
return FinalizeColor(Color);
return Color;
}
#endregion