ProjectFrame.ts 16 KB

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