-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathForm1.cs
1010 lines (871 loc) · 39.3 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Ref: https://www.youtube.com/watch?v=6iv7y5v1e0A - C# Capture
// Ref: https://www.youtube.com/watch?v=Q63GV5tnXN0 - Crop Bitmap
// Ref: https://www.c-sharpcorner.com/UploadFile/2d2d83/how-to-capture-a-screen-using-C-Sharp/ - Capture Screen
using ChromaSDK;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Threading;
using System.Windows.Forms;
using static ChromaSDK.ChromaAnimationAPI;
namespace WinForm_RobloxChromaMod
{
public partial class Form1 : Form
{
bool _mWaitForExit = true;
bool _mMouseDown = false;
bool _mMouseOver = false;
public static Point _sMouseMoveStart = Point.Empty;
public static Point _sMouseMoveEnd = Point.Empty;
public static Point _sMouseMoveOffset = Point.Empty;
public static int _sScreenIndex = -1;
Image _mCaptureImage = null;
FChromaSDKScene _mScene = null;
Dictionary<string, int> _mEffectIndexes = new Dictionary<string, int>();
bool _mExtended = true; // extended keyboars support
int _mAmbientColor = 0;
string _mPreviousEffect = string.Empty;
const string ANIMATION_IDLE = "Animations/Idle";
const string ANIMATION_DEAD = "Animations/Dead";
const string ANIMATION_CLIMBING = "Animations/Climbing";
const string ANIMATION_FLYING = "Animations/Flying";
const string ANIMATION_RUNNING = "Animations/Running";
const string ANIMATION_SEATED = "Animations/Seated";
const string ANIMATION_SWIMMING = "Animations/Swimming";
const string ANIMATION_JUMPING = "Animations/Jumping";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// list all monitors
int indexScreen = 1;
foreach (Screen screen in Screen.AllScreens)
{
string deviceName = string.Format("{0}: {1}", indexScreen, screen.DeviceName);
_mCboMonitors.Items.Add(deviceName);
++indexScreen;
}
_mCboMonitors.SelectedIndexChanged -= CboItemChanged;
try
{
_mCboMonitors.SelectedIndex = _sScreenIndex + 1;
}
catch
{
_mCboMonitors.SelectedIndex = 0;
}
_mCboMonitors.SelectedIndexChanged += CboItemChanged;
// create capture bitmap
_mCaptureImage = new Bitmap(_mPictureBox.Width, _mPictureBox.Height, PixelFormat.Format24bppRgb);
// setup scene
_mScene = new FChromaSDKScene();
const int SPEED_MULTIPLIER = 1;
FChromaSDKSceneEffect effect;
for (int animation = 1; animation <= 15; ++animation)
{
effect = new FChromaSDKSceneEffect();
effect._mAnimation = string.Format("Animations/Effect{0}", animation);
effect._mSpeed = SPEED_MULTIPLIER;
effect._mBlend = EChromaSDKSceneBlend.SB_None;
effect._mState = false;
effect._mMode = EChromaSDKSceneMode.SM_Add;
_mScene._mEffects.Add(effect);
_mEffectIndexes[effect._mAnimation] = (int)_mScene._mEffects.Count - 1;
}
// idle
effect = new FChromaSDKSceneEffect();
effect._mAnimation = ANIMATION_IDLE;
effect._mSpeed = SPEED_MULTIPLIER;
effect._mBlend = EChromaSDKSceneBlend.SB_None;
effect._mState = false;
effect._mMode = EChromaSDKSceneMode.SM_Add;
_mScene._mEffects.Add(effect);
_mEffectIndexes[effect._mAnimation] = (int)_mScene._mEffects.Count - 1;
// climbing
effect = new FChromaSDKSceneEffect();
effect._mAnimation = ANIMATION_CLIMBING;
effect._mSpeed = SPEED_MULTIPLIER;
effect._mBlend = EChromaSDKSceneBlend.SB_None;
effect._mState = false;
effect._mMode = EChromaSDKSceneMode.SM_Add;
_mScene._mEffects.Add(effect);
_mEffectIndexes[effect._mAnimation] = (int)_mScene._mEffects.Count - 1;
// flying
effect = new FChromaSDKSceneEffect();
effect._mAnimation = ANIMATION_FLYING;
effect._mSpeed = SPEED_MULTIPLIER;
effect._mBlend = EChromaSDKSceneBlend.SB_None;
effect._mState = false;
effect._mMode = EChromaSDKSceneMode.SM_Add;
_mScene._mEffects.Add(effect);
_mEffectIndexes[effect._mAnimation] = (int)_mScene._mEffects.Count - 1;
// running
effect = new FChromaSDKSceneEffect();
effect._mAnimation = ANIMATION_RUNNING;
effect._mSpeed = SPEED_MULTIPLIER;
effect._mBlend = EChromaSDKSceneBlend.SB_None;
effect._mState = false;
effect._mMode = EChromaSDKSceneMode.SM_Add;
_mScene._mEffects.Add(effect);
_mEffectIndexes[effect._mAnimation] = (int)_mScene._mEffects.Count - 1;
// seated
effect = new FChromaSDKSceneEffect();
effect._mAnimation = ANIMATION_SEATED;
effect._mSpeed = SPEED_MULTIPLIER;
effect._mBlend = EChromaSDKSceneBlend.SB_None;
effect._mState = false;
effect._mMode = EChromaSDKSceneMode.SM_Add;
_mScene._mEffects.Add(effect);
_mEffectIndexes[effect._mAnimation] = (int)_mScene._mEffects.Count - 1;
// swimming
effect = new FChromaSDKSceneEffect();
effect._mAnimation = ANIMATION_SWIMMING;
effect._mSpeed = SPEED_MULTIPLIER;
effect._mBlend = EChromaSDKSceneBlend.SB_None;
effect._mState = false;
effect._mMode = EChromaSDKSceneMode.SM_Add;
_mScene._mEffects.Add(effect);
_mEffectIndexes[effect._mAnimation] = (int)_mScene._mEffects.Count - 1;
// jumping
effect = new FChromaSDKSceneEffect();
effect._mAnimation = ANIMATION_JUMPING;
effect._mSpeed = SPEED_MULTIPLIER;
effect._mBlend = EChromaSDKSceneBlend.SB_None;
effect._mState = false;
effect._mMode = EChromaSDKSceneMode.SM_Add;
_mScene._mEffects.Add(effect);
_mEffectIndexes[effect._mAnimation] = (int)_mScene._mEffects.Count - 1;
// dead
effect = new FChromaSDKSceneEffect();
effect._mAnimation = ANIMATION_DEAD;
effect._mSpeed = SPEED_MULTIPLIER;
effect._mBlend = EChromaSDKSceneBlend.SB_None;
effect._mState = false;
effect._mMode = EChromaSDKSceneMode.SM_Add;
_mScene._mEffects.Add(effect);
_mEffectIndexes[effect._mAnimation] = (int)_mScene._mEffects.Count - 1;
// Start game loop
ThreadStart ts = new ThreadStart(GameLoop);
Thread thread = new Thread(ts);
thread.Start();
}
private void Form1_FormClosed(object sender, System.Windows.Forms.FormClosedEventArgs e)
{
_mWaitForExit = false;
if (_mCaptureImage != null)
{
_mCaptureImage.Dispose();
}
}
#region Input Events
private void PictureMouseDown(object sender, MouseEventArgs e)
{
if (!_mMouseDown && _mMouseOver)
{
_mMouseDown = true;
_sMouseMoveStart = new Point(e.X + _sMouseMoveOffset.X, e.Y + _sMouseMoveOffset.Y);
_sMouseMoveOffset = Point.Empty;
}
}
private void PictureMouseUp(object sender, MouseEventArgs e)
{
if (_mMouseDown)
{
_sMouseMoveEnd = new Point(e.X + _sMouseMoveOffset.X, e.Y + _sMouseMoveOffset.Y);
_sMouseMoveOffset = new Point(_sMouseMoveStart.X - _sMouseMoveEnd.X, _sMouseMoveStart.Y - _sMouseMoveEnd.Y);
}
_mMouseDown = false;
}
private void PictureMouseMove(object sender, MouseEventArgs e)
{
if (_mMouseDown)
{
_sMouseMoveEnd = new Point(e.X + _sMouseMoveOffset.X, e.Y + _sMouseMoveOffset.Y);
}
}
private void PictureMouseEnter(object sender, EventArgs e)
{
_mMouseOver = true;
}
private void PictureMouseLeave(object sender, EventArgs e)
{
_mMouseOver = false;
}
#endregion Input Events
private void SetEffect(string effectName)
{
if (effectName != _mPreviousEffect)
{
_mPreviousEffect = effectName;
_mLabelButtonEffect.Text = effectName;
for (int animation = 1; animation <= 15; ++animation)
{
string animationName = string.Format("Animations/Effect{0}", animation);
int effectIndex = _mEffectIndexes[animationName];
_mScene._mEffects[effectIndex]._mState = false;
}
switch (effectName)
{
case "Effect1":
case "Effect2":
case "Effect3":
case "Effect4":
case "Effect5":
case "Effect6":
case "Effect7":
case "Effect8":
case "Effect9":
case "Effect10":
case "Effect11":
case "Effect12":
case "Effect13":
case "Effect14":
case "Effect15":
{
string animationName = string.Format("Animations/{0}", effectName);
int effectIndex = _mEffectIndexes[animationName];
_mScene._mEffects[effectIndex]._mState = true;
}
break;
}
}
}
#region Player State
private Dictionary<string, bool> _mPlayerState = new Dictionary<string, bool>();
private bool GetPlayerState(string state)
{
if (!_mPlayerState.ContainsKey(state))
{
return false;
}
return _mPlayerState[state];
}
private void SetPlayerState(string state, bool flag)
{
_mPlayerState[state] = flag;
}
#endregion
private void _mCaptureTimer_Tick(object sender, EventArgs e)
{
Graphics captureGraphics = null;
Graphics g = null;
Bitmap bmp = null;
Brush brush = null;
Pen pen = null;
try
{
int selectedIndex = _mCboMonitors.SelectedIndex;
if (selectedIndex < 1 || (selectedIndex - 1) >= Screen.AllScreens.Length)
{
return; // skip capture
}
// get the selected screen
Screen screen = Screen.AllScreens[selectedIndex - 1];
// capture from screen
Rectangle captureRectangle = screen.Bounds;
// create graphics
captureGraphics = Graphics.FromImage(_mCaptureImage);
// copy pixels from screen
captureGraphics.CopyFromScreen(
captureRectangle.Left + _sMouseMoveStart.X - _sMouseMoveEnd.X,
captureRectangle.Top + _sMouseMoveStart.Y - _sMouseMoveEnd.Y, 0, 0, captureRectangle.Size);
// find the center of the picture box
int x = Math.Max(0, _mPictureBox.Width / 2);
int y = Math.Max(0, _mPictureBox.Height / 2);
#region Draw Capture Marker
brush = new SolidBrush(Color.FromKnownColor(KnownColor.White));
const int markerBorder = 3;
pen = new Pen(brush, markerBorder);
const int markerSize = 4;
captureGraphics.DrawRectangle(pen, x - markerSize, y - markerSize, markerSize * 2, markerSize * 2);
#endregion Draw Capture Marker
// do some cropping
g = _mPictureBox.CreateGraphics();
Rectangle rectCropArea = new Rectangle(
0,
0,
_mPictureBox.Width,
_mPictureBox.Height);
g.DrawImage(_mCaptureImage, 0, 0);
// read the center pixel
bmp = new Bitmap(_mCaptureImage);
Color color = bmp.GetPixel(x, y);
_mButtonColor.Text = string.Format("{0},{1},{2}", color.R, color.G, color.B);
#region Button Effects
if (MatchColorRed(color, 1))
{
SetEffect("Effect1");
}
else if (MatchColorRed(color, 2))
{
SetEffect("Effect2");
}
else if (MatchColorRed(color, 3))
{
SetEffect("Effect3");
}
else if (MatchColorRed(color, 4))
{
SetEffect("Effect4");
}
else if (MatchColorRed(color, 5))
{
SetEffect("Effect5");
}
else if (MatchColorRed(color, 6))
{
SetEffect("Effect6");
}
else if (MatchColorRed(color, 7))
{
SetEffect("Effect7");
}
else if (MatchColorRed(color, 8))
{
SetEffect("Effect8");
}
else if (MatchColorRed(color, 9))
{
SetEffect("Effect9");
}
else if (MatchColorRed(color, 10))
{
SetEffect("Effect10");
}
else if (MatchColorRed(color, 11))
{
SetEffect("Effect11");
}
else if (MatchColorRed(color, 12))
{
SetEffect("Effect12");
}
else if (MatchColorRed(color, 13))
{
SetEffect("Effect13");
}
else if (MatchColorRed(color, 14))
{
SetEffect("Effect14");
}
else if (MatchColorRed(color, 15))
{
SetEffect("Effect15");
}
else
{
SetEffect("None");
}
#endregion Button Effects
const byte MASK_IDLE = 0x0;
const byte MASK_DEAD = 0x1;
const byte MASK_CLIMBING = 0x1 << 1;
const byte MASK_JUMPING = 0x1 << 2;
const byte MASK_FLYING = 0x1 << 3;
const byte MASK_RUNNING = 0x1 << 4;
const byte MASK_SWIMMING = 0x1 << 5;
const byte MASK_SEATED = 0x1 << 6;
_mScene._mEffects[_mEffectIndexes[ANIMATION_DEAD]]._mState =
MatchColorGeeenMask(color, MASK_DEAD);
_mScene._mEffects[_mEffectIndexes[ANIMATION_CLIMBING]]._mState =
MatchColorGeeenMask(color, MASK_CLIMBING);
_mScene._mEffects[_mEffectIndexes[ANIMATION_RUNNING]]._mState =
MatchColorGeeenMask(color, MASK_RUNNING);
_mScene._mEffects[_mEffectIndexes[ANIMATION_FLYING]]._mState =
MatchColorGeeenMask(color, MASK_FLYING);
_mScene._mEffects[_mEffectIndexes[ANIMATION_SEATED]]._mState =
MatchColorGeeenMask(color, MASK_SEATED);
_mScene._mEffects[_mEffectIndexes[ANIMATION_SWIMMING]]._mState =
MatchColorGeeenMask(color, MASK_SWIMMING);
_mScene._mEffects[_mEffectIndexes[ANIMATION_JUMPING]]._mState =
MatchColorGeeenMask(color, MASK_JUMPING);
_mScene._mEffects[_mEffectIndexes[ANIMATION_IDLE]]._mState = color.G == MASK_IDLE;
_mLblDebugDead.Text = string.Format("Dead: {0}", _mScene._mEffects[_mEffectIndexes[ANIMATION_DEAD]]._mState);
_mLblDebugClimbing.Text = string.Format("Climbing: {0}", _mScene._mEffects[_mEffectIndexes[ANIMATION_CLIMBING]]._mState);
_mLblDebugJumping.Text = string.Format("Jumping: {0}", _mScene._mEffects[_mEffectIndexes[ANIMATION_JUMPING]]._mState);
_mLblDebugFlying.Text = string.Format("Flying: {0}", _mScene._mEffects[_mEffectIndexes[ANIMATION_FLYING]]._mState);
_mLblDebugRunning.Text = string.Format("Running: {0}", _mScene._mEffects[_mEffectIndexes[ANIMATION_RUNNING]]._mState);
_mLblDebugSeated.Text = string.Format("Seated: {0}", _mScene._mEffects[_mEffectIndexes[ANIMATION_SEATED]]._mState);
_mLblDebugSwimming.Text = string.Format("Swimming: {0}", _mScene._mEffects[_mEffectIndexes[ANIMATION_SWIMMING]]._mState);
}
catch (Exception ex)
{
Console.Error.WriteLine("Failed to capture image exception: {0}", ex);
}
finally
{
// cleanup
if (pen != null)
{
pen.Dispose();
}
if (brush != null)
{
brush.Dispose();
}
if (bmp != null)
{
bmp.Dispose();
}
if (g != null)
{
g.Dispose();
}
if (captureGraphics != null)
{
captureGraphics.Dispose();
}
}
}
#region Helper methods
private bool MatchColor(Color color, byte red, byte green, byte blue)
{
return (color.R == red && color.G == green && color.B == blue);
}
private bool MatchColorRed(Color color, byte red)
{
return (color.R == red);
}
private bool MatchColorGeeenMask(Color color, byte mask)
{
return ((color.G & mask) == mask);
}
#endregion Helper methods
#region Blending
int HIBYTE(int a)
{
return (a & 0xFF00) >> 8;
}
int LOBYTE(int a)
{
return (a & 0x00FF);
}
int GetKeyColorIndex(int row, int column)
{
return ChromaAnimationAPI.GetMaxColumn(Device2D.Keyboard) * row + column;
}
void SetKeyColor(int[] colors, int rzkey, int color)
{
const int customFlag = 1 << 24;
int row = HIBYTE(rzkey);
int column = LOBYTE(rzkey);
colors[GetKeyColorIndex(row, column)] = color | customFlag;
}
void SetKeyColorRGB(int[] colors, int rzkey, int red, int green, int blue)
{
SetKeyColor(colors, rzkey, ChromaAnimationAPI.GetRGB(red, green, blue));
}
int GetColorArraySize1D(Device1D device)
{
int maxLeds = ChromaAnimationAPI.GetMaxLeds(device);
return maxLeds;
}
int GetColorArraySize2D(Device2D device)
{
int maxRow = ChromaAnimationAPI.GetMaxRow(device);
int maxColumn = ChromaAnimationAPI.GetMaxColumn(device);
return maxRow * maxColumn;
}
int MultiplyColor(int color1, int color2)
{
int redColor1 = color1 & 0xFF;
int greenColor1 = (color1 >> 8) & 0xFF;
int blueColor1 = (color1 >> 16) & 0xFF;
int redColor2 = color2 & 0xFF;
int greenColor2 = (color2 >> 8) & 0xFF;
int blueColor2 = (color2 >> 16) & 0xFF;
int red = (int)Math.Floor(255 * ((redColor1 / 255.0f) * (redColor2 / 255.0f)));
int green = (int)Math.Floor(255 * ((greenColor1 / 255.0f) * (greenColor2 / 255.0f)));
int blue = (int)Math.Floor(255 * ((blueColor1 / 255.0f) * (blueColor2 / 255.0f)));
return ChromaAnimationAPI.GetRGB(red, green, blue);
}
int AverageColor(int color1, int color2)
{
return ChromaAnimationAPI.LerpColor(color1, color2, 0.5f);
}
int AddColor(int color1, int color2)
{
int redColor1 = color1 & 0xFF;
int greenColor1 = (color1 >> 8) & 0xFF;
int blueColor1 = (color1 >> 16) & 0xFF;
int redColor2 = color2 & 0xFF;
int greenColor2 = (color2 >> 8) & 0xFF;
int blueColor2 = (color2 >> 16) & 0xFF;
int red = Math.Min(redColor1 + redColor2, 255) & 0xFF;
int green = Math.Min(greenColor1 + greenColor2, 255) & 0xFF;
int blue = Math.Min(blueColor1 + blueColor2, 255) & 0xFF;
return ChromaAnimationAPI.GetRGB(red, green, blue);
}
int SubtractColor(int color1, int color2)
{
int redColor1 = color1 & 0xFF;
int greenColor1 = (color1 >> 8) & 0xFF;
int blueColor1 = (color1 >> 16) & 0xFF;
int redColor2 = color2 & 0xFF;
int greenColor2 = (color2 >> 8) & 0xFF;
int blueColor2 = (color2 >> 16) & 0xFF;
int red = Math.Max(redColor1 - redColor2, 0) & 0xFF;
int green = Math.Max(greenColor1 - greenColor2, 0) & 0xFF;
int blue = Math.Max(blueColor1 - blueColor2, 0) & 0xFF;
return ChromaAnimationAPI.GetRGB(red, green, blue);
}
int MaxColor(int color1, int color2)
{
int redColor1 = color1 & 0xFF;
int greenColor1 = (color1 >> 8) & 0xFF;
int blueColor1 = (color1 >> 16) & 0xFF;
int redColor2 = color2 & 0xFF;
int greenColor2 = (color2 >> 8) & 0xFF;
int blueColor2 = (color2 >> 16) & 0xFF;
int red = Math.Max(redColor1, redColor2) & 0xFF;
int green = Math.Max(greenColor1, greenColor2) & 0xFF;
int blue = Math.Max(blueColor1, blueColor2) & 0xFF;
return ChromaAnimationAPI.GetRGB(red, green, blue);
}
int MinColor(int color1, int color2)
{
int redColor1 = color1 & 0xFF;
int greenColor1 = (color1 >> 8) & 0xFF;
int blueColor1 = (color1 >> 16) & 0xFF;
int redColor2 = color2 & 0xFF;
int greenColor2 = (color2 >> 8) & 0xFF;
int blueColor2 = (color2 >> 16) & 0xFF;
int red = Math.Min(redColor1, redColor2) & 0xFF;
int green = Math.Min(greenColor1, greenColor2) & 0xFF;
int blue = Math.Min(blueColor1, blueColor2) & 0xFF;
return ChromaAnimationAPI.GetRGB(red, green, blue);
}
int InvertColor(int color)
{
int red = 255 - (color & 0xFF);
int green = 255 - ((color >> 8) & 0xFF);
int blue = 255 - ((color >> 16) & 0xFF);
return ChromaAnimationAPI.GetRGB(red, green, blue);
}
int MultiplyNonZeroTargetColorLerp(int color1, int color2, int inputColor)
{
if (inputColor == 0)
{
return inputColor;
}
float red = (inputColor & 0xFF) / 255.0f;
float green = ((inputColor & 0xFF00) >> 8) / 255.0f;
float blue = ((inputColor & 0xFF0000) >> 16) / 255.0f;
float t = (red + green + blue) / 3.0f;
return ChromaAnimationAPI.LerpColor(color1, color2, t);
}
int Thresh(int color1, int color2, int inputColor)
{
float red = (inputColor & 0xFF) / 255.0f;
float green = ((inputColor & 0xFF00) >> 8) / 255.0f;
float blue = ((inputColor & 0xFF0000) >> 16) / 255.0f;
float t = (red + green + blue) / 3.0f;
if (t == 0.0)
{
return 0;
}
if (t < 0.5)
{
return color1;
}
else
{
return color2;
}
}
void BlendAnimation1D(FChromaSDKSceneEffect effect, FChromaSDKDeviceFrameIndex deviceFrameIndex, int device, Device1D device1d, string animationName,
int[] colors, int[] tempColors)
{
int size = GetColorArraySize1D(device1d);
int frameId = deviceFrameIndex._mFrameIndex[device];
int frameCount = ChromaAnimationAPI.GetFrameCountName(animationName);
if (frameId < frameCount)
{
//cout << animationName << ": " << (1 + frameId) << " of " << frameCount << endl;
float duration;
ChromaAnimationAPI.GetFrameName(animationName, frameId, out duration, tempColors, size, null, 0);
for (int i = 0; i < size; ++i)
{
int color1 = colors[i]; //target
int tempColor = tempColors[i]; //source
// BLEND
int color2;
switch (effect._mBlend)
{
case EChromaSDKSceneBlend.SB_None:
color2 = tempColor; //source
break;
case EChromaSDKSceneBlend.SB_Invert:
if (tempColor != 0) //source
{
color2 = InvertColor(tempColor); //source inverted
}
else
{
color2 = 0;
}
break;
case EChromaSDKSceneBlend.SB_Threshold:
color2 = Thresh(effect._mPrimaryColor, effect._mSecondaryColor, tempColor); //source
break;
case EChromaSDKSceneBlend.SB_Lerp:
default:
color2 = MultiplyNonZeroTargetColorLerp(effect._mPrimaryColor, effect._mSecondaryColor, tempColor); //source
break;
}
// MODE
switch (effect._mMode)
{
case EChromaSDKSceneMode.SM_Max:
colors[i] = MaxColor(color1, color2);
break;
case EChromaSDKSceneMode.SM_Min:
colors[i] = MinColor(color1, color2);
break;
case EChromaSDKSceneMode.SM_Average:
colors[i] = AverageColor(color1, color2);
break;
case EChromaSDKSceneMode.SM_Multiply:
colors[i] = MultiplyColor(color1, color2);
break;
case EChromaSDKSceneMode.SM_Add:
colors[i] = AddColor(color1, color2);
break;
case EChromaSDKSceneMode.SM_Subtract:
colors[i] = SubtractColor(color1, color2);
break;
case EChromaSDKSceneMode.SM_Replace:
default:
if (color2 != 0)
{
colors[i] = color2;
}
break;
}
}
deviceFrameIndex._mFrameIndex[device] = (frameId + frameCount + effect._mSpeed) % frameCount;
}
}
void BlendAnimation2D(FChromaSDKSceneEffect effect, FChromaSDKDeviceFrameIndex deviceFrameIndex, int device, Device2D device2D, string animationName,
int[] colors, int[] tempColors)
{
int size = GetColorArraySize2D(device2D);
int frameId = deviceFrameIndex._mFrameIndex[device];
int frameCount = ChromaAnimationAPI.GetFrameCountName(animationName);
if (frameId < frameCount)
{
//cout << animationName << ": " << (1 + frameId) << " of " << frameCount << endl;
float duration;
ChromaAnimationAPI.GetFrameName(animationName, frameId, out duration, tempColors, size, null, 0);
for (int i = 0; i < size; ++i)
{
int color1 = colors[i]; //target
int tempColor = tempColors[i]; //source
// BLEND
int color2;
switch (effect._mBlend)
{
case EChromaSDKSceneBlend.SB_None:
color2 = tempColor; //source
break;
case EChromaSDKSceneBlend.SB_Invert:
if (tempColor != 0) //source
{
color2 = InvertColor(tempColor); //source inverted
}
else
{
color2 = 0;
}
break;
case EChromaSDKSceneBlend.SB_Threshold:
color2 = Thresh(effect._mPrimaryColor, effect._mSecondaryColor, tempColor); //source
break;
case EChromaSDKSceneBlend.SB_Lerp:
default:
color2 = MultiplyNonZeroTargetColorLerp(effect._mPrimaryColor, effect._mSecondaryColor, tempColor); //source
break;
}
// MODE
switch (effect._mMode)
{
case EChromaSDKSceneMode.SM_Max:
colors[i] = MaxColor(color1, color2);
break;
case EChromaSDKSceneMode.SM_Min:
colors[i] = MinColor(color1, color2);
break;
case EChromaSDKSceneMode.SM_Average:
colors[i] = AverageColor(color1, color2);
break;
case EChromaSDKSceneMode.SM_Multiply:
colors[i] = MultiplyColor(color1, color2);
break;
case EChromaSDKSceneMode.SM_Add:
colors[i] = AddColor(color1, color2);
break;
case EChromaSDKSceneMode.SM_Subtract:
colors[i] = SubtractColor(color1, color2);
break;
case EChromaSDKSceneMode.SM_Replace:
default:
if (color2 != 0)
{
colors[i] = color2;
}
break;
}
}
deviceFrameIndex._mFrameIndex[device] = (frameId + frameCount + effect._mSpeed) % frameCount;
}
}
void BlendAnimations(FChromaSDKScene scene,
int[] colorsChromaLink, int[] tempColorsChromaLink,
int[] colorsHeadset, int[] tempColorsHeadset,
int[] colorsKeyboard, int[] tempColorsKeyboard,
int[] colorsKeyboardExtended, int[] tempColorsKeyboardExtended,
int[] colorsKeypad, int[] tempColorsKeypad,
int[] colorsMouse, int[] tempColorsMouse,
int[] colorsMousepad, int[] tempColorsMousepad)
{
// blend active animations
List<FChromaSDKSceneEffect> effects = scene._mEffects;
foreach (FChromaSDKSceneEffect effect in effects)
{
if (effect._mState)
{
FChromaSDKDeviceFrameIndex deviceFrameIndex = effect._mFrameIndex;
//iterate all device types
for (int d = (int)Device.ChromaLink; d < (int)Device.MAX; ++d)
{
string animationName = effect._mAnimation;
switch ((Device)d)
{
case Device.ChromaLink:
animationName += "_ChromaLink.chroma";
BlendAnimation1D(effect, deviceFrameIndex, d, Device1D.ChromaLink, animationName, colorsChromaLink, tempColorsChromaLink);
break;
case Device.Headset:
animationName += "_Headset.chroma";
BlendAnimation1D(effect, deviceFrameIndex, d, Device1D.Headset, animationName, colorsHeadset, tempColorsHeadset);
break;
case Device.Keyboard:
animationName += "_Keyboard.chroma";
BlendAnimation2D(effect, deviceFrameIndex, d, Device2D.Keyboard, animationName, colorsKeyboard, tempColorsKeyboard);
break;
case Device.KeyboardExtended:
animationName += "_KeyboardExtended.chroma";
BlendAnimation2D(effect, deviceFrameIndex, d, Device2D.KeyboardExtended, animationName, colorsKeyboardExtended, tempColorsKeyboardExtended);
break;
case Device.Keypad:
animationName += "_Keypad.chroma";
BlendAnimation2D(effect, deviceFrameIndex, d, Device2D.Keypad, animationName, colorsKeypad, tempColorsKeypad);
break;
case Device.Mouse:
animationName += "_Mouse.chroma";
BlendAnimation2D(effect, deviceFrameIndex, d, Device2D.Mouse, animationName, colorsMouse, tempColorsMouse);
break;
case Device.Mousepad:
animationName += "_Mousepad.chroma";
BlendAnimation1D(effect, deviceFrameIndex, d, Device1D.Mousepad, animationName, colorsMousepad, tempColorsMousepad);
break;
}
}
}
}
}
public void SetStaticColor(int[] colors, int color)
{
for (int i = 0; i < colors.Length; ++i)
{
colors[i] = color;
}
}
#endregion Blending
public void GameLoop()
{
int sizeChromaLink = GetColorArraySize1D(Device1D.ChromaLink);
int sizeHeadset = GetColorArraySize1D(Device1D.Headset);
int sizeKeyboard = GetColorArraySize2D(Device2D.Keyboard);
int sizeKeyboardExtended = GetColorArraySize2D(Device2D.KeyboardExtended);
int sizeKeypad = GetColorArraySize2D(Device2D.Keypad);
int sizeMouse = GetColorArraySize2D(Device2D.Mouse);
int sizeMousepad = GetColorArraySize1D(Device1D.Mousepad);
int[] colorsChromaLink = new int[sizeChromaLink];
int[] colorsHeadset = new int[sizeHeadset];
int[] colorsKeyboard = new int[sizeKeyboard];
int[] colorsKeyboardExtended = new int[sizeKeyboardExtended];
int[] colorsKeyboardKeys = new int[sizeKeyboard];
int[] colorsKeypad = new int[sizeKeypad];
int[] colorsMouse = new int[sizeMouse];
int[] colorsMousepad = new int[sizeMousepad];
int[] tempColorsChromaLink = new int[sizeChromaLink];
int[] tempColorsHeadset = new int[sizeHeadset];
int[] tempColorsKeyboard = new int[sizeKeyboard];
int[] tempColorsKeyboardExtended = new int[sizeKeyboardExtended];
int[] tempColorsKeypad = new int[sizeKeypad];
int[] tempColorsMouse = new int[sizeMouse];
int[] tempColorsMousepad = new int[sizeMousepad];
uint timeMS = 0;
while (_mWaitForExit)
{
// start with a blank frame
SetStaticColor(colorsChromaLink, _mAmbientColor);
SetStaticColor(colorsHeadset, _mAmbientColor);
if (_mExtended)
{
SetStaticColor(colorsKeyboardExtended, _mAmbientColor);
}
else
{
SetStaticColor(colorsKeyboard, _mAmbientColor);
}
SetStaticColor(colorsKeyboardKeys, _mAmbientColor);
SetStaticColor(colorsKeypad, _mAmbientColor);
SetStaticColor(colorsMouse, _mAmbientColor);
SetStaticColor(colorsMousepad, _mAmbientColor);
BlendAnimations(_mScene,
colorsChromaLink, tempColorsChromaLink,
colorsHeadset, tempColorsHeadset,
colorsKeyboard, tempColorsKeyboard,
colorsKeyboardExtended, tempColorsKeyboardExtended,
colorsKeypad, tempColorsKeypad,
colorsMouse, tempColorsMouse,
colorsMousepad, tempColorsMousepad);
#region Setup Hotkeys
SetKeyColorRGB(colorsKeyboardKeys, (int)Keyboard.RZKEY.RZKEY_W, 255, 255, 0);
SetKeyColorRGB(colorsKeyboardKeys, (int)Keyboard.RZKEY.RZKEY_A, 255, 255, 0);
SetKeyColorRGB(colorsKeyboardKeys, (int)Keyboard.RZKEY.RZKEY_S, 255, 255, 0);
SetKeyColorRGB(colorsKeyboardKeys, (int)Keyboard.RZKEY.RZKEY_D, 255, 255, 0);
#endregion Setup Hotkeys
ChromaAnimationAPI.SetEffectCustom1D((int)Device1D.ChromaLink, colorsChromaLink);
ChromaAnimationAPI.SetEffectCustom1D((int)Device1D.Headset, colorsHeadset);
ChromaAnimationAPI.SetEffectCustom1D((int)Device1D.Mousepad, colorsMousepad);
if (_mExtended)
{
ChromaAnimationAPI.SetCustomColorFlag2D((int)Device2D.KeyboardExtended, colorsKeyboardExtended);
ChromaAnimationAPI.SetEffectKeyboardCustom2D((int)Device2D.KeyboardExtended, colorsKeyboardExtended, colorsKeyboardKeys);
}
else
{
ChromaAnimationAPI.SetCustomColorFlag2D((int)Device2D.Keyboard, colorsKeyboard);
ChromaAnimationAPI.SetEffectKeyboardCustom2D((int)Device2D.Keyboard, colorsKeyboard, colorsKeyboardKeys);
}
ChromaAnimationAPI.SetEffectCustom2D((int)Device2D.Keypad, colorsKeypad);
ChromaAnimationAPI.SetEffectCustom2D((int)Device2D.Mouse, colorsMouse);
Thread.Sleep(33); //30 FPS
timeMS += 33;
}
}