Skip to content

Commit

Permalink
add ability to remove ActionItems from the catalog; change catalog so…
Browse files Browse the repository at this point in the history
…rt to be case-insensitive; refactor Clone to be a deep copy instead of shallow copy;
  • Loading branch information
joekolodz committed Mar 21, 2024
1 parent 1f379e9 commit 5c2aef9
Show file tree
Hide file tree
Showing 16 changed files with 329 additions and 36 deletions.
48 changes: 40 additions & 8 deletions src/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:tb="http://www.hardcodet.net/taskbar"
xmlns:controls="clr-namespace:SierraHOTAS.Controls"
xmlns:dataTemplates="clr-namespace:SierraHOTAS.ViewModels.DataTemplates">
xmlns:dataTemplates="clr-namespace:SierraHOTAS.ViewModels.DataTemplates"
xmlns:models="clr-namespace:SierraHOTAS.Models">

<Application.Resources>

Expand Down Expand Up @@ -35,6 +36,8 @@
<controls:AxisValueToPercentageConverter x:Key="AxisValueToPercentageConverter"/>
<controls:FileNameConverter x:Key="FileNameConverter"/>
<controls:NullToVisibilityConverter x:Key="NullToVisibilityConverter"/>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>


<BooleanToVisibilityConverter x:Key="VisibleIfTrueConverter" />

Expand Down Expand Up @@ -208,14 +211,14 @@
<Setter Property="Background" Value="{StaticResource HoverBrush}"/>
</Trigger>

<!-- highlighting the selected row -->
<!-- highlighting the selected row -->
<Trigger Property="IsSelected" Value="True">
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="BorderBrush" Value="{StaticResource SelectedBorderBrush}"/>
<Setter Property="Background" Value="{StaticResource SelectedBrush}"/>
</Trigger>

<!-- if a device becomes unconnected, then grey out the item -->
<!-- if a device becomes unconnected, then grey out the item -->
<DataTrigger Binding="{Binding IsDeviceLoaded}" Value="false">
<Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
</DataTrigger>
Expand All @@ -225,8 +228,8 @@

</Style.Triggers>



<!-- highlight color (needs mouseover trigger)-->
<Setter Property="Template">
<Setter.Value>
Expand All @@ -241,7 +244,7 @@
</Setter>

</Style>

<!-- device list view column header style -->
<Style x:Key="DeviceListViewGridViewStyle" TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="Height" Value="24"/>
Expand Down Expand Up @@ -445,11 +448,40 @@
</ControlTemplate>

<Style x:Key="{x:Type ComboBox}" TargetType="ComboBox">
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate DataType="{x:Type models:ActionCatalogItem}">
<StackPanel Orientation="Horizontal">
<TextBlock
Text="{Binding ActionName}"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Focusable="True"
Background="Transparent"
Foreground="{StaticResource TextBrush}"
Width="300"
/>
<Button Command="{Binding RemoveActionCatalogItemCommand}"
CommandParameter="{Binding}"
Visibility="{Binding IsRemovable, Converter={StaticResource BooleanToVisibilityConverter}}"
ToolTip="Remove Action"
ToolTipService.InitialShowDelay="800"
HorizontalContentAlignment="Right"
HorizontalAlignment="Right"
Background="Transparent"
BorderBrush="Transparent">
<Image Source="../Images/remove_red_26x26.png" Height="12" Width="12"/>
</Button>
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
<Setter Property="Foreground" Value="{StaticResource TextBrush}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBox">
<Grid>
<CheckBox></CheckBox>
<ToggleButton
Name="ToggleButton"
Template="{StaticResource ComboBoxToggleButton}"
Expand Down Expand Up @@ -563,7 +595,7 @@
<Setter Property="Foreground" Value="{StaticResource TextHeaderBrush}"/>
<Setter Property="Margin" Value="2"/>
</Style>

<Style x:Key="DataGridCellStyle" TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
Expand All @@ -573,7 +605,7 @@
</Trigger>
</Style.Triggers>
</Style>

<Style x:Key="DataGridRowStyle" TargetType="{x:Type DataGridRow}">
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Foreground" Value="{StaticResource TextBrush}"/>
Expand Down
31 changes: 26 additions & 5 deletions src/Models/ActionCatalog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ public ActionCatalog()
Catalog.CollectionChanged += Catalog_CollectionChanged;
}

public void PostDeserializeProcess()
{
foreach (var item in Catalog)
{
item.RemoveRequested += RemoveRequested_Handler;
}
}

private void Catalog_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.OldItems != null)
Expand All @@ -38,13 +46,16 @@ private void Catalog_CollectionChanged(object sender, NotifyCollectionChangedEve
item.PropertyChanged += Item_PropertyChanged;
}
}

CollectionChanged?.Invoke(sender, e);
}

private void Item_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (sender is ActionCatalogItem item)
{
ReSortItem(item);
//Catalog = Catalog.OrderBy(x => x.ActionName).ToObservableCollection();
}
}

Expand All @@ -66,11 +77,6 @@ private int GetSortPosition(ActionCatalogItem item)
return i;
}

//public void Sort()
//{
// Catalog = Catalog.OrderBy(x => x.ActionName).ToObservableCollection();
//}

public void Clear()
{
Catalog.Clear();
Expand All @@ -95,8 +101,10 @@ public void Add(ActionCatalogItem item)
{
var i = Catalog.First(x => x.ActionName == item.ActionName);
Catalog.Remove(i);
i.RemoveRequested -= RemoveRequested_Handler;
}

item.RemoveRequested += RemoveRequested_Handler;
Catalog.Add(item);
}

Expand All @@ -108,13 +116,26 @@ public void Add(ActionCatalogItem item, string buttonName)
{
item.ActionName = $"Action for {buttonName}";
}

item.RemoveRequested += RemoveRequested_Handler;
Insert(item);
}

private void RemoveRequested_Handler(object sender, ActionCatalogItemRemovedRequestedEventArgs e)
{
if (Catalog.Contains(e.ActionCatalogItem))
{
if (e.ActionCatalogItem.NoAction) return;
Catalog.Remove(e.ActionCatalogItem);
}
e.ActionCatalogItem.RemoveRequested -= RemoveRequested_Handler;
}

private void Insert(ActionCatalogItem item)
{
var index = GetSortPosition(item);
Catalog.Insert(index, item);
item.RemoveRequested += RemoveRequested_Handler;
Logging.Log.Debug($"[{item.ActionName}] added to actions catalog at position: {index}");
}

Expand Down
21 changes: 19 additions & 2 deletions src/Models/ActionCatalogItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,25 @@
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using SierraHOTAS.ViewModels.Commands;
using System.Windows.Input;
using SierraJSON;

