// Class: openfl.display.Tile

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;
function openfl_display_ITile() {return require("./../../openfl/display/ITile");}
function openfl_geom_ColorTransform() {return require("./../../openfl/geom/ColorTransform");}
function openfl_geom_Matrix() {return require("./../../openfl/geom/Matrix");}

// Constructor

var Tile = function(id,x,y,scaleX,scaleY,rotation,originX,originY) {
	if(originY == null) {
		originY = 0;
	}
	if(originX == null) {
		originX = 0;
	}
	if(rotation == null) {
		rotation = 0;
	}
	if(scaleY == null) {
		scaleY = 1;
	}
	if(scaleX == null) {
		scaleX = 1;
	}
	if(y == null) {
		y = 0;
	}
	if(x == null) {
		x = 0;
	}
	if(id == null) {
		id = 0;
	}
	this.__id = id;
	this.__matrix = new (openfl_geom_Matrix().default)();
	if(x != 0) {
		this.set_x(x);
	}
	if(y != 0) {
		this.set_y(y);
	}
	if(scaleX != 1) {
		this.set_scaleX(scaleX);
	}
	if(scaleY != 1) {
		this.set_scaleY(scaleY);
	}
	if(rotation != 0) {
		this.set_rotation(rotation);
	}
	this.__originX = originX;
	this.__originY = originY;
	this.__alpha = 1;
	this.__visible = true;
	this.__alphaDirty = true;
	this.__sourceDirty = true;
	this.__transformDirty = true;
	this.__visibleDirty = true;
}

// Meta

