Skip to content

Commit

Permalink
new serialize attribute NoHide to force button Type to serialize to J…
Browse files Browse the repository at this point in the history
…SON; remove ButtonMap copy in hotasqueue and replace it with just a lookup given the current mode;
  • Loading branch information
joekolodz committed Nov 12, 2024
1 parent a022d9a commit 3b12d4d
Show file tree
Hide file tree
Showing 19 changed files with 174 additions and 137 deletions.
4 changes: 2 additions & 2 deletions src/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,13 @@

<DataTemplate x:Key="LinearAxisMapTemplate">
<WrapPanel>
<controls:LinearAxisMap Margin="1"/>
<controls:LinearAxisMap Margin="1" AxisHand="{Binding AxisValue}" IsMultiAction="{Binding IsMultiAction}" SegmentCount="{Binding SegmentCount}" SegmentBoundary="{Binding SegmentBoundary}"/>
</WrapPanel>
</DataTemplate>

<DataTemplate x:Key="RadialAxisMapTemplate">
<WrapPanel>
<controls:RadialAxisMap Margin="1"/>
<controls:RadialAxisMap Margin="1" AxisHand="{Binding AxisValue}" IsMultiAction="{Binding IsMultiAction}" SegmentCount="{Binding SegmentCount}" SegmentBoundary="{Binding SegmentBoundary}"/>
</WrapPanel>
</DataTemplate>

Expand Down
61 changes: 36 additions & 25 deletions src/Controls/LinearAxisMap.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,42 @@ public partial class LinearAxisMap : UserControl
private double _gaugeWidth = 120;
private double _gaugeHeight = 20;

public static DependencyProperty AxisHandProperty = DependencyProperty.Register(nameof(AxisHand), typeof(int), typeof(LinearAxisMap), new FrameworkPropertyMetadata(0, OnPropertyChangedChanged));
public static DependencyProperty IsMultiActionProperty = DependencyProperty.Register(nameof(IsMultiAction), typeof(int), typeof(LinearAxisMap), new FrameworkPropertyMetadata(0, OnPropertyChangedChanged));
public static DependencyProperty SegmentCountProperty = DependencyProperty.Register(nameof(SegmentCount), typeof(int), typeof(LinearAxisMap), new FrameworkPropertyMetadata(0, OnPropertyChangedChanged));
public static DependencyProperty SegmentBoundaryProperty = DependencyProperty.Register(nameof(SegmentBoundary), typeof(int), typeof(LinearAxisMap), new FrameworkPropertyMetadata(0, OnPropertyChangedChanged));

private static void OnPropertyChangedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
if (!(sender is LinearAxisMap prop)) return;
if (e.Property.Name == nameof(AxisHand)) prop.DrawRectangle((int)e.NewValue);
if (e.Property.Name == nameof(IsMultiAction) || e.Property.Name == nameof(SegmentCount)) prop.OnSegmentsChanged();
if (e.Property.Name == nameof(SegmentBoundary)) prop.ChangeSegmentBoundary();
}

public int AxisHand
{
get => (int)GetValue(AxisHandProperty);
set => SetValue(AxisHandProperty, value);
}

public int IsMultiAction
{
get => (int)GetValue(IsMultiActionProperty);
set => SetValue(IsMultiActionProperty, value);
}

public int SegmentCount
{
get => (int)GetValue(SegmentCountProperty);
set => SetValue(SegmentCountProperty, value);
}

public int SegmentBoundary
{
get => (int)GetValue(SegmentBoundaryProperty);
set => SetValue(SegmentBoundaryProperty, value);
}
public LinearAxisMap()
{
InitializeComponent();
Expand All @@ -38,33 +73,17 @@ private bool SegmentFilter(object segment)
return _axisVm.SegmentFilter(segment);
}

private void _axisVm_OnAxisValueChanged(object sender, AxisChangedViewModelEventArgs e)
{
DrawRectangle(e.Value);
}

private void AxisMap_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (_axisVm != null)
{
_axisVm.OnAxisValueChanged -= _axisVm_OnAxisValueChanged;
_axisVm.PropertyChanged -= _axisVm_PropertyChanged;
_axisVm.SegmentBoundaryChanged -= _axisVm_SegmentBoundaryChanged;
}

_axisVm = DataContext as AxisMapViewModel;
if (_axisVm == null) return;

SetSegmentBoundaryFilter();

_axisVm.OnAxisValueChanged += _axisVm_OnAxisValueChanged;
_axisVm.PropertyChanged += _axisVm_PropertyChanged;
_axisVm.SegmentBoundaryChanged += _axisVm_SegmentBoundaryChanged;

OnSegmentsChanged();
}

private void _axisVm_SegmentBoundaryChanged(object sender, EventArgs e)
private void ChangeSegmentBoundary()
{
Dispatcher?.Invoke(() =>
{
Expand All @@ -83,14 +102,6 @@ private void SetSegmentBoundaryFilter()
}
}

private void _axisVm_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(_axisVm.SegmentCount) || e.PropertyName == nameof(_axisVm.IsMultiAction))
{
OnSegmentsChanged();
}
}

private void OnSegmentsChanged()
{
RemoveAllSegmentLines();
Expand Down
73 changes: 47 additions & 26 deletions src/Controls/RadialAxisMap.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System;
using SierraHOTAS.ViewModels;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
using SierraHOTAS.ViewModels;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
Expand All @@ -11,57 +12,84 @@ namespace SierraHOTAS.Controls
{
public partial class RadialAxisMap : UserControl
{
private static int instances = 0;
private Guid id;
private AxisMapViewModel _axisVm;
private Line _arc;
private Ellipse _circle;
private readonly List<Line> _segmentLines;
private double _gaugeDiameter = 40;
private readonly Color _directionalColor;

public static DependencyProperty AxisHandProperty = DependencyProperty.Register(nameof(AxisHand), typeof(int), typeof(RadialAxisMap), new FrameworkPropertyMetadata(0, OnPropertyChangedChanged));
public static DependencyProperty IsMultiActionProperty = DependencyProperty.Register(nameof(IsMultiAction), typeof(int), typeof(RadialAxisMap), new FrameworkPropertyMetadata(0, OnPropertyChangedChanged));
public static DependencyProperty SegmentCountProperty = DependencyProperty.Register(nameof(SegmentCount), typeof(int), typeof(RadialAxisMap), new FrameworkPropertyMetadata(0, OnPropertyChangedChanged));
public static DependencyProperty SegmentBoundaryProperty = DependencyProperty.Register(nameof(SegmentBoundary), typeof(int), typeof(RadialAxisMap), new FrameworkPropertyMetadata(0, OnPropertyChangedChanged));

private static void OnPropertyChangedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
if (!(sender is RadialAxisMap prop)) return;
if(e.Property.Name == nameof(AxisHand)) prop.DrawCircle((int)e.NewValue);
if(e.Property.Name == nameof(IsMultiAction) || e.Property.Name == nameof(SegmentCount)) prop.OnSegmentsChanged();
if(e.Property.Name == nameof(SegmentBoundary)) prop.ChangeSegmentBoundary();
}

public int AxisHand
{
get => (int)GetValue(AxisHandProperty);
set => SetValue(AxisHandProperty, value);
}

public int IsMultiAction
{
get => (int)GetValue(IsMultiActionProperty);
set => SetValue(IsMultiActionProperty, value);
}

public int SegmentCount
{
get => (int)GetValue(SegmentCountProperty);
set => SetValue(SegmentCountProperty, value);
}

public int SegmentBoundary
{
get => (int)GetValue(SegmentBoundaryProperty);
set => SetValue(SegmentBoundaryProperty, value);
}

public RadialAxisMap()
{
InitializeComponent();
instances++;

_segmentLines = new List<Line>();
_directionalColor = (Color)ColorConverter.ConvertFromString("#80e5ff");

DataContextChanged += AxisMap_DataContextChanged;

CreateDial();

id = Guid.NewGuid();
Debug.WriteLine($"RadialAxisMap Constructor - instances:{instances} - {id}");
}

private bool SegmentFilter(object segment)
{
return _axisVm.SegmentFilter(segment);
}

private void _axisVm_OnAxisValueChanged(object sender, AxisChangedViewModelEventArgs e)
{
DrawCircle(e.Value);
}

private void AxisMap_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (_axisVm != null)
{
_axisVm.OnAxisValueChanged -= _axisVm_OnAxisValueChanged;
_axisVm.PropertyChanged -= _axisVm_PropertyChanged;
_axisVm.SegmentBoundaryChanged -= _axisVm_SegmentBoundaryChanged;
}

_axisVm = DataContext as AxisMapViewModel;
if (_axisVm == null) return;

SetSegmentBoundaryFilter();

_axisVm.OnAxisValueChanged += _axisVm_OnAxisValueChanged;
_axisVm.PropertyChanged += _axisVm_PropertyChanged;
_axisVm.SegmentBoundaryChanged += _axisVm_SegmentBoundaryChanged;

OnSegmentsChanged();
}

private void _axisVm_SegmentBoundaryChanged(object sender, EventArgs e)
private void ChangeSegmentBoundary()
{
Dispatcher.Invoke(() =>
{
Expand All @@ -79,13 +107,6 @@ private void SetSegmentBoundaryFilter()
view.Filter = SegmentFilter;
}
}
private void _axisVm_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(_axisVm.SegmentCount) || e.PropertyName == nameof(_axisVm.IsMultiAction))
{
OnSegmentsChanged();
}
}

