updated pointer.js script to work with new line entity

This commit is contained in:
Eric Levin 2015-06-04 15:44:39 -07:00
parent a12fd5c3d0
commit a2ae51371b

View file

@ -1,30 +1,17 @@
// pointer.js // pointer.js
// examples // examples
// //
// Created by Eric Levin on May 26, 2015 // Created by Seth Alves on May 15th
// Modified by Eric Levin on June 4
// Copyright 2015 High Fidelity, Inc. // Copyright 2015 High Fidelity, Inc.
// //
// Provides a pointer with option to draw on surfaces // Provides a pointer with option to draw on surfaces
// //
// Distributed under the Apache License, Version 2.0. // Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
var lineEntityID = null; var lineEntityID = null;
var lineIsRezzed = false; var lineIsRezzed = false;
var altHeld = false;
var lineCreated = false;
var position, positionOffset, prevPosition;
var nearLinePosition;
var strokes = [];
var STROKE_ADJUST = 0.005;
var DISTANCE_DRAW_THRESHOLD = .02;
var drawDistance = 0;
var LINE_WIDTH = 20;
var userCanPoint = false;
var userCanDraw = false;
var BUTTON_SIZE = 32; var BUTTON_SIZE = 32;
var PADDING = 3; var PADDING = 3;
@ -43,16 +30,7 @@ var buttonOnColor = {
HIFI_PUBLIC_BUCKET = "http://s3.amazonaws.com/hifi-public/"; HIFI_PUBLIC_BUCKET = "http://s3.amazonaws.com/hifi-public/";
var screenSize = Controller.getViewportDimensions(); var screenSize = Controller.getViewportDimensions();
var drawButton = Overlays.addOverlay("image", { var userCanPoint = false;
x: screenSize.x / 2 - BUTTON_SIZE + PADDING * 2,
y: screenSize.y - (BUTTON_SIZE + PADDING),
width: BUTTON_SIZE,
height: BUTTON_SIZE,
imageURL: HIFI_PUBLIC_BUCKET + "images/pencil.png?v2",
color: buttonOffColor,
alpha: 1
});
var pointerButton = Overlays.addOverlay("image", { var pointerButton = Overlays.addOverlay("image", {
x: screenSize.x / 2 - BUTTON_SIZE * 2 + PADDING, x: screenSize.x / 2 - BUTTON_SIZE * 2 + PADDING,
y: screenSize.y - (BUTTON_SIZE + PADDING), y: screenSize.y - (BUTTON_SIZE + PADDING),
@ -61,14 +39,12 @@ var pointerButton = Overlays.addOverlay("image", {
imageURL: HIFI_PUBLIC_BUCKET + "images/laser.png", imageURL: HIFI_PUBLIC_BUCKET + "images/laser.png",
color: buttonOffColor, color: buttonOffColor,
alpha: 1 alpha: 1
}) });
var center = Vec3.sum(MyAvatar.position, Vec3.multiply(2.0, Quat.getFront(Camera.getOrientation())));
center.y += 0.5;
function calculateNearLinePosition(targetPosition) { function nearLinePoint(targetPosition) {
var handPosition = MyAvatar.getRightPalmPosition(); var handPosition = MyAvatar.getRightPalmPosition();
var along = Vec3.subtract(targetPosition, handPosition); var along = Vec3.subtract(targetPosition, handPosition);
along = Vec3.normalize(along); along = Vec3.normalize(along);
@ -87,39 +63,40 @@ function removeLine() {
function createOrUpdateLine(event) { function createOrUpdateLine(event) {
if (!userCanPoint) {
return;
}
var pickRay = Camera.computePickRay(event.x, event.y); var pickRay = Camera.computePickRay(event.x, event.y);
var intersection = Entities.findRayIntersection(pickRay, true); // accurate picking var intersection = Entities.findRayIntersection(pickRay, true); // accurate picking
var props = Entities.getEntityProperties(intersection.entityID); var props = Entities.getEntityProperties(intersection.entityID);
if (intersection.intersects) { if (intersection.intersects && userCanPoint) {
startPosition = intersection.intersection; var points = [nearLinePoint(intersection.intersection), intersection.intersection]
var subtractVec = Vec3.multiply(Vec3.normalize(pickRay.direction), STROKE_ADJUST);
startPosition = Vec3.subtract(startPosition, subtractVec);
nearLinePosition = calculateNearLinePosition(intersection.intersection);
positionOffset = Vec3.subtract(startPosition, nearLinePosition);
if (lineIsRezzed) { if (lineIsRezzed) {
Entities.editEntity(lineEntityID, { Entities.editEntity(lineEntityID, {
position: nearLinePosition, position: nearLinePoint(intersection.intersection),
dimensions: positionOffset, linePoints: points,
dimensions: {
x: 1,
y: 1,
z: 1
},
lifetime: 15 + props.lifespan // renew lifetime
}); });
if (userCanDraw) {
draw();
}
} else { } else {
lineIsRezzed = true; lineIsRezzed = true;
prevPosition = startPosition;
lineEntityID = Entities.addEntity({ lineEntityID = Entities.addEntity({
type: "Line", type: "Line",
position: nearLinePosition, position: nearLinePoint(intersection.intersection),
dimensions: positionOffset, linePoints: points,
dimensions: {
x: 1,
y: 1,
z: 1
},
color: { color: {
red: 255, red: 255,
green: 255, green: 255,
blue: 255 blue: 255
}, },
lifetime: 15 // if someone crashes while pointing, don't leave the line there forever.
}); });
} }
} else { } else {
@ -127,120 +104,44 @@ function createOrUpdateLine(event) {
} }
} }
function draw() {
//We only want to draw line if distance between starting and previous point is large enough function mousePressEvent(event) {
drawDistance = Vec3.distance(startPosition, prevPosition); if (!event.isLeftButton) {
if (drawDistance < DISTANCE_DRAW_THRESHOLD) {
return; return;
} }
var offset = Vec3.subtract(startPosition, prevPosition); createOrUpdateLine(event);
strokes.push(Entities.addEntity({
type: "Line",
position: prevPosition,
dimensions: offset,
color: {
red: 200,
green: 40,
blue: 200
},
lineWidth: LINE_WIDTH
}));
prevPosition = startPosition;
}
function mousePressEvent(event) {
var clickedOverlay = Overlays.getOverlayAtPoint({ var clickedOverlay = Overlays.getOverlayAtPoint({
x: event.x, x: event.x,
y: event.y y: event.y
}); });
if (clickedOverlay == drawButton) {
userCanDraw = !userCanDraw;
if (userCanDraw === true) {
Overlays.editOverlay(drawButton, {
color: buttonOnColor
});
} else {
Overlays.editOverlay(drawButton, {
color: buttonOffColor
});
}
}
if (clickedOverlay == pointerButton) { if (clickedOverlay == pointerButton) {
userCanPoint = !userCanPoint; userCanPoint = !userCanPoint;
if (userCanPoint === true) { if (userCanPoint === true) {
Overlays.editOverlay(pointerButton, { Overlays.editOverlay(pointerButton, {
color: buttonOnColor color: buttonOnColor
}); });
if (userCanDraw === true) {
Overlays.editOverlay(drawButton, {
color: buttonOnColor
});
}
} else { } else {
Overlays.editOverlay(pointerButton, { Overlays.editOverlay(pointerButton, {
color: buttonOffColor color: buttonOffColor
}); });
Overlays.editOverlay(drawButton, {
color: buttonOffColor
});
} }
} }
if (!event.isLeftButton || altHeld) {
return;
}
Controller.mouseMoveEvent.connect(mouseMoveEvent);
createOrUpdateLine(event);
lineCreated = true;
} }
function mouseMoveEvent(event) { function mouseMoveEvent(event) {
createOrUpdateLine(event); createOrUpdateLine(event);
} }
function mouseReleaseEvent(event) { function mouseReleaseEvent(event) {
if (!lineCreated) { if (!event.isLeftButton) {
return; return;
} }
Controller.mouseMoveEvent.disconnect(mouseMoveEvent);
removeLine(); removeLine();
lineCreated = false;
} }
function keyPressEvent(event) { Controller.mouseMoveEvent.connect(mouseMoveEvent);
if (event.text == "ALT") {
altHeld = true;
}
}
function keyReleaseEvent(event) {
if (event.text == "ALT") {
altHeld = false;
}
}
function cleanup() {
for (var i = 0; i < strokes.length; i++) {
Entities.deleteEntity(strokes[i]);
}
Overlays.deleteOverlay(drawButton);
Overlays.deleteOverlay(pointerButton);
}
Script.scriptEnding.connect(cleanup);
Controller.mousePressEvent.connect(mousePressEvent); Controller.mousePressEvent.connect(mousePressEvent);
Controller.mouseReleaseEvent.connect(mouseReleaseEvent); Controller.mouseReleaseEvent.connect(mouseReleaseEvent);
Controller.keyPressEvent.connect(keyPressEvent);
Controller.keyReleaseEvent.connect(keyReleaseEvent);