ProjectFrame.ts 18 KB

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