improved canvas render to context
This commit is contained in:
parent
8934d40c0b
commit
d56b871554
@ -73,21 +73,54 @@ abstract Canvas(Array<Array<Pixel>>) to Array<Array<Pixel>> from Array<Array<Pix
|
|||||||
Renders the canvas directly to the context
|
Renders the canvas directly to the context
|
||||||
**/
|
**/
|
||||||
public function renderToContext(ctx: WindowContext){
|
public function renderToContext(ctx: WindowContext){
|
||||||
|
var lastBgColor: Color = null;
|
||||||
|
var lastTextColor: Color = null;
|
||||||
|
|
||||||
for (lineIndex => line in this) {
|
for (lineIndex => line in this) {
|
||||||
if (line == null || line.length == 0) {
|
if (line == null || line.length == 0) {
|
||||||
|
// Line is empty
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ctx.setCursorPos(0, lineIndex);
|
||||||
|
|
||||||
|
var pritableLine = "";
|
||||||
|
|
||||||
for (pixelIndex => pixel in line) {
|
for (pixelIndex => pixel in line) {
|
||||||
if (pixel == null) {
|
if (pixel == null) {
|
||||||
|
// If the pixel is null we need to skip it with setCurosrPos.
|
||||||
|
// Otherwise we will print an empty space with bg color
|
||||||
|
ctx.write(pritableLine);
|
||||||
|
pritableLine = "";
|
||||||
|
ctx.setCursorPos(pixelIndex + 1, lineIndex);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.setCursorPos(pixelIndex, lineIndex);
|
if (pixel.bg != lastBgColor) {
|
||||||
ctx.setBackgroundColor(pixel.bg);
|
// The background color has changed, we need to print the current line and set the new background color
|
||||||
ctx.setTextColor(pixel.textColor);
|
ctx.write(pritableLine);
|
||||||
ctx.write(pixel.char);
|
pritableLine = "";
|
||||||
|
|
||||||
|
// Set the new background color
|
||||||
|
ctx.setBackgroundColor(pixel.bg);
|
||||||
|
lastBgColor = pixel.bg;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Same as above but for the text color
|
||||||
|
if (pixel.textColor != lastTextColor) {
|
||||||
|
// The text color has changed, we need to print the current line and set the new text color
|
||||||
|
ctx.write(pritableLine);
|
||||||
|
pritableLine = "";
|
||||||
|
|
||||||
|
// Set the new text color
|
||||||
|
ctx.setTextColor(pixel.textColor);
|
||||||
|
lastTextColor = pixel.textColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
pritableLine += pixel.char;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ctx.write(pritableLine);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user