NewAnimationAssetDialog.cs 5.9 KB

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