Torque3D/Templates/BaseGame/game/tools/assetBrowser/scripts/editAsset.tscript
JeffR f3cad0d77e Converts the ad-hoc design of the Material Editor to utilize the same inspector interface as most everything else does.
- Overhauls the material editor to simplify and streamline the logic behind it since the inspector does most of the work
- Tweak a few order positions of materialdefinition fields to work better
- Sets AO, Rough and Metal channel fields to use an enum type for human readability
- Updates the MaterialPreview gui control to work with assetIds
- MatEd now supports setting of parent material to inherit from
- Creating a new material now can prompt selecting an existing material to inherit from
- Can now edit the mapTo value of a material in the matEd
- New standalone Composite Texture Editor window for convering AO, Roughness and Metalness maps in a material to an ORMMap
- Can also star the creation of a composite texture via RMB context menu in AB on an image asset
- Moved logic of CubemapEditor from MatEd to it's own stuff
- Made ImageAsset fields now be more clear when they have nothing assigned, and also have a clear button to empty the field's value so it's consistent across the board
- Reorganized the layout of the gui and image files for the MatEd to be easier to navigate
- MaterialEditor now overlays the EditorGUI instead of being forcefully embedded in it, allowing easy editing of the MatEd Gui via the Gui editor
2025-08-03 12:03:02 -05:00

611 lines
18 KiB
Plaintext