private void OnSegmentsChanged()
{
Expand Down
5 changes: 4 additions & 1 deletion src/CustomSierraJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using SierraJSON;
using System;
using System.Linq;
using System.Reflection;

namespace SierraHOTAS
{
Expand Down Expand Up @@ -154,7 +155,9 @@ private static void SerializeAxis(HOTASAxis axis)
if (prop.Name == nameof(axis.SoundFileName) && string.IsNullOrEmpty((string)prop.GetValue(axis))) continue;
if (prop.Name == nameof(axis.SoundVolume) && Math.Abs((float)prop.GetValue(axis) - 1.0d) < 0.01) continue;

Serializer.WriteKeyValue(prop.Name, value);
var isNoHide = prop.GetCustomAttribute(typeof(SierraJsonNoHide));

Serializer.WriteKeyValue(prop.Name, value, isNoHide != null);
}
Serializer.WriteObjectEnd();
}
Expand Down
9 changes: 5 additions & 4 deletions src/Models/HOTASAxis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ namespace SierraHOTAS.Models
{
public class HOTASAxis : IHotasBaseMap
{
public event EventHandler<AxisDirectionChangedEventArgs> OnAxisDirectionChanged;
public event EventHandler<AxisSegmentChangedEventArgs> OnAxisSegmentChanged;
public event EventHandler<AxisDirectionChangedEventArgs> AxisDirectionChanged;
public event EventHandler<AxisSegmentChangedEventArgs> AxisSegmentChanged;

public int MapId { get; set; }
public string MapName { get; set; }
[SierraJsonNoHide]
public HOTASButton.ButtonType Type { get; set; }
public ObservableCollection<HOTASButton> ButtonMap { get; set; }
public ObservableCollection<HOTASButton> ReverseButtonMap { get; set; }
Expand Down Expand Up @@ -106,7 +107,7 @@ private void SetDirection(int value)
Direction = AxisDirection.Forward;
}

OnAxisDirectionChanged?.Invoke(this, new AxisDirectionChangedEventArgs() { NewDirection = Direction });
AxisDirectionChanged?.Invoke(this, new AxisDirectionChangedEventArgs() { NewDirection = Direction });
}

private void DetectSelectedSegment(int value)
Expand All @@ -121,7 +122,7 @@ private void DetectSelectedSegment(int value)

_currentSegment = newSegment;
IsSegmentChanged = true;
OnAxisSegmentChanged?.Invoke(this, new AxisSegmentChangedEventArgs() { NewSegment = _currentSegment });
AxisSegmentChanged?.Invoke(this, new AxisSegmentChangedEventArgs() { NewSegment = _currentSegment });
}

public void CalculateSegmentRange(int segments)
Expand Down
1 change: 1 addition & 0 deletions src/Models/HOTASButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public enum ButtonType

