Skip to content

Commit

Permalink
Working on making JoystickReactive more agnostic with controller types
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianLima committed Feb 20, 2018
1 parent 9451340 commit 69d2bf5
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 61 deletions.
85 changes: 62 additions & 23 deletions HueHue/Helpers/Modes/JoystickHelper.cs
@@ -1,12 +1,12 @@
using System;
using System.Collections.Generic;
using SharpDX.DirectInput;
using HueHue.Helpers.Devices;
using Newtonsoft.Json;
using System.IO;
using RGB.NET.Core;
using Newtonsoft.Json.Linq;
using RGB.NET.Core;
using SharpDX.DirectInput;
using System;
using System.Collections.Generic;
using System.IO;
using static HueHue.Helpers.Modes.JoystickButtonToColor;
using HueHue.Helpers.Devices;

namespace HueHue.Helpers.Modes
{
Expand Down Expand Up @@ -115,16 +115,17 @@ public JoystickButtonToColor()

}

public JoystickButtonToColor(Color color, JoystickOffset button, ButtonTypeEnum buttonType, byte pressedBrightness, byte releasedBrightness)
public JoystickButtonToColor(Color color, JoystickOffset button, ControlTypeEnum buttonType, byte pressedBrightness, byte releasedBrightness, int[] minCenterMaxValue)
{
this._color = color;
this._button = button;
this._button_type = buttonType;
this._control_type = buttonType;
this._pressed_brightness = pressedBrightness;
this._released_brightness = releasedBrightness;
this._Centered_brightness = releasedBrightness;
this._min_max_value = minCenterMaxValue;
}

public enum ButtonTypeEnum
public enum ControlTypeEnum
{
Color,
Brightness
Expand All @@ -141,35 +142,73 @@ public Color Color
}

private JoystickOffset _button;

/// <summary>
/// Gets or sets the desired button
/// </summary>
public JoystickOffset Button
{
get { return _button; }
set { _button = value; }
}

private ButtonTypeEnum _button_type;

public ButtonTypeEnum ButtonType
private ControlTypeEnum _control_type;
/// <summary>
/// Gets or sets if this button is controlling color or brightness
/// </summary>
public ControlTypeEnum ControlType
{
get { return _button_type; }
set { _button_type = value; }
get { return _control_type; }
set { _control_type = value; }
}

private byte _pressed_brightness;

/// <summary>
/// Gets or sets the brightness to set when this button is pressed
/// </summary>
public byte PressedBrightness
{
get { return _pressed_brightness; }
set { _pressed_brightness = value; }
}

private byte _released_brightness;
private byte _Centered_brightness;
/// <summary>
/// Gets or sets the brightness to set when this button is released
/// </summary>
public byte CenteredBrightness
{
get { return _Centered_brightness; }
set { _Centered_brightness = value; }
}

public byte ReleasedBrightness
private int[] _min_max_value;
/// <summary>
/// Gets or sets the maximum known positions for this axis when ControlType is brightness
/// </summary>
public int[] MinMaxValue
{
get { return _released_brightness; }
set { _released_brightness = value; }
get { return _min_max_value; }
private set { _min_max_value = value; }
}

/// <summary>
/// Sets the min and max values for this axis
/// </summary>
public void SetMinMaxValues(int value)
{
if (this._min_max_value == null)
{
_min_max_value = new int[2] { value, value };
}

if (_min_max_value[0] > value)
{
_min_max_value[0] = value;
}
else if (value > _min_max_value[1])
{
_min_max_value[1] = value;
}
}
}

Expand Down Expand Up @@ -201,9 +240,9 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist

//Enums are stored as int64 on the JSON, to read it properly converting the index is needed
JoystickOffset button = (JoystickOffset)jo["Button"].Value<Int64>();
ButtonTypeEnum buttonType = (ButtonTypeEnum)jo["ButtonType"].Value<Int64>();
ControlTypeEnum buttonType = (ControlTypeEnum)jo["ButtonType"].Value<Int64>();

