UIResourceOps.ts 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. 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_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(EditorEvents.DeleteResourceNotification, 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_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. }
  93. handleWidgetEvent(ev: Atomic.UIWidgetEvent) {
  94. if (ev.type == Atomic.UI_EVENT_TYPE_CLICK) {
  95. var id = ev.target.id;
  96. if (id == "create") {
  97. var componentName = this.nameField.text;
  98. var outputFile = Atomic.addTrailingSlash(this.resourcePath) + componentName;
  99. // Check to see if we have a file extension. If we don't then assume .js
  100. if (outputFile.indexOf(".") == -1) {
  101. outputFile += ".js";
  102. }
  103. if (ResourceOps.CreateNewComponent(outputFile, componentName)) {
  104. this.hide();
  105. this.sendEvent(EditorEvents.EditResource, { path: outputFile });
  106. }
  107. return true;
  108. }
  109. if (id == "cancel") {
  110. this.hide();
  111. return true;
  112. }
  113. }
  114. }
  115. resourcePath: string;
  116. nameField: Atomic.UIEditField;
  117. }
  118. export class CreateScript extends ModalWindow {
  119. constructor(resourcePath: string) {
  120. super();
  121. this.resourcePath = resourcePath;
  122. this.init("New Script", "AtomicEditor/editor/ui/resourcecreatecomponent.tb.txt");
  123. this.nameField = <Atomic.UIEditField>this.getWidget("component_name");
  124. }
  125. handleWidgetEvent(ev: Atomic.UIWidgetEvent) {
  126. if (ev.type == Atomic.UI_EVENT_TYPE_CLICK) {
  127. var id = ev.target.id;
  128. if (id == "create") {
  129. var scriptName = this.nameField.text;
  130. var outputFile = Atomic.addTrailingSlash(this.resourcePath) + scriptName;
  131. // Check to see if we have a file extension. If we don't then assume .js
  132. if (outputFile.indexOf(".") == -1) {
  133. outputFile += ".js";
  134. }
  135. if (ResourceOps.CreateNewScript(outputFile, scriptName)) {
  136. this.hide();
  137. this.sendEvent(EditorEvents.EditResource, { path: outputFile });
  138. }
  139. return true;
  140. }
  141. if (id == "cancel") {
  142. this.hide();
  143. return true;
  144. }
  145. }
  146. }
  147. resourcePath: string;
  148. nameField: Atomic.UIEditField;
  149. }
  150. export class CreateScene extends ModalWindow {
  151. constructor(resourcePath: string) {
  152. super();
  153. this.resourcePath = resourcePath;
  154. this.init("New Scene", "AtomicEditor/editor/ui/resourcecreatecomponent.tb.txt");
  155. this.nameField = <Atomic.UIEditField>this.getWidget("component_name");
  156. }
  157. handleWidgetEvent(ev: Atomic.UIWidgetEvent) {
  158. if (ev.type == Atomic.UI_EVENT_TYPE_CLICK) {
  159. var id = ev.target.id;
  160. if (id == "create") {
  161. var sceneName = this.nameField.text;
  162. var outputFile = Atomic.addTrailingSlash(this.resourcePath) + sceneName;
  163. if (outputFile.indexOf(".scene") == -1) outputFile += ".scene";
  164. if (ResourceOps.CreateNewScene(outputFile, sceneName)) {
  165. this.hide();
  166. this.sendEvent(EditorEvents.EditResource, { path: outputFile });
  167. }
  168. return true;
  169. }
  170. if (id == "cancel") {
  171. this.hide();
  172. return true;
  173. }
  174. }
  175. }
  176. resourcePath: string;
  177. nameField: Atomic.UIEditField;
  178. }
  179. export class CreateMaterial extends ModalWindow {
  180. constructor(resourcePath: string) {
  181. super();
  182. this.resourcePath = resourcePath;
  183. this.init("New Material", "AtomicEditor/editor/ui/resourcecreatecomponent.tb.txt");
  184. this.nameField = <Atomic.UIEditField>this.getWidget("component_name");
  185. }
  186. handleWidgetEvent(ev: Atomic.UIWidgetEvent) {
  187. if (ev.type == Atomic.UI_EVENT_TYPE_CLICK) {
  188. var id = ev.target.id;
  189. if (id == "create") {
  190. var materialName = this.nameField.text;
  191. var outputFile = Atomic.addTrailingSlash(this.resourcePath) + materialName;
  192. if (outputFile.indexOf(".material") == -1) outputFile += ".material";
  193. if (ResourceOps.CreateNewMaterial(outputFile, materialName)) {
  194. this.hide();
  195. this.sendEvent(EditorEvents.EditResource, { path: outputFile });
  196. }
  197. return true;
  198. }
  199. if (id == "cancel") {
  200. this.hide();
  201. return true;
  202. }
  203. }
  204. }
  205. resourcePath: string;
  206. nameField: Atomic.UIEditField;
  207. }
  208. export class RenameAsset extends ModalWindow {
  209. constructor(asset: ToolCore.Asset) {
  210. super();
  211. this.asset = asset;
  212. this.init("Rename Resource", "AtomicEditor/editor/ui/renameasset.tb.txt");
  213. var currentName = <Atomic.UITextField>this.getWidget("current_name");
  214. this.nameEdit = <Atomic.UIEditField>this.getWidget("new_name");
  215. currentName.text = asset.name;
  216. this.nameEdit.text = asset.name;
  217. this.resizeToFitContent();
  218. this.center();
  219. }
  220. handleWidgetEvent(ev: Atomic.UIWidgetEvent) {
  221. if (ev.type == Atomic.UI_EVENT_TYPE_CLICK) {
  222. var id = ev.target.id;
  223. if (id == "rename") {
  224. this.hide();
  225. if (this.asset.name != this.nameEdit.text) {
  226. let oldPath = this.asset.path;
  227. this.asset.rename(this.nameEdit.text);
  228. let eventData: EditorEvents.RenameResourceEvent = {
  229. path: oldPath,
  230. newPath: this.asset.path,
  231. newName: this.nameEdit.text,
  232. asset: this.asset
  233. };
  234. this.sendEvent(EditorEvents.RenameResourceNotification, eventData);
  235. }
  236. return true;
  237. }
  238. if (id == "cancel") {
  239. this.hide();
  240. return true;
  241. }
  242. }
  243. }
  244. nameEdit: Atomic.UIEditField;
  245. asset: ToolCore.Asset;
  246. }