Skip to content

Commit

Permalink
improving input graph window
Browse files Browse the repository at this point in the history
  • Loading branch information
joekolodz committed Sep 30, 2021
1 parent eddcd3d commit c77e4e3
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 130 deletions.
2 changes: 1 addition & 1 deletion src/ViewModels/HOTASCollectionViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ private void CreateNewModeProfile()

private void ShowInputGraphWindow()
{
_eventAggregator.Publish(new ShowInputGraphWindowEvent(h => _deviceList.AxisChanged += h, h => _deviceList.AxisChanged -= h));
_eventAggregator.Publish(new ShowInputGraphWindowEvent(_deviceList, h => _deviceList.AxisChanged += h, h => _deviceList.AxisChanged -= h));
}

private void EditModeProfile(ModeActivationItem item)
Expand Down
4 changes: 3 additions & 1 deletion src/ViewModels/ShowInputGraphWindowEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ namespace SierraHOTAS.ViewModels
{
public class ShowInputGraphWindowEvent
{
public IHOTASCollection DeviceList { get; set; }
public Action<EventHandler<AxisChangedEventArgs>> AxisChangedHandler { get; set; }
public Action<EventHandler<AxisChangedEventArgs>> CancelCallbackRemoveHandler { get; set; }
public ShowInputGraphWindowEvent(Action<EventHandler<AxisChangedEventArgs>> axisChangedHandler, Action<EventHandler<AxisChangedEventArgs>> cancelCallbackRemoveHandler)
public ShowInputGraphWindowEvent(IHOTASCollection deviceList, Action<EventHandler<AxisChangedEventArgs>> axisChangedHandler, Action<EventHandler<AxisChangedEventArgs>> cancelCallbackRemoveHandler)
{
DeviceList = deviceList;
AxisChangedHandler = axisChangedHandler;
CancelCallbackRemoveHandler = cancelCallbackRemoveHandler;
}
Expand Down
16 changes: 0 additions & 16 deletions src/Views/InputGraphWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,6 @@
<Border BorderBrush="{StaticResource GroupBorderBrush}" BorderThickness="1">
<Grid>
<Canvas Name="LineGraphCanvas" Background="{StaticResource WindowBackgroundBrush}"/>
<StackPanel Orientation="Vertical" HorizontalAlignment="Left">
<Button Name="StartCapture" Content="Start Capture" Click="StartCapture_OnClick"/>
<Button Name="StopCapture" Content="Stop Capture" Click="StopCapture_OnClick"/>

<StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
<Label Content="Deviation" Foreground="{StaticResource TextHeaderBrush}" Width="60"/>
<TextBlock Text="{Binding Deviation}" Foreground="{StaticResource TextBrush}" Width="50"/>
</StackPanel>

<StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
<Label Content="Min/max" Foreground="{StaticResource TextHeaderBrush}" Width="60"/>
<TextBlock Text="{Binding Min}" Foreground="{StaticResource TextBrush}" Width="40"/>
<Label Content="/" Foreground="{StaticResource TextHeaderBrush}" Width="15"/>
<TextBlock Text="{Binding Max}" Foreground="{StaticResource TextBrush}" Width="40"/>
</StackPanel>
</StackPanel>
</Grid>
</Border>

Expand Down
196 changes: 86 additions & 110 deletions src/Views/InputGraphWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows;
Expand All @@ -17,73 +18,105 @@ namespace SierraHOTAS.Views
/// <summary>
/// Interaction logic for InputGraphWindow.xaml
/// </summary>
public partial class InputGraphWindow : Window, INotifyPropertyChanged
public partial class InputGraphWindow : Window
{
public Dispatcher AppDispatcher { get; set; }

private List<IHOTASDevice> _axisDeviceList;
private int _graphScale;
private int _deviation;
private int _min;
private int _max;
public int Deviation
{
get => _deviation;
set
{
_deviation = value;
OnPropertyChanged(nameof(Deviation));
}
}
private readonly Action<EventHandler<AxisChangedEventArgs>> _callBackRemoveHandler;
private Point _windowPosition;
private DispatcherTimer _dispatcherTimer;
private int _meterStrokeThickness = 2;

public int Min
public InputGraphWindow(IHOTASCollection deviceList, Action<EventHandler<AxisChangedEventArgs>> handler, Action<EventHandler<AxisChangedEventArgs>> callBackRemoveHandler)
{
get => _min;
set
InitializeComponent();

DataContext = this;

foreach (var d in deviceList.Devices)
{
_min = value;
OnPropertyChanged(nameof(Min));
if (d.Capabilities.AxeCount <= 0) continue;
if (_axisDeviceList == null) _axisDeviceList = new List<IHOTASDevice>();
_axisDeviceList.Add(d);
}
//bind the names to a checkbox control and draw a canvas for each device selected?
//the devices in _axisDeviceList will have the names of the true axis that are actually on the device


handler(AxisChangedHandler);
_callBackRemoveHandler = callBackRemoveHandler;

SizeChanged += InputGraphWindow_OnSizeChanged;
CalculateScale((int)LineGraphCanvas.Height);

_dispatcherTimer = new DispatcherTimer(new TimeSpan(0, 0, 0, 0, 25), DispatcherPriority.Render, DrawLoop, Dispatcher.CurrentDispatcher);
}

public int Max

private double _meterPosition = 0d;
private readonly Line _meterLine = new Line() { Name = "Meter" };
private void DrawLoop(object sender, EventArgs e)
{
get => _max;
set
{
_max = value;
OnPropertyChanged(nameof(Max));
}
}
LineGraphCanvas.Children.Remove(_meterLine);

private readonly Dictionary<int, Collection<int>> _pointsDictionary;

private readonly int[] _shiftList;
++_meterPosition;
if (_meterPosition > LineGraphCanvas.ActualWidth) _meterPosition = 0;

_meterLine.Stroke = new SolidColorBrush(Colors.Yellow);
_meterLine.StrokeThickness = _meterStrokeThickness;
_meterLine.X1 = _meterPosition;
_meterLine.Y1 = 0;
_meterLine.X2 = _meterPosition;
_meterLine.Y2 = LineGraphCanvas.ActualHeight;

private readonly Action<EventHandler<AxisChangedEventArgs>> _callBackRemoveHandler;
private bool _isCapturing;
public InputGraphWindow(Action<EventHandler<AxisChangedEventArgs>> handler, Action<EventHandler<AxisChangedEventArgs>> callBackRemoveHandler)
{
InitializeComponent();
LineGraphCanvas.Children.Add(_meterLine);

DataContext = this;
var removeUiElements = new List<UIElement>();

_pointsDictionary = new Dictionary<int, Collection<int>>();
_shiftList = new int[28];//from JoysticOffset.cs
var meterPositionBoundary = _meterPosition + _meterStrokeThickness + 6;

AppDispatcher = Dispatcher;
handler(AxisChangedHandler);
foreach (UIElement child in LineGraphCanvas.Children)
{

_callBackRemoveHandler = callBackRemoveHandler;
if (child is Ellipse)
{
var x = child as Ellipse;
var p = child.PointToScreen(new Point(0, 0));//Ellipse uses the parent canvas X,Y coord. it does not have its own
var beforeX = p.X;
p.X -= _windowPosition.X;
//p.Y -= _windowPosition.Y;

_graphScale = (int)Width;

SizeChanged += InputGraphWindow_OnSizeChanged;
//Debug.WriteLine($"Meter: {_meterPosition}, Dot:{beforeX}, Window:{_windowPosition.X}, new Dot:{p.X}");

if (Math.Abs(p.X - meterPositionBoundary) < .01)
{
removeUiElements.Add(x);
}
}
}

foreach (var child in removeUiElements)
{
LineGraphCanvas.Children.Remove(child);
}

}

private void InputGraphWindow_OnSizeChanged(object sender, SizeChangedEventArgs e)
{
_graphScale = 65535 / (int)ActualHeight;
CalculateScale((int)LineGraphCanvas.ActualHeight);
}

protected override void OnLocationChanged(EventArgs e)
{
_windowPosition = new Point(Left, Top);
base.OnLocationChanged(e);
}

private void CalculateScale(int height)
{
_graphScale = 65535 / (height - 80);
}

private void AxisChangedHandler(object sender, AxisChangedEventArgs e)
Expand All @@ -93,15 +126,6 @@ private void AxisChangedHandler(object sender, AxisChangedEventArgs e)

private void TrackAxis(AxisChangedEventArgs e)
{
//var axisName = JoystickOffsetValues.GetName(e.AxisId);

if (!_pointsDictionary.ContainsKey(e.AxisId))
{
_pointsDictionary.Add(e.AxisId, new Collection<int>());
}

var points = _pointsDictionary[e.AxisId];

var c = Colors.White;
var scaledValue = e.Value / _graphScale;

Expand All @@ -118,37 +142,26 @@ private void TrackAxis(AxisChangedEventArgs e)
break;
case 8:
c = Colors.Fuchsia;
scaledValue = e.Value / 80;
//scaledValue = e.Value / 80;
break;
}
}

if (e.Device.Name.ToLower().Contains("throttle"))
{
if (_isCapturing && e.AxisId == 12)
{
if (points.Count > 100)
points.RemoveAt(0);

points.Add(e.Value);
Max = points.Max();
Min = points.Min();
Deviation = Max - Min;
}

switch (e.AxisId)
{
case 0:
c = Colors.Red;
scaledValue = e.Value / 80;
//scaledValue = e.Value / 80;
break;
case 4:
c = Colors.Yellow;
scaledValue = e.Value / 80;
//scaledValue = e.Value / 80;
break;
case 8:
c = Colors.Cyan;
scaledValue = e.Value / 80;
//scaledValue = e.Value / 80;
break;
case 12:
c = Colors.MediumPurple;
Expand All @@ -162,8 +175,8 @@ private void TrackAxis(AxisChangedEventArgs e)
}
}

Dispatcher.InvokeAsync(() => Draw1(e.AxisId, scaledValue, c));

AppDispatcher.Invoke(() => Draw1(e.AxisId, scaledValue, c));
}

private void Draw1(int axisId, int value, Color color)
Expand All @@ -177,52 +190,15 @@ private void Draw1(int axisId, int value, Color color)
Height = 2
};

