hubs/scripts/print-bindings.mjs

168 строки
5.2 KiB
JavaScript
Исходник Обычный вид История

2018-12-14 09:23:14 +03:00
import { paths } from "../msrc/src/systems/userinput/paths";
2018-12-13 20:35:54 +03:00
function capitalize(str) {
return str && str[0].toUpperCase() + str.substring(1);
}
function fromCamelCase(str) {
return capitalize(str.replace(/([^A-Z])([A-Z])/g, "$1 $2").toLowerCase());
}
function pretty(str) {
const words = fromCamelCase(str)
.replace(/keyboard-var/, "keyboard")
2018-12-14 21:40:02 +03:00
.replace(/vive-var/, "keyboard")
.replace(/pressed2/, "pressed")
.replace(/joy y2/, "joy y")
.replace(/_vec2/, "")
2018-12-13 20:35:54 +03:00
.replace(/rising/, "pressed")
.replace(/falling/, "released")
.split("/")
.slice(2);
words[0] = capitalize(words[0]);
return words.join(" ");
}
2018-12-14 21:40:02 +03:00
function prettyXform(xform) {
return capitalize(fromCamelCase(xform.replace("falling", "released")));
2018-12-13 20:35:54 +03:00
}
2018-12-14 09:23:14 +03:00
function getSources(src) {
if (src instanceof Array) {
return src;
} else {
return Object.values(src);
2018-12-13 20:35:54 +03:00
}
2018-12-14 09:23:14 +03:00
}
2018-12-14 21:40:02 +03:00
const excludeXforms = [
"always",
"any",
"compose_vec2",
"copy",
"copyIfTrue",
"max_vec2",
"normalize_vec2",
"rising",
"risingWithFrameDelay",
"scale",
"touch_axis_scroll"
];
2018-12-13 20:35:54 +03:00
2018-12-14 09:23:14 +03:00
function getPaths(set, dest) {
const paths = [];
for (const binding of set) {
if (!(binding.dest && binding.dest.value)) continue;
if (!binding.src) continue;
2018-12-14 21:40:02 +03:00
2018-12-14 09:23:14 +03:00
if (binding.dest.value === dest) {
2018-12-14 21:40:02 +03:00
if (!excludeXforms.includes(binding.xform.name)) paths.push(prettyXform(binding.xform.name));
2018-12-14 09:23:14 +03:00
for (const src of getSources(binding.src)) {
const subPaths = getPaths(set, src);
if (subPaths) paths.push(subPaths);
}
2018-12-13 20:35:54 +03:00
}
}
2018-12-14 21:40:02 +03:00
2018-12-14 09:23:14 +03:00
if (paths.length === 0) {
2018-12-14 21:40:02 +03:00
return pretty(dest);
2018-12-14 09:23:14 +03:00
}
if (paths.length === 1 && typeof paths[0] === "string") {
return paths[0];
}
return paths;
}
const exclude = [
paths.actions.cameraDelta,
2019-08-08 03:17:31 +03:00
paths.actions.cursor.right.stopDrawing,
2018-12-14 09:23:14 +03:00
paths.actions.leftHand.index,
paths.actions.leftHand.middleRingPinky,
paths.actions.leftHand.pose,
paths.actions.leftHand.stopDrawing,
paths.actions.leftHand.stopTeleport,
paths.actions.leftHand.thumb,
paths.actions.rightHand.index,
paths.actions.rightHand.middleRingPinky,
paths.actions.rightHand.pose,
paths.actions.rightHand.stopDrawing,
paths.actions.rightHand.stopTeleport,
paths.actions.rightHand.thumb,
paths.actions.stopGazeTeleport,
paths.actions.thaw
];
2018-12-14 21:40:02 +03:00
function quoteLast(sentence) {
const words = sentence.split(" ");
if (words.length < 2) return words[0];
return `${words[0]} "${words.slice(1).join(" ")}"`;
}
function formatSource(source, indent) {
if (typeof source === "string") return indent + quoteLast(source);
if (source.every(x => typeof x === "string")) return source.map(x => indent + " " + quoteLast(x)).join("\n");
return source.map(x => formatSource(x, indent + " ")).join("\n") + "\n";
}
2018-12-14 09:23:14 +03:00
function formatSources(sources) {
2018-12-14 21:40:02 +03:00
const indent = " ";
2018-12-14 09:23:14 +03:00
if (sources.length === 1 && typeof sources[0] === "string") {
2018-12-14 21:40:02 +03:00
return indent + quoteLast(sources[0]);
} else if (typeof sources === "string") {
return indent + quoteLast(sources);
2018-12-14 09:23:14 +03:00
} else {
2018-12-14 21:40:02 +03:00
return sources.map(x => formatSource(x, indent)).join("\n");
}
}
function flatten(arr) {
if (arr.length === 1) {
return flatten(arr[0]);
2018-12-14 09:23:14 +03:00
}
2018-12-14 21:40:02 +03:00
return arr;
2018-12-14 09:23:14 +03:00
}
function printSet(set) {
for (const binding of set) {
if (!(binding.dest && binding.dest.value)) continue;
const dest = binding.dest.value;
if (!dest.includes("/actions/") || exclude.includes(dest)) continue;
if (!binding.src) continue;
console.log(
2018-12-14 21:40:02 +03:00
"\n [" + pretty(binding.dest.value) + "]",
2018-12-14 09:23:14 +03:00
"\n" +
2018-12-14 21:40:02 +03:00
(excludeXforms.includes(binding.xform.name) ? "" : " " + prettyXform(binding.xform.name) + "\n") +
formatSources(flatten(getSources(binding.src).map(src => flatten(getPaths(set, src)))))
2018-12-14 09:23:14 +03:00
);
}
2018-12-13 20:35:54 +03:00
}
function printBindings(bindingsName, bindings) {
console.log("\n" + fromCamelCase(bindingsName));
for (const setName in bindings) {
2018-12-14 09:23:14 +03:00
console.log("\n <" + fromCamelCase(setName) + ">");
2018-12-13 20:35:54 +03:00
printSet(bindings[setName]);
}
}
import { touchscreenUserBindings } from "../msrc/src/systems/userinput/bindings/touchscreen-user";
2018-12-14 09:23:14 +03:00
import { keyboardMouseUserBindings } from "../msrc/src/systems/userinput/bindings/keyboard-mouse-user";
2018-12-13 20:35:54 +03:00
import { oculusTouchUserBindings } from "../msrc/src/systems/userinput/bindings/oculus-touch-user";
2018-12-14 09:23:14 +03:00
import { viveUserBindings } from "../msrc/src/systems/userinput/bindings/vive-user";
2018-12-13 20:35:54 +03:00
import generate3DOFTriggerBindings from "../msrc/src/systems/userinput/bindings/oculus-go-user";
import { daydreamUserBindings } from "../msrc/src/systems/userinput/bindings/daydream-user";
2018-12-14 09:23:14 +03:00
import { xboxControllerUserBindings } from "../msrc/src/systems/userinput/bindings/xbox-controller-user";
2018-12-14 21:40:02 +03:00
const oculusGoUserBindings = generate3DOFTriggerBindings(paths.device.oculusgo);
2018-12-14 09:23:14 +03:00
const gearVRControllerUserBindings = generate3DOFTriggerBindings(paths.device.gearVRController);
2018-12-14 21:40:02 +03:00
[
["touchscreenUserBindings", touchscreenUserBindings],
["keyboardMouseUserBindings", keyboardMouseUserBindings],
["oculusTouchUserBindings", oculusTouchUserBindings],
["viveUserBindings", viveUserBindings],
["oculusGoUserBindings", oculusGoUserBindings],
["daydreamUserBindings", daydreamUserBindings],
["gearVRControllerUserBindings", gearVRControllerUserBindings],
["xboxControllerUserBindings", xboxControllerUserBindings]
].forEach(([name, bindings]) => printBindings(name, bindings));