package lib.ui; import kernel.ui.Pixel; import lib.ui.Canvas; /** Layer multiple canvases and compress them into one from a top down view. **/ class CanvasStack { private var layers:Array<{c:Canvas, offset:Pos}> = []; private var width:Int = 0; private var hight:Int = 0; public function new() {} public function addLayer(c:Canvas, offset:Pos) { this.layers.push({c: c, offset: offset}); var layerWidth = c.maxWidth() + offset.x; var layerHight = c.height() + offset.y; if (layerWidth > this.width) { this.width = layerWidth; } if (layerHight > this.hight) { this.hight = layerHight; } } public function compress():Canvas { var rtn = new Canvas(); for (y in 0...this.hight) { for (x in 0...this.width) { var pos = new Pos({x: x, y: y}); rtn.set(pos, this.pixelAt(pos)); } } return rtn; } public function pixelAt(pos:Pos):Null { for (i in 0...this.layers.length) { var newi = (this.layers.length - 1) - i; var offset = this.layers[newi].offset; var px = this.layers[newi].c.get(pos - offset); if (px != null) { return px; } } return null; } }