Tile.__name__ = ["openfl","display","Tile"];
Tile.__interfaces__ = [(openfl_display_ITile().default)];
Tile.prototype = {
	clone: function() {
		var tile = new Tile(this.__id);
		tile.set_matrix(this.__matrix.clone());
		tile.set_tileset(this.__tileset);
		return tile;
	},
	__setRenderDirty: function() {
		if(this.parent != null) {
			this.parent.__setRenderDirty();
		}
	},
	__updateTileArray: function(position,tileArray,forceUpdate) {
		var cachePosition = tileArray.position;
		tileArray.position = position;
		if(this.__shaderDirty || forceUpdate) {
			tileArray.set_shader(this.__shader);
			this.__shaderDirty = false;
		}
		if(this.__colorTransformDirty || forceUpdate) {
			tileArray.set_colorTransform(this.__colorTransform);
			this.__colorTransformDirty = false;
		}
		if(this.__visibleDirty || forceUpdate) {
			tileArray.set_visible(this.__visible);
			tileArray.__bufferDirty = true;
			this.__visibleDirty = false;
		}
		if(this.__alphaDirty || forceUpdate) {
			tileArray.set_alpha(this.__alpha);
			tileArray.__bufferDirty = true;
			this.__alphaDirty = false;
		}
		if(this.__sourceDirty || forceUpdate) {
			if(this.__rect == null) {
				tileArray.set_id(this.__id);
			} else {
				tileArray.set_rect(this.get_rect());
			}
			tileArray.set_tileset(this.__tileset);
			tileArray.__bufferDirty = true;
			this.__sourceDirty = true;
		}
		if(this.__transformDirty || forceUpdate) {
			if(this.__originX != 0 || this.__originY != 0) {
				Tile.__tempMatrix.setTo(1,0,0,1,-this.__originX,-this.__originY);
				Tile.__tempMatrix.concat(this.__matrix);
				tileArray.set_matrix(Tile.__tempMatrix);
			} else {
				tileArray.set_matrix(this.__matrix);
			}
			tileArray.__bufferDirty = true;
			this.__transformDirty = false;
		}
		tileArray.position = cachePosition;
	},
	get_alpha: function() {
		return this.__alpha;
	},
	set_alpha: function(value) {
		this.__alphaDirty = true;
		this.__setRenderDirty();
		return this.__alpha = value;
	},
	get_colorTransform: function() {
		if(this.__colorTransform == null) {
			this.__colorTransform = new (openfl_geom_ColorTransform().default)();
		}
		return this.__colorTransform;
	},
	set_colorTransform: function(value) {
		if(this.__colorTransform == null) {
			if(value != null) {
				this.__colorTransform = value.__clone();
			}
		} else if(value != null) {
			this.__colorTransform.__copyFrom(value);
		} else {
			this.__colorTransform.__identity();
		}
		this.__colorTransformDirty = true;
		this.__setRenderDirty();
		return value;
	},
	get_id: function() {
		return this.__id;
	},
	set_id: function(value) {
		this.__sourceDirty = true;
		this.__setRenderDirty();
		return this.__id = value;
	},
	get_matrix: function() {
		return this.__matrix;
	},
	set_matrix: function(value) {
		this.__rotation = null;
		this.__scaleX = null;
		this.__scaleY = null;
		this.__transformDirty = true;
		this.__setRenderDirty();
		return this.__matrix = value;
	},
	get_originX: function() {
		return this.__originX;
	},
	set_originX: function(value) {
		this.__transformDirty = true;
		this.__setRenderDirty();
		return this.__originX = value;
	},
	get_originY: function() {
		return this.__originY;
	},
	set_originY: function(value) {
		this.__transformDirty = true;
		this.__setRenderDirty();
		return this.__originY = value;
	},
	get_rect: function() {
		return this.__rect;
	},
	set_rect: function(value) {
		this.__sourceDirty = true;
		this.__setRenderDirty();
		return this.__rect = value;
	},
	get_rotation: function() {
		if(this.__rotation == null) {
			if(this.__matrix.b == 0 && this.__matrix.c == 0) {
				this.__rotation = 0;
				this.__rotationSine = 0;
				this.__rotationCosine = 1;
			} else {
				var radians = Math.atan2(this.__matrix.d,this.__matrix.c) - Math.PI / 2;
				this.__rotation = radians * (180 / Math.PI);
				this.__rotationSine = Math.sin(radians);
				this.__rotationCosine = Math.cos(radians);
			}
		}
		return this.__rotation;
	},
	set_rotation: function(value) {
		if(value != this.__rotation) {
			this.__rotation = value;
			var radians = value * (Math.PI / 180);
			this.__rotationSine = Math.sin(radians);
			this.__rotationCosine = Math.cos(radians);
			var __scaleX = this.get_scaleX();
			var __scaleY = this.get_scaleY();
			this.__matrix.a = this.__rotationCosine * __scaleX;
			this.__matrix.b = this.__rotationSine * __scaleX;
			this.__matrix.c = -this.__rotationSine * __scaleY;
			this.__matrix.d = this.__rotationCosine * __scaleY;
			this.__transformDirty = true;
			this.__setRenderDirty();
		}
		return value;
	},
	get_scaleX: function() {
		if(this.__scaleX == null) {
			if(this.get_matrix().b == 0) {
				this.__scaleX = this.__matrix.a;
			} else {
				this.__scaleX = Math.sqrt(this.__matrix.a * this.__matrix.a + this.__matrix.b * this.__matrix.b);
			}
		}
		return this.__scaleX;
	},
	set_scaleX: function(value) {
		if(this.__scaleX != value) {
			this.__scaleX = value;
			if(this.__matrix.b == 0) {
				this.__matrix.a = value;
			} else {
				var rotation = this.get_rotation();
				var a = this.__rotationCosine * value;
				var b = this.__rotationSine * value;
				this.__matrix.a = a;
				this.__matrix.b = b;
			}
			this.__transformDirty = true;
			this.__setRenderDirty();
		}
		return value;
	},
	get_scaleY: function() {
		if(this.__scaleY == null) {
			if(this.__matrix.c == 0) {
				this.__scaleY = this.get_matrix().d;
			} else {
				this.__scaleY = Math.sqrt(this.__matrix.c * this.__matrix.c + this.__matrix.d * this.__matrix.d);
			}
		}
		return this.__scaleY;
	},
	set_scaleY: function(value) {
		if(this.__scaleY != value) {
			this.__scaleY = value;
			if(this.__matrix.c == 0) {
				this.__matrix.d = value;
			} else {
				var rotation = this.get_rotation();
				var c = -this.__rotationSine * value;
				var d = this.__rotationCosine * value;
				this.__matrix.c = c;
				this.__matrix.d = d;
			}
			this.__transformDirty = true;
			this.__setRenderDirty();
		}
		return value;
	},
	get_shader: function() {
		return this.__shader;
	},
	set_shader: function(value) {
		this.__shaderDirty = true;
		this.__setRenderDirty();
		return this.__shader = value;
	},
	get_tileset: function() {
		return this.__tileset;
	},
	set_tileset: function(value) {
		this.__sourceDirty = true;
		this.__setRenderDirty();
		return this.__tileset = value;
	},
	get_visible: function() {
		return this.__visible;
	},
	set_visible: function(value) {
		this.__visibleDirty = true;
		this.__setRenderDirty();
		return this.__visible = value;
	},
	get_x: function() {
		return this.__matrix.tx;
	},
	set_x: function(value) {
		this.__transformDirty = true;
		this.__setRenderDirty();
		return this.__matrix.tx = value;
	},
	get_y: function() {
		return this.__matrix.ty;
	},
	set_y: function(value) {
		this.__transformDirty = true;
		this.__setRenderDirty();
		return this.__matrix.ty = value;
	}
};
Tile.prototype.__class__ = $hxClasses["openfl.display.Tile"] = Tile;

