From 8620da24052dfe91e71dcf57bf89ac2c48427407 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Wed, 18 Sep 2019 22:00:42 -0500 Subject: [PATCH] Refactor displaying threshold sliders. Instead of having the sliders show and hide themselves, create the ones that are active in MainWindow. --- smx-config/CurrentSMXDevice.cs | 3 +- smx-config/MainWindow.xaml | 24 ++++----- smx-config/MainWindow.xaml.cs | 92 ++++++++++++++++++++++++++++++++++ smx-config/Widgets.cs | 41 +-------------- 4 files changed, 107 insertions(+), 53 deletions(-) diff --git a/smx-config/CurrentSMXDevice.cs b/smx-config/CurrentSMXDevice.cs index 3ffca59..3a2eb3c 100644 --- a/smx-config/CurrentSMXDevice.cs +++ b/smx-config/CurrentSMXDevice.cs @@ -26,7 +26,7 @@ namespace smx_config public struct LoadFromConfigDelegateArgs { // This indicates which fields changed since the last call. - public bool ConfigurationChanged, InputChanged, TestDataChanged; + public bool ConnectionsChanged, ConfigurationChanged, InputChanged, TestDataChanged; // Data for each of two controllers: public LoadFromConfigDelegateArgsPerController[] controller; @@ -111,6 +111,7 @@ namespace smx_config if(WasConnected[pad] != controller.info.connected) { args.ConfigurationChanged = true; + args.ConnectionsChanged = true; WasConnected[pad] = controller.info.connected; } diff --git a/smx-config/MainWindow.xaml b/smx-config/MainWindow.xaml index 61c1a96..014e633 100644 --- a/smx-config/MainWindow.xaml +++ b/smx-config/MainWindow.xaml @@ -135,7 +135,7 @@ Use if the platform is too sensitive. - + xml:space="preserve" TextAlignment="Center" >Set the force required to trigger each arrow. - - - - - - - - - - - + + + + + diff --git a/smx-config/MainWindow.xaml.cs b/smx-config/MainWindow.xaml.cs index c980c4e..a3386e9 100644 --- a/smx-config/MainWindow.xaml.cs +++ b/smx-config/MainWindow.xaml.cs @@ -68,6 +68,85 @@ namespace smx_config }; } + List thresholdSliderNames = new List() + { + "up-left", "up", "up-right", "left", "center", "right", "down-left", "down", "down-right", "cardinal", "corner", + }; + + Dictionary thresholdToIcon = new Dictionary() + { + { "up-left", "Resources/pad_up_left.png" }, + { "up", "Resources/pad_up.png" }, + { "up-right", "Resources/pad_up_right.png" }, + { "left", "Resources/pad_left.png" }, + { "center", "Resources/pad_center.png" }, + { "right", "Resources/pad_right.png" }, + { "down-left", "Resources/pad_down_left.png" }, + { "down", "Resources/pad_down.png" }, + { "down-right","Resources/pad_down_right.png" }, + { "cardinal", "Resources/pad_cardinal.png" }, + { "corner", "Resources/pad_diagonal.png" }, + }; + + bool IsThresholdSliderShown(string type) + { + bool AdvancedModeEnabled = (bool) AdvancedModeEnabledCheckbox.IsChecked; + SMX.SMXConfig config = ActivePad.GetFirstActivePadConfig(); + bool[] enabledPanels = config.GetEnabledPanels(); + + // Up and center are shown in both modes. + switch(type) + { + case "up-left": return AdvancedModeEnabled && enabledPanels[0]; + case "up": return enabledPanels[1]; + case "up-right": return AdvancedModeEnabled && enabledPanels[2]; + case "left": return AdvancedModeEnabled && enabledPanels[3]; + case "center": return enabledPanels[4]; + case "right": return AdvancedModeEnabled && enabledPanels[5]; + case "down-left": return AdvancedModeEnabled && enabledPanels[6]; + case "down": return AdvancedModeEnabled && enabledPanels[7]; + case "down-right": return AdvancedModeEnabled && enabledPanels[8]; + + // Show cardinal and corner if at least one panel they affect is enabled. + case "cardinal": return !AdvancedModeEnabled && (enabledPanels[3] || enabledPanels[5] || enabledPanels[8]); + case "corner": return !AdvancedModeEnabled && (enabledPanels[0] || enabledPanels[2] || enabledPanels[6] || enabledPanels[8]); + default: return true; + } + } + + ThresholdSlider CreateThresholdSlider(string type) + { + ThresholdSlider slider = new ThresholdSlider(); + slider.Type = type; + string iconPath = "pack://application:,,,/" + thresholdToIcon[type]; + slider.Icon = (new ImageSourceConverter()).ConvertFromString(iconPath) as ImageSource; + return slider; + } + + void CreateThresholdSliders() + { + SMX.SMXConfig config = ActivePad.GetFirstActivePadConfig(); + bool[] enabledPanels = config.GetEnabledPanels(); + + // remove the threshold sliders from xaml, create them all here + // + // remove the AdvancedModeEnabled binding and ShouldBeDisplayed, handle that here + // by creating the ones we need + // + // then we can add custom sliders too + ThresholdSliderContainer.Children.Clear(); + foreach(string sliderName in thresholdSliderNames) + { + if(!IsThresholdSliderShown(sliderName)) + continue; + + ThresholdSlider slider = CreateThresholdSlider(sliderName); + DockPanel.SetDock(slider, Dock.Top); + slider.Margin = new Thickness(0,8,0,0); + ThresholdSliderContainer.Children.Add(slider); + } + } + public override void OnApplyTemplate() { base.OnApplyTemplate(); @@ -83,6 +162,8 @@ namespace smx_config AutoLightsColor.StoppedDragging += delegate() { showAutoLightsColor.Stop(); }; AutoLightsColor.StoppedDragging += delegate() { showAutoLightsColor.Stop(); }; + CreateThresholdSliders(); + // This doesn't happen at the same time AutoLightsColor is used, since they're on different tabs. Diagnostics.SetShowAllLights += delegate(bool on) { @@ -182,6 +263,12 @@ namespace smx_config RefreshUploadPadText(args); RefreshSelectedColorPicker(); + // If a device has connected or disconnected, refresh the displayed threshold + // sliders. Don't do this otherwise, or we'll do this when the sliders are + // dragged. + if(args.ConnectionsChanged) + CreateThresholdSliders(); + // Show the threshold warning explanation if any panels are showing the threshold warning icon. bool ShowThresholdWarningText = false; foreach(Tuple activePad in ActivePad.ActivePads()) @@ -385,6 +472,11 @@ namespace smx_config CurrentSMXDevice.singleton.FireConfigurationChanged(null); } + private void AdvancedModeEnabledCheckbox_Changed(object sender, RoutedEventArgs e) + { + CreateThresholdSliders(); + } + private void ExportSettings(object sender, RoutedEventArgs e) { // Save the current thresholds on the first available pad as a preset. diff --git a/smx-config/Widgets.cs b/smx-config/Widgets.cs index 1348bfd..02fc949 100644 --- a/smx-config/Widgets.cs +++ b/smx-config/Widgets.cs @@ -107,19 +107,13 @@ namespace smx_config } public static readonly DependencyProperty AdvancedModeEnabledProperty = DependencyProperty.Register("AdvancedModeEnabled", - typeof(bool), typeof(ThresholdSlider), new FrameworkPropertyMetadata(false, RefreshAdvancedModeEnabledCallback)); + typeof(bool), typeof(ThresholdSlider), new FrameworkPropertyMetadata(false)); public bool AdvancedModeEnabled { get { return (bool) GetValue(AdvancedModeEnabledProperty); } set { SetValue(AdvancedModeEnabledProperty, value); } } - private static void RefreshAdvancedModeEnabledCallback(DependencyObject target, DependencyPropertyChangedEventArgs args) - { - ThresholdSlider self = target as ThresholdSlider; - self.RefreshVisibility(); - } - DoubleSlider slider; Label LowerLabel, UpperLabel; Image ThresholdWarning; @@ -255,41 +249,8 @@ namespace smx_config bool ShowThresholdWarning = config.ShowThresholdWarning(panelIdx); ThresholdWarning.Visibility = ShowThresholdWarning? Visibility.Visible:Visibility.Hidden; - RefreshVisibility(); UpdatingUI = false; } - - void RefreshVisibility() - { - SMX.SMXConfig config = ActivePad.GetFirstActivePadConfig(); - this.Visibility = ShouldBeDisplayed(config)? Visibility.Visible:Visibility.Collapsed; - } - - // Return true if this slider should be displayed. Only display a slider if it affects - // at least one panel which is enabled. - private bool ShouldBeDisplayed(SMX.SMXConfig config) - { - bool[] enabledPanels = config.GetEnabledPanels(); - - // Up and center are shown in both modes. - switch(Type) - { - case "up-left": return AdvancedModeEnabled && enabledPanels[0]; - case "up": return enabledPanels[1]; - case "up-right": return AdvancedModeEnabled && enabledPanels[2]; - case "left": return AdvancedModeEnabled && enabledPanels[3]; - case "center": return enabledPanels[4]; - case "right": return AdvancedModeEnabled && enabledPanels[5]; - case "down-left": return AdvancedModeEnabled && enabledPanels[6]; - case "down": return AdvancedModeEnabled && enabledPanels[7]; - case "down-right": return AdvancedModeEnabled && enabledPanels[8]; - - // Show cardinal and corner if at least one panel they affect is enabled. - case "cardinal": return !AdvancedModeEnabled && (enabledPanels[3] || enabledPanels[5] || enabledPanels[8]); - case "corner": return !AdvancedModeEnabled && (enabledPanels[0] || enabledPanels[2] || enabledPanels[6] || enabledPanels[8]); - default: return true; - } - } } // A button with a selectable highlight.