NewParticleAssetDialog.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. function NewParticleAssetDialog::init(%this, %width, %height)
  2. {
  3. //Get the dialog contents
  4. %window = %this.getObject(0);
  5. %content = %window.getObject(0);
  6. //Create the file text box
  7. %form = new GuiGridCtrl()
  8. {
  9. class = "EditorForm";
  10. extent = %width SPC %height;
  11. cellSizeX = %width;
  12. cellSizeY = 50;
  13. };
  14. %form.addListener(%this);
  15. %item = %form.addFormItem("Image Asset", %width SPC 30);
  16. %this.imageDropDown = %form.createDropDownItem(%item);
  17. %this.populateImageDropDown();
  18. %item = %form.addFormItem("Animation Asset", %width SPC 30);
  19. %this.animationDropDown = %form.createDropDownItem(%item);
  20. %this.populateAnimationDropDown();
  21. %item = %form.addFormItem("Target Folder", %width SPC 30);
  22. %this.folderBox = %form.createFolderOpenItem(%item, "Select Target Folder");
  23. %this.folderBox.Command = %this.getId() @ ".Validate();";
  24. %item = %form.addFormItem("Asset Name", %width SPC 30);
  25. %this.assetNameBox = %form.createTextEditItem(%item);
  26. %this.assetNameBox.Command = %this.getId() @ ".Validate();";
  27. %item = %form.addFormItem("Target Module", %width SPC 30);
  28. %this.moduleNameBox = %form.createTextEditItem(%item);
  29. %this.moduleNameBox.setActive(false);
  30. %content.add(%form);
  31. %this.feedback = new GuiControl()
  32. {
  33. HorizSizing = "right";
  34. VertSizing = "bottom";
  35. Position = "12 270";
  36. Extent = (%width - 24) SPC 80;
  37. text = "Select an Image Asset or Animation Assset to get started!";
  38. };
  39. ThemeManager.setProfile(%this.feedback, "infoProfile");
  40. %this.cancelButton = new GuiButtonCtrl()
  41. {
  42. HorizSizing = "right";
  43. VertSizing = "bottom";
  44. Position = "478 370";
  45. Extent = "100 30";
  46. Text = "Cancel";
  47. Command = %this.getID() @ ".onClose();";
  48. };
  49. ThemeManager.setProfile(%this.cancelButton, "buttonProfile");
  50. %this.createButton = new GuiButtonCtrl()
  51. {
  52. HorizSizing = "right";
  53. VertSizing = "bottom";
  54. Position = "588 368";
  55. Extent = "100 34";
  56. Text = "Create";
  57. Command = %this.getID() @ ".onCreate();";
  58. };
  59. ThemeManager.setProfile(%this.createButton, "primaryButtonProfile");
  60. %content.add(%this.feedback);
  61. %content.add(%this.cancelButton);
  62. %content.add(%this.createButton);
  63. %this.prevFolder = "";
  64. }
  65. function NewParticleAssetDialog::Validate(%this)
  66. {
  67. %this.createButton.active = false;
  68. %imageAssetID = %this.imageDropDown.getText();
  69. %animationAssetID = %this.animationDropDown.getText();
  70. if(%imageAssetID $= "" && %animationAssetID $= "")
  71. {
  72. %this.feedback.setText("Select an Image Asset or Animation Assset to get started!");
  73. return false;
  74. }
  75. %assetID = %imageAssetID;
  76. if(%imageAssetID $= "")
  77. {
  78. %assetID = %animationAssetID;
  79. }
  80. %assetFilePath = AssetDatabase.getAssetFilePath(%assetID);
  81. if(%this.folderBox.getText() $= "")
  82. {
  83. %this.folderBox.setText(makeRelativePath(filePath(%assetFilePath), getMainDotCsDir()));
  84. }
  85. if(%this.assetNameBox.getText() $= "")
  86. {
  87. %this.assetNameBox.setText(getUnit(%assetID, 1, ":") @ "_PT");
  88. }
  89. %folderPath = %this.getFolderPath();
  90. %assetName = %this.assetNameBox.getText();
  91. if(%folderPath !$= %this.prevFolder)
  92. {
  93. %modSig = EditorCore.findModuleOfPath(%folderPath @ "a.txt");
  94. %this.moduleNameBox.setText(%modSig);
  95. %this.prevFolder = %folderPath;
  96. }
  97. %assetPath = %folderPath @ %assetName @ ".asset.taml";
  98. %moduleName = getUnit(%this.moduleNameBox.getText(), 0, "_");
  99. %moduleVersion = getUnit(%this.moduleNameBox.getText(), 1, "_");
  100. %assetID = %moduleName @ ":" @ %assetName;
  101. if(%folderPath $= "")
  102. {
  103. %this.feedback.setText("Please select a target folder.");
  104. return false;
  105. }
  106. if(%assetName $= "")
  107. {
  108. %this.feedback.setText("A particle asset must have an Asset Name.");
  109. return false;
  110. }
  111. if(%moduleName $= "")
  112. {
  113. %this.feedback.setText("You can only create a particle asset inside of a module.");
  114. return false;
  115. }
  116. %button = AssetAdmin.Dictionary["ParticleAsset"].getButton(%assetID);
  117. if(isObject(%button))
  118. {
  119. %this.feedback.setText("An asset by this name already exists in this module. Try choosing a different name.");
  120. return false;
  121. }
  122. %this.createButton.active = true;
  123. %this.feedback.setText("Press the Create button to open the new asset for editing.");
  124. return true;
  125. }
  126. function NewParticleAssetDialog::onClose(%this)
  127. {
  128. Canvas.popDialog(%this);
  129. }
  130. function NewParticleAssetDialog::onCreate(%this)
  131. {
  132. if(%this.validate())
  133. {
  134. %folderPath = %this.getFolderPath();
  135. %assetName = %this.assetNameBox.getText();
  136. %assetPath = %folderPath @ %assetName @ ".asset.taml";
  137. %moduleName = getUnit(%this.moduleNameBox.getText(), 0, "_");
  138. %moduleVersion = getUnit(%this.moduleNameBox.getText(), 1, "_");
  139. %assetID = %moduleName @ ":" @ %assetName;
  140. //Time to create a new file
  141. %newAsset = new ParticleAsset()
  142. {
  143. assetName = %assetName;
  144. };
  145. %newEmitter = %newAsset.createEmitter();
  146. %newEmitter.setEmitterName("DefaultEmitter");
  147. if(%this.imageDropDown.getText() !$= "")
  148. {
  149. %newEmitter.setImage(%this.imageDropDown.getText());
  150. }
  151. else
  152. {
  153. %newEmitter.setAnimation(%this.animationDropDown.getText());
  154. }
  155. %assetImportSuccessful = TAMLWrite(%newAsset, %assetPath);
  156. %moduleDef = ModuleDatabase.findModule(%moduleName, %moduleVersion);
  157. AssetDatabase.addDeclaredAsset(%moduleDef, %assetPath);
  158. //Do we already have this button?
  159. %button = AssetAdmin.Dictionary["ParticleAsset"].getButton(%assetID);
  160. if(!isObject(%button))
  161. {
  162. //Load it into the image dictionary
  163. %button = AssetAdmin.Dictionary["ParticleAsset"].addButton(%assetID);
  164. }
  165. %button.onClick();
  166. %this.onClose();
  167. }
  168. }
  169. function NewParticleAssetDialog::onFolderOpened(%this, %textBox)
  170. {
  171. %this.Validate();
  172. }
  173. function NewParticleAssetDialog::getFolderPath(%this)
  174. {
  175. %folderPath = stripTrailingSpaces(makeFullPath(%this.folderBox.getText()));
  176. %length = strlen(%folderPath);
  177. %lastChar = getSubStr(%folderPath, %length - 1, 1);
  178. if(%lastChar $= "/")
  179. {
  180. return %folderPath;
  181. }
  182. return %folderPath @ "/";
  183. }
  184. function NewParticleAssetDialog::onDropDownClosed(%this, %dropDown)
  185. {
  186. if(%dropDown == %this.imageDropDown && %dropDown.getText() !$= "")
  187. {
  188. %this.animationDropDown.setSelected(0);
  189. }
  190. else if(%dropDown == %this.animationDropDown && %dropDown.getText() !$= "")
  191. {
  192. %this.imageDropDown.setSelected(0);
  193. }
  194. %this.validate();
  195. }
  196. function NewParticleAssetDialog::populateImageDropDown(%this)
  197. {
  198. %this.imageDropDown.clearItems();
  199. %query = new AssetQuery();
  200. AssetDatabase.findAllAssets(%query);
  201. AssetDatabase.findAssetType(%query, "ImageAsset", true);
  202. for(%i = 0; %i < %query.getCount(); %i++)
  203. {
  204. %assetID = %query.getAsset(%i);
  205. if(!AssetDatabase.isAssetInternal(%assetID))
  206. {
  207. %this.imageDropDown.addItem(%assetID);
  208. }
  209. }
  210. %query.delete();
  211. %this.imageDropDown.sortByText();
  212. %this.imageDropDown.insertItem(0, "");
  213. %this.imageDropDown.setSelected(0);
  214. }
  215. function NewParticleAssetDialog::populateAnimationDropDown(%this)
  216. {
  217. %this.animationDropDown.clearItems();
  218. %query = new AssetQuery();
  219. AssetDatabase.findAllAssets(%query);
  220. AssetDatabase.findAssetType(%query, "AnimationAsset", true);
  221. for(%i = 0; %i < %query.getCount(); %i++)
  222. {
  223. %assetID = %query.getAsset(%i);
  224. if(!AssetDatabase.isAssetInternal(%assetID))
  225. {
  226. %this.animationDropDown.addItem(%assetID);
  227. }
  228. }
  229. %query.delete();
  230. %this.animationDropDown.sortByText();
  231. %this.animationDropDown.insertItem(0, "");
  232. %this.animationDropDown.setSelected(0);
  233. }