History

finished webEntityLaserInput, now working on cloning

Dante Ruiz committed 9 years ago

Commit 4b592681 parent 63387026 files · +667 −16Browse files

Showing the changes to scripts/system/controllers/controllerModules/webEntityLaserInput.js (1 of 6 files). Show all

Unified · Split

scripts/system/controllers/controllerModules/webEntityLaserInput.js+478−0
@@ -0,0 +1,478 @@
1+"use strict";
2+
3+// webEntityLaserInput.js
4+//
5+// Distributed under the Apache License, Version 2.0.
6+// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
7+
8+/*jslint bitwise: true */
9+
10+/* global Script, Controller, LaserPointers, RayPick, RIGHT_HAND, LEFT_HAND, Mat4, MyAvatar, Vec3, Camera, Quat,
11+ getGrabPointSphereOffset, getEnabledModuleByName, makeRunningValues, Entities, NULL_UUID,
12+ enableDispatcherModule, disableDispatcherModule, entityIsDistanceGrabbable,
13+ makeDispatcherModuleParameters, MSECS_PER_SEC, HAPTIC_PULSE_STRENGTH, HAPTIC_PULSE_DURATION,
14+ PICK_MAX_DISTANCE, COLORS_GRAB_SEARCHING_HALF_SQUEEZE, COLORS_GRAB_SEARCHING_FULL_SQUEEZE, COLORS_GRAB_DISTANCE_HOLD,
15+ AVATAR_SELF_ID, DEFAULT_SEARCH_SPHERE_DISTANCE, TRIGGER_OFF_VALUE, TRIGGER_ON_VALUE,
16+
17+*/
18+
19+Script.include("/~/system/controllers/controllerDispatcherUtils.js");
20+Script.include("/~/system/libraries/controllers.js");
21+
22+(function() {
23+ var halfPath = {
24+ type: "line3d",
25+ color: COLORS_GRAB_SEARCHING_HALF_SQUEEZE,
26+ visible: true,
27+ alpha: 1,
28+ solid: true,
29+ glow: 1.0,
30+ lineWidth: 5,
31+ ignoreRayIntersection: true, // always ignore this
32+ drawInFront: true, // Even when burried inside of something, show it.
33+ parentID: AVATAR_SELF_ID
34+ };
35+ var halfEnd = {
36+ type: "sphere",
37+ solid: true,
38+ color: COLORS_GRAB_SEARCHING_HALF_SQUEEZE,
39+ alpha: 0.9,
40+ ignoreRayIntersection: true,
41+ drawInFront: true, // Even when burried inside of something, show it.
42+ visible: true
43+ };
44+ var fullPath = {
45+ type: "line3d",
46+ color: COLORS_GRAB_SEARCHING_FULL_SQUEEZE,
47+ visible: true,
48+ alpha: 1,
49+ solid: true,
50+ glow: 1.0,
51+ lineWidth: 5,
52+ ignoreRayIntersection: true, // always ignore this
53+ drawInFront: true, // Even when burried inside of something, show it.
54+ parentID: AVATAR_SELF_ID
55+ };
56+ var fullEnd = {
57+ type: "sphere",
58+ solid: true,
59+ color: COLORS_GRAB_SEARCHING_FULL_SQUEEZE,
60+ alpha: 0.9,
61+ ignoreRayIntersection: true,
62+ drawInFront: true, // Even when burried inside of something, show it.
63+ visible: true
64+ };
65+ var holdPath = {
66+ type: "line3d",
67+ color: COLORS_GRAB_DISTANCE_HOLD,
68+ visible: true,
69+ alpha: 1,
70+ solid: true,
71+ glow: 1.0,
72+ lineWidth: 5,
73+ ignoreRayIntersection: true, // always ignore this
74+ drawInFront: true, // Even when burried inside of something, show it.
75+ parentID: AVATAR_SELF_ID
76+ };
77+
78+ var renderStates = [{name: "half", path: halfPath, end: halfEnd},
79+ {name: "full", path: fullPath, end: fullEnd},
80+ {name: "hold", path: holdPath}];
81+
82+ var defaultRenderStates = [{name: "half", distance: DEFAULT_SEARCH_SPHERE_DISTANCE, path: halfPath},
83+ {name: "full", distance: DEFAULT_SEARCH_SPHERE_DISTANCE, path: fullPath},
84+ {name: "hold", distance: DEFAULT_SEARCH_SPHERE_DISTANCE, path: holdPath}];
85+
86+
87+ // triggered when stylus presses a web overlay/entity
88+ var HAPTIC_STYLUS_STRENGTH = 1.0;
89+ var HAPTIC_STYLUS_DURATION = 20.0;
90+
91+ function laserTargetHasKeyboardFocus(laserTarget) {
92+ if (laserTarget && laserTarget !== NULL_UUID) {
93+ return Entities.keyboardFocusOverlay === laserTarget;
94+ }
95+ }
96+
97+ function setKeyboardFocusOnLaserTarget(laserTarget) {
98+ if (laserTarget && laserTarget !== NULL_UUID) {
99+ Entities.wantsHandControllerPointerEvents(laserTarget);
100+ Overlays.keyboardFocusOverlay = NULL_UUID;
101+ Entities.keyboardFocusEntity = laserTarget;
102+ }
103+ }
104+
105+ function sendHoverEnterEventToLaserTarget(hand, laserTarget) {
106+ if (!laserTarget) {
107+ return;
108+ }
109+ var pointerEvent = {
110+ type: "Move",
111+ id: hand + 1, // 0 is reserved for hardware mouse
112+ pos2D: laserTarget.position2D,
113+ pos3D: laserTarget.position,
114+ normal: laserTarget.normal,
115+ direction: Vec3.subtract(ZERO_VEC, laserTarget.normal),
116+ button: "None"
117+ };
118+
119+ if (laserTarget.entityID && laserTarget.entityID !== NULL_UUID) {
120+ Entities.sendHoverEnterEntity(laserTarget.entityID, pointerEvent);
121+ }
122+ }
123+
124+ function sendHoverOverEventToLaserTarget(hand, laserTarget) {
125+
126+ if (!laserTarget) {
127+ return;
128+ }
129+ var pointerEvent = {
130+ type: "Move",
131+ id: hand + 1, // 0 is reserved for hardware mouse
132+ pos2D: laserTarget.position2D,
133+ pos3D: laserTarget.position,
134+ normal: laserTarget.normal,
135+ direction: Vec3.subtract(ZERO_VEC, laserTarget.normal),
136+ button: "None"
137+ };
138+
139+ if (laserTarget.entityID && laserTarget.entityID !== NULL_UUID) {
140+ Entities.sendMouseMoveOnEntity(laserTarget.entityID, pointerEvent);
141+ Entities.sendHoverOverEntity(laserTarget.entityID, pointerEvent);
142+ }
143+ }
144+
145+
146+ function sendTouchStartEventToLaserTarget(hand, laserTarget) {
147+ var pointerEvent = {
148+ type: "Press",
149+ id: hand + 1, // 0 is reserved for hardware mouse
150+ pos2D: laserTarget.position2D,
151+ pos3D: laserTarget.position,
152+ normal: laserTarget.normal,
153+ direction: Vec3.subtract(ZERO_VEC, laserTarget.normal),
154+ button: "Primary",
155+ isPrimaryHeld: true
156+ };
157+
158+ if (laserTarget.entityID && laserTarget.entityID !== NULL_UUID) {
159+ Entities.sendMousePressOnEntity(laserTarget.entityID, pointerEvent);
160+ Entities.sendClickDownOnEntity(laserTarget.entityID, pointerEvent);
161+ }
162+ }
163+
164+ function sendTouchEndEventToLaserTarget(hand, laserTarget) {
165+ var pointerEvent = {
166+ type: "Release",
167+ id: hand + 1, // 0 is reserved for hardware mouse
168+ pos2D: laserTarget.position2D,
169+ pos3D: laserTarget.position,
170+ normal: laserTarget.normal,
171+ direction: Vec3.subtract(ZERO_VEC, laserTarget.normal),
172+ button: "Primary"
173+ };
174+
175+ if (laserTarget.entityID && laserTarget.entityID !== NULL_UUID) {
176+ Entities.sendMouseReleaseOnEntity(laserTarget.entityID, pointerEvent);
177+ Entities.sendClickReleaseOnEntity(laserTarget.entityID, pointerEvent);
178+ Entities.sendHoverLeaveEntity(laserTarget.entityID, pointerEvent);
179+ }
180+ }
181+
182+ function sendTouchMoveEventToLaserTarget(hand, laserTarget) {
183+ var pointerEvent = {
184+ type: "Move",
185+ id: hand + 1, // 0 is reserved for hardware mouse
186+ pos2D: laserTarget.position2D,
187+ pos3D: laserTarget.position,
188+ normal: laserTarget.normal,
189+ direction: Vec3.subtract(ZERO_VEC, laserTarget.normal),
190+ button: "Primary",
191+ isPrimaryHeld: true
192+ };
193+
194+ if (laserTarget.entityID && laserTarget.entityID !== NULL_UUID) {
195+ Entities.sendMouseMoveOnEntity(laserTarget.entityID, pointerEvent);
196+ Entities.sendHoldingClickOnEntity(laserTarget.entityID, pointerEvent);
197+ }
198+ }
199+
200+ function calculateTargetFromEntity(intersection, props) {
201+ if (props.rotation === undefined) {
202+ // if rotation is missing from props object, then this entity has probably been deleted.
203+ return null;
204+ }
205+
206+ // project stylus tip onto entity plane.
207+ var normal = Vec3.multiplyQbyV(props.rotation, {x: 0, y: 0, z: 1});
208+ Vec3.multiplyQbyV(props.rotation, {x: 0, y: 1, z: 0});
209+ var distance = Vec3.dot(Vec3.subtract(intersection, props.position), normal);
210+ var position = Vec3.subtract(intersection, Vec3.multiply(normal, distance));
211+
212+ // generate normalized coordinates
213+ var invRot = Quat.inverse(props.rotation);
214+ var localPos = Vec3.multiplyQbyV(invRot, Vec3.subtract(position, props.position));
215+ var invDimensions = { x: 1 / props.dimensions.x, y: 1 / props.dimensions.y, z: 1 / props.dimensions.z };
216+ var normalizedPosition = Vec3.sum(Vec3.multiplyVbyV(localPos, invDimensions), props.registrationPoint);
217+
218+ // 2D position on entity plane in meters, relative to the bounding box upper-left hand corner.
219+ var position2D = { x: normalizedPosition.x * props.dimensions.x,
220+ y: (1 - normalizedPosition.y) * props.dimensions.y }; // flip y-axis
221+
222+ return {
223+ entityID: props.id,
224+ entityProps: props,
225+ overlayID: null,
226+ distance: distance,
227+ position: position,
228+ position2D: position2D,
229+ normal: normal,
230+ normalizedPosition: normalizedPosition,
231+ dimensions: props.dimensions,
232+ valid: true
233+ };
234+ }
235+
236+ function distance2D(a, b) {
237+ var dx = (a.x - b.x);
238+ var dy = (a.y - b.y);
239+ return Math.sqrt(dx * dx + dy * dy);
240+ }
241+
242+ function WebEntityLaserInput(hand) {
243+ this.hand = hand;
244+ this.active = false;
245+ this.previousLaserClickedTarget = false;
246+ this.laserPressingTarget = false;
247+ this.hover = false;
248+ this.mode = "none";
249+ this.pressEnterLaserTarget = null;
250+ this.laserTarget = null;
251+ this.laserTargetID = null;
252+ this.lastValidTargetID = null;
253+
254+ this.handToController = function() {
255+ return (this.hand === RIGHT_HAND) ? Controller.Standard.RightHand : Controller.Standard.LeftHand;
256+ };
257+
258+ this.getOtherModule = function() {
259+ return (this.hand === RIGHT_HAND) ? leftWebEntityLaserInput : rightWebEntityLaserInput;
260+ };
261+
262+ this.parameters = makeDispatcherModuleParameters(
263+ 550,
264+ this.hand === RIGHT_HAND ? ["rightHand"] : ["leftHand"],
265+ [],
266+ 100);
267+
268+ this.requestTouchFocus = function(laserTarget) {
269+ if (laserTarget !== null || laserTarget !== undefined) {
270+ sendHoverEnterEventToLaserTarget(this.hand, this.laserTarget);
271+ this.lastValidTargetID = laserTarget;
272+ }
273+ };
274+
275+ this.relinquishTouchFocus = function() {
276+ // send hover leave event.
277+ var pointerEvent = { type: "Move", id: this.hand + 1 };
278+ Entities.sendMouseMoveOnEntity(this.lastValidTargetID, pointerEvent);
279+ Entities.sendHoverOverEntity(this.lastValidTargetID, pointerEvent);
280+ Entities.sendHoverLeaveEntity(this.lastValidID, pointerEvent);
281+ };
282+
283+ this.updateLaserTargets = function(controllerData) {
284+ var intersection = controllerData.rayPicks[this.hand];
285+ this.laserTargetID = intersection.objectID;
286+ var props = Entities.getEntityProperties(intersection.objectID);
287+ this.laserTarget = calculateTargetFromEntity(intersection.intersection, props);
288+ };
289+
290+ this.processControllerTriggers = function(controllerData) {
291+ if (controllerData.triggerClicks[this.hand]) {
292+ this.mode = "full";
293+ this.laserPressingTarget = true;
294+ this.hover = false;
295+ } else if (controllerData.triggerValues[this.hand] > TRIGGER_ON_VALUE) {
296+ this.mode = "half";
297+ this.laserPressingTarget = false;
298+ this.hover = true;
299+ this.requestTouchFocus(this.laserTargetID);
300+ } else {
301+ this.mode = "none";
302+ this.laserPressingTarget = false;
303+ this.hover = false;
304+ this.relinquishTouchFocus();
305+
306+ }
307+ };
308+
309+ this.hovering = function() {
310+ if (!laserTargetHasKeyboardFocus(this.laserTagetID)) {
311+ setKeyboardFocusOnLaserTarget(this.laserTargetID);
312+ }
313+ sendHoverOverEventToLaserTarget(this.hand, this.laserTarget);
314+ };
315+
316+ this.laserPressEnter = function () {
317+ sendTouchStartEventToLaserTarget(this.hand, this.laserTarget);
318+ Controller.triggerHapticPulse(HAPTIC_STYLUS_STRENGTH, HAPTIC_STYLUS_DURATION, this.hand);
319+
320+ this.touchingEnterTimer = 0;
321+ this.pressEnterLaserTarget = this.laserTarget;
322+ this.deadspotExpired = false;
323+
324+ var LASER_PRESS_TO_MOVE_DEADSPOT = 0.026;
325+ this.deadspotRadius = Math.tan(LASER_PRESS_TO_MOVE_DEADSPOT) * this.laserTarget.distance;
326+ };
327+
328+ this.laserPressExit = function () {
329+ if (this.laserTarget === null) {
330+ return;
331+ }
332+
333+ // send press event
334+ if (this.deadspotExpired) {
335+ sendTouchEndEventToLaserTarget(this.hand, this.laserTarget);
336+ } else {
337+ sendTouchEndEventToLaserTarget(this.hand, this.pressEnterLaserTarget);
338+ }
339+ };
340+
341+ this.laserPressing = function (controllerData, dt) {
342+ this.touchingEnterTimer += dt;
343+
344+ if (this.laserTarget) {
345+ var POINTER_PRESS_TO_MOVE_DELAY = 0.33; // seconds
346+ if (this.deadspotExpired || this.touchingEnterTimer > POINTER_PRESS_TO_MOVE_DELAY ||
347+ distance2D(this.laserTarget.position2D,
348+ this.pressEnterLaserTarget.position2D) > this.deadspotRadius) {
349+ sendTouchMoveEventToLaserTarget(this.hand, this.laserTarget);
350+ this.deadspotExpired = true;
351+ }
352+ } else {
353+ this.laserPressingTarget = false;
354+ }
355+ };
356+
357+ this.releaseTouchEvent = function() {
358+ if (this.pressEnterLaserTarget === null) {
359+ return;
360+ }
361+
362+ sendTouchEndEventToLaserTarget(this.hand, this.pressEnterLaserTarget);
363+ }
364+
365+ this.updateLaserPointer = function(controllerData) {
366+ var RADIUS = 0.005;
367+ var dim = { x: RADIUS, y: RADIUS, z: RADIUS };
368+
369+ if (this.mode === "full") {
370+ fullEnd.dimensions = dim;
371+ LaserPointers.editRenderState(this.laserPointer, this.mode, {path: fullPath, end: fullEnd});
372+ } else if (this.mode === "half") {
373+ halfEnd.dimensions = dim;
374+ LaserPointers.editRenderState(this.laserPointer, this.mode, {path: halfPath, end: halfEnd});
375+ }
376+
377+ LaserPointers.enableLaserPointer(this.laserPointer);
378+ LaserPointers.setRenderState(this.laserPointer, this.mode);
379+ };
380+
381+ this.isPointingAtWebEntity = function(controllerData) {
382+ var intersection = controllerData.rayPicks[this.hand];
383+ var entityProperty = Entities.getEntityProperties(intersection.objectID);
384+ var entityType = entityProperty.type;
385+
386+ if ((intersection.type === RayPick.INTERSECTED_ENTITY && entityType === "Web")) {
387+ return true;
388+ }
389+ return false
390+ };
391+
392+ this.exitModule = function() {
393+ this.releaseTouchEvent();
394+ this.relinquishTouchFocus();
395+ this.reset();
396+ this.updateLaserPointer();
397+ LaserPointers.disableLaserPointer(this.laserPointer);
398+ };
399+
400+ this.reset = function() {
401+ this.pressEnterLaserTarget = null;
402+ this.laserTarget = null;
403+ this.laserTargetID = null;
404+ this.laserPressingTarget = false;
405+ this.previousLaserClickedTarget = null;
406+ this.mode = "none";
407+ this.active = false;
408+ };
409+
410+ this.isReady = function(controllerData) {
411+ var otherModule = this.getOtherModule();
412+ if (this.isPointingAtWebEntity(controllerData) && !otherModule.active) {
413+ return makeRunningValues(true, [], []);
414+ }
415+
416+ return makeRunningValues(false, [], []);
417+ };
418+
419+ this.run = function(controllerData, deltaTime) {
420+ if (!this.isPointingAtWebEntity(controllerData)) {
421+ this.exitModule();
422+ return makeRunningValues(false, [], []);
423+ }
424+
425+ this.updateLaserTargets(controllerData);
426+ this.processControllerTriggers(controllerData);
427+ this.updateLaserPointer(controllerData);
428+
429+ if (!this.previousLaserClickedTarget && this.laserPressingTarget) {
430+ this.laserPressEnter();
431+ }
432+ if (this.previousLaserClickedTarget && !this.laserPressingTarget) {
433+ this.laserPressExit();
434+ }
435+ this.previousLaserClickedTarget = this.laserPressingTarget;
436+
437+ if (this.laserPressingTarget) {
438+ this.laserPressing(controllerData, deltaTime);
439+ }
440+
441+ if (this.hover) {
442+ this.hovering();
443+ }
444+ return makeRunningValues(true, [], []);
445+ };
446+
447+ this.cleanup = function() {
448+ LaserPointers.disableLaserPointer(this.laserPointer);
449+ LaserPointers.removeLaserPointer(this.laserPointer);
450+ }
451+
452+ this.laserPointer = LaserPointers.createLaserPointer({
453+ joint: (this.hand == RIGHT_HAND) ? "_CONTROLLER_RIGHTHAND" : "_CONTROLLER_LEFTHAND",
454+ filter: RayPick.PICK_ENTITIES,
455+ maxDistance: PICK_MAX_DISTANCE,
456+ posOffset: getGrabPointSphereOffset(this.handToController()),
457+ renderStates: renderStates,
458+ faceAvatar: true,
459+ defaultRenderStates: defaultRenderStates
460+ });
461+ };
462+
463+
464+ var leftWebEntityLaserInput = new WebEntityLaserInput(LEFT_HAND);
465+ var rightWebEntityLaserInput = new WebEntityLaserInput(RIGHT_HAND);
466+
467+ enableDispatcherModule("LeftWebEntityLaserInput", leftWebEntityLaserInput);
468+ enableDispatcherModule("RightWebEntityLaserInput", rightWebEntityLaserInput);
469+
470+ this.cleanup = function() {
471+ leftWebEntityLaserInput.cleanup();
472+ rightWebEntityLaserInput.cleanup();
473+ disableDispatcherModule("LeftWebEntityLaserInput");
474+ disableDispatcherModule("RightWebEntityLaserInput");
475+ };
476+ Script.scriptEnding.connect(this.cleanup);
477+
478+}());

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t