Rename Canvas::CellType::kText and simplify.

This commit is contained in:
ArthurSonzogni 2024-04-11 23:39:51 +02:00
parent 94b0e97900
commit 6e613520c6
No known key found for this signature in database
GPG Key ID: 41D98248C074CD6C
2 changed files with 28 additions and 37 deletions

View File

@ -96,7 +96,8 @@ struct Canvas {
void DrawText(int x, int y, const std::string& value, const Stylizer& style); void DrawText(int x, int y, const std::string& value, const Stylizer& style);
// Draw using directly pixels or images -------------------------------------- // Draw using directly pixels or images --------------------------------------
// Pixel/image coordinates correspond 1:1 // x is considered to be a multiple of 2.
// y is considered to be a multiple of 4.
void DrawPixel(int x, int y, const Pixel&); void DrawPixel(int x, int y, const Pixel&);
void DrawImage(int x, int y, const Image&); void DrawImage(int x, int y, const Image&);
@ -109,15 +110,18 @@ struct Canvas {
bool IsIn(int x, int y) const { bool IsIn(int x, int y) const {
return x >= 0 && x < width_ && y >= 0 && y < height_; return x >= 0 && x < width_ && y >= 0 && y < height_;
} }
enum CellType { enum CellType {
kBraille, kCell, // Units of size 2x4
kBlock, kBlock, // Units of size 2x2
kText, kBraille, // Units of size 1x1
}; };
struct Cell { struct Cell {
CellType type = kText; CellType type = kCell;
Pixel content; Pixel content;
}; };
struct XY { struct XY {
int x; int x;
int y; int y;

View File

@ -810,7 +810,7 @@ void Canvas::DrawText(int x,
continue; continue;
} }
Cell& cell = storage_[XY{x / 2, y / 4}]; Cell& cell = storage_[XY{x / 2, y / 4}];
cell.type = CellType::kText; cell.type = CellType::kCell;
cell.content.character = it; cell.content.character = it;
style(cell.content); style(cell.content);
x += 2; x += 2;
@ -821,11 +821,9 @@ void Canvas::DrawText(int x,
/// @param x the x coordinate of the pixel. /// @param x the x coordinate of the pixel.
/// @param y the y coordinate of the pixel. /// @param y the y coordinate of the pixel.
/// @param p the pixel to draw. /// @param p the pixel to draw.
void Canvas::DrawPixel(int x, void Canvas::DrawPixel(int x, int y, const Pixel& p) {
int y, Cell& cell = storage_[XY{x / 2, y / 4}];
const Pixel& p) { cell.type = CellType::kCell;
Cell& cell = storage_[XY{x, y}];
cell.type = CellType::kText; // Epixu: should we add kCustom or something?
cell.content = p; cell.content = p;
} }
@ -835,33 +833,22 @@ void Canvas::DrawPixel(int x,
/// @param x the x coordinate corresponding to the top-left corner of the image. /// @param x the x coordinate corresponding to the top-left corner of the image.
/// @param y the y coordinate corresponding to the top-left corner of the image. /// @param y the y coordinate corresponding to the top-left corner of the image.
/// @param image the image to draw. /// @param image the image to draw.
void Canvas::DrawImage(int x, void Canvas::DrawImage(int x, int y, const Image& image) {
int y, x /= 2;
const Image& image) { y /= 4;
Box crop = image.stencil; const int dx_begin = std::max(0, -x);
const int dy_begin = std::max(0, -y);
const int dx_end = std::min(image.dimx(), width_ - x);
const int dy_end = std::min(image.dimy(), height_ - y);
if (x < 0) { for (int dy = dy_begin; dy < dy_end; ++dy) {
crop.x_min -= x; for (int dx = dx_begin; dx < dx_end; ++dx) {
x = 0; Cell& cell = storage_[XY{
} x + dx,
y + dy,
if (y < 0) { }];
crop.y_min -= y; cell.type = CellType::kCell;
y = 0; cell.content = image.PixelAt(dx, dy);
}
if (crop.IsEmpty()) {
return;
}
const auto xend = x + crop.x_max - crop.x_min;
const auto yend = y + crop.y_max - crop.y_min;
for (int py = y; py < yend; ++py) {
for (int px = x; px < xend; ++px) {
Cell& cell = storage_[XY{px, py}];
cell.type = CellType::kText; // Epixu: should we add kCustom or something?
cell.content = image.PixelAt(crop.x_min + px - x, crop.y_min + py - y);
} }
} }
} }