From 1ba658ee454b3f5c5a7f93a720b795ce5abf6e67 Mon Sep 17 00:00:00 2001 From: David Rowe Date: Thu, 3 Aug 2017 19:52:39 +1200 Subject: [PATCH] Implement grouping and ungrouping --- scripts/vr-edit/modules/groups.js | 55 +++++++++++++++++++++++++++++-- scripts/vr-edit/vr-edit.js | 6 ++-- 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/scripts/vr-edit/modules/groups.js b/scripts/vr-edit/modules/groups.js index c62615f25e..39217c8a76 100644 --- a/scripts/vr-edit/modules/groups.js +++ b/scripts/vr-edit/modules/groups.js @@ -80,16 +80,67 @@ Groups = function () { } function group() { - // TODO + var rootID, + i, + count; + + // Make the first entity in the first group the root and link the first entities of all other groups to it. + rootID = groupRootEntityIDs[0]; + for (i = 1, count = groupRootEntityIDs.length; i < count; i += 1) { + Entities.editEntity(groupRootEntityIDs[i], { + parentID: rootID + }); + } + + // Update selection. + groupRootEntityIDs.splice(1, groupRootEntityIDs.length - 1); + for (i = 1, count = groupSelectionDetails.length; i < count; i += 1) { + groupSelectionDetails[i][0].parentID = rootID; + groupSelectionDetails[0] = groupSelectionDetails[0].concat(groupSelectionDetails[i]); + } + groupSelectionDetails.splice(1, groupSelectionDetails.length - 1); } function ungroup() { - // TODO + var rootID, + childrenIDs, + childrenIDIndexes, + i, + count, + NULL_UUID = "{00000000-0000-0000-0000-000000000000}"; + + // Compile information on children. + rootID = groupRootEntityIDs[0]; + childrenIDs = []; + childrenIDIndexes = []; + for (i = 1, count = groupSelectionDetails[0].length; i < count; i += 1) { + if (groupSelectionDetails[0][i].parentID === rootID) { + childrenIDs.push(groupSelectionDetails[0][i].id); + childrenIDIndexes.push(i); + } + } + childrenIDIndexes.push(groupSelectionDetails[0].length); // Extra item at end to aid updating selection. + + // Unlink direct children from root entity. + for (i = 0, count = childrenIDs.length; i < count; i += 1) { + Entities.editEntity(childrenIDs[i], { + parentID: NULL_UUID + }); + } + + // Update selection. + groupRootEntityIDs = groupRootEntityIDs.concat(childrenIDs); + for (i = 0, count = childrenIDs.length; i < count; i += 1) { + groupSelectionDetails.push(groupSelectionDetails[0].slice(childrenIDIndexes[i], childrenIDIndexes[i + 1])); + groupSelectionDetails[i + 1][0].parentID = NULL_UUID; + } + groupSelectionDetails[0].splice(1, groupSelectionDetails[0].length - childrenIDIndexes[0]); } function clear() { groupRootEntityIDs = []; groupSelectionDetails = []; + numberOfEntitiesSelected = 0; } function destroy() { diff --git a/scripts/vr-edit/vr-edit.js b/scripts/vr-edit/vr-edit.js index 6ddaeb5b97..4231865016 100644 --- a/scripts/vr-edit/vr-edit.js +++ b/scripts/vr-edit/vr-edit.js @@ -1215,17 +1215,15 @@ } function onUICommand(command) { - if (toolSelected === TOOL_GROUP) { - grouping.clear(); - } - switch (command) { case "scaleTool": + grouping.clear(); toolSelected = TOOL_SCALE; ui.setToolIcon(ui.SCALE_TOOL); ui.updateUIEntities(); break; case "cloneTool": + grouping.clear(); toolSelected = TOOL_CLONE; ui.setToolIcon(ui.CLONE_TOOL); ui.updateUIEntities();