ProjectFrame.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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 ScriptWidget = require("ui/ScriptWidget");
  23. import Editor = require("editor/Editor");
  24. import EditorEvents = require("editor/EditorEvents");
  25. import ProjectFrameMenu = require("./menus/ProjectFrameMenu");
  26. import MenuItemSources = require("./menus/MenuItemSources");
  27. import ServiceLocator from "../../extensionServices/ServiceLocator";
  28. class ProjectFrame extends ScriptWidget {
  29. folderList: Atomic.UIListView;
  30. menu: ProjectFrameMenu;
  31. currentFolder: ToolCore.Asset;
  32. resourceFolder: ToolCore.Asset;
  33. assetGUIDToItemID = {};
  34. resourcesID: number = -1;
  35. assetReferencePath: string = null;
  36. currentReferencedButton: Atomic.UIButton = null;
  37. containerScrollToHeight: number;
  38. containerScrollToHeightCounter: number;
  39. constructor(parent: Atomic.UIWidget) {
  40. super();
  41. this.menu = new ProjectFrameMenu();
  42. this.load("AtomicEditor/editor/ui/projectframe.tb.txt");
  43. this.gravity = Atomic.UI_GRAVITY_TOP_BOTTOM;
  44. var projectviewcontainer = parent.getWidget("projectviewcontainer");
  45. projectviewcontainer.addChild(this);
  46. var foldercontainer = this.getWidget("foldercontainer");
  47. var folderList = this.folderList = new Atomic.UIListView();
  48. folderList.rootList.id = "folderList_";
  49. foldercontainer.addChild(folderList);
  50. // events
  51. this.subscribeToEvent("ProjectLoaded", (data) => this.handleProjectLoaded(data));
  52. this.subscribeToEvent("ProjectUnloaded", (data) => this.handleProjectUnloaded(data));
  53. this.subscribeToEvent("DragEnded", (data: Atomic.DragEndedEvent) => this.handleDragEnded(data));
  54. this.subscribeToEvent("ResourceAdded", (ev: ToolCore.ResourceAddedEvent) => this.handleResourceAdded(ev));
  55. this.subscribeToEvent("ResourceRemoved", (ev: ToolCore.ResourceRemovedEvent) => this.handleResourceRemoved(ev));
  56. this.subscribeToEvent(EditorEvents.DeleteResource, (ev: EditorEvents.DeleteResourceEvent) => this.handleDeleteResource(ev));
  57. this.subscribeToEvent("AssetRenamed", (ev: ToolCore.AssetRenamedEvent) => this.handleAssetRenamed(ev));
  58. this.subscribeToEvent(EditorEvents.InspectorProjectReference, (ev: EditorEvents.InspectorProjectReferenceEvent) => { this.handleInspectorProjectReferenceHighlight(ev.path) });
  59. folderList.subscribeToEvent("UIListViewSelectionChanged", (event: Atomic.UIListViewSelectionChangedEvent) => this.handleFolderListSelectionChangedEvent(event));
  60. // this.subscribeToEvent(EditorEvents.ResourceFolderCreated, (ev: EditorEvents.ResourceFolderCreatedEvent) => this.handleResourceFolderCreated(ev));
  61. // this uses FileWatcher which doesn't catch subfolder creation
  62. this.subscribeToEvent("FileChanged", (data) => {
  63. // console.log("File CHANGED! ", data.fileName);
  64. });
  65. }
  66. handleAssetRenamed(ev: ToolCore.AssetRenamedEvent) {
  67. var container: Atomic.UILayout = <Atomic.UILayout>this.getWidget("contentcontainer");
  68. for (var widget = container.firstChild; widget; widget = widget.next) {
  69. if (widget.id == ev.asset.guid) {
  70. if (widget["assetButton"]) {
  71. widget["assetButton"].text = ev.asset.name + ev.asset.extension;
  72. widget["assetButton"].dragObject = new Atomic.UIDragObject(ev.asset, ev.asset.name);
  73. }
  74. break;
  75. }
  76. }
  77. }
  78. /**
  79. * Called when the user deletes a resource
  80. * @param {EditorEvents.DeleteResourceEvent} ev
  81. */
  82. handleDeleteResource(ev: EditorEvents.DeleteResourceEvent) {
  83. var db = ToolCore.getAssetDatabase();
  84. db.deleteAsset(ev.asset);
  85. ServiceLocator.resourceServices.deleteResource(ev);
  86. }
  87. handleResourceRemoved(ev: ToolCore.ResourceRemovedEvent) {
  88. var folderList = this.folderList;
  89. let asset = ToolCore.assetDatabase.getAssetByGUID(ev.guid);
  90. if (asset) {
  91. ServiceLocator.resourceServices.deleteResource({ path: asset.path, asset: asset });
  92. }
  93. folderList.deleteItemByID(ev.guid);
  94. var container: Atomic.UILayout = <Atomic.UILayout>this.getWidget("contentcontainer");
  95. for (var widget = container.firstChild; widget; widget = widget.next) {
  96. if (widget.id == ev.guid) {
  97. container.removeChild(widget);
  98. break;
  99. }
  100. }
  101. }
  102. handleResourceAdded(ev: ToolCore.ResourceAddedEvent) {
  103. var db = ToolCore.getAssetDatabase();
  104. var asset = db.getAssetByGUID(ev.guid);
  105. var parent = asset.parent;
  106. var folderList = this.folderList;
  107. // these can be out of order
  108. if (asset.isFolder()) {
  109. if (!parent) {
  110. var id = folderList.addRootItem(asset.name, "Folder.icon", asset.guid);
  111. this.resourcesID = id;
  112. this.assetGUIDToItemID[asset.guid] = id;
  113. this.resourceFolder = asset;
  114. } else {
  115. var parentItemID = this.assetGUIDToItemID[parent.guid];
  116. var id = folderList.addChildItem(parentItemID, asset.name, "Folder.icon", asset.guid);
  117. this.assetGUIDToItemID[asset.guid] = id;
  118. }
  119. } else if (parent == this.currentFolder) {
  120. var container: Atomic.UILayout = <Atomic.UILayout>this.getWidget("contentcontainer");
  121. container.addChild(this.createButtonLayout(asset));
  122. }
  123. }
  124. handleWidgetEvent(data: Atomic.UIWidgetEvent): boolean {
  125. if (data.type == Atomic.UI_EVENT_TYPE_RIGHT_POINTER_UP) {
  126. var id = data.target.id;
  127. var db = ToolCore.getAssetDatabase();
  128. var asset: ToolCore.Asset;
  129. if (id == "folderList_")
  130. asset = db.getAssetByGUID(this.folderList.hoverItemID);
  131. else
  132. asset = db.getAssetByGUID(id);
  133. if (asset) {
  134. this.menu.createAssetContextMenu(this, asset, data.x, data.y);
  135. return true;
  136. }
  137. }
  138. if (data.type == Atomic.UI_EVENT_TYPE_CLICK) {
  139. var id = data.target.id;
  140. if (this.menu.handlePopupMenu(data.target, data.refid))
  141. return true;
  142. // create
  143. if (id == "menu create") {
  144. if (!ToolCore.toolSystem.project) return;
  145. var src = MenuItemSources.getMenuItemSource("project create items");
  146. var menu = new Atomic.UIMenuWindow(data.target, "create popup");
  147. menu.show(src);
  148. return true;
  149. }
  150. var db = ToolCore.getAssetDatabase();
  151. var fs = Atomic.getFileSystem();
  152. if (data.target && data.target.id.length) {
  153. if (id == "folderList_") {
  154. var list = <Atomic.UISelectList>data.target;
  155. var selectedId = list.selectedItemID;
  156. // selectedId == 0 = root "Resources"
  157. if (selectedId != "0") {
  158. var asset = db.getAssetByGUID(selectedId);
  159. if (asset.isFolder)
  160. this.refreshContent(asset);
  161. }
  162. return true;
  163. }
  164. var asset = db.getAssetByGUID(id);
  165. if (asset) {
  166. if (asset.isFolder()) {
  167. this.folderList.selectItemByID(id);
  168. this.refreshContent(asset);
  169. } else {
  170. this.sendEvent(EditorEvents.EditResource, { "path": asset.path });
  171. }
  172. }
  173. }
  174. if (this.currentReferencedButton) {
  175. this.currentReferencedButton.setState(4, false);
  176. this.currentReferencedButton = null;
  177. }
  178. }
  179. return false;
  180. }
  181. rescan(asset: ToolCore.Asset) {
  182. var db = ToolCore.getAssetDatabase();
  183. db.scan();
  184. }
  185. selectPath(path: string) {
  186. var db = ToolCore.getAssetDatabase();
  187. var asset = db.getAssetByPath(path);
  188. if (!asset)
  189. return;
  190. this.folderList.selectItemByID(asset.guid);
  191. }
  192. handleFolderListSelectionChangedEvent(event: Atomic.UIListViewSelectionChangedEvent) {
  193. var selectedId = this.folderList.selectedItemID;
  194. if (selectedId != "0") {
  195. var db = ToolCore.getAssetDatabase();
  196. var asset = db.getAssetByGUID(selectedId);
  197. if (!asset)
  198. return;
  199. if (asset.isFolder)
  200. this.refreshContent(asset);
  201. }
  202. }
  203. handleDragEnded(data: Atomic.DragEndedEvent) {
  204. var asset: ToolCore.Asset;
  205. if (data.target) {
  206. var container: Atomic.UILayout = <Atomic.UILayout>this.getWidget("contentcontainer");
  207. if (data.target.id == "contentcontainerscroll" || container.isAncestorOf(data.target)) {
  208. if (data.target["asset"])
  209. asset = <ToolCore.Asset>data.target["asset"];
  210. if (!asset || !asset.isFolder)
  211. asset = this.currentFolder;
  212. }
  213. }
  214. if (!asset) {
  215. // if the drop target is the folderList's root select widget
  216. var rootList = this.folderList.rootList;
  217. var hoverID = rootList.hoverItemID;
  218. if (hoverID == "")
  219. return;
  220. var db = ToolCore.getAssetDatabase();
  221. asset = db.getAssetByGUID(hoverID);
  222. }
  223. if (!asset || !asset.isFolder)
  224. return;
  225. var dragObject = data.dragObject;
  226. if (dragObject.object && dragObject.object.typeName == "Node") {
  227. var node = <Atomic.Node>dragObject.object;
  228. var prefabComponent = <Atomic.PrefabComponent>node.getComponent("PrefabComponent");
  229. if (prefabComponent) {
  230. prefabComponent.savePrefab();
  231. }
  232. else {
  233. var destFilename = Atomic.addTrailingSlash(asset.path);
  234. destFilename += node.name + ".prefab";
  235. var file = new Atomic.File(destFilename, Atomic.FILE_WRITE);
  236. node.saveXML(file);
  237. file.close();
  238. }
  239. this.rescan(asset);
  240. return;
  241. } else if (dragObject.object && dragObject.object.typeName == "Asset") {
  242. var dragAsset = <ToolCore.Asset>dragObject.object;
  243. // get the folder we dragged on
  244. var destPath = Atomic.addTrailingSlash(asset.path);
  245. dragAsset.move(destPath + dragAsset.name + dragAsset.extension);
  246. this.refreshContent(this.currentFolder);
  247. return true;
  248. }
  249. // dropped some files?
  250. var filenames = dragObject.filenames;
  251. if (!filenames.length)
  252. return;
  253. var fileSystem = Atomic.getFileSystem();
  254. for (var i in filenames) {
  255. var srcFilename = filenames[i];
  256. var pathInfo = Atomic.splitPath(srcFilename);
  257. var destFilename = Atomic.addTrailingSlash(asset.path);
  258. destFilename += pathInfo.fileName + pathInfo.ext;
  259. fileSystem.copy(srcFilename, destFilename);
  260. }
  261. this.rescan(asset);
  262. }
  263. handleProjectLoaded(data) {
  264. this.folderList.rootList.value = 0;
  265. this.folderList.setExpanded(this.resourcesID, true);
  266. this.refreshContent(this.resourceFolder);
  267. }
  268. handleProjectUnloaded(data) {
  269. this.folderList.deleteAllItems();
  270. this.resourceFolder = null;
  271. var container: Atomic.UILayout = <Atomic.UILayout>this.getWidget("contentcontainer");
  272. container.deleteAllChildren();
  273. }
  274. // Shows referenced file in projectframe
  275. handleInspectorProjectReferenceHighlight(path: string): void {
  276. this.assetReferencePath = path;
  277. var db = ToolCore.getAssetDatabase();
  278. var asset = db.getAssetByPath(this.resourceFolder.getPath() + "/" + path);
  279. this.folderList.selectAllItems(false);
  280. this.folderList.selectItemByID(asset.parent.guid, true);
  281. this.refreshContent(asset.parent);
  282. this.folderList.scrollToSelectedItem();
  283. }
  284. private refreshContent(folder: ToolCore.Asset) {
  285. if (this.currentFolder != folder) {
  286. this.sendEvent(EditorEvents.ContentFolderChanged, { path: folder.path });
  287. }
  288. this.currentFolder = folder;
  289. var db = ToolCore.getAssetDatabase();
  290. var container: Atomic.UILayout = <Atomic.UILayout>this.getWidget("contentcontainer");
  291. container.deleteAllChildren();
  292. var assets = db.getFolderAssets(folder.path);
  293. this.containerScrollToHeightCounter = 0;
  294. for (var i in assets) {
  295. var asset = assets[i];
  296. container.addChild(this.createButtonLayout(asset));
  297. this.containerScrollToHeightCounter++;
  298. }
  299. var containerScroll: Atomic.UIScrollContainer = <Atomic.UIScrollContainer>this.getWidget("contentcontainerscroll");
  300. containerScroll.scrollTo(0, this.containerScrollToHeight);
  301. }
  302. private createButtonLayout(asset: ToolCore.Asset): Atomic.UILayout {
  303. var system = ToolCore.getToolSystem();
  304. var project = system.project;
  305. var fs = Atomic.getFileSystem();
  306. var pathinfo = Atomic.splitPath(asset.path);
  307. var bitmapID = "Folder.icon";
  308. if (fs.fileExists(asset.path)) {
  309. bitmapID = "FileBitmap";
  310. }
  311. if (pathinfo.ext == ".js") {
  312. if (project.isComponentsDirOrFile(asset.path)) {
  313. bitmapID = "ComponentBitmap";
  314. }
  315. else {
  316. bitmapID = "JavascriptBitmap";
  317. }
  318. }
  319. var blayout = new Atomic.UILayout();
  320. blayout.id = asset.guid;
  321. blayout.gravity = Atomic.UI_GRAVITY_LEFT;
  322. var spacer = new Atomic.UIWidget();
  323. spacer.rect = [0, 0, 8, 8];
  324. blayout.addChild(spacer);
  325. var button = new Atomic.UIButton();
  326. // setup the drag object
  327. button.dragObject = new Atomic.UIDragObject(asset, asset.name);
  328. var lp = new Atomic.UILayoutParams;
  329. var buttonHeight = lp.height = 20;
  330. //Get the path of the button and compare it to the asset's path to highlight
  331. var resourcePath = this.resourceFolder.getPath() + "/" + this.assetReferencePath;
  332. //Highlight Button UI
  333. if (resourcePath == asset.path) {
  334. button.setState(4, true);
  335. this.currentReferencedButton = button;
  336. this.containerScrollToHeight = this.containerScrollToHeightCounter * buttonHeight;
  337. }
  338. var fd = new Atomic.UIFontDescription();
  339. fd.id = "Vera";
  340. fd.size = 11;
  341. button.gravity = Atomic.UI_GRAVITY_LEFT;
  342. var image = new Atomic.UISkinImage(bitmapID);
  343. image.rect = [0, 0, 12, 12];
  344. image.gravity = Atomic.UI_GRAVITY_RIGHT;
  345. blayout.addChild(image);
  346. image["asset"] = asset;
  347. button.id = asset.guid;
  348. button.layoutParams = lp;
  349. button.fontDescription = fd;
  350. button.text = asset.name + asset.extension;
  351. button.skinBg = "TBButton.flat";
  352. button["asset"] = asset;
  353. blayout["assetButton"] = button;
  354. blayout.addChild(button);
  355. return blayout;
  356. }
  357. }
  358. export = ProjectFrame;