Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.58 KB | None | 0 0
  1. public void ParticleEffect(Color[] toColor, string[] regions, uint interval, CancellationTokenSource cts, int speed = 50)
  2.         {
  3.             //Check if cancelled
  4.             if (cts.IsCancellationRequested) return;
  5.  
  6.             //Setup Custom Effect
  7.             var refreshKeyGrid = KeyboardCustom.Create();
  8.             refreshKeyGrid = _keyboardGrid;
  9.  
  10.             var _effectGrid = KeyboardCustom.Create();
  11.             _effectGrid = refreshKeyGrid;
  12.  
  13.             //Dictionary to store colors
  14.             Dictionary<string, ColorFader> colorFaderDict = new Dictionary<string, ColorFader>();
  15.  
  16.             //Setup effect by making all keys first color in array.
  17.             try
  18.             {
  19.                 refreshKeyGrid.Set(ToColoreCol(toColor[0]));
  20.                 Keyboard.SetCustomAsync(refreshKeyGrid);
  21.                 Thread.Sleep(500);
  22.             }
  23.             catch (Exception ex)
  24.             {
  25.                 CheckRazerEx(ex);
  26.             }
  27.            
  28.             //Begin loop
  29.             while (true)
  30.             {
  31.                 //Check if cancelled
  32.                 if (cts.IsCancellationRequested) break;
  33.  
  34.                 //New RNG variable
  35.                 var rnd = new Random();
  36.  
  37.                 //Clear color dictionary
  38.                 colorFaderDict.Clear();
  39.  
  40.                 //Loop through key ids passed in (this effect only applies to these keys)
  41.                 foreach (var key in regions)
  42.                 {
  43.                     //Check if cancelled
  44.                     if (cts.IsCancellationRequested) return;
  45.  
  46.                     //Verify if defined key is valid according to Colore
  47.                     if (Enum.IsDefined(typeof(Key), key))
  48.                     {
  49.                         var rndCol = Color.Black;
  50.  
  51.                         //Convert key id to colore accepted value
  52.                         var keyid = (Key)Enum.Parse(typeof(Key), key);
  53.  
  54.                         //Set a new random color which is not the previous color
  55.                         do
  56.                         {
  57.                             rndCol = toColor[rnd.Next(toColor.Length)];
  58.                         } while (FromColoreCol(Keyboard[keyid]).ToArgb() == rndCol.ToArgb());
  59.  
  60.                         //Store colors in dictionary for use later.
  61.                         if (!colorFaderDict.ContainsKey(key))
  62.                         {
  63.                             colorFaderDict.Add(key,
  64.                                 new ColorFader(FromColoreCol(refreshKeyGrid[keyid]), rndCol, interval));
  65.                         }
  66.  
  67.                     }
  68.                 }
  69.  
  70.                 //Begin new task to not block calling thread
  71.                 Task t = Task.Factory.StartNew(async () =>
  72.                 {
  73.                     //randomize order of keys
  74.                     var _regions = regions.OrderBy(x => rnd.Next()).ToArray();
  75.  
  76.                     //loop through each key
  77.                     foreach (var key in _regions)
  78.                     {
  79.                         //Check if cancelled
  80.                         if (cts.IsCancellationRequested) return;
  81.  
  82.                         //Check valid values
  83.                         if (!Enum.IsDefined(typeof(Key), key)) continue;
  84.                         if (!colorFaderDict.ContainsKey(key)) continue;
  85.  
  86.                         //Loop through colors in dictionary
  87.                         foreach (var color in colorFaderDict[key].Fade())
  88.                         {
  89.                             //Check if cancelled
  90.                             if (cts.IsCancellationRequested) return;
  91.                             if (Enum.IsDefined(typeof(Key), key))
  92.                             {
  93.                                 //Set key to random color from dictionary
  94.                                 var keyid = (Key)Enum.Parse(typeof(Key), key);
  95.                                 var rzCol = ToColoreCol(color);
  96.  
  97.                                 try
  98.                                 {
  99.                                     //check color is not already set to prevent duplicates
  100.                                     if (_effectGrid[keyid].Value != rzCol)
  101.                                     {
  102.                                         _effectGrid[keyid] = rzCol;
  103.                                     }
  104.  
  105.                                 }
  106.                                 catch (Exception ex)
  107.                                 {
  108.                                     CheckRazerEx(ex);
  109.                                 }
  110.                                
  111.                             }
  112.                         }
  113.  
  114.                         //set keyboard to current effect step
  115.                         await Keyboard.SetCustomAsync(_effectGrid);
  116.  
  117.                         //Pause task according to speed value passed in
  118.                         Thread.Sleep(speed);
  119.                     }
  120.                 });
  121.                
  122.                 //Pause task after loop is complete
  123.                 Thread.Sleep(colorFaderDict.Count * speed);
  124.             }
  125.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement