SelectSkinPanel.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import { _decorator, AssetManager, Button, Component, instantiate, Node, Prefab, sp, Vec2 } from 'cc';
  2. import { allSkinList, getAllSkinKeys, Skin, SkinData } from '../../script/Manager/LocalDataMgr';
  3. import { SkinItem } from './SkinItem';
  4. import { LayerMgr } from '../../script/Manager/LayerMgr';
  5. import { BundleName } from '../../script/Config/EnumCfg';
  6. import EventMgr from '../../script/Manager/EventMgr';
  7. const { ccclass, property } = _decorator;
  8. @ccclass('SelectSkinPanel')
  9. export class SelectSkinPanel extends Component {
  10. @property(Node)
  11. itemRoot: Node = null;
  12. @property(Node)
  13. itemPrefab: Node = null;
  14. @property({type:Vec2,tooltip:"spine偏移量"})
  15. spineoffset:Vec2 = new Vec2(0,0);
  16. @property(Button)
  17. btnClose: Button = null;
  18. _skinData: SkinData = null
  19. _allSkinList:{ [key: number]: string };
  20. _items: Node[] = [];
  21. _itemPool: Node[] = [];
  22. _hasCreatedItems:boolean = false;
  23. protected start(): void {
  24. this.btnClose.node.on(Node.EventType.TOUCH_END,this.onBtnCloseClick,this)
  25. }
  26. init() {
  27. LayerMgr.instance.loadBundle("editor", () => {
  28. this._allSkinList = allSkinList
  29. this._skinData = Skin.getSkinData();
  30. let allSkillKeys = getAllSkinKeys()
  31. let count = allSkillKeys.length;
  32. for(let i = 0; i < count; i++){
  33. this.createItem(allSkillKeys[i],this.spineoffset);
  34. }
  35. });
  36. }
  37. onBtnCloseClick()
  38. {
  39. this.node.active = false
  40. EventMgr.ins.dispatchEvent("regenerate")
  41. }
  42. createItem(key: number,spineOffset:Vec2) {
  43. let itemNode = this._itemPool.length > 0 ? this._itemPool.pop() : instantiate(this.itemPrefab);
  44. itemNode.parent = this.itemRoot;
  45. this._items.push(itemNode);
  46. itemNode.active = true
  47. let spPath = allSkinList[key]
  48. this.loadSpineData("editor",spPath, (spine: Prefab) => {
  49. itemNode.getComponent(SkinItem).init(key,spine,"loop2",spineOffset,this.judgeSelect(key))
  50. })
  51. }
  52. onSingleSkinClick() {
  53. Skin.setSelectStatu(true)
  54. }
  55. onMultiSkinClick() {
  56. Skin.setSelectStatu(false)
  57. }
  58. judgeSelect(key)
  59. {
  60. let selSkins = this._skinData.selectedSkinList
  61. let index = selSkins.indexOf(key)
  62. return index !== -1
  63. }
  64. // spineDataMap: Map<string, sp.SkeletonData> = new Map();
  65. spineDataMap: Map<string, Prefab> = new Map();
  66. // /**
  67. // * 加载spine数据
  68. // * @param path spine数据路径
  69. // * @param callback 回调函数
  70. // */
  71. // loadSpineData(bundleName: BundleName | string, spinepath: string, callback: (spineData: sp.SkeletonData) => void) {
  72. // let bundle: AssetManager.Bundle = LayerMgr.instance.getBundle(bundleName)
  73. // if (bundle) {
  74. // bundle.load(spinepath, sp.SkeletonData, (err, res) => {
  75. // if (err) {
  76. // console.error(err)
  77. // return
  78. // }
  79. // let spineData: sp.SkeletonData = (res as sp.SkeletonData);
  80. // callback(spineData)
  81. // })
  82. // }
  83. // }
  84. loadSpineData(bundleName: BundleName | string, spinepath: string, callback: (sp:Prefab) => void) {
  85. let bundle: AssetManager.Bundle = LayerMgr.instance.getBundle(bundleName)
  86. if (bundle) {
  87. bundle.load(spinepath, Prefab, (err, res) => {
  88. if (err) {
  89. console.error(err)
  90. return
  91. }
  92. let spineData: Prefab = (res as Prefab);
  93. callback(spineData)
  94. })
  95. }
  96. }
  97. // loadSpine(path: string, callback: (spineData: sp.SkeletonData) => void) {
  98. // if (this.spineDataMap.has(path)) {
  99. // callback(this.spineDataMap.get(path));
  100. // return;
  101. // }
  102. // this.loadSpineData("editor", path, (spineData: sp.SkeletonData) => {
  103. // this.spineDataMap.set(path, spineData);
  104. // callback(spineData);
  105. // });
  106. // }
  107. loadSpine(path: string, callback: (sp:Prefab) => void) {
  108. if (this.spineDataMap.has(path)) {
  109. callback(this.spineDataMap.get(path));
  110. return;
  111. }
  112. this.loadSpineData("editor", path, (sp:Prefab) => {
  113. this.spineDataMap.set(path, sp);
  114. callback(sp);
  115. });
  116. }
  117. clearSelectAllSkins()
  118. {
  119. Skin.removeAllSelectSkin()
  120. this.refreshSkinSelectioin()
  121. EventMgr.ins.dispatchEvent("regenerate")
  122. }
  123. refreshSkinSelectioin()
  124. {
  125. let allSkillKeys = getAllSkinKeys()
  126. let count = allSkillKeys.length;
  127. for(let i = 0; i < count; i++){
  128. let item = this._items[i]
  129. item.getComponent(SkinItem).setSelectSkin(false)
  130. }
  131. }
  132. }