ProjectFrame.ts 18 KB

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