diff --git a/scripts/system/edit.js b/scripts/system/edit.js index 11fc88dcbd..0346e1c7a1 100644 --- a/scripts/system/edit.js +++ b/scripts/system/edit.js @@ -2523,18 +2523,24 @@ var PropertiesTool = function (opts) { propertyRanges: propertyRanges, }); } else if (data.type === "materialTargetRequest") { - var properties = Entities.getEntityProperties(data.entityID, ["type", "parentID"]); - var parentModel = properties.parentID !== Uuid.NULL && - Entities.getEntityProperties(properties.parentID, ["type"]).type === "Model"; - var parentModelData; - if (properties.type === "Material" && parentModel) { - parentModelData = Graphics.getModel(properties.parentID); - } - emitScriptEvent({ - type: 'materialTargetReply', - materialTargetData: parentModelData, - }); - } + var parentModelData; + var properties = Entities.getEntityProperties(data.entityID, ["type", "parentID"]); + if (properties.type === "Material" && properties.parentID !== Uuid.NULL) { + var parentType = Entities.getEntityProperties(properties.parentID, ["type"]).type; + if (parentType === "Model" || Entities.getNestableType(properties.parentID) === "avatar") { + parentModelData = Graphics.getModel(properties.parentID); + } else if (parentType === "Shape" || parentType === "Box" || parentType === "Sphere") { + parentModelData = {}; + parentModelData.numMeshes = 1; + parentModelData.materialNames = []; + } + } + emitScriptEvent({ + type: 'materialTargetReply', + entityID: data.entityID, + materialTargetData: parentModelData, + }); + } }; HMD.displayModeChanged.connect(function() { diff --git a/scripts/system/html/js/entityProperties.js b/scripts/system/html/js/entityProperties.js index 3283e6c266..78a6e26ff0 100644 --- a/scripts/system/html/js/entityProperties.js +++ b/scripts/system/html/js/entityProperties.js @@ -55,6 +55,7 @@ const GROUPS = [ label: "Parent", type: "string", propertyID: "parentID", + onChange: parentIDChanged, }, { label: "Parent Joint Index", @@ -2009,6 +2010,9 @@ function createStringProperty(property, elProperty) { elInput.addEventListener('change', createEmitTextPropertyUpdateFunction(property)); + if (propertyData.onChange !== undefined) { + elInput.addEventListener('change', propertyData.onChange); + } elProperty.appendChild(elInput); @@ -2478,7 +2482,7 @@ function resetDynamicMultiselectProperty(elDivOptions) { let elDivOption = elInputs[0].parentNode; elDivOption.parentNode.removeChild(elDivOption); } - elDivOptions.firstChild.style.display = "block"; // show "No Options" text + elDivOptions.firstChild.style.display = null; // show "No Options" text elDivOptions.parentNode.lastChild.style.display = "none"; // hide Select/Clear all buttons } @@ -2624,6 +2628,17 @@ function createProperty(propertyData, propertyElementID, propertyName, propertyI } +/** + * PROPERTY-SPECIFIC CALLBACKS + */ + +function parentIDChanged() { + if (selectedEntityProperties.type === "Material") { + requestMaterialTarget(); + } +} + + /** * BUTTON CALLBACKS */ @@ -3158,6 +3173,10 @@ function setTextareaScrolling(element) { * MATERIAL TARGET FUNCTIONS */ +function requestMaterialTarget() { + EventBridge.emitWebEvent(JSON.stringify({ type: 'materialTargetRequest', entityID: selectedEntityProperties.id })); +} + function setMaterialTargetData(materialTargetData) { let elDivOptions = getPropertyInputElement("parentMaterialName"); resetDynamicMultiselectProperty(elDivOptions); @@ -3167,7 +3186,7 @@ function setMaterialTargetData(materialTargetData) { } elDivOptions.firstChild.style.display = "none"; // hide "No Options" text - elDivOptions.parentNode.lastChild.style.display = "block"; // show Select/Clear all buttons + elDivOptions.parentNode.lastChild.style.display = null; // show Select/Clear all buttons let numMeshes = materialTargetData.numMeshes; for (let i = 0; i < numMeshes; ++i) { @@ -3243,35 +3262,35 @@ function sendMaterialTargetProperty() { let elDivOptions = getPropertyInputElement("parentMaterialName"); let elInputs = elDivOptions.getElementsByClassName("materialTargetInput"); - let materialTargetList = ""; + let materialTargetList = []; for (let i = 0; i < elInputs.length; ++i) { let elInput = elInputs[i]; if (elInput.checked) { let targetID = elInput.getAttribute("targetID"); if (elInput.getAttribute("isMaterialName") === "true") { - materialTargetList += "mat::" + targetID + ","; + materialTargetList.push("mat::" + targetID); } else { - materialTargetList += targetID + ","; + materialTargetList.push(targetID); } } } - if (materialTargetList !== "") { - materialTargetList = materialTargetList.substring(0, materialTargetList.length - 1); - materialTargetList = "[" + materialTargetList + "]"; + let propertyValue = materialTargetList.join(","); + if (propertyValue.length > 1) { + propertyValue = "[" + propertyValue + "]"; } - updateProperty("parentMaterialName", materialTargetList, false); + updateProperty("parentMaterialName", propertyValue, false); } function materialTargetPropertyUpdate(propertyValue) { let elDivOptions = getPropertyInputElement("parentMaterialName"); let elInputs = elDivOptions.getElementsByClassName("materialTargetInput"); - if (propertyValue.charAt(0) === '[') { + if (propertyValue.startsWith('[')) { propertyValue = propertyValue.substring(1, propertyValue.length); } - if (propertyValue.charAt(propertyValue.length - 1) === ']') { + if (propertyValue.endsWith(']')) { propertyValue = propertyValue.substring(0, propertyValue.length - 1); } @@ -3782,7 +3801,7 @@ function loaded() { } if (hasSelectedEntityChanged && selectedEntityProperties.type === "Material") { - EventBridge.emitWebEvent(JSON.stringify({ type: 'materialTargetRequest', entityID: selectedEntityProperties.id })); + requestMaterialTarget(); } let activeElement = document.activeElement; @@ -3835,7 +3854,9 @@ function loaded() { } } } else if (data.type === 'materialTargetReply') { - setMaterialTargetData(data.materialTargetData); + if (data.entityID === selectedEntityProperties.id) { + setMaterialTargetData(data.materialTargetData); + } } });