return new JoystickButtonToColor(new Color(a, r, g, b), button, buttonType, jo["PressedBrightness"].Value<byte>(), jo["ReleasedBrightness"].Value<byte>());
return new JoystickButtonToColor(new Color(a, r, g, b), button, buttonType, jo["PressedBrightness"].Value<byte>(), jo["ReleasedBrightness"].Value<byte>(), jo["MinCenterMaxValue"].Value<int[]>());
}

public override bool CanWrite
Expand Down
1 change: 1 addition & 0 deletions HueHue/MainWindow.xaml
Expand Up @@ -118,5 +118,6 @@
<materialDesign:PackIcon Kind="Settings" Height="24" Width="24" />
</Button>
<materialDesign:Snackbar Grid.Column="1" Margin="10,0,216,0" Grid.Row="2" x:Name="snackBar" MessageQueue="{materialDesign:MessageQueue}" Style="{DynamicResource SnackbarMessage}" materialDesign:ShadowAssist.ShadowDepth="Depth2"/>
<materialDesign:DialogHost x:Name="dialogHost" CloseOnClickAway="True" Grid.RowSpan="3" Margin="0,0,0,5" Grid.ColumnSpan="2" />
</Grid>
</Window>
6 changes: 3 additions & 3 deletions HueHue/Views/AddButton.xaml
Expand Up @@ -16,7 +16,7 @@
<RowDefinition Height="119*"/>
</Grid.RowDefinitions>
<materialDesign:ColorZone Background="{DynamicResource PrimaryHueMidBrush}" Height="46"/>
<TextBlock TextWrapping="Wrap" Margin="2,0,-2,18.8" TextAlignment="Center" Height="24" VerticalAlignment="Bottom" > Center all your Axys and press a button now!</TextBlock>
<TextBlock TextWrapping="Wrap" Margin="2,0,-2,18.8" TextAlignment="Center" Height="24" VerticalAlignment="Bottom" > Center all your Axis and press a button now!</TextBlock>

<Border BorderThickness="2" BorderBrush="{DynamicResource PrimaryHueMidBrush}" MinWidth="256" MinHeight="256" ClipToBounds="True" Margin="0,44,0,0" Grid.RowSpan="2"/>
<materialDesign:Card
Expand All @@ -30,7 +30,7 @@
Style="{StaticResource MaterialDesignCircularProgressBar}"
Value="50" Height="35" Width="33"/>
</materialDesign:Card>
<ToggleButton x:Name="toggle_ignore" Style="{StaticResource MaterialDesignSwitchToggleButton}" VerticalAlignment="Top" Margin="0,84,10,0" IsChecked="{Binding .}" ToolTip="Button4 must be ignored on PS2 Guitars because the keep it pressed the whole time." Height="25" HorizontalAlignment="Right" Width="36" Grid.Row="1" />
<TextBlock TextWrapping="Wrap" Margin="0,0,51,10" TextAlignment="Right" Height="24" VerticalAlignment="Bottom" Grid.Row="1" ToolTip="Button4 must be ignored on PS2 Guitars because the keep it pressed the whole time." Text="Ignore button4" />
<ToggleButton x:Name="toggle_ignore" IsChecked="True" Style="{StaticResource MaterialDesignSwitchToggleButton}" Margin="0,0,10,10" ToolTip="Button4 must be ignored on PS2 Guitars because the keep it pressed the whole time." HorizontalAlignment="Right" Width="36" Grid.Row="1" Height="25" VerticalAlignment="Bottom" />
<TextBlock TextWrapping="Wrap" Margin="0,0,51,10" TextAlignment="Right" Height="24" VerticalAlignment="Bottom" Grid.Row="1" ToolTip="Button4 must be ignored on PS2 Guitars because the keep it pressed the whole time." Text="Ignore button4" HorizontalAlignment="Right" Width="249" />
</Grid>
</UserControl>
15 changes: 8 additions & 7 deletions HueHue/Views/AddButton.xaml.cs
Expand Up @@ -18,16 +18,15 @@ public partial class AddButton : UserControl
DispatcherTimer timer;
public JoystickButtonToColor buttonColor;
bool firstRun = true;
bool ignoreB4 = true;
ButtonTypeEnum buttonType;
ControlTypeEnum controlType;

