Skip to content

Commit

Permalink
convert PercentGradientLayerHandler to non-render layer
Browse files Browse the repository at this point in the history
  • Loading branch information
Aytackydln committed Dec 21, 2024
1 parent ccad247 commit ecdb940
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace AuroraRgb.EffectsEngine;

// most of this file is created by AI and adjusted, idk what it does
public class ZoneKeyPercentDrawer(EffectLayer effectLayer)
{
public void PercentEffect(Color foregroundColor, Color backgroundColor, KeySequence sequence, double value,
Expand Down Expand Up @@ -238,7 +239,6 @@ private void PercentEffectOnKeys(Color foregroundColor, Color backgroundColor, D
foregroundColor = ColorUtils.BlendColors(backgroundColor, foregroundColor, percent);
}


switch (percentEffectType)
{
case PercentEffectType.AllAtOnce:
Expand Down Expand Up @@ -285,4 +285,99 @@ private void PercentEffectOnKeys(Color foregroundColor, Color backgroundColor, D
}
}
}

public void PercentEffect(ColorSpectrum spectrum, KeySequence sequence, double value,
double total, PercentEffectType percentEffectType,
double flashPast = 0.0, bool flashReversed = false)
{
var progressTotal = (value / total) switch
{
< 0.0 => 0.0,
> 1.0 => 1.0,
_ => value / total
};

var zoneKeysCache = new ZoneKeysCache();
zoneKeysCache.SetSequence(sequence);
var keys = zoneKeysCache.GetKeys();

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

switch (percentEffectType, sequence.Type)
{
case (PercentEffectType.AllAtOnce, _):
{
var color = spectrum.GetColorAt(progressTotal, 1.0f, flashAmount);
effectLayer.Set(keys, in color);
return;
}
case (PercentEffectType.Progressive, KeySequenceType.Sequence):
{
if (progressTotal <= 0)
{
return;
}
for (var i = 0; i < keys.Length; i++)
{
var position = (double)i * keys.Length / (keys.Length - 1);
var keyProgress = Math.Clamp(position/keys.Length, 0, 1);
if (keyProgress > progressTotal)
{
return;
}
var key = keys[i];
var color = spectrum.GetColorAt(keyProgress, 1.0f, flashAmount);
effectLayer.Set(key, in color);
}
break;
}
case (PercentEffectType.Progressive_Gradual, KeySequenceType.Sequence):
{
if (progressTotal <= 0)
{
return;
}
var delta = 1.0 / keys.Length;
for (var i = 0; i < keys.Length; i++)
{
var position = (double)i * keys.Length / (keys.Length - 1);
var keyProgress = Math.Clamp(position/keys.Length, 0, 1) * (keys.Length - 1) / keys.Length;
var key = keys[i];
var color = spectrum.GetColorAt(keyProgress, 1.0f, flashAmount);

if (progressTotal - keyProgress < delta)
{
var keyCoverage = (progressTotal - keyProgress) / delta;
if (keyCoverage < float.Epsilon)
{
return;
}
var partialColor = ColorUtils.MultiplyColorByScalar(color, keyCoverage);
effectLayer.Set(key, in partialColor);
return;
}
effectLayer.Set(key, in color);
}
break;
}
case (PercentEffectType.Highest_Key, KeySequenceType.Sequence):
case (PercentEffectType.Highest_Key_Blend, KeySequenceType.Sequence):
{
if (progressTotal <= 0)
{
return;
}
var highestKey = Math.Clamp((int)(progressTotal * keys.Length), 0, keys.Length - 1);

var key = keys[highestKey];
var color = spectrum.GetColorAt(progressTotal, 1.0f, flashAmount);
effectLayer.Set(key, in color);
return;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public override void Default()
[LogicOverrideIgnoreProperty("_PrimaryColor")]
[LogicOverrideIgnoreProperty("SecondaryColor")]
[LayerHandlerMeta(Name = "Percent (Gradient)", IsDefault = true)]
public sealed class PercentGradientLayerHandler() : LayerHandler<PercentGradientLayerHandlerProperties, BitmapEffectLayer>("PercentLayer")
public sealed class PercentGradientLayerHandler() : LayerHandler<PercentGradientLayerHandlerProperties>("PercentLayer")
{
protected override UserControl CreateControl()
{
Expand All @@ -48,10 +48,8 @@ public override EffectLayer Render(IGameState gameState)
var value = Properties.Logic?._Value ?? gameState.GetNumber(Properties.VariablePath);
var maxvalue = Properties.Logic?._MaxValue ?? gameState.GetNumber(Properties.MaxVariablePath);

EffectLayer.PercentEffect(Properties.Gradient.GetColorSpectrum(), Properties.Sequence, value, maxvalue, Properties.PercentType, Properties.BlinkThreshold, Properties.BlinkDirection);
// below is for no-render layer
//var percentDrawer = new ZoneKeyPercentDrawer(EffectLayer);
//percentDrawer.PercentEffect(Properties.Gradient.GetColorSpectrum(), Properties.Sequence, value, maxvalue, Properties.PercentType, Properties.BlinkThreshold, Properties.BlinkDirection);
var percentDrawer = new ZoneKeyPercentDrawer(EffectLayer);
percentDrawer.PercentEffect(Properties.Gradient.GetColorSpectrum(), Properties.Sequence, value, maxvalue, Properties.PercentType, Properties.BlinkThreshold, Properties.BlinkDirection);
return EffectLayer;
}

Expand Down

0 comments on commit ecdb940

Please sign in to comment.