ProjectFrame.ts 18 KB

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