SelectSkinPanel.ts 4.7 KB

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