NewImageAssetDialog.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. function NewImageAssetDialog::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 File", %width SPC 30);
  16. %this.imageFileBox = %form.createFileOpenItem(%item, "PNG (*.PNG)|*.PNG|JPEG (*.JPG;*.JPEG;*.JPE)|*.JPG;*.JPEG;*.JPE", "Open Image File");
  17. %this.imageFileBox.Command = %this.getId() @ ".Validate();";
  18. %item = %form.addFormItem("Asset Name", %width SPC 30);
  19. %this.assetNameBox = %form.createTextEditItem(%item);
  20. %this.assetNameBox.Command = %this.getId() @ ".Validate();";
  21. %item = %form.addFormItem("Target Module", %width SPC 30);
  22. %this.moduleNameBox = %form.createTextEditItem(%item);
  23. %this.moduleNameBox.setActive(false);
  24. %content.add(%form);
  25. %this.feedback = new GuiControl()
  26. {
  27. HorizSizing = "right";
  28. VertSizing = "bottom";
  29. Position = "12 170";
  30. Extent = (%width - 24) SPC 80;
  31. text = "Select an Image File to get started!";
  32. };
  33. ThemeManager.setProfile(%this.feedback, "infoProfile");
  34. %this.cancelButton = new GuiButtonCtrl()
  35. {
  36. HorizSizing = "right";
  37. VertSizing = "bottom";
  38. Position = "478 270";
  39. Extent = "100 30";
  40. Text = "Cancel";
  41. Command = %this.getID() @ ".onClose();";
  42. };
  43. ThemeManager.setProfile(%this.cancelButton, "buttonProfile");
  44. %this.createButton = new GuiButtonCtrl()
  45. {
  46. HorizSizing = "right";
  47. VertSizing = "bottom";
  48. Position = "588 268";
  49. Extent = "100 34";
  50. Text = "Create";
  51. Command = %this.getID() @ ".onCreate();";
  52. };
  53. ThemeManager.setProfile(%this.createButton, "primaryButtonProfile");
  54. %content.add(%this.feedback);
  55. %content.add(%this.cancelButton);
  56. %content.add(%this.createButton);
  57. %this.prevFile = "";
  58. }
  59. function NewImageAssetDialog::Validate(%this)
  60. {
  61. %this.createButton.active = false;
  62. %file = %this.imageFileBox.getText();
  63. %assetName = %this.assetNameBox.getText();
  64. if(%file !$= %this.prevFile)
  65. {
  66. //remove the extention
  67. %fileNameSansExt = fileBase(fileName(%file));
  68. if(%fileNameSansExt !$= "" && %assetName $= "")
  69. {
  70. %assetName = %fileNameSansExt;
  71. %this.assetNameBox.setText(%fileNameSansExt);
  72. }
  73. %modSig = EditorCore.findModuleOfPath(%file);
  74. %this.moduleNameBox.setText(%modSig);
  75. %this.prevFile = %file;
  76. }
  77. %assetPath = filePath(%file) @ "/" @ %assetName @ ".asset.taml";
  78. %moduleName = getUnit(%this.moduleNameBox.getText(), 0, "_");
  79. %moduleVersion = getUnit(%this.moduleNameBox.getText(), 1, "_");
  80. %assetID = %moduleName @ ":" @ %assetName;
  81. if(!isFile(%file))
  82. {
  83. //We need a real image file!
  84. %this.feedback.setText("An existing image file must be used to create an image asset!");
  85. return false;
  86. }
  87. if(!isValidImageFile(%file))
  88. {
  89. //We need a real image file!
  90. %this.feedback.setText("There is a problem with the image file!");
  91. return false;
  92. }
  93. if(%file $= "")
  94. {
  95. %this.feedback.setText("Select an Image File to get started!");
  96. return false;
  97. }
  98. if(%assetName $= "")
  99. {
  100. %this.feedback.setText("An image asset must have an Asset Name.");
  101. return false;
  102. }
  103. if(%moduleName $= "")
  104. {
  105. %this.feedback.setText("You can only create an image asset inside of a module.");
  106. return false;
  107. }
  108. %button = AssetAdmin.Dictionary["ImageAsset"].getButton(%assetID);
  109. if(isObject(%button))
  110. {
  111. %this.feedback.setText("An asset by this name already exists in this module. Try choosing a different name.");
  112. return false;
  113. }
  114. %this.createButton.active = true;
  115. %this.feedback.setText("Press the Create button to open the new asset for editing.");
  116. return true;
  117. }
  118. function NewImageAssetDialog::onClose(%this)
  119. {
  120. Canvas.popDialog(%this);
  121. }
  122. function NewImageAssetDialog::onCreate(%this)
  123. {
  124. if(%this.validate())
  125. {
  126. %file = makeFullPath(%this.imageFileBox.getText());
  127. %assetName = %this.assetNameBox.getText();
  128. %assetPath = filePath(%file) @ "/" @ %assetName @ ".asset.taml";
  129. %moduleName = getUnit(%this.moduleNameBox.getText(), 0, "_");
  130. %moduleVersion = getUnit(%this.moduleNameBox.getText(), 1, "_");
  131. %assetID = %moduleName @ ":" @ %assetName;
  132. //Time to create a new file
  133. %newAsset = new ImageAsset()
  134. {
  135. assetName = %assetName;
  136. imageFile = %file;
  137. };
  138. %assetImportSuccessful = TAMLWrite(%newAsset, %assetPath);
  139. %moduleDef = ModuleDatabase.findModule(%moduleName, %moduleVersion);
  140. AssetDatabase.addDeclaredAsset(%moduleDef, %assetPath);
  141. //Refresh the asset so that the loose file will be a path relative to the asset file.
  142. AssetDatabase.refreshAsset(%assetID);
  143. //Do we already have this button?
  144. %button = AssetAdmin.Dictionary["ImageAsset"].getButton(%assetID);
  145. if(!isObject(%button))
  146. {
  147. //Load it into the image dictionary
  148. %button = AssetAdmin.Dictionary["ImageAsset"].addButton(%assetID);
  149. }
  150. %button.onClick();
  151. %this.onClose();
  152. }
  153. }
  154. function NewImageAssetDialog::onFileOpened(%this, %textBox)
  155. {
  156. %this.Validate();
  157. }