Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void ParticleEffect(Color[] toColor, string[] regions, uint interval, CancellationTokenSource cts, int speed = 50)
- {
- //Check if cancelled
- if (cts.IsCancellationRequested) return;
- //Setup Custom Effect
- var refreshKeyGrid = KeyboardCustom.Create();
- refreshKeyGrid = _keyboardGrid;
- var _effectGrid = KeyboardCustom.Create();
- _effectGrid = refreshKeyGrid;
- //Dictionary to store colors
- Dictionary<string, ColorFader> colorFaderDict = new Dictionary<string, ColorFader>();
- //Setup effect by making all keys first color in array.
- try
- {
- refreshKeyGrid.Set(ToColoreCol(toColor[0]));
- Keyboard.SetCustomAsync(refreshKeyGrid);
- Thread.Sleep(500);
- }
- catch (Exception ex)
- {
- CheckRazerEx(ex);
- }
- //Begin loop
- while (true)
- {
- //Check if cancelled
- if (cts.IsCancellationRequested) break;
- //New RNG variable
- var rnd = new Random();
- //Clear color dictionary
- colorFaderDict.Clear();
- //Loop through key ids passed in (this effect only applies to these keys)
- foreach (var key in regions)
- {
- //Check if cancelled
- if (cts.IsCancellationRequested) return;
- //Verify if defined key is valid according to Colore
- if (Enum.IsDefined(typeof(Key), key))
- {
- var rndCol = Color.Black;
- //Convert key id to colore accepted value
- var keyid = (Key)Enum.Parse(typeof(Key), key);
- //Set a new random color which is not the previous color
- do
- {
- rndCol = toColor[rnd.Next(toColor.Length)];
- } while (FromColoreCol(Keyboard[keyid]).ToArgb() == rndCol.ToArgb());
- //Store colors in dictionary for use later.
- if (!colorFaderDict.ContainsKey(key))
- {
- colorFaderDict.Add(key,
- new ColorFader(FromColoreCol(refreshKeyGrid[keyid]), rndCol, interval));
- }
- }
- }
- //Begin new task to not block calling thread
- Task t = Task.Factory.StartNew(async () =>
- {
- //randomize order of keys
- var _regions = regions.OrderBy(x => rnd.Next()).ToArray();
- //loop through each key
- foreach (var key in _regions)
- {
- //Check if cancelled
- if (cts.IsCancellationRequested) return;
- //Check valid values
- if (!Enum.IsDefined(typeof(Key), key)) continue;
- if (!colorFaderDict.ContainsKey(key)) continue;
- //Loop through colors in dictionary
- foreach (var color in colorFaderDict[key].Fade())
- {
- //Check if cancelled
- if (cts.IsCancellationRequested) return;
- if (Enum.IsDefined(typeof(Key), key))
- {
- //Set key to random color from dictionary
- var keyid = (Key)Enum.Parse(typeof(Key), key);
- var rzCol = ToColoreCol(color);
- try
- {
- //check color is not already set to prevent duplicates
- if (_effectGrid[keyid].Value != rzCol)
- {
- _effectGrid[keyid] = rzCol;
- }
- }
- catch (Exception ex)
- {
- CheckRazerEx(ex);
- }
- }
- }
- //set keyboard to current effect step
- await Keyboard.SetCustomAsync(_effectGrid);
- //Pause task according to speed value passed in
- Thread.Sleep(speed);
- }
- });
- //Pause task after loop is complete
- Thread.Sleep(colorFaderDict.Count * speed);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement