Skip to content

Commit

Permalink
Added toggle binds
Browse files Browse the repository at this point in the history
- Toggle binds can be used to display a specific page/display when a button is pressed, but returns to the previous page. A use case for this could be the Brake Magic feature on Mercedes F1 cars, where a screen displaying brake temperatures is displayed temporarily until it is de-activated.
  • Loading branch information
MorGuux committed Aug 8, 2022
1 parent 7eb26e0 commit 64dfd6d
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 13 deletions.
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// Vous pouvez spécifier toutes les valeurs ou indiquer les numéros de build et de révision par défaut
// en utilisant '*', comme indiqué ci-dessous :
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.3.0.0")]
[assembly: AssemblyFileVersion("1.3.0.0")]
[assembly: AssemblyVersion("1.4.0.0")]
[assembly: AssemblyFileVersion("1.4.0.0")]
4 changes: 4 additions & 0 deletions Property.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<StackPanel Orientation="Horizontal">
<styles:SHButtonSecondary x:Name="btnAddBind" HorizontalAlignment="Left" Click="btnAddBind_Click">New bind</styles:SHButtonSecondary>
<styles:SHButtonSecondary x:Name="btnAddCyclerBind" HorizontalAlignment="Left" Click="btnAddCyclerBind_Click">New cycler bind</styles:SHButtonSecondary>
<styles:SHButtonSecondary x:Name="btnAddToggleBind" HorizontalAlignment="Left" Click="btnAddToggleBind_Click">New toggle bind</styles:SHButtonSecondary>
</StackPanel>
<ItemsControl x:Name="pnlBinds" ItemsSource="{Binding Binds}">
<ItemsControl.Resources>
Expand All @@ -35,6 +36,9 @@
<DataTemplate DataType="{x:Type local:SwitchableCyclerBind}">
<local:CyclerBind DataContext="{Binding}"/>
</DataTemplate>
<DataTemplate DataType="{x:Type local:SwitchableToggleBind}">
<local:ToggleBind DataContext="{Binding}"/>
</DataTemplate>
</ItemsControl.Resources>
</ItemsControl>
</StackPanel>
Expand Down
5 changes: 5 additions & 0 deletions Property.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ private void btnAddCyclerBind_Click(object sender, RoutedEventArgs e)
((IList)pnlBinds.ItemsSource).Add(new SwitchableCyclerBind());
}

private void btnAddToggleBind_Click(object sender, RoutedEventArgs e)
{
((IList)pnlBinds.ItemsSource).Add(new SwitchableToggleBind());
}

private void DeleteBind_OnExecuted(object sender, ExecutedRoutedEventArgs e)
{
((SwitchableProperty)this.DataContext).Binds.Remove(e.Parameter as SwitchableBind);
Expand Down
7 changes: 7 additions & 0 deletions SwitchableProperties.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="ToggleBind.xaml.cs">
<DependentUpon>ToggleBind.xaml</DependentUpon>
</Compile>
<Compile Include="CyclerBind.xaml.cs">
<DependentUpon>CyclerBind.xaml</DependentUpon>
</Compile>
Expand All @@ -85,6 +88,10 @@
</Compile>
</ItemGroup>
<ItemGroup>
<Page Include="ToggleBind.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="CyclerBind.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
Expand Down
51 changes: 40 additions & 11 deletions SwitchablePropertiesPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ public void Init(PluginManager pluginManager)
this.TriggerEvent($"{property.Property.PropertyName}Update");
});
}
else if (bind.GetType() == typeof(SwitchableToggleBind))
{
this.AddAction($"{property.Property.PropertyName}_{bind.ActionName}", (a, b) =>
{
property.PropertyValue = property.GetToggleValue(((SwitchableToggleBind)bind).PropertyValue);
this.TriggerEvent($"{property.Property.PropertyName}Update");
});
}
}
}
}
Expand All @@ -145,37 +153,38 @@ public void Init(PluginManager pluginManager)
internal class SwitchablePropertyContainer
{
internal string PropertyValue;
internal int PropertyIndex;
private string _lastPropertyValue;
private int _propertyIndex;
internal SwitchableProperty Property;

internal string GetNextBindValue()
{
PropertyIndex = GetIndexOfBind();
_propertyIndex = GetIndexOfBind();

PropertyIndex++;
if ((PropertyIndex) > Property.Binds
_propertyIndex++;
if ((_propertyIndex) > Property.Binds
.OfType<SwitchableValueBind>()
.Count() - 1)
{
PropertyIndex = 0;
_propertyIndex = 0;
}

return GetBindValueAt(PropertyIndex);
return GetBindValueAt(_propertyIndex);
}

internal string GetPreviousBindValue()
{
PropertyIndex = GetIndexOfBind();
_propertyIndex = GetIndexOfBind();

PropertyIndex--;
if ((PropertyIndex) < 0)
_propertyIndex--;
if ((_propertyIndex) < 0)
{
PropertyIndex = Property.Binds
_propertyIndex = Property.Binds
.OfType<SwitchableValueBind>()
.Count() - 1;
}

return GetBindValueAt(PropertyIndex);
return GetBindValueAt(_propertyIndex);
}

private string GetBindValueAt(int index)
Expand All @@ -195,6 +204,20 @@ private int GetIndexOfBind()
return Property.Binds
.IndexOf(activeBind);
}

internal string GetToggleValue(string bindValue)
{
if (bindValue == PropertyValue)
{
PropertyValue = _lastPropertyValue;
}
else
{
_lastPropertyValue = PropertyValue;
PropertyValue = bindValue;
}
return PropertyValue;
}
}

public class SwitchableProperty
Expand All @@ -220,6 +243,12 @@ public class SwitchableCyclerBind : SwitchableBind
public string Direction { get; set; }
}

public class SwitchableToggleBind : SwitchableBind
{
public override string ActionName { get; set; }
public string PropertyValue { get; set; }
}

//USED FOR CONVERTING V1 SETTINGS into V1.1+
internal class OldSwitchablePropertiesSettings
{
Expand Down
34 changes: 34 additions & 0 deletions ToggleBind.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<UserControl x:Class="SwitchableProperties.ToggleBind"
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:SwitchableProperties"
xmlns:ui="clr-namespace:SimHub.Plugins.UI;assembly=SimHub.Plugins"
xmlns:styles="clr-namespace:SimHub.Plugins.Styles;assembly=SimHub.Plugins"
mc:Ignorable="d"
d:DesignHeight="30" d:DesignWidth="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
<ColumnDefinition Width="0.5*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="Bind Name" HorizontalAlignment="Left" VerticalAlignment="Center"/>
<Label Grid.Row="0" Grid.Column="2" Content="Toggle Value" HorizontalAlignment="Left" VerticalAlignment="Center"/>
<TextBox x:Name="txtBindName" Grid.Row="0" Grid.Column="1" TextWrapping="NoWrap" Text="{Binding ActionName}" Margin="5,2,20,2" VerticalContentAlignment="Center"/>
<TextBox x:Name="txtBindValue" Grid.Row="0" Grid.Column="3" TextWrapping="NoWrap" Text="{Binding PropertyValue}" Margin="5,2,6,2" VerticalContentAlignment="Center"/>
<ui:ControlsEditor Grid.Row="0" Grid.Column="4">
<ui:ControlsEditor.ActionName>
<MultiBinding StringFormat='SwitchablePropertiesPlugin.{0}_{1}'>
<Binding Path="DataContext.PropertyName" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type local:Property}}"/>
<Binding Path="ActionName"/>
</MultiBinding>
</ui:ControlsEditor.ActionName>
</ui:ControlsEditor>
<styles:SHButtonPrimary Grid.Row="0" Grid.Column="5" x:Name="btnDelete" Command="{x:Static local:Property.DeleteBindCommand}" CommandParameter="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:Bind}}}" HorizontalAlignment="Right" VerticalAlignment="Center">Delete</styles:SHButtonPrimary>
</Grid>
</UserControl>
27 changes: 27 additions & 0 deletions ToggleBind.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SwitchableProperties
{
/// <summary>
/// Interaction logic for ToggleBind.xaml
/// </summary>
public partial class ToggleBind : UserControl
{
public ToggleBind()
{
InitializeComponent();
}
}
}

0 comments on commit 64dfd6d

Please sign in to comment.