using System; using System.Collections.Generic; using System.Linq; using System.Drawing; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Forms; using Blynclight; using Cyotek.Windows.Forms; using System.Windows.Input; namespace Tasklight.Controller { public class MainWindowViewModel : ModelBase { #region Model Declaration private void CommandPlaceholder(object obj) { return; } private BlynclightController _lightController; private bool _lightConnected; private Visibility _connectVisible; private Visibility _commandsVisible; private int _red; private int _green; private int _blue; private ICommand _lightConnect; private ICommand _lightUpdate; private ICommand _lightReset; private ICommand _lightDisconnect; private ICommand _redSet; private ICommand _greenSet; private ICommand _blueSet; private ICommand _yellowSet; private ICommand _magentaSet; private ICommand _cyanSet; private ICommand _whiteSet; private ICommand _orangeSet; private ICommand _colorPick; private bool _flashEnabled; private bool _dimmed; private int _selectedFlashIndex; private List _flashNames; public BlynclightController LightController { get { return _lightController; } set { SetProperty(ref _lightController, value, nameof(LightController)); } } public bool LightConnected { get { return _lightConnected; } set { SetProperty(ref _lightConnected, value, nameof(LightConnected)); } } public Visibility ConnectVisible { get { return _connectVisible; } set { SetProperty(ref _connectVisible, value, nameof(ConnectVisible)); } } public Visibility CommandsVisible { get { return _commandsVisible; } set { SetProperty(ref _commandsVisible, value, nameof(CommandsVisible)); } } public int Red { get { return _red; } set { SetProperty(ref _red, value, nameof(Red)); } } public int Green { get { return _green; } set { SetProperty(ref _green, value, nameof(Green)); } } public int Blue { get { return _blue; } set { SetProperty(ref _blue, value, nameof(Blue)); } } public ICommand LightConnect { get { return _lightConnect; } set { SetProperty(ref _lightConnect, value, nameof(LightConnect)); } } public ICommand LightUpdate { get { return _lightUpdate; } set { SetProperty(ref _lightUpdate, _lightUpdate, nameof(LightUpdate)); } } public ICommand LightReset { get { return _lightReset; } set { SetProperty(ref _lightReset, value, nameof(LightReset)); } } public ICommand LightDisconnect { get { return _lightDisconnect; } set { SetProperty(ref _lightDisconnect, value, nameof(LightDisconnect)); } } public ICommand RedSet { get { return _redSet; } set { SetProperty(ref _redSet, value, nameof(RedSet)); } } public ICommand GreenSet { get { return _greenSet; } set { SetProperty(ref _greenSet, value, nameof(GreenSet)); } } public ICommand BlueSet { get { return _blueSet; } set { SetProperty(ref _blueSet, value, nameof(BlueSet)); } } public ICommand YellowSet { get { return _yellowSet; } set { SetProperty(ref _yellowSet, value, nameof(YellowSet)); } } public ICommand MagentaSet { get { return _magentaSet; } set { SetProperty(ref _magentaSet, value, nameof(MagentaSet)); } } public ICommand CyanSet { get { return _cyanSet; } set { SetProperty(ref _cyanSet, value, nameof(CyanSet)); } } public ICommand WhiteSet { get { return _whiteSet; } set { SetProperty(ref _whiteSet, value, nameof(WhiteSet)); } } public ICommand OrangeSet { get { return _orangeSet; } set { SetProperty(ref _orangeSet, value, nameof(OrangeSet)); } } public ICommand ColorPick { get { return _colorPick; } set { SetProperty(ref _colorPick, value, nameof(ColorPick)); } } public bool FlashEnabled { get { return _flashEnabled; } set { SetProperty(ref _flashEnabled, value, nameof(FlashEnabled)); } } public bool Dimmed { get { return _dimmed; } set { SetProperty(ref _dimmed, value, nameof(Dimmed)); } } public int SelectedFlashIndex { get { return _selectedFlashIndex; } set { SetProperty(ref _selectedFlashIndex, value, nameof(SelectedFlashIndex)); } } public List FlashNames { get { return _flashNames; } set { SetProperty(ref _flashNames, value, nameof(FlashNames)); } } #endregion public MainWindowViewModel() { PreInitializeCommands(); InitializeVariables(); InitializeCommands(); } private void InitializeVariables() { LightController = new BlynclightController(); LightConnected = false; ConnectVisible = Visibility.Visible; CommandsVisible = Visibility.Collapsed; Red = 0; Green = 0; Blue = 0; FlashEnabled = false; Dimmed = false; SelectedFlashIndex = -1; FlashNames = new List(); FlashNames.AddRange(new string[] { "Low", "Medium", "High", "Pulse" }); } private void PreInitializeCommands() { _lightConnect = new RelayCommand(CommandPlaceholder); _lightUpdate = new RelayCommand(DoLightUpdate); _lightReset = new RelayCommand(DoResetLight); _lightDisconnect = new RelayCommand(DoDisconnectLight); _redSet = new RelayCommand(DoSetRed); _greenSet = new RelayCommand(DoSetGreen); _blueSet = new RelayCommand(DoSetBlue); _yellowSet = new RelayCommand(DoSetYellow); _magentaSet = new RelayCommand(DoSetMagenta); _cyanSet = new RelayCommand(DoSetCyan); _whiteSet = new RelayCommand(DoSetWhite); _orangeSet = new RelayCommand(DoSetOrange); _colorPick = new RelayCommand(DoPickColor); } private void InitializeCommands() { LightConnect = new RelayCommand(DoLightConnect); LightUpdate = new RelayCommand(DoLightUpdate); LightReset = new RelayCommand(DoResetLight); LightDisconnect = new RelayCommand(DoDisconnectLight); RedSet = new RelayCommand(DoSetRed); GreenSet = new RelayCommand(DoSetGreen); BlueSet = new RelayCommand(DoSetBlue); YellowSet = new RelayCommand(DoSetYellow); MagentaSet = new RelayCommand(DoSetMagenta); CyanSet = new RelayCommand(DoSetCyan); WhiteSet = new RelayCommand(DoSetWhite); OrangeSet = new RelayCommand(DoSetOrange); ColorPick = new RelayCommand(DoPickColor); } private void DoLightConnect(object obj) { int LightsConnected = LightController.InitBlyncDevices(); if (LightsConnected > 0) { LightConnected = true; ConnectVisible = Visibility.Collapsed; CommandsVisible = Visibility.Visible; } else InitializeVariables(); } private void DoLightUpdate(object obj) { LightController.TurnOnRGBLights(0, Convert.ToByte(Red), Convert.ToByte(Green), Convert.ToByte(Blue)); if (Dimmed) LightController.SetLightDim(0); else LightController.ClearLightDim(0); if (FlashEnabled) { switch (FlashNames.ElementAt(SelectedFlashIndex)) { case "Low": LightController.SelectLightFlashSpeed(0, 0x01); break; case "Medium": LightController.SelectLightFlashSpeed(0, 0x02); break; case "High": LightController.SelectLightFlashSpeed(0, 0x03); break; case "Pulse": LightController.SelectLightFlashSpeed(0, 0x04); break; } LightController.StartLightFlash(0); } else LightController.StopLightFlash(0); } private void DoResetLight(object obj) { LightController.ResetLight(0); Red = 0; Green = 0; Blue = 0; FlashEnabled = false; Dimmed = false; SelectedFlashIndex = -1; } private void DoDisconnectLight(object obj) { LightController.ResetLight(0); InitializeVariables(); } private void DoSetRed(object obj) { Red = 255; Green = 0; Blue = 0; LightController.TurnOnRedLight(0); } private void DoSetGreen(object obj) { Red = 0; Green = 255; Blue = 0; LightController.TurnOnGreenLight(0); } private void DoSetBlue(object obj) { Red = 0; Green = 0; Blue = 255; LightController.TurnOnBlueLight(0); } private void DoSetYellow(object obj) { Red = 255; Green = 255; Blue = 0; LightController.TurnOnYellowLight(0); } private void DoSetMagenta(object obj) { Red = 255; Green = 0; Blue = 255; LightController.TurnOnMagentaLight(0); } private void DoSetCyan(object obj) { Red = 0; Green = 255; Blue = 255; LightController.TurnOnCyanLight(0); } private void DoSetWhite(object obj) { Red = 255; Green = 255; Blue = 255; LightController.TurnOnWhiteLight(0); } private void DoSetOrange(object obj) { Red = 255; Green = 165; Blue = 0; LightController.TurnOnOrangeLight(0); } private void DoPickColor(object obj) { ColorPickerDialog cpd = new ColorPickerDialog(); cpd.Color = Color.FromArgb(Red, Green, Blue); cpd.ShowDialog(); if (cpd.DialogResult == DialogResult.OK) { Red = cpd.Color.R; Green = cpd.Color.G; Blue = cpd.Color.B; } cpd.Dispose(); } } }