// Class: openfl.display.TileArray

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_display__$TileArray_TileArrayIterator() {return require("./../../openfl/display/_TileArray/TileArrayIterator");}
function openfl__$Vector_Vector_$Impl_$() {return require("./../../openfl/_Vector/Vector_Impl_");}
function lime_utils__$DataPointer_DataPointer_$Impl_$() {return require("./../../lime/utils/_DataPointer/DataPointer_Impl_");}
function openfl_geom_ColorTransform() {return require("./../../openfl/geom/ColorTransform");}
function Std() {return require("./../../Std");}
function openfl_geom_Matrix() {return require("./../../openfl/geom/Matrix");}
function openfl_geom_Rectangle() {return require("./../../openfl/geom/Rectangle");}

// Constructor

var TileArray = function(length) {
	if(length == null) {
		length = 0;
	}
	this.__cacheAlpha = -1;
	this.__data = (openfl__$Vector_Vector_$Impl_$().default)._new(length * 21);
	this.__dirty = (openfl__$Vector_Vector_$Impl_$().default)._new(length * 5);
	this.__shaders = (openfl__$Vector_Vector_$Impl_$().default)._new(length);
	this.__tilesets = (openfl__$Vector_Vector_$Impl_$().default)._new(length);
	this.__visible = (openfl__$Vector_Vector_$Impl_$().default)._new(length);
	this.__length = length;
}

// Meta