var shift = _shiftList[axisId];

Canvas.SetLeft(dot, shift++);
Canvas.SetLeft(dot, _meterPosition - _meterStrokeThickness);
Canvas.SetBottom(dot, value);
LineGraphCanvas.Children.Add(dot);

if (shift > ActualWidth)
{
shift = 0;
LineGraphCanvas.Children.Clear();
}

_shiftList[axisId] = shift;
}

private void StartCapture_OnClick(object sender, RoutedEventArgs e)
{
_isCapturing = true;
}

private void StopCapture_OnClick(object sender, RoutedEventArgs e)
{
_isCapturing = false;
Deviation = 0;
Max = 0;
Min = 0;
}

protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (e.Key == Key.Escape) CloseInternal();
}

private void CloseInternal()
protected override void OnClosed(EventArgs e)
{
_dispatcherTimer.Stop();
_callBackRemoveHandler(AxisChangedHandler);

Close();
}

public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
2 changes: 1 addition & 1 deletion src/Views/ViewService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private void ShowModeProfileConfigWindow(ShowModeProfileConfigWindowEvent eventM

private void ShowInputGraphWindow(ShowInputGraphWindowEvent eventMessage)
{
new InputGraphWindow(eventMessage.AxisChangedHandler, eventMessage.CancelCallbackRemoveHandler) { Owner = _mainWindow }.Show();
new InputGraphWindow(eventMessage.DeviceList, eventMessage.AxisChangedHandler, eventMessage.CancelCallbackRemoveHandler) { Owner = _mainWindow }.Show();
}
}
}
2 changes: 1 addition & 1 deletion tests/HOTASCollectionViewModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,7 @@ public void show_input_graph()
hotasVm.Initialize();
hotasVm.ShowInputGraphWindowCommand.Execute(default);

subEventAggregator.ReceivedWithAnyArgs().Publish(new ShowInputGraphWindowEvent(null, null));
subEventAggregator.ReceivedWithAnyArgs().Publish(new ShowInputGraphWindowEvent(null, null, null));
}

[Fact]
Expand Down

0 comments on commit c77e4e3

Please sign in to comment.