CreateProject.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. import EditorEvents = require("../../editor/EditorEvents");
  23. import EditorUI = require("../EditorUI");
  24. import ModalWindow = require("./ModalWindow");
  25. import ProjectTemplates = require("../../resources/ProjectTemplates");
  26. class CreateProject extends ModalWindow {
  27. constructor(projectTemplate: ProjectTemplates.ProjectTemplateDefinition) {
  28. super();
  29. this.projectTemplate = projectTemplate;
  30. this.init("Create Project", "AtomicEditor/editor/ui/createproject.tb.txt");
  31. this.projectPathField = <Atomic.UIEditField>this.getWidget("project_path");
  32. this.projectNameField = <Atomic.UIEditField>this.getWidget("project_name");
  33. this.appIDField = <Atomic.UIEditField>this.getWidget("app_id");
  34. this.projectLanguageField = <Atomic.UISelectDropdown>this.getWidget("project_language");
  35. this.image = <Atomic.UIImageWidget>this.getWidget("project_image");
  36. this.desktopButton = this.addPlatformButton("desktop", "AtomicEditor/editor/images/Desktop128.png");
  37. this.desktopButton.value = 1;
  38. this.androidButton = this.addPlatformButton("android", "AtomicEditor/editor/images/Android128.png");
  39. this.iosButton = this.addPlatformButton("ios", "AtomicEditor/editor/images/iOS128.png");
  40. this.html5Button = this.addPlatformButton("html5", "AtomicEditor/editor/images/HTML5128.png");
  41. if (!projectTemplate.screenshot)
  42. this.image.visibility = Atomic.UI_WIDGET_VISIBILITY_GONE;
  43. else
  44. this.image.image = projectTemplate.screenshot;
  45. var fileSystem = Atomic.getFileSystem();
  46. var userDocuments = fileSystem.userDocumentsDir;
  47. if (Atomic.platform == "MacOSX") {
  48. userDocuments += "Documents/AtomicProjects";
  49. } else {
  50. userDocuments += "AtomicProjects";
  51. }
  52. this.projectPathField.text = userDocuments;
  53. this.populateLanguageSelectionList();
  54. // Need to manually set the focus so the contents get auto-selected
  55. this.projectNameField.setFocus();
  56. this.resizeToFitContent();
  57. this.center();
  58. }
  59. addPlatformButton(platformName:string, platformLogo:string):Atomic.UIButton {
  60. var platformcontainer = <Atomic.UILayout>this.getWidget("platformcontainer");
  61. // IMAGE BUTTON
  62. var id = platformName;
  63. var size = 92;
  64. var button = new Atomic.UIButton();
  65. button.id = id;
  66. button.toggleMode = true;
  67. var lp = new Atomic.UILayoutParams();
  68. lp.minWidth = size;
  69. lp.minHeight = size;
  70. button.layoutParams = lp;
  71. button.gravity = Atomic.UI_GRAVITY_ALL;
  72. var image = new Atomic.UIImageWidget();
  73. image.image = platformLogo;
  74. var rect = [0, 0, size, size];
  75. image.rect = rect;
  76. button.addChild(image);
  77. if (platformName != "desktop") {
  78. var greenplus = new Atomic.UIImageWidget();
  79. greenplus.image = "AtomicEditor/editor/images/green_plus.png";
  80. rect = [size-18, 2, size-2, 18];
  81. greenplus.rect = rect;
  82. greenplus.visibility = Atomic.UI_WIDGET_VISIBILITY_INVISIBLE;
  83. button.addChild(greenplus);
  84. button["greenPlus"] = greenplus;
  85. }
  86. platformcontainer.addChild(button);
  87. return button;
  88. }
  89. tryProjectCreate(): boolean {
  90. var name = this.projectNameField.text.trim();
  91. if (!name) {
  92. EditorUI.showModalError("New Project Editor Error", "Please enter a project name");
  93. return false;
  94. }
  95. var folder = this.projectPathField.text.trim();
  96. if (!folder) {
  97. EditorUI.showModalError("New Project Editor Error", "Please choose a root project folder");
  98. return false;
  99. }
  100. folder += "/" + name;
  101. var fileSystem = Atomic.getFileSystem();
  102. if (fileSystem.dirExists(folder) || fileSystem.fileExists(folder)) {
  103. var message = folder + " exists\n\nPlease choose a different root folder or project name";
  104. EditorUI.showModalError("New Project Editor Error", message);
  105. return false;
  106. }
  107. folder = Atomic.addTrailingSlash(folder);
  108. if (!fileSystem.dirExists(folder)) {
  109. // Determine if we have a language template for the selected language.
  110. let templateDetail: ProjectTemplates.ProjectTemplateDetail;
  111. let selectedLanguage = this.projectLanguageField.text;
  112. // Check whether we have a required IDE installed for C# projects
  113. var atomicNET = false;
  114. if (selectedLanguage == "CSharp" || selectedLanguage == "C#") {
  115. atomicNET = true;
  116. if (!ToolCore.netProjectSystem.getIDEAvailable()) {
  117. this.hide();
  118. EditorUI.getModelOps().showAtomicNETWindow();
  119. return false;
  120. }
  121. }
  122. for (let i = 0; i < this.projectTemplate.templates.length; i++) {
  123. if (this.projectTemplate.templates[i].language === selectedLanguage) {
  124. templateDetail = this.projectTemplate.templates[i];
  125. break;
  126. }
  127. }
  128. // Do the creation!
  129. if (templateDetail && fileSystem.dirExists(templateDetail.folder)) {
  130. if (!fileSystem.copyDir(templateDetail.folder, folder)) {
  131. var message = "Unable to copy folder: " + templateDetail.folder + " to " + folder;
  132. EditorUI.showModalError("New Project Editor Error", message);
  133. return false;
  134. }
  135. var utils = new Editor.FileUtils();
  136. utils.createDirs(folder + "Cache");
  137. utils.createDirs(folder + "Settings");
  138. if (!fileSystem.dirExists(folder)) {
  139. var message = "Unable to create folder: " + folder + "\n\nPlease choose a different root folder or project name";
  140. EditorUI.showModalError("New Project Editor Error", message);
  141. return false;
  142. }
  143. // Look for the .atomic project file and if it exists, then rename it
  144. let fileResults = fileSystem.scanDir(folder, "*.atomic", Atomic.SCAN_FILES, false);
  145. if (fileResults.length === 1) {
  146. fileSystem.rename(folder + fileResults[0], folder + name + ".atomic");
  147. } else {
  148. // Just create the file. We either don't have one existing, or we have more than one and don't know which one to rename
  149. var file = new Atomic.File(folder + name + ".atomic", Atomic.FILE_WRITE);
  150. file.close();
  151. }
  152. // Look for a .userprefs file and if it exists, then rename it
  153. fileResults = fileSystem.scanDir(folder, "*.userprefs", Atomic.SCAN_FILES, false);
  154. if (fileResults.length === 1) {
  155. fileSystem.rename(folder + fileResults[0], folder + name + ".userprefs");
  156. }
  157. // create project settings
  158. var platforms = ["desktop"];
  159. if (this.androidButton.value == 1) {
  160. platforms.push("android");
  161. }
  162. if (this.iosButton.value == 1) {
  163. platforms.push("ios");
  164. }
  165. var projectSettings = {
  166. name : name,
  167. platforms : platforms
  168. }
  169. var jsonFile = new Atomic.File(folder + "Settings/Project.json", Atomic.FILE_WRITE);
  170. if (jsonFile.isOpen()) {
  171. jsonFile.writeString(JSON.stringify(projectSettings, null, 2));
  172. jsonFile.flush();
  173. jsonFile.close();
  174. }
  175. // Generate AtomicNET project if necessary
  176. if (atomicNET) {
  177. if (!ProjectTemplates.generateAtomicNETProject({
  178. name: name,
  179. appID : this.appIDField.text,
  180. platforms : platforms,
  181. projectFolder : folder
  182. })) {
  183. var message = "Unable to generate AtomicNET project: " + folder;
  184. EditorUI.showModalError("New Project Editor Error", message);
  185. return false;
  186. }
  187. }
  188. this.hide();
  189. this.sendEvent(EditorEvents.LoadProject, { path: folder });
  190. return true;
  191. } else {
  192. let message = [
  193. "Unable to create project for:",
  194. "",
  195. `language: ${selectedLanguage}`,
  196. `template: ${templateDetail.folder}`,
  197. "",
  198. "Please choose a different language."
  199. ].join("\n");
  200. EditorUI.showModalError("New Project Editor Error", message);
  201. return false;
  202. }
  203. }
  204. return false;
  205. }
  206. handleLanguageSwitch(selectedLanguage:string) {
  207. if (selectedLanguage == "CSharp" || selectedLanguage == "C#") {
  208. this.html5Button["greenPlus"].visibility = Atomic.UI_WIDGET_VISIBILITY_INVISIBLE;
  209. this.html5Button.value = 0;
  210. this.html5Button.disable();
  211. } else {
  212. this.html5Button.enable();
  213. this.html5Button["greenPlus"].visibility = this.html5Button.value == 1 ? Atomic.UI_WIDGET_VISIBILITY_VISIBLE : Atomic.UI_WIDGET_VISIBILITY_INVISIBLE;
  214. }
  215. }
  216. handleWidgetEvent(ev: Atomic.UIWidgetEvent) {
  217. if (ev.type == Atomic.UI_EVENT_TYPE_CLICK) {
  218. var id = ev.target.id;
  219. if (id == "cancel") {
  220. this.hide();
  221. return true;
  222. }
  223. else if (id == "choose_path") {
  224. var utils = new Editor.FileUtils();
  225. var path = utils.newProjectFileDialog();
  226. if (path.length)
  227. this.projectPathField.text = path;
  228. return true;
  229. }
  230. else if (id == "create") {
  231. this.tryProjectCreate();
  232. return true;
  233. }
  234. } else if (ev.type == Atomic.UI_EVENT_TYPE_CHANGED) {
  235. // handle language change
  236. if (ev.target.id == "project_language") {
  237. this.handleLanguageSwitch(this.projectLanguageField.text);
  238. }
  239. if (ev.target.id == "desktop") {
  240. // desktop is always selected
  241. this.desktopButton.value = 1;
  242. } else if (ev.target["greenPlus"]) {
  243. ev.target["greenPlus"].visibility = ev.target.value == 1 ? Atomic.UI_WIDGET_VISIBILITY_VISIBLE : Atomic.UI_WIDGET_VISIBILITY_INVISIBLE;
  244. }
  245. }
  246. }
  247. /**
  248. * Queries the json file for languages that are available to this template and populates the
  249. * list.
  250. */
  251. populateLanguageSelectionList() {
  252. this.projectLanguageFieldSource.clear();
  253. this.projectTemplate.templates.forEach(templateDetail => {
  254. this.projectLanguageFieldSource.addItem(new Atomic.UISelectItem(templateDetail.language));
  255. });
  256. this.projectLanguageField.source = this.projectLanguageFieldSource;
  257. this.projectLanguageField.value = 0;
  258. }
  259. projectPathField: Atomic.UIEditField;
  260. projectNameField: Atomic.UIEditField;
  261. appIDField: Atomic.UIEditField;
  262. projectLanguageField: Atomic.UISelectDropdown;
  263. projectLanguageFieldSource: Atomic.UISelectItemSource = new Atomic.UISelectItemSource();
  264. image: Atomic.UIImageWidget;
  265. desktopButton: Atomic.UIButton;
  266. androidButton: Atomic.UIButton;
  267. iosButton: Atomic.UIButton;
  268. html5Button: Atomic.UIButton;
  269. projectTemplate: ProjectTemplates.ProjectTemplateDefinition;
  270. }
  271. export = CreateProject;