CreateProject.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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 EditorUI = require("../EditorUI");
  23. import ModalWindow = require("./ModalWindow");
  24. import Preferences = require("../../editor/Preferences");
  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.defaultPath = Preferences.getInstance().editorFeatures.defaultPath;
  32. this.defaultLanguage = Preferences.getInstance().editorFeatures.defaultLanguage;
  33. this.init("Create Project", "AtomicEditor/editor/ui/createproject.tb.txt");
  34. this.projectPathField = <Atomic.UIEditField>this.getWidget("project_path");
  35. this.projectNameField = <Atomic.UIEditField>this.getWidget("project_name");
  36. this.appIDField = <Atomic.UIEditField>this.getWidget("app_id");
  37. this.projectLanguageField = <Atomic.UISelectDropdown>this.getWidget("project_language");
  38. this.image = <Atomic.UIImageWidget>this.getWidget("project_image");
  39. this.desktopButton = this.addPlatformButton("desktop", "AtomicEditor/editor/images/Desktop128.png");
  40. this.desktopButton.value = 1;
  41. this.androidButton = this.addPlatformButton("android", "AtomicEditor/editor/images/Android128.png");
  42. this.iosButton = this.addPlatformButton("ios", "AtomicEditor/editor/images/iOS128.png");
  43. this.html5Button = this.addPlatformButton("html5", "AtomicEditor/editor/images/HTML5128.png");
  44. if (!projectTemplate.screenshot)
  45. this.image.visibility = Atomic.UI_WIDGET_VISIBILITY.UI_WIDGET_VISIBILITY_GONE;
  46. else
  47. this.image.image = projectTemplate.screenshot;
  48. // If we're specifying where to put the project (initially), then we are opening
  49. // an example directly so use the same name
  50. if (projectPath) {
  51. this.projectNameField.text = projectTemplate.name;
  52. }
  53. this.projectPathField.text = (projectPath) ? projectPath : this.defaultPath;
  54. this.populateLanguageSelectionList();
  55. // Need to manually set the focus so the contents get auto-selected
  56. this.projectNameField.setFocus();
  57. this.resizeToFitContent();
  58. this.center();
  59. }
  60. addPlatformButton(platformName:string, platformLogo:string):Atomic.UIButton {
  61. var platformcontainer = <Atomic.UILayout>this.getWidget("platformcontainer");
  62. // IMAGE BUTTON
  63. var id = platformName;
  64. var size = 92;
  65. var button = new Atomic.UIButton();
  66. button.id = id;
  67. button.toggleMode = true;
  68. var lp = new Atomic.UILayoutParams();
  69. lp.minWidth = size;
  70. lp.minHeight = size;
  71. button.layoutParams = lp;
  72. button.gravity = Atomic.UI_GRAVITY.UI_GRAVITY_ALL;
  73. var image = new Atomic.UIImageWidget();
  74. image.image = platformLogo;
  75. var rect = [0, 0, size, size];
  76. image.rect = rect;
  77. button.addChild(image);
  78. if (platformName != "desktop") {
  79. var greenplus = new Atomic.UIImageWidget();
  80. greenplus.image = "AtomicEditor/editor/images/green_plus.png";
  81. rect = [size - 18, 2, size - 2, 18];
  82. greenplus.rect = rect;
  83. greenplus.visibility = Atomic.UI_WIDGET_VISIBILITY.UI_WIDGET_VISIBILITY_INVISIBLE;
  84. button.addChild(greenplus);
  85. button["greenPlus"] = greenplus;
  86. }
  87. platformcontainer.addChild(button);
  88. return button;
  89. }
  90. tryProjectCreate(): boolean {
  91. var name = this.projectNameField.text.trim();
  92. if (!name) {
  93. EditorUI.showModalError("New Project Editor Error", "Please enter a project name");
  94. return false;
  95. }
  96. var folder = this.projectPathField.text.trim();
  97. var projectPath = this.projectPath;
  98. // if we changed the project path, clear it
  99. if (folder != this.projectPath) {
  100. projectPath = "";
  101. }
  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 (!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 (!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.FileMode.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.FileMode.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(Editor.EditorLoadProjectEventType, { 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.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.UI_WIDGET_VISIBILITY_VISIBLE : Atomic.UI_WIDGET_VISIBILITY.UI_WIDGET_VISIBILITY_INVISIBLE;
  222. }
  223. }
  224. handleWidgetEvent(ev: Atomic.UIWidgetEvent) {
  225. if (ev.type == Atomic.UI_EVENT_TYPE.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. if (this.tryProjectCreate()) {
  240. let needsWrite = false;
  241. let prefs = Preferences.getInstance();
  242. if (prefs.editorFeatures.defaultLanguage != this.projectLanguageField.text) {
  243. prefs.editorFeatures.defaultLanguage = this.projectLanguageField.text;
  244. needsWrite = true;
  245. }
  246. if (prefs.editorFeatures.defaultPath != this.projectPathField.text) {
  247. prefs.editorFeatures.defaultPath = this.projectPathField.text;
  248. needsWrite = true;
  249. }
  250. if (needsWrite) prefs.write();
  251. this.hide();
  252. }
  253. return true;
  254. }
  255. } else if (ev.type == Atomic.UI_EVENT_TYPE.UI_EVENT_TYPE_CHANGED) {
  256. // handle language change
  257. if (ev.target.id == "project_language") {
  258. this.handleLanguageSwitch(this.projectLanguageField.text);
  259. }
  260. if (ev.target.id == "desktop") {
  261. // desktop is always selected
  262. this.desktopButton.value = 1;
  263. } else if (ev.target["greenPlus"]) {
  264. ev.target["greenPlus"].visibility = ev.target.value == 1 ? Atomic.UI_WIDGET_VISIBILITY.UI_WIDGET_VISIBILITY_VISIBLE : Atomic.UI_WIDGET_VISIBILITY.UI_WIDGET_VISIBILITY_INVISIBLE;
  265. }
  266. }
  267. }
  268. /**
  269. * Queries the json file for languages that are available to this template and populates the
  270. * list.
  271. */
  272. populateLanguageSelectionList() {
  273. this.projectLanguageFieldSource.clear();
  274. this.projectTemplate.languages.forEach(language => {
  275. this.projectLanguageFieldSource.addItem(new Atomic.UISelectItem(language));
  276. });
  277. this.projectLanguageField.source = this.projectLanguageFieldSource;
  278. this.projectLanguageField.value = 0;
  279. let defrank = -1;
  280. let jsrank = -1;
  281. let csrank = -1;
  282. let tsrank = -1;
  283. let ii = 0;
  284. for ( ii = 0; ii < this.projectLanguageFieldSource.getItemCount(); ii++ ) { // get rankings
  285. if ( this.projectLanguageFieldSource.getItemStr( ii ) == "JavaScript" ) jsrank = ii;
  286. if ( this.projectLanguageFieldSource.getItemStr( ii ) == "CSharp" ) csrank = ii;
  287. if ( this.projectLanguageFieldSource.getItemStr( ii ) == "TypeScript" ) tsrank = ii;
  288. }
  289. if ( this.defaultLanguage == "JavaScript" ) defrank = jsrank; // which is the default language
  290. if ( this.defaultLanguage == "CSharp" ) defrank = csrank;
  291. if ( this.defaultLanguage == "TypeScript" ) defrank = tsrank;
  292. if ( defrank > -1 ) this.projectLanguageField.value = defrank; // the default language is present
  293. else if ( jsrank > -1 ) this.projectLanguageField.value = jsrank; // js is present
  294. else if ( csrank > -1 ) this.projectLanguageField.value = csrank; // cs is present
  295. else if ( tsrank > -1 ) this.projectLanguageField.value = tsrank; // ts is present
  296. }
  297. projectPathField: Atomic.UIEditField;
  298. projectNameField: Atomic.UIEditField;
  299. appIDField: Atomic.UIEditField;
  300. projectLanguageField: Atomic.UISelectDropdown;
  301. projectLanguageFieldSource: Atomic.UISelectItemSource = new Atomic.UISelectItemSource();
  302. image: Atomic.UIImageWidget;
  303. desktopButton: Atomic.UIButton;
  304. androidButton: Atomic.UIButton;
  305. iosButton: Atomic.UIButton;
  306. html5Button: Atomic.UIButton;
  307. // if we have specified a projectPath, the dest will not be the combination of path + name
  308. projectPath: string;
  309. projectTemplate: ProjectTemplates.ProjectTemplateDefinition;
  310. defaultPath: string;
  311. defaultLanguage: string;
  312. }
  313. export = CreateProject;