editAsset.tscript 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. function AssetBrowser_editAsset::saveAsset(%this)
  2. {
  3. %file = AssetDatabase.getAssetFilePath(%this.editedAssetId);
  4. %success = TamlWrite(AssetBrowser_editAsset.editedAsset, %file);
  5. AssetBrowser.reloadAsset(%this.editedAssetId);
  6. AssetBrowser.refresh();
  7. %assetType = AssetDatabase.getAssetType(%this.editedAssetId);
  8. %assetDef = AssetDatabase.acquireAsset(%this.editedAssetId);
  9. %assetDef.refreshAsset();
  10. AssetBrowser.call("on" @ %assetType @ "Changed", %assetDef);
  11. AssetDatabase.releaseAsset(%this.editedAssetId);
  12. Canvas.popDialog(AssetBrowser_editAsset);
  13. }
  14. function AssetBrowser::editAsset(%this, %assetDef)
  15. {
  16. //Find out what type it is
  17. //If the passed-in definition param is blank, then we're likely called via a popup
  18. if(%assetDef $= "")
  19. {
  20. if(AssetDatabase.isDeclaredAsset(EditAssetPopup.assetId))
  21. {
  22. %assetDef = AssetDatabase.acquireAsset(EditAssetPopup.assetId);
  23. }
  24. else
  25. {
  26. //if it's not a valid asset at all, then it's probably a folder
  27. %folder = strreplace(EditAssetPopup.assetId, ":", "/");
  28. if(isDirectory(%folder))
  29. {
  30. AssetBrowser.navigateTo(%folder);
  31. }
  32. else
  33. {
  34. %object = getToken(EditAssetPopup.assetId, ":", 1);
  35. if(isObject(%object))
  36. {
  37. if(%object.isMemberOfClass("SimDatablock"))
  38. {
  39. DatablockEditorPlugin.openDatablock( %object );
  40. }
  41. }
  42. }
  43. }
  44. }
  45. else if(AssetDatabase.isDeclaredAsset(%assetDef))
  46. {
  47. //Turns out we were passed an assetid, not an asset definition.
  48. //Grab the asset def from that
  49. %assetDef = AssetDatabase.acquireAsset(%assetDef);
  50. }
  51. if(%assetDef !$= "")
  52. {
  53. %assetType = %assetDef.getClassName();
  54. //Build out the edit command
  55. %buildCommand = %this @ ".edit" @ %assetType @ "(" @ %assetDef @ ");";
  56. eval(%buildCommand);
  57. }
  58. }
  59. function AssetBrowser::appendSubLevel(%this)
  60. {
  61. %assetDef = AssetDatabase.acquireAsset(EditAssetPopup.assetId);
  62. %assetType = %assetDef.getClassName();
  63. schedule( 1, 0, "EditorOpenSceneAppend", %assetDef);
  64. }
  65. function AssetBrowser::editAssetInfo(%this)
  66. {
  67. Canvas.pushDialog(AssetBrowser_editAsset);
  68. %assetDef = AssetDatabase.acquireAsset(EditAssetPopup.assetId);
  69. AssetBrowser.tempAsset = %assetDef.deepClone();
  70. AssetEditInspector.inspect(AssetBrowser.tempAsset);
  71. AssetBrowser_editAsset.editedAssetId = EditAssetPopup.assetId;
  72. AssetBrowser_editAsset.editedAsset = AssetBrowser.tempAsset;
  73. //remove some of the groups we don't need:
  74. for(%i=0; %i < AssetEditInspector.getCount(); %i++)
  75. {
  76. %caption = AssetEditInspector.getObject(%i).caption;
  77. if(%caption $= "Ungrouped" || %caption $= "Object" || %caption $= "Editing"
  78. || %caption $= "Persistence" || %caption $= "Dynamic Fields")
  79. {
  80. AssetEditInspector.remove(AssetEditInspector.getObject(%i));
  81. %i--;
  82. }
  83. }
  84. }
  85. //------------------------------------------------------------
  86. function AssetBrowser::reloadAsset(%this, %assetId)
  87. {
  88. %moduleName = getToken(%assetId, ":", 0);
  89. %moduleDef = ModuleDatabase.findModule(%moduleName);
  90. %assetName = getToken(%assetId, ":", 1);
  91. %assetFilePath = AssetDatabase.getAssetFilePath(%assetId);
  92. AssetDatabase.removeDeclaredAsset(%assetId);
  93. AssetDatabase.addDeclaredAsset(%moduleDef, %assetFilePath);
  94. }
  95. function AssetBrowser::refreshAsset(%this, %assetId)
  96. {
  97. if(%assetId $= "")
  98. {
  99. //if we have no passed-in asset ID, we're probably going through the popup menu, so get our edit popup id
  100. %assetId = EditAssetPopup.assetId;
  101. }
  102. AssetDatabase.refreshAsset(%assetId);
  103. %this.refresh();
  104. }
  105. //------------------------------------------------------------
  106. function AssetBrowser::regeneratePreviewImage(%this)
  107. {
  108. %assetDef = AssetDatabase.acquireAsset(EditAssetPopup.assetId);
  109. %dummyObj = new ScriptObject();
  110. %dummyObj.moduleName = AssetDatabase.getAssetModule(EditAssetPopup.assetId).moduleId;
  111. %dummyObj.assetName = AssetDatabase.getAssetName(EditAssetPopup.assetId);
  112. %regenCommand = "AssetBrowser.generate" @ EditAssetPopup.assetType @
  113. "PreviewImage(" @ %dummyObj @ ", true);";
  114. eval(%regenCommand);
  115. %dummyObj.delete();
  116. AssetDatabase.releaseAsset(EditAssetPopup.assetId);
  117. }
  118. //------------------------------------------------------------
  119. function AssetBrowser::renameAsset(%this)
  120. {
  121. //Find out what type it is
  122. //%assetDef = AssetDatabase.acquireAsset(EditAssetPopup.assetId);
  123. %curFirstResponder = AssetBrowser.getFirstResponder();
  124. if(%curFirstResponder != 0)
  125. %curFirstResponder.clearFirstResponder();
  126. if(EditFolderPopup.visible == false)
  127. {
  128. AssetBrowser.selectedAssetPreview-->AssetNameLabel.setActive(true);
  129. AssetBrowser.selectedAssetPreview-->AssetNameLabel.setFirstResponder();
  130. }
  131. }
  132. function AssetBrowser::performRenameAsset(%this, %originalAssetName, %newName)
  133. {
  134. //if the name is different to the asset's original name, rename it!
  135. if(%originalAssetName !$= %newName)
  136. {
  137. %moduleName = AssetBrowser.selectedModule;
  138. if(EditAssetPopup.assetType !$= "Folder")
  139. {
  140. if(%this.isMethod("rename" @ EditAssetPopup.assetType))
  141. {
  142. %oldAssetId = %moduleName @ ":" @ %originalAssetName;
  143. %assetDef = AssetDatabase.acquireAsset(%oldAssetId);
  144. //Do the rename command
  145. %buildCommand = %this @ ".rename" @ EditAssetPopup.assetType @ "(" @ %assetDef @ "," @ %newName @ ");";
  146. eval(%buildCommand);
  147. }
  148. else
  149. {
  150. //do generic action here
  151. %oldAssetId = %moduleName @ ":" @ %originalAssetName;
  152. %assetDef = AssetDatabase.acquireAsset(%oldAssetId);
  153. %assetFileName = fileName(%assetDef.getFileName());
  154. %assetFilePath = filePath(%assetDef.getFileName());
  155. %pattern = %assetFilePath @ "/" @ %assetFileName @ ".*";
  156. echo("Searching for matches of asset files following pattern: " @ %pattern);
  157. //get list of files that match the name in the same dir
  158. //do a rename on them
  159. for (%file = findFirstFileMultiExpr(%pattern); %file !$= ""; %file = findNextFileMultiExpr(%pattern))
  160. {
  161. if(%file !$= %assetFileName)
  162. renameAssetLooseFile(%file, %newName);
  163. }
  164. //update the assetDef
  165. replaceInFile(%assetDef.getFileName(), %originalAssetName, %newName);
  166. renameAssetFile(%assetDef, %newName);
  167. }
  168. }
  169. else
  170. {
  171. %buildCommand = %this @ ".renameFolder(\"" @ EditAssetPopup.assetId @ "\",\"" @ %newName @ "\");";
  172. eval(%buildCommand);
  173. }
  174. }
  175. //Make sure everything is refreshed
  176. %this.refresh();
  177. //Update the selection to immediately jump to the new asset
  178. AssetBrowser-->filterTree.clearSelection();
  179. %ModuleItem = AssetBrowser-->filterTree.findItemByName(%moduleName);
  180. // TODO is this correct?
  181. /*%assetType = %ModuleItem.getClassName();
  182. %assetTypeId = AssetBrowser-->filterTree.findChildItemByName(%ModuleItem, %assetType);
  183. AssetBrowser-->filterTree.selectItem(%assetTypeId);
  184. %selectedItem = AssetBrowser-->filterTree.getSelectedItem();
  185. AssetBrowser-->filterTree.scrollVisibleByObjectId(%selectedItem);
  186. AssetBrowser-->filterTree.buildVisibleTree();*/
  187. }
  188. function renameAssetFile(%assetDef, %newName)
  189. {
  190. %assetId = %assetDef.getAssetID();
  191. %module = AssetDatabase.getAssetModule(%assetId);
  192. %moduleId = %module.moduleId;
  193. %assetPath = AssetDatabase.getAssetFilePath(%assetId);
  194. %newPath = filePath(%assetPath) @ "/" @ %newName @ ".asset.taml";
  195. %copiedSuccess = pathCopy(%assetPath, %newPath);
  196. if(!%copiedSuccess)
  197. return "";
  198. %deleteSuccess = fileDelete(%assetPath);
  199. if(!%deleteSuccess)
  200. return "";
  201. //Remove the old declaration
  202. AssetDatabase.removeDeclaredAsset(%assetId);
  203. //Add with the new file
  204. AssetDatabase.addDeclaredAsset(%module, %newPath);
  205. //Perform the rename in the file/system itself
  206. AssetDatabase.renameDeclaredAsset(%assetId, %moduleId @ ":" @ %newName);
  207. }
  208. function renameAssetLooseFile(%file, %newName)
  209. {
  210. %newPath = filePath(%file) @ "/" @ %newName @ fileExt(%file);
  211. %copiedSuccess = pathCopy(%file, %newPath);
  212. if(!%copiedSuccess)
  213. return "";
  214. %deleteSuccess = fileDelete(%file);
  215. if(!%deleteSuccess)
  216. return "";
  217. return fileName(%newPath);
  218. }
  219. function AssetNameField::onReturn(%this)
  220. {
  221. %this.clearFirstResponder();
  222. %this.setActive(false);
  223. AssetBrowser.performRenameAsset(%this.originalAssetName, %this.getText());
  224. }
  225. //------------------------------------------------------------
  226. function AssetBrowser::moveAsset(%this, %assetId, %destination)
  227. {
  228. if(EditAssetPopup.assetType $= "Folder")
  229. {
  230. //Do any cleanup required given the type
  231. if(%this.isMethod("moveFolder"))
  232. eval(%this @ ".moveFolder("@%assetId@",\""@%destination@"\");");
  233. }
  234. else
  235. {
  236. %assetDef = AssetDatabase.acquireAsset(%assetId);
  237. %assetType = AssetDatabase.getAssetType(%assetId);
  238. //Do any cleanup required given the type
  239. if(%this.isMethod("move"@%assetType))
  240. {
  241. %command = %this @ ".move" @ %assetType @ "(" @ %assetDef @ ",\"" @ %destination @ "\");";
  242. eval(%this @ ".move" @ %assetType @ "(" @ %assetDef @ ",\"" @ %destination @ "\");");
  243. }
  244. }
  245. %this.refresh();
  246. }
  247. function moveAssetFile(%assetDef, %destinationPath)
  248. {
  249. %assetPath = makeFullPath(AssetDatabase.getAssetFilePath(%assetDef.getAssetId()));
  250. %assetFilename = fileName(%assetPath);
  251. %newAssetPath = %destinationPath @ "/" @ %assetFilename;
  252. %copiedSuccess = pathCopy(%assetPath, %destinationPath @ "/" @ %assetFilename);
  253. if(!%copiedSuccess)
  254. return "";
  255. %deleteSuccess = fileDelete(%assetPath);
  256. if(!%deleteSuccess)
  257. return "";
  258. return %newAssetPath;
  259. }
  260. function moveAssetLooseFile(%file, %destinationPath)
  261. {
  262. %filename = fileName(%file);
  263. %copiedSuccess = pathCopy(%file, %destinationPath @ "/" @ %filename);
  264. if(!%copiedSuccess)
  265. return false;
  266. %deleteSuccess = fileDelete(%file);
  267. return %deleteSuccess;
  268. }
  269. //------------------------------------------------------------
  270. function AssetBrowser::duplicateAsset(%this)
  271. {
  272. %assetDef = AssetDatabase.acquireAsset(EditAssetPopup.assetId);
  273. %assetType = AssetDatabase.getAssetType(EditAssetPopup.assetId);
  274. %trailingNum = getTrailingNumber(%assetDef.assetName);
  275. if(%trailingNum != -1)
  276. {
  277. %trailingNum++;
  278. %newName = stripTrailingNumber(%assetDef.assetName) @ (%trailingNum);
  279. }
  280. else
  281. {
  282. %newName = stripTrailingNumber(%assetDef.assetName) @ "1";
  283. }
  284. AssetBrowser_assetNameEditTxt.text = %newName;
  285. AssetBrowser_assetNameEdit.callback = "AssetBrowser.doDuplicateAsset();";
  286. if(EditorSettings.value("AssetManagement/Assets/promptOnRename", "1") == 1)
  287. Canvas.pushDialog(AssetBrowser_assetNameEdit);
  288. else
  289. eval(AssetBrowser_assetNameEdit.callback);
  290. }
  291. function AssetBrowser::doDuplicateAsset(%this)
  292. {
  293. %assetDef = AssetDatabase.acquireAsset(EditAssetPopup.assetId);
  294. %assetType = AssetDatabase.getAssetType(EditAssetPopup.assetId);
  295. if(AssetBrowser_assetNameEditTxt.text !$= "" && AssetBrowser_assetNameEditTxt.text !$= %assetDef.assetName)
  296. {
  297. //this acts as a redirect based on asset type and will enact the appropriate function
  298. //so for a GameObjectAsset, it'll become %this.duplicateGameObjectAsset(%assetDef, %targetModule);
  299. //and call to the tools/assetBrowser/scripts/assetTypes/gameObject.tscript file for implementation
  300. if(%this.isMethod("duplicate"@%assetType))
  301. eval(%this @ ".duplicate"@%assetType@"("@%assetDef@","@AssetBrowser_assetNameEditTxt.text@");");
  302. AssetBrowser.refresh();
  303. }
  304. }
  305. function duplicateAssetFile(%assetDef, %newAssetName)
  306. {
  307. %assetPath = makeFullPath(AssetDatabase.getAssetFilePath(%assetDef.getAssetId()));
  308. %assetFilepath = filePath(%assetPath);
  309. %assetFileExt = fileExt(%assetPath);
  310. %newAssetPath = %assetFilepath @ "/" @ %newAssetName @ ".asset.taml";
  311. %copiedSuccess = pathCopy(%assetPath, %newAssetPath);
  312. if(!%copiedSuccess)
  313. return "";
  314. replaceInFile(%newAssetPath, %assetDef.assetName, %newAssetName);
  315. %module = AssetBrowser.dirHandler.getModuleFromAddress(%newAssetPath);
  316. //Add with the new file
  317. AssetDatabase.addDeclaredAsset(%module, %newAssetPath);
  318. return %newAssetPath;
  319. }
  320. function duplicateAssetLooseFile(%file, %newFilename)
  321. {
  322. %filePath = filePath(%file);
  323. %fileExt = fileExt(%file);
  324. %newPath = %filePath @ "/" @ %newFilename @ %fileExt;
  325. %copiedSuccess = pathCopy(%file, %newPath);
  326. if(!%copiedSuccess)
  327. return "";
  328. return %newPath;
  329. }
  330. //------------------------------------------------------------
  331. function AssetBrowser::deleteAsset(%this)
  332. {
  333. //Find out what type it is
  334. //%assetDef = AssetDatabase.acquireAsset(EditAssetPopup.assetId);
  335. //%assetType = %assetDef.getClassName();
  336. toolsMessageBoxOKCancel("Warning!", "This will delete the selected content and the files associated to it, do you wish to continue?",
  337. "AssetBrowser.confirmDeleteAsset();", "");
  338. }
  339. function AssetBrowser::confirmDeleteAsset(%this)
  340. {
  341. %currentSelectedItem = AssetBrowserFilterTree.getSelectedItem();
  342. %currentItemParent = AssetBrowserFilterTree.getParentItem(%currentSelectedItem);
  343. if(EditFolderPopup.visible)
  344. {
  345. if(EditFolderPopup.dirPath !$= "")
  346. %folderPath = EditFolderPopup.dirPath;
  347. else
  348. %folderPath = AssetBrowserFilterTree.getItemValue(%currentSelectedItem) @ "/" @ AssetBrowserFilterTree.getItemText(%currentSelectedItem);
  349. if(%this.isMethod("deleteFolder"))
  350. eval(%this @ ".deleteFolder(\""@%folderPath@"\");");
  351. }
  352. else
  353. {
  354. %assetDef = AssetDatabase.acquireAsset(EditAssetPopup.assetId);
  355. %assetType = AssetDatabase.getAssetType(EditAssetPopup.assetType);
  356. if(!isObject(%assetDef))
  357. return;
  358. //Do any cleanup required given the type
  359. if(%this.isMethod("delete"@%assetType))
  360. eval(%this @ ".delete"@%assetType@"("@%assetDef@");");
  361. AssetDatabase.deleteAsset(EditAssetPopup.assetId, true, false);
  362. }
  363. %this.refresh();
  364. }
  365. //------------------------------------------------------------
  366. function AssetBrowser::updateAssetReference(%this, %targetPath, %oldAssetId, %newAssetId)
  367. {
  368. //assetbrowser.updateAssetReference("data/pbr/levels", "pbr:material_ball", "TreeTest:TestTree");
  369. //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
  370. //This is potentially disastrous and break a lot of things if done hapazardly, so be careful
  371. %fullPath = makeFullPath(%targetPath);
  372. //First, wipe out any files inside the folder first
  373. %file = findFirstFileMultiExpr( %fullPath @ "/*.*", true);
  374. %fileObj = new FileObject();
  375. new ArrayObject(lineCache);
  376. while( %file !$= "" )
  377. {
  378. lineCache.empty();
  379. %fileModified = false;
  380. if(%fileObj.openForRead(%file))
  381. {
  382. while( !%fileObj.isEOF() )
  383. {
  384. %unModLine = %fileObj.readLine();
  385. %line = strreplace( %unModLine, %oldAssetId, %newAssetId );
  386. if(%unModLine !$= %line)
  387. %fileModified = true;
  388. lineCache.add(%line);
  389. }
  390. }
  391. if(%fileModified && %fileObj.openForWrite(%file))
  392. {
  393. for(%i=0; %i<lineCache.count(); %i++)
  394. {
  395. %line = lineCache.getKey(%i);
  396. %fileObj.writeline(%line);
  397. }
  398. %fileObj.close();
  399. }
  400. %file = findNextFileMultiExpr( %fullPath @ "/*.*" );
  401. }
  402. lineCache.delete();
  403. %fileObj.delete();
  404. }
  405. function AssetBrowser::openFileLocation(%this)
  406. {
  407. %filePath = "";
  408. if(EditAssetPopup.assetId !$= "")
  409. {
  410. if(AssetDatabase.isDeclaredAsset(EditAssetPopup.assetId))
  411. {
  412. %filePath = AssetDatabase.getAssetPath(EditAssetPopup.assetId);
  413. }
  414. else
  415. {
  416. //probably a file path
  417. %pathSplit = strpos(EditAssetPopup.assetId, ":");
  418. if(%pathSplit != -1)
  419. {
  420. %path = getSubStr(EditAssetPopup.assetId, 0, %pathSplit);
  421. %file = getSubStr(EditAssetPopup.assetId, %pathSplit + 1);
  422. //datablocks pack the originator file in the parent path as-is, so check that
  423. if(fileExt(%path) !$= "")
  424. {
  425. %filePath = %path;
  426. }
  427. else
  428. {
  429. %filePath = %path @ "/" @ %file;
  430. }
  431. }
  432. }
  433. }
  434. else if(EditLevelAssetPopup.assetId !$= "")
  435. {
  436. %filePath = AssetDatabase.getAssetPath(EditAssetPopup.assetId);
  437. }
  438. else if(EditTerrainAssetPopup.assetId !$= "")
  439. {
  440. %filePath = AssetDatabase.getAssetPath(EditAssetPopup.assetId);
  441. }
  442. if(isFile(%filePath) || isDirectory(%filePath))
  443. {
  444. %fullPath = makeFullPath(%filePath);
  445. if(fileExt(%fullPath) $= ".tscript")
  446. %fullPath = filePath(%fullPath);
  447. if($platform $= "windows")
  448. {
  449. %cmd = "cd \"" @ %fullPath @ "\" && start .";
  450. systemCommand(%cmd);
  451. }
  452. else
  453. {
  454. %cmd = "open \"" @ %fullPath @ "\"";
  455. systemCommand(%cmd);
  456. }
  457. }
  458. }
  459. function AssetBrowser::openFolderLocation(%this)
  460. {
  461. %filePath = AssetBrowser.dirHandler.currentAddress;
  462. if(%filePath !$= "")
  463. {
  464. if($platform $= "windows")
  465. {
  466. %cmd = "cd \"" @ makeFullPath(%filePath) @ "\" && start .";
  467. systemCommand(%cmd);
  468. }
  469. else
  470. {
  471. %cmd = "open \"" @ makeFullPath(%filePath) @ "\"";
  472. systemCommand(%cmd);
  473. }
  474. }
  475. }