diff --git a/EncoderVisualizer.csproj b/EncoderVisualizer.csproj index 36c8971..4411465 100644 --- a/EncoderVisualizer.csproj +++ b/EncoderVisualizer.csproj @@ -65,12 +65,6 @@ True Resources.resx - - Form - - - testboi.cs - Component @@ -78,17 +72,11 @@ - - MainWindow.cs - ResXFileCodeGenerator Designer Resources.Designer.cs - - testboi.cs - SettingsSingleFileGenerator diff --git a/MainWindow.cs b/MainWindow.cs index e577b20..82700c4 100644 --- a/MainWindow.cs +++ b/MainWindow.cs @@ -5,11 +5,14 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using static System.Net.Mime.MediaTypeNames; namespace EncoderVisualizer { public class MainWindow: Form { + delegate VKBDeviceTab AddTabCallback(VKBDevice dev); + delegate void RemoveTabCallback(VKBDevice dev); public static MainWindow Instance { get { return actualInstance ?? (actualInstance = new MainWindow()); } } private static MainWindow actualInstance = null; private readonly TabControl Devices; @@ -27,11 +30,26 @@ private MainWindow() { } public VKBDeviceTab AddDevice(VKBDevice dev) { + if (InvokeRequired) + { + AddTabCallback d = new AddTabCallback(AddDevice); + return Invoke(d, new object[] { dev }) as VKBDeviceTab; + } VKBDeviceTab tab = new VKBDeviceTab(dev); tab.Text = dev.DeviceName; Devices.Controls.Add(tab); return tab; } + public void RemoveDevice(VKBDevice dev) + { + if (InvokeRequired) + { + RemoveTabCallback d = new RemoveTabCallback(RemoveDevice); + Invoke(d, new object[] { dev }); + return; + } + Devices.Controls.Remove(dev.Tab); + } } } diff --git a/VKB/VKBConnectionHandler.cs b/VKB/VKBConnectionHandler.cs index 1f3d140..2333f43 100644 --- a/VKB/VKBConnectionHandler.cs +++ b/VKB/VKBConnectionHandler.cs @@ -16,10 +16,31 @@ private VKBConnectionHandler() { public void Startup(Object sender, EventArgs e) { IEnumerable DevList = DeviceList.Local.GetHidDevices(vendorID: 0x231D); + DeviceList.Local.Changed += DevicesChanged; foreach (HidDevice dev in DevList) { Devices.Add(new VKBDevice(dev)); } } + public void DevicesChanged(Object sender, EventArgs e) + { + IEnumerable DevList = DeviceList.Local.GetHidDevices(vendorID: 0x231D); + List lostDevices = new List(); + foreach (VKBDevice dev in Devices) + { + if(!DevList.Contains(dev.HidDev)) lostDevices.Add(dev); + } + foreach (VKBDevice dev in lostDevices) { + MainWindow.Instance.RemoveDevice(dev); + Devices.Remove(dev); + } + foreach (HidDevice dev in DevList) + { + if(Devices.Find(d => d.HidDev == dev) == null) + { + Devices.Add(new VKBDevice(dev)); + } + } + } } } diff --git a/VKB/VKBDevice.cs b/VKB/VKBDevice.cs index 8900976..b86d147 100644 --- a/VKB/VKBDevice.cs +++ b/VKB/VKBDevice.cs @@ -19,7 +19,7 @@ public class VKBDevice public HidDevice HidDev; private HidStream Stream; private HidDeviceInputReceiver Receiver; - private VKBDeviceTab Tab; + public VKBDeviceTab Tab; private SortedList Encoders = new SortedList(); private byte lastSeqNo; public VKBDevice(HidDevice dev) { diff --git a/VKBDeviceTab.cs b/VKBDeviceTab.cs index e8f39b9..a94e768 100644 --- a/VKBDeviceTab.cs +++ b/VKBDeviceTab.cs @@ -14,6 +14,8 @@ public class VKBDeviceTab: TabPage private readonly TableLayoutPanel TLayout; private readonly FlowLayoutPanel FLayout; private readonly Label TBox; + private readonly Label NoEncs; + private int encCount = 0; public VKBDeviceTab(VKBDevice dev) { TLayout = new TableLayoutPanel { Dock = DockStyle.Fill, @@ -29,18 +31,35 @@ public VKBDeviceTab(VKBDevice dev) { Dock = DockStyle.Top, Text = $"{dev.DeviceName}, PID {dev.HidDev.ProductID:X4}, S/N {dev.SerialNumber}" }; + NoEncs = new Label + { + Dock = DockStyle.Fill, + Text = "No Encoders detected. Make sure that the device:\n" + + "1. is running nJoy32 firmware 2.17.9 or newer\n" + + "2. has \"Virtual BUS over USB\" enabled in VKBDevCfg (and the setting has been Set to the device)\n" + + "3. has encoders configured on the physical button layer", + TextAlign = System.Drawing.ContentAlignment.MiddleLeft, + Font = new System.Drawing.Font(System.Drawing.FontFamily.GenericSansSerif, 16, System.Drawing.FontStyle.Bold) + }; + Controls.Add(TLayout); TLayout.Controls.Add(TBox); - TLayout.Controls.Add(FLayout); + TLayout.Controls.Add(NoEncs); } public EncoderBox AddEncoderBox(VKBEncoder enc) { - if (FLayout.InvokeRequired) + if (FLayout.InvokeRequired || TLayout.InvokeRequired) { AddEncoderBoxCallback d = new AddEncoderBoxCallback(AddEncoderBox); return this.Invoke(d, new object[] { enc }) as EncoderBox; } + if(encCount == 0) + { + TLayout.Controls.Remove(NoEncs); + TLayout.Controls.Add(FLayout); + } + encCount++; EncoderBox box = new EncoderBox(enc); FLayout.Controls.Add(box); return box;