// Class: openfl.display.Bitmap

var $global = typeof window != "undefined" ? window : typeof global != "undefined" ? global : typeof self != "undefined" ? self : this

$global.Object.defineProperty(exports, "__esModule", {value: true});

var __map_reserved = {};

// Imports

var $hxClasses = require("./../../hxClasses_stub").default;
var $import = require("./../../import_stub").default;
var $extend = require("./../../extend_stub").default;
function openfl_display_DisplayObject() {return require("./../../openfl/display/DisplayObject");}
function openfl_geom_Rectangle() {return require("./../../openfl/geom/Rectangle");}
function openfl__$internal_renderer_canvas_CanvasBitmap() {return require("./../../openfl/_internal/renderer/canvas/CanvasBitmap");}
function openfl__$internal_renderer_dom_DOMBitmap() {return require("./../../openfl/_internal/renderer/dom/DOMBitmap");}
function openfl__$internal_renderer_opengl_GLBitmap() {return require("./../../openfl/_internal/renderer/opengl/GLBitmap");}
function lime_graphics_RendererType() {return require("./../../lime/graphics/RendererType");}
function openfl_geom_Matrix() {return require("./../../openfl/geom/Matrix");}

// Constructor

var Bitmap = function(bitmapData,pixelSnapping,smoothing) {
	if(smoothing == null) {
		smoothing = false;
	}
	(openfl_display_DisplayObject().default).call(this);
	this.__bitmapData = bitmapData;
	this.pixelSnapping = pixelSnapping;
	this.smoothing = smoothing;
	if(pixelSnapping == null) {
		this.pixelSnapping = "auto";
	}
}

// Meta