function AssetBrowser_editAsset::saveAsset(%this)
{
AssetBrowser_editAssetWindow.text = "Asset Properties";
AssetBrowser.callAssetTypeFunc(%this.editedObjectType, "onSaveProperties", %this.editedObjectData);
AssetBrowser.callAssetTypeFunc(%this.editedObjectType, "onChanged", %this.editedObjectData);
Canvas.popDialog(AssetBrowser_editAsset);
}
function AssetBrowser::editAsset(%this, %assetDef)
{
if(%this.selectMode)
{
%this.selectAsset(%this.selectedAsset);
}
else
{
if(AssetDatabase.isDeclaredAsset(%assetDef))
{
%assetDef = AssetDatabase.acquireAsset(%assetDef);
}
if(isObject(%assetDef))
{
%assetType = AssetDatabase.getAssetType(%assetDef.getAssetId());
%this.callAssetTypeFunc(%assetType, "onEdit", %assetDef);
}
else if(%this.popupMenu.objectType !$= "" && %this.popupMenu.objectData !$= "")
{
%this.callAssetTypeFunc(%this.popupMenu.objectType, "onEdit", %this.popupMenu.objectData);
}
}
}
function AssetBrowser::appendSubLevel(%this)
{
%assetDef = AssetDatabase.acquireAsset(EditAssetPopup.assetId);
%assetType = %assetDef.getClassName();
schedule( 1, 0, "EditorOpenSceneAppend", %assetDef);
}
function AssetBrowser::editAssetInfo(%this)
{
if(%this.popupMenu.objectType !$= "" && %this.popupMenu.objectData !$= "")
{
%this.callAssetTypeFunc(%this.popupMenu.objectType, "onEditProperties", %this.popupMenu.objectData);
}
}
//------------------------------------------------------------
function AssetBrowser::reloadAsset(%this, %assetId)
{
%moduleName = getToken(%assetId, ":", 0);
%moduleDef = ModuleDatabase.findModule(%moduleName);
%assetName = getToken(%assetId, ":", 1);
%assetFilePath = AssetDatabase.getAssetFilePath(%assetId);
AssetDatabase.removeDeclaredAsset(%assetId);
AssetDatabase.addDeclaredAsset(%moduleDef, %assetFilePath);
}
function AssetBrowser::refreshAsset(%this, %assetId)
{
if(%assetId $= "")
{
//if we have no passed-in asset ID, we're probably going through the popup menu, so get our edit popup id
%assetId = EditAssetPopup.assetId;
}
AssetDatabase.refreshAsset(%assetId);
%this.refresh();
}
//------------------------------------------------------------
function AssetBrowser::regeneratePreviewImage(%this)
{
%assetDef = AssetDatabase.acquireAsset(AssetBrowser.popupMenu.objectData);
%dummyObj = new ScriptObject();
%dummyObj.moduleName = AssetDatabase.getAssetModule(AssetBrowser.popupMenu.objectData).moduleId;
%dummyObj.assetName = AssetDatabase.getAssetName(AssetBrowser.popupMenu.objectData);
%assetType = AssetBrowser.popupMenu.objectType;
AssetBrowser.callAssetTypeFunc(%assetType, "generatePreviewImage", %dummyObj, true);
%dummyObj.delete();
AssetDatabase.releaseAsset(AssetBrowser.popupMenu.objectData);
}
//------------------------------------------------------------
function AssetBrowser::renameAsset(%this)
{
//Find out what type it is
//%assetDef = AssetDatabase.acquireAsset(EditAssetPopup.assetId);
%curFirstResponder = AssetBrowser.getFirstResponder();
if(%curFirstResponder != 0)
%curFirstResponder.clearFirstResponder();
if(EditFolderPopup.visible == false)
{
AssetBrowser.selectedAssetPreview-->AssetNameLabel.setActive(true);
AssetBrowser.selectedAssetPreview-->AssetNameLabel.setFirstResponder();
}
}
function AssetBrowser::performRenameAsset(%this, %originalAssetName, %newName)
{
//if the name is different to the asset's original name, rename it!
if(%originalAssetName !$= %newName)
{
%moduleName = AssetBrowser.selectedModule;
if(EditAssetPopup.assetType !$= "Folder")
{
if(%this.isMethod("rename" @ EditAssetPopup.assetType))
{
%oldAssetId = %moduleName @ ":" @ %originalAssetName;
%assetDef = AssetDatabase.acquireAsset(%oldAssetId);
//Do the rename command
%buildCommand = %this @ ".rename" @ EditAssetPopup.assetType @ "(" @ %assetDef @ "," @ %newName @ ");";
eval(%buildCommand);
}
else
{
//do generic action here
%oldAssetId = %moduleName @ ":" @ %originalAssetName;
%assetDef = AssetDatabase.acquireAsset(%oldAssetId);
%assetFileName = fileName(%assetDef.getFileName());
%assetFilePath = filePath(%assetDef.getFileName());
%pattern = %assetFilePath @ "/" @ %assetFileName @ ".*";
echo("Searching for matches of asset files following pattern: " @ %pattern);
//get list of files that match the name in the same dir
//do a rename on them
for (%file = findFirstFileMultiExpr(%pattern); %file !$= ""; %file = findNextFileMultiExpr(%pattern))
{
if(%file !$= %assetFileName)
renameAssetLooseFile(%file, %newName);
}
//update the assetDef
replaceInFile(%assetDef.getFileName(), %originalAssetName, %newName);
renameAssetFile(%assetDef, %newName);
}
}
else
{
%buildCommand = %this @ ".renameFolder(\"" @ EditAssetPopup.assetId @ "\",\"" @ %newName @ "\");";
eval(%buildCommand);
}
}
//Make sure everything is refreshed
%this.refresh();
//Update the selection to immediately jump to the new asset
AssetBrowser-->filterTree.clearSelection();
%ModuleItem = AssetBrowser-->filterTree.findItemByName(%moduleName);
// TODO is this correct?
/*%assetType = %ModuleItem.getClassName();
%assetTypeId = AssetBrowser-->filterTree.findChildItemByName(%ModuleItem, %assetType);
AssetBrowser-->filterTree.selectItem(%assetTypeId);
%selectedItem = AssetBrowser-->filterTree.getSelectedItem();
AssetBrowser-->filterTree.scrollVisibleByObjectId(%selectedItem);
AssetBrowser-->filterTree.buildVisibleTree();*/
}
function renameAssetFile(%assetDef, %newName)
{
%assetId = %assetDef.getAssetID();
%module = AssetDatabase.getAssetModule(%assetId);
%moduleId = %module.moduleId;
%assetPath = AssetDatabase.getAssetFilePath(%assetId);
%newPath = filePath(%assetPath) @ "/" @ %newName @ ".asset.taml";
%copiedSuccess = pathCopy(%assetPath, %newPath);
if(!%copiedSuccess)
return "";
%deleteSuccess = fileDelete(%assetPath);
if(!%deleteSuccess)
return "";
//Remove the old declaration
AssetDatabase.removeDeclaredAsset(%assetId);
//Add with the new file
AssetDatabase.addDeclaredAsset(%module, %newPath);
//Perform the rename in the file/system itself
AssetDatabase.renameDeclaredAsset(%assetId, %moduleId @ ":" @ %newName);
}
function renameAssetLooseFile(%file, %newName)
{
%newPath = filePath(%file) @ "/" @ %newName @ fileExt(%file);
%copiedSuccess = pathCopy(%file, %newPath);
if(!%copiedSuccess)
return "";
%deleteSuccess = fileDelete(%file);
if(!%deleteSuccess)
return "";
return fileName(%newPath);
}
function AssetNameField::onReturn(%this)
{
%this.clearFirstResponder();
%this.setActive(false);
AssetBrowser.performRenameAsset(%this.originalAssetName, %this.getText());
}
//------------------------------------------------------------
function AssetBrowser::moveAsset(%this, %assetId, %destination)
{
if(EditAssetPopup.assetType $= "Folder")
{
//Do any cleanup required given the type
if(%this.isMethod("moveFolder"))
eval(%this @ ".moveFolder("@%assetId@",\""@%destination@"\");");
}
else
{
%assetDef = AssetDatabase.acquireAsset(%assetId);
%assetType = AssetDatabase.getAssetType(%assetId);
//Do any cleanup required given the type
if(%this.isMethod("move"@%assetType))
{
%command = %this @ ".move" @ %assetType @ "(" @ %assetDef @ ",\"" @ %destination @ "\");";
eval(%this @ ".move" @ %assetType @ "(" @ %assetDef @ ",\"" @ %destination @ "\");");
}
}
%this.refresh();
}
function moveAssetFile(%assetDef, %destinationPath)
{
%assetPath = makeFullPath(AssetDatabase.getAssetFilePath(%assetDef.getAssetId()));
%assetFilename = fileName(%assetPath);
%newAssetPath = %destinationPath @ "/" @ %assetFilename;
%copiedSuccess = pathCopy(%assetPath, %destinationPath @ "/" @ %assetFilename);
if(!%copiedSuccess)
return "";
%deleteSuccess = fileDelete(%assetPath);
if(!%deleteSuccess)
return "";
return %newAssetPath;
}
function moveAssetLooseFile(%file, %destinationPath)
{
%filename = fileName(%file);
%copiedSuccess = pathCopy(%file, %destinationPath @ "/" @ %filename);
if(!%copiedSuccess)
return false;
%deleteSuccess = fileDelete(%file);
return %deleteSuccess;
}
//------------------------------------------------------------
function AssetBrowser::duplicateAsset(%this)
{
%assetDef = AssetDatabase.acquireAsset(%this.popupMenu.objectData);
%assetType = %this.popupMenu.objectType;
%trailingNum = getTrailingNumber(%assetDef.assetName);
if(%trailingNum != -1)
{
%trailingNum++;
%newName = stripTrailingNumber(%assetDef.assetName) @ (%trailingNum);
}
else
{
%newName = stripTrailingNumber(%assetDef.assetName) @ "1";
}
AssetBrowser_assetNameEditTxt.text = %newName;
AssetBrowser_assetNameEdit.callback = "AssetBrowser.doDuplicateAsset();";
if(EditorSettings.value("AssetManagement/Assets/promptOnRename", "1") == 1)
Canvas.pushDialog(AssetBrowser_assetNameEdit);
else
eval(AssetBrowser_assetNameEdit.callback);
}
function AssetBrowser::doDuplicateAsset(%this)
{
%assetType = %this.popupMenu.objectType;
%assetDef = AssetDatabase.acquireAsset(%this.popupMenu.objectData);
if(AssetBrowser_assetNameEditTxt.text !$= "" && AssetBrowser_assetNameEditTxt.text !$= %assetDef.assetName)
{
AssetDatabase.releaseAsset(%this.popupMenu.objectData);
%this.callAssetTypeFunc(%assetType, "onDuplicate", %this.popupMenu.objectData, AssetBrowser_assetNameEditTxt.text);
%this.refresh();
}
}
function duplicateAssetFile(%assetDef, %newAssetName)
{
%assetPath = makeFullPath(AssetDatabase.getAssetFilePath(%assetDef.getAssetId()));
%assetFilepath = filePath(%assetPath);
%assetFileExt = fileExt(%assetPath);
%newAssetPath = %assetFilepath @ "/" @ %newAssetName @ ".asset.taml";
%copiedSuccess = pathCopy(%assetPath, %newAssetPath);
if(!%copiedSuccess)
return "";
replaceInFile(%newAssetPath, %assetDef.assetName, %newAssetName);
%module = AssetBrowser.dirHandler.getModuleFromAddress(%newAssetPath);
//Add with the new file
AssetDatabase.addDeclaredAsset(%module, %newAssetPath);
return %newAssetPath;
}
function duplicateAssetLooseFile(%file, %newFilename)
{
%filePath = filePath(%file);
%fileExt = fileExt(%file);
%newPath = %filePath @ "/" @ %newFilename @ %fileExt;
%copiedSuccess = pathCopy(%file, %newPath);
if(!%copiedSuccess)
return "";
return %newPath;
}
//------------------------------------------------------------
function AssetBrowser::deleteAsset(%this)
{
//Find out what type it is
//%assetDef = AssetDatabase.acquireAsset(EditAssetPopup.assetId);
//%assetType = %assetDef.getClassName();
toolsMessageBoxOKCancel("Warning!", "This will delete the selected content and the files associated to it, do you wish to continue?",
"AssetBrowser.confirmDeleteAsset();", "");
}
function AssetBrowser::confirmDeleteAsset(%this)
{
%currentSelectedItem = AssetBrowserFilterTree.getSelectedItem();
%currentItemParent = AssetBrowserFilterTree.getParentItem(%currentSelectedItem);
%this.callAssetTypeFunc(%this.popupMenu.objectType, "onDelete", %this.popupMenu.objectData);
%this.refresh();
}
//------------------------------------------------------------
function AssetBrowser::updateAssetReference(%this, %targetPath, %oldAssetId, %newAssetId)
{
//assetbrowser.updateAssetReference("data/pbr/levels", "pbr:material_ball", "TreeTest:TestTree");
//this will go through every file in the game directory and swap the assetIDs to update the reference in the event something was renamed, or something was moved
//This is potentially disastrous and break a lot of things if done hapazardly, so be careful
%fullPath = makeFullPath(%targetPath);
//First, wipe out any files inside the folder first
%file = findFirstFileMultiExpr( %fullPath @ "/*.*", true);
%fileObj = new FileObject();
new ArrayObject(lineCache);
while( %file !$= "" )
{
lineCache.empty();
%fileModified = false;
if(%fileObj.openForRead(%file))
{
while( !%fileObj.isEOF() )
{
%unModLine = %fileObj.readLine();
%line = strreplace( %unModLine, %oldAssetId, %newAssetId );
if(%unModLine !$= %line)
%fileModified = true;
lineCache.add(%line);
}
}
if(%fileModified && %fileObj.openForWrite(%file))
{
for(%i=0; %i<lineCache.count(); %i++)
{
%line = lineCache.getKey(%i);
%fileObj.writeline(%line);
}
%fileObj.close();
}
%file = findNextFileMultiExpr( %fullPath @ "/*.*" );
}
lineCache.delete();
%fileObj.delete();
}
function AssetBrowser::openFileLocation(%this)
{
if(isFunction("systemCommand"))
{
warnf("AssetBrowser::openFileLocation() - systemCommand function disabled in this build. Unable to launch application to edit file.");
return;
}
%filePath = "";
if(%this.popuMenu.assetId !$= "")
{
if(AssetDatabase.isDeclaredAsset(%this.popuMenu.objectData))
{
%filePath = AssetDatabase.getAssetPath(%this.popuMenu.objectData);
}
else
{
//probably a file path
%pathSplit = strpos(%this.popuMenu.objectData, ":");
if(%pathSplit != -1)
{
%path = getSubStr(%this.popuMenu.objectData, 0, %pathSplit);
%file = getSubStr(%this.popuMenu.objectData, %pathSplit + 1);
//datablocks pack the originator file in the parent path as-is, so check that
if(fileExt(%path) !$= "")
{
%filePath = %path;
}
else
{
%filePath = %path @ "/" @ %file;
}
}
}
}
if(isFile(%filePath) || isDirectory(%filePath))
{
%fullPath = makeFullPath(%filePath);
if(fileExt(%fullPath) $= ".tscript")
%fullPath = filePath(%fullPath);
if($platform $= "windows")
{
%cmd = "cd \"" @ %fullPath @ "\" && start .";
systemCommand(%cmd);
}
else
{
%cmd = "open \"" @ %fullPath @ "\"";
systemCommand(%cmd);
}
}
}
function AssetBrowser::openFolderLocation(%this, %folderPath)
{
if(!isFunction("systemCommand"))
{
warn("AssetBrowser::openFolderLocation() - systemCommand function disabled in this build. Unable to launch application to open this folder.");
return;
}
if(%folderPath $= "")
%folderPath = filePath(%this.dirHandler.currentAddress);
if(%folderPath !$= "")
{
if($platform $= "windows")
{
%cmd = "cd \"" @ makeFullPath(%folderPath) @ "\" && start .";
systemCommand(%cmd);
}
else
{
%cmd = "open \"" @ makeFullPath(%folderPath) @ "\"";
systemCommand(%cmd);
}
}
}
//-------------------------------------------------------------
function AssetBrowser::getAssetBackupCount(%this, %assetId)
{
//process it and then check if we have any autosave backups
%processedId = strReplace(EditAssetPopup.assetId, ":", "_");
%autosavePath = "tools/autosave/" @ %processedId @ "/";
RestoreBackupListPopup.clearItems();
if(isDirectory(%autosavePath))
{
%dirs = getDirectoryList(%autosavePath);
%count = getFieldCount(%dirs);
return %count;
}
return 0;
}
function AssetBrowser::restoreAssetBackup(%this, %assetId, %index)
{
if(!AssetDatabase.isDeclaredAsset(%assetId))
{
error("AssetBrowser::restoreAssetBackup() - Attempted to restore backed up version of asset: " @ %assetId @ " but asset is not validly declared!");
return;
}
//process it and then check if we have any autosave backups
%processedId = strReplace(%assetId, ":", "_");
%autosavePath = "tools/autosave/" @ %processedId @ "/";
if(isDirectory(%autosavePath))
{
%dirs = getDirectoryList(%autosavePath);
%count = getFieldCount(%dirs);
if(%count > 0)
{
%saveArray = new ArrayObject(){};
//loop over the entries and find the oldest one
for(%f=0; %f < %count; %f++)
{
%saveArray.add(getField(%dirs, %f));
}
%saveArray.sortk();
%folderName = %saveArray.getKey(%index);
//now we just copy the contents of the folder into our assetId path and refresh
%assetPath = AssetDatabase.getAssetPath(%assetId);
%autosaveFullPath = %autosavePath @ "/" @ %folderName @ "/";
%autosaveFullPath = strReplace(%autosaveFullPath, "//", "/");
%file = findFirstFile( %autosaveFullPath @ "*.*" );
while( %file !$= "" )
{
%fileName = fileName(%file);
%assetFileName = %assetPath @ "/" @ %fileName;
warn("| Copying file from: " @ %file @ " to: " @ %assetFileName);
if(!pathCopy(%file, %assetFileName, false))
{
error("AssetBrowser::restoreAssetBackup() - Something went wrong when copying the file: " @ %file @ " to " @ %assetFileName);
}
%file = findNextFile( %autosaveFullPath @ "*.*" );
}
AssetBrowser.reloadAsset(%assetId);
}
else
{
error("AssetBrowser::restoreAssetBackup() - Attempted to restore backed up version of asset: " @ %assetId @ " but no autosaves were found!");
}
}
else
{
error("AssetBrowser::restoreAssetBackup() - Attempted to restore backed up version of asset: " @ %assetId @ " but autosave directory doesn't exist!");
}
}