ProjectFrame.ts 19 KB

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