ProjectFrame.ts 18 KB

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