From d56b8715546221d12055eabc13c48cc3c753d56a Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Sat, 4 Feb 2023 20:41:10 +0100 Subject: [PATCH] improved canvas render to context --- src/lib/ui/Canvas.hx | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/src/lib/ui/Canvas.hx b/src/lib/ui/Canvas.hx index 67e5396..e9f4d56 100644 --- a/src/lib/ui/Canvas.hx +++ b/src/lib/ui/Canvas.hx @@ -73,21 +73,54 @@ abstract Canvas(Array>) to Array> from Array line in this) { if (line == null || line.length == 0) { + // Line is empty continue; } + ctx.setCursorPos(0, lineIndex); + + var pritableLine = ""; + for (pixelIndex => pixel in line) { 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; } - ctx.setCursorPos(pixelIndex, lineIndex); - ctx.setBackgroundColor(pixel.bg); - ctx.setTextColor(pixel.textColor); - ctx.write(pixel.char); + if (pixel.bg != lastBgColor) { + // The background color has changed, we need to print the current line and set the new background color + ctx.write(pritableLine); + 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); } } }