CreateProject.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. var projectPath = this.projectPath;
  103. // if we changed the project path, clear it
  104. if (folder != this.projectPath) {
  105. projectPath = "";
  106. }
  107. if (!folder) {
  108. EditorUI.showModalError("New Project Editor Error", "Please choose a root project folder");
  109. return false;
  110. }
  111. var fileSystem = Atomic.getFileSystem();
  112. if (!projectPath) {
  113. folder += "/" + name;
  114. if (fileSystem.dirExists(folder) || fileSystem.fileExists(folder)) {
  115. var message = folder + " exists\n\nPlease choose a different root folder or project name";
  116. EditorUI.showModalError("New Project Editor Error", message);
  117. return false;
  118. }
  119. }
  120. folder = Atomic.addTrailingSlash(folder);
  121. // Determine if we have a language template for the selected language.
  122. let selectedLanguage = this.projectLanguageField.text;
  123. // Check whether we have a required IDE installed for C# projects
  124. var atomicNET = false;
  125. if (selectedLanguage == "CSharp" || selectedLanguage == "C#") {
  126. atomicNET = true;
  127. if (!ToolCore.netProjectSystem.getIDEAvailable()) {
  128. this.hide();
  129. EditorUI.getModelOps().showAtomicNETWindow();
  130. return false;
  131. }
  132. }
  133. let templateFolder = "";
  134. for (let i = 0; i < this.projectTemplate.languages.length; i++) {
  135. if (this.projectTemplate.languages[i] === selectedLanguage) {
  136. templateFolder = this.projectTemplate.folder + selectedLanguage + "/";
  137. break;
  138. }
  139. }
  140. // Do the creation!
  141. if (templateFolder != "" && fileSystem.dirExists(templateFolder)) {
  142. if (!projectPath) {
  143. if (!fileSystem.copyDir(templateFolder, folder)) {
  144. var message = "Unable to copy folder: " + templateFolder + " to " + folder;
  145. EditorUI.showModalError("New Project Editor Error", message);
  146. return false;
  147. }
  148. }
  149. var utils = new Editor.FileUtils();
  150. utils.createDirs(folder + "Cache");
  151. utils.createDirs(folder + "Settings");
  152. if (!fileSystem.dirExists(folder)) {
  153. var message = "Unable to create folder: " + folder + "\n\nPlease choose a different root folder or project name";
  154. EditorUI.showModalError("New Project Editor Error", message);
  155. return false;
  156. }
  157. // Look for the .atomic project file and if it exists, then rename it
  158. let fileResults = fileSystem.scanDir(folder, "*.atomic", Atomic.SCAN_FILES, false);
  159. if (fileResults.length === 1) {
  160. fileSystem.rename(folder + fileResults[0], folder + name + ".atomic");
  161. } else {
  162. // 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
  163. var file = new Atomic.File(folder + name + ".atomic", Atomic.FILE_WRITE);
  164. file.close();
  165. }
  166. // Look for a .userprefs file and if it exists, then rename it
  167. fileResults = fileSystem.scanDir(folder, "*.userprefs", Atomic.SCAN_FILES, false);
  168. if (fileResults.length === 1) {
  169. fileSystem.rename(folder + fileResults[0], folder + name + ".userprefs");
  170. }
  171. // create project settings
  172. var platforms = ["desktop"];
  173. if (this.androidButton.value == 1) {
  174. platforms.push("android");
  175. }
  176. if (this.iosButton.value == 1) {
  177. platforms.push("ios");
  178. }
  179. var projectSettings = {
  180. name : name,
  181. platforms : platforms
  182. }
  183. var jsonFile = new Atomic.File(folder + "Settings/Project.json", Atomic.FILE_WRITE);
  184. if (jsonFile.isOpen()) {
  185. jsonFile.writeString(JSON.stringify(projectSettings, null, 2));
  186. jsonFile.flush();
  187. jsonFile.close();
  188. }
  189. // Generate AtomicNET project if necessary
  190. if (atomicNET) {
  191. if (!ProjectTemplates.generateAtomicNETProject({
  192. name: name,
  193. appID : this.appIDField.text,
  194. platforms : platforms,
  195. projectFolder : folder,
  196. projectTemplate : this.projectTemplate
  197. })) {
  198. var message = "Unable to generate AtomicNET project: " + folder;
  199. EditorUI.showModalError("New Project Editor Error", message);
  200. return false;
  201. }
  202. }
  203. this.hide();
  204. this.sendEvent(EditorEvents.LoadProject, { path: folder });
  205. return true;
  206. } else {
  207. let message = [
  208. "Unable to create project for:",
  209. "",
  210. `language: ${selectedLanguage}`,
  211. `template: ${templateFolder}`,
  212. "",
  213. "Please choose a different language."
  214. ].join("\n");
  215. EditorUI.showModalError("New Project Editor Error", message);
  216. return false;
  217. }
  218. }
  219. handleLanguageSwitch(selectedLanguage:string) {
  220. if (selectedLanguage == "CSharp" || selectedLanguage == "C#") {
  221. this.html5Button["greenPlus"].visibility = Atomic.UI_WIDGET_VISIBILITY_INVISIBLE;
  222. this.html5Button.value = 0;
  223. this.html5Button.disable();
  224. } else {
  225. this.html5Button.enable();
  226. this.html5Button["greenPlus"].visibility = this.html5Button.value == 1 ? Atomic.UI_WIDGET_VISIBILITY_VISIBLE : Atomic.UI_WIDGET_VISIBILITY_INVISIBLE;
  227. }
  228. }
  229. handleWidgetEvent(ev: Atomic.UIWidgetEvent) {
  230. if (ev.type == Atomic.UI_EVENT_TYPE_CLICK) {
  231. var id = ev.target.id;
  232. if (id == "cancel") {
  233. this.hide();
  234. return true;
  235. }
  236. else if (id == "choose_path") {
  237. var utils = new Editor.FileUtils();
  238. var path = utils.newProjectFileDialog();
  239. if (path.length)
  240. this.projectPathField.text = path;
  241. return true;
  242. }
  243. else if (id == "create") {
  244. this.tryProjectCreate();
  245. return true;
  246. }
  247. } else if (ev.type == Atomic.UI_EVENT_TYPE_CHANGED) {
  248. // handle language change
  249. if (ev.target.id == "project_language") {
  250. this.handleLanguageSwitch(this.projectLanguageField.text);
  251. }
  252. if (ev.target.id == "desktop") {
  253. // desktop is always selected
  254. this.desktopButton.value = 1;
  255. } else if (ev.target["greenPlus"]) {
  256. ev.target["greenPlus"].visibility = ev.target.value == 1 ? Atomic.UI_WIDGET_VISIBILITY_VISIBLE : Atomic.UI_WIDGET_VISIBILITY_INVISIBLE;
  257. }
  258. }
  259. }
  260. /**
  261. * Queries the json file for languages that are available to this template and populates the
  262. * list.
  263. */
  264. populateLanguageSelectionList() {
  265. this.projectLanguageFieldSource.clear();
  266. this.projectTemplate.languages.forEach(language => {
  267. this.projectLanguageFieldSource.addItem(new Atomic.UISelectItem(language));
  268. });
  269. this.projectLanguageField.source = this.projectLanguageFieldSource;
  270. this.projectLanguageField.value = 0;
  271. }
  272. projectPathField: Atomic.UIEditField;
  273. projectNameField: Atomic.UIEditField;
  274. appIDField: Atomic.UIEditField;
  275. projectLanguageField: Atomic.UISelectDropdown;
  276. projectLanguageFieldSource: Atomic.UISelectItemSource = new Atomic.UISelectItemSource();
  277. image: Atomic.UIImageWidget;
  278. desktopButton: Atomic.UIButton;
  279. androidButton: Atomic.UIButton;
  280. iosButton: Atomic.UIButton;
  281. html5Button: Atomic.UIButton;
  282. // if we have specified a projectPath, the dest will not be the combination of path + name
  283. projectPath: string;
  284. projectTemplate: ProjectTemplates.ProjectTemplateDefinition;
  285. }
  286. export = CreateProject;