public AddButton(Guid _guid, JoystickHelper helper, ButtonTypeEnum _buttonType)
public AddButton(Guid _guid, JoystickHelper helper, ControlTypeEnum _controlType)
{
InitializeComponent();

joystick = helper.HookJoystick(_guid);
buttonType = _buttonType;
toggle_ignore.DataContext = ignoreB4;
controlType = _controlType;
toggle_ignore.DataContext = this;

timer = new DispatcherTimer() { Interval = new TimeSpan(20) };
timer.Tick += Timer_Tick;
Expand All @@ -46,9 +45,10 @@ private void Timer_Tick(object sender, EventArgs e)
foreach (var item in datas)
{
JoystickUpdate x = datas[0];
if ((x.Offset != JoystickOffset.Buttons4 && !ignoreB4) && x.Value > 0)
if ((x.Offset != JoystickOffset.Buttons4 && (toggle_ignore.IsChecked ?? false)) && x.Value > 0)
{
buttonColor = new JoystickButtonToColor() { Button = x.Offset, Color = new Color(), ButtonType = buttonType, PressedBrightness = 64, ReleasedBrightness = 255 };
buttonColor = new JoystickButtonToColor() { Button = x.Offset, Color = new Color(), ControlType = controlType, PressedBrightness = 64, CenteredBrightness = 255 };
buttonColor.SetMinMaxValues(x.Value);
timer.Stop();
DialogHost.CloseDialogCommand.Execute(buttonColor, this);
}
Expand All @@ -60,6 +60,7 @@ private void Timer_Tick(object sender, EventArgs e)

private void UserControl_DialogClosing(object sender, DialogClosingEventArgs eventArgs)
{
GC.Collect();
}
}
}
8 changes: 4 additions & 4 deletions HueHue/Views/ButtonBrightnessPicker.xaml
Expand Up @@ -14,11 +14,11 @@
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Slider Margin="4.4,8,9,0" VerticalAlignment="Top" Grid.Column="1" x:Name="PressedBrightness" Value="{Binding PressedBrightness}" Style="{StaticResource MaterialDesignDiscreteSlider}" Minimum="0" Maximum="255"/>
<Label Content="Pressed Brightness:" HorizontalAlignment="Left" Margin="20,36,0,0" VerticalAlignment="Top"/>
<Label Content="Released Brightness:" Margin="11,62,166,0" VerticalAlignment="Top" Grid.ColumnSpan="2"/>
<Label Content="Binded Button:" Margin="45,10,0.6,0" VerticalAlignment="Top"/>
<Label Content="Pressed Brightness:" HorizontalAlignment="Left" Margin="24,37,0,0" VerticalAlignment="Top" HorizontalContentAlignment="Right"/>
<Label Content="Centered Brightness:" Margin="15,62,2,0" VerticalAlignment="Top" HorizontalContentAlignment="Right"/>
<Label Content="Binded Button:" Margin="44,10,2,0" VerticalAlignment="Top" HorizontalContentAlignment="Right"/>
<Label Content="{Binding Button}" Margin="4.4,10,10,0" VerticalAlignment="Top" Grid.Column="1" x:Name="BindedButton" Height="26"/>
<Rectangle Margin="81,91,81,0" RadiusY="4" RadiusX="4" x:Name="rectangle" x:FieldModifier="public" Grid.ColumnSpan="2" Fill="{StaticResource PrimaryHueLightBrush}" Height="4" VerticalAlignment="Top"/>
<Rectangle Margin="81,94,81,0" RadiusY="4" RadiusX="4" x:Name="rectangle" x:FieldModifier="public" Grid.ColumnSpan="2" Fill="{StaticResource PrimaryHueLightBrush}" Height="4" VerticalAlignment="Top"/>
<Slider Margin="3.4,33,10,0" VerticalAlignment="Top" Grid.Column="1" Value="{Binding ReleasedBrightness}" Style="{StaticResource MaterialDesignDiscreteSlider}" Minimum="0" Maximum="255"/>
</Grid>
</UserControl>
15 changes: 7 additions & 8 deletions HueHue/Views/JoystickMode.xaml
Expand Up @@ -34,19 +34,19 @@
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ComboBox Margin="174,4,105,0" VerticalAlignment="Top" Height="28" x:Name="combo_joysticks" SelectionChanged="combo_joysticks_SelectionChanged"/>
<TextBlock HorizontalAlignment="Left" Margin="75,16,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="16" Width="94" TextAlignment="Right" Text="Selected Joystick:"/>
<Button Content="Refresh" HorizontalAlignment="Right" VerticalAlignment="Top" Width="100" Click="Button_Click"/>
<ComboBox Margin="174,37,105,0" VerticalAlignment="Top" Height="28" x:Name="combo_MultipleButtons" SelectedIndex="{Binding JoystickMultipleButtons}" SelectionChanged="combo_MultipleButtons_SelectionChanged">
<ComboBox Margin="189,4,85,0" VerticalAlignment="Top" Height="28" x:Name="combo_joysticks" SelectionChanged="combo_joysticks_SelectionChanged"/>
<TextBlock HorizontalAlignment="Left" Margin="0,18,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="16" Width="184" TextAlignment="Right" Text="Selected Joystick:"/>
<Button Content="Refresh" HorizontalAlignment="Right" VerticalAlignment="Top" Width="80" Click="Button_Click"/>
<ComboBox Margin="189,37,85,0" VerticalAlignment="Top" Height="28" x:Name="combo_MultipleButtons" SelectedIndex="{Binding JoystickMultipleButtons}" SelectionChanged="combo_MultipleButtons_SelectionChanged">
<ComboBoxItem Content="Color LEDs alternated"/>
<ComboBoxItem Content="Divide the strip in sections"/>
</ComboBox>
<ComboBox Margin="174,70,105,0" VerticalAlignment="Top" Height="28" x:Name="combo_UseDefault" SelectedIndex="{Binding JoystickUseDefault}" SelectionChanged="combo_UseDefault_SelectionChanged">
<ComboBox Margin="189,70,85,0" VerticalAlignment="Top" Height="28" x:Name="combo_UseDefault" SelectedIndex="{Binding JoystickUseDefault}" SelectionChanged="combo_UseDefault_SelectionChanged">
<ComboBoxItem Content="Do nothing"/>
<ComboBoxItem Content="Return to a default Color"/>
</ComboBox>
<TextBlock HorizontalAlignment="Left" Margin="0,49,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="16" Width="169" TextAlignment="Right"><Run Text="When multiple buttons pressed:"/></TextBlock>
<TextBlock HorizontalAlignment="Left" Margin="8,82,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="16" Width="161" TextAlignment="Right"><Run Text="When all buttons are released:"/></TextBlock>
<TextBlock HorizontalAlignment="Left" Margin="0,49,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="16" Width="184" TextAlignment="Right" Text="When multiple buttons pressed:"/>
<TextBlock HorizontalAlignment="Left" Margin="8,82,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="16" Width="176" TextAlignment="Right" Text="When all buttons are released:"/>
<Slider x:Name="sliderSection" Visibility="Collapsed" Value="{Binding Length}" HorizontalAlignment="Right" Margin="0,71,98,0" VerticalAlignment="Top" Width="166" Height="54" Minimum="1" Maximum="30" Style="{StaticResource MaterialDesignDiscreteSlider}" Grid.RowSpan="2"/>
<TextBlock x:Name="textSection" Text="Section Length:" HorizontalAlignment="Left" Margin="87,8,0,0" VerticalAlignment="Top" Width="82" Grid.Row="1" Visibility="Collapsed"/>
</Grid>
Expand All @@ -61,6 +61,5 @@
ToolTip="Add Button to Brightness" VerticalAlignment="Bottom" Click="Button_AddButtonBrightness_Click" Grid.Row="3">
<materialDesign:PackIcon Kind="Brightness6" Height="24" Width="24"/>
</Button>
<!--<materialDesign:DialogHost x:Name="dialogHost" CloseOnClickAway="True" Grid.RowSpan="4" Margin="0,-30,-0.4,29.4" />-->
</Grid>
</UserControl>

0 comments on commit 69d2bf5

Please sign in to comment.