FTXUI  5.0.0
C++ functional terminal UI.
Loading...
Searching...
No Matches
image.cpp
Go to the documentation of this file.
1// Copyright 2020 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
4#include <sstream> // IWYU pragma: keep
5#include <string>
6#include <vector>
7
10
11namespace ftxui {
12
13namespace {
14Pixel& dev_null_pixel() {
15 static Pixel pixel;
16 return pixel;
17}
18} // namespace
19
20Image::Image(int dimx, int dimy)
21 : stencil{0, dimx - 1, 0, dimy - 1},
22 dimx_(dimx),
23 dimy_(dimy),
24 pixels_(dimy, std::vector<Pixel>(dimx)) {}
25
26/// @brief Access a character in a cell at a given position.
27/// @param x The cell position along the x-axis.
28/// @param y The cell position along the y-axis.
29std::string& Image::at(int x, int y) {
30 return PixelAt(x, y).character;
31}
32
33/// @brief Access a character in a cell at a given position.
34/// @param x The cell position along the x-axis.
35/// @param y The cell position along the y-axis.
36const std::string& Image::at(int x, int y) const {
37 return PixelAt(x, y).character;
38}
39
40/// @brief Access a cell (Pixel) at a given position.
41/// @param x The cell position along the x-axis.
42/// @param y The cell position along the y-axis.
43Pixel& Image::PixelAt(int x, int y) {
44 return stencil.Contain(x, y) ? pixels_[y][x] : dev_null_pixel();
45}
46
47/// @brief Access a cell (Pixel) at a given position.
48/// @param x The cell position along the x-axis.
49/// @param y The cell position along the y-axis.
50const Pixel& Image::PixelAt(int x, int y) const {
51 return stencil.Contain(x, y) ? pixels_[y][x] : dev_null_pixel();
52}
53
54/// @brief Clear all the pixel from the screen.
56 for (auto& line : pixels_) {
57 for (auto& cell : line) {
58 cell = Pixel();
59 }
60 }
61}
62
63} // namespace ftxui
Pixel & PixelAt(int x, int y)
Access a cell (Pixel) at a given position.
Definition image.cpp:43
std::string & at(int x, int y)
Access a character in a cell at a given position.
Definition image.cpp:29
Image()=delete
Box stencil
Definition image.hpp:38
void Clear()
Clear all the pixel from the screen.
Definition image.cpp:55
std::vector< std::vector< Pixel > > pixels_
Definition image.hpp:43
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:26
bool Contain(int x, int y) const
Definition box.cpp:35
A Unicode character and its associated style.
Definition pixel.hpp:15
std::string character
Definition pixel.hpp:43