UIResourceOps.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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 ResourceOps = require("resources/ResourceOps");
  25. import ProjectTemplates = require("resources/ProjectTemplates");
  26. export class ResourceDelete extends ModalWindow {
  27. constructor(asset: ToolCore.Asset) {
  28. super();
  29. this.asset = asset;
  30. this.init("Delete Resource", "AtomicEditor/editor/ui/resourcedelete.tb.txt");
  31. var message = <Atomic.UIEditField>this.getWidget("message");
  32. var text = "Are you sure you want to delete resource:\n\n";
  33. text += asset.path;
  34. text += "\n\nThis operation cannot be undone";
  35. message.text = text;
  36. this.resizeToFitContent();
  37. this.center();
  38. }
  39. handleWidgetEvent(ev: Atomic.UIWidgetEvent) {
  40. if (ev.type == Atomic.UI_EVENT_TYPE.UI_EVENT_TYPE_CLICK) {
  41. var id = ev.target.id;
  42. if (id == "delete") {
  43. this.hide();
  44. let eventData = {
  45. path: this.asset.path
  46. };
  47. var db = ToolCore.getAssetDatabase();
  48. db.deleteAsset(this.asset);
  49. this.sendEvent(Editor.EditorDeleteResourceNotificationEventData(eventData));
  50. return true;
  51. }
  52. if (id == "cancel") {
  53. this.hide();
  54. return true;
  55. }
  56. }
  57. }
  58. asset: ToolCore.Asset;
  59. }
  60. export class CreateFolder extends ModalWindow {
  61. constructor(resourcePath: string) {
  62. super();
  63. this.resourcePath = resourcePath;
  64. this.init("New Folder", "AtomicEditor/editor/ui/resourcenewfolder.tb.txt");
  65. this.nameField = <Atomic.UIEditField>this.getWidget("folder_name");
  66. }
  67. handleWidgetEvent(ev: Atomic.UIWidgetEvent) {
  68. if (ev.type == Atomic.UI_EVENT_TYPE.UI_EVENT_TYPE_CLICK) {
  69. var id = ev.target.id;
  70. if (id == "create") {
  71. var resourcePath = Atomic.addTrailingSlash(this.resourcePath) + this.nameField.text;
  72. if (ResourceOps.CreateNewFolder(resourcePath)) {
  73. this.hide();
  74. }
  75. return true;
  76. }
  77. if (id == "cancel") {
  78. this.hide();
  79. return true;
  80. }
  81. }
  82. }
  83. resourcePath: string;
  84. nameField: Atomic.UIEditField;
  85. }
  86. export class CreateComponent extends ModalWindow {
  87. constructor(resourcePath: string) {
  88. super();
  89. this.resourcePath = resourcePath;
  90. this.init("New Component", "AtomicEditor/editor/ui/resourcecreatecomponent.tb.txt");
  91. this.nameField = <Atomic.UIEditField>this.getWidget("component_name");
  92. this.templateField = <Atomic.UISelectDropdown>this.getWidget("template_list");
  93. this.loadTemplatesList();
  94. }
  95. /**
  96. *
  97. * Gets the template definitions and loads it up
  98. */
  99. loadTemplatesList() {
  100. this.templates = ProjectTemplates.GetNewFileTemplateDefinitions("component");
  101. this.templateFieldSource.clear();
  102. this.templates.forEach( template => {
  103. this.templateFieldSource.addItem(new Atomic.UISelectItem(template.name));
  104. });
  105. this.templateField.source = this.templateFieldSource;
  106. this.templateField.value = 0;
  107. }
  108. handleWidgetEvent(ev: Atomic.UIWidgetEvent) {
  109. if (ev.type == Atomic.UI_EVENT_TYPE.UI_EVENT_TYPE_CLICK) {
  110. var id = ev.target.id;
  111. if (id == "create") {
  112. var componentName = this.nameField.text;
  113. var outputFile = Atomic.addTrailingSlash(this.resourcePath) + componentName;
  114. let selectedTemplate : Editor.Templates.FileTemplateDefinition = null;
  115. this.templates.forEach(t => {
  116. if (t.name == this.templateField.text) {
  117. selectedTemplate = t;
  118. }
  119. });
  120. if (selectedTemplate) {
  121. // Check to see if we have a file extension. If we don't then use the one defined in the template
  122. if (outputFile.indexOf(".") == -1) {
  123. outputFile += selectedTemplate.ext;
  124. }
  125. if (ResourceOps.CreateNewComponent(outputFile, componentName, selectedTemplate)) {
  126. this.hide();
  127. this.sendEvent(Editor.EditorEditResourceEventData({ path: outputFile, lineNumber: 0 }));
  128. }
  129. return true;
  130. } else {
  131. return false;
  132. }
  133. }
  134. if (id == "cancel") {
  135. this.hide();
  136. return true;
  137. }
  138. }
  139. }
  140. templates: Editor.Templates.FileTemplateDefinition[];
  141. resourcePath: string;
  142. nameField: Atomic.UIEditField;
  143. templateField: Atomic.UISelectDropdown;
  144. templateFieldSource: Atomic.UISelectItemSource = new Atomic.UISelectItemSource();
  145. }
  146. export class CreateScript extends ModalWindow {
  147. constructor(resourcePath: string) {
  148. super();
  149. this.resourcePath = resourcePath;
  150. this.init("New Script", "AtomicEditor/editor/ui/resourcecreatescript.tb.txt");
  151. this.nameField = <Atomic.UIEditField>this.getWidget("script_name");
  152. this.templateField = <Atomic.UISelectDropdown>this.getWidget("template_list");
  153. this.loadTemplatesList();
  154. }
  155. /**
  156. *
  157. * Gets the template definitions and loads it up
  158. */
  159. loadTemplatesList() {
  160. this.templates = ProjectTemplates.GetNewFileTemplateDefinitions("script");
  161. this.templateFieldSource.clear();
  162. this.templates.forEach( template => {
  163. this.templateFieldSource.addItem(new Atomic.UISelectItem(template.name));
  164. });
  165. this.templateField.source = this.templateFieldSource;
  166. this.templateField.value = 0;
  167. }
  168. handleWidgetEvent(ev: Atomic.UIWidgetEvent) {
  169. if (ev.type == Atomic.UI_EVENT_TYPE.UI_EVENT_TYPE_CLICK) {
  170. var id = ev.target.id;
  171. if (id == "create") {
  172. var scriptName = this.nameField.text;
  173. var outputFile = Atomic.addTrailingSlash(this.resourcePath) + scriptName;
  174. let selectedTemplate : Editor.Templates.FileTemplateDefinition = null;
  175. this.templates.forEach(t => {
  176. if (t.name == this.templateField.text) {
  177. selectedTemplate = t;
  178. }
  179. });
  180. if (selectedTemplate) {
  181. // Check to see if we have a file extension. If we don't then use the one defined in the template
  182. if (outputFile.lastIndexOf(`.${selectedTemplate.ext}`) != outputFile.length - selectedTemplate.ext.length) {
  183. outputFile += selectedTemplate.ext;
  184. }
  185. if (ResourceOps.CreateNewScript(outputFile, scriptName, selectedTemplate)) {
  186. this.hide();
  187. this.sendEvent(Editor.EditorEditResourceEventData({ path: outputFile, lineNumber: 0 }));
  188. }
  189. return true;
  190. } else {
  191. return false;
  192. }
  193. }
  194. if (id == "cancel") {
  195. this.hide();
  196. return true;
  197. }
  198. }
  199. }
  200. templates: Editor.Templates.FileTemplateDefinition[];
  201. resourcePath: string;
  202. nameField: Atomic.UIEditField;
  203. templateField: Atomic.UISelectDropdown;
  204. templateFieldSource: Atomic.UISelectItemSource = new Atomic.UISelectItemSource();
  205. }
  206. export class CreateScene extends ModalWindow {
  207. constructor(resourcePath: string) {
  208. super();
  209. this.resourcePath = resourcePath;
  210. this.init("New Scene", "AtomicEditor/editor/ui/resourcecreateresource.tb.txt");
  211. this.nameField = <Atomic.UIEditField>this.getWidget("component_name");
  212. }
  213. handleWidgetEvent(ev: Atomic.UIWidgetEvent) {
  214. if (ev.type == Atomic.UI_EVENT_TYPE.UI_EVENT_TYPE_CLICK) {
  215. var id = ev.target.id;
  216. if (id == "create") {
  217. var sceneName = this.nameField.text;
  218. var outputFile = Atomic.addTrailingSlash(this.resourcePath) + sceneName;
  219. if (outputFile.indexOf(".scene") == -1) outputFile += ".scene";
  220. if (ResourceOps.CreateNewScene(outputFile, sceneName)) {
  221. this.hide();
  222. this.sendEvent(Editor.EditorEditResourceEventData({ path: outputFile, lineNumber: 0 }));
  223. }
  224. return true;
  225. }
  226. if (id == "cancel") {
  227. this.hide();
  228. return true;
  229. }
  230. }
  231. }
  232. resourcePath: string;
  233. nameField: Atomic.UIEditField;
  234. }
  235. export class CreateMaterial extends ModalWindow {
  236. constructor(resourcePath: string) {
  237. super();
  238. this.resourcePath = resourcePath;
  239. this.init("New Material", "AtomicEditor/editor/ui/resourcecreateresource.tb.txt");
  240. this.nameField = <Atomic.UIEditField>this.getWidget("component_name");
  241. }
  242. handleWidgetEvent(ev: Atomic.UIWidgetEvent) {
  243. if (ev.type == Atomic.UI_EVENT_TYPE.UI_EVENT_TYPE_CLICK) {
  244. var id = ev.target.id;
  245. if (id == "create") {
  246. var materialName = this.nameField.text;
  247. var outputFile = Atomic.addTrailingSlash(this.resourcePath) + materialName;
  248. if (outputFile.indexOf(".material") == -1) outputFile += ".material";
  249. if (ResourceOps.CreateNewMaterial(outputFile, materialName)) {
  250. this.hide();
  251. this.sendEvent(Editor.EditorEditResourceEventData({ path: outputFile, lineNumber: 0 }));
  252. }
  253. return true;
  254. }
  255. if (id == "cancel") {
  256. this.hide();
  257. return true;
  258. }
  259. }
  260. }
  261. resourcePath: string;
  262. nameField: Atomic.UIEditField;
  263. }
  264. export class RenameAsset extends ModalWindow {
  265. constructor(asset: ToolCore.Asset) {
  266. super();
  267. this.asset = asset;
  268. this.init("Rename Resource", "AtomicEditor/editor/ui/renameasset.tb.txt");
  269. var currentName = <Atomic.UITextField>this.getWidget("current_name");
  270. this.nameEdit = <Atomic.UIEditField>this.getWidget("new_name");
  271. currentName.text = asset.name;
  272. this.nameEdit.text = asset.name;
  273. this.resizeToFitContent();
  274. this.center();
  275. this.nameEdit.setFocus();
  276. }
  277. handleWidgetEvent(ev: Atomic.UIWidgetEvent) {
  278. if (ev.type == Atomic.UI_EVENT_TYPE.UI_EVENT_TYPE_CLICK) {
  279. var id = ev.target.id;
  280. if (id == "rename") {
  281. this.hide();
  282. if (this.asset.name != this.nameEdit.text) {
  283. let oldPath = this.asset.path;
  284. this.asset.rename(this.nameEdit.text);
  285. let eventData: Editor.EditorRenameResourceNotificationEvent = {
  286. path: oldPath,
  287. newPath: this.asset.path,
  288. newName: this.nameEdit.text,
  289. asset: this.asset
  290. };
  291. this.sendEvent(Editor.EditorRenameResourceNotificationEventData(eventData));
  292. }
  293. return true;
  294. }
  295. if (id == "cancel") {
  296. this.hide();
  297. return true;
  298. }
  299. }
  300. }
  301. nameEdit: Atomic.UIEditField;
  302. asset: ToolCore.Asset;
  303. }