Skip to content

Commit

Permalink
Global dcs-bios control search added to ctrlref (#486)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdahlblom authored Feb 24, 2024
1 parent 9f39f36 commit 7ca28f7
Show file tree
Hide file tree
Showing 6 changed files with 342 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/ControlReference/CTRLMainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
Closing="MainWindow_OnClosing"
SizeChanged="MainWindow_OnSizeChanged"
WindowStartupLocation="Manual"
KeyDown="MainWindow_OnKeyDown"
Icon="/ctrlref;component/Images/FlightPanels01.png" >

<Grid >
Expand All @@ -37,6 +38,7 @@
<MenuItem Name ="MenuItemExportValues" Header="Copy values to Clipboard" Click="MenuItemExportValues_OnClick" />
</MenuItem>
<MenuItem Header="Help">
<MenuItem Header="Keyboard shortcuts" Click="MenuItem_OnClick" />
<MenuItem Name="MenuItemDiscord" Header="Discord Server" Click="MenuItemDiscord_OnClick" />
</MenuItem>
</Menu>
Expand Down
32 changes: 32 additions & 0 deletions src/ControlReference/CTRLMainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -750,5 +750,37 @@ private void MainWindow_OnClosing(object sender, CancelEventArgs e)
Common.ShowErrorMessageBox(ex);
}
}

private void MainWindow_OnKeyDown(object sender, KeyEventArgs e)
{
try
{
if (e.Key == Key.F && (Keyboard.Modifiers & (ModifierKeys.Control)) == (ModifierKeys.Control))
{
TextBoxSearchControl.Focus();
}
if (e.Key == Key.F && (Keyboard.Modifiers & (ModifierKeys.Control | ModifierKeys.Shift)) == (ModifierKeys.Control | ModifierKeys.Shift))
{
var globalSearchWindow = new GlobalSearchWindow();
globalSearchWindow.ShowDialog();
}
}
catch (Exception ex)
{
Common.ShowErrorMessageBox(ex);
}
}

private void MenuItem_OnClick(object sender, RoutedEventArgs e)
{
try
{
MessageBox.Show("CTRL + F => Search in module\nCTRL + SHIFT + F => Search in all modules");
}
catch (Exception ex)
{
Common.ShowErrorMessageBox(ex);
}
}
}
}
2 changes: 1 addition & 1 deletion src/ControlReference/ControlReference.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<PackageProjectUrl>https://github.com/DCS-Skunkworks/DCSFlightpanels</PackageProjectUrl>
<Description>DCS-BIOS Control Reference</Description>
<Company>DCSFlightpanels (DCSFP)</Company>
<AssemblyVersion>1.15.8</AssemblyVersion>
<AssemblyVersion>1.15.9</AssemblyVersion>
<StartupObject>ControlReference.App</StartupObject>
<ProduceReferenceAssembly>False</ProduceReferenceAssembly>
</PropertyGroup>
Expand Down
55 changes: 55 additions & 0 deletions src/ControlReference/Windows/GlobalSearchWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<Window x:Class="ControlReference.Windows.GlobalSearchWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:customControls="clr-namespace:ControlReference.CustomControls"
mc:Ignorable="d"
Title="Search in all dcs-bios modules" Height="500" Width="600"
Loaded="GlobalSearchWindow_OnLoaded"
WindowStartupLocation="CenterOwner"
KeyDown="GlobalSearchWindow_OnKeyDown" KeyUp="GlobalSearchWindow_OnKeyUp" Icon="/Images/FlightPanels01.png">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30" />
<RowDefinition Height="35" />
<RowDefinition Height="1*" />
<RowDefinition Height="30" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>


<DockPanel Grid.Row="0" Grid.Column="0" Background="#7ebbfc">
<Label Name="LabelControl" Width="Auto" Content="" Margin="10,0,0,0" DockPanel.Dock="Left"/>
</DockPanel>

<StackPanel Grid.Row="1" Grid.Column="0" Height="22" Orientation="Horizontal" Margin="5,0,0,0" HorizontalAlignment="Right">
<TextBox Name="TextBoxSearchControl" TextWrapping="NoWrap" IsReadOnly="False" Width="Auto" MinWidth="550" KeyDown="TextBoxSearchControl_OnKeyDown"/>
<Button Width="22">
<Image Source="/Images/search_controls.png" Name="ButtonSearchControls" Tag="Search" MouseDown="ButtonSearchControls_OnMouseDown" ToolTip="Global Search for DCS-BIOS Control"/>
</Button>
</StackPanel>

<ScrollViewer Grid.Row="2" Grid.Column="0">
<StackPanel Name="StackPanelControls" Height="Auto" Width="Auto" Margin="10,5,10,5">
<StackPanel.Resources>
<Style TargetType="{x:Type Border}">
<Setter Property="Margin" Value="0,10,0,0" />
<Setter Property="Background" Value="WhiteSmoke" />
<Setter Property="BorderBrush" Value="Black" />
<Setter Property="BorderThickness" Value="2" />
<Setter Property="CornerRadius" Value="5" />
</Style>
<Style TargetType="{x:Type customControls:TextBlockSelectable}">
<Setter Property="Margin" Value="5,5,5,5" />
<Setter Property="Background" Value="WhiteSmoke" />
</Style>
</StackPanel.Resources>

</StackPanel>
</ScrollViewer>
<DockPanel Grid.Row="3" Grid.Column="0" Background="White" />
</Grid>
</Window>
206 changes: 206 additions & 0 deletions src/ControlReference/Windows/GlobalSearchWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
using System;
using System.Collections.Generic;
using System.Media;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ClassLibraryCommon;
using ControlReference.CustomControls;
using DCS_BIOS.ControlLocator;
using DCS_BIOS.Json;

namespace ControlReference.Windows
{
/// <summary>
/// Interaction logic for GlobalSearchWindow.xaml
/// </summary>
public partial class GlobalSearchWindow
{
private bool _isLoaded;

public GlobalSearchWindow()
{
InitializeComponent();
}

private void SetFormState()
{
}

private void GlobalSearchWindow_OnLoaded(object sender, RoutedEventArgs e)
{
try
{
if (_isLoaded)
{
return;
}

SetFormState();
TextBoxSearchControl.Focus();
_isLoaded = true;
}
catch (Exception exception)
{
MessageBox.Show(exception.Message + Environment.NewLine + exception.StackTrace);
}
}

private void ShowControls(List<Tuple<string, DCSBIOSControl>> controls)
{
StackPanelControls.Children.Clear();
var lastControl = "";
TextBlockSelectable textBlock = null;
foreach (var tuple in controls)
{
if (tuple.Item1 != lastControl)
{
lastControl = tuple.Item1;
textBlock = new TextBlockSelectable(tuple.Item1 + Environment.NewLine);
textBlock.MouseEnter += Common.UIElement_OnMouseEnterHandIcon;
textBlock.MouseLeave += Common.UIElement_OnMouseLeaveNormalIcon;
SetContextMenu(textBlock);

textBlock.FontFamily = new System.Windows.Media.FontFamily("Consolas");
textBlock.Width = double.NaN;

var border = new Border
{
Child = textBlock
};
StackPanelControls.Children.Add(border);
StackPanelControls.Children.Add(new Line());

StackPanelControls.UpdateLayout();
}

if (textBlock != null)
{
textBlock.Text += tuple.Item2.Identifier + "\t" + tuple.Item2.Description + Environment.NewLine;
}
}
}

private void GlobalSearchWindow_OnKeyDown(object sender, KeyEventArgs e)
{
try
{
if (e.Key == Key.F && (Keyboard.Modifiers & (ModifierKeys.Control | ModifierKeys.Shift)) == (ModifierKeys.Control | ModifierKeys.Shift))
{
TextBoxSearchControl.Focus();
}
}
catch (Exception ex)
{
Common.ShowErrorMessageBox(ex);
}
}

private void SetContextMenu(TextBlockSelectable textBlock)
{
try
{
//_contextMenu.Opened += TextBlockContextMenuOpened;
ContextMenu contextMenu = new();
contextMenu.Opened += TextBlock_ContextMenuOpened;
contextMenu.Tag = textBlock;
var menuItemCopy = new MenuItem
{
Tag = textBlock,
Header = "Copy"
};
menuItemCopy.Click += MenuItemCopy_OnClick;
contextMenu.Items.Add(menuItemCopy);
textBlock.ContextMenu = contextMenu;
}
catch (Exception exception)
{
Common.ShowMessageBox(exception.Message + Environment.NewLine + exception.StackTrace);
}
}

private static void TextBlock_ContextMenuOpened(object sender, RoutedEventArgs e)
{
try
{
var contextMenu = (ContextMenu)sender;
var textBlock = (TextBlockSelectable)contextMenu.Tag;

((MenuItem)contextMenu.Items[0])!.IsEnabled = !string.IsNullOrEmpty(textBlock.SelectedText);
}
catch (Exception ex)
{
Common.ShowErrorMessageBox(ex);
}
}

private void MenuItemCopy_OnClick(object sender, RoutedEventArgs e)
{
try
{
var textBlock = ((MenuItem)sender).Tag;
CopyToClipboard((TextBlockSelectable)textBlock);
}
catch (Exception exception)
{
Common.ShowMessageBox(exception.Message + Environment.NewLine + exception.StackTrace);
}
}

private static void CopyToClipboard(TextBlockSelectable textBlock)
{
if (string.IsNullOrEmpty(textBlock.SelectedText)) textBlock.SelectAll();

Clipboard.SetText(textBlock.SelectedText ?? "");
SystemSounds.Asterisk.Play();
}

private void GlobalSearchWindow_OnKeyUp(object sender, KeyEventArgs e)
{
try
{
if (e.Key != Key.Escape) return;

Close();
}
catch (Exception exception)
{
Common.ShowMessageBox(exception.Message + Environment.NewLine + exception.StackTrace);
}
}

private void TextBoxSearchControl_OnKeyDown(object sender, KeyEventArgs e)
{
try
{
if (string.IsNullOrEmpty(TextBoxSearchControl.Text)) return;

if (e.Key != Key.Enter) return;

var controls = DCSBIOSControlLocator.GlobalControlSearch(TextBoxSearchControl.Text);
ShowControls(controls);
}
catch (Exception ex)
{
Common.ShowErrorMessageBox(ex);
}
}

private void ButtonSearchControls_OnMouseDown(object sender, MouseButtonEventArgs e)
{
try
{
if (string.IsNullOrEmpty(TextBoxSearchControl.Text)) return;

var controls = DCSBIOSControlLocator.GlobalControlSearch(TextBoxSearchControl.Text);
ShowControls(controls);
}
catch (Exception ex)
{
Common.ShowErrorMessageBox(ex);
}
}
}
}
47 changes: 46 additions & 1 deletion src/DCS-BIOS/ControlLocator/DCSBIOSControlLocator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DCS_BIOS.Json;
using System.Globalization;
using DCS_BIOS.Json;
using DCS_BIOS.StringClasses;


Expand Down Expand Up @@ -232,6 +233,50 @@ public static IEnumerable<DCSBIOSControl> GetInputControls()
return _dcsbiosControls.Where(controlObject => controlObject.Inputs.Count > 0);
}

public static List<Tuple<string, DCSBIOSControl>> GlobalControlSearch(string keyword)
{
var returnList = new List<Tuple<string, DCSBIOSControl>>();
try
{

lock (LockObject)
{
var directoryInfo = new DirectoryInfo(_jsonDirectory);
IEnumerable<FileInfo> files;
try
{
files = directoryInfo.EnumerateFiles("*.json", SearchOption.TopDirectoryOnly);
}
catch (Exception ex)
{
throw new Exception($"Failed to find DCS-BIOS json files. -> {Environment.NewLine}{ex.Message}");
}

foreach (var file in files)
{
var controls = ReadControlsFromDocJson(file.FullName);

if(controls == null || controls.Count == 0) continue;

foreach (var dcsbiosControl in controls)
{
if(dcsbiosControl.Identifier.ToLower(CultureInfo.InvariantCulture).Contains(keyword.ToLower(CultureInfo.InvariantCulture)) ||
dcsbiosControl.Description.ToLower(CultureInfo.InvariantCulture).Contains(keyword.ToLower(CultureInfo.InvariantCulture)))
{
returnList.Add(new Tuple<string, DCSBIOSControl>(file.Name, dcsbiosControl));
}
}
}
}
}
catch (Exception ex)
{
throw new Exception($"{DCSBIOS_JSON_NOT_FOUND_ERROR_MESSAGE} ==>[{_jsonDirectory}]<=={Environment.NewLine}{ex.Message}{Environment.NewLine}{ex.StackTrace}");
}

return returnList;
}

public static string GetLuaCommand(string dcsbiosIdentifier, bool includeSignature)
{
return LuaAssistant.GetLuaCommand(dcsbiosIdentifier, includeSignature);
Expand Down

0 comments on commit 7ca28f7

Please sign in to comment.