"use strict";
cc._RF.push(module, 'f71efmhV/JOvaxw3x/8YNYh', 'MySprite');
// Script/MySprite.ts

"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
//@ts-ignore
var gfx = cc.gfx;
var vfmtCustom = new gfx.VertexFormat([
    { name: gfx.ATTR_POSITION, type: gfx.ATTR_TYPE_FLOAT32, num: 2 },
    { name: gfx.ATTR_UV0, type: gfx.ATTR_TYPE_FLOAT32, num: 2 },
    { name: gfx.ATTR_COLOR, type: gfx.ATTR_TYPE_UINT8, num: 4, normalize: true },
    { name: "uv_range", type: gfx.ATTR_TYPE_FLOAT32, num: 4 },
    { name: "position_range", type: gfx.ATTR_TYPE_FLOAT32, num: 4 },
    { name: "texture_pixel", type: gfx.ATTR_TYPE_FLOAT32, num: 2 },
]);
var _a = cc._decorator, ccclass = _a.ccclass, menu = _a.menu, disallowMultiple = _a.disallowMultiple;
/*
* Char 缓存模式下，自定义参数没用
*/
var MySprite = /** @class */ (function (_super) {
    __extends(MySprite, _super);
    function MySprite() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    MySprite.prototype._resetAssembler = function () {
        //@ts-ignore
        cc.Assembler.init(this);
        //@ts-ignore
        this._setCustomVertsData(this._assembler);
        //@ts-ignore
        this._updateColor();
        //@ts-ignore
        this.setVertsDirty();
    };
    //@ts-ignore
    MySprite.prototype._setCustomVertsData = function (assembler) {
        //@ts-ignore
        if (!this._assembler["override_vertsData"]) {
            assembler.floatsPerVert = 15;
            assembler.getVfmt = function () {
                return vfmtCustom;
            };
            assembler.getBuffer = function () {
                //@ts-ignore
                return cc.renderer._handle.getBuffer("mesh", this.getVfmt());
            };
            if (this.type == cc.Sprite.Type.SLICED) {
                assembler._renderData.createQuadData(0, assembler.verticesFloats, assembler.indicesCount);
                var indices = assembler._renderData.iDatas[0];
                var indexOffset = 0;
                for (var r = 0; r < 3; ++r) {
                    for (var c = 0; c < 3; ++c) {
                        var start = r * 4 + c;
                        indices[indexOffset++] = start;
                        indices[indexOffset++] = start + 1;
                        indices[indexOffset++] = start + 4;
                        indices[indexOffset++] = start + 1;
                        indices[indexOffset++] = start + 5;
                        indices[indexOffset++] = start + 4;
                    }
                }
            }
            else if (this.type === cc.Sprite.Type.FILLED && this.fillType == cc.Sprite.FillType.RADIAL) {
                assembler._renderData.createFlexData(0, 4, 6, assembler.getVfmt());
                assembler.updateIndices();
            }
            assembler.uuvs = assembler.updateUVs;
            assembler.updateUVs = function (sprite, fillStart, fillEnd) {
                assembler.uuvs(sprite, fillStart, fillEnd);
                if (!sprite || (sprite && !sprite._spriteFrame))
                    return;
                var uv = sprite._spriteFrame.uv;
                var isRotated = sprite._spriteFrame.isRotated();
                var l = uv[0], r = uv[2], b = uv[1], t = uv[5];
                if (isRotated) {
                    // cc图集里的旋转总是顺时针旋转90度，以原左下角为中心。（旋转后左下角变为左上角）
                    l = uv[1];
                    r = uv[3];
                    b = uv[0];
                    t = uv[4];
                }
                var x_range_min = l, x_range_max = r;
                var y_range_min = t, y_range_max = b;
                var uvOffset = assembler.uvOffset;
                var floatsPerVert = assembler.floatsPerVert;
                var verts = assembler._renderData.vDatas[0];
                var worldPos = sprite.node.convertToWorldSpaceAR(cc.v2(0, 0));
                /**求出自己中心点的世界坐标 */
                var centerPos = worldPos.add(cc.v2(sprite.node.width * (0.5 - sprite.node.anchorX), sprite.node.height * (0.5 - sprite.node.anchorY)));
                var pos_left = centerPos.x - sprite.node.width / 2;
                var pos_right = centerPos.x + sprite.node.width / 2;
                var pos_bottom = centerPos.y - sprite.node.height / 2;
                var pos_top = centerPos.y + sprite.node.height / 2;
                var size = sprite.spriteFrame._originalSize;
                for (var i = 0; i < (verts.length * floatsPerVert) / 4; i++) {
                    var dstOffset = floatsPerVert * i + uvOffset;
                    //付值顺序为左上右下, UV渲染是由左至右，上至下
                    if (isRotated) {
                        verts[dstOffset + 3] = y_range_max;
                        verts[dstOffset + 4] = x_range_min;
                        verts[dstOffset + 5] = y_range_min;
                        verts[dstOffset + 6] = x_range_max;
                    }
                    else {
                        verts[dstOffset + 3] = x_range_min;
                        verts[dstOffset + 4] = y_range_min;
                        verts[dstOffset + 5] = x_range_max;
                        verts[dstOffset + 6] = y_range_max;
                    }
                    verts[dstOffset + 7] = pos_left;
                    verts[dstOffset + 8] = pos_bottom;
                    verts[dstOffset + 9] = pos_right;
                    verts[dstOffset + 10] = pos_top;
                    verts[dstOffset + 11] = size.width;
                    verts[dstOffset + 12] = size.height;
                }
                console.warn(verts);
            };
            assembler.initData();
            assembler.initLocal();
            //@ts-ignore
            this._assembler["override_vertsData"] = true;
        }
    };
    MySprite = __decorate([
        ccclass,
        disallowMultiple,
        menu('引擎扩展/组件扩展/MySprite')
    ], MySprite);
    return MySprite;
}(cc.Sprite));
exports.default = MySprite;

cc._RF.pop();