SelectSkinPanel.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.node.active = false,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. createItem(key: number,spineOffset:Vec2) {
  38. let itemNode = this._itemPool.length > 0 ? this._itemPool.pop() : instantiate(this.itemPrefab);
  39. itemNode.parent = this.itemRoot;
  40. this._items.push(itemNode);
  41. itemNode.active = true
  42. let spPath = allSkinList[key]
  43. this.loadSpineData("editor",spPath, (spine: Prefab) => {
  44. itemNode.getComponent(SkinItem).init(key,spine,"loop2",spineOffset,this.judgeSelect(key))
  45. })
  46. }
  47. onSingleSkinClick() {
  48. Skin.setSelectStatu(true)
  49. EventMgr.ins.dispatchEvent("regenerate")
  50. }
  51. onMultiSkinClick() {
  52. Skin.setSelectStatu(false)
  53. EventMgr.ins.dispatchEvent("regenerate")
  54. }
  55. judgeSelect(key)
  56. {
  57. let selSkins = this._skinData.selectedSkinList
  58. let index = selSkins.indexOf(key)
  59. return index !== -1
  60. }
  61. // spineDataMap: Map<string, sp.SkeletonData> = new Map();
  62. spineDataMap: Map<string, Prefab> = new Map();
  63. // /**
  64. // * 加载spine数据
  65. // * @param path spine数据路径
  66. // * @param callback 回调函数
  67. // */
  68. // loadSpineData(bundleName: BundleName | string, spinepath: string, callback: (spineData: sp.SkeletonData) => void) {
  69. // let bundle: AssetManager.Bundle = LayerMgr.instance.getBundle(bundleName)
  70. // if (bundle) {
  71. // bundle.load(spinepath, sp.SkeletonData, (err, res) => {
  72. // if (err) {
  73. // console.error(err)
  74. // return
  75. // }
  76. // let spineData: sp.SkeletonData = (res as sp.SkeletonData);
  77. // callback(spineData)
  78. // })
  79. // }
  80. // }
  81. loadSpineData(bundleName: BundleName | string, spinepath: string, callback: (sp:Prefab) => void) {
  82. let bundle: AssetManager.Bundle = LayerMgr.instance.getBundle(bundleName)
  83. if (bundle) {
  84. bundle.load(spinepath, Prefab, (err, res) => {
  85. if (err) {
  86. console.error(err)
  87. return
  88. }
  89. let spineData: Prefab = (res as Prefab);
  90. callback(spineData)
  91. })
  92. }
  93. }
  94. // loadSpine(path: string, callback: (spineData: sp.SkeletonData) => void) {
  95. // if (this.spineDataMap.has(path)) {
  96. // callback(this.spineDataMap.get(path));
  97. // return;
  98. // }
  99. // this.loadSpineData("editor", path, (spineData: sp.SkeletonData) => {
  100. // this.spineDataMap.set(path, spineData);
  101. // callback(spineData);
  102. // });
  103. // }
  104. loadSpine(path: string, callback: (sp:Prefab) => void) {
  105. if (this.spineDataMap.has(path)) {
  106. callback(this.spineDataMap.get(path));
  107. return;
  108. }
  109. this.loadSpineData("editor", path, (sp:Prefab) => {
  110. this.spineDataMap.set(path, sp);
  111. callback(sp);
  112. });
  113. }
  114. clearSelectAllSkins()
  115. {
  116. Skin.removeAllSelectSkin()
  117. this.refreshSkinSelectioin()
  118. }
  119. refreshSkinSelectioin()
  120. {
  121. let allSkillKeys = getAllSkinKeys()
  122. let count = allSkillKeys.length;
  123. for(let i = 0; i < count; i++){
  124. let item = this._items[i]
  125. item.getComponent(SkinItem).setSelectSkin(false)
  126. }
  127. }
  128. }