import { _decorator, Component, Node, SpriteFrame, Texture2D, v2 } from 'cc';
import { ImagePack } from '../ImagePack/ImagePack';
const { ccclass, property } = _decorator;

@ccclass('BaseSpriteFrame')
export class BaseSpriteFrame {

    //初始化状态
    InitState: boolean = false;
    //精灵帧
    _SpriteFrame: SpriteFrame;
    //回调函数
    CallBackFunc: Function;

    constructor(ImgPath: string, ImgIndex: number, CallBack?: Function) {
        if (CallBack) this.CallBackFunc = CallBack;
        ImagePack.getInstance().ReadNpkTable("sprite/" + ImgPath.toLocaleLowerCase(), (ImgObj) => {
            const Png = ImgObj.Png_List[ImgIndex];
            this._SpriteFrame = new SpriteFrame();
            let tex = new Texture2D();
            tex.reset({
                width: Png.Width,
                height: Png.Height,
                format: Texture2D.PixelFormat.RGBA8888,
                mipmapLevel: 0,
            });
            tex.uploadData(Png.PNGdata);
            // 更新 0 级 Mipmap。
            tex.updateImage();
            this._SpriteFrame.texture = tex;
            this._SpriteFrame.offset = v2(Png.Xpos, -Png.Ypos);

            this.CallBackFunc(this._SpriteFrame);
            this.InitState = true;
        });
    }
}


