Skip to content

Commit

Permalink
Multiple inputs support for plugins. Enums supported in plugin GUI. A…
Browse files Browse the repository at this point in the history
…xis merger plugin implemented
  • Loading branch information
Snoothy committed Apr 11, 2018
1 parent c157f63 commit 0f9436c
Show file tree
Hide file tree
Showing 21 changed files with 224 additions and 29 deletions.
31 changes: 31 additions & 0 deletions UCR.Core/Models/CallbackMultiplexer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using HidWizards.UCR.Core.Models.Binding;

namespace HidWizards.UCR.Core.Models
{
public class CallbackMultiplexer
{
private DeviceBinding.ValueChanged _mappingUpdate;
private readonly int _index;
private readonly List<long> _cache;

public CallbackMultiplexer(List<long> cache, int index, DeviceBinding.ValueChanged mappingUpdate)
{
_mappingUpdate = mappingUpdate;
_index = index;
_cache = cache;
}

public void Update(long value)
{
_cache[_index] = value;
_mappingUpdate(value);
}

~CallbackMultiplexer()
{
_mappingUpdate = null;
}
}
}
14 changes: 8 additions & 6 deletions UCR.Core/Models/Mapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class Mapping
/* Runtime */
private Profile Profile { get; set; }
private List<long> InputCache { get; set; }
private List<CallbackMultiplexer> Multiplexer { get; set; }

[XmlIgnore]
public string FullTitle
Expand Down Expand Up @@ -54,10 +55,13 @@ internal bool IsBound()
internal void PrepareMapping()
{
InputCache = new List<long>();
foreach (var deviceBinding in DeviceBindings)
DeviceBindings.ForEach(_ => InputCache.Add(0L));
Multiplexer = new List<CallbackMultiplexer>();
for (var i = 0; i < DeviceBindings.Count; i++)
{
deviceBinding.Callback = Update;
InputCache.Add(0L);
var cm = new CallbackMultiplexer(InputCache, i, Update);
Multiplexer.Add(cm);
DeviceBindings[i].Callback = cm.Update;
}
}

Expand All @@ -81,11 +85,9 @@ internal Mapping GetOverridenMapping()

return null;
}

// TODO Add Guid to distinguish devicebindings

public void Update(long value)
{
InputCache[0] = value;
foreach (var plugin in Plugins)
{
if (plugin.State == Guid.Empty || Profile.GetRuntimeState(plugin.State))
Expand Down
1 change: 1 addition & 0 deletions UCR.Core/UCR.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
<Compile Include="Managers\DeviceGroupsManager.cs" />
<Compile Include="Managers\DevicesManager.cs" />
<Compile Include="Managers\ProfilesManager.cs" />
<Compile Include="Models\CallbackMultiplexer.cs" />
<Compile Include="Models\Device.cs" />
<Compile Include="Models\DeviceGroup.cs" />
<Compile Include="Models\Binding\DeviceBinding.cs" />
Expand Down
71 changes: 71 additions & 0 deletions UCR.Plugins/Remapper/AxisMerger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using HidWizards.UCR.Core.Attributes;
using HidWizards.UCR.Core.Models;
using HidWizards.UCR.Core.Models.Binding;
using HidWizards.UCR.Core.Utilities;

namespace HidWizards.UCR.Plugins.Remapper
{
[Plugin("Axis merger")]
[PluginInput(DeviceBindingCategory.Range, "Axis high")]
[PluginInput(DeviceBindingCategory.Range, "Axis low")]
[PluginOutput(DeviceBindingCategory.Range, "Axis")]
public class AxisMerger : Plugin
{
[PluginGui("Dead zone", ColumnOrder = 1)]
public int DeadZone { get; set; }

[PluginGui("Mode", ColumnOrder = 0)]
public AxisMergerMode Mode { get; set; }

[PluginGui("Invert high", RowOrder = 1)]
public bool InvertHigh { get; set; }

[PluginGui("Invert low", RowOrder = 2)]
public bool InvertLow { get; set; }

public AxisMerger()
{
DeadZone = 0;
}

public override void Update(params long[] values)
{
var valueHigh = values[0];
var valueLow = values[1];
long valueOutput;

if (InvertHigh) valueHigh *= -1;
if (InvertLow) valueLow *= -1;

switch (Mode)
{
case AxisMergerMode.Average:
valueOutput = (valueHigh + valueLow) / 2L;
break;
case AxisMergerMode.Greatest:
valueOutput = Math.Abs(valueHigh) > Math.Abs(valueLow) ? valueHigh : valueLow;
break;
case AxisMergerMode.Sum:
valueOutput = valueHigh + valueLow;
break;
default:
valueOutput = 0L;
break;
}

if (DeadZone != 0)
{
valueOutput = Functions.ApplyRangeDeadZone(valueOutput, DeadZone);
}
WriteOutput(0, valueOutput);
}

public enum AxisMergerMode
{
Average,
Greatest,
Sum
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using HidWizards.UCR.Core.Models.Binding;
using HidWizards.UCR.Core.Utilities;

namespace HidWizards.UCR.Plugins.AxisSplitter
namespace HidWizards.UCR.Plugins.Remapper
{
[Plugin("Axis splitter")]
[PluginInput(DeviceBindingCategory.Range, "Axis")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using HidWizards.UCR.Core.Models.Binding;
using HidWizards.UCR.Core.Utilities;

namespace HidWizards.UCR.Plugins.AxisToAxis
namespace HidWizards.UCR.Plugins.Remapper
{
[Plugin("Axis to axis")]
[PluginInput(DeviceBindingCategory.Range, "Axis")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System;
using System.Collections.Generic;
using HidWizards.UCR.Core.Attributes;
using HidWizards.UCR.Core.Models;
using HidWizards.UCR.Core.Models.Binding;
using HidWizards.UCR.Core.Utilities;

namespace HidWizards.UCR.Plugins.AxisToButton
namespace HidWizards.UCR.Plugins.Remapper
{
[Plugin("Axis to button")]
[PluginInput(DeviceBindingCategory.Range, "Axis")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using HidWizards.UCR.Core.Models.Binding;
using HidWizards.UCR.Core.Utilities;

namespace HidWizards.UCR.Plugins.AxisToMouse
namespace HidWizards.UCR.Plugins.Remapper
{
[Plugin("Axis to mouse", Disabled = true)]
[PluginInput(DeviceBindingCategory.Range, "Axis")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using HidWizards.UCR.Core.Models.Binding;
using HidWizards.UCR.Core.Utilities;

namespace HidWizards.UCR.Plugins.ButtonToAxis
namespace HidWizards.UCR.Plugins.Remapper
{
[Plugin("Button to axis")]
[PluginInput(DeviceBindingCategory.Momentary, "Button")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using HidWizards.UCR.Core.Models;
using HidWizards.UCR.Core.Models.Binding;

namespace HidWizards.UCR.Plugins.ButtonToButton
namespace HidWizards.UCR.Plugins.Remapper
{
[Plugin("Button to button")]
[PluginInput(DeviceBindingCategory.Momentary, "Button")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System.Collections.Generic;
using HidWizards.UCR.Core.Attributes;
using HidWizards.UCR.Core.Attributes;
using HidWizards.UCR.Core.Models;
using HidWizards.UCR.Core.Models.Binding;
using HidWizards.UCR.Core.Utilities;

namespace HidWizards.UCR.Plugins.MouseToAxis
namespace HidWizards.UCR.Plugins.Remapper
{
[Plugin("Mouse to Axis", Disabled = true)]
[PluginInput(DeviceBindingCategory.Delta, "Mouse axis")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using HidWizards.UCR.Core.Models;
using HidWizards.UCR.Core.Models.Binding;

namespace HidWizards.UCR.Plugins.ButtonToState
namespace HidWizards.UCR.Plugins.State
{
[Plugin("Button to state")]
[PluginInput(DeviceBindingCategory.Momentary, "Button")]
Expand Down
17 changes: 9 additions & 8 deletions UCR.Plugins/UCR.Plugins.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,15 @@
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="AxisSplitter\AxisSplitter.cs" />
<Compile Include="AxisToAxis\AxisToAxis.cs" />
<Compile Include="AxisToButton\AxisToButton.cs" />
<Compile Include="AxisToMouse\AxisToMouse.cs" />
<Compile Include="ButtonToAxis\ButtonToAxis.cs" />
<Compile Include="ButtonToButton\ButtonToButton.cs" />
<Compile Include="ButtonToState\ButtonToState.cs" />
<Compile Include="MouseToAxis\MouseToAxis.cs" />
<Compile Include="Remapper\AxisMerger.cs" />
<Compile Include="Remapper\AxisSplitter.cs" />
<Compile Include="Remapper\AxisToAxis.cs" />
<Compile Include="Remapper\AxisToButton.cs" />
<Compile Include="Remapper\AxisToMouse.cs" />
<Compile Include="Remapper\ButtonToAxis.cs" />
<Compile Include="Remapper\ButtonToButton.cs" />
<Compile Include="State\ButtonToState.cs" />
<Compile Include="Remapper\MouseToAxis.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion UCR.Tests/ModelTests/AttributeTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using HidWizards.UCR.Core.Models.Binding;
using HidWizards.UCR.Plugins.ButtonToButton;
using HidWizards.UCR.Plugins.Remapper;
using NUnit.Framework;

namespace HidWizards.UCR.Tests.ModelTests
Expand Down
3 changes: 1 addition & 2 deletions UCR.Tests/ModelTests/PersistenceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
using HidWizards.UCR.Core;
using HidWizards.UCR.Core.Models;
using HidWizards.UCR.Core.Models.Binding;
using HidWizards.UCR.Plugins.AxisToAxis;
using HidWizards.UCR.Plugins.ButtonToButton;
using HidWizards.UCR.Plugins.Remapper;
using HidWizards.UCR.Tests.Factory;
using NUnit.Framework;

Expand Down
4 changes: 3 additions & 1 deletion UCR.Tests/ModelTests/ProfileTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using HidWizards.UCR.Core;
using HidWizards.UCR.Core.Managers;
using HidWizards.UCR.Core.Models;
using HidWizards.UCR.Core.Models.Binding;
using HidWizards.UCR.Plugins.ButtonToButton;
using HidWizards.UCR.Plugins.Remapper;
using HidWizards.UCR.Tests.Factory;
using NUnit.Framework;

Expand Down
7 changes: 7 additions & 0 deletions UCR/UCR.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@
<Compile Include="Views\Controls\PluginGuiTemplateSelector.cs" />
<Compile Include="Views\Controls\PluginViewTemplateSelector.cs" />
<Compile Include="ViewModels\DeviceViewModels\DeviceListControlViewModel.cs" />
<Compile Include="Views\Controls\EnumControl.xaml.cs">
<DependentUpon>EnumControl.xaml</DependentUpon>
</Compile>
<Compile Include="Views\Controls\StateControl.xaml.cs">
<DependentUpon>StateControl.xaml</DependentUpon>
</Compile>
Expand Down Expand Up @@ -177,6 +180,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Views\Controls\EnumControl.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="Views\Controls\StateControl.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down
14 changes: 14 additions & 0 deletions UCR/Views/Controls/EnumControl.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<UserControl x:Class="HidWizards.UCR.Views.Controls.EnumControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:HidWizards.UCR.Views.Controls"
mc:Ignorable="d"
d:DesignHeight="100" d:DesignWidth="300">
<Grid>
<GroupBox Header="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=PluginProperty.Name}" Width="Auto" Padding="5">
<ComboBox x:Name="EnumComboBox" Width="200" SelectionChanged="EnumComboBox_OnSelectionChanged" ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=Enums}"/>
</GroupBox>
</Grid>
</UserControl>
61 changes: 61 additions & 0 deletions UCR/Views/Controls/EnumControl.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using HidWizards.UCR.Core.Models;
using HidWizards.UCR.ViewModels;

namespace HidWizards.UCR.Views.Controls
{
public partial class EnumControl : UserControl
{
public static readonly DependencyProperty PluginPropertyProperty = DependencyProperty.Register("PluginProperty", typeof(PluginProperty), typeof(EnumControl), new PropertyMetadata(default(PluginProperty)));

public PluginProperty PluginProperty
{
get { return (PluginProperty)GetValue(PluginPropertyProperty); }
set { SetValue(PluginPropertyProperty, value); }
}

public ObservableCollection<ComboBoxItemViewModel> Enums { get; set; }

public EnumControl()
{
InitializeComponent();
Loaded += UserControl_Loaded;
}

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
if (PluginProperty == null) return;
PopulateEnums();
}

private void PopulateEnums()
{
Enums = new ObservableCollection<ComboBoxItemViewModel>();
ComboBoxItemViewModel selectedEnum = null;
foreach (var enumValue in Enum.GetValues(PluginProperty.Property.GetType()))
{
var comboBoxItem = new ComboBoxItemViewModel(enumValue.ToString(), enumValue);
Enums.Add(comboBoxItem);
if (enumValue.Equals(PluginProperty.Property)) selectedEnum = comboBoxItem;
}
EnumComboBox.ItemsSource = Enums;
if (selectedEnum != null)
{
EnumComboBox.SelectedItem = selectedEnum;
}
else
{
EnumComboBox.SelectedIndex = 0;
}
}


private void EnumComboBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
PluginProperty.Property = ((ComboBoxItemViewModel) EnumComboBox.SelectedItem).Value;
}
}
}
3 changes: 3 additions & 0 deletions UCR/Views/Controls/PluginControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@
<DataTemplate x:Key="StateTemplate">
<controls:StateControl PluginProperty="{Binding}"/>
</DataTemplate>
<DataTemplate x:Key="EnumTemplate">
<controls:EnumControl PluginProperty="{Binding}"/>
</DataTemplate>

</ItemsControl.Resources>
</ItemsControl>
Expand Down
Loading

0 comments on commit 0f9436c

Please sign in to comment.