editAsset.tscript 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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. AssetBrowser.call("on" @ %assetType @ "Changed", %assetDef);
  10. AssetDatabase.releaseAsset(%this.editedAssetId);
  11. Canvas.popDialog(AssetBrowser_editAsset);
  12. }
  13. function AssetBrowser::editAsset(%this, %assetDef)
  14. {
  15. //Find out what type it is
  16. //If the passed-in definition param is blank, then we're likely called via a popup
  17. if(%assetDef $= "")
  18. {
  19. if(AssetDatabase.isDeclaredAsset(EditAssetPopup.assetId))
  20. {
  21. %assetDef = AssetDatabase.acquireAsset(EditAssetPopup.assetId);
  22. }
  23. else
  24. {
  25. //if it's not a valid asset at all, then it's probably a folder
  26. %folder = strreplace(EditAssetPopup.assetId, ":", "/");
  27. if(isDirectory(%folder))
  28. {
  29. AssetBrowser.navigateTo(%folder);
  30. }
  31. else
  32. {
  33. %object = getToken(EditAssetPopup.assetId, ":", 1);
  34. if(isObject(%object))
  35. {
  36. if(%object.isMemberOfClass("SimDatablock"))
  37. {
  38. DatablockEditorPlugin.openDatablock( %object );
  39. }
  40. }
  41. }
  42. }
  43. }
  44. else if(AssetDatabase.isDeclaredAsset(%assetDef))
  45. {
  46. //Turns out we were passed an assetid, not an asset definition.
  47. //Grab the asset def from that
  48. %assetDef = AssetDatabase.acquireAsset(%assetDef);
  49. }
  50. if(%assetDef !$= "")
  51. {
  52. %assetType = %assetDef.getClassName();
  53. //Build out the edit command
  54. %buildCommand = %this @ ".edit" @ %assetType @ "(" @ %assetDef @ ");";
  55. eval(%buildCommand);
  56. }
  57. }
  58. function AssetBrowser::appendSubLevel(%this)
  59. {
  60. %assetDef = AssetDatabase.acquireAsset(EditAssetPopup.assetId);
  61. %assetType = %assetDef.getClassName();
  62. schedule( 1, 0, "EditorOpenSceneAppend", %assetDef);
  63. }
  64. function AssetBrowser::editAssetInfo(%this)
  65. {
  66. Canvas.pushDialog(AssetBrowser_editAsset);
  67. %assetDef = AssetDatabase.acquireAsset(EditAssetPopup.assetId);
  68. eval("AssetBrowser.tempAsset = new " @ %assetDef.getClassName() @ "();");
  69. AssetBrowser.tempAsset.assignFieldsFrom(%assetDef);
  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. }
  149. else
  150. {
  151. %buildCommand = %this @ ".renameFolder(\"" @ EditAssetPopup.assetId @ "\",\"" @ %newName @ "\");";
  152. eval(%buildCommand);
  153. }
  154. }
  155. //Make sure everything is refreshed
  156. %this.refresh();
  157. //Update the selection to immediately jump to the new asset
  158. AssetBrowser-->filterTree.clearSelection();
  159. %ModuleItem = AssetBrowser-->filterTree.findItemByName(%moduleName);
  160. // TODO is this correct?
  161. /*%assetType = %ModuleItem.getClassName();
  162. %assetTypeId = AssetBrowser-->filterTree.findChildItemByName(%ModuleItem, %assetType);
  163. AssetBrowser-->filterTree.selectItem(%assetTypeId);
  164. %selectedItem = AssetBrowser-->filterTree.getSelectedItem();
  165. AssetBrowser-->filterTree.scrollVisibleByObjectId(%selectedItem);
  166. AssetBrowser-->filterTree.buildVisibleTree();*/
  167. }
  168. function renameAssetFile(%assetDef, %newName)
  169. {
  170. %assetId = %assetDef.getAssetID();
  171. %module = AssetDatabase.getAssetModule(%assetId);
  172. %moduleId = %module.moduleId;
  173. %assetPath = AssetDatabase.getAssetFilePath(%assetId);
  174. %newPath = filePath(%assetPath) @ "/" @ %newName @ ".asset.taml";
  175. %copiedSuccess = pathCopy(%assetPath, %newPath);
  176. if(!%copiedSuccess)
  177. return "";
  178. %deleteSuccess = fileDelete(%assetPath);
  179. if(!%deleteSuccess)
  180. return "";
  181. //Remove the old declaration
  182. AssetDatabase.removeDeclaredAsset(%assetId);
  183. //Add with the new file
  184. AssetDatabase.addDeclaredAsset(%module, %newPath);
  185. //Perform the rename in the file/system itself
  186. AssetDatabase.renameDeclaredAsset(%assetId, %moduleId @ ":" @ %newName);
  187. }
  188. function renameAssetLooseFile(%file, %newName)
  189. {
  190. %newPath = filePath(%file) @ "/" @ %newName @ fileExt(%file);
  191. %copiedSuccess = pathCopy(%file, %newPath);
  192. if(!%copiedSuccess)
  193. return "";
  194. %deleteSuccess = fileDelete(%file);
  195. if(!%deleteSuccess)
  196. return "";
  197. return fileName(%newPath);
  198. }
  199. function AssetNameField::onReturn(%this)
  200. {
  201. %this.clearFirstResponder();
  202. %this.setActive(false);
  203. AssetBrowser.performRenameAsset(%this.originalAssetName, %this.getText());
  204. }
  205. //------------------------------------------------------------
  206. function AssetBrowser::moveAsset(%this, %assetId, %destination)
  207. {
  208. if(EditAssetPopup.assetType $= "Folder")
  209. {
  210. //Do any cleanup required given the type
  211. if(%this.isMethod("moveFolder"))
  212. eval(%this @ ".moveFolder("@%assetId@",\""@%destination@"\");");
  213. }
  214. else
  215. {
  216. %assetDef = AssetDatabase.acquireAsset(%assetId);
  217. %assetType = AssetDatabase.getAssetType(%assetId);
  218. //Do any cleanup required given the type
  219. if(%this.isMethod("move"@%assetType))
  220. {
  221. %command = %this @ ".move" @ %assetType @ "(" @ %assetDef @ ",\"" @ %destination @ "\");";
  222. eval(%this @ ".move" @ %assetType @ "(" @ %assetDef @ ",\"" @ %destination @ "\");");
  223. }
  224. }
  225. %this.refresh();
  226. }
  227. function moveAssetFile(%assetDef, %destinationPath)
  228. {
  229. %assetPath = makeFullPath(AssetDatabase.getAssetFilePath(%assetDef.getAssetId()));
  230. %assetFilename = fileName(%assetPath);
  231. %newAssetPath = %destinationPath @ "/" @ %assetFilename;
  232. %copiedSuccess = pathCopy(%assetPath, %destinationPath @ "/" @ %assetFilename);
  233. if(!%copiedSuccess)
  234. return "";
  235. %deleteSuccess = fileDelete(%assetPath);
  236. if(!%deleteSuccess)
  237. return "";
  238. return %newAssetPath;
  239. }
  240. function moveAssetLooseFile(%file, %destinationPath)
  241. {
  242. %filename = fileName(%file);
  243. %copiedSuccess = pathCopy(%file, %destinationPath @ "/" @ %filename);
  244. if(!%copiedSuccess)
  245. return false;
  246. %deleteSuccess = fileDelete(%file);
  247. return %deleteSuccess;
  248. }
  249. //------------------------------------------------------------
  250. function AssetBrowser::duplicateAsset(%this)
  251. {
  252. %assetDef = AssetDatabase.acquireAsset(EditAssetPopup.assetId);
  253. %assetType = AssetDatabase.getAssetType(EditAssetPopup.assetId);
  254. %trailingNum = getTrailingNumber(%assetDef.assetName);
  255. if(%trailingNum != -1)
  256. {
  257. %trailingNum++;
  258. %newName = stripTrailingNumber(%assetDef.assetName) @ (%trailingNum);
  259. }
  260. else
  261. {
  262. %newName = stripTrailingNumber(%assetDef.assetName) @ "1";
  263. }
  264. AssetBrowser_assetNameEditTxt.text = %newName;
  265. AssetBrowser_assetNameEdit.callback = "AssetBrowser.doDuplicateAsset();";
  266. if(EditorSettings.value("AssetManagement/Assets/promptOnRename", "1") == 1)
  267. Canvas.pushDialog(AssetBrowser_assetNameEdit);
  268. else
  269. eval(AssetBrowser_assetNameEdit.callback);
  270. }
  271. function AssetBrowser::doDuplicateAsset(%this)
  272. {
  273. %assetDef = AssetDatabase.acquireAsset(EditAssetPopup.assetId);
  274. %assetType = AssetDatabase.getAssetType(EditAssetPopup.assetId);
  275. if(AssetBrowser_assetNameEditTxt.text !$= "" && AssetBrowser_assetNameEditTxt.text !$= %assetDef.assetName)
  276. {
  277. //this acts as a redirect based on asset type and will enact the appropriate function
  278. //so for a GameObjectAsset, it'll become %this.duplicateGameObjectAsset(%assetDef, %targetModule);
  279. //and call to the tools/assetBrowser/scripts/assetTypes/gameObject.tscript file for implementation
  280. if(%this.isMethod("duplicate"@%assetType))
  281. eval(%this @ ".duplicate"@%assetType@"("@%assetDef@","@AssetBrowser_assetNameEditTxt.text@");");
  282. AssetBrowser.refresh();
  283. }
  284. }
  285. function duplicateAssetFile(%assetDef, %newAssetName)
  286. {
  287. %assetPath = makeFullPath(AssetDatabase.getAssetFilePath(%assetDef.getAssetId()));
  288. %assetFilepath = filePath(%assetPath);
  289. %assetFileExt = fileExt(%assetPath);
  290. %newAssetPath = %assetFilepath @ "/" @ %newAssetName @ ".asset.taml";
  291. %copiedSuccess = pathCopy(%assetPath, %newAssetPath);
  292. if(!%copiedSuccess)
  293. return "";
  294. replaceInFile(%newAssetPath, %assetDef.assetName, %newAssetName);
  295. %module = AssetBrowser.dirHandler.getModuleFromAddress(%newAssetPath);
  296. //Add with the new file
  297. AssetDatabase.addDeclaredAsset(%module, %newAssetPath);
  298. return %newAssetPath;
  299. }
  300. function duplicateAssetLooseFile(%file, %newFilename)
  301. {
  302. %filePath = filePath(%file);
  303. %fileExt = fileExt(%file);
  304. %newPath = %filePath @ "/" @ %newFilename @ %fileExt;
  305. %copiedSuccess = pathCopy(%file, %newPath);
  306. if(!%copiedSuccess)
  307. return "";
  308. return %newPath;
  309. }
  310. //------------------------------------------------------------
  311. function AssetBrowser::deleteAsset(%this)
  312. {
  313. //Find out what type it is
  314. //%assetDef = AssetDatabase.acquireAsset(EditAssetPopup.assetId);
  315. //%assetType = %assetDef.getClassName();
  316. toolsMessageBoxOKCancel("Warning!", "This will delete the selected content and the files associated to it, do you wish to continue?",
  317. "AssetBrowser.confirmDeleteAsset();", "");
  318. }
  319. function AssetBrowser::confirmDeleteAsset(%this)
  320. {
  321. %currentSelectedItem = AssetBrowserFilterTree.getSelectedItem();
  322. %currentItemParent = AssetBrowserFilterTree.getParentItem(%currentSelectedItem);
  323. if(EditFolderPopup.visible)
  324. {
  325. if(EditFolderPopup.dirPath !$= "")
  326. %folderPath = EditFolderPopup.dirPath;
  327. else
  328. %folderPath = AssetBrowserFilterTree.getItemValue(%currentSelectedItem) @ "/" @ AssetBrowserFilterTree.getItemText(%currentSelectedItem);
  329. if(%this.isMethod("deleteFolder"))
  330. eval(%this @ ".deleteFolder(\""@%folderPath@"\");");
  331. }
  332. else
  333. {
  334. %assetDef = AssetDatabase.acquireAsset(EditAssetPopup.assetId);
  335. %assetType = AssetDatabase.getAssetType(EditAssetPopup.assetType);
  336. if(!isObject(%assetDef))
  337. return;
  338. //Do any cleanup required given the type
  339. if(%this.isMethod("delete"@%assetType))
  340. eval(%this @ ".delete"@%assetType@"("@%assetDef@");");
  341. AssetDatabase.deleteAsset(EditAssetPopup.assetId, true, false);
  342. }
  343. %this.refresh();
  344. }
  345. //------------------------------------------------------------
  346. function AssetBrowser::updateAssetReference(%this, %targetPath, %oldAssetId, %newAssetId)
  347. {
  348. //assetbrowser.updateAssetReference("data/pbr/levels", "pbr:material_ball", "TreeTest:TestTree");
  349. //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
  350. //This is potentially disastrous and break a lot of things if done hapazardly, so be careful
  351. %fullPath = makeFullPath(%targetPath);
  352. //First, wipe out any files inside the folder first
  353. %file = findFirstFileMultiExpr( %fullPath @ "/*.*", true);
  354. %fileObj = new FileObject();
  355. new ArrayObject(lineCache);
  356. while( %file !$= "" )
  357. {
  358. lineCache.empty();
  359. %fileModified = false;
  360. if(%fileObj.openForRead(%file))
  361. {
  362. while( !%fileObj.isEOF() )
  363. {
  364. %unModLine = %fileObj.readLine();
  365. %line = strreplace( %unModLine, %oldAssetId, %newAssetId );
  366. if(%unModLine !$= %line)
  367. %fileModified = true;
  368. lineCache.add(%line);
  369. }
  370. }
  371. if(%fileModified && %fileObj.openForWrite(%file))
  372. {
  373. for(%i=0; %i<lineCache.count(); %i++)
  374. {
  375. %line = lineCache.getKey(%i);
  376. %fileObj.writeline(%line);
  377. }
  378. %fileObj.close();
  379. }
  380. %file = findNextFileMultiExpr( %fullPath @ "/*.*" );
  381. }
  382. lineCache.delete();
  383. %fileObj.delete();
  384. }
  385. function AssetBrowser::openFileLocation(%this)
  386. {
  387. %filePath = "";
  388. if(EditAssetPopup.assetId !$= "")
  389. {
  390. if(AssetDatabase.isDeclaredAsset(EditAssetPopup.assetId))
  391. {
  392. %filePath = AssetDatabase.getAssetPath(EditAssetPopup.assetId);
  393. }
  394. else
  395. {
  396. //probably a file path
  397. %pathSplit = strpos(EditAssetPopup.assetId, ":");
  398. if(%pathSplit != -1)
  399. {
  400. %path = getSubStr(EditAssetPopup.assetId, 0, %pathSplit);
  401. %file = getSubStr(EditAssetPopup.assetId, %pathSplit + 1);
  402. //datablocks pack the originator file in the parent path as-is, so check that
  403. if(fileExt(%path) !$= "")
  404. {
  405. %filePath = %path;
  406. }
  407. else
  408. {
  409. %filePath = %path @ "/" @ %file;
  410. }
  411. }
  412. }
  413. }
  414. else if(EditLevelAssetPopup.assetId !$= "")
  415. {
  416. %filePath = AssetDatabase.getAssetPath(EditAssetPopup.assetId);
  417. }
  418. else if(EditTerrainAssetPopup.assetId !$= "")
  419. {
  420. %filePath = AssetDatabase.getAssetPath(EditAssetPopup.assetId);
  421. }
  422. if(isFile(%filePath) || isDirectory(%filePath))
  423. {
  424. %fullPath = makeFullPath(%filePath);
  425. if(fileExt(%fullPath) $= ".tscript")
  426. %fullPath = filePath(%fullPath);
  427. if($platform $= "windows")
  428. {
  429. %cmd = "cd \"" @ %fullPath @ "\" && start .";
  430. systemCommand(%cmd);
  431. }
  432. else
  433. {
  434. %cmd = "open \"" @ %fullPath @ "\"";
  435. systemCommand(%cmd);
  436. }
  437. }
  438. }
  439. function AssetBrowser::openFolderLocation(%this)
  440. {
  441. %filePath = AssetBrowser.dirHandler.currentAddress;
  442. if(%filePath !$= "")
  443. {
  444. if($platform $= "windows")
  445. {
  446. %cmd = "cd \"" @ makeFullPath(%filePath) @ "\" && start .";
  447. systemCommand(%cmd);
  448. }
  449. else
  450. {
  451. %cmd = "open \"" @ makeFullPath(%filePath) @ "\"";
  452. systemCommand(%cmd);
  453. }
  454. }
  455. }