overte/scripts/system/controllers/toggleAdvancedMovementForHandControllers.js

167 lines
4.6 KiB
JavaScript
Raw Normal View History

2016-08-23 12:34:32 -07:00
"use strict";
2016-08-18 15:29:49 -07:00
// Created by james b. pollack @imgntn on 8/18/2016
// Copyright 2016 High Fidelity, Inc.
2016-08-18 15:29:49 -07:00
//
2016-08-23 12:34:32 -07:00
// advanced movements settings are in individual controller json files
// what we do is check the status of the 'advance movement' checkbox when you enter HMD mode
// if 'advanced movement' is checked...we give you the defaults that are in the json.
// if 'advanced movement' is not checked... we override the advanced controls with basic ones.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
2016-08-23 12:34:32 -07:00
(function() { // BEGIN LOCAL_SCOPE
var mappingName, basicMapping, isChecked;
2016-08-18 11:07:50 -07:00
2016-08-18 16:50:50 -07:00
var TURN_RATE = 1000;
2016-08-19 14:29:14 -07:00
var MENU_ITEM_NAME = "Advanced Movement For Hand Controllers";
var isDisabled = false;
var previousSetting = MyAvatar.useAdvancedMovementControls;
if (previousSetting === false) {
2016-08-18 11:07:50 -07:00
previousSetting = false;
isChecked = false;
2016-08-18 11:07:50 -07:00
}
if (previousSetting === true) {
2016-08-19 14:51:15 -07:00
previousSetting = true;
isChecked = true;
2016-08-18 11:07:50 -07:00
}
2016-08-17 15:08:43 -07:00
function addAdvancedMovementItemToSettingsMenu() {
Menu.addMenuItem({
menuName: "Settings",
2016-08-19 14:29:14 -07:00
menuItemName: MENU_ITEM_NAME,
2016-08-17 15:08:43 -07:00
isCheckable: true,
2016-08-18 11:07:50 -07:00
isChecked: previousSetting
2016-08-17 15:08:43 -07:00
});
}
function rotate180() {
2016-08-18 15:58:12 -07:00
var newOrientation = Quat.multiply(MyAvatar.orientation, Quat.angleAxis(180, {
x: 0,
y: 1,
z: 0
}))
MyAvatar.orientation = newOrientation
2016-08-17 15:08:43 -07:00
}
var inFlipTurn = false;
function registerBasicMapping() {
2016-08-17 15:08:43 -07:00
mappingName = 'Hifi-AdvancedMovement-Dev-' + Math.random();
basicMapping = Controller.newMapping(mappingName);
basicMapping.from(Controller.Standard.LY).to(function(value) {
2016-10-06 13:44:51 -07:00
if (isDisabled) {
return;
}
var stick = Controller.getValue(Controller.Standard.LS);
if (value === 1 && Controller.Hardware.OculusTouch !== undefined) {
rotate180();
2016-08-19 14:29:14 -07:00
} else if (Controller.Hardware.Vive !== undefined) {
if (value > 0.75 && inFlipTurn === false) {
inFlipTurn = true;
rotate180();
Script.setTimeout(function() {
inFlipTurn = false;
2016-08-18 16:50:50 -07:00
}, TURN_RATE)
}
}
return;
2016-08-18 09:24:01 -07:00
});
basicMapping.from(Controller.Standard.RY).to(function(value) {
2016-10-06 13:44:51 -07:00
if (isDisabled) {
return;
}
var stick = Controller.getValue(Controller.Standard.RS);
if (value === 1 && Controller.Hardware.OculusTouch !== undefined) {
rotate180();
2016-08-19 14:29:14 -07:00
} else if (Controller.Hardware.Vive !== undefined) {
if (value > 0.75 && inFlipTurn === false) {
inFlipTurn = true;
rotate180();
Script.setTimeout(function() {
inFlipTurn = false;
2016-08-18 17:24:18 -07:00
}, TURN_RATE)
}
}
return;
})
2016-08-17 17:33:24 -07:00
}
2016-08-17 15:08:43 -07:00
function enableMappings() {
Controller.enableMapping(mappingName);
}
function disableMappings() {
2016-08-17 15:08:43 -07:00
Controller.disableMapping(mappingName);
}
function scriptEnding() {
2016-08-19 14:29:14 -07:00
Menu.removeMenuItem("Settings", MENU_ITEM_NAME);
disableMappings();
2016-08-17 15:08:43 -07:00
}
2016-08-17 15:08:43 -07:00
function menuItemEvent(menuItem) {
2016-08-19 14:29:14 -07:00
if (menuItem == MENU_ITEM_NAME) {
isChecked = Menu.isOptionChecked(MENU_ITEM_NAME);
2016-08-17 15:08:43 -07:00
if (isChecked === true) {
MyAvatar.useAdvancedMovementControls = true;
disableMappings();
2017-03-14 17:16:49 -07:00
} else if (isChecked === false) {
MyAvatar.useAdvancedMovementControls = false;
enableMappings();
2016-08-17 15:08:43 -07:00
}
}
}
addAdvancedMovementItemToSettingsMenu();
Script.scriptEnding.connect(scriptEnding);
Menu.menuItemEvent.connect(menuItemEvent);
registerBasicMapping();
2016-08-18 16:50:50 -07:00
Script.setTimeout(function() {
if (previousSetting === true) {
disableMappings();
2016-08-18 16:51:04 -07:00
} else {
2016-08-18 16:50:50 -07:00
enableMappings();
}
2016-08-18 16:52:32 -07:00
}, 100)
2016-08-18 11:07:50 -07:00
HMD.displayModeChanged.connect(function(isHMDMode) {
if (isHMDMode) {
if (Controller.Hardware.Vive !== undefined || Controller.Hardware.OculusTouch !== undefined) {
if (isChecked === true) {
disableMappings();
} else if (isChecked === false) {
enableMappings();
}
}
}
2016-08-23 12:34:32 -07:00
});
var HIFI_ADVANCED_MOVEMENT_DISABLER_CHANNEL = 'Hifi-Advanced-Movement-Disabler';
function handleMessage(channel, message, sender) {
if (channel == HIFI_ADVANCED_MOVEMENT_DISABLER_CHANNEL) {
if (message == 'disable') {
isDisabled = true;
} else if (message == 'enable') {
isDisabled = false;
}
}
}
Messages.subscribe(HIFI_ADVANCED_MOVEMENT_DISABLER_CHANNEL);
Messages.messageReceived.connect(handleMessage);
2016-08-23 12:34:32 -07:00
}()); // END LOCAL_SCOPE