From d139b38d3199a9b883e8d0bdb49b6a940cdc7afe Mon Sep 17 00:00:00 2001 From: Malin Ejdbo Date: Fri, 11 Jun 2021 16:30:16 +0200 Subject: [PATCH] Seperate the different joystick configurations into their own assets --- data/assets/base_blank.asset | 1 - data/assets/examples/basic.scene | 1 - data/assets/util/default_joystick.asset | 228 ------------------ data/assets/util/joysticks/PS4.asset | 118 +++++++++ data/assets/util/joysticks/SpaceMouse.asset | 55 +++++ .../util/joysticks/SpaceMouseNonSticky.asset | 55 +++++ data/assets/util/joysticks/Xbox.asset | 116 +++++++++ .../util/joysticks/joystick_helper.asset | 66 +++++ .../interaction/joystickcamerastates.h | 3 +- 9 files changed, 412 insertions(+), 231 deletions(-) delete mode 100644 data/assets/util/default_joystick.asset create mode 100644 data/assets/util/joysticks/PS4.asset create mode 100644 data/assets/util/joysticks/SpaceMouse.asset create mode 100644 data/assets/util/joysticks/SpaceMouseNonSticky.asset create mode 100644 data/assets/util/joysticks/Xbox.asset create mode 100644 data/assets/util/joysticks/joystick_helper.asset diff --git a/data/assets/base_blank.asset b/data/assets/base_blank.asset index 571724ac09..b318dcf2fd 100644 --- a/data/assets/base_blank.asset +++ b/data/assets/base_blank.asset @@ -10,7 +10,6 @@ asset.require('spice/base') -- Load default key bindings applicable to most scenes asset.require('dashboard/default_dashboard') asset.require('util/default_keybindings') -asset.require('util/default_joystick') -- Load web gui local webGui = asset.require('util/webgui') diff --git a/data/assets/examples/basic.scene b/data/assets/examples/basic.scene index 71fdb1b98a..b8c3eba619 100644 --- a/data/assets/examples/basic.scene +++ b/data/assets/examples/basic.scene @@ -8,7 +8,6 @@ asset.require('spice/base') asset.require('util/default_keybindings') asset.require('util/default_dashboard') -asset.require('util/default_joystick') asset.require('util/webgui') local spheres = asset.require('examples/spheres') diff --git a/data/assets/util/default_joystick.asset b/data/assets/util/default_joystick.asset deleted file mode 100644 index 8ecce48410..0000000000 --- a/data/assets/util/default_joystick.asset +++ /dev/null @@ -1,228 +0,0 @@ -local propertyHelper = asset.require('./property_helper') - --- Allowed values for the second parameter of bindJoystickAxis: --- "None" --- "Orbit X" --- "Orbit Y" --- "Zoom" -- both in and out --- "Zoom In" --- "Zoom Out" --- "LocalRoll X" --- "LocalRoll Y" --- "GlobalRoll X" --- "GlobalRoll Y" --- "Pan X" --- "Pan Y" --- Third parameter determines whether the axis should be inverted --- Fourth parameter determines whether the axis should be normalized from [-1,1] to [0,1] - - -local XBoxController = { - LeftThumbStick = { 0 , 1 }, - RightThumbStick = { 2, 3 }, - LeftTrigger = 4, - RightTrigger = 5, - A = 0, - B = 1, - X = 2, - Y = 3, - LB = 4, - RB = 5, - Select = 6, - Start = 7, - LeftStick = 8, - RightStick = 9, - DPad = { - Up = 10, - Right = 11, - Down = 12, - Left = 13 - } -} - -local PS4Controller = { - LeftThumbStick = { 0 , 1 }, - RightThumbStick = { 2, 5 }, - LeftTrigger = 3, - RightTrigger = 4, - A = 1, -- Cross - B = 2, -- Circle - X = 0, -- Square - Y = 3, -- Triangle - LB = 4, - RB = 5, - Select = 9, -- options - Start = 12, -- PS button - LeftStick = 10, - RightStick = 11, - DPad = { - Up = 14, - Right = 15, - Down = 16, - Left = 17 - } -} - -local SpaceMouse = { - Push = {0, 1, 2}, -- left/right, back/forth, up/down - Twist = {5}, -- left/right - Tilt = {4, 3}, -- left/right, back/forth - LeftButton = 0, - RightButton = 1 -} - --- Variables to store the state of the joystick between frames -Joystick = {} -Joystick.State = {} -Joystick.State.IsInRollMode = false -Joystick.State.Axis = {} - -local bindLocalRoll = function(axis) - return [[ - -- We only want to store the current state in the first mode that is enabled, otherwise we will overwrite the backup - if not Joystick.State.IsInRollMode then - -- Save current axis state - Joystick.State.Axis.Type, Joystick.State.Axis.Inverted, Joystick.State.Axis.Normalized, Joystick.State.Axis.Sticky, Joystick.State.Axis.Sensitivity = openspace.navigation.joystickAxis(]] .. axis .. [[) - end - - -- Set new axis state - openspace.navigation.bindJoystickAxis(]] .. axis .. [[, "LocalRoll X", Joystick.State.Axis.Inverted, Joystick.State.Axis.Normalized, Joystick.State.Axis.Sticky, Joystick.State.Axis.Sensitivity); - Joystick.State.IsInRollMode = true - ]] -end - -local bindGlobalRoll = function(axis) - return [[ - -- We only want to store the current state in the first mode that is enabled, otherwise we will overwrite the backup - if not Joystick.State.IsInRollMode then - -- Save current axis state - Joystick.State.Axis.Type, Joystick.State.Axis.Inverted, Joystick.State.Axis.Normalized, Joystick.State.Axis.Sticky, Joystick.State.Axis.Sensitivity = openspace.navigation.joystickAxis(]] .. axis .. [[) - end - - -- Set new axis state - openspace.navigation.bindJoystickAxis(]] .. axis .. [[, "GlobalRoll X", Joystick.State.Axis.Inverted, Joystick.State.Axis.Normalized, Joystick.State.Axis.Sticky, Joystick.State.Axis.Sensitivity); - Joystick.State.IsInRollMode = true - ]] -end - -local permaBindLocalRoll = function(axis) - return [[ - -- Save current axis state - Joystick.State.Axis.Type, Joystick.State.Axis.Inverted, Joystick.State.Axis.Normalized, Joystick.State.Axis.Sticky, Joystick.State.Axis.Sensitivity = openspace.navigation.joystickAxis(]] .. axis .. [[) - - -- Set new axis state - openspace.navigation.bindJoystickAxis(]] .. axis .. [[, "LocalRoll X", Joystick.State.Axis.Inverted, Joystick.State.Axis.Normalized, Joystick.State.Axis.Sticky, Joystick.State.Axis.Sensitivity); - ]] -end - -local permaBindGlobalRoll = function(axis) - return [[ - -- Save current axis state - Joystick.State.Axis.Type, Joystick.State.Axis.Inverted, Joystick.State.Axis.Normalized, Joystick.State.Axis.Sticky, Joystick.State.Axis.Sensitivity = openspace.navigation.joystickAxis(]] .. axis .. [[) - - -- Set new axis state - openspace.navigation.bindJoystickAxis(]] .. axis .. [[, "GlobalRoll X", Joystick.State.Axis.Inverted, Joystick.State.Axis.Normalized, Joystick.State.Axis.Sticky, Joystick.State.Axis.Sensitivity); - ]] -end - -local unbindRoll = function(axis) - return [[ - -- Reset previous state - openspace.navigation.bindJoystickAxis(]] .. axis .. [[, Joystick.State.Axis.Type, Joystick.State.Axis.Inverted, Joystick.State.Axis.Normalized, Joystick.State.Axis.Sticky, Joystick.State.Axis.Sensitivity); - ]] -end - -asset.onInitialize(function() - -- Set the controller to the connected controller - -- Currently: XBoxController, PS4Controller or SpaceMouse - local controller = XBoxController; - - -- Case of XBoxController or PS4Controller - if(controller.A ~= nil) then - openspace.navigation.setAxisDeadZone(controller.LeftThumbStick[1], 0.15) - openspace.navigation.setAxisDeadZone(controller.LeftThumbStick[2], 0.15) - openspace.navigation.setAxisDeadZone(controller.RightThumbStick[1], 0.15) - openspace.navigation.setAxisDeadZone(controller.RightThumbStick[2], 0.15) - - openspace.navigation.bindJoystickAxis(controller.LeftThumbStick[1], "Orbit X"); - openspace.navigation.bindJoystickAxis(controller.LeftThumbStick[2], "Orbit Y", true); - openspace.navigation.bindJoystickAxis(controller.RightThumbStick[1], "Pan X", true); - openspace.navigation.bindJoystickAxis(controller.RightThumbStick[2], "Pan Y", true); - openspace.navigation.bindJoystickAxis(controller.LeftTrigger, "Zoom Out", false, true); - openspace.navigation.bindJoystickAxis(controller.RightTrigger, "Zoom In", false, true); - - openspace.navigation.bindJoystickButton( - controller.LB, - bindLocalRoll(controller.RightThumbStick[1]), - "Switch to local roll mode" - ) - openspace.navigation.bindJoystickButton( - controller.LB, - unbindRoll(controller.RightThumbStick[1]), - "Switch back to normal mode", - "Release" - ) - openspace.navigation.bindJoystickButton( - controller.RB, - bindGlobalRoll(controller.RightThumbStick[1]), - "Switch to global roll mode" - ) - openspace.navigation.bindJoystickButton( - controller.RB, - unbindRoll(controller.RightThumbStick[1]), - "Switch back to normal mode", - "Release" - ) - - openspace.navigation.bindJoystickButton( - controller.A, - propertyHelper.invert('NavigationHandler.OrbitalNavigator.Friction.ZoomFriction'), - "Toggle zoom friction" - ) - openspace.navigation.bindJoystickButton( - controller.B, - propertyHelper.invert('NavigationHandler.OrbitalNavigator.Friction.RotationalFriction'), - "Toggle rotational friction" - ) - openspace.navigation.bindJoystickButton( - controller.DPad.Left, - propertyHelper.invert('NavigationHandler.OrbitalNavigator.Friction.RollFriction'), - "Toggle roll friction" - ) - - openspace.navigation.bindJoystickButton( - controller.X, - "openspace.setPropertyValueSingle('NavigationHandler.OrbitalNavigator.Aim', '');" .. - "openspace.setPropertyValueSingle('NavigationHandler.OrbitalNavigator.Anchor', 'Earth');" .. - "openspace.setPropertyValueSingle('NavigationHandler.OrbitalNavigator.RetargetAnchor', nil);", - "Switch target to Earth" - ) - openspace.navigation.bindJoystickButton( - controller.Y, - "openspace.setPropertyValueSingle('NavigationHandler.OrbitalNavigator.Aim', '');" .. - "openspace.setPropertyValueSingle('NavigationHandler.OrbitalNavigator.Anchor', 'Mars');" .. - "openspace.setPropertyValueSingle('NavigationHandler.OrbitalNavigator.RetargetAnchor', nil);", - "Switch target to Mars" - ) - -- Case of SpaceMouse - elseif (controller.LeftButton ~= nil) then - openspace.navigation.bindJoystickAxis(controller.Push[1], "Orbit X", false, false, true, 40.0); - openspace.navigation.bindJoystickAxis(controller.Push[2], "Orbit Y", false, false, true, 40.0); - openspace.navigation.bindJoystickAxis(controller.Twist[1], "Pan X", true, false, true, 40.0); - openspace.navigation.bindJoystickAxis(controller.Tilt[2], "Pan Y", false, false, true, 35.0); - openspace.navigation.bindJoystickAxis(controller.Push[3], "Zoom", false, false, true, 40.0); - openspace.navigation.bindJoystickAxis(controller.Tilt[1], "LocalRoll X", false, false, true, 35.0); - - openspace.navigation.bindJoystickButton( - controller.LeftButton, - permaBindLocalRoll(controller.Tilt[1]), - "Switch to local roll mode" - ) - - openspace.navigation.bindJoystickButton( - controller.RightButton, - permaBindGlobalRoll(controller.Tilt[1]), - "Switch to global roll mode" - ) - end -end) diff --git a/data/assets/util/joysticks/PS4.asset b/data/assets/util/joysticks/PS4.asset new file mode 100644 index 0000000000..405a3bc133 --- /dev/null +++ b/data/assets/util/joysticks/PS4.asset @@ -0,0 +1,118 @@ +local propertyHelper = asset.require('./../property_helper') +local joystickHelper = asset.require('./joystick_helper') + +-- Allowed values for the second parameter of bindJoystickAxis: +-- "None" +-- "Orbit X" +-- "Orbit Y" +-- "Zoom" -- both in and out +-- "Zoom In" +-- "Zoom Out" +-- "LocalRoll X" +-- "LocalRoll Y" +-- "GlobalRoll X" +-- "GlobalRoll Y" +-- "Pan X" +-- "Pan Y" +-- Third parameter determines whether the axis should be inverted +-- Fourth parameter determines whether the axis should be normalized from [-1,1] to [0,1] +-- Fifth parameters determins if the axis should be "Sticky" or not. +-- The axis values can either go back to 0 when the joystick is released or it can +-- stay at the value it was before the joystick was released. +-- The latter is called a sticky axis, when the values don't go back to 0. +-- Sixth parameter is the sensitivity for the axis + +local PS4Controller = { + LeftThumbStick = { 0 , 1 }, + RightThumbStick = { 2, 5 }, + L2 = 3, + R2 = 4, + Cross = 1, + Circle = 2, + Square = 0, + Triangle = 3, + L1 = 4, + R1 = 5, + Share = 8, + Options = 9, + PS = 12, + LeftStick = 10, + RightStick = 11, + TouchPad = 13, + DPad = { + Up = 14, + Right = 15, + Down = 16, + Left = 17 + } +} + +asset.onInitialize(function() + local controller = PS4Controller; + + openspace.navigation.setAxisDeadZone(controller.LeftThumbStick[1], 0.15) + openspace.navigation.setAxisDeadZone(controller.LeftThumbStick[2], 0.15) + openspace.navigation.setAxisDeadZone(controller.RightThumbStick[1], 0.15) + openspace.navigation.setAxisDeadZone(controller.RightThumbStick[2], 0.15) + + openspace.navigation.bindJoystickAxis(controller.LeftThumbStick[1], "Orbit X"); + openspace.navigation.bindJoystickAxis(controller.LeftThumbStick[2], "Orbit Y", true); + openspace.navigation.bindJoystickAxis(controller.RightThumbStick[1], "Pan X", true); + openspace.navigation.bindJoystickAxis(controller.RightThumbStick[2], "Pan Y", true); + openspace.navigation.bindJoystickAxis(controller.L2, "Zoom Out", false, true); + openspace.navigation.bindJoystickAxis(controller.R2, "Zoom In", false, true); + + openspace.navigation.bindJoystickButton( + controller.L1, + joystickHelper.bindLocalRoll(controller.RightThumbStick[1]), + "Switch to local roll mode" + ) + openspace.navigation.bindJoystickButton( + controller.L1, + joystickHelper.unbindRoll(controller.RightThumbStick[1]), + "Switch back to normal mode", + "Release" + ) + openspace.navigation.bindJoystickButton( + controller.R1, + joystickHelper.bindGlobalRoll(controller.RightThumbStick[1]), + "Switch to global roll mode" + ) + openspace.navigation.bindJoystickButton( + controller.R1, + joystickHelper.unbindRoll(controller.RightThumbStick[1]), + "Switch back to normal mode", + "Release" + ) + + openspace.navigation.bindJoystickButton( + controller.Cross, + propertyHelper.invert('NavigationHandler.OrbitalNavigator.Friction.ZoomFriction'), + "Toggle zoom friction" + ) + openspace.navigation.bindJoystickButton( + controller.Circle, + propertyHelper.invert('NavigationHandler.OrbitalNavigator.Friction.RotationalFriction'), + "Toggle rotational friction" + ) + openspace.navigation.bindJoystickButton( + controller.DPad.Left, + propertyHelper.invert('NavigationHandler.OrbitalNavigator.Friction.RollFriction'), + "Toggle roll friction" + ) + + openspace.navigation.bindJoystickButton( + controller.Square, + "openspace.setPropertyValueSingle('NavigationHandler.OrbitalNavigator.Aim', '');" .. + "openspace.setPropertyValueSingle('NavigationHandler.OrbitalNavigator.Anchor', 'Earth');" .. + "openspace.setPropertyValueSingle('NavigationHandler.OrbitalNavigator.RetargetAnchor', nil);", + "Switch target to Earth" + ) + openspace.navigation.bindJoystickButton( + controller.Triangle, + "openspace.setPropertyValueSingle('NavigationHandler.OrbitalNavigator.Aim', '');" .. + "openspace.setPropertyValueSingle('NavigationHandler.OrbitalNavigator.Anchor', 'Mars');" .. + "openspace.setPropertyValueSingle('NavigationHandler.OrbitalNavigator.RetargetAnchor', nil);", + "Switch target to Mars" + ) +end) diff --git a/data/assets/util/joysticks/SpaceMouse.asset b/data/assets/util/joysticks/SpaceMouse.asset new file mode 100644 index 0000000000..f5b72915bf --- /dev/null +++ b/data/assets/util/joysticks/SpaceMouse.asset @@ -0,0 +1,55 @@ +local propertyHelper = asset.require('./../property_helper') +local joystickHelper = asset.require('./joystick_helper') + +-- Allowed values for the second parameter of bindJoystickAxis: +-- "None" +-- "Orbit X" +-- "Orbit Y" +-- "Zoom" -- both in and out +-- "Zoom In" +-- "Zoom Out" +-- "LocalRoll X" +-- "LocalRoll Y" +-- "GlobalRoll X" +-- "GlobalRoll Y" +-- "Pan X" +-- "Pan Y" +-- Third parameter determines whether the axis should be inverted +-- Fourth parameter determines whether the axis should be normalized from [-1,1] to [0,1] +-- Fifth parameters determins if the axis should be "Sticky" or not. +-- The axis values can either go back to 0 when the joystick is released or it can +-- stay at the value it was before the joystick was released. +-- The latter is called a sticky axis, when the values don't go back to 0. +-- This version of the SpaceMouse IS Sticky. +-- Sixth parameter is the sensitivity for the axis + +local SpaceMouse = { + Push = {0, 1, 2}, -- left/right, back/forth, up/down + Twist = {5}, -- left/right + Tilt = {4, 3}, -- left/right, back/forth + LeftButton = 0, + RightButton = 1 +} + +asset.onInitialize(function() + local controller = SpaceMouse; + + openspace.navigation.bindJoystickAxis(controller.Push[1], "Orbit X", false, false, true, 40.0); + openspace.navigation.bindJoystickAxis(controller.Push[2], "Orbit Y", false, false, true, 40.0); + openspace.navigation.bindJoystickAxis(controller.Twist[1], "Pan X", true, false, true, 40.0); + openspace.navigation.bindJoystickAxis(controller.Tilt[2], "Pan Y", false, false, true, 35.0); + openspace.navigation.bindJoystickAxis(controller.Push[3], "Zoom", false, false, true, 40.0); + openspace.navigation.bindJoystickAxis(controller.Tilt[1], "LocalRoll X", false, false, true, 35.0); + + openspace.navigation.bindJoystickButton( + controller.LeftButton, + joystickHelper.permaBindLocalRoll(controller.Tilt[1]), + "Switch to local roll mode" + ) + + openspace.navigation.bindJoystickButton( + controller.RightButton, + joystickHelper.permaBindGlobalRoll(controller.Tilt[1]), + "Switch to global roll mode" + ) +end) diff --git a/data/assets/util/joysticks/SpaceMouseNonSticky.asset b/data/assets/util/joysticks/SpaceMouseNonSticky.asset new file mode 100644 index 0000000000..84e8987eb5 --- /dev/null +++ b/data/assets/util/joysticks/SpaceMouseNonSticky.asset @@ -0,0 +1,55 @@ +local propertyHelper = asset.require('./../property_helper') +local joystickHelper = asset.require('./joystick_helper') + +-- Allowed values for the second parameter of bindJoystickAxis: +-- "None" +-- "Orbit X" +-- "Orbit Y" +-- "Zoom" -- both in and out +-- "Zoom In" +-- "Zoom Out" +-- "LocalRoll X" +-- "LocalRoll Y" +-- "GlobalRoll X" +-- "GlobalRoll Y" +-- "Pan X" +-- "Pan Y" +-- Third parameter determines whether the axis should be inverted +-- Fourth parameter determines whether the axis should be normalized from [-1,1] to [0,1] +-- Fifth parameters determins if the axis should be "Sticky" or not. +-- The axis values can either go back to 0 when the joystick is released or it can +-- stay at the value it was before the joystick was released. +-- The latter is called a sticky axis, when the values don't go back to 0. +-- This version of the SpaceMouse is NOT Sticky. +-- Sixth parameter is the sensitivity for the axis + +local SpaceMouse = { + Push = {0, 1, 2}, -- left/right, back/forth, up/down + Twist = {5}, -- left/right + Tilt = {4, 3}, -- left/right, back/forth + LeftButton = 0, + RightButton = 1 +} + +asset.onInitialize(function() + local controller = SpaceMouse; + + openspace.navigation.bindJoystickAxis(controller.Push[1], "Orbit X", false); + openspace.navigation.bindJoystickAxis(controller.Push[2], "Orbit Y", false); + openspace.navigation.bindJoystickAxis(controller.Twist[1], "Pan X", true); + openspace.navigation.bindJoystickAxis(controller.Tilt[2], "Pan Y", false); + openspace.navigation.bindJoystickAxis(controller.Push[3], "Zoom", false); + openspace.navigation.bindJoystickAxis(controller.Tilt[1], "LocalRoll X", false); + + openspace.navigation.bindJoystickButton( + controller.LeftButton, + joystickHelper.permaBindLocalRoll(controller.Tilt[1]), + "Switch to local roll mode" + ) + + openspace.navigation.bindJoystickButton( + controller.RightButton, + joystickHelper.permaBindGlobalRoll(controller.Tilt[1]), + "Switch to global roll mode" + ) +end) diff --git a/data/assets/util/joysticks/Xbox.asset b/data/assets/util/joysticks/Xbox.asset new file mode 100644 index 0000000000..60e6ca6906 --- /dev/null +++ b/data/assets/util/joysticks/Xbox.asset @@ -0,0 +1,116 @@ +local propertyHelper = asset.require('./../property_helper') +local joystickHelper = asset.require('./joystick_helper') + +-- Allowed values for the second parameter of bindJoystickAxis: +-- "None" +-- "Orbit X" +-- "Orbit Y" +-- "Zoom" -- both in and out +-- "Zoom In" +-- "Zoom Out" +-- "LocalRoll X" +-- "LocalRoll Y" +-- "GlobalRoll X" +-- "GlobalRoll Y" +-- "Pan X" +-- "Pan Y" +-- Third parameter determines whether the axis should be inverted +-- Fourth parameter determines whether the axis should be normalized from [-1,1] to [0,1] +-- Fifth parameters determins if the axis should be "Sticky" or not. +-- The axis values can either go back to 0 when the joystick is released or it can +-- stay at the value it was before the joystick was released. +-- The latter is called a sticky axis, when the values don't go back to 0. +-- Sixth parameter is the sensitivity for the axis + +local XBoxController = { + LeftThumbStick = { 0 , 1 }, + RightThumbStick = { 2, 3 }, + LeftTrigger = 4, + RightTrigger = 5, + A = 0, + B = 1, + X = 2, + Y = 3, + LB = 4, + RB = 5, + Select = 6, + Start = 7, + LeftStick = 8, + RightStick = 9, + DPad = { + Up = 10, + Right = 11, + Down = 12, + Left = 13 + } +} + +asset.onInitialize(function() + local controller = XBoxController; + + openspace.navigation.setAxisDeadZone(controller.LeftThumbStick[1], 0.15) + openspace.navigation.setAxisDeadZone(controller.LeftThumbStick[2], 0.15) + openspace.navigation.setAxisDeadZone(controller.RightThumbStick[1], 0.15) + openspace.navigation.setAxisDeadZone(controller.RightThumbStick[2], 0.15) + + openspace.navigation.bindJoystickAxis(controller.LeftThumbStick[1], "Orbit X"); + openspace.navigation.bindJoystickAxis(controller.LeftThumbStick[2], "Orbit Y", true); + openspace.navigation.bindJoystickAxis(controller.RightThumbStick[1], "Pan X", true); + openspace.navigation.bindJoystickAxis(controller.RightThumbStick[2], "Pan Y", true); + openspace.navigation.bindJoystickAxis(controller.LeftTrigger, "Zoom Out", false, true); + openspace.navigation.bindJoystickAxis(controller.RightTrigger, "Zoom In", false, true); + + openspace.navigation.bindJoystickButton( + controller.LB, + joystickHelper.bindLocalRoll(controller.RightThumbStick[1]), + "Switch to local roll mode" + ) + openspace.navigation.bindJoystickButton( + controller.LB, + joystickHelper.unbindRoll(controller.RightThumbStick[1]), + "Switch back to normal mode", + "Release" + ) + openspace.navigation.bindJoystickButton( + controller.RB, + joystickHelper.bindGlobalRoll(controller.RightThumbStick[1]), + "Switch to global roll mode" + ) + openspace.navigation.bindJoystickButton( + controller.RB, + joystickHelper.unbindRoll(controller.RightThumbStick[1]), + "Switch back to normal mode", + "Release" + ) + + openspace.navigation.bindJoystickButton( + controller.A, + propertyHelper.invert('NavigationHandler.OrbitalNavigator.Friction.ZoomFriction'), + "Toggle zoom friction" + ) + openspace.navigation.bindJoystickButton( + controller.B, + propertyHelper.invert('NavigationHandler.OrbitalNavigator.Friction.RotationalFriction'), + "Toggle rotational friction" + ) + openspace.navigation.bindJoystickButton( + controller.DPad.Left, + propertyHelper.invert('NavigationHandler.OrbitalNavigator.Friction.RollFriction'), + "Toggle roll friction" + ) + + openspace.navigation.bindJoystickButton( + controller.X, + "openspace.setPropertyValueSingle('NavigationHandler.OrbitalNavigator.Aim', '');" .. + "openspace.setPropertyValueSingle('NavigationHandler.OrbitalNavigator.Anchor', 'Earth');" .. + "openspace.setPropertyValueSingle('NavigationHandler.OrbitalNavigator.RetargetAnchor', nil);", + "Switch target to Earth" + ) + openspace.navigation.bindJoystickButton( + controller.Y, + "openspace.setPropertyValueSingle('NavigationHandler.OrbitalNavigator.Aim', '');" .. + "openspace.setPropertyValueSingle('NavigationHandler.OrbitalNavigator.Anchor', 'Mars');" .. + "openspace.setPropertyValueSingle('NavigationHandler.OrbitalNavigator.RetargetAnchor', nil);", + "Switch target to Mars" + ) +end) diff --git a/data/assets/util/joysticks/joystick_helper.asset b/data/assets/util/joysticks/joystick_helper.asset new file mode 100644 index 0000000000..169003d658 --- /dev/null +++ b/data/assets/util/joysticks/joystick_helper.asset @@ -0,0 +1,66 @@ +-- Variables to store the state of the joystick between frames +Joystick = {} +Joystick.State = {} +Joystick.State.IsInRollMode = false +Joystick.State.Axis = {} + +local bindLocalRoll = function(axis) + return [[ + -- We only want to store the current state in the first mode that is enabled, otherwise we will overwrite the backup + if not Joystick.State.IsInRollMode then + -- Save current axis state + Joystick.State.Axis.Type, Joystick.State.Axis.Inverted, Joystick.State.Axis.Normalized, Joystick.State.Axis.Sticky, Joystick.State.Axis.Sensitivity = openspace.navigation.joystickAxis(]] .. axis .. [[) + end + + -- Set new axis state + openspace.navigation.bindJoystickAxis(]] .. axis .. [[, "LocalRoll X", Joystick.State.Axis.Inverted, Joystick.State.Axis.Normalized, Joystick.State.Axis.Sticky, Joystick.State.Axis.Sensitivity); + Joystick.State.IsInRollMode = true + ]] +end + +local bindGlobalRoll = function(axis) + return [[ + -- We only want to store the current state in the first mode that is enabled, otherwise we will overwrite the backup + if not Joystick.State.IsInRollMode then + -- Save current axis state + Joystick.State.Axis.Type, Joystick.State.Axis.Inverted, Joystick.State.Axis.Normalized, Joystick.State.Axis.Sticky, Joystick.State.Axis.Sensitivity = openspace.navigation.joystickAxis(]] .. axis .. [[) + end + + -- Set new axis state + openspace.navigation.bindJoystickAxis(]] .. axis .. [[, "GlobalRoll X", Joystick.State.Axis.Inverted, Joystick.State.Axis.Normalized, Joystick.State.Axis.Sticky, Joystick.State.Axis.Sensitivity); + Joystick.State.IsInRollMode = true + ]] +end + +local permaBindLocalRoll = function(axis) + return [[ + -- Save current axis state + Joystick.State.Axis.Type, Joystick.State.Axis.Inverted, Joystick.State.Axis.Normalized, Joystick.State.Axis.Sticky, Joystick.State.Axis.Sensitivity = openspace.navigation.joystickAxis(]] .. axis .. [[) + + -- Set new axis state + openspace.navigation.bindJoystickAxis(]] .. axis .. [[, "LocalRoll X", Joystick.State.Axis.Inverted, Joystick.State.Axis.Normalized, Joystick.State.Axis.Sticky, Joystick.State.Axis.Sensitivity); + ]] +end + +local permaBindGlobalRoll = function(axis) + return [[ + -- Save current axis state + Joystick.State.Axis.Type, Joystick.State.Axis.Inverted, Joystick.State.Axis.Normalized, Joystick.State.Axis.Sticky, Joystick.State.Axis.Sensitivity = openspace.navigation.joystickAxis(]] .. axis .. [[) + + -- Set new axis state + openspace.navigation.bindJoystickAxis(]] .. axis .. [[, "GlobalRoll X", Joystick.State.Axis.Inverted, Joystick.State.Axis.Normalized, Joystick.State.Axis.Sticky, Joystick.State.Axis.Sensitivity); + ]] +end + +local unbindRoll = function(axis) + return [[ + -- Reset previous state + openspace.navigation.bindJoystickAxis(]] .. axis .. [[, Joystick.State.Axis.Type, Joystick.State.Axis.Inverted, Joystick.State.Axis.Normalized, Joystick.State.Axis.Sticky, Joystick.State.Axis.Sensitivity); + ]] +end + +asset.export("bindLocalRoll", bindLocalRoll) +asset.export("bindGlobalRoll", bindGlobalRoll) +asset.export("permaBindLocalRoll", permaBindLocalRoll) +asset.export("permaBindGlobalRoll", permaBindGlobalRoll) +asset.export("unbindRoll", unbindRoll) \ No newline at end of file diff --git a/include/openspace/interaction/joystickcamerastates.h b/include/openspace/interaction/joystickcamerastates.h index 15188faf26..f8f86f239b 100644 --- a/include/openspace/interaction/joystickcamerastates.h +++ b/include/openspace/interaction/joystickcamerastates.h @@ -130,6 +130,7 @@ inline std::string to_string( case T::OrbitY: return "Orbit Y"; case T::ZoomIn: return "Zoom In"; case T::ZoomOut: return "Zoom Out"; + case T::Zoom: return "Zoom In and Out"; case T::LocalRollX: return "LocalRoll X"; case T::LocalRollY: return "LocalRoll Y"; case T::GlobalRollX: return "GlobalRoll X"; @@ -149,9 +150,9 @@ from_string(std::string_view string) if (string == "None") { return T::None; } if (string == "Orbit X") { return T::OrbitX; } if (string == "Orbit Y") { return T::OrbitY; } - if (string == "Zoom") { return T::Zoom; } if (string == "Zoom In") { return T::ZoomIn; } if (string == "Zoom Out") { return T::ZoomOut; } + if (string == "Zoom") { return T::Zoom; } if (string == "LocalRoll X") { return T::LocalRollX; } if (string == "LocalRoll Y") { return T::LocalRollY; } if (string == "GlobalRoll X") { return T::GlobalRollX; }