FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
surface.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 <algorithm>
5#include <sstream> // IWYU pragma: keep
6#include <string>
7#include <vector>
8
11
12namespace ftxui {
13
14namespace {
15Cell& dev_null_cell() {
16 static Cell cell;
17 return cell;
18}
19} // namespace
20
21Surface::Surface(int dimx, int dimy)
22 : stencil{0, dimx - 1, 0, dimy - 1},
23 dimx_(dimx),
24 dimy_(dimy),
25 cells_(static_cast<size_t>(dimx) * static_cast<size_t>(dimy)) {}
26
27/// @brief Access a character in a cell at a given position.
28/// @param x The cell position along the x-axis.
29/// @param y The cell position along the y-axis.
30std::string& Surface::at(int x, int y) {
31 return CellAt(x, y).character;
32}
33
34/// @brief Access a character in a cell at a given position.
35/// @param x The cell position along the x-axis.
36/// @param y The cell position along the y-axis.
37const std::string& Surface::at(int x, int y) const {
38 return CellAt(x, y).character;
39}
40
41/// @brief Access a cell (Cell) at a given position.
42/// @param x The cell position along the x-axis.
43/// @param y The cell position along the y-axis.
44Cell& Surface::CellAt(int x, int y) {
45 return stencil.Contain(x, y) ? FastCellAt(x, y) : dev_null_cell();
46}
47
48/// @brief Access a cell (Cell) at a given position.
49/// @param x The cell position along the x-axis.
50/// @param y The cell position along the y-axis.
51const Cell& Surface::CellAt(int x, int y) const {
52 return stencil.Contain(x, y) ? FastCellAt(x, y) : dev_null_cell();
53}
54
55/// @brief Access a cell (Cell) at a given position, without stencil check.
56/// @param x The cell position along the x-axis.
57/// @param y The cell position along the y-axis.
58Cell& Surface::FastCellAt(int x, int y) {
59 return cells_[static_cast<size_t>(y) * static_cast<size_t>(dimx_) +
60 static_cast<size_t>(x)];
61}
62
63/// @brief Access a cell (Cell) at a given position, without stencil check.
64/// @param x The cell position along the x-axis.
65/// @param y The cell position along the y-axis.
66const Cell& Surface::FastCellAt(int x, int y) const {
67 return cells_[static_cast<size_t>(y) * static_cast<size_t>(dimx_) +
68 static_cast<size_t>(x)];
69}
70
71/// @brief Clear all the cells from the surface.
72void Surface::Clear() {
73 std::fill(cells_.begin(), cells_.end(), Cell());
74}
75
76} // namespace ftxui
The FTXUI ftxui:: namespace.
Definition animation.hpp:11
int y
Definition elements.hpp:126