class Test {
static function main() {
var tileSize = 50;
var tileMargin = 3;
for(grid in new GridIterator(6, 5)) {
var div = js.Browser.document.createDivElement();
div.style.width = tileSize + "px";
div.style.height = tileSize + "px";
div.style.position = "absolute";
div.style.left = (grid.x * (tileSize+tileMargin)) + "px";
div.style.top = (grid.y * (tileSize+tileMargin)) + "px";
div.style.background = "#333";
js.Browser.document.body.appendChild(div);
}
}
}
class GridIterator {
var gridWidth:Int = 0;
var gridHeight:Int = 0;
var i:Int = 0;
public inline function new(gridWidth:Int, gridHeight:Int) {
this.gridWidth = gridWidth;
this.gridHeight = gridHeight;
}
public inline function hasNext() {
return i < gridWidth * gridHeight;
}
public inline function next() {
return new GridIteratorObject(i++, gridWidth);
}
}
class GridIteratorObject {
public var index(default, null):Int;
public var x(default, null):Int;
public var y(default, null):Int;
public inline function new(index:Int, gridWidth:Int) {
this.index = index;
this.x = index % gridWidth;
this.y = Std.int(index / gridWidth);
}
}