content/hifi-content/caitlyn/dev/spacenavigator/SpacemouseInput.js
2022-02-13 22:19:19 +01:00

138 lines
No EOL
6.1 KiB
JavaScript

"use strict";
/// <reference path="hifi.d.ts" />
/// <reference path="Utility.ts" />
/* globals Utility */
// import {Utility} from "./Utility.js";
var Utility = Script.require("./Utility.js?" + Date.now());
module.exports = /** @class */ (function () {
function SpacemouseInput() {
// constants
this.SPEED = 100; // speed multiplier
this.YAW_SPEED = 200; // yaw speed is slower
this.DELTA_TIME = 1 / 60; // a pretend deltatime
this.PITCH_MINIMUM = -80; // clamp pitch up
this.PITCH_MAXIMUM = 80; // clamp pitch down
this.MAPPING_NAME = "org.mikeybailey.spacemouse.flycam"; // controller mapping
// private class properties
this.isFirstMove = true; // this will probably go away
this.firstCameraMode = ""; // flycam off camera mode
this.isFlyCameraActive = false; // fly camera is active
this.position = { x: 0, y: 0, z: 0 }; // values from controller inpout
this.rotation = { x: 0, y: 0, z: 0 }; // euler values from controller input
this.utility = new Utility();
this.utility.isDebug = true;
this.utility.debugLog("SMC!");
this.setupMapping();
this.mapping.enable(true);
this.firstCameraMode = Camera.getModeString();
if (this.firstCameraMode === "independent") {
this.firstCameraMode = "third person";
}
// this.inputTimer = Script.setInterval(function() {}, 1000);
Script.scriptEnding.connect(this, "destroy");
}
SpacemouseInput.prototype.doCube = function (doit) {
if (doit) {
this.testCubeID = Entities.addEntity({
type: "Model",
position: Vec3.sum(Camera.position, Vec3.multiplyQbyV(Camera.orientation, { x: 0, y: 0, z: 1 })),
rotation: Camera.orientation,
dimensions: { x: 0.25, y: 0.25, z: 0.25 },
registrationPoint: { x: 0, y: 0, z: 0 },
modelURL: "http://hifi-content.s3.amazonaws.com/caitlyn/dev/spacenavigator/droneBuggo.fbx?2"
}, true);
}
else {
Entities.deleteEntity(this.testCubeID);
}
};
SpacemouseInput.prototype.setupMapping = function () {
var _this = this;
this.mapping = Controller.newMapping(this.MAPPING_NAME);
this.mapping.from(Controller.Hardware.Spacemouse.TranslateX).to(function (value) {
_this.position.x = value * _this.SPEED * _this.DELTA_TIME;
_this.move();
});
this.mapping.from(Controller.Hardware.Spacemouse.TranslateY).to(function (value) {
_this.position.z = value * _this.SPEED * _this.DELTA_TIME;
_this.move();
});
this.mapping.from(Controller.Hardware.Spacemouse.TranslateZ).to(function (value) {
_this.position.y = -value * _this.SPEED * _this.DELTA_TIME;
_this.move();
});
this.mapping.from(Controller.Hardware.Spacemouse.RotateX).to(function (value) {
_this.rotation.x = value * _this.SPEED * _this.DELTA_TIME;
_this.rotate();
});
this.mapping.from(Controller.Hardware.Spacemouse.RotateY).to(function (value) {
_this.rotation.z = value * _this.SPEED * _this.DELTA_TIME;
});
this.mapping.from(Controller.Hardware.Spacemouse.RotateZ).to(function (value) {
_this.rotation.y = -value * _this.YAW_SPEED * _this.DELTA_TIME;
_this.rotate();
});
this.mapping.from(Controller.Hardware.Spacemouse.LeftButton).to(function (value) {
if (value) {
_this.onLeftButtonPress();
}
});
this.mapping.from(Controller.Hardware.Spacemouse.RightButton).to(function (value) {
if (value) {
_this.onRightButtonPress();
}
});
this.mapping.enable(true);
};
SpacemouseInput.prototype.onLeftButtonPress = function () {
return;
};
SpacemouseInput.prototype.onRightButtonPress = function () {
if (this.isFlyCameraActive) {
Camera.setModeString(this.firstCameraMode);
this.utility.debugLog(this.firstCameraMode);
this.isFlyCameraActive = false;
this.doCube(false);
this.position = Vec3.ZERO;
this.rotation = Vec3.ZERO;
this.utility.sleep(1000);
}
else {
Camera.setModeString("independent");
this.utility.debugLog("independent");
this.isFlyCameraActive = true;
this.doCube(true);
this.utility.sleep(1000);
}
};
SpacemouseInput.prototype.move = function () {
this.position = Vec3.multiplyQbyV(Camera.orientation, this.position);
this.position = Vec3.sum(Camera.position, this.position);
Camera.setPosition(this.position);
this.position = Vec3.ZERO;
};
SpacemouseInput.prototype.rotate = function () {
if (!this.isFlyCameraActive) {
return;
}
this.rotation = Vec3.multiplyQbyV(Camera.orientation, this.rotation);
var orientation = Quat.multiply(Quat.fromVec3Degrees(this.rotation), Camera.orientation);
var cameraAngles = Quat.safeEulerAngles(Quat.cancelOutRoll(orientation));
cameraAngles.x = this.utility.clamp(cameraAngles.x, this.PITCH_MINIMUM, this.PITCH_MAXIMUM);
Camera.orientation = Quat.fromPitchYawRollDegrees(cameraAngles.x, cameraAngles.y, cameraAngles.z);
var cubeY = Quat.safeEulerAngles(Camera.orientation);
cubeY = cubeY.y + 180;
Entities.editEntity(this.testCubeID, {
position: { x: Camera.position.x, y: Camera.position.y, z: Camera.position.z },
rotation: Camera.orientation
});
this.rotation = Vec3.ZERO;
};
SpacemouseInput.prototype.destroy = function () {
this.mapping.enable(false);
// this.mapping.loadMapping("./spacemouse.json");
// this.mapping.enable(true);
};
return SpacemouseInput;
}());
//# sourceMappingURL=SpacemouseInput.js.map