Bitmap.__name__ = ["openfl","display","Bitmap"];
Bitmap.__super__ = (openfl_display_DisplayObject().default);
Bitmap.prototype = $extend((openfl_display_DisplayObject().default).prototype, {
	__enterFrame: function(deltaTime) {
		if(this.__bitmapData != null && this.__bitmapData.image != null && this.__bitmapData.image.version != this.__imageVersion) {
			this.__setRenderDirty();
		}
	},
	__getBounds: function(rect,matrix) {
		if(this.__bitmapData != null) {
			var bounds = (openfl_geom_Rectangle().default).__pool.get();
			bounds.setTo(0,0,this.__bitmapData.width,this.__bitmapData.height);
			bounds.__transform(bounds,matrix);
			rect.__expand(bounds.x,bounds.y,bounds.width,bounds.height);
			(openfl_geom_Rectangle().default).__pool.release(bounds);
		}
	},
	__hitTest: function(x,y,shapeFlag,stack,interactiveOnly,hitObject) {
		if(!hitObject.get_visible() || this.__isMask || this.__bitmapData == null) {
			return false;
		}
		if(this.get_mask() != null && !this.get_mask().__hitTestMask(x,y)) {
			return false;
		}
		this.__getRenderTransform();
		var px = this.__renderTransform.__transformInverseX(x,y);
		var py = this.__renderTransform.__transformInverseY(x,y);
		if(px > 0 && py > 0 && px <= this.__bitmapData.width && py <= this.__bitmapData.height) {
			if(this.__scrollRect != null && !this.__scrollRect.contains(px,py)) {
				return false;
			}
			if(stack != null && !interactiveOnly) {
				stack.push(hitObject);
			}
			return true;
		}
		return false;
	},
	__hitTestMask: function(x,y) {
		if(this.__bitmapData == null) {
			return false;
		}
		this.__getRenderTransform();
		var px = this.__renderTransform.__transformInverseX(x,y);
		var py = this.__renderTransform.__transformInverseY(x,y);
		if(px > 0 && py > 0 && px <= this.__bitmapData.width && py <= this.__bitmapData.height) {
			return true;
		}
		return false;
	},
	__renderCairo: function(renderer) {
		if(this.__bitmapData != null && this.__bitmapData.image != null) {
			this.__imageVersion = this.__bitmapData.image.version;
		}
	},
	__renderCairoMask: function(renderer) {
		renderer.cairo.rectangle(0,0,this.get_width(),this.get_height());
	},
	__renderCanvas: function(renderer) {
		if(this.__bitmapData != null && this.__bitmapData.image != null) {
			this.__imageVersion = this.__bitmapData.image.version;
		}
		this.__updateCacheBitmap(renderer,!this.__worldColorTransform.__isDefault());
		if(this.__cacheBitmap != null && !this.__isCacheBitmapRender) {
			(openfl__$internal_renderer_canvas_CanvasBitmap().default).render(this.__cacheBitmap,renderer);
		} else {
			(openfl__$internal_renderer_canvas_CanvasBitmap().default).render(this,renderer);
		}
		this.__renderEvent(renderer);
	},
	__renderCanvasMask: function(renderer) {
		renderer.context.rect(0,0,this.get_width(),this.get_height());
	},
	__renderDOM: function(renderer) {
		this.__updateCacheBitmap(renderer,!this.__worldColorTransform.__isDefault());
		if(this.__cacheBitmap != null && !this.__isCacheBitmapRender) {
			this.__renderDOMClear(renderer);
			this.__cacheBitmap.stage = this.stage;
			(openfl__$internal_renderer_dom_DOMBitmap().default).render(this.__cacheBitmap,renderer);
		} else {
			(openfl__$internal_renderer_dom_DOMBitmap().default).render(this,renderer);
		}
		this.__renderEvent(renderer);
	},
	__renderDOMClear: function(renderer) {
		(openfl__$internal_renderer_dom_DOMBitmap().default).clear(this,renderer);
	},
	__renderGL: function(renderer) {
		if(this.__bitmapData != null && this.__bitmapData.image != null) {
			this.__imageVersion = this.__bitmapData.image.version;
		}
		this.__updateCacheBitmap(renderer,false);
		if(this.__cacheBitmap != null && !this.__isCacheBitmapRender) {
			(openfl__$internal_renderer_opengl_GLBitmap().default).render(this.__cacheBitmap,renderer);
		} else {
			(openfl__$internal_renderer_opengl_GLBitmap().default).render(this,renderer);
		}
		this.__renderEvent(renderer);
	},
	__renderGLMask: function(renderer) {
		(openfl__$internal_renderer_opengl_GLBitmap().default).renderMask(this,renderer);
	},
	__updateCacheBitmap: function(renderer,force) {
		if(this.__filters == null && renderer.__type == (lime_graphics_RendererType().default).OPENGL) {
			return false;
		}
		return (openfl_display_DisplayObject().default).prototype.__updateCacheBitmap.call(this,renderer,force);
	},
	__updateMask: function(maskGraphics) {
		if(this.__bitmapData == null) {
			return;
		}
		maskGraphics.__commands.overrideMatrix(this.__worldTransform);
		maskGraphics.beginFill(0);
		maskGraphics.drawRect(0,0,this.__bitmapData.width,this.__bitmapData.height);
		if(maskGraphics.__bounds == null) {
			maskGraphics.__bounds = new (openfl_geom_Rectangle().default)();
		}
		this.__getBounds(maskGraphics.__bounds,(openfl_geom_Matrix().default).__identity);
		(openfl_display_DisplayObject().default).prototype.__updateMask.call(this,maskGraphics);
	},
	get_bitmapData: function() {
		return this.__bitmapData;
	},
	set_bitmapData: function(value) {
		this.__bitmapData = value;
		this.smoothing = false;
		this.__setRenderDirty();
		var tmp = this.__filters != null;
		this.__imageVersion = -1;
		return this.__bitmapData;
	},
	get_height: function() {
		if(this.__bitmapData != null) {
			return this.__bitmapData.height * Math.abs(this.get_scaleY());
		}
		return 0;
	},
	set_height: function(value) {
		if(this.__bitmapData != null) {
			if(value != this.__bitmapData.height) {
				this.__setRenderDirty();
				this.set_scaleY(value / this.__bitmapData.height);
			}
			return value;
		}
		return 0;
	},
	get_width: function() {
		if(this.__bitmapData != null) {
			return this.__bitmapData.width * Math.abs(this.__scaleX);
		}
		return 0;
	},
	set_width: function(value) {
		if(this.__bitmapData != null) {
			if(value != this.__bitmapData.width) {
				this.__setRenderDirty();
				this.set_scaleX(value / this.__bitmapData.width);
			}
			return value;
		}
		return 0;
	}
});
Bitmap.prototype.__class__ = $hxClasses["openfl.display.Bitmap"] = Bitmap;

// Init

Object.defineProperty(Bitmap.prototype,"bitmapData",{ get : function () { return this.get_bitmapData (); }, set : function (v) { return this.set_bitmapData (v); }});

// Statics




// Export

exports.default = Bitmap;