From f159d4c91dae39f9f05c9009e10a6dd44da2abec Mon Sep 17 00:00:00 2001 From: Triplelexx Date: Tue, 31 Jan 2017 20:45:35 +0000 Subject: [PATCH 1/6] create nameTag.js script Running the script creates a text entity that will hover over the users head showing their display name. --- scripts/system/nameTag.js | 98 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 scripts/system/nameTag.js diff --git a/scripts/system/nameTag.js b/scripts/system/nameTag.js new file mode 100644 index 0000000000..adaab83297 --- /dev/null +++ b/scripts/system/nameTag.js @@ -0,0 +1,98 @@ +"use strict"; + +/*jslint vars: true, plusplus: true*/ +/*global Entities, Script, Quat, Vec3, MyAvatar, print*/ +// nameTag.js +// +// Created by Triplelexx on 17/01/31 +// Copyright 2017 High Fidelity, Inc. +// +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html + +const NAMETAG_DIMENSIONS = { + x: 1.0, + y: 0.2, + z: 1.0 +} + +const CLIENTONLY = false; +const NULL_UUID = "{00000000-0000-0000-0000-000000000000}"; +const ENTITY_CHECK_INTERVAL = 5000; // ms = 5 seconds +const STARTUP_DELAY = 2000; // ms = 2 second +const OLD_AGE = 3500; // we recreate the entity if older than this time in seconds +const TTL = 2; // time to live in seconds if script is not running +const HEIGHT_ABOVE_HEAD = 0.2; +const HEAD_OFFSET = -0.025; + +var nametagEntityID = NULL_UUID; +var lastCheckForEntity = 0; + +// create the name tag entity after a brief delay +Script.setTimeout(function() { + addNameTag(); +}, STARTUP_DELAY); + +function addNameTag() { + var nametagPosition = Vec3.sum(MyAvatar.getHeadPosition(), Vec3.multiply(HEAD_OFFSET, Quat.getFront(MyAvatar.orientation))); + nametagPosition.y += HEIGHT_ABOVE_HEAD; + var modelNameTagProperties = { + type: 'Text', + text: MyAvatar.displayName, + parentID: MyAvatar.sessionUUID, + dimensions: NAMETAG_DIMENSIONS, + position: nametagPosition + } + nametagEntityID = Entities.addEntity(modelNameTagProperties, CLIENTONLY); +} + +function deleteNameTag() { + if(nametagEntityID !== NULL_UUID) { + Entities.deleteEntity(nametagEntityID); + nametagEntityID = NULL_UUID; + } +} + +// cleanup on ending +Script.scriptEnding.connect(cleanup); +function cleanup() { + deleteNameTag(); +} + +Script.update.connect(update); +function update() { + // bail if no entity + if(nametagEntityID == NULL_UUID) { + return; + } + + if(Date.now() - lastCheckForEntity > ENTITY_CHECK_INTERVAL) { + checkForEntity(); + lastCheckForEntity = Date.now(); + } +} + +function checkForEntity() { + var nametagProps = Entities.getEntityProperties(nametagEntityID); + + // it is possible for the age to not be a valid number, we check for this and bail accordingly + if(nametagProps.age < 1) { + return; + } + + // it's too old make a new one, otherwise update + if(nametagProps.age > OLD_AGE || nametagProps.age == undefined) { + deleteNameTag(); + addNameTag(); + } else { + var nametagPosition = Vec3.sum(MyAvatar.getHeadPosition(), Vec3.multiply(HEAD_OFFSET, Quat.getFront(MyAvatar.orientation))); + nametagPosition.y += HEIGHT_ABOVE_HEAD; + Entities.editEntity(nametagEntityID, { + position: nametagPosition, + // lifetime is in seconds we add TTL on top of the next poll time + lifetime: Math.round(nametagProps.age) + (ENTITY_CHECK_INTERVAL / 1000) + TTL, + text: MyAvatar.displayName + }); + } +} From be68f84100ed073266722f22b7df556f07060504 Mon Sep 17 00:00:00 2001 From: Triplelexx Date: Tue, 31 Jan 2017 22:14:39 +0000 Subject: [PATCH 2/6] update nameTag.js comments --- scripts/system/nameTag.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/scripts/system/nameTag.js b/scripts/system/nameTag.js index adaab83297..cd8df2c477 100644 --- a/scripts/system/nameTag.js +++ b/scripts/system/nameTag.js @@ -7,6 +7,7 @@ // Created by Triplelexx on 17/01/31 // Copyright 2017 High Fidelity, Inc. // +// Running the script creates a text entity that will hover over the user's head showing their display name. // // Distributed under the Apache License, Version 2.0. // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html @@ -38,6 +39,7 @@ function addNameTag() { var nametagPosition = Vec3.sum(MyAvatar.getHeadPosition(), Vec3.multiply(HEAD_OFFSET, Quat.getFront(MyAvatar.orientation))); nametagPosition.y += HEIGHT_ABOVE_HEAD; var modelNameTagProperties = { + name: MyAvatar.displayName + ' Name Tag', type: 'Text', text: MyAvatar.displayName, parentID: MyAvatar.sessionUUID, @@ -62,7 +64,7 @@ function cleanup() { Script.update.connect(update); function update() { - // bail if no entity + // if no entity we return if(nametagEntityID == NULL_UUID) { return; } @@ -76,12 +78,12 @@ function update() { function checkForEntity() { var nametagProps = Entities.getEntityProperties(nametagEntityID); - // it is possible for the age to not be a valid number, we check for this and bail accordingly + // it is possible for the age to not be a valid number, we check for this and return accordingly if(nametagProps.age < 1) { return; } - // it's too old make a new one, otherwise update + // it's too old or we receive undefined make a new one, otherwise update if(nametagProps.age > OLD_AGE || nametagProps.age == undefined) { deleteNameTag(); addNameTag(); From b5d2926605f57b78137a12f9ca7493b3a8448290 Mon Sep 17 00:00:00 2001 From: Triplelexx Date: Wed, 1 Feb 2017 12:49:41 +0000 Subject: [PATCH 3/6] make text entity smaller and allow dynamic resizing based on name length --- scripts/system/nameTag.js | 74 ++++++++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 29 deletions(-) diff --git a/scripts/system/nameTag.js b/scripts/system/nameTag.js index cd8df2c477..ceb7583a2e 100644 --- a/scripts/system/nameTag.js +++ b/scripts/system/nameTag.js @@ -12,12 +12,6 @@ // Distributed under the Apache License, Version 2.0. // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html -const NAMETAG_DIMENSIONS = { - x: 1.0, - y: 0.2, - z: 1.0 -} - const CLIENTONLY = false; const NULL_UUID = "{00000000-0000-0000-0000-000000000000}"; const ENTITY_CHECK_INTERVAL = 5000; // ms = 5 seconds @@ -26,8 +20,11 @@ const OLD_AGE = 3500; // we recreate the entity if older than this time in secon const TTL = 2; // time to live in seconds if script is not running const HEIGHT_ABOVE_HEAD = 0.2; const HEAD_OFFSET = -0.025; +const SIZE_Y = 0.075; +const LETTER_OFFSET = 0.0225; +const LINE_HEIGHT = 0.05; -var nametagEntityID = NULL_UUID; +var nameTagEntityID = NULL_UUID; var lastCheckForEntity = 0; // create the name tag entity after a brief delay @@ -36,26 +33,53 @@ Script.setTimeout(function() { }, STARTUP_DELAY); function addNameTag() { - var nametagPosition = Vec3.sum(MyAvatar.getHeadPosition(), Vec3.multiply(HEAD_OFFSET, Quat.getFront(MyAvatar.orientation))); - nametagPosition.y += HEIGHT_ABOVE_HEAD; + var nameTagPosition = Vec3.sum(MyAvatar.getHeadPosition(), Vec3.multiply(HEAD_OFFSET, Quat.getFront(MyAvatar.orientation))); + nameTagPosition.y += HEIGHT_ABOVE_HEAD; var modelNameTagProperties = { name: MyAvatar.displayName + ' Name Tag', type: 'Text', text: MyAvatar.displayName, + lineHeight: LINE_HEIGHT, parentID: MyAvatar.sessionUUID, - dimensions: NAMETAG_DIMENSIONS, - position: nametagPosition + dimensions: dimensionsFromName(), + position: nameTagPosition } - nametagEntityID = Entities.addEntity(modelNameTagProperties, CLIENTONLY); + nameTagEntityID = Entities.addEntity(modelNameTagProperties, CLIENTONLY); } +function updateNameTag() { + var nameTagProps = Entities.getEntityProperties(nameTagEntityID); + var nameTagPosition = Vec3.sum(MyAvatar.getHeadPosition(), Vec3.multiply(HEAD_OFFSET, Quat.getFront(MyAvatar.orientation))); + nameTagPosition.y += HEIGHT_ABOVE_HEAD; + + Entities.editEntity(nameTagEntityID, { + position: nameTagPosition, + dimensions: dimensionsFromName(), + // lifetime is in seconds we add TTL on top of the next poll time + lifetime: Math.round(nameTagProps.age) + (ENTITY_CHECK_INTERVAL / 1000) + TTL, + text: MyAvatar.displayName + }); +}; + function deleteNameTag() { - if(nametagEntityID !== NULL_UUID) { - Entities.deleteEntity(nametagEntityID); - nametagEntityID = NULL_UUID; + if(nameTagEntityID !== NULL_UUID) { + Entities.deleteEntity(nameTagEntityID); + nameTagEntityID = NULL_UUID; } } +function dimensionsFromName() { + var nameTagDimensions = { + x: 0.0, + y: SIZE_Y, + z: 0.0 + } + for(var letter in MyAvatar.displayName) { + nameTagDimensions.x += LETTER_OFFSET; + } + return nameTagDimensions; +}; + // cleanup on ending Script.scriptEnding.connect(cleanup); function cleanup() { @@ -65,7 +89,7 @@ function cleanup() { Script.update.connect(update); function update() { // if no entity we return - if(nametagEntityID == NULL_UUID) { + if(nameTagEntityID == NULL_UUID) { return; } @@ -76,25 +100,17 @@ function update() { } function checkForEntity() { - var nametagProps = Entities.getEntityProperties(nametagEntityID); - + var nameTagProps = Entities.getEntityProperties(nameTagEntityID); // it is possible for the age to not be a valid number, we check for this and return accordingly - if(nametagProps.age < 1) { + if(nameTagProps.age < 1) { return; } - + // it's too old or we receive undefined make a new one, otherwise update - if(nametagProps.age > OLD_AGE || nametagProps.age == undefined) { + if(nameTagProps.age > OLD_AGE || nameTagProps.age == undefined) { deleteNameTag(); addNameTag(); } else { - var nametagPosition = Vec3.sum(MyAvatar.getHeadPosition(), Vec3.multiply(HEAD_OFFSET, Quat.getFront(MyAvatar.orientation))); - nametagPosition.y += HEIGHT_ABOVE_HEAD; - Entities.editEntity(nametagEntityID, { - position: nametagPosition, - // lifetime is in seconds we add TTL on top of the next poll time - lifetime: Math.round(nametagProps.age) + (ENTITY_CHECK_INTERVAL / 1000) + TTL, - text: MyAvatar.displayName - }); + updateNameTag(); } } From 5bf29f46570c92c392047d5bdf7824e5e17d783c Mon Sep 17 00:00:00 2001 From: Triplelexx Date: Wed, 1 Feb 2017 12:59:40 +0000 Subject: [PATCH 4/6] improve dimensionsFromName implementation --- scripts/system/nameTag.js | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/scripts/system/nameTag.js b/scripts/system/nameTag.js index ceb7583a2e..c95db0e647 100644 --- a/scripts/system/nameTag.js +++ b/scripts/system/nameTag.js @@ -69,15 +69,11 @@ function deleteNameTag() { } function dimensionsFromName() { - var nameTagDimensions = { - x: 0.0, + return { + x: LETTER_OFFSET * MyAvatar.displayName.length, y: SIZE_Y, z: 0.0 - } - for(var letter in MyAvatar.displayName) { - nameTagDimensions.x += LETTER_OFFSET; - } - return nameTagDimensions; + }; }; // cleanup on ending From 1a58dad3adc3525bd79c754b297412c35f781b41 Mon Sep 17 00:00:00 2001 From: Triplelexx Date: Thu, 2 Feb 2017 01:12:55 +0000 Subject: [PATCH 5/6] change age check check for equal to -1 instead of < 1, think this would be more suitable if undefined is returned. --- scripts/system/nameTag.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/system/nameTag.js b/scripts/system/nameTag.js index c95db0e647..88f7ab1f89 100644 --- a/scripts/system/nameTag.js +++ b/scripts/system/nameTag.js @@ -98,7 +98,7 @@ function update() { function checkForEntity() { var nameTagProps = Entities.getEntityProperties(nameTagEntityID); // it is possible for the age to not be a valid number, we check for this and return accordingly - if(nameTagProps.age < 1) { + if(nameTagProps.age == -1) { return; } From e7244b47a9e9b6f96e66390f5194af847dd5a307 Mon Sep 17 00:00:00 2001 From: Triplelexx Date: Tue, 7 Feb 2017 21:55:55 +0000 Subject: [PATCH 6/6] increase LETTER_OFFSET value allow more room for characters, could be improved by detecting each one --- scripts/system/nameTag.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/scripts/system/nameTag.js b/scripts/system/nameTag.js index 88f7ab1f89..e25db69064 100644 --- a/scripts/system/nameTag.js +++ b/scripts/system/nameTag.js @@ -21,7 +21,7 @@ const TTL = 2; // time to live in seconds if script is not running const HEIGHT_ABOVE_HEAD = 0.2; const HEAD_OFFSET = -0.025; const SIZE_Y = 0.075; -const LETTER_OFFSET = 0.0225; +const LETTER_OFFSET = 0.03; // arbitrary value to dynamically change width, could be more accurate by detecting characters const LINE_HEIGHT = 0.05; var nameTagEntityID = NULL_UUID; @@ -35,7 +35,7 @@ Script.setTimeout(function() { function addNameTag() { var nameTagPosition = Vec3.sum(MyAvatar.getHeadPosition(), Vec3.multiply(HEAD_OFFSET, Quat.getFront(MyAvatar.orientation))); nameTagPosition.y += HEIGHT_ABOVE_HEAD; - var modelNameTagProperties = { + var nameTagProperties = { name: MyAvatar.displayName + ' Name Tag', type: 'Text', text: MyAvatar.displayName, @@ -44,7 +44,7 @@ function addNameTag() { dimensions: dimensionsFromName(), position: nameTagPosition } - nameTagEntityID = Entities.addEntity(modelNameTagProperties, CLIENTONLY); + nameTagEntityID = Entities.addEntity(nameTagProperties, CLIENTONLY); } function updateNameTag() { @@ -73,7 +73,7 @@ function dimensionsFromName() { x: LETTER_OFFSET * MyAvatar.displayName.length, y: SIZE_Y, z: 0.0 - }; + } }; // cleanup on ending