FindDependencyQueryWnd.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. import FileUtils from "./FileUtils";
  2. import UIWindow, { WindData } from "./UIWindow";
  3. const FairyGUI = CS.FairyGUI
  4. const FairyEditor = CS.FairyEditor
  5. const System = CS.System
  6. const App = FairyEditor.App;
  7. type resListData = {
  8. item: CS.FairyEditor.FPackageItem
  9. querts: CS.FairyEditor.FPackage[]
  10. }
  11. export default class FindDependencyQueryWnd extends UIWindow {
  12. constructor() {
  13. super(new WindData("Extend", "FindDependencyQuery"))
  14. }
  15. static show() {
  16. let url = FileUtils.joinUrl("Extend", "FindDependencyQuery")
  17. if (UIWindow.FindUrl(url) == undefined) {
  18. UIWindow.add(new this)
  19. }
  20. super.show(url)
  21. }
  22. private projectCount: CS.FairyGUI.GTextField;
  23. private projectList: CS.FairyGUI.GList;
  24. private yilaiList: CS.FairyGUI.GList;
  25. private yilaiCount: CS.FairyGUI.GTextField;
  26. private resList: CS.FairyGUI.GList;
  27. private resCount: CS.FairyGUI.GTextField;
  28. private externalCount: CS.FairyGUI.GTextField;
  29. private externalList: CS.FairyGUI.GList;
  30. private close: CS.FairyGUI.GButton;
  31. //依赖选中的包体集合
  32. private yilaiSelectPkgs = new Array<CS.FairyEditor.FPackage>();
  33. //点击锁定查询的pkgs
  34. private lockPkgs = new Array<CS.FairyEditor.FPackage>();
  35. private yilaiCB: CS.FairyGUI.GButton
  36. private state: CS.FairyGUI.Controller;
  37. private progress: CS.FairyGUI.GProgressBar;
  38. protected onInit(): void {
  39. super.onInit();
  40. let self = this;
  41. this.projectCount = this.contentPane.GetChild("projectCount").asTextField;
  42. this.yilaiCount = this.contentPane.GetChild("yilaiCount").asTextField;
  43. this.resCount = this.contentPane.GetChild("resCount").asTextField;
  44. this.projectList = this.contentPane.GetChild("projectList").asList;
  45. this.yilaiList = this.contentPane.GetChild("yilaiList").asList;
  46. this.resList = this.contentPane.GetChild("resList").asList;
  47. this.externalCount = this.contentPane.GetChild("externalCount").asTextField;
  48. this.externalList = this.contentPane.GetChild("externalList").asList;
  49. this.state = this.contentPane.GetController("state")
  50. this.progress = this.contentPane.GetChild("progress").asProgress;
  51. this.contentPane.GetChild("close").onClick.Add(this.onCloseClick.bind(this));
  52. this.yilaiCB = this.contentPane.GetChild("yilaiCB").asButton;
  53. this.yilaiCB.onClick.Add(() => {
  54. self.onUpdateShown();
  55. })
  56. //复选框: 发布清除的资源不显示
  57. this.yilaiCB.visible = false;
  58. this.projectList.itemRenderer = this.onProjectListRenderer.bind(this);
  59. this.yilaiList.itemRenderer = this.onYiLaiListItemRenderer.bind(this);
  60. this.projectList.onClickItem.Add(this.onProjectListClickItem.bind(this));
  61. this.yilaiList.onClickItem.Add(this.onYiLaiListClickItem.bind(this));
  62. this.resList.itemRenderer = this.onResListItemRenderer.bind(this);
  63. this.resList.onClickItem.Add(this.onResListClickItem.bind(this));
  64. this.externalList.itemRenderer = this.onExternalListItemRenderer.bind(this);
  65. this.externalList.onClickItem.Add(this.onExternalListClickItem.bind(this));
  66. this.resList.SetVirtual();
  67. }
  68. protected onShown(): void {
  69. super.onShown();
  70. this.onUpdateShown()
  71. this.yilaiCB.selected = false;
  72. }
  73. protected onHide(): void {
  74. super.onHide();
  75. this.projectList.numItems = 0;
  76. this.yilaiList.numItems = 0;
  77. this.projectList.data = null;
  78. this.yilaiList.data = null;
  79. this.resList.numItems = 0;
  80. this.resList.data = null;
  81. this.externalList.data = null;
  82. this.externalList.numItems = 0;
  83. }
  84. private onUpdateShown() {
  85. //获取项目包体
  86. let pkgs = []
  87. for (let index = 0; index < App.project.allPackages.Count; index++) {
  88. pkgs.push(App.project.allPackages.get_Item(index))
  89. }
  90. this.projectList.data = pkgs;
  91. this.projectList.numItems = pkgs.length;
  92. this.projectCount.templateVars = this.getTemplateVars(["value"], [pkgs.length.toString()]);
  93. }
  94. private onProjectListRenderer(index: number, obj: CS.FairyGUI.GButton) {
  95. let data = this.projectList.data[index] as CS.FairyEditor.FPackage;
  96. obj.text = data.name
  97. obj.data = data;
  98. obj.__onDispose = () => {
  99. obj.text = "";
  100. obj.data = null;
  101. }
  102. }
  103. /**点击项目包体 查找对应的包体总依赖 */
  104. private async onProjectListClickItem(item: CS.FairyGUI.EventContext) {
  105. this.state.selectedIndex = 1;
  106. let querys = new Array<CS.FairyEditor.FPackage>();
  107. let pkg = item.data.data as CS.FairyEditor.FPackage
  108. let count = pkg.items.Count
  109. this.progress.max = count;
  110. this.progress.value = 0;
  111. let list3Array = new Array<resListData>();
  112. let map = new Map<string, CS.FairyEditor.FPackage>();
  113. for (let index = 0; index < count; index++) {
  114. let item = pkg.items.get_Item(index);
  115. this.progress.value = index;
  116. if (index % 50 == 0) {
  117. await FileUtils.sleep(0)
  118. }
  119. //未导出资源 不查找
  120. if (!item.exported) continue;
  121. if (item.type != FairyEditor.FPackageItemType.COMPONENT) {
  122. continue
  123. }
  124. if (this.yilaiCB.selected) {
  125. FileUtils.isClearOnPublish(item, map, 0)
  126. } else {
  127. // //获取包体对应的依赖
  128. let list = FileUtils.createGenericList<CS.FairyEditor.FPackageItem>(FairyEditor.FPackageItem)
  129. list.Add(item)
  130. let data = new FairyEditor.DependencyQuery()
  131. data.QueryDependencies(list, FairyEditor.DependencyQuery.SeekLevel.ALL)
  132. for (let index = 0; index < data.references.Count; index++) {
  133. let reference = data.references.get_Item(index)
  134. let pkgID = reference.pkgId
  135. let yilaipkg = App.project.GetPackage(pkgID)
  136. if (yilaipkg == null) continue
  137. if (yilaipkg.GetItem(reference.itemId) && querys.indexOf(yilaipkg) == -1) {
  138. querys.push(yilaipkg)
  139. }
  140. }
  141. }
  142. list3Array.push({
  143. item: item,
  144. querts: null
  145. })
  146. }
  147. this.clearSelectPkgs();
  148. this.lockPkgs = []
  149. if (this.yilaiCB.selected) {
  150. map.forEach((v, k) => {
  151. App.consoleView.Log(v.name)
  152. }, this)
  153. querys = Array.from(map).map(v => v[1])
  154. }
  155. this.yilaiList.data = querys;
  156. this.yilaiList.numItems = querys.length;
  157. this.yilaiCount.templateVars = this.getTemplateVars(["value"], [querys.length.toString()]);
  158. this.resList.data = list3Array;
  159. this.resList.numItems = list3Array.length;
  160. this.resCount.templateVars = this.getTemplateVars(["value"], [list3Array.length.toString()]);
  161. this.externalList.numItems = null;
  162. this.externalList.data = null;
  163. this.externalCount.templateVars = this.getTemplateVars(["value"], ['0']);
  164. this.state.selectedIndex = 0;
  165. }
  166. /**获取资源点击的依赖包体 */
  167. private getResYiLaiArray(item: CS.FairyEditor.FPackageItem) {
  168. let querys = new Array<CS.FairyEditor.FPackage>();
  169. let list = FileUtils.createGenericList<CS.FairyEditor.FPackageItem>(FairyEditor.FPackageItem)
  170. list.Add(item)
  171. let data = new FairyEditor.DependencyQuery()
  172. data.QueryDependencies(list, FairyEditor.DependencyQuery.SeekLevel.ALL)
  173. for (let index = 0; index < data.references.Count; index++) {
  174. let reference = data.references.get_Item(index)
  175. let pkgID = reference.pkgId
  176. let yilaipkg = App.project.GetPackage(pkgID)
  177. if (yilaipkg == null) continue
  178. if (yilaipkg.GetItem(reference.itemId) && querys.indexOf(yilaipkg) == -1) {
  179. querys.push(yilaipkg)
  180. }
  181. }
  182. return querys
  183. }
  184. private onResListItemRenderer(index: number, obj: CS.FairyGUI.GButton) {
  185. let data = this.resList.data[index] as resListData;
  186. obj.text = data.item.name
  187. obj.data = data;
  188. if (this.yilaiCB.selected) {
  189. let map = new Map<string, CS.FairyEditor.FPackage>();
  190. FileUtils.isClearOnPublish(data.item, map, 0)
  191. data.querts = Array.from(map).map(x => x[1])
  192. } else {
  193. data.querts = this.getResYiLaiArray(data.item)
  194. }
  195. let state = 0
  196. if (this.yilaiSelectPkgs.length != 0) {
  197. if (this.isResInYiLai(obj, this.yilaiSelectPkgs)) {
  198. state = 1
  199. } else {
  200. state = 2
  201. }
  202. }
  203. obj.asCom.GetController("state").selectedIndex = state
  204. obj.__onDispose = () => {
  205. obj.text = "";
  206. obj.data = null;
  207. }
  208. }
  209. private onResListClickItem(obj: CS.FairyGUI.EventContext) {
  210. let data = obj.data.data as resListData
  211. let item = data.item
  212. this.clearSelectPkgs();
  213. this.yilaiList.data = data.querts;
  214. this.yilaiList.numItems = data.querts.length;
  215. this.yilaiCount.templateVars = this.getTemplateVars(["value"], [data.querts.length.toString()]);
  216. //获取当前显示的包体 是否在锁定查询的信息里面
  217. let lockPkgs = new Array<CS.FairyEditor.FPackage>();
  218. for (let index = 0; index < data.querts.length; index++) {
  219. let pkg = data.querts[index]
  220. if (this.lockPkgs.indexOf(pkg) != -1) {
  221. lockPkgs.push(pkg)
  222. this.yilaiSelectPkgs.push(pkg)
  223. }
  224. }
  225. for (let index = 0; index < this.resList.numChildren; index++) {
  226. let item = this.resList.GetChildAt(index)
  227. if (this.isResInYiLai(item, lockPkgs))
  228. this.resList.GetChildAt(index).asCom.GetController("state").selectedIndex = 1;
  229. else
  230. this.resList.GetChildAt(index).asCom.GetController("state").selectedIndex = 2;
  231. }
  232. App.consoleView.Log(FileUtils.getUBBUrl(item.GetURL(), item.name));
  233. this.updateExternalList(item)
  234. }
  235. //刷新外部资源列表
  236. private updateExternalList(item: CS.FairyEditor.FPackageItem) {
  237. //刷新当前选中的包体 对应的依赖资源
  238. let query = this.getDependencyQuery(item)
  239. //如果勾选的外部依赖包体 那么只查找对应的 否则显示全部
  240. if (this.yilaiSelectPkgs.length > 0)
  241. for (let index = query.length - 1; index >= 0; index--) {
  242. const element = query[index];
  243. if (this.yilaiSelectPkgs.indexOf(element.owner) == -1) {
  244. query.splice(index, 1)
  245. }
  246. }
  247. this.externalList.data = query;
  248. this.externalList.numItems = this.externalList.data.length;
  249. this.externalCount.templateVars = this.getTemplateVars(["value"], [this.externalList.data.length + ""]);
  250. }
  251. //渲染点击的资源 外部依赖的详细资源
  252. private onExternalListItemRenderer(index: number, obj: CS.FairyGUI.GButton) {
  253. let data = this.externalList.data[index] as CS.FairyEditor.FPackageItem;
  254. obj.text = data.name
  255. obj.data = data;
  256. obj.GetChild("icon").asLoader.url = data.GetIcon(false, false, true);
  257. obj.__onDispose = () => {
  258. obj.text = "";
  259. obj.data = null;
  260. obj.GetChild("icon").asLoader.url = "";
  261. }
  262. }
  263. private onExternalListClickItem(obj: CS.FairyGUI.EventContext) {
  264. let data = obj.data.data as CS.FairyEditor.FPackageItem
  265. App.consoleView.Log(FileUtils.getUBBUrl(data.GetURL(), `依赖:${data.owner.name}/${data.name}`))
  266. }
  267. /**生成模板值 */
  268. private getTemplateVars(keys: Array<string>, nums: Array<string>) {
  269. let dic = FileUtils.createGenericDictionary<string, string>(System.String, System.String);
  270. keys.forEach((key, index) => {
  271. dic.set_Item(key, nums[index])
  272. }, this)
  273. return dic;
  274. }
  275. //清空依赖选中
  276. private clearSelectPkgs() {
  277. this.yilaiList.SelectNone()
  278. this.yilaiSelectPkgs = [];
  279. }
  280. /**获取依赖清单 */
  281. private getDependencyQuery(item: CS.FairyEditor.FPackageItem) {
  282. //获取包体对应的依赖
  283. let yilaiItems = new Array<CS.FairyEditor.FPackageItem>()
  284. if (this.yilaiCB.selected) {
  285. let map = new Map<string, CS.FairyEditor.FPackageItem>();
  286. FileUtils.getClearOnPublishPackageItem(item, map, 0)
  287. yilaiItems = Array.from(map).map(x => x[1])
  288. } else {
  289. let list = FileUtils.createGenericList<CS.FairyEditor.FPackageItem>(FairyEditor.FPackageItem)
  290. list.Add(item)
  291. let data = new FairyEditor.DependencyQuery()
  292. data.QueryDependencies(list, FairyEditor.DependencyQuery.SeekLevel.ALL)
  293. for (let index = 0; index < data.references.Count; index++) {
  294. let reference = data.references.get_Item(index)
  295. let pkgID = reference.pkgId
  296. let yilaipkg = App.project.GetPackage(pkgID)
  297. if (yilaipkg == null) continue
  298. let yilaiItem = yilaipkg.GetItem(reference.itemId)
  299. if (yilaiItem && yilaiItems.indexOf(yilaiItem) == -1) {
  300. //App.consoleView.Log(FileUtils.getUBBUrl(yilaiItem.GetURL(), yilaiItem.name));
  301. yilaiItems.push(yilaiItem)
  302. }
  303. }
  304. }
  305. return yilaiItems;
  306. }
  307. private onYiLaiListClickItem(obj: CS.FairyGUI.EventContext) {
  308. let btn = obj.data;
  309. //锁定的包体不允许取消选中
  310. if (!btn.selected && this.lockPkgs.indexOf(btn.data) != -1) {
  311. btn.selected = true;
  312. return
  313. }
  314. this.getSelectYiLaiPkgs();
  315. //获取选中的物体
  316. for (let index = 0; index < this.resList.numChildren; index++) {
  317. let resItem = this.resList.GetChildAt(index)
  318. if (this.isResInYiLai(resItem, this.yilaiSelectPkgs) || this.isResInYiLai(resItem, this.lockPkgs)) {
  319. resItem.asCom.GetController("state").selectedIndex = 1;
  320. } else {
  321. resItem.asCom.GetController("state").selectedIndex = 2;
  322. }
  323. }
  324. //刷新当前外部资源列表
  325. if (this.resList.selectedIndex < 0) {
  326. this.externalList.numItems = 0;
  327. this.externalList.data = null;
  328. this.externalCount.templateVars = this.getTemplateVars(['value'], ['0'])
  329. } else {
  330. let res = this.resList.GetChildAt(this.resList.selectedIndex).data as resListData;
  331. this.updateExternalList(res.item)
  332. }
  333. }
  334. //获取依赖列表全部选中的包
  335. private getSelectYiLaiPkgs() {
  336. //获取全部选中的物体
  337. let selectIndexs = this.yilaiList.GetSelection()
  338. let selectPkgs = new Array<CS.FairyEditor.FPackage>();
  339. for (let index = 0; index < selectIndexs.Count; index++) {
  340. selectPkgs.push(this.yilaiList.GetChildAt(selectIndexs.get_Item(index)).data as CS.FairyEditor.FPackage);
  341. }
  342. this.yilaiSelectPkgs = selectPkgs;
  343. }
  344. //资源是否在依赖列表中
  345. private isResInYiLai(obj: CS.FairyGUI.GObject, pkgs: CS.FairyEditor.FPackage[]) {
  346. let querts = (obj.data as resListData).querts;
  347. if (pkgs.length == 0) return false
  348. //判断两个数组是否有相同的值
  349. return pkgs.some(pkg => {
  350. return querts.some(resItem => {
  351. return resItem.name == pkg.name
  352. })
  353. })
  354. }
  355. private onYiLaiListItemRenderer(index: number, obj: CS.FairyGUI.GButton) {
  356. let data = this.yilaiList.data[index] as CS.FairyEditor.FPackage;
  357. obj.text = data.name
  358. obj.data = data;
  359. obj.selected = this.lockPkgs.indexOf(data) != -1
  360. let cb = obj.GetChild("cb").asButton
  361. cb.selected = obj.selected;
  362. cb.RemoveEventListeners();
  363. cb.onClick.Add(this.onCBLock.bind(this, data, obj))
  364. obj.__onDispose = () => {
  365. obj.text = "";
  366. obj.data = null;
  367. }
  368. }
  369. //CB锁定
  370. private onCBLock(data: CS.FairyEditor.FPackage, obj: CS.FairyGUI.GButton, item: CS.FairyGUI.EventContext) {
  371. let index = this.lockPkgs.indexOf(data)
  372. if (index == -1) {
  373. if (obj.selected)
  374. item.StopPropagation()
  375. this.lockPkgs.push(data)
  376. } else {
  377. this.lockPkgs.splice(index, 1)
  378. item.StopPropagation()
  379. }
  380. }
  381. private onCloseClick() {
  382. let self = this;
  383. self.Hide();
  384. FairyGUI.Timers.inst.Add(0.05, 1, () => {
  385. UIWindow.remove(this)
  386. })
  387. }
  388. }