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