|
| 1 | +using RGB.NET.Core; |
| 2 | +using System; |
| 3 | +using System.Collections.Concurrent; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Diagnostics; |
| 6 | +using System.Linq; |
| 7 | +using Color = RGB.NET.Core.Color; |
| 8 | + |
| 9 | +namespace Chromatics.Extensions.RGB.NET.Decorators |
| 10 | +{ |
| 11 | + public class BPMPWMDecorator : AbstractUpdateAwareDecorator, ILedGroupDecorator |
| 12 | + { |
| 13 | + private readonly ListLedGroup ledGroup; |
| 14 | + private readonly Random random = new Random(); |
| 15 | + private readonly double waveFrequency; |
| 16 | + private readonly Color[] colors; |
| 17 | + private readonly Color baseColor; |
| 18 | + private readonly int groupSize; |
| 19 | + private readonly List<Led> leds; |
| 20 | + private ConcurrentDictionary<Led, double> ledPositions; |
| 21 | + private Dictionary<Led, Color> currentColors; |
| 22 | + private Dictionary<Led, Color> targetColors; |
| 23 | + private Dictionary<Led, double> transitionStartTimes; |
| 24 | + private double Timing; |
| 25 | + private double nextBeatTime; |
| 26 | + private double interval; |
| 27 | + private int bpm; |
| 28 | + private double fadeTime; |
| 29 | + |
| 30 | + public BPMPWMDecorator(ListLedGroup _ledGroup, int bpm, double waveFrequency, Color[] colors, double fadeTime, int groupSize, RGBSurface surface, bool updateIfDisabled = false, Color baseColor = default(Color)) : base(surface, updateIfDisabled) |
| 31 | + { |
| 32 | + this.ledGroup = _ledGroup; |
| 33 | + this.bpm = bpm; |
| 34 | + this.waveFrequency = waveFrequency; |
| 35 | + this.colors = colors; |
| 36 | + this.baseColor = baseColor == default(Color) ? Color.Transparent : baseColor; |
| 37 | + this.fadeTime = fadeTime; |
| 38 | + this.groupSize = groupSize; |
| 39 | + |
| 40 | + leds = ledGroup.OrderBy(led => led.Id).ToList(); // Ensure the list is ordered correctly |
| 41 | + CalculateInterval(); |
| 42 | + ledPositions = new ConcurrentDictionary<Led, double>(); |
| 43 | + currentColors = new Dictionary<Led, Color>(); |
| 44 | + targetColors = new Dictionary<Led, Color>(); |
| 45 | + transitionStartTimes = new Dictionary<Led, double>(); |
| 46 | + Timing = 0; |
| 47 | + nextBeatTime = interval; // Initialize nextBeatTime |
| 48 | + } |
| 49 | + |
| 50 | + private void CalculateInterval() |
| 51 | + { |
| 52 | + // Convert BPM to interval in seconds (interval = 60 / BPM) |
| 53 | + interval = 60.0 / bpm; |
| 54 | + } |
| 55 | + |
| 56 | + public override void OnAttached(IDecoratable decoratable) |
| 57 | + { |
| 58 | + base.OnAttached(decoratable); |
| 59 | + ledGroup.Detach(); |
| 60 | + |
| 61 | + Debug.WriteLine("Arena Light Show Decorator Attached"); |
| 62 | + |
| 63 | + foreach (var led in leds) |
| 64 | + { |
| 65 | + ledPositions.TryAdd(led, random.NextDouble() * Math.PI * 2); |
| 66 | + var initialColor = colors[random.Next(colors.Length)]; |
| 67 | + currentColors[led] = initialColor; |
| 68 | + targetColors[led] = initialColor; |
| 69 | + transitionStartTimes[led] = 0; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public override void OnDetached(IDecoratable decoratable) |
| 74 | + { |
| 75 | + base.OnDetached(decoratable); |
| 76 | + ledPositions.Clear(); |
| 77 | + currentColors.Clear(); |
| 78 | + targetColors.Clear(); |
| 79 | + transitionStartTimes.Clear(); |
| 80 | + Timing = 0; |
| 81 | + nextBeatTime = interval; |
| 82 | + |
| 83 | + Debug.WriteLine("Arena Light Show Decorator Detached"); |
| 84 | + } |
| 85 | + |
| 86 | + protected override void Update(double deltaTime) |
| 87 | + { |
| 88 | + try |
| 89 | + { |
| 90 | + if (ledGroup == null) return; |
| 91 | + |
| 92 | + Timing += deltaTime; |
| 93 | + |
| 94 | + // Check if it's time for the next beat |
| 95 | + if (Timing >= nextBeatTime) |
| 96 | + { |
| 97 | + nextBeatTime += interval; // Schedule next beat |
| 98 | + |
| 99 | + foreach (var led in leds) |
| 100 | + { |
| 101 | + int groupIndex = groupSize == 0 ? random.Next(leds.Count) : (int)(leds.IndexOf(led) % groupSize); |
| 102 | + int colorIndex = (groupIndex + (int)(Timing / interval)) % colors.Length; |
| 103 | + |
| 104 | + targetColors[led] = colors[colorIndex]; |
| 105 | + transitionStartTimes[led] = Timing; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + foreach (var led in leds) |
| 110 | + { |
| 111 | + if (!ledPositions.ContainsKey(led)) continue; |
| 112 | + |
| 113 | + double timeSinceTransitionStart = Timing - transitionStartTimes[led]; |
| 114 | + float transitionProgress = fadeTime > 0 ? (float)Math.Min(timeSinceTransitionStart / fadeTime, 1.0) : 1.0f; |
| 115 | + |
| 116 | + var color = fadeTime == 0 ? targetColors[led] : Lerp(currentColors[led], targetColors[led], transitionProgress); |
| 117 | + led.Color = color; |
| 118 | + |
| 119 | + // Update the current color if the transition is complete |
| 120 | + if (transitionProgress >= 1.0) |
| 121 | + { |
| 122 | + currentColors[led] = targetColors[led]; |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + catch (Exception ex) |
| 127 | + { |
| 128 | +#if DEBUG |
| 129 | + Debug.WriteLine($"Exception: {ex.Message}"); |
| 130 | +#endif |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + private static Color Lerp(Color start, Color end, float amount) |
| 135 | + { |
| 136 | + float r = start.R + (end.R - start.R) * amount; |
| 137 | + float g = start.G + (end.G - start.G) * amount; |
| 138 | + float b = start.B + (end.B - start.B) * amount; |
| 139 | + |
| 140 | + return new Color((byte)(r * 255), (byte)(g * 255), (byte)(b * 255)); |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments