Skip to content

Commit

Permalink
Lua console adapting cleanup UI tweaks (#65)
Browse files Browse the repository at this point in the history
* Lua Console & UI tweaks

* server checks if calls to Lua Console is allowed
* UI, lua code box auto width
* removed user visible mentions of LuaConsole, instead use Lua Console
* search banner added

* Minor changes

* functions moved to UserControlAPIBase
* variables renamed and functions made private or internal
* search banners added
* minor version bumped
  • Loading branch information
jdahlblom authored Jan 28, 2024
1 parent 30576b5 commit f98367b
Show file tree
Hide file tree
Showing 20 changed files with 444 additions and 264 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Install server in ```Scripts``` folder and add entry to Export.lua.

* Start the mission
* Connect client, once connected dcs-insight server sends all APIs it has
* With [LuaConsole enabled](https://github.com/DCS-Skunkworks/dcs-insight/wiki/LuaConsole) you can execute lua snippets, query the environment
* With [Lua Console enabled](https://github.com/DCS-Skunkworks/dcs-insight/wiki/Lua-Console) you can execute lua snippets, query the environment
* You can poll an API for changes
* You can search for API

Expand Down
10 changes: 7 additions & 3 deletions src/client/DCSInsight/DCSInsight.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
<!-- for WinForms -->
<AssemblyName>dcs-insight</AssemblyName>
<Version>1.0.0</Version>
<AssemblyVersion>1.8.7</AssemblyVersion>
<AssemblyVersion>1.9.0</AssemblyVersion>
<ApplicationIcon>Images\Magnifier_icon.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<None Remove="Images\clear_search_result.png" />
<None Remove="Images\cue_banner_search.png" />
<None Remove="Images\cue_banner_search_api.png" />
<None Remove="Images\cue_banner_search_dcsbios_controls.png" />
<None Remove="Images\cue_banner_search_icommands.png" />
<None Remove="Images\Icon_green_lamp_off.png" />
<None Remove="Images\Icon_green_lamp_on.png" />
<None Remove="Images\Magnifier_icon.ico" />
Expand All @@ -31,7 +33,9 @@
</ItemGroup>
<ItemGroup>
<Resource Include="Images\clear_search_result.png" />
<Resource Include="Images\cue_banner_search.png" />
<Resource Include="Images\cue_banner_search_api.png" />
<Resource Include="Images\cue_banner_search_dcsbios_controls.png" />
<Resource Include="Images\cue_banner_search_icommands.png" />
<Resource Include="Images\Icon_green_lamp_off.png" />
<Resource Include="Images\Icon_green_lamp_on.png" />
<Resource Include="Images\Magnifier_icon.ico" />
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/client/DCSInsight/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<TextBox Name="TextBoxPort" Text="7790" Width="40"></TextBox>
<Button Name="ButtonConnect" Content="Connect" BorderBrush="Gray" Margin="20,0,0,0" Click="ButtonConnect_OnClick"></Button>
<CheckBox Name="CheckBoxTop" Content="On Top" BorderBrush="Gray" Margin="20,0,0,0" Checked="CheckBoxTop_OnChecked" Unchecked="CheckBoxTop_OnUnchecked"></CheckBox>
<TextBox Name="TextBoxSearchAPI" TextWrapping="NoWrap" IsReadOnly="False" Width="150" Margin="20,0,0,0" KeyDown="TextBoxSearchAPI_OnKeyDown"/>
<TextBox Name="TextBoxSearchAPI" TextWrapping="NoWrap" IsReadOnly="False" Width="150" Margin="20,0,0,0" PreviewKeyDown="TextBoxSearchAPI_OnPreviewKeyDown" />
<Button>
<Image Source="/Images/search_api.png" Name="ButtonSearchAPI" Tag="Search" MouseDown="ButtonSearchAPI_OnMouseDown" ToolTip="Search for DCS API"/>
</Button>
Expand Down
37 changes: 22 additions & 15 deletions src/client/DCSInsight/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
{
if (_formLoaded) return;

TextBoxSearchLuaControls.SetBackgroundSearchBanner(TextBoxSearchAPI);
ShowVersionInfo();
SetFormState();
CheckBoxTop.IsChecked = true;
Expand Down Expand Up @@ -469,21 +470,6 @@ private void ShowAPIs(bool searching = false)
}
}

private void TextBoxSearchAPI_OnKeyDown(object sender, KeyEventArgs e)
{
try
{
if (e.Key == Key.Enter)
{
ShowAPIs(true);
}
}
catch (Exception ex)
{
Common.ShowErrorMessageBox(ex);
}
}

private void ShowVersionInfo()
{
try
Expand Down Expand Up @@ -630,5 +616,26 @@ private void ButtonLuaWindow_OnClick(object sender, RoutedEventArgs e)
Common.ShowErrorMessageBox(ex);
}
}

private void TextBoxSearchAPI_OnPreviewKeyDown(object sender, KeyEventArgs e)
{
try
{
TextBoxSearchLuaControls.HandleTyping(TextBoxSearchAPI);
if (Common.LuaConsoleIsLoaded && !Common.LuaConsoleSearchWarningGiven)
{
Common.ShowMessageBox("[warning] Any existing lua code written in the Lua Console will be discarded if you search.");
Common.LuaConsoleSearchWarningGiven = true;
}
if (e.Key == Key.Enter)
{
ShowAPIs(true);
}
}
catch (Exception ex)
{
Common.ShowErrorMessageBox(ex);
}
}
}
}
126 changes: 119 additions & 7 deletions src/client/DCSInsight/Misc/Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,30 @@
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using NLog;

namespace DCSInsight.Misc
{
public static class Common
internal static class Common
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
internal static bool LuaConsoleIsLoaded;
internal static bool LuaConsoleSearchWarningGiven;

public static void ShowMessageBox(string message)
internal static void ShowMessageBox(string message)
{
MessageBox.Show(message, "", MessageBoxButton.OK, MessageBoxImage.Information);
}

public static string GetApplicationPath()
internal static string GetApplicationPath()
{
return AppDomain.CurrentDomain.BaseDirectory;
}

public static void ShowErrorMessageBox(Exception ex, string message = null)
internal static void ShowErrorMessageBox(Exception ex, string message = null)
{
Logger.Error(ex, message);
MessageBox.Show(ex.Message, $"Details logged to error log.{Environment.NewLine}{ex.Source}", MessageBoxButton.OK, MessageBoxImage.Error);
Expand All @@ -36,7 +40,7 @@ public static void ShowErrorMessageBox(Exception ex, string message = null)
/// 1, 0 : folder found, json not found
/// 1, 1 : folder found, json found
/// </returns>
public static Tuple<bool, bool> CheckJSONDirectory(string jsonDirectory)
internal static Tuple<bool, bool> CheckJSONDirectory(string jsonDirectory)
{
if (string.IsNullOrEmpty(jsonDirectory) || !Directory.Exists(jsonDirectory))
{
Expand All @@ -63,14 +67,122 @@ public static Tuple<bool, bool> CheckJSONDirectory(string jsonDirectory)
return new Tuple<bool, bool>(true, jsonFound);
}

public static void UIElement_OnMouseEnterHandIcon(object sender, MouseEventArgs e)
internal static void UIElement_OnMouseEnterHandIcon(object sender, MouseEventArgs e)
{
Mouse.OverrideCursor = Cursors.Hand;
}

public static void UIElement_OnMouseLeaveNormalIcon(object sender, MouseEventArgs e)
internal static void UIElement_OnMouseLeaveNormalIcon(object sender, MouseEventArgs e)
{
Mouse.OverrideCursor = Cursors.Arrow;
}

internal static T FindVisualChild<T>(DependencyObject dependencyObject) where T : DependencyObject
{
if (dependencyObject == null) return null;

for (var i = 0; i < VisualTreeHelper.GetChildrenCount(dependencyObject); i++)
{
var child = VisualTreeHelper.GetChild(dependencyObject, i);

if (child is T o)
{
return o;
}

var childItem = FindVisualChild<T>(child);
if (childItem != null) return childItem;
}
return null;
}

/// <summary>
/// Finds a Child of a given item in the visual tree.
/// </summary>
/// <param name="parent">A direct parent of the queried item.</param>
/// <typeparam name="T">The type of the queried item.</typeparam>
/// <param name="childName">x:Name or Name of child. </param>
/// <returns>The first parent item that matches the submitted type parameter.
/// If not matching item can be found,
/// a null parent is being returned.</returns>
internal static T FindChild<T>(this DependencyObject parent, string childName) where T : DependencyObject
{
// Confirm parent and childName are valid.
if (parent == null) return null;

T foundChild = null;

var childrenCount = VisualTreeHelper.GetChildrenCount(parent);
for (var i = 0; i < childrenCount; i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
// If the child is not of the request child type child
if (child is not T childType)
{
// recursively drill down the tree
foundChild = FindChild<T>(child, childName);

// If the child is found, break, so we do not overwrite the found child.
if (foundChild != null) break;
}
else if (!string.IsNullOrEmpty(childName))
{
// If the child's name is set for search
if (childType is not FrameworkElement frameworkElement || frameworkElement.Name != childName) continue;
// if the child's name is of the request name
foundChild = childType;
break;
}
else
{
// child element found.
foundChild = childType;
break;
}
}

return foundChild;
}

internal static bool TryFindVisualChildByName<TChild>(this DependencyObject parent, string childElementName, out TChild childElement, bool isCaseSensitive = false)
where TChild : FrameworkElement
{
childElement = null;

// Popup.Child content is not part of the visual tree.
// To prevent traversal from breaking when parent is a Popup,
// we need to explicitly extract the content.
if (parent is Popup popup)
{
parent = popup.Child;
}

if (parent == null)
{
return false;
}

var stringComparison = isCaseSensitive
? StringComparison.Ordinal
: StringComparison.OrdinalIgnoreCase;

for (var i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
if (child is TChild resultElement
&& resultElement.Name.Equals(childElementName, stringComparison))
{
childElement = resultElement;
return true;
}

if (child.TryFindVisualChildByName(childElementName, out childElement))
{
return true;
}
}

return false;
}
}
}
1 change: 1 addition & 0 deletions src/client/DCSInsight/Misc/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
internal static class Constants
{
internal const string ListEnvironmentSnippet = "local keys = {}\r\nfor k, v in pairs(_G) do\r\n\tkeys[#keys+1] = k \r\nend\r\nreturn keys";
internal const string LuaConsole = "Lua Console";
}
}
2 changes: 1 addition & 1 deletion src/client/DCSInsight/Misc/TextBoxSearchLoSetCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal static void SetBackgroundSearchBanner(TextBox textBoxSearch)
var textImageBrush = new ImageBrush
{
ImageSource = new BitmapImage(
new Uri("pack://application:,,,/dcs-insight;component/Images/cue_banner_search.png", UriKind.RelativeOrAbsolute)),
new Uri("pack://application:,,,/dcs-insight;component/Images/cue_banner_search_icommands.png", UriKind.RelativeOrAbsolute)),
AlignmentX = AlignmentX.Left,
Stretch = Stretch.Uniform
};
Expand Down
20 changes: 19 additions & 1 deletion src/client/DCSInsight/Misc/TextBoxSearchLuaControls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@ namespace DCSInsight.Misc
{
internal static class TextBoxSearchLuaControls
{
internal static void HandleTyping(TextBox textBoxSearch)
{
try
{
if (string.IsNullOrEmpty(textBoxSearch.Text))
{
SetBackgroundSearchBanner(textBoxSearch);
return;
}

textBoxSearch.Background = null;
}
catch (Exception ex)
{
Common.ShowErrorMessageBox(ex);
}
}

internal static void SetBackgroundSearchBanner(TextBox textBoxSearch)
{
try
Expand All @@ -22,7 +40,7 @@ internal static void SetBackgroundSearchBanner(TextBox textBoxSearch)
var textImageBrush = new ImageBrush
{
ImageSource = new BitmapImage(
new Uri("pack://application:,,,/dcs-insight;component/Images/cue_banner_search.png", UriKind.RelativeOrAbsolute)),
new Uri("pack://application:,,,/dcs-insight;component/Images/cue_banner_search_api.png", UriKind.RelativeOrAbsolute)),
AlignmentX = AlignmentX.Left,
Stretch = Stretch.Uniform
};
Expand Down
10 changes: 7 additions & 3 deletions src/client/DCSInsight/UserControls/UserControlAPI.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
Loaded="UserControlAPI_OnLoaded"
mc:Ignorable="d"
d:DesignHeight="400" d:DesignWidth="800" Height="auto" IsTabStop="True">
<Grid >
<Grid Name="GridMain">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
Expand All @@ -16,6 +16,7 @@
<RowDefinition Height="10*" />
<RowDefinition Height="10*" />
<RowDefinition Height="10*" />
<RowDefinition Height="10*" />
<RowDefinition Height="20" />
</Grid.RowDefinitions>

Expand All @@ -29,12 +30,15 @@
<ItemsControl Name="ItemsControlParameters" Grid.Column="0" Grid.Row="2" KeyboardNavigation.TabNavigation="Cycle" Margin="5,5,5,5">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
<DockPanel Name="DockPanelParameters" FlowDirection="LeftToRight" LastChildFill="False" Margin="5,5,5,5"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>

<DockPanel Grid.Column="0" Grid.Row="3" Margin="5,10,0,0" >
<StackPanel Name="StackPanelBottom" Grid.Column="0" Grid.Row="3" Margin="5,5,5,5">
</StackPanel>

<DockPanel Grid.Column="0" Grid.Row="4" Margin="5,10,10,0" >
<Label Name="LabelResult" Content="Result" DockPanel.Dock="Left"></Label>
<TextBox Name="TextBoxResult" MaxHeight="400" TextWrapping="Wrap" VerticalScrollBarVisibility="Visible"></TextBox>
</DockPanel>
Expand Down
Loading

0 comments on commit f98367b

Please sign in to comment.