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

93 lines
No EOL
3.3 KiB
JavaScript

// building block.js
//
// Script Type: Entity Script
// Copyright 2016 High Fidelity, Inc.
//
// Snapping building blocks
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
/*global MyAvatar, Entities, AnimationCache, SoundCache, Scene, Camera, Overlays, HMD, AvatarList, AvatarManager, Controller, UndoStack, Window, Account, GlobalServices, Script, ScriptDiscoveryService, LODManager, Menu, Vec3, Quat, AudioDevice, Paths, Clipboard, Settings, XMLHttpRequest, randFloat, randInt */
(function() {
Script.include("../libraries/utils.js");
var SNAP_RELATIVE = true; // snap relative to the position of a parent/root so rotated children snap according to parent position. Else global or absolute space.
var SNAP_ANGLE = 45; // Snapping for rotation allowed at increments of 45 degrees
var SNAP_UNIT = {x:0.2, y:0.2, z:0.2};//2.0, 1.5, 1.5; // xyz parameters for snap increment
var baserot;
var basepos;
function rotToDeg(rotation) {
var angle = Math.round((Quat.safeEulerAngles(rotation).y + 360.0) % 360.0);
return angle;
}
function BlockSnap() {
}
BlockSnap.prototype = {
preload: function(entityID) {
this.entityID = entityID;
},
clickReleaseOnEntity: function(entityID, mouseEvent) {
if (!mouseEvent.isLeftButton) {
return;
}
print("Click up");
var userData = Entities.getEntityProperties(this.entityID, ["userData"]).userData;
//var configDataOject = JSON.parse(userData);
var copyEnt = Entities.getEntityProperties(this.entityID, ["rotation","position"]);
var jsonEnt = JSON.stringify(copyEnt);
var data = JSON.parse(jsonEnt);
baserot = rotToDeg(data.rotation);
basepos = data.position;
basepos.x = SNAP_UNIT.x * Math.round (data.position.x/SNAP_UNIT.x);
basepos.y = SNAP_UNIT.y * Math.round (data.position.x/SNAP_UNIT.y);
basepos.z = SNAP_UNIT.z * Math.round (data.position.x/SNAP_UNIT.z);
// Get my current rotation
baserot.y = SNAP_ANGLE * Math.round(data.rotation.y/SNAP_ANGLE);
// Edit & write new properties to entityIDvar
newProperties = {position:basepos, rotation:baserot};
Entities.editEntity(this.entityID, newProperties);
},
releaseGrab: function() {
var userData = Entities.getEntityProperties(this.entityID, ["userData"]).userData;
//var configDataOject = JSON.parse(userData);
var copyEnt = Entities.getEntityProperties(this.entityID, ["rotation","position"]);
var jsonEnt = JSON.stringify(copyEnt);
var data = JSON.parse(jsonEnt);
baserot = rotToDeg(data.rotation);
basepos = data.position;
basepos.x = SNAP_UNIT.x * Math.round (basepos.x/SNAP_UNIT.x);
basepos.y = SNAP_UNIT.y * Math.round (basepos.x/SNAP_UNIT.y);
basepos.z = SNAP_UNIT.z * Math.round (basepos.x/SNAP_UNIT.z);
// Get my current rotation
baserot.y = SNAP_ANGLE * Math.round(baserot.y/SNAP_ANGLE);
// Edit & write new properties to entityIDvar
newProperties = {position:basepos, rotation:baserot};
Entities.editEntity(this.entityID, newProperties);
},
};
return new BlockSnap();
});