CreateProject.ts 15 KB

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