Showing the changes to script-archive/example/scripts/controllerScriptingExamples.js (1 of 401 files). Show all
Unified · Split
| @@ -0,0 +1,146 @@ | |||
| 1 | // | ||
| 2 | // controllerScriptingExamples.js | ||
| 3 | // examples | ||
| 4 | // | ||
| 5 | // Created by Sam Gondelman on 6/2/15 | ||
| 6 | // Copyright 2015 High Fidelity, Inc. | ||
| 7 | // | ||
| 8 | // Distributed under the Apache License, Version 2.0. | ||
| 9 | // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html | ||
| 10 | // | ||
| 11 | |||
| 12 | // Assumes you only have the default keyboard connected | ||
| 13 | |||
| 14 | function findAction(name) { | ||
| 15 | var actions = Controller.getAllActions(); | ||
| 16 | for (var i = 0; i < actions.length; i++) { | ||
| 17 | if (actions[i].actionName == name) { | ||
| 18 | return i; | ||
| 19 | } | ||
| 20 | } | ||
| 21 | // If the action isn't found, it will default to the first available action | ||
| 22 | return 0; | ||
| 23 | } | ||
| 24 | |||
| 25 | |||
| 26 | var hydra = Controller.Hardware.Hydra; | ||
| 27 | if (hydra !== undefined) { | ||
| 28 | print("-----------------------------------"); | ||
| 29 | var mapping = Controller.newMapping("Test"); | ||
| 30 | var standard = Controller.Standard; | ||
| 31 | print("standard:" + standard); | ||
| 32 | mapping.from(function () { return Math.sin(Date.now() / 250); }).to(function (newValue, oldValue, source) { | ||
| 33 | print("function source newValue:" + newValue + ", oldValue:" + oldValue + ", source:" + source); | ||
| 34 | }); | ||
| 35 | mapping.from(hydra.L1).to(standard.A); | ||
| 36 | mapping.from(hydra.L2).to(standard.B); | ||
| 37 | mapping.from(hydra.L3).to(function (newValue, oldValue, source) { | ||
| 38 | print("hydra.L3 newValue:" + newValue + ", oldValue:" + oldValue + ", source:" + source); | ||
| 39 | }); | ||
| 40 | Controller.enableMapping("Test"); | ||
| 41 | print("-----------------------------------"); | ||
| 42 | } else { | ||
| 43 | print("couldn't find hydra"); | ||
| 44 | } | ||
| 45 | |||
| 46 | Object.keys(Controller.Standard).forEach(function (input) { | ||
| 47 | print("Controller.Standard." + input + ":" + Controller.Standard[input]); | ||
| 48 | }); | ||
| 49 | |||
| 50 | Object.keys(Controller.Hardware).forEach(function (deviceName) { | ||
| 51 | Object.keys(Controller.Hardware[deviceName]).forEach(function (input) { | ||
| 52 | print("Controller.Hardware." + deviceName + "." + input + ":" + Controller.Hardware[deviceName][input]); | ||
| 53 | }); | ||
| 54 | }); | ||
| 55 | |||
| 56 | Object.keys(Controller.Actions).forEach(function (actionName) { | ||
| 57 | print("Controller.Actions." + actionName + ":" + Controller.Actions[actionName]); | ||
| 58 | }); | ||
| 59 | |||
| 60 | // Resets every device to its default key bindings: | ||
| 61 | Controller.resetAllDeviceBindings(); | ||
| 62 | |||
| 63 | // Query all actions | ||
| 64 | print("All Actions: \n" + Controller.getAllActions()); | ||
| 65 | |||
| 66 | var actionId = findAction("YAW_LEFT") | ||
| 67 | |||
| 68 | print("Yaw Left action ID: " + actionId) | ||
| 69 | |||
| 70 | // Each action stores: | ||
| 71 | // action: int representation of enum | ||
| 72 | print("Action int: \n" + Controller.getAllActions()[actionId].action); | ||
| 73 | |||
| 74 | // actionName: string representation of enum | ||
| 75 | print("Action name: \n" + Controller.getAllActions()[actionId].actionName); | ||
| 76 | |||
| 77 | // inputChannels: list of all inputchannels that control that action | ||
| 78 | print("Action input channels: \n" + Controller.getAllActions()[actionId].inputChannels + "\n"); | ||
| 79 | |||
| 80 | |||
| 81 | // Each input channel stores: | ||
| 82 | // action: Action that this InputChannel maps to | ||
| 83 | print("Input channel action: \n" + Controller.getAllActions()[actionId].inputChannels[0].action); | ||
| 84 | |||
| 85 | // scale: sensitivity of input | ||
| 86 | print("Input channel scale: \n" + Controller.getAllActions()[actionId].inputChannels[0].scale); | ||
| 87 | |||
| 88 | // input and modifier: Inputs | ||
| 89 | print("Input channel input and modifier: \n" + Controller.getAllActions()[actionId].inputChannels[0].input + "\n" + Controller.getAllActions()[actionId].inputChannels[0].modifier + "\n"); | ||
| 90 | |||
| 91 | |||
| 92 | // Each Input stores: | ||
| 93 | // device: device of input | ||
| 94 | print("Input device: \n" + Controller.getAllActions()[actionId].inputChannels[0].input.device); | ||
| 95 | |||
| 96 | // channel: channel of input | ||
| 97 | print("Input channel: \n" + Controller.getAllActions()[actionId].inputChannels[0].input.channel); | ||
| 98 | |||
| 99 | // type: type of input (Unknown, Button, Axis, Joint) | ||
| 100 | print("Input type: \n" + Controller.getAllActions()[actionId].inputChannels[0].input.type); | ||
| 101 | |||
| 102 | // id: id of input | ||
| 103 | print("Input id: \n" + Controller.getAllActions()[actionId].inputChannels[0].input.id + "\n"); | ||
| 104 | |||
| 105 | |||
| 106 | // You can get the name of a device from its id | ||
| 107 | print("Device 1 name: \n" + Controller.getDeviceName(Controller.getAllActions()[actionId].inputChannels[0].input.id)); | ||
| 108 | |||
| 109 | // You can also get all of a devices input channels | ||
| 110 | print("Device 1's input channels: \n" + Controller.getAllInputsForDevice(1) + "\n"); | ||
| 111 | |||
| 112 | |||
| 113 | // Modifying properties: | ||
| 114 | // The following code will switch the "w" and "s" key functionality and adjust their scales | ||
| 115 | var s = Controller.getAllActions()[0].inputChannels[0]; | ||
| 116 | var w = Controller.getAllActions()[1].inputChannels[0]; | ||
| 117 | |||
| 118 | // You must remove an input controller before modifying it so the old input controller isn't registered anymore | ||
| 119 | // removeInputChannel and addInputChannel return true if successful, false otherwise | ||
| 120 | Controller.removeInputChannel(s); | ||
| 121 | Controller.removeInputChannel(w); | ||
| 122 | print(s.scale); | ||
| 123 | s.action = 1; | ||
| 124 | s.scale = .01; | ||
| 125 | |||
| 126 | w.action = 0; | ||
| 127 | w.scale = 10000; | ||
| 128 | Controller.addInputChannel(s); | ||
| 129 | Controller.addInputChannel(w); | ||
| 130 | print(s.scale); | ||
| 131 | |||
| 132 | // You can get all the available inputs for any device | ||
| 133 | // Each AvailableInput has: | ||
| 134 | // input: the Input itself | ||
| 135 | // inputName: string representing the input | ||
| 136 | var availableInputs = Controller.getAvailableInputs(1); | ||
| 137 | for (i = 0; i < availableInputs.length; i++) { | ||
| 138 | print(availableInputs[i].inputName); | ||
| 139 | } | ||
| 140 | |||
| 141 | // You can modify key bindings by using these avaiable inputs | ||
| 142 | // This will replace e (up) with 6 | ||
| 143 | var e = Controller.getAllActions()[actionId].inputChannels[0]; | ||
| 144 | Controller.removeInputChannel(e); | ||
| 145 | e.input = availableInputs[6].input; | ||
| 146 | Controller.addInputChannel(e); | ||