public int MapId { get; set; }
public string MapName { get; set; }
[SierraJsonNoHide]
public ButtonType Type { get; set; }
public int ShiftModePage { get; set; }
public bool IsShift { get; set; }
Expand Down
30 changes: 3 additions & 27 deletions src/Models/HOTASQueue.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
//todo can remove sharpdx dependency...replace JoystickUpdate with custom array. can do this in the joystickwrapper for the call to GetCurrentState
using SharpDX.DirectInput;
using SierraHOTAS.Win32;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using SierraHOTAS.Win32;

namespace SierraHOTAS.Models
{
Expand Down Expand Up @@ -39,7 +38,6 @@ public class HOTASQueue : IHOTASQueue

private IKeyboard Keyboard { get; set; }
private IJoystick Joystick { get; set; }
private ObservableCollection<IHotasBaseMap> _buttonMap;
private Dictionary<int, ObservableCollection<IHotasBaseMap>> _modes;

public HOTASQueue(IKeyboard keyboard)
Expand All @@ -51,10 +49,9 @@ public void Listen(IJoystick joystick, Dictionary<int, ObservableCollection<IHot
{
Joystick = joystick;
_modes = modes;
_mode = (_modes != null && _modes.Any()) ? _modes.First().Key : 1;
_modeActivationButtons = modeActivationButtons;

if (_modes.Count > 0) _buttonMap = _modes[1];

_jitterDetectionDictionary = new Dictionary<int, JitterDetection>();

_actionJobs = new BlockingCollection<ActionJobItem>();
Expand Down Expand Up @@ -481,29 +478,15 @@ private void OnAxisChanged(JoystickUpdate state)
AxisChanged?.Invoke(this, new AxisChangedEventArgs() { AxisId = (int)state.Offset, Value = state.Value, Device = null });
}

//public IHotasBaseMap GetMap(int buttonOffset)
//{
// return _buttonMap.FirstOrDefault(m => m.MapId == buttonOffset);
//}

public IHotasBaseMap GetMap(int buttonOffset)
{
var map = _buttonMap.FirstOrDefault(m => m.MapId == buttonOffset);
if (map == null)
{
Debug.WriteLine("fuck");
}
return map;
return _modes[_mode].FirstOrDefault(m => m.MapId == buttonOffset);
}

private IHotasBaseMap GetMapFromParentMode(int parentModeId, int buttonOffset)
{
var parentMode = _modes[parentModeId];
var map = parentMode.FirstOrDefault(m => m.MapId == buttonOffset) as HOTASButton;
//if (map.ActionCatalogItem.Actions.Count > 0 && map.ShiftModePage <= 0)
//{
// ModeSelected?.Invoke(this, new ModeSelectedEventArgs(){IsShift = true, Mode = parentModeId });
//}
return map;
}

Expand All @@ -517,16 +500,9 @@ private void OnButtonRelease(int buttonId)
ButtonReleased?.Invoke(this, new ButtonPressedEventArgs() { ButtonId = buttonId, Device = null });
}

[Obsolete]
public void SetButtonMap(ObservableCollection<IHotasBaseMap> buttonMap)
{
_buttonMap = buttonMap;
}

public void ActivateMode(int mode)
{
_mode = mode;
_buttonMap = _modes[_mode];
}

public void SetModesCollection(Dictionary<int, ObservableCollection<IHotasBaseMap>> modes)
Expand Down
1 change: 0 additions & 1 deletion src/Models/IHOTASQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public interface IHOTASQueue
void ForceButtonPress(JoystickOffset offset, bool isDown);
void Stop();
IHotasBaseMap GetMap(int buttonOffset);
void SetButtonMap(ObservableCollection<IHotasBaseMap> buttonMap);
void ActivateMode(int mode);
void SetModesCollection(Dictionary<int, ObservableCollection<IHotasBaseMap>> modes);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Models/MediaPlayerWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void Play()

public void Close()
{
_audioFile.Close();
_audioFile?.Close();
}

public void Open(string sourceFilePath)
Expand Down
Loading

0 comments on commit 3b12d4d

Please sign in to comment.