History

archive remaining JS scripts

Andrew Meadows committed 10 years ago

Commit c0b446c1 parent 9556995401 files · +0 −0Browse files

Showing the changes to script-archive/example/scripts/controllerScriptingExamples.js (1 of 401 files). Show all

Unified · Split

script-archive/example/scripts/controllerScriptingExamples.js+146−0
@@ -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
14function 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
26var hydra = Controller.Hardware.Hydra;
27if (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
46Object.keys(Controller.Standard).forEach(function (input) {
47 print("Controller.Standard." + input + ":" + Controller.Standard[input]);
48});
49
50Object.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
56Object.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:
61Controller.resetAllDeviceBindings();
62
63// Query all actions
64print("All Actions: \n" + Controller.getAllActions());
65
66var actionId = findAction("YAW_LEFT")
67
68print("Yaw Left action ID: " + actionId)
69
70// Each action stores:
71// action: int representation of enum
72print("Action int: \n" + Controller.getAllActions()[actionId].action);
73
74// actionName: string representation of enum
75print("Action name: \n" + Controller.getAllActions()[actionId].actionName);
76
77// inputChannels: list of all inputchannels that control that action
78print("Action input channels: \n" + Controller.getAllActions()[actionId].inputChannels + "\n");
79
80
81// Each input channel stores:
82// action: Action that this InputChannel maps to
83print("Input channel action: \n" + Controller.getAllActions()[actionId].inputChannels[0].action);
84
85// scale: sensitivity of input
86print("Input channel scale: \n" + Controller.getAllActions()[actionId].inputChannels[0].scale);
87
88// input and modifier: Inputs
89print("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
94print("Input device: \n" + Controller.getAllActions()[actionId].inputChannels[0].input.device);
95
96// channel: channel of input
97print("Input channel: \n" + Controller.getAllActions()[actionId].inputChannels[0].input.channel);
98
99// type: type of input (Unknown, Button, Axis, Joint)
100print("Input type: \n" + Controller.getAllActions()[actionId].inputChannels[0].input.type);
101
102// id: id of input
103print("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
107print("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
110print("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
115var s = Controller.getAllActions()[0].inputChannels[0];
116var 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
120Controller.removeInputChannel(s);
121Controller.removeInputChannel(w);
122print(s.scale);
123s.action = 1;
124s.scale = .01;
125
126w.action = 0;
127w.scale = 10000;
128Controller.addInputChannel(s);
129Controller.addInputChannel(w);
130print(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
136var availableInputs = Controller.getAvailableInputs(1);
137for (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
143var e = Controller.getAllActions()[actionId].inputChannels[0];
144Controller.removeInputChannel(e);
145e.input = availableInputs[6].input;
146Controller.addInputChannel(e);