Separate a reusable Image class from Screen (#834)

Co-authored-by: ArthurSonzogni <sonzogniarthur@gmail.com>
This commit is contained in:
Dimo Markov
2024-04-27 12:03:44 +03:00
committed by GitHub
parent 1f6e1101e8
commit 293ff179f6
12 changed files with 243 additions and 107 deletions

View File

@@ -42,11 +42,6 @@ namespace ftxui {
namespace {
Pixel& dev_null_pixel() {
static Pixel pixel;
return pixel;
}
#if defined(_WIN32)
void WindowsEmulateVT100Terminal() {
static bool done = false;
@@ -392,15 +387,11 @@ Screen Screen::Create(Dimensions dimension) {
return {dimension.dimx, dimension.dimy};
}
Screen::Screen(int dimx, int dimy)
: stencil{0, dimx - 1, 0, dimy - 1},
dimx_(dimx),
dimy_(dimy),
pixels_(dimy, std::vector<Pixel>(dimx)) {
Screen::Screen(int dimx, int dimy) : Image{dimx, dimy} {
#if defined(_WIN32)
// The placement of this call is a bit weird, however we can assume that
// anybody who instantiates a Screen object eventually wants to output
// something to the console.
// something to the console. If that is not the case, use an instance of Image instead.
// As we require UTF8 for all input/output operations we will just switch to
// UTF8 encoding here
SetConsoleOutputCP(CP_UTF8);
@@ -450,34 +441,6 @@ void Screen::Print() const {
std::cout << ToString() << '\0' << std::flush;
}
/// @brief Access a character in a cell at a given position.
/// @param x The cell position along the x-axis.
/// @param y The cell position along the y-axis.
std::string& Screen::at(int x, int y) {
return PixelAt(x, y).character;
}
/// @brief Access a character in a cell at a given position.
/// @param x The cell position along the x-axis.
/// @param y The cell position along the y-axis.
const std::string& Screen::at(int x, int y) const {
return PixelAt(x, y).character;
}
/// @brief Access a cell (Pixel) at a given position.
/// @param x The cell position along the x-axis.
/// @param y The cell position along the y-axis.
Pixel& Screen::PixelAt(int x, int y) {
return stencil.Contain(x, y) ? pixels_[y][x] : dev_null_pixel();
}
/// @brief Access a cell (Pixel) at a given position.
/// @param x The cell position along the x-axis.
/// @param y The cell position along the y-axis.
const Pixel& Screen::PixelAt(int x, int y) const {
return stencil.Contain(x, y) ? pixels_[y][x] : dev_null_pixel();
}
/// @brief Return a string to be printed in order to reset the cursor position
/// to the beginning of the screen.
///
@@ -517,11 +480,8 @@ std::string Screen::ResetPosition(bool clear) const {
/// @brief Clear all the pixel from the screen.
void Screen::Clear() {
for (auto& line : pixels_) {
for (auto& cell : line) {
cell = Pixel();
}
}
Image::Clear();
cursor_.x = dimx_ - 1;
cursor_.y = dimy_ - 1;