ProjectFrame.ts 19 KB

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