CreateProject.ts 15 KB

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