From d6ff4a0551df292e4aa84d6cd04356809cccfdf7 Mon Sep 17 00:00:00 2001 From: SpoinkyNL Date: Thu, 18 Feb 2016 22:33:41 +0100 Subject: [PATCH] Improved keyboard missing handling, implemented keyboard select --- .../Artemis/KeyboardProviders/Corsair/K70.cs | 26 ++-------------- .../Artemis/KeyboardProviders/Corsair/K95.cs | 22 ------------- .../KeyboardProviders/Razer/BlackWidow.cs | 22 +++++++++++-- Artemis/Artemis/Models/MainModel.cs | 2 +- .../Artemis/ViewModels/FlyoutBaseViewModel.cs | 3 ++ .../Flyouts/FlyoutSettingsViewModel.cs | 31 ++++++++++++++++++- Artemis/Artemis/ViewModels/ShellViewModel.cs | 2 +- .../Views/Flyouts/FlyoutSettingsView.xaml | 3 +- 8 files changed, 60 insertions(+), 51 deletions(-) diff --git a/Artemis/Artemis/KeyboardProviders/Corsair/K70.cs b/Artemis/Artemis/KeyboardProviders/Corsair/K70.cs index 42b77047c..765381193 100644 --- a/Artemis/Artemis/KeyboardProviders/Corsair/K70.cs +++ b/Artemis/Artemis/KeyboardProviders/Corsair/K70.cs @@ -3,6 +3,7 @@ using System.Drawing; using System.Windows.Forms; using Artemis.Utilities; using CUE.NET; +using CUE.NET.Devices.Generic.Enums; using CUE.NET.Devices.Keyboard; using CUE.NET.Exceptions; @@ -25,8 +26,9 @@ namespace Artemis.KeyboardProviders.Corsair } catch (CUEException e) { - if (e.Message.Contains("not found")) + if (e.Error == CorsairError.ServerNotFound) return false; + throw; } return true; @@ -82,27 +84,5 @@ namespace Artemis.KeyboardProviders.Corsair } _keyboard.Update(true); } - - /* - RectangleF[,] ledRectangles = new RectangleF[bitmap.Width, bitmap.Height]; - RectangleKeyGroup[,] ledGroups = new RectangleKeyGroup[bitmap.Width, bitmap.Height]; - //_keyboard.Brush = new SolidColorBrush(Color.Black); - for (var x = 0 ; x < bitmap.Width; x++) - { - for (var y = 0; y < bitmap.Height; y++) - { - ledRectangles[x, y] = new RectangleF(_keyboard.KeyboardRectangle.X * (x*(_keyboard.KeyboardRectangle.Width / bitmap.Width / _keyboard.KeyboardRectangle.X)), _keyboard.KeyboardRectangle.Y*(y*(_keyboard.KeyboardRectangle.Height / bitmap.Height / _keyboard.KeyboardRectangle.Y)), _keyboard.KeyboardRectangle.Width / bitmap.Width, _keyboard.KeyboardRectangle.Height / bitmap.Height); - ledGroups[x, y] = new RectangleKeyGroup(_keyboard, ledRectangles[x, y], 0.01f) { Brush = new SolidColorBrush(bitmap.GetPixel(x, y)) }; - } - } - _keyboard.Update(); - for (var x = 0; x < bitmap.Width; x++) - { - for (var y = 0; y < bitmap.Height; y++) - { - _keyboard.DetachKeyGroup(ledGroups[x, y]); - } - } - */ } } \ No newline at end of file diff --git a/Artemis/Artemis/KeyboardProviders/Corsair/K95.cs b/Artemis/Artemis/KeyboardProviders/Corsair/K95.cs index cb808cacb..63e9fafa2 100644 --- a/Artemis/Artemis/KeyboardProviders/Corsair/K95.cs +++ b/Artemis/Artemis/KeyboardProviders/Corsair/K95.cs @@ -84,27 +84,5 @@ namespace Artemis.KeyboardProviders.Corsair } _keyboard.Update(true); } - - /* - RectangleF[,] ledRectangles = new RectangleF[bitmap.Width, bitmap.Height]; - RectangleKeyGroup[,] ledGroups = new RectangleKeyGroup[bitmap.Width, bitmap.Height]; - //_keyboard.Brush = new SolidColorBrush(Color.Black); - for (var x = 0 ; x < bitmap.Width; x++) - { - for (var y = 0; y < bitmap.Height; y++) - { - ledRectangles[x, y] = new RectangleF(_keyboard.KeyboardRectangle.X * (x*(_keyboard.KeyboardRectangle.Width / bitmap.Width / _keyboard.KeyboardRectangle.X)), _keyboard.KeyboardRectangle.Y*(y*(_keyboard.KeyboardRectangle.Height / bitmap.Height / _keyboard.KeyboardRectangle.Y)), _keyboard.KeyboardRectangle.Width / bitmap.Width, _keyboard.KeyboardRectangle.Height / bitmap.Height); - ledGroups[x, y] = new RectangleKeyGroup(_keyboard, ledRectangles[x, y], 0.01f) { Brush = new SolidColorBrush(bitmap.GetPixel(x, y)) }; - } - } - _keyboard.Update(); - for (var x = 0; x < bitmap.Width; x++) - { - for (var y = 0; y < bitmap.Height; y++) - { - _keyboard.DetachKeyGroup(ledGroups[x, y]); - } - } - */ } } \ No newline at end of file diff --git a/Artemis/Artemis/KeyboardProviders/Razer/BlackWidow.cs b/Artemis/Artemis/KeyboardProviders/Razer/BlackWidow.cs index c577757c3..15cf44672 100644 --- a/Artemis/Artemis/KeyboardProviders/Razer/BlackWidow.cs +++ b/Artemis/Artemis/KeyboardProviders/Razer/BlackWidow.cs @@ -1,7 +1,9 @@ -using System.Drawing; +using System; +using System.Drawing; using Artemis.KeyboardProviders.Razer.Utilities; using Corale.Colore.Core; using Corale.Colore.Razer.Keyboard; +using Color = Corale.Colore.Core.Color; namespace Artemis.KeyboardProviders.Razer { @@ -14,7 +16,23 @@ namespace Artemis.KeyboardProviders.Razer public override bool CanEnable() { - return Chroma.IsSdkAvailable(); + if (!Chroma.IsSdkAvailable()) + return false; + + // Some people have Synapse installed, but not a Chroma keyboard, deal with this + try + { + // Create a bitmap to send as a test + var b = new Bitmap(22, 6); + var razerArray = RazerUtilities.BitmapColorArray(b, 6, 22); + Chroma.Instance.Keyboard.SetGrid(razerArray); + Chroma.Instance.Keyboard.Clear(); + } + catch (NullReferenceException) + { + return false; + } + return true; } public override void Enable() diff --git a/Artemis/Artemis/Models/MainModel.cs b/Artemis/Artemis/Models/MainModel.cs index 53d8f92a0..e795e21b7 100644 --- a/Artemis/Artemis/Models/MainModel.cs +++ b/Artemis/Artemis/Models/MainModel.cs @@ -88,7 +88,7 @@ namespace Artemis.Models ChangeKeyboard(keyboard ?? KeyboardProviders.First(k => k.Name == "Logitech G910 Orion Spark RGB")); } - private void ChangeKeyboard(KeyboardProvider keyboardProvider) + public void ChangeKeyboard(KeyboardProvider keyboardProvider) { if (ActiveKeyboard != null && keyboardProvider.Name == ActiveKeyboard.Name) return; diff --git a/Artemis/Artemis/ViewModels/FlyoutBaseViewModel.cs b/Artemis/Artemis/ViewModels/FlyoutBaseViewModel.cs index 871e508ae..26ad9231e 100644 --- a/Artemis/Artemis/ViewModels/FlyoutBaseViewModel.cs +++ b/Artemis/Artemis/ViewModels/FlyoutBaseViewModel.cs @@ -33,6 +33,7 @@ namespace Artemis.ViewModels return; _isOpen = value; + HandleOpen(); NotifyOfPropertyChange(() => IsOpen); } } @@ -50,5 +51,7 @@ namespace Artemis.ViewModels NotifyOfPropertyChange(() => Position); } } + + protected abstract void HandleOpen(); } } \ No newline at end of file diff --git a/Artemis/Artemis/ViewModels/Flyouts/FlyoutSettingsViewModel.cs b/Artemis/Artemis/ViewModels/Flyouts/FlyoutSettingsViewModel.cs index 0eaa31b99..bee91c22c 100644 --- a/Artemis/Artemis/ViewModels/Flyouts/FlyoutSettingsViewModel.cs +++ b/Artemis/Artemis/ViewModels/Flyouts/FlyoutSettingsViewModel.cs @@ -1,19 +1,48 @@ using System.Diagnostics; +using System.Linq; +using System.Windows.Forms; +using Artemis.Models; +using Caliburn.Micro; using MahApps.Metro.Controls; namespace Artemis.ViewModels.Flyouts { public class FlyoutSettingsViewModel : FlyoutBaseViewModel { - public FlyoutSettingsViewModel() + private string _selectedKeyboardProvider; + public MainModel MainModel { get; set; } + + public FlyoutSettingsViewModel(MainModel mainModel) { + MainModel = mainModel; Header = "settings"; Position = Position.Right; } + public BindableCollection KeyboardProviders + => new BindableCollection(MainModel.KeyboardProviders.Select(k => k.Name)); + + public string SelectedKeyboardProvider + { + get { return _selectedKeyboardProvider; } + set + { + if (value == _selectedKeyboardProvider) return; + _selectedKeyboardProvider = value; + NotifyOfPropertyChange(() => SelectedKeyboardProvider); + + MainModel.ChangeKeyboard(MainModel.KeyboardProviders.First(k => k.Name == _selectedKeyboardProvider)); + } + } + public void NavigateTo(string url) { Process.Start(new ProcessStartInfo(url)); } + + protected override void HandleOpen() + { + SelectedKeyboardProvider = MainModel.ActiveKeyboard?.Name; + } } } \ No newline at end of file diff --git a/Artemis/Artemis/ViewModels/ShellViewModel.cs b/Artemis/Artemis/ViewModels/ShellViewModel.cs index 85f5939d2..9ca08e766 100644 --- a/Artemis/Artemis/ViewModels/ShellViewModel.cs +++ b/Artemis/Artemis/ViewModels/ShellViewModel.cs @@ -23,7 +23,7 @@ namespace Artemis.ViewModels _gamesVm = new GamesViewModel(MainModel) {DisplayName = "Games"}; _overlaysVm = new OverlaysViewModel(MainModel) {DisplayName = "Overlays"}; - Flyouts.Add(new FlyoutSettingsViewModel()); + Flyouts.Add(new FlyoutSettingsViewModel(MainModel)); // By now Effects are added to the MainModel so we can savely start one ToggleEffects(); diff --git a/Artemis/Artemis/Views/Flyouts/FlyoutSettingsView.xaml b/Artemis/Artemis/Views/Flyouts/FlyoutSettingsView.xaml index e495c6198..7e3745fd9 100644 --- a/Artemis/Artemis/Views/Flyouts/FlyoutSettingsView.xaml +++ b/Artemis/Artemis/Views/Flyouts/FlyoutSettingsView.xaml @@ -42,8 +42,9 @@