diff --git a/scripts/system/controllers/mouseLook.js b/scripts/system/controllers/mouseLook.js index a8126a8e69..0e37144e12 100644 --- a/scripts/system/controllers/mouseLook.js +++ b/scripts/system/controllers/mouseLook.js @@ -6,13 +6,23 @@ by rampa3 (https://github.com/rampa3) and vegaslon (https://github.com/vegaslon) var oldMode; + var tabletUp; + + var away; + var mouseLookEnabled = false; + + var tablet = Tablet.getTablet("com.highfidelity.interface.tablet.system"); if (mouseLookEnabled) { - mouseLookOn(); + if (!tablet.tabletShown){ + mouseLookOn(); + } } - Controller.keyPressEvent.connect(function(event) { + Controller.keyPressEvent.connect(onKeyPressEvent); + + function onKeyPressEvent(event) { if (event.text === 'm') { if (mouseLookEnabled) { if (!Camera.getCaptureMouse()){ @@ -35,31 +45,53 @@ by rampa3 (https://github.com/rampa3) and vegaslon (https://github.com/vegaslon) mouseLookOff(); } } - }); + } - var tablet = Tablet.getTablet("com.highfidelity.interface.tablet.system"); - tablet.tabletShownChanged.connect(function () { - if (!tablet.toolbarMode) { - if (tablet.tabletShown) { - Window.displayAnnouncement("Tablet is up – mouse look temporarily off."); - mouseLookOff(); + tablet.tabletShownChanged.connect(onTabletShownChanged); - } else if (!tablet.tabletShown) { - Window.displayAnnouncement("Tablet hidden – mouse look on."); + function onTabletShownChanged() { + if (mouseLookEnabled) { + if (!tablet.toolbarMode) { + if (tablet.tabletShown) { + tabletUp = true; + if (!away) { + Window.displayAnnouncement("Tablet is up – mouse look temporarily off."); + mouseLookOff(); + } + } else if (!tablet.tabletShown) { + tabletUp = false; + if (!away) { + Window.displayAnnouncement("Tablet hidden – mouse look on."); + mouseLookOn(); + } + } + } + } + } + + MyAvatar.wentAway.connect(onWentAway); + + function onWentAway() { + if (mouseLookEnabled) { + away = true; + if (!tabletUp){ + Window.displayAnnouncement("Away state ON – mouse look temporarily off.") + mouseLookOff() + } + } + } + + MyAvatar.wentActive.connect(onWentActive); + + function onWentActive() { + if (mouseLookEnabled) { + away = false; + if (!tabletUp) { + Window.displayAnnouncement("Away state OFF – mouse look on."); mouseLookOn(); } } - }); - - MyAvatar.wentAway.connect(function () { - Window.displayAnnouncement("Away state ON – mouse look temporarily off.") - mouseLookOff() - }); - - MyAvatar.wentActive.connect(function () { - Window.displayAnnouncement("Away state OFF – mouse look on."); - mouseLookOn(); - }); + } function mouseLookOn() { oldMode = Camera.mode; @@ -80,8 +112,16 @@ by rampa3 (https://github.com/rampa3) and vegaslon (https://github.com/vegaslon) Camera.modeUpdated.connect(onCameraModeUpdated); - Script.scriptEnding.connect(function() { + Script.scriptEnding.connect(onScriptEnding); + + function onScriptEnding() { Camera.captureMouse = false; - }); + Controller.keyPressEvent.disconnect(onKeyPressEvent); + tablet.tabletShownChanged.disconnect(onTabletShownChanged); + MyAvatar.wentAway.disconnect(onWentAway); + MyAvatar.wentActive.disconnect(onWentActive); + Camera.modeUpdated.disconnect(onCameraModeUpdated); + Script.scriptEnding.disconnect(); + } }()); // END LOCAL_SCOPE \ No newline at end of file