CreateProject.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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, projectPath?: string) {
  28. super();
  29. this.projectPath = projectPath;
  30. this.projectTemplate = projectTemplate;
  31. this.init("Create Project", "AtomicEditor/editor/ui/createproject.tb.txt");
  32. this.projectPathField = <Atomic.UIEditField>this.getWidget("project_path");
  33. this.projectNameField = <Atomic.UIEditField>this.getWidget("project_name");
  34. this.appIDField = <Atomic.UIEditField>this.getWidget("app_id");
  35. this.projectLanguageField = <Atomic.UISelectDropdown>this.getWidget("project_language");
  36. this.image = <Atomic.UIImageWidget>this.getWidget("project_image");
  37. this.desktopButton = this.addPlatformButton("desktop", "AtomicEditor/editor/images/Desktop128.png");
  38. this.desktopButton.value = 1;
  39. this.androidButton = this.addPlatformButton("android", "AtomicEditor/editor/images/Android128.png");
  40. this.iosButton = this.addPlatformButton("ios", "AtomicEditor/editor/images/iOS128.png");
  41. this.html5Button = this.addPlatformButton("html5", "AtomicEditor/editor/images/HTML5128.png");
  42. if (!projectTemplate.screenshot)
  43. this.image.visibility = Atomic.UI_WIDGET_VISIBILITY_GONE;
  44. else
  45. this.image.image = projectTemplate.screenshot;
  46. var fileSystem = Atomic.getFileSystem();
  47. var userDocuments = fileSystem.userDocumentsDir;
  48. if (Atomic.platform == "MacOSX") {
  49. userDocuments += "Documents/AtomicProjects";
  50. } else {
  51. userDocuments += "AtomicProjects";
  52. }
  53. // If we're specifying where to put the project (initially), then we are opening
  54. // an example directly so use the same name
  55. if (projectPath) {
  56. this.projectNameField.text = projectTemplate.name;
  57. }
  58. this.projectPathField.text = projectPath ? projectPath : userDocuments;
  59. this.populateLanguageSelectionList();
  60. // Need to manually set the focus so the contents get auto-selected
  61. this.projectNameField.setFocus();
  62. this.resizeToFitContent();
  63. this.center();
  64. }
  65. addPlatformButton(platformName:string, platformLogo:string):Atomic.UIButton {
  66. var platformcontainer = <Atomic.UILayout>this.getWidget("platformcontainer");
  67. // IMAGE BUTTON
  68. var id = platformName;
  69. var size = 92;
  70. var button = new Atomic.UIButton();
  71. button.id = id;
  72. button.toggleMode = true;
  73. var lp = new Atomic.UILayoutParams();
  74. lp.minWidth = size;
  75. lp.minHeight = size;
  76. button.layoutParams = lp;
  77. button.gravity = Atomic.UI_GRAVITY_ALL;
  78. var image = new Atomic.UIImageWidget();
  79. image.image = platformLogo;
  80. var rect = [0, 0, size, size];
  81. image.rect = rect;
  82. button.addChild(image);
  83. if (platformName != "desktop") {
  84. var greenplus = new Atomic.UIImageWidget();
  85. greenplus.image = "AtomicEditor/editor/images/green_plus.png";
  86. rect = [size-18, 2, size-2, 18];
  87. greenplus.rect = rect;
  88. greenplus.visibility = Atomic.UI_WIDGET_VISIBILITY_INVISIBLE;
  89. button.addChild(greenplus);
  90. button["greenPlus"] = greenplus;
  91. }
  92. platformcontainer.addChild(button);
  93. return button;
  94. }
  95. tryProjectCreate(): boolean {
  96. var name = this.projectNameField.text.trim();
  97. if (!name) {
  98. EditorUI.showModalError("New Project Editor Error", "Please enter a project name");
  99. return false;
  100. }
  101. var folder = this.projectPathField.text.trim();
  102. if (!folder) {
  103. EditorUI.showModalError("New Project Editor Error", "Please choose a root project folder");
  104. return false;
  105. }
  106. var fileSystem = Atomic.getFileSystem();
  107. if (!this.projectPath) {
  108. folder += "/" + name;
  109. if (fileSystem.dirExists(folder) || fileSystem.fileExists(folder)) {
  110. var message = folder + " exists\n\nPlease choose a different root folder or project name";
  111. EditorUI.showModalError("New Project Editor Error", message);
  112. return false;
  113. }
  114. }
  115. folder = Atomic.addTrailingSlash(folder);
  116. // Determine if we have a language template for the selected language.
  117. let selectedLanguage = this.projectLanguageField.text;
  118. // Check whether we have a required IDE installed for C# projects
  119. var atomicNET = false;
  120. if (selectedLanguage == "CSharp" || selectedLanguage == "C#") {
  121. atomicNET = true;
  122. if (!ToolCore.netProjectSystem.getIDEAvailable()) {
  123. this.hide();
  124. EditorUI.getModelOps().showAtomicNETWindow();
  125. return false;
  126. }
  127. }
  128. let templateFolder = "";
  129. for (let i = 0; i < this.projectTemplate.languages.length; i++) {
  130. if (this.projectTemplate.languages[i] === selectedLanguage) {
  131. templateFolder = this.projectTemplate.folder + selectedLanguage + "/";
  132. break;
  133. }
  134. }
  135. // Do the creation!
  136. if (templateFolder != "" && fileSystem.dirExists(templateFolder)) {
  137. if (!this.projectPath) {
  138. if (!fileSystem.copyDir(templateFolder, folder)) {
  139. var message = "Unable to copy folder: " + templateFolder + " to " + folder;
  140. EditorUI.showModalError("New Project Editor Error", message);
  141. return false;
  142. }
  143. }
  144. var utils = new Editor.FileUtils();
  145. utils.createDirs(folder + "Cache");
  146. utils.createDirs(folder + "Settings");
  147. if (!fileSystem.dirExists(folder)) {
  148. var message = "Unable to create folder: " + folder + "\n\nPlease choose a different root folder or project name";
  149. EditorUI.showModalError("New Project Editor Error", message);
  150. return false;
  151. }
  152. // Look for the .atomic project file and if it exists, then rename it
  153. let fileResults = fileSystem.scanDir(folder, "*.atomic", Atomic.SCAN_FILES, false);
  154. if (fileResults.length === 1) {
  155. fileSystem.rename(folder + fileResults[0], folder + name + ".atomic");
  156. } else {
  157. // 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
  158. var file = new Atomic.File(folder + name + ".atomic", Atomic.FILE_WRITE);
  159. file.close();
  160. }
  161. // Look for a .userprefs file and if it exists, then rename it
  162. fileResults = fileSystem.scanDir(folder, "*.userprefs", Atomic.SCAN_FILES, false);
  163. if (fileResults.length === 1) {
  164. fileSystem.rename(folder + fileResults[0], folder + name + ".userprefs");
  165. }
  166. // create project settings
  167. var platforms = ["desktop"];
  168. if (this.androidButton.value == 1) {
  169. platforms.push("android");
  170. }
  171. if (this.iosButton.value == 1) {
  172. platforms.push("ios");
  173. }
  174. var projectSettings = {
  175. name : name,
  176. platforms : platforms
  177. }
  178. var jsonFile = new Atomic.File(folder + "Settings/Project.json", Atomic.FILE_WRITE);
  179. if (jsonFile.isOpen()) {
  180. jsonFile.writeString(JSON.stringify(projectSettings, null, 2));
  181. jsonFile.flush();
  182. jsonFile.close();
  183. }
  184. // Generate AtomicNET project if necessary
  185. if (atomicNET) {
  186. if (!ProjectTemplates.generateAtomicNETProject({
  187. name: name,
  188. appID : this.appIDField.text,
  189. platforms : platforms,
  190. projectFolder : folder,
  191. projectTemplate : this.projectTemplate
  192. })) {
  193. var message = "Unable to generate AtomicNET project: " + folder;
  194. EditorUI.showModalError("New Project Editor Error", message);
  195. return false;
  196. }
  197. }
  198. this.hide();
  199. this.sendEvent(EditorEvents.LoadProject, { path: folder });
  200. return true;
  201. } else {
  202. let message = [
  203. "Unable to create project for:",
  204. "",
  205. `language: ${selectedLanguage}`,
  206. `template: ${templateFolder}`,
  207. "",
  208. "Please choose a different language."
  209. ].join("\n");
  210. EditorUI.showModalError("New Project Editor Error", message);
  211. return false;
  212. }
  213. }
  214. handleLanguageSwitch(selectedLanguage:string) {
  215. if (selectedLanguage == "CSharp" || selectedLanguage == "C#") {
  216. this.html5Button["greenPlus"].visibility = Atomic.UI_WIDGET_VISIBILITY_INVISIBLE;
  217. this.html5Button.value = 0;
  218. this.html5Button.disable();
  219. } else {
  220. this.html5Button.enable();
  221. this.html5Button["greenPlus"].visibility = this.html5Button.value == 1 ? Atomic.UI_WIDGET_VISIBILITY_VISIBLE : Atomic.UI_WIDGET_VISIBILITY_INVISIBLE;
  222. }
  223. }
  224. handleWidgetEvent(ev: Atomic.UIWidgetEvent) {
  225. if (ev.type == Atomic.UI_EVENT_TYPE_CLICK) {
  226. var id = ev.target.id;
  227. if (id == "cancel") {
  228. this.hide();
  229. return true;
  230. }
  231. else if (id == "choose_path") {
  232. var utils = new Editor.FileUtils();
  233. var path = utils.newProjectFileDialog();
  234. if (path.length)
  235. this.projectPathField.text = path;
  236. return true;
  237. }
  238. else if (id == "create") {
  239. this.tryProjectCreate();
  240. return true;
  241. }
  242. } else if (ev.type == Atomic.UI_EVENT_TYPE_CHANGED) {
  243. // handle language change
  244. if (ev.target.id == "project_language") {
  245. this.handleLanguageSwitch(this.projectLanguageField.text);
  246. }
  247. if (ev.target.id == "desktop") {
  248. // desktop is always selected
  249. this.desktopButton.value = 1;
  250. } else if (ev.target["greenPlus"]) {
  251. ev.target["greenPlus"].visibility = ev.target.value == 1 ? Atomic.UI_WIDGET_VISIBILITY_VISIBLE : Atomic.UI_WIDGET_VISIBILITY_INVISIBLE;
  252. }
  253. }
  254. }
  255. /**
  256. * Queries the json file for languages that are available to this template and populates the
  257. * list.
  258. */
  259. populateLanguageSelectionList() {
  260. this.projectLanguageFieldSource.clear();
  261. this.projectTemplate.languages.forEach(language => {
  262. this.projectLanguageFieldSource.addItem(new Atomic.UISelectItem(language));
  263. });
  264. this.projectLanguageField.source = this.projectLanguageFieldSource;
  265. this.projectLanguageField.value = 0;
  266. }
  267. projectPathField: Atomic.UIEditField;
  268. projectNameField: Atomic.UIEditField;
  269. appIDField: Atomic.UIEditField;
  270. projectLanguageField: Atomic.UISelectDropdown;
  271. projectLanguageFieldSource: Atomic.UISelectItemSource = new Atomic.UISelectItemSource();
  272. image: Atomic.UIImageWidget;
  273. desktopButton: Atomic.UIButton;
  274. androidButton: Atomic.UIButton;
  275. iosButton: Atomic.UIButton;
  276. html5Button: Atomic.UIButton;
  277. // if we have specified a projectPath, the dest will not be the combination of path + name
  278. projectPath: string;
  279. projectTemplate: ProjectTemplates.ProjectTemplateDefinition;
  280. }
  281. export = CreateProject;