TileArray.__name__ = ["openfl","display","TileArray"];
TileArray.__interfaces__ = [(openfl_display_ITile().default)];
TileArray.prototype = {
	iterator: function() {
		return new (openfl_display__$TileArray_TileArrayIterator().default)(this);
	},
	__init: function(position) {
		this.position = position;
		this.set_alpha(1);
		this.set_colorTransform(null);
		this.set_id(0);
		this.set_matrix(null);
		this.set_tileset(null);
		this.set_visible(true);
		this.__dirty.set(4 + position * 5,true);
	},
	__updateGLBuffer: function(gl,defaultTileset,worldAlpha,defaultColorTransform) {
		var _gthis = this;
		var attributeLength = 25;
		var stride = attributeLength * 6;
		var bufferLength = this.__length * stride;
		if(this.__bufferData == null) {
			var this1;
			if(bufferLength != null) {
				this1 = new Float32Array(bufferLength);
			} else {
				this1 = null;
			}
			this.__bufferData = this1;
			this.__bufferSkipped = (openfl__$Vector_Vector_$Impl_$().default)._new(this.__length);
			this.__bufferDirty = true;
		} else if(this.__bufferData.length != bufferLength) {
			var this2;
			if(bufferLength != null) {
				this2 = new Float32Array(bufferLength);
			} else {
				this2 = null;
			}
			var data = this2;
			if(this.__bufferData.length <= data.length) {
				data.set(this.__bufferData);
				if(this.__bufferData.length == 0) {
					this.__bufferDirty = true;
				} else {
					var cacheLength = this.__bufferData.length;
					var _g1 = cacheLength;
					var _g = bufferLength;
					while(_g1 < _g) {
						var i = _g1++;
						this.__dirty.set(4 + this.position * 5,true);
					}
				}
			} else {
				data.set(this.__bufferData.subarray(0,data.length));
			}
			this.__bufferData = data;
			this.__bufferSkipped.set_length(this.__length);
			this.__bufferDirty = true;
		}
		if(this.__buffer == null || this.__bufferContext != gl) {
			this.__bufferContext = gl;
			this.__buffer = gl.createBuffer();
		}
		gl.bindBuffer(gl.ARRAY_BUFFER,this.__buffer);
		if(this.__bufferDirty || this.__cacheAlpha != worldAlpha || this.__cacheDefaultTileset != defaultTileset) {
			var tileMatrix;
			var tileColorTransform;
			var tileRect = null;
			var offset = 0;
			var alpha;
			var visible;
			var tileset;
			var tileData;
			var id;
			var bitmapWidth;
			var bitmapHeight;
			var tileWidth;
			var tileHeight;
			var uvX;
			var uvY;
			var uvWidth;
			var uvHeight;
			var x;
			var y;
			var x2;
			var y2;
			var x3;
			var y3;
			var x4;
			var y4;
			var redMultiplier;
			var greenMultiplier;
			var blueMultiplier;
			var alphaMultiplier;
			var redOffset;
			var greenOffset;
			var blueOffset;
			var alphaOffset;
			this.position = 0;
			var __skipTile = function(i1,offset1) {
				var _g2 = 0;
				while(_g2 < 6) {
					var i2 = _g2++;
					_gthis.__bufferData[offset1 + attributeLength * i2 + 4] = 0;
				}
				_gthis.__bufferSkipped.set(i1,true);
			};
			var _g11 = 0;
			var _g3 = this.__length;
			while(_g11 < _g3) {
				var i3 = _g11++;
				this.position = i3;
				offset = i3 * stride;
				alpha = this.get_alpha();
				visible = this.get_visible();
				if(!visible || alpha <= 0) {
					__skipTile(i3,offset);
					continue;
				}
				tileset = this.get_tileset();
				if(tileset == null) {
					tileset = defaultTileset;
				}
				if(tileset == null) {
					__skipTile(i3,offset);
					continue;
				}
				id = this.get_id();
				if(id > -1) {
					if(id >= tileset.__data.length) {
						__skipTile(i3,offset);
						continue;
					}
					tileData = tileset.__data[id];
					if(tileData == null) {
						__skipTile(i3,offset);
						continue;
					}
					tileWidth = tileData.width;
					tileHeight = tileData.height;
					uvX = tileData.__uvX;
					uvY = tileData.__uvY;
					uvWidth = tileData.__uvWidth;
					uvHeight = tileData.__uvHeight;
				} else {
					tileRect = this.get_rect();
					if(tileRect == null) {
						__skipTile(i3,offset);
						continue;
					}
					tileWidth = tileRect.width;
					tileHeight = tileRect.height;
					if(tileWidth <= 0 || tileHeight <= 0) {
						__skipTile(i3,offset);
						continue;
					}
					bitmapWidth = tileset.__bitmapData.width;
					bitmapHeight = tileset.__bitmapData.height;
					uvX = tileRect.x / bitmapWidth;
					uvY = tileRect.y / bitmapHeight;
					uvWidth = tileRect.get_right() / bitmapWidth;
					uvHeight = tileRect.get_bottom() / bitmapHeight;
				}
				tileMatrix = this.get_matrix();
				x = tileMatrix.__transformX(0,0);
				y = tileMatrix.__transformY(0,0);
				x2 = tileMatrix.__transformX(tileWidth,0);
				y2 = tileMatrix.__transformY(tileWidth,0);
				x3 = tileMatrix.__transformX(0,tileHeight);
				y3 = tileMatrix.__transformY(0,tileHeight);
				x4 = tileMatrix.__transformX(tileWidth,tileHeight);
				y4 = tileMatrix.__transformY(tileWidth,tileHeight);
				alpha *= worldAlpha;
				tileColorTransform = this.get_colorTransform();
				tileColorTransform.__combine(defaultColorTransform);
				redMultiplier = tileColorTransform.redMultiplier;
				greenMultiplier = tileColorTransform.greenMultiplier;
				blueMultiplier = tileColorTransform.blueMultiplier;
				alphaMultiplier = tileColorTransform.alphaMultiplier;
				redOffset = tileColorTransform.redOffset;
				greenOffset = tileColorTransform.greenOffset;
				blueOffset = tileColorTransform.blueOffset;
				alphaOffset = tileColorTransform.alphaOffset;
				this.__bufferData[offset] = x;
				this.__bufferData[offset + 1] = y;
				this.__bufferData[offset + 2] = uvX;
				this.__bufferData[offset + 3] = uvY;
				this.__bufferData[offset + attributeLength] = x2;
				this.__bufferData[offset + attributeLength + 1] = y2;
				this.__bufferData[offset + attributeLength + 2] = uvWidth;
				this.__bufferData[offset + attributeLength + 3] = uvY;
				this.__bufferData[offset + attributeLength * 2] = x3;
				this.__bufferData[offset + attributeLength * 2 + 1] = y3;
				this.__bufferData[offset + attributeLength * 2 + 2] = uvX;
				this.__bufferData[offset + attributeLength * 2 + 3] = uvHeight;
				this.__bufferData[offset + attributeLength * 3] = x3;
				this.__bufferData[offset + attributeLength * 3 + 1] = y3;
				this.__bufferData[offset + attributeLength * 3 + 2] = uvX;
				this.__bufferData[offset + attributeLength * 3 + 3] = uvHeight;
				this.__bufferData[offset + attributeLength * 4] = x2;
				this.__bufferData[offset + attributeLength * 4 + 1] = y2;
				this.__bufferData[offset + attributeLength * 4 + 2] = uvWidth;
				this.__bufferData[offset + attributeLength * 4 + 3] = uvY;
				this.__bufferData[offset + attributeLength * 5] = x4;
				this.__bufferData[offset + attributeLength * 5 + 1] = y4;
				this.__bufferData[offset + attributeLength * 5 + 2] = uvWidth;
				this.__bufferData[offset + attributeLength * 5 + 3] = uvHeight;
				var _g21 = 0;
				while(_g21 < 6) {
					var i4 = _g21++;
					this.__bufferData[offset + attributeLength * i4 + 4] = alpha;
					this.__bufferData[offset + attributeLength * i4 + 5] = redMultiplier;
					this.__bufferData[offset + attributeLength * i4 + 10] = greenMultiplier;
					this.__bufferData[offset + attributeLength * i4 + 15] = blueMultiplier;
					this.__bufferData[offset + attributeLength * i4 + 20] = alphaMultiplier;
					this.__bufferData[offset + attributeLength * i4 + 21] = redOffset / 255;
					this.__bufferData[offset + attributeLength * i4 + 22] = greenOffset / 255;
					this.__bufferData[offset + attributeLength * i4 + 23] = blueOffset / 255;
					this.__bufferData[offset + attributeLength * i4 + 24] = alphaOffset / 255;
				}
				this.__bufferSkipped.set(i3,false);
			}
			gl.bufferData(gl.ARRAY_BUFFER,this.__bufferData.byteLength,(lime_utils__$DataPointer_DataPointer_$Impl_$().default).fromArrayBufferView(this.__bufferData),gl.DYNAMIC_DRAW);
			this.__cacheAlpha = worldAlpha;
			this.__cacheDefaultTileset = defaultTileset;
			this.__bufferDirty = false;
		}
		return this.__buffer;
	},
	get_alpha: function() {
		return this.__data.get(11 + this.position * 21);
	},
	set_alpha: function(value) {
		this.__dirty.set(2 + this.position * 5,true);
		return this.__data.set(11 + this.position * 21,value);
	},
	get_colorTransform: function() {
		if(this.__colorTransform == null) {
			this.__colorTransform = new (openfl_geom_ColorTransform().default)();
		}
		var i = 12 + this.position * 21;
		var tmp = this.__data.get(i);
		this.__colorTransform.redMultiplier = tmp;
		var tmp1 = this.__data.get(i + 1);
		this.__colorTransform.greenMultiplier = tmp1;
		var tmp2 = this.__data.get(i + 2);
		this.__colorTransform.blueMultiplier = tmp2;
		var tmp3 = this.__data.get(i + 3);
		this.__colorTransform.alphaMultiplier = tmp3;
		var tmp4 = this.__data.get(i + 4);
		this.__colorTransform.redOffset = tmp4;
		var tmp5 = this.__data.get(i + 5);
		this.__colorTransform.greenOffset = tmp5;
		var tmp6 = this.__data.get(i + 6);
		this.__colorTransform.blueOffset = tmp6;
		var tmp7 = this.__data.get(i + 7);
		this.__colorTransform.alphaOffset = tmp7;
		return this.__colorTransform;
	},
	set_colorTransform: function(value) {
		var i = 12 + this.position * 21;
		if(value != null) {
			this.__data.set(i,value.redMultiplier);
			this.__data.set(i + 1,value.greenMultiplier);
			this.__data.set(i + 2,value.blueMultiplier);
			this.__data.set(i + 3,value.alphaMultiplier);
			this.__data.set(i + 4,value.redOffset);
			this.__data.set(i + 5,value.greenOffset);
			this.__data.set(i + 6,value.blueOffset);
			this.__data.set(i + 7,value.alphaOffset);
		} else {
			this.__data.set(i,1);
			this.__data.set(i + 1,1);
			this.__data.set(i + 2,1);
			this.__data.set(i + 3,1);
			this.__data.set(i + 4,0);
			this.__data.set(i + 5,0);
			this.__data.set(i + 6,0);
			this.__data.set(i + 7,0);
		}
		this.__dirty.set(3 + this.position * 5,true);
		return value;
	},
	get_id: function() {
		return (Std().default)["int"](this.__data.get(this.position * 21));
	},
	set_id: function(value) {
		this.__dirty.set(this.position * 5,true);
		this.__data.set(this.position * 21,value);
		return value;
	},
	get_length: function() {
		return this.__length;
	},
	set_length: function(value) {
		this.__data.set_length(value * 21);
		this.__dirty.set_length(value * 5);
		this.__shaders.set_length(value);
		this.__tilesets.set_length(value);
		this.__visible.set_length(value);
		if(value > this.__length) {
			var cachePosition = this.position;
			var _g1 = this.__length;
			var _g = value;
			while(_g1 < _g) {
				var i = _g1++;
				this.__init(i);
			}
			this.position = cachePosition;
		}
		this.__length = value;
		return value;
	},
	get_matrix: function() {
		if(this.__matrix == null) {
			this.__matrix = new (openfl_geom_Matrix().default)();
		}
		var i = 5 + this.position * 21;
		var tmp = this.__data.get(i);
		this.__matrix.a = tmp;
		var tmp1 = this.__data.get(i + 1);
		this.__matrix.b = tmp1;
		var tmp2 = this.__data.get(i + 2);
		this.__matrix.c = tmp2;
		var tmp3 = this.__data.get(i + 3);
		this.__matrix.d = tmp3;
		var tmp4 = this.__data.get(i + 4);
		this.__matrix.tx = tmp4;
		var tmp5 = this.__data.get(i + 5);
		this.__matrix.ty = tmp5;
		return this.__matrix;
	},
	set_matrix: function(value) {
		var i = 5 + this.position * 21;
		if(value != null) {
			this.__data.set(i,value.a);
			this.__data.set(i + 1,value.b);
			this.__data.set(i + 2,value.c);
			this.__data.set(i + 3,value.d);
			this.__data.set(i + 4,value.tx);
			this.__data.set(i + 5,value.ty);
		} else {
			this.__data.set(i,1);
			this.__data.set(i + 1,0);
			this.__data.set(i + 2,0);
			this.__data.set(i + 3,1);
			this.__data.set(i + 4,0);
			this.__data.set(i + 5,0);
		}
		this.__dirty.set(1 + this.position * 5,true);
		return value;
	},
	get_rect: function() {
		if(this.__rect == null) {
			this.__rect = new (openfl_geom_Rectangle().default)();
		}
		var i = 1 + this.position * 21;
		var tmp = this.__data.get(i);
		this.__rect.x = tmp;
		var tmp1 = this.__data.get(i + 1);
		this.__rect.y = tmp1;
		var tmp2 = this.__data.get(i + 2);
		this.__rect.width = tmp2;
		var tmp3 = this.__data.get(i + 3);
		this.__rect.height = tmp3;
		return this.__rect;
	},
	set_rect: function(value) {
		if(value != null) {
			this.__data.set(this.position * 21,-1);
			var i = 1 + this.position * 21;
			this.__data.set(i,value.x);
			this.__data.set(i + 1,value.y);
			this.__data.set(i + 2,value.width);
			this.__data.set(i + 3,value.height);
		} else {
			var i1 = 1 + this.position * 21;
			this.__data.set(i1,0);
			this.__data.set(i1 + 1,0);
			this.__data.set(i1 + 2,0);
			this.__data.set(i1 + 3,0);
		}
		this.__dirty.set(this.position * 5,true);
		return value;
	},
	get_shader: function() {
		return this.__shaders.get(this.position);
	},
	set_shader: function(value) {
		this.__shaders.set(this.position,value);
		return value;
	},
	get_tileset: function() {
		return this.__tilesets.get(this.position);
	},
	set_tileset: function(value) {
		this.__tilesets.set(this.position,value);
		return value;
	},
	get_visible: function() {
		return this.__visible.get(this.position);
	},
	set_visible: function(value) {
		this.__visible.set(this.position,value);
		return value;
	}
};
TileArray.prototype.__class__ = $hxClasses["openfl.display.TileArray"] = TileArray;

// Init

Object.defineProperties(TileArray.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); }}, "length" : { get : function () { return this.get_length (); }, set : function (v) { return this.set_length (v); }}, "matrix" : { get : function () { return this.get_matrix (); }, set : function (v) { return this.set_matrix (v); }}, "rect" : { get : function () { return this.get_rect (); }, set : function (v) { return this.set_rect (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); }}});

// Statics


TileArray.ID_INDEX = 0
TileArray.RECT_INDEX = 1
TileArray.MATRIX_INDEX = 5
TileArray.ALPHA_INDEX = 11
TileArray.COLOR_TRANSFORM_INDEX = 12
TileArray.DATA_LENGTH = 21
TileArray.SOURCE_DIRTY_INDEX = 0
TileArray.MATRIX_DIRTY_INDEX = 1
TileArray.ALPHA_DIRTY_INDEX = 2
TileArray.COLOR_TRANSFORM_DIRTY_INDEX = 3
TileArray.ALL_DIRTY_INDEX = 4
TileArray.DIRTY_LENGTH = 5

// Export

exports.default = TileArray;