Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Motion Toggle #842

Merged
merged 3 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions HandheldCompanion/Actions/GyroActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public class GyroActions : IActions
{
public MotionInput MotionInput = MotionInput.JoystickCamera;
public MotionMode MotionMode = MotionMode.Off;
public bool MotionToggleStatus = false;
public bool MotionTogglePressed = false; // for debouncing

public ButtonState MotionTrigger = new();

Expand Down
1 change: 1 addition & 0 deletions HandheldCompanion/Controls/Mapping/GyroMapping.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
SelectionChanged="cB_UMC_MotionDefaultOffOn_SelectionChanged">
<ComboBoxItem Content="{x:Static resx:Resources.ProfilesPage_UMCMotionOff}" />
<ComboBoxItem Content="{x:Static resx:Resources.ProfilesPage_UMCMotionOn}" />
<ComboBoxItem Content="{x:Static resx:Resources.ProfilesPage_UMCMotionToggle}" />
</ComboBox>
</ui:SimpleStackPanel>
</Grid>
Expand Down
27 changes: 24 additions & 3 deletions HandheldCompanion/Managers/MotionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,34 @@ private static void CalculateMotion(ControllerState controllerState)

// update timestamp
double TotalMilliseconds = TimerManager.Stopwatch.Elapsed.TotalMilliseconds;
DeltaSeconds = (TotalMilliseconds - PreviousTotalMilliseconds) / 1000L;
DeltaSeconds = (TotalMilliseconds - PreviousTotalMilliseconds) / 1000;
PreviousTotalMilliseconds = TotalMilliseconds;

// check if motion trigger is pressed
//toggle motion when trigger is pressed
if (gyroAction.MotionMode == MotionMode.Toggle)
{
if (gyroAction.MotionTogglePressed)
{
if (!controllerState.ButtonState.ContainsTrue(gyroAction.MotionTrigger))
{
gyroAction.MotionTogglePressed = false; // disable debounce flag
}
}
else
{
if (controllerState.ButtonState.ContainsTrue(gyroAction.MotionTrigger))
{
gyroAction.MotionToggleStatus = !gyroAction.MotionToggleStatus;
gyroAction.MotionTogglePressed = true; // enable debounce flag
}
}
}
Comment on lines 146 to +170
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic for toggling motion based on the trigger press is implemented correctly. However, the debounce logic could be simplified by using a single conditional block instead of nested if statements. Consider refactoring for readability.


// check if motion input is active
bool MotionTriggered =
(gyroAction.MotionMode == MotionMode.Off && controllerState.ButtonState.ContainsTrue(gyroAction.MotionTrigger)) ||
(gyroAction.MotionMode == MotionMode.On && !controllerState.ButtonState.ContainsTrue(gyroAction.MotionTrigger));
(gyroAction.MotionMode == MotionMode.On && !controllerState.ButtonState.ContainsTrue(gyroAction.MotionTrigger)) ||
(gyroAction.MotionMode == MotionMode.Toggle && gyroAction.MotionToggleStatus);
Valkirie marked this conversation as resolved.
Show resolved Hide resolved

bool MotionMapped = action?.ActionType != ActionType.Disabled;

Expand Down
14 changes: 12 additions & 2 deletions HandheldCompanion/Properties/Resources.Designer.cs

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

8 changes: 6 additions & 2 deletions HandheldCompanion/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -762,13 +762,17 @@
</data>
<data name="ProfilesPage_UMCMotionOn" xml:space="preserve">
<value>Enabled, turn off with button(s)</value>
</data>
<data name="ProfilesPage_UMCMotionToggle" xml:space="preserve">
<value>Toggle between enabled or disabled with button(s)</value>
</data>
<data name="ProfilesPage_UMCMotionOnOff" xml:space="preserve">
<value>Motion activation</value>
</data>
<data name="ProfilesPage_UMCMotionOnOffDesc" xml:space="preserve">
<value>With motion input disabled, use selected button(s) to enable motion,
with motion input enabled, use selected button(s) to disable motion.</value>
<value>With motion input disabled, hold selected button(s) to enable motion,
with motion input enabled, hold selected button(s) to disable motion,
with motion input toggle, press selected button(s) to switch from enabled to disabled and viceversa.</value>
Valkirie marked this conversation as resolved.
Show resolved Hide resolved
</data>
<data name="ProfilesPage_UMCSelectionRightLeftDesc" xml:space="preserve">
<value>Select the device that will receive the motion commands</value>
Expand Down
3 changes: 2 additions & 1 deletion HandheldCompanion/Utils/InputUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public enum MotionOuput
public enum MotionMode
{
Off = 0,
On = 1
On = 1,
Toggle = 2
}

public enum OverlayModelMode
Expand Down
1 change: 1 addition & 0 deletions HandheldCompanion/Views/QuickPages/QuickProfilesPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,7 @@
SelectionChanged="cB_UMC_MotionDefaultOffOn_SelectionChanged">
<ComboBoxItem Content="{x:Static resx:Resources.ProfilesPage_UMCMotionOff}" />
<ComboBoxItem Content="{x:Static resx:Resources.ProfilesPage_UMCMotionOn}" />
<ComboBoxItem Content="{x:Static resx:Resources.ProfilesPage_UMCMotionToggle}" />
</ComboBox>
</Grid>

Expand Down
Loading