newAsset.tscript 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. function CreateAssetButton::onClick(%this)
  23. {
  24. AddNewAssetPopup.showPopup(Canvas);
  25. }
  26. function AssetBrowser_newAsset::onWake(%this)
  27. {
  28. %targetModule = AssetBrowser.dirHandler.getModuleFromAddress(AssetBrowser.dirHandler.currentAddress).ModuleId;
  29. if(%targetModule $= "")
  30. %targetModule = EditorSettings.value("Assets/New/defaultModule", "");
  31. NewAssetTargetModule.setText(%targetModule);
  32. %moduleDef = ModuleDatabase.findModule(%targetModule);
  33. %targetPath = AssetBrowser.dirHandler.currentAddress;
  34. if(!startsWith(%targetPath, %moduleDef.ModulePath))
  35. {
  36. %targetPath = %moduleDef.ModulePath;
  37. }
  38. NewAssetTargetAddress.text = %targetPath;
  39. }
  40. function AssetBrowser_newAssetWindow::onClose(%this)
  41. {
  42. NewAssetPropertiesInspector.clearFields();
  43. Canvas.popDialog(AssetBrowser_newAsset);
  44. }
  45. function NewAssetTypeList::onWake(%this)
  46. {
  47. %this.refresh();
  48. }
  49. function NewAssetTypeList::refresh(%this)
  50. {
  51. %this.clear();
  52. //TODO: make this more automated
  53. //%this.add("GameObject", 0);
  54. %this.add("ComponentAsset", 0);
  55. %this.add("ImageAsset", 1);
  56. %this.add("MaterialAsset", 2);
  57. %this.add("ShapeAsset", 3);
  58. %this.add("SoundAsset", 4);
  59. %this.add("StateMachineAsset", 5);
  60. }
  61. function NewAssetTypeList::onSelected(%this)
  62. {
  63. %assetType = %this.getText();
  64. if(%assetType $= "ComponentAsset")
  65. {
  66. NewComponentAssetSettings.hidden = false;
  67. }
  68. }
  69. function NewAssetModuleBtn::onClick(%this)
  70. {
  71. Canvas.pushDialog(AssetBrowser_AddModule);
  72. AssetBrowser_addModuleWindow.selectWindow();
  73. }
  74. function AssetBrowser::setupCreateNewAsset(%this, %assetType, %moduleName, %callback, %nameOverride)
  75. {
  76. Canvas.pushDialog(AssetBrowser_newAsset);
  77. %shortAssetTypeName = strreplace(%assetType, "Asset", "");
  78. AssetBrowser_newAssetWindow.text = "New" SPC %shortAssetTypeName SPC "Asset";
  79. NewAssetPropertiesInspector.clearFields();
  80. AssetBrowser_newAsset.callbackFunc = %callback;
  81. //get rid of the old one if we had one.
  82. if(isObject(%this.newAssetSettings))
  83. %this.newAssetSettings.delete();
  84. %this.newAssetSettings = new ScriptObject();
  85. %this.newAssetSettings.assetType = %assetType;
  86. %this.newAssetSettings.moduleName = %moduleName;
  87. %newAssetName = "New" @ %shortAssetTypeName;
  88. if(%nameOverride !$= "")
  89. %newAssetName = %nameOverride;
  90. NewAssetPropertiesInspector.startGroup("General");
  91. NewAssetPropertiesInspector.addField("assetName", "New Asset Name", "String", "Name of the new asset", %newAssetName, "", %this.newAssetSettings);
  92. //NewAssetPropertiesInspector.addField("AssetType", "New Asset Type", "List", "Type of the new asset", %assetType, "Component,Image,Material,Shape,Sound,State Machine", %newAssetSettings);
  93. //NewAssetPropertiesInspector.addField("friendlyName", "Friendly Name", "String", "Human-readable name of new asset", "", "", %this.newAssetSettings);
  94. NewAssetPropertiesInspector.addCallbackField("description", "Description", "Command", "Description of the new asset", "", "", "updateNewAssetField", %this.newAssetSettings);
  95. NewAssetPropertiesInspector.endGroup();
  96. if(%this.isMethod("setupCreateNew"@%assetType))
  97. {
  98. %command = %this @ ".setupCreateNew"@%assetType @"();";
  99. eval(%command);
  100. }
  101. NewAssetPropertiesInspector.refresh();
  102. }
  103. function NewAssetPropertiesInspector::updateNewAssetField(%this)
  104. {
  105. %this.schedule(32, "update");
  106. }
  107. function newAssetUpdatePath(%newPath)
  108. {
  109. AssetBrowser.navigateTo(%newPath);
  110. NewAssetTargetAddress.text = %newPath;
  111. NewAssetTargetModule.text = AssetBrowser.dirHandler.getModuleFromAddress(AssetBrowser.dirHandler.currentAddress).ModuleId;
  112. }
  113. //
  114. function AssetBrowser::importNewFile(%this)
  115. {
  116. %importingPath = "";
  117. %dlg = new OpenFileDialog()
  118. {
  119. Filters = "(All Files (*.*)|*.*|";
  120. DefaultFile = "";
  121. ChangePath = false;
  122. MustExist = true;
  123. MultipleFiles = false;
  124. forceRelativePath = false;
  125. };
  126. if ( %dlg.Execute() )
  127. {
  128. %importingPath = makeFullPath(%dlg.FileName);
  129. }
  130. %dlg.delete();
  131. AssetBrowser.onBeginDropFiles();
  132. AssetBrowser.onDropFile(%importingPath);
  133. AssetBrowser.onEndDropFiles();
  134. }
  135. //
  136. function NewAssetTargetModule::onSelect(%this, %idx, %idy)
  137. {
  138. %newModuleName = %this.getText();
  139. %currentTargetPath = NewAssetTargetAddress.getText();
  140. if(!startsWith(%currentTargetPath, "data/" @ %newModuleName @ "/"))
  141. {
  142. NewAssetTargetAddress.setText("data/" @ %newModuleName @ "/");
  143. }
  144. }
  145. //
  146. //We do a quick validation that mandatory fields are filled in before passing along to the asset-type specific function
  147. function CreateNewAsset()
  148. {
  149. //To enusre that any in-progress-of-being-edited field applies it's changes
  150. %lastEditField = AssetBrowser_newAsset.getFirstResponder();
  151. if(isObject(%lastEditField) && %lastEditField.isMethod("forceValidateText"))
  152. %lastEditField.forceValidateText();
  153. %assetName = AssetBrowser.newAssetSettings.assetName;
  154. if(%assetName $= "")
  155. {
  156. toolsMessageBoxOK( "Error", "Attempted to make a new asset with no name!");
  157. return;
  158. }
  159. %firstChar = getSubStr(%assetName, 0, 1);
  160. if(isInt(%firstChar))
  161. {
  162. toolsMessageBoxOK( "Error", "Names cannot start with a number!");
  163. return;
  164. }
  165. //get the selected module data
  166. %moduleName = NewAssetTargetModule.getText();
  167. if(%moduleName $= "")
  168. {
  169. toolsMessageBoxOK( "Error", "Attempted to make a new asset with no module!");
  170. return;
  171. }
  172. //First, we need to make sure we're not creating a conflicting asset
  173. if(AssetDatabase.isDeclaredAsset(%moduleName @ ":" @ %assetName))
  174. {
  175. toolsMessageBoxOK( "Error", "An asset with the ID: " @ %moduleName @ ":" @ %assetName
  176. @ " already exists! Please review and rename.");
  177. return;
  178. }
  179. %assetType = AssetBrowser.newAssetSettings.assetType;
  180. if(%assetType $= "MaterialAsset" || %assetType $= "TerrainMaterialAsset" ||
  181. %assetType $= "GUIAsset")
  182. {
  183. if(isObject(%assetName))
  184. {
  185. toolsMessageBoxOK( "Error", "Attempted to create a new asset that requires " @
  186. "a unique name, as the object definition must be unique. " @
  187. "Please use a new name.");
  188. return;
  189. }
  190. }
  191. %currentTargetPath = NewAssetTargetAddress.getText();
  192. %modulePath = makeRelativePath(ModuleDatabase.findModule(%moduleName).ModulePath);
  193. if(!startsWith(%currentTargetPath, %modulePath))
  194. {
  195. toolsMessageBoxOK( "Error", "Attempting to create a new asset in an invalid path. " @
  196. "Please set the target path to be within the target module folder.");
  197. return;
  198. }
  199. AssetBrowser.newAssetSettings.moduleName = %moduleName;
  200. if(%assetType $= "")
  201. {
  202. toolsMessageBoxOK( "Error", "Attempted to make a new asset with no type!");
  203. return;
  204. }
  205. %assetFilePath = eval(AssetBrowser @ ".create"@%assetType@"();");
  206. Canvas.popDialog(AssetBrowser_newAsset);
  207. //Load it
  208. if(!AssetDatabase.isDeclaredAsset(%moduleName @ ":" @ %assetName))
  209. {
  210. %moduleDef = ModuleDatabase.findModule(%moduleName,1);
  211. AssetDatabase.addDeclaredAsset(%moduleDef, %assetFilePath);
  212. //For utilities' sake, we'll acquire it immediately so it can be utilized
  213. //without delay if it's got any script/dependencies stuff
  214. AssetDatabase.acquireAsset("\"" @ %moduleName @ ":" @ %assetName @ "\"");
  215. }
  216. if(AssetBrowser_newAsset.callbackFunc !$= "")
  217. {
  218. %callbackCommand = "" @ AssetBrowser_newAsset.callbackFunc @ "(\"" @ %moduleName @ ":" @ %assetName @ "\");";
  219. eval(%callbackCommand);
  220. }
  221. //Update the selection to immediately jump to the new asset
  222. /*AssetBrowser-->filterTree.clearSelection();
  223. %ModuleItem = AssetBrowser-->filterTree.findItemByName(%moduleName);
  224. %assetTypeId = AssetBrowser-->filterTree.findChildItemByName(%ModuleItem, %assetType);
  225. AssetBrowser-->filterTree.selectItem(%assetTypeId);
  226. %selectedItem = AssetBrowser-->filterTree.getSelectedItem();
  227. AssetBrowser-->filterTree.scrollVisibleByObjectId(%selectedItem);
  228. AssetBrowser-->filterTree.buildVisibleTree(); */
  229. AssetBrowser.refresh();
  230. }
  231. function ParentComponentList::onWake(%this)
  232. {
  233. %this.refresh();
  234. }
  235. function ParentComponentList::refresh(%this)
  236. {
  237. %this.clear();
  238. %assetQuery = new AssetQuery();
  239. if(!AssetDatabase.findAssetType(%assetQuery, "ComponentAsset"))
  240. return; //if we didn't find ANY, just exit
  241. // Find all the types.
  242. %count = %assetQuery.getCount();
  243. /*%categories = "";
  244. for (%i = 0; %i < %count; %i++)
  245. {
  246. %assetId = %assetQuery.getAsset(%i);
  247. %componentAsset = AssetDatabase.acquireAsset(%assetId);
  248. %componentName = %componentAsset.componentName;
  249. if(%componentName $= "")
  250. %componentName = %componentAsset.componentClass;
  251. %this.add(%componentName, %i);
  252. }*/
  253. %categories = "";
  254. for (%i = 0; %i < %count; %i++)
  255. {
  256. %assetId = %assetQuery.getAsset(%i);
  257. %componentAsset = AssetDatabase.acquireAsset(%assetId);
  258. %componentClass = %componentAsset.componentClass;
  259. if (!isInList(%componentClass, %categories))
  260. %categories = %categories TAB %componentClass;
  261. }
  262. %categories = trim(%categories);
  263. %index = 0;
  264. %categoryCount = getFieldCount(%categories);
  265. for (%i = 0; %i < %categoryCount; %i++)
  266. {
  267. %category = getField(%categories, %i);
  268. %this.addCategory(%category);
  269. for (%j = 0; %j < %count; %j++)
  270. {
  271. %assetId = %assetQuery.getAsset(%j);
  272. %componentAsset = AssetDatabase.acquireAsset(%assetId);
  273. %componentName = %componentAsset.componentName;
  274. %componentClass = %componentAsset.componentClass;
  275. if (%componentClass $= %category)
  276. {
  277. if(%componentName !$= "")
  278. %this.add(" "@%componentName, %i);
  279. }
  280. }
  281. }
  282. }
  283. //----------------------------------------------------------
  284. // Game Object creation
  285. //----------------------------------------------------------
  286. function EWorldEditor::createGameObject( %this )
  287. {
  288. AssetBrowser.createGameObjectAsset();
  289. }