GuiEditorSaveGuiDialog.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. function GuiEditorSaveGuiDialog::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("Gui Name", %width SPC 30);
  16. %this.guiNameBox = %form.createTextEditItem(%item);
  17. %this.guiNameBox.Command = %this.getId() @ ".Validate();";
  18. if(%this.defaultFileName !$= "")
  19. {
  20. %name = %this.defaultFileName;
  21. }
  22. else if(GuiEditor.rootGui.getName() !$= "")
  23. {
  24. %name = GuiEditor.rootGui.getName() @ ".gui";
  25. }
  26. else
  27. {
  28. %name = "untitled.gui";
  29. }
  30. %this.guiNameBox.text = %name;
  31. %item = %form.addFormItem("Save Format", %width SPC 30);
  32. %this.guiFormatDropDown = %form.createDropDownItem(%item);
  33. %this.guiFormatDropDown.Command = %this.getId() @ ".Validate();";
  34. %this.populateFormatDropDown();
  35. %this.guiFormatDropDown.setSelected(%this.formatIndex);
  36. %item = %form.addFormItem("Target Folder", %width SPC 30);
  37. %this.folderBox = %form.createFolderOpenItem(%item, "Select Target Folder");
  38. %this.folderBox.Command = %this.getId() @ ".Validate();";
  39. if(%this.defaultFolder !$= "")
  40. {
  41. %this.folderBox.setText(%this.defaultFolder);
  42. }
  43. %item = %form.addFormItem("Target Module", %width SPC 30);
  44. %this.moduleNameBox = %form.createTextEditItem(%item);
  45. %this.moduleNameBox.setActive(false);
  46. if(%this.defaultModule !$= "")
  47. {
  48. %this.moduleNameBox.setText(%this.defaultModule);
  49. }
  50. %content.add(%form);
  51. %this.feedback = new GuiControl()
  52. {
  53. HorizSizing = "right";
  54. VertSizing = "bottom";
  55. Position = "12 220";
  56. Extent = (%width - 24) SPC 80;
  57. text = "Select a name and target folder!";
  58. textWrap = true;
  59. textExtend = true;
  60. };
  61. ThemeManager.setProfile(%this.feedback, "infoProfile");
  62. %this.cancelButton = new GuiButtonCtrl()
  63. {
  64. HorizSizing = "right";
  65. VertSizing = "bottom";
  66. Position = "478 320";
  67. Extent = "100 30";
  68. Text = "Cancel";
  69. Command = %this.getID() @ ".onClose();";
  70. };
  71. ThemeManager.setProfile(%this.cancelButton, "buttonProfile");
  72. %this.saveButton = new GuiButtonCtrl()
  73. {
  74. HorizSizing = "right";
  75. VertSizing = "bottom";
  76. Position = "588 318";
  77. Extent = "100 34";
  78. Text = "Save";
  79. Command = %this.getID() @ ".onSave();";
  80. };
  81. ThemeManager.setProfile(%this.saveButton, "primaryButtonProfile");
  82. %content.add(%this.feedback);
  83. %content.add(%this.cancelButton);
  84. %content.add(%this.saveButton);
  85. %this.prevFolder = "";
  86. %this.validate();
  87. }
  88. function GuiEditorSaveGuiDialog::Validate(%this)
  89. {
  90. %this.createButton.active = false;
  91. %folderPath = %this.getFolderPath();
  92. %guiName = %this.guiNameBox.getText();
  93. if(%folderPath !$= %this.prevFolder)
  94. {
  95. %modSig = EditorCore.findModuleOfPath(%folderPath @ "a.txt");
  96. %this.moduleNameBox.setText(%modSig);
  97. %this.prevFolder = %folderPath;
  98. }
  99. %moduleName = getUnit(%this.moduleNameBox.getText(), 0, "_");
  100. %moduleVersion = getUnit(%this.moduleNameBox.getText(), 1, "_");
  101. if(%folderPath $= "")
  102. {
  103. %this.feedback.setText("Please select a target folder.");
  104. return false;
  105. }
  106. if(%guiName $= "")
  107. {
  108. %this.feedback.setText("A Gui file must have a name.");
  109. return false;
  110. }
  111. if(%moduleName $= "")
  112. {
  113. %this.feedback.setText("You can only save a Gui file inside of a module.");
  114. return false;
  115. }
  116. else
  117. {
  118. %module = ModuleDatabase.findModule(%moduleName, %moduleVersion);
  119. if(!isObject(%module))
  120. {
  121. %this.feedback.setText("There was a problem finding the module for this file.");
  122. return false;
  123. }
  124. else if(%module.Synchronized)
  125. {
  126. %this.feedback.setText("You cannot add files to a library module. Updates to the module would remove your files. Instead, create your own module and add files to it.");
  127. return false;
  128. }
  129. }
  130. %filePath = %this.getFilePath();
  131. if(isFile(%filePath) && !isWriteableFileName(%filePath))
  132. {
  133. %this.feedback.setText("A file by this name already exists and it is read-only.");
  134. return false;
  135. }
  136. if(isFile(%filePath))
  137. {
  138. %this.createButton.active = true;
  139. %this.feedback.setText("A file by this name already exists. It will be overwritten.");
  140. return true;
  141. }
  142. %this.createButton.active = true;
  143. %this.feedback.setText("A new Gui file will be created!");
  144. return true;
  145. }
  146. function GuiEditorSaveGuiDialog::onSave(%this)
  147. {
  148. if(%this.validate())
  149. {
  150. %filePath = %this.getFilePath();
  151. %formatIndex = %this.guiFormatDropDown.getSelectedItem();
  152. GuiEditor.SaveCore(%filePath, %formatIndex, %this.folderBox.getText(), %this.moduleNameBox.getText());
  153. %this.onClose();
  154. }
  155. }
  156. function GuiEditorSaveGuiDialog::onFolderOpened(%this, %textBox)
  157. {
  158. %this.Validate();
  159. }
  160. function GuiEditorSaveGuiDialog::getFilePath(%this)
  161. {
  162. %folderPath = %this.getFolderPath();
  163. %fileName = %this.guiNameBox.getText();
  164. %filePath = pathConcat(%folderPath, %fileName);
  165. %length = strlen(%filePath);
  166. if(%this.guiFormatDropDown.getSelectedItem() == 0)
  167. {
  168. if(%length >= 4)
  169. {
  170. %sub = getSubStr(%filePath, %length - 4, 4);
  171. }
  172. if(%sub !$= ".gui")
  173. {
  174. %filePath = %filePath @ ".gui";
  175. }
  176. }
  177. else
  178. {
  179. if(%length >= 4)
  180. {
  181. %sub4 = getSubStr(%filePath, %length - 4, 4);
  182. }
  183. if(%length >= 9)
  184. {
  185. %sub9 = getSubStr(%filePath, %length - 9, 9);
  186. }
  187. if(%sub9 !$= ".gui.taml")
  188. {
  189. if(%sub4 $= ".gui")
  190. {
  191. %filePath = %filePath @ ".taml";
  192. }
  193. else
  194. {
  195. %filePath = %filePath @ ".gui.taml";
  196. }
  197. }
  198. }
  199. return %filePath;
  200. }
  201. function GuiEditorSaveGuiDialog::getFolderPath(%this)
  202. {
  203. %folderPath = stripTrailingSpaces(makeFullPath(%this.folderBox.getText()));
  204. %length = strlen(%folderPath);
  205. %lastChar = getSubStr(%folderPath, %length - 1, 1);
  206. if(%lastChar $= "/")
  207. {
  208. return %folderPath;
  209. }
  210. return %folderPath @ "/";
  211. }
  212. function GuiEditorSaveGuiDialog::onDropDownClosed(%this, %dropDown)
  213. {
  214. %this.validate();
  215. }
  216. function GuiEditorSaveGuiDialog::populateFormatDropDown(%this)
  217. {
  218. %this.guiFormatDropDown.clearItems();
  219. %this.guiFormatDropDown.addItem("GUI File (*.gui)");
  220. %this.guiFormatDropDown.addItem("TAML (*.gui.taml)");
  221. %this.guiFormatDropDown.setSelected(0);
  222. }