import { _decorator, AssetManager, Button, Component, instantiate, Node, Prefab, sp, Vec2 } from 'cc'; import { allSkinList, getAllSkinKeys, Skin, SkinData } from '../../script/Manager/LocalDataMgr'; import { SkinItem } from './SkinItem'; import { LayerMgr } from '../../script/Manager/LayerMgr'; import { BundleName } from '../../script/Config/EnumCfg'; import EventMgr from '../../script/Manager/EventMgr'; const { ccclass, property } = _decorator; @ccclass('SelectSkinPanel') export class SelectSkinPanel extends Component { @property(Node) itemRoot: Node = null; @property(Node) itemPrefab: Node = null; @property({type:Vec2,tooltip:"spine偏移量"}) spineoffset:Vec2 = new Vec2(0,0); @property(Button) btnClose: Button = null; _skinData: SkinData = null _allSkinList:{ [key: number]: string }; _items: Node[] = []; _hasCreatedItems:boolean = false; protected start(): void { this.btnClose.node.on(Node.EventType.TOUCH_END,this.onBtnCloseClick,this) } init() { if(this._hasCreatedItems) return; this._hasCreatedItems = true; this._allSkinList = allSkinList this._skinData = Skin.getSkinData(); let allSkillKeys = getAllSkinKeys() let count = allSkillKeys.length; for (let i = 0; i < count; i++) { this.createItem(allSkillKeys[i], this.spineoffset); } } onBtnCloseClick() { this.node.active = false EventMgr.ins.dispatchEvent("regenerate") } createItem(key: number,spineOffset:Vec2) { let itemNode = instantiate(this.itemPrefab); itemNode.parent = this.itemRoot; this._items.push(itemNode); itemNode.active = true let spPath = allSkinList[key] this.loadSpineData("editor",spPath, (spine: Prefab) => { itemNode.getComponent(SkinItem).init(key,spine,"loop2",spineOffset,this.judgeSelect(key)) }) } onSingleSkinClick() { Skin.setSelectStatu(true) } onMultiSkinClick() { Skin.setSelectStatu(false) } judgeSelect(key) { let selSkins = this._skinData.selectedSkinList let index = selSkins.indexOf(key) return index !== -1 } // spineDataMap: Map = new Map(); spineDataMap: Map = new Map(); // /** // * 加载spine数据 // * @param path spine数据路径 // * @param callback 回调函数 // */ // loadSpineData(bundleName: BundleName | string, spinepath: string, callback: (spineData: sp.SkeletonData) => void) { // let bundle: AssetManager.Bundle = LayerMgr.instance.getBundle(bundleName) // if (bundle) { // bundle.load(spinepath, sp.SkeletonData, (err, res) => { // if (err) { // console.error(err) // return // } // let spineData: sp.SkeletonData = (res as sp.SkeletonData); // callback(spineData) // }) // } // } loadSpineData(bundleName: BundleName | string, spinepath: string, callback: (sp:Prefab) => void) { let bundle: AssetManager.Bundle = LayerMgr.instance.getBundle(bundleName) if (bundle) { bundle.load(spinepath, Prefab, (err, res) => { if (err) { console.error(err) return } let spineData: Prefab = (res as Prefab); callback(spineData) }) } } // loadSpine(path: string, callback: (spineData: sp.SkeletonData) => void) { // if (this.spineDataMap.has(path)) { // callback(this.spineDataMap.get(path)); // return; // } // this.loadSpineData("editor", path, (spineData: sp.SkeletonData) => { // this.spineDataMap.set(path, spineData); // callback(spineData); // }); // } loadSpine(path: string, callback: (sp:Prefab) => void) { if (this.spineDataMap.has(path)) { callback(this.spineDataMap.get(path)); return; } this.loadSpineData("editor", path, (sp:Prefab) => { this.spineDataMap.set(path, sp); callback(sp); }); } clearSelectAllSkins() { Skin.removeAllSelectSkin() this.refreshSkinSelectioin() EventMgr.ins.dispatchEvent("regenerate") } refreshSkinSelectioin() { let allSkillKeys = getAllSkinKeys() let count = allSkillKeys.length; for(let i = 0; i < count; i++){ let item = this._items[i] item.getComponent(SkinItem).setSelectSkin(false) } } }