Skip to content

Commit

Permalink
implement freeform drawing for Percent (Gradual) layer
Browse files Browse the repository at this point in the history
  • Loading branch information
Aytackydln committed Dec 22, 2024
1 parent e87c9c0 commit c27f509
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,83 @@ public void PercentEffect(ColorSpectrum spectrum, KeySequence sequence, double v
effectLayer.Set(key, in color);
return;
}
case (_, KeySequenceType.FreeForm):
{
PercentEffectOnFreeForm(spectrum, sequence.Freeform, keys, value, total, flashPast, flashReversed, effectLayer);
return;
}
}
}

private static void PercentEffectOnFreeForm(ColorSpectrum spectrum, FreeFormObject freeform,
DeviceKeys[] keys, double value, double total,
double flashPast, bool flashReversed, EffectLayer effectLayer)
{
// Calculate progress
var progressTotal = Math.Clamp(value / total, 0.0, 1.0);

var percent = 1.0;
// Apply flash effect if needed
if (flashPast > 0.0 && ((flashReversed && progressTotal >= flashPast) || (!flashReversed && progressTotal <= flashPast)))
{
percent = Math.Sin(Time.GetMillisecondsSinceEpoch() % 1000.0D / 1000.0D * Math.PI);
}

// Calculate freeform object's world coordinates and corners
var freeformCorners = GetFreeFormCorners(freeform, progressTotal);

// Iterate through keys and determine their color based on their position
foreach (var key in keys)
{
// Get key's rectangle in canvas coordinates
ref readonly var keyRect = ref Effects.Canvas.GetRectangle(key);

// Create key corners
var keyCorners = new[]
{
new PointF(keyRect.Left, keyRect.Bottom),
new PointF(keyRect.Right, keyRect.Bottom),
new PointF(keyRect.Right, keyRect.Top),
new PointF(keyRect.Left, keyRect.Top)
};

// Calculate coverage
var coverageRatio = CalculateKeyCoverage(freeformCorners, keyCorners) * percent;
if (coverageRatio < double.Epsilon)
{
continue;
}

var x = GetKeyColorPosition(keyCorners, freeform);

var keyColor = ColorUtils.MultiplyColorByScalar(
spectrum.GetColorAt(x),
Math.Min(coverageRatio, 1.0)
);

effectLayer.Set(key, in keyColor);
}
}

private static double GetKeyColorPosition(PointF[] keyCorners, FreeFormObject freeform)
{
// First calculate the center point of the key using its corners
var centerX = keyCorners.Average(p => p.X);
var centerY = keyCorners.Average(p => p.Y);

// Calculate the key's center position as a ratio of the freeform's width
// Convert FreeForm coordinates to canvas coordinates
var xPos = (freeform.X + Effects.Canvas.GridBaselineX) * Effects.Canvas.EditorToCanvasWidth;
var yPos = (freeform.Y + Effects.Canvas.GridBaselineY) * Effects.Canvas.EditorToCanvasHeight;
var width = freeform.Width * Effects.Canvas.EditorToCanvasWidth;
var height = freeform.Height * Effects.Canvas.EditorToCanvasHeight;

var x = (centerX - xPos) / width;
var y = (centerY - yPos) / height;

var sin = Math.Sin(freeform.Angle * Math.PI / 180);
var cos = Math.Cos(freeform.Angle * Math.PI / 180);

return x * cos - y * sin;
}
}
4 changes: 2 additions & 2 deletions Project-Aurora/Project-Aurora/Settings/FreeFormObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public sealed class FreeFormObject : IEquatable<FreeFormObject>
{
private float _x;
/// <summary>
/// Get/Set the X coordinate for this region.
/// Get/Set the original X coordinate for this region. Absolute X coordinate is calculated based on the rotation angle.
/// </summary>
public float X
{
Expand All @@ -33,7 +33,7 @@ public float X

private float _y;
/// <summary>
/// Get/Set the Y coordinate for this region.
/// Get/Set the original Y coordinate for this region. Absolute Y coordinate is calculated based on the rotation angle.
/// </summary>
public float Y
{
Expand Down

0 comments on commit c27f509

Please sign in to comment.