NewAnimationAssetDialog.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. function NewAnimationAssetDialog::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("Target Folder", %width SPC 30);
  19. %this.folderBox = %form.createFolderOpenItem(%item, "Select Target Folder");
  20. %this.folderBox.Command = %this.getId() @ ".Validate();";
  21. %item = %form.addFormItem("Asset Name", %width SPC 30);
  22. %this.assetNameBox = %form.createTextEditItem(%item);
  23. %this.assetNameBox.Command = %this.getId() @ ".Validate();";
  24. %item = %form.addFormItem("Target Module", %width SPC 30);
  25. %this.moduleNameBox = %form.createTextEditItem(%item);
  26. %this.moduleNameBox.setActive(false);
  27. %content.add(%form);
  28. %this.feedback = new GuiControl()
  29. {
  30. HorizSizing = "right";
  31. VertSizing = "bottom";
  32. Position = "12 220";
  33. Extent = (%width - 24) SPC 80;
  34. text = "Select an Image Asset to get started!";
  35. textWrap = true;
  36. textExtend = true;
  37. };
  38. ThemeManager.setProfile(%this.feedback, "infoProfile");
  39. %this.cancelButton = new GuiButtonCtrl()
  40. {
  41. HorizSizing = "right";
  42. VertSizing = "bottom";
  43. Position = "478 320";
  44. Extent = "100 30";
  45. Text = "Cancel";
  46. Command = %this.getID() @ ".onClose();";
  47. };
  48. ThemeManager.setProfile(%this.cancelButton, "buttonProfile");
  49. %this.createButton = new GuiButtonCtrl()
  50. {
  51. HorizSizing = "right";
  52. VertSizing = "bottom";
  53. Position = "588 318";
  54. Extent = "100 34";
  55. Text = "Create";
  56. Command = %this.getID() @ ".onCreate();";
  57. };
  58. ThemeManager.setProfile(%this.createButton, "primaryButtonProfile");
  59. %content.add(%this.feedback);
  60. %content.add(%this.cancelButton);
  61. %content.add(%this.createButton);
  62. %this.prevFolder = "";
  63. }
  64. function NewAnimationAssetDialog::Validate(%this)
  65. {
  66. %this.createButton.active = false;
  67. %assetID = %this.imageDropDown.getText();
  68. if(%assetID $= "")
  69. {
  70. %this.feedback.setText("Select an Image Asset to get started!");
  71. return false;
  72. }
  73. %imageAssetFilePath = AssetDatabase.getAssetFilePath(%assetID);
  74. if(%this.folderBox.getText() $= "")
  75. {
  76. %this.folderBox.setText(makeRelativePath(filePath(%imageAssetFilePath), getMainDotCsDir()));
  77. }
  78. if(%this.assetNameBox.getText() $= "")
  79. {
  80. %this.assetNameBox.setText(getUnit(%assetID, 1, ":") @ "_AN");
  81. }
  82. %folderPath = %this.getFolderPath();
  83. %assetName = %this.assetNameBox.getText();
  84. if(%folderPath !$= %this.prevFolder)
  85. {
  86. %modSig = EditorCore.findModuleOfPath(%folderPath @ "a.txt");
  87. %this.moduleNameBox.setText(%modSig);
  88. %this.prevFolder = %folderPath;
  89. }
  90. %assetPath = pathConcat(%folderPath, %assetName @ ".animation.taml");
  91. %moduleName = getUnit(%this.moduleNameBox.getText(), 0, "_");
  92. %moduleVersion = getUnit(%this.moduleNameBox.getText(), 1, "_");
  93. %assetID = %moduleName @ ":" @ %assetName;
  94. if(%folderPath $= "")
  95. {
  96. %this.feedback.setText("Please select a target folder.");
  97. return false;
  98. }
  99. if(%assetName $= "")
  100. {
  101. %this.feedback.setText("An animation asset must have an Asset Name.");
  102. return false;
  103. }
  104. if(%moduleName $= "")
  105. {
  106. %this.feedback.setText("You can only create an animation asset inside of a module.");
  107. return false;
  108. }
  109. else
  110. {
  111. %module = ModuleDatabase.findModule(%moduleName, %moduleVersion);
  112. if(!isObject(%module))
  113. {
  114. %this.feedback.setText("There was a problem finding the module for this asset.");
  115. return false;
  116. }
  117. else if(%module.Synchronized)
  118. {
  119. %this.feedback.setText("You cannot add assets to a library module. Updates to the module would remove your assets. Instead, create your own module and add assets to it. Remember to have your module scan for assets.");
  120. return false;
  121. }
  122. }
  123. %button = AssetAdmin.Dictionary["AnimationAsset"].getButton(%assetID);
  124. if(isObject(%button))
  125. {
  126. %this.feedback.setText("An asset by this name already exists in this module. Try choosing a different name.");
  127. return false;
  128. }
  129. %this.createButton.active = true;
  130. %this.feedback.setText("Press the Create button to open the new asset for editing. Your new asset will have the extension animation.taml. You must have your module scan the asset's folder for this extension.");
  131. return true;
  132. }
  133. function NewAnimationAssetDialog::onCreate(%this)
  134. {
  135. if(%this.validate())
  136. {
  137. %folderPath = %this.getFolderPath();
  138. %assetName = %this.assetNameBox.getText();
  139. %assetPath = pathConcat(%folderPath, %assetName @ ".animation.taml");
  140. %moduleName = getUnit(%this.moduleNameBox.getText(), 0, "_");
  141. %moduleVersion = getUnit(%this.moduleNameBox.getText(), 1, "_");
  142. %assetID = %moduleName @ ":" @ %assetName;
  143. //Time to create a new file
  144. %newAsset = new AnimationAsset()
  145. {
  146. assetName = %assetName;
  147. Image = %this.imageDropDown.getText();
  148. AnimationFrames = "0 1";
  149. AnimationTime = "1";
  150. AnimationCycle = "1";
  151. };
  152. %assetImportSuccessful = TAMLWrite(%newAsset, %assetPath);
  153. %moduleDef = ModuleDatabase.findModule(%moduleName, %moduleVersion);
  154. AssetDatabase.addDeclaredAsset(%moduleDef, %assetPath);
  155. //Do we already have this button?
  156. %button = AssetAdmin.Dictionary["AnimationAsset"].getButton(%assetID);
  157. if(!isObject(%button))
  158. {
  159. //Load it into the image dictionary
  160. %button = AssetAdmin.Dictionary["AnimationAsset"].addButton(%assetID);
  161. }
  162. %button.onClick();
  163. %this.onClose();
  164. }
  165. }
  166. function NewAnimationAssetDialog::onFolderOpened(%this, %textBox)
  167. {
  168. %this.Validate();
  169. }
  170. function NewAnimationAssetDialog::getFolderPath(%this)
  171. {
  172. %folderPath = stripTrailingSpaces(makeFullPath(%this.folderBox.getText()));
  173. %length = strlen(%folderPath);
  174. %lastChar = getSubStr(%folderPath, %length - 1, 1);
  175. if(%lastChar $= "/")
  176. {
  177. return %folderPath;
  178. }
  179. return %folderPath @ "/";
  180. }
  181. function NewAnimationAssetDialog::onDropDownClosed(%this, %dropDown)
  182. {
  183. %this.validate();
  184. }
  185. function NewAnimationAssetDialog::populateImageDropDown(%this)
  186. {
  187. %this.imageDropDown.clearItems();
  188. %query = new AssetQuery();
  189. AssetDatabase.findAllAssets(%query);
  190. AssetDatabase.findAssetType(%query, "ImageAsset", true);
  191. for(%i = 0; %i < %query.getCount(); %i++)
  192. {
  193. %assetID = %query.getAsset(%i);
  194. if(!AssetDatabase.isAssetInternal(%assetID))
  195. {
  196. %imageAsset = AssetDatabase.acquireAsset(%assetID);
  197. if(%imageAsset.getFrameCount() > 1)
  198. {
  199. %this.imageDropDown.addItem(%assetID);
  200. }
  201. }
  202. }
  203. %query.delete();
  204. %this.imageDropDown.sortByText();
  205. %this.imageDropDown.insertItem(0, "");
  206. %this.imageDropDown.setSelected(0);
  207. }