namespace SierraHOTAS.Models
{
public class ActionCatalogItem : INotifyPropertyChanged, IComparable<ActionCatalogItem>
{
public event EventHandler<ActionCatalogItemRemovedRequestedEventArgs> RemoveRequested;

private string _actionName;
public const string NO_ACTION_TEXT = "<No Action>";

public Guid Id { get; set; }

[SierraJsonIgnore]
public bool NoAction { get; set; } = false;

[SierraJsonIgnore] public bool IsRemovable => !NoAction;

[DefaultValue("")]
public string ActionName
Expand All @@ -34,23 +42,32 @@ public string ActionName
public int CompareTo(ActionCatalogItem other)
{
if (other == null) return 1;
var value = string.CompareOrdinal(other.ActionName, ActionName) * -1;
var value = string.Compare(other.ActionName, ActionName, comparisonType: StringComparison.OrdinalIgnoreCase) * -1;
Logging.Log.Info($"Comparing:'{other.ActionName}' to '{ActionName}': {value}");
return value;
}

public override string ToString()
{
return $"{ActionName} - {Actions.Count}";
return $"{ActionName} ({Actions.Count} button actions)";
}

private ICommand _removeActionCatalogItemCommand;

public ICommand RemoveActionCatalogItemCommand => _removeActionCatalogItemCommand ?? (_removeActionCatalogItemCommand = new CommandHandlerWithParameter<ActionCatalogItem>(RemoveActionCatalogItem));

public ActionCatalogItem()
{
ActionName = "";
Actions = new ObservableCollection<ButtonAction>();
Id = Guid.NewGuid();
}

private void RemoveActionCatalogItem(ActionCatalogItem item)
{
RemoveRequested?.Invoke(this, new ActionCatalogItemRemovedRequestedEventArgs(item));
}

public static ActionCatalogItem EmptyItem()
{
return new ActionCatalogItem {Id = Guid.Empty, NoAction = true, ActionName = NO_ACTION_TEXT, Actions = new ObservableCollection<ButtonAction>() };
Expand Down
13 changes: 13 additions & 0 deletions src/Models/ActionCatalogItemRemovedRequestedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;

namespace SierraHOTAS.Models
{
public class ActionCatalogItemRemovedRequestedEventArgs : EventArgs
{
public ActionCatalogItem ActionCatalogItem { get; set; }
public ActionCatalogItemRemovedRequestedEventArgs(ActionCatalogItem item)
{
ActionCatalogItem = item;
}
}
}
1 change: 1 addition & 0 deletions src/Models/FileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ public IHOTASCollection FileOpen(string path)
if (version == HOTASCollection.FileFormatVersion)
{
collection = Serializer.ToObject<HOTASCollection>(json, new CustomSierraJsonConverter());
collection.ActionCatalog.PostDeserializeProcess();
}
//else - based on version, use factory to get old version and convert/map to latest version, then re-save
else
Expand Down
49 changes: 47 additions & 2 deletions src/Models/HOTASAxis.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using SierraHOTAS.ViewModels;
using SierraJSON;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using SierraJSON;

namespace SierraHOTAS.Models
{
Expand Down Expand Up @@ -42,6 +42,51 @@ public HOTASAxis()
_previousValues = new int[_arraySize];
}

/// <summary>
/// All properties are copied except for ActionCatalogItem because ActionCatalogItem is a reference to an entry in a global dictionary that we don't want to duplicate
/// New Segment property changed handlers are added, not re-referenced.
/// </summary>
/// <returns></returns>
public IHotasBaseMap Clone()
{
var cloneButtonMap = new ObservableCollection<HOTASButton>();
foreach (var b in ButtonMap)
{
cloneButtonMap.Add((HOTASButton)b.Clone());
}

var cloneReverseButtonMap = new ObservableCollection<HOTASButton>();
foreach (var b in ReverseButtonMap)
{
cloneButtonMap.Add((HOTASButton)b.Clone());
}

var cloneSegments = new ObservableCollection<Segment>();
foreach (var s in Segments)
{
cloneSegments.Add(s.Clone());
}

var clone = new HOTASAxis()
{
MapId = MapId,
MapName = MapName,
Type = Type,
ButtonMap = cloneButtonMap,
ReverseButtonMap = cloneReverseButtonMap,
IsDirectional = IsDirectional,
IsMultiAction = IsMultiAction,
SoundFileName = SoundFileName,
SoundVolume = SoundVolume,
Segments = cloneSegments,
Direction = Direction
};

clone.AddSegmentBoundaryHandlers();

return clone;
}

public void SetAxis(int value)
{
SetDirection(value);
Expand Down Expand Up @@ -115,7 +160,7 @@ public void CreateActionMapList()
}
}

private void AddSegmentBoundaryHandlers()
public void AddSegmentBoundaryHandlers()
{
foreach (var item in Segments)
{
Expand Down
Loading

0 comments on commit 5c2aef9

Please sign in to comment.