var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
/**
 * @module annie
 */
var annie;
(function (annie) {
    /**
     * 利用 Bitmap() 构造函数,可以创建包含对 BitmapData 对象的引用的 Bitmap 对象。
     * 创建了 Bitmap 对象后,使用父 Sprite 实例的 addChild() 或 addChildAt() 方法将位图放在显示列表中。
     * @class annie.Bitmap
     * @public
     * @extends annie.DisplayObject
     * @since 1.0.0
     */
    var Bitmap = (function (_super) {
        __extends(Bitmap, _super);
        /**
         * 构造函数
         * @method Bitmap
         * @since 1.0.0
         * @public
         * @param {Image|Video|Canvas} bitmapData 一个HTMl Image的实例,小程序或者小游戏里则只能是一个图片的地址
         * @param {annie.Rectangle} rect 设置显示Image的区域,不设置值则全部显示Image的内容,小程序或者小游戏里没有这个参数
         * @example
         *      //html5
         *      var imgEle=new Image();
         *      imgEle.onload=function (e) {
         *          var bitmap = new annie.Bitmap(imgEle)
         *          //居中对齐
         *          bitmap.x = (s.stage.desWidth - bitmap.width) / 2;
         *          bitmap.y = (s.stage.desHeight - bitmap.height) / 2;
         *          s.addChild(bitmap);
         *          //截取图片的某一部分显示
         *          var rect = new annie.Rectangle(0, 0, 200, 200),
         *          rectBitmap = new annie.Bitmap(imgEle, rect);
         *          rectBitmap.x = (s.stage.desWidth - bitmap.width) / 2;
         *          rectBitmap.y = 100;
         *          s.addChild(rectBitmap);
         *      }
         *      imgEle.src='http://test.annie2x.com/test.jpg';
         *      //小程序或者小游戏
         *      var imgEle="http://test.annie2x.com/test.jpg";
         *      var bitmap=new annie.Bitmap(imgEle);
         *      s.addChild(bitmap);
         *
         * <p><a href="http://test.annie2x.com/annie/Bitmap/index.html" target="_blank">测试链接</a></p>
         */
        function Bitmap(bitmapData) {
            _super.call(this);
            this._cacheCanvas = null;
            this._bitmapData = null;
            var s = this;
            s._instanceType = "annie.Bitmap";
            s.bitmapData = bitmapData;
        }
        Object.defineProperty(Bitmap.prototype, "bitmapData", {
            /**
             * <h4><font color="red">小游戏不支持 小程序不支持</font></h4>
             * HTML的一个Image对象或者是canvas对象或者是video对象
             * @property bitmapData
             * @public
             * @since 1.0.0
             * @type {any}
             * @default null
             */
            get: function () {
                return this._bitmapData;
            },
            set: function (value) {
                var s = this;
                if (typeof (value) == "string") {
                    var img = new Image();
                    img.src = value;
                    s.clearBounds();
                    s._bitmapData = img;
                    s._texture = img;
                }
                else {
                    if (value != s._bitmapData) {
                        s.clearBounds();
                        s._bitmapData = value;
                        s._texture = value;
                    }
                }
            },
            enumerable: true,
            configurable: true
        });
        Bitmap.prototype._onUpdateTexture = function () {
            var s = this;
            var texture = s._bitmapData;
            if (!texture || texture.width == 0 || texture.height == 0) {
                return;
            }
            var bw = texture.width;
            var bh = texture.height;
            if (s._bounds.width != bw || s._bounds.height != bh) {
                s._bounds.width = bw;
                s._bounds.height = bh;
                s._updateSplitBounds();
                if (s._filters.length > 0) {
                    s.a2x_uf = true;
                }
                s._texture = texture;
            }
            if (s.a2x_uf) {
                s.a2x_uf = false;
                s._needCheckDrawBounds = true;
                if (!s._cacheCanvas) {
                    s._cacheCanvas = document.createElement("canvas");
                }
                var canvas = s._cacheCanvas;
                canvas.width = bw;
                canvas.heigth = bh;
                canvas.style.width = Math.ceil(bw / annie.devicePixelRatio) + "px";
                canvas.style.height = Math.ceil(bh / annie.devicePixelRatio) + "px";
                var ctx = canvas.getContext("2d");
                ctx.clearRect(0, 0, bw, bh);
                ////////////////////
                ctx.drawImage(texture, 0, 0);
                /////////////////////
                var cf = s._filters;
                var cfLen = cf.length;
                if (cfLen > 0) {
                    var imageData = ctx.getImageData(0, 0, bw, bh);
                    for (var i = 0; i < cfLen; i++) {
                        cf[i].drawFilter(imageData);
                    }
                    ctx.putImageData(imageData, 0, 0);
                    s._texture = canvas;
                }
                else {
                    s._texture = texture;
                }
            }
            if (s._needCheckDrawBounds) {
                s._checkDrawBounds();
            }
        };
        Bitmap.prototype._onUpdateMatrixAndAlpha = function () {
            _super.prototype._onUpdateMatrixAndAlpha.call(this);
            if (this.a2x_um) {
                this._needCheckDrawBounds = true;
            }
            this.a2x_um = false;
            this.a2x_ua = false;
        };
        /**
         * <h4><font color="red">小游戏不支持 小程序不支持</font></h4>
         * 从Bitmap中剥离出单独的小图以供特殊用途
         * @method convertToImage
         * @static
         * @public
         * @since 1.0.0
         * @param {annie.Bitmap} bitmap
         * @param {annie.Rectangle} rect 截图范围
         * @param {boolean} isNeedImage 是否一定要返回img,如果不为true则有时返回的是canvas
         * @return {Canvas|BitmapData}
         * @example
         *      var spriteSheetImg = new Image(),
         *      rect = new annie.Rectangle(0, 0, 200, 200),
         *      yourBitmap = new annie.Bitmap(spriteSheetImg, rect);
         *      spriteSheetImg.onload=function(e){
         *          var singleSmallImg = annie.Bitmap.convertToImage(yourBitmap);//convertToImage是annie.Bitmap的一个静态方法
         *          console.log(singleSmallImg);
         *      }
         *      spriteSheetImg.src = 'http://test.annie2x.com/test.jpg';
         */
        Bitmap.convertToImage = function (bitmap, rect, isNeedImage) {
            if (isNeedImage === void 0) { isNeedImage = true; }
            var _canvas;
            if (isNeedImage) {
                _canvas = annie.DisplayObject._canvas;
            }
            else {
                _canvas = window.document.createElement("canvas");
            }
            var w = rect.width, h = rect.height, ctx = _canvas.getContext("2d");
            _canvas.width = w;
            _canvas.height = h;
            ctx.drawImage(bitmap.bitmapData, rect.x, rect.y, w, h, 0, 0, w, h);
            if (isNeedImage) {
                var img = new Image();
                img.src = _canvas.toDataURL();
                return img;
            }
            else {
                return _canvas;
            }
        };
        Bitmap.prototype.destroy = function () {
            //清除相应的数据引用
            var s = this;
            _super.prototype.destroy.call(this);
            s._bitmapData = null;
            s._cacheCanvas = null;
        };
        return Bitmap;
    }(annie.DisplayObject));
    annie.Bitmap = Bitmap;
})(annie || (annie = {}));

    
Top