Skip to content
This repository has been archived by the owner on Oct 6, 2024. It is now read-only.

Commit

Permalink
Show last module, category and control when open (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdahlblom authored Apr 1, 2024
1 parent 5e6a34c commit b5bd939
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 36 deletions.
2 changes: 2 additions & 0 deletions src/BIOSBuddy.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=BIOS/@EntryIndexedValue">BIOS</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=DCSBIOS/@EntryIndexedValue">DCSBIOS</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/XamlNaming/Abbreviations/=DCSBIOS/@EntryIndexedValue">DCSBIOS</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/XamlNaming/Abbreviations/=JSON/@EntryIndexedValue">JSON</s:String>
<s:Boolean x:Key="/Default/UserDictionary/Words/=DCSBIOS/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=DCSBIOSU/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
8 changes: 7 additions & 1 deletion src/BIOSBuddy/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,15 @@
<setting name="UpgradeRequired" serializeAs="String">
<value>True</value>
</setting>
<setting name="LastProfileID" serializeAs="String">
<setting name="LastModuleOpen" serializeAs="String">
<value>-1</value>
</setting>
<setting name="LastCategoryOpen" serializeAs="String">
<value>""</value>
</setting>
<setting name="ScrollViewerVerticalOffset" serializeAs="String">
<value>0</value>
</setting>
</BIOSBuddy.Properties.Settings>
</userSettings>
</configuration>
2 changes: 1 addition & 1 deletion src/BIOSBuddy/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
</Button>
</ToolBar>

<ScrollViewer Grid.Row="2" VerticalScrollBarVisibility="Auto">
<ScrollViewer Name="ScrollViewerControls" Grid.Row="2" VerticalScrollBarVisibility="Auto" Loaded="ScrollViewerControls_OnLoaded">
<ItemsControl Name="ItemsControlControls" />
</ScrollViewer>

Expand Down
118 changes: 90 additions & 28 deletions src/BIOSBuddy/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using DCS_BIOS.Serialized;
using DCS_BIOS.ControlLocator;
using System.Reflection;
using System.Windows.Media;

namespace BIOSBuddy
{
Expand All @@ -44,7 +45,9 @@ public partial class MainWindow : IDisposable, IDcsBiosConnectionListener, ICate
private bool _checkDCSBIOSVersionOnce;
private List<DCSBIOSControl> _metaControls;
private bool _changeOfModuleActive;
private bool _showLastProfile = true;
private bool _showLastModule = true;
private bool _showLastCategory = true;
private bool _showLastControlVisible = true;

public MainWindow()
{
Expand All @@ -56,7 +59,7 @@ public MainWindow()
/*
* Correct JSON folder path, move away from $USERDIRECTORY$.
*/
Settings.Default.DCSBiosJSONLocation = Environment.ExpandEnvironmentVariables(Settings.Default.DCSBiosJSONLocation.Contains("$USERDIRECTORY$") ?
Settings.Default.DCSBiosJSONLocation = Environment.ExpandEnvironmentVariables(Settings.Default.DCSBiosJSONLocation.Contains("$USERDIRECTORY$") ?
Settings.Default.DCSBiosJSONLocation.Replace("$USERDIRECTORY$", "%userprofile%") : Settings.Default.DCSBiosJSONLocation);
Settings.Default.Save();

Expand Down Expand Up @@ -109,7 +112,7 @@ private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)

ShowVersionInfo();

Top = Settings.Default.MainWindowTop;
Top = Settings.Default.MainWindowTop;
Left = Settings.Default.MainWindowLeft;
Height = Settings.Default.MainWindowHeight;
Width = Settings.Default.MainWindowWidth;
Expand Down Expand Up @@ -220,10 +223,10 @@ private void CreateDCSBIOS()
return;
}

_dcsBios = new DCSBIOS(Settings.Default.DCSBiosIPFrom,
Settings.Default.DCSBiosIPTo,
int.Parse(Settings.Default.DCSBiosPortFrom),
int.Parse(Settings.Default.DCSBiosPortTo),
_dcsBios = new DCSBIOS(Settings.Default.DCSBiosIPFrom,
Settings.Default.DCSBiosIPTo,
int.Parse(Settings.Default.DCSBiosPortFrom),
int.Parse(Settings.Default.DCSBiosPortTo),
DcsBiosNotificationMode.Parse);
if (!_dcsBios.HasLastException())
{
Expand Down Expand Up @@ -284,34 +287,43 @@ private void UpdateComboBoxModules()
return;
}

var found = false;
var index = -1;
ComboBoxModules.DataContext = DCSAircraft.Modules;
ComboBoxModules.ItemsSource = DCSAircraft.Modules;
ComboBoxModules.Items.Refresh();
if (_showLastProfile)
if (_showLastModule)
{
foreach (var module in DCSAircraft.Modules)
foreach (var module in DCSAircraft.Modules.Where(o => o.ID == Settings.Default.LastModuleOpen))
{
if (module.ID != Settings.Default.LastProfileID) continue;

ComboBoxModules.SelectedIndex = DCSAircraft.Modules.IndexOf(module);
found = true;
index = DCSAircraft.Modules.IndexOf(module);
break;
}
_showLastProfile = false;
_showLastModule = false;
}

if (!found) ComboBoxModules.SelectedIndex = 0;
UpdateComboBoxCategories();
ComboBoxModules.SelectedIndex = index >= 0 ? index : 0;
}

private void UpdateComboBoxCategories()
{
var categoriesList = _loadedControls.Select(o => o.Category).DistinctBy(o => o).ToList();

var found = false;
ComboBoxCategory.DataContext = categoriesList;
ComboBoxCategory.ItemsSource = categoriesList;
ComboBoxCategory.Items.Refresh();
ComboBoxCategory.SelectedIndex = 0;

if (_showLastCategory)
{
if (ComboBoxCategory.Items.Contains(Settings.Default.LastCategoryOpen))
{
found = true;
ComboBoxCategory.SelectedItem = Settings.Default.LastCategoryOpen;
}
_showLastCategory = false;
}

if (!found) ComboBoxCategory.SelectedIndex = 0;
}

private void ComboBoxModules_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
Expand All @@ -328,7 +340,7 @@ private void ComboBoxModules_OnSelectionChanged(object sender, SelectionChangedE
}

var selectedModule = (DCSAircraft)ComboBoxModules.SelectedItem;
Settings.Default.LastProfileID = selectedModule.ID;
Settings.Default.LastModuleOpen = selectedModule.ID;
Settings.Default.Save();
DCSBIOSControlLocator.DCSAircraft = selectedModule;
_loadedControls = DCSBIOSControlLocator.GetModuleControlsFromJson(selectedModule.JSONFilename);
Expand All @@ -344,7 +356,6 @@ private void ComboBoxModules_OnSelectionChanged(object sender, SelectionChangedE
}
UpdateComboBoxCategories();
_changeOfModuleActive = false;
ShowControls();
}
catch (Exception ex)
{
Expand All @@ -363,6 +374,10 @@ private void ComboBoxCategory_OnSelectionChanged(object sender, SelectionChanged
try
{
TextBoxSearchControl.Text = "";
var selectedCategory = (string)ComboBoxCategory.SelectedItem;
Settings.Default.LastCategoryOpen = selectedCategory;
Settings.Default.Save();

ShowControls();
}
catch (Exception ex)
Expand Down Expand Up @@ -435,11 +450,16 @@ private void ShowControls(bool searching = false)
filteredControls = _loadedControls.Where(o => o.Description.ToLower().Contains(searchWord) || o.Identifier.ToLower().Contains(searchWord))
.ToList();
}

foreach (var dcsbiosControl in filteredControls)
{
var luaCommand = DCSBIOSControlLocator.GetLuaCommand(dcsbiosControl.Identifier, true);
_dcsbiosUIControlPanels.Add(new DCSBIOSControlUserControl( dcsbiosControl, luaCommand));
_dcsbiosUIControlPanels.Add(new DCSBIOSControlUserControl(dcsbiosControl, luaCommand));
}

if (filteredControls.Any())
{
ItemsControlControls.Focus();
}

ItemsControlControls.ItemsSource = null;
Expand All @@ -448,11 +468,6 @@ private void ShowControls(bool searching = false)

LabelStatusBarRightInformation.Text = $"{filteredControls.Count()} DCS-BIOS Controls loaded.";

if (filteredControls.Any())
{
ItemsControlControls.Focus();
}

UpdateSearchButton();

if (searching)
Expand Down Expand Up @@ -729,6 +744,7 @@ private void MainWindow_OnClosing(object sender, CancelEventArgs e)
{
try
{
SaveVisibleUserControl();
Settings.Default.MainWindowTop = Top;
Settings.Default.MainWindowLeft = Left;
Settings.Default.Save();
Expand Down Expand Up @@ -770,7 +786,7 @@ private void MenuItem_OnClick(object sender, RoutedEventArgs e)
Common.ShowErrorMessageBox(ex);
}
}

private void ShowVersionInfo()
{
try
Expand All @@ -783,5 +799,51 @@ private void ShowVersionInfo()
Common.ShowErrorMessageBox(ex);
}
}

private bool IsUserControlVisibleInScrollViewer(UserControl userControl, ScrollViewer scrollViewer)
{
// Get the bounds of the UserControl
var controlBounds = userControl.TransformToAncestor(scrollViewer)
.TransformBounds(new Rect(0.0, 0.0, userControl.ActualWidth, userControl.ActualHeight));

// Get the visible region of the ScrollViewer
var visibleRegion = new Rect(scrollViewer.HorizontalOffset, scrollViewer.VerticalOffset, scrollViewer.ViewportWidth, scrollViewer.ViewportHeight);

// Check if the UserControl is within the visible region of the ScrollViewer
return visibleRegion.IntersectsWith(controlBounds);
}

private void SaveVisibleUserControl()
{
foreach (UserControl userControl in ItemsControlControls.Items)
{
if (!IsUserControlVisibleInScrollViewer(userControl, ScrollViewerControls)) continue;

Settings.Default.ScrollViewerVerticalOffset = ScrollViewerControls.VerticalOffset;
Settings.Default.Save();

break;
}
}

private void ScrollViewerControls_OnLoaded(object sender, RoutedEventArgs e)
{
try
{
if (!_showLastControlVisible) return;

ItemsControlControls.UpdateLayout();
ScrollViewerControls.UpdateLayout();
if (Settings.Default.ScrollViewerVerticalOffset < ScrollViewerControls.ScrollableHeight)
{
ScrollViewerControls.ScrollToVerticalOffset(Settings.Default.ScrollViewerVerticalOffset);
}
_showLastControlVisible = false;
}
catch (Exception ex)
{
Common.ShowErrorMessageBox(ex);
}
}
}
}
32 changes: 28 additions & 4 deletions src/BIOSBuddy/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion src/BIOSBuddy/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,14 @@
<Setting Name="UpgradeRequired" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
<Setting Name="LastProfileID" Type="System.Int32" Scope="User">
<Setting Name="LastModuleOpen" Type="System.Int32" Scope="User">
<Value Profile="(Default)">-1</Value>
</Setting>
<Setting Name="LastCategoryOpen" Type="System.String" Scope="User">
<Value Profile="(Default)">""</Value>
</Setting>
<Setting Name="ScrollViewerVerticalOffset" Type="System.Double" Scope="User">
<Value Profile="(Default)">0</Value>
</Setting>
</Settings>
</SettingsFile>
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public DCSBIOSControlUserControl(DCSBIOSControl dcsbiosControl, string luaComman
{
InitializeComponent();

Name = dcsbiosControl.Identifier;
_dcsbiosControl = dcsbiosControl;
_luaCommand = luaCommand;
foreach (var dcsbiosControlOutput in dcsbiosControl.Outputs)
Expand Down

0 comments on commit b5bd939

Please sign in to comment.