// Init

Object.defineProperties(Tile.prototype,{ "alpha" : { get : function () { return this.get_alpha (); }, set : function (v) { return this.set_alpha (v); }}, "colorTransform" : { get : function () { return this.get_colorTransform (); }, set : function (v) { return this.set_colorTransform (v); }}, "id" : { get : function () { return this.get_id (); }, set : function (v) { return this.set_id (v); }}, "matrix" : { get : function () { return this.get_matrix (); }, set : function (v) { return this.set_matrix (v); }}, "originX" : { get : function () { return this.get_originX (); }, set : function (v) { return this.set_originX (v); }}, "originY" : { get : function () { return this.get_originY (); }, set : function (v) { return this.set_originY (v); }}, "rect" : { get : function () { return this.get_rect (); }, set : function (v) { return this.set_rect (v); }}, "rotation" : { get : function () { return this.get_rotation (); }, set : function (v) { return this.set_rotation (v); }}, "scaleX" : { get : function () { return this.get_scaleX (); }, set : function (v) { return this.set_scaleX (v); }}, "scaleY" : { get : function () { return this.get_scaleY (); }, set : function (v) { return this.set_scaleY (v); }}, "shader" : { get : function () { return this.get_shader (); }, set : function (v) { return this.set_shader (v); }}, "tileset" : { get : function () { return this.get_tileset (); }, set : function (v) { return this.set_tileset (v); }}, "visible" : { get : function () { return this.get_visible (); }, set : function (v) { return this.set_visible (v); }}, "x" : { get : function () { return this.get_x (); }, set : function (v) { return this.set_x (v); }}, "y" : { get : function () { return this.get_y (); }, set : function (v) { return this.set_y (v); }}});

// Statics

Tile.__fromTileArray = function(position,tileArray) {
	var cachePosition = tileArray.position;
	tileArray.position = position;
	var tile = new Tile();
	tile.set_alpha(tileArray.get_alpha());
	tile.set_id(tileArray.get_id());
	tileArray.set_matrix(tile.get_matrix());
	tileArray.position = cachePosition;
	return tile;
}
Tile.__tempMatrix = new (openfl_geom_Matrix().default)()

// Export

exports.default = Tile;