You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
196 lines
6.1 KiB
196 lines
6.1 KiB
using Calculator; |
|
using Colore; |
|
using Colore.Data; |
|
using Colore.Effects.Keyboard; |
|
using System; |
|
using System.Collections.Generic; |
|
using System.Linq; |
|
using System.Threading; |
|
|
|
namespace ChromaController |
|
{ |
|
internal class ColoreOutput |
|
{ |
|
private IChroma chroma; |
|
private bool _exit = false; |
|
|
|
private readonly object keyEffectsLock = new object(); |
|
|
|
private readonly List<KeyEffect> keyEffects = new List<KeyEffect>(); |
|
private readonly List<KeyEffect> keyEffectTrash = new List<KeyEffect>(); |
|
private readonly string chromaEffectPath; |
|
private readonly Color dNumberPassiveColor = Color.FromRgb(0x002800); |
|
private readonly Color numpadPassiveColor = Color.FromRgb(0x000028); |
|
|
|
private readonly Key[] numpadKeys = new Key[] |
|
{ |
|
Key.Num0, |
|
Key.Num1, |
|
Key.Num3, |
|
Key.Num2, |
|
Key.Num4, |
|
Key.Num5, |
|
Key.Num6, |
|
Key.Num7, |
|
Key.Num8, |
|
Key.Num9, |
|
Key.NumDivide, |
|
Key.NumMultiply, |
|
Key.NumSubtract, |
|
Key.NumAdd, |
|
Key.NumEnter, |
|
Key.NumDecimal |
|
}; |
|
|
|
private readonly Key[] dNumberKeys = new Key[] |
|
{ |
|
Key.D0, |
|
Key.D1, |
|
Key.D2, |
|
Key.D3, |
|
Key.D4, |
|
Key.D5, |
|
Key.D6, |
|
Key.D7, |
|
Key.D8, |
|
Key.D9, |
|
}; |
|
|
|
public ColoreOutput(string chromaEffectPath) |
|
{ |
|
this.chromaEffectPath = chromaEffectPath; |
|
} |
|
|
|
public void Start() |
|
{ |
|
chroma = chroma = ColoreProvider.CreateNativeAsync().Result; |
|
KeyboardCustom keyboardGrid = KeyboardCustom.Create(); |
|
LoadChromaEffect(chromaEffectPath, keyboardGrid); |
|
LoadInitialColors(keyboardGrid); |
|
SetKeyboardGrid(keyboardGrid); |
|
lock (keyEffectsLock) |
|
{ |
|
keyEffects.Add(new FlashKey(Key.LeftControl, Color.Black, Color.Black, 50)); |
|
} |
|
Loop(); |
|
} |
|
|
|
private void SetKeyboardGrid(KeyboardCustom keyboardGrid) |
|
{ |
|
chroma.Keyboard.SetCustomAsync(keyboardGrid); |
|
} |
|
|
|
private void LoadInitialColors(KeyboardCustom keyboardGrid) |
|
{ |
|
keyboardGrid[Key.End] = Color.Red; |
|
Array.ForEach(dNumberKeys, key => keyboardGrid[key] = dNumberPassiveColor); |
|
Array.ForEach(numpadKeys, key => keyboardGrid[key] = numpadPassiveColor); |
|
} |
|
|
|
private void LoadChromaEffect(string path, KeyboardCustom keyboardGrid) |
|
{ |
|
ChromaEffectsReader reader = new ChromaEffectsReader(); |
|
EffectRegion[] regions = reader.ReadFirstStatic(path); |
|
Array.ForEach(regions, x => x.AddToGrid(keyboardGrid)); |
|
} |
|
|
|
private void Loop() |
|
{ |
|
while (!_exit) |
|
{ |
|
Thread.Sleep(50); |
|
IterKeyEffects(); |
|
DeleteFinishedKeyEffects(); |
|
} |
|
chroma.UninitializeAsync(); |
|
} |
|
|
|
public void MemorySet() |
|
{ |
|
chroma.Keyboard.SetKeyAsync(Key.Delete, Color.Orange, false); |
|
} |
|
|
|
public void MemoryCleared() |
|
{ |
|
chroma.Keyboard.SetKeyAsync(Key.Delete, Color.Black, false); |
|
} |
|
|
|
public void PrintNumber(string result) |
|
{ |
|
for (int i = 0; i < result.Length; i++) |
|
{ |
|
Key[] key = new Key[] { GetKey(result[i]) }; |
|
lock (keyEffectsLock) |
|
{ |
|
//KeyEffect errorFlash = keyEffects.Find(x => x.Keys.SequenceEqual(NumpadKeys)); |
|
//if (errorFlash != null) keyEffects.Remove(errorFlash); |
|
keyEffects.Add(new FlashKey(key, Color.White, dNumberPassiveColor, 10, i * 12)); |
|
} |
|
} |
|
} |
|
|
|
public void Exit() |
|
{ |
|
_exit = true; |
|
} |
|
|
|
private Key GetKey(char character) |
|
{ |
|
if (character == ',') |
|
return Key.OemPeriod; |
|
if (character == '-') |
|
return Key.OemMinus; |
|
if (char.GetNumericValue(character) >= 0) |
|
return dNumberKeys[(int)char.GetNumericValue(character)]; |
|
throw new ArgumentException($"Character '{character}' was not expected", "character"); |
|
} |
|
|
|
private void DeleteFinishedKeyEffects() |
|
{ |
|
if (keyEffectTrash.Count > 0) |
|
{ |
|
lock (keyEffectsLock) |
|
{ |
|
keyEffectTrash.ForEach(effect => keyEffects.Remove(effect)); |
|
} |
|
keyEffectTrash.Clear(); |
|
} |
|
} |
|
|
|
private void IterKeyEffects() |
|
{ |
|
lock (keyEffectsLock) |
|
{ |
|
if (keyEffects.Count == 0) |
|
return; |
|
foreach (KeyEffect keyEffect in keyEffects) |
|
{ |
|
if (keyEffect.Step(chroma)) |
|
keyEffectTrash.Add(keyEffect); |
|
} |
|
} |
|
} |
|
|
|
internal void ErrorFlash() |
|
{ |
|
lock (keyEffectsLock) |
|
{ |
|
KeyEffect errorFlash = keyEffects.Find(x => x.Keys.SequenceEqual(numpadKeys)); |
|
if (errorFlash != null) keyEffects.Remove(errorFlash); |
|
keyEffects.Add(new FlashKey(numpadKeys, Color.Red, numpadPassiveColor, 10)); |
|
} |
|
} |
|
|
|
internal void NumpadKeyPressed(Key pressedKey) |
|
{ |
|
Key[] key = new Key[] { pressedKey }; |
|
lock (keyEffectsLock) |
|
{ |
|
KeyEffect keyEffect = keyEffects.Find(x => x.Keys.SequenceEqual(key)); |
|
if (keyEffect != null) |
|
keyEffects.Remove(keyEffect); |
|
keyEffects.Add(new FadeKey(key, Color.White, numpadPassiveColor, 0.05f)); |
|
} |
|
} |
|
} |
|
} |