Skip to content

Commit

Permalink
Improve virtual trackpads (#597)
Browse files Browse the repository at this point in the history
* improve the way we handle virtual trackpads opacity

- add small vibration when virtual trackpads are touched (make this a setting ?)

* use Rumble()

* add touchleave on trackpads

Should fix the opacity issue.

* add Duration setting on Rumble
  • Loading branch information
Valkirie authored May 30, 2023
1 parent d193c20 commit 99a2f09
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 49 deletions.
2 changes: 1 addition & 1 deletion ControllerCommon/Controllers/IController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ public virtual bool IsConnected()
return false;
}

public virtual void Rumble(int loop)
public virtual void Rumble(int Loop = 1, byte LeftValue = byte.MaxValue, byte RightValue = byte.MaxValue, byte Duration = 125)
{ }

public virtual bool IsPlugged()
Expand Down
2 changes: 1 addition & 1 deletion HandheldCompanion/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
<value>0</value>
</setting>
<setting name="OverlayControllerOpacity" serializeAs="String">
<value>1</value>
<value>0.25</value>
</setting>
<setting name="OverlayControllerBackgroundColor" serializeAs="String">
<value>#00000000</value>
Expand Down
10 changes: 4 additions & 6 deletions HandheldCompanion/Controllers/NeptuneController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,22 +290,20 @@ public virtual bool IsVirtualMuted()
return isVirtualMuted;
}

public override void Rumble(int loop)
public override void Rumble(int Loop = 1, byte LeftValue = byte.MaxValue, byte RightValue = byte.MaxValue, byte Duration = 125)
{
Task.Factory.StartNew(async () =>
{
for (int i = 0; i < loop * 2; i++)
for (int i = 0; i < Loop * 2; i++)
{
if (i % 2 == 0)
SetVibration(byte.MaxValue, byte.MaxValue);
SetVibration(LeftValue, RightValue);
else
SetVibration(0, 0);

await Task.Delay(125);
await Task.Delay(Duration);
}
});

base.Rumble(loop);
}

public override void Plug()
Expand Down
10 changes: 4 additions & 6 deletions HandheldCompanion/Controllers/XInputController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,22 +283,20 @@ public override void SetVibration(byte LargeMotor, byte SmallMotor)
Controller.SetVibration(vibration);
}

public override void Rumble(int loop)
public override void Rumble(int Loop = 1, byte LeftValue = byte.MaxValue, byte RightValue = byte.MaxValue, byte Duration = 125)
{
Task.Factory.StartNew(async () =>
{
for (int i = 0; i < loop * 2; i++)
for (int i = 0; i < Loop * 2; i++)
{
if (i % 2 == 0)
SetVibration(byte.MaxValue, byte.MaxValue);
SetVibration(LeftValue, RightValue);
else
SetVibration(0, 0);

await Task.Delay(125);
await Task.Delay(Duration);
}
});

base.Rumble(loop);
}

public override void Plug()
Expand Down
2 changes: 1 addition & 1 deletion HandheldCompanion/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion HandheldCompanion/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
<Value Profile="(Default)">0</Value>
</Setting>
<Setting Name="OverlayControllerOpacity" Type="System.Double" Scope="User">
<Value Profile="(Default)">1</Value>
<Value Profile="(Default)">0.25</Value>
</Setting>
<Setting Name="OverlayControllerBackgroundColor" Type="System.String" Scope="User">
<Value Profile="(Default)">#00000000</Value>
Expand Down
21 changes: 0 additions & 21 deletions HandheldCompanion/Views/Pages/OverlayPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,6 @@ private void UpdateUI_TrackpadsPosition(int trackpadsAlignment)
else
button.Style = Application.Current.FindResource("DefaultButtonStyle") as Style;
}

switch (trackpadsAlignment)
{
case 0:
MainWindow.overlayTrackpad.VerticalAlignment = VerticalAlignment.Top;
break;
case 1:
MainWindow.overlayTrackpad.VerticalAlignment = VerticalAlignment.Center;
break;
case 2:
MainWindow.overlayTrackpad.VerticalAlignment = VerticalAlignment.Bottom;
break;
}
}

private void UpdateUI_ControllerPosition(int controllerAlignment)
Expand Down Expand Up @@ -188,11 +175,6 @@ private void SliderControllerSize_ValueChanged(object sender, RoutedPropertyChan

private void SliderTrackpadsSize_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
MainWindow.overlayTrackpad.LeftTrackpad.Width = SliderTrackpadsSize.Value;
MainWindow.overlayTrackpad.RightTrackpad.Width = SliderTrackpadsSize.Value;
MainWindow.overlayTrackpad.Height = SliderTrackpadsSize.Value;
MainWindow.overlayTrackpad.HorizontalAlignment = HorizontalAlignment.Stretch;

if (!IsLoaded)
return;

Expand Down Expand Up @@ -234,9 +216,6 @@ private void TrackpadsAlignment_Click(object sender, RoutedEventArgs e)

private void SliderTrackpadsOpacity_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
MainWindow.overlayTrackpad.LeftTrackpad.Opacity = SliderTrackpadsOpacity.Value;
MainWindow.overlayTrackpad.RightTrackpad.Opacity = SliderTrackpadsOpacity.Value;

if (!IsLoaded)
return;

Expand Down
15 changes: 7 additions & 8 deletions HandheldCompanion/Views/Windows/OverlayTrackpad.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,39 @@
x:Class="HandheldCompanion.Views.Windows.OverlayTrackpad"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:HelixToolkit="clr-namespace:HelixToolkit.Wpf;assembly=HelixToolkit.Wpf"
xmlns:common="clr-namespace:HandheldCompanion.Views.Classes"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:HandheldCompanion.Views.Windows"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ui="http://schemas.modernwpf.com/2019"
Title="Virtual Trackpads"
HorizontalAlignment="Stretch"
Closing="Window_Closing"
WindowStartupLocation="CenterScreen"
mc:Ignorable="d">

<Grid>
<!-- Left Trackpad -->
<Border
Name="LeftTrackpad"
Height="{Binding ActualHeight, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type FrameworkElement}}}"
Margin="15,0,0,0"
HorizontalAlignment="Left"
Background="Black"
CornerRadius="10"
Opacity="0.25"
PreviewTouchDown="Trackpad_PreviewTouchDown"
PreviewTouchMove="Trackpad_PreviewTouchMove"
PreviewTouchUp="Trackpad_PreviewTouchUp" />
PreviewTouchUp="Trackpad_PreviewTouchUp"
TouchLeave="Trackpad_PreviewTouchUp" />

<!-- Right Trackpad -->
<Border
Name="RightTrackpad"
Height="{Binding ActualHeight, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type FrameworkElement}}}"
Margin="0,0,15,0"
HorizontalAlignment="Right"
Background="Black"
CornerRadius="10"
Opacity="0.25"
PreviewTouchDown="Trackpad_PreviewTouchDown"
PreviewTouchMove="Trackpad_PreviewTouchMove"
PreviewTouchUp="Trackpad_PreviewTouchUp" />
PreviewTouchUp="Trackpad_PreviewTouchUp"
TouchLeave="Trackpad_PreviewTouchUp" />
</Grid>
</common:OverlayWindow>
71 changes: 67 additions & 4 deletions HandheldCompanion/Views/Windows/OverlayTrackpad.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using ControllerCommon.Pipes;
using HandheldCompanion.Managers;
using HandheldCompanion.Views.Classes;
using System;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input;
using Application = System.Windows.Application;
using HorizontalAlignment = System.Windows.HorizontalAlignment;

namespace HandheldCompanion.Views.Windows
{
Expand All @@ -21,21 +25,74 @@ private class TouchInput
private TouchInput rightInput;
private double dpiInput;

private double TrackpadOpacity = 0.25;
private double TrackpadOpacityTouched = 0.10; // extra opacity when touched

public OverlayTrackpad()
{
InitializeComponent();

SettingsManager.SettingValueChanged += SettingsManager_SettingValueChanged;

// touch vars
dpiInput = GetWindowsScaling();
leftInput = new TouchInput();
rightInput = new TouchInput();
}

private void SettingsManager_SettingValueChanged(string name, object value)
{
// UI thread (async)
Application.Current.Dispatcher.BeginInvoke(() =>
{
switch (name)
{
case "OverlayTrackpadsSize":
{
int size = Convert.ToInt32(value);
this.LeftTrackpad.Width = size;
this.RightTrackpad.Width = size;
this.Height = size;
this.HorizontalAlignment = HorizontalAlignment.Stretch;
}
break;
case "OverlayTrackpadsAlignment":
{
int trackpadsAlignment = Convert.ToInt32(value);
switch (trackpadsAlignment)
{
case 0:
this.VerticalAlignment = VerticalAlignment.Top;
break;
case 1:
this.VerticalAlignment = VerticalAlignment.Center;
break;
case 2:
this.VerticalAlignment = VerticalAlignment.Bottom;
break;
}
}
break;
case "OverlayTrackpadsOpacity":
{
TrackpadOpacity = Convert.ToDouble(value);
LeftTrackpad.Opacity = TrackpadOpacity;
RightTrackpad.Opacity = TrackpadOpacity;
}
break;
}
});
}

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// do something
}

private void UpdateUI_TrackpadsPosition(int trackpadsAlignment)
{
}

public double GetWindowsScaling()
{
return Screen.PrimaryScreen.Bounds.Width / SystemParameters.PrimaryScreenWidth;
Expand Down Expand Up @@ -118,7 +175,10 @@ private void Trackpad_PreviewTouchDown(object sender, TouchEventArgs e)

Trackpad_TouchInput(e, CursorAction.CursorDown, CursorButton.TouchLeft);

LeftTrackpad.Opacity += 0.10;
LeftTrackpad.Opacity = TrackpadOpacity + TrackpadOpacityTouched;

// send vibration (todo: make it a setting)
ControllerManager.GetTargetController()?.Rumble(1, 25, 0, 60);
}
break;
case "RightTrackpad":
Expand All @@ -129,7 +189,10 @@ private void Trackpad_PreviewTouchDown(object sender, TouchEventArgs e)

Trackpad_TouchInput(e, CursorAction.CursorDown, CursorButton.TouchRight);

RightTrackpad.Opacity += 0.10;
RightTrackpad.Opacity = TrackpadOpacity + TrackpadOpacityTouched;

// send vibration (todo: make it a setting)
ControllerManager.GetTargetController()?.Rumble(1, 0, 25, 60);
}
break;
}
Expand All @@ -147,14 +210,14 @@ private void Trackpad_PreviewTouchUp(object sender, TouchEventArgs e)
{
leftInput.Flags = 0;
Trackpad_TouchInput(e, CursorAction.CursorUp, CursorButton.TouchLeft);
LeftTrackpad.Opacity -= 0.10;
LeftTrackpad.Opacity = TrackpadOpacity - TrackpadOpacityTouched;
}
break;
case "RightTrackpad":
{
rightInput.Flags = 0;
Trackpad_TouchInput(e, CursorAction.CursorUp, CursorButton.TouchRight);
RightTrackpad.Opacity -= 0.10;
RightTrackpad.Opacity = TrackpadOpacity - TrackpadOpacityTouched;
}
break;
}
Expand Down

0 comments on commit 99a2f09

Please sign in to comment.