FTXUI 7.0.3
C++ functional terminal UI.
Loading...
Searching...
No Matches
canvas.hpp
Go to the documentation of this file.
1// Copyright 2021 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#ifndef FTXUI_DOM_CANVAS_HPP
5#define FTXUI_DOM_CANVAS_HPP
6
7#include <cstddef> // for size_t
8#include <functional> // for function
9#include <string> // for string
10#include <unordered_map> // for unordered_map
11
12#include "ftxui/screen/color.hpp" // for Color
13#include "ftxui/screen/surface.hpp" // for Cell, Surface
14#include "ftxui/util/export.hpp"
15
16#ifdef DrawText
17// Solución alternativa para WinUsr.h (a través de Windows.h) que define macros que rompen cosas.
18// https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-drawtext
19#undef DrawText
20#endif
21
22namespace ftxui {
23
24/// @brief Canvas es un búfer dibujable asociado con operaciones de dibujo.
25///
26/// Canvas es un área dibujable que se puede utilizar para crear gráficos complejos.
27/// Soporta dibujar puntos, líneas, círculos, elipses, texto e imágenes usando
28/// caracteres braille, de bloque o normales.
29///
30/// Nota: Un terminal contiene celdas. Una celda es una unidad de:
31/// - Caracteres braille de 2x4 (1x1 píxel)
32/// - Caracteres de bloque de 2x2 (2x2 píxeles)
33/// - Caracteres normales de 2x4 (2x4 píxeles)
34///
35/// Debe multiplicar la coordenada x por 2 y la coordenada y por 4 para
36/// obtener la posición correcta en el terminal.
37///
38/// @ingroup dom
39struct FTXUI_EXPORT(DOM) Canvas {
40 public:
41 Canvas() = default;
42 Canvas(int width, int height);
43
44 // Getters:
45 int width() const { return width_; }
46 int height() const { return height_; }
47 Cell GetCell(int x, int y) const;
48 // [Obsoleto] alias de GetCell.
49 Cell GetPixel(int x, int y) const { return GetCell(x, y); }
50
51 using Stylizer = std::function<void(Cell&)>;
52
53 // Dibuja usando caracteres braille --------------------------------------------
54 void DrawPointOn(int x, int y);
55 void DrawPointOff(int x, int y);
56 void DrawPointToggle(int x, int y);
57 void DrawPoint(int x, int y, bool value);
58 void DrawPoint(int x, int y, bool value, const Stylizer& s);
59 void DrawPoint(int x, int y, bool value, const Color& color);
60 void DrawPointLine(int x1, int y1, int x2, int y2);
61 void DrawPointLine(int x1, int y1, int x2, int y2, const Stylizer& s);
62 void DrawPointLine(int x1, int y1, int x2, int y2, const Color& color);
63 void DrawPointCircle(int x, int y, int radius);
64 void DrawPointCircle(int x, int y, int radius, const Stylizer& s);
65 void DrawPointCircle(int x, int y, int radius, const Color& color);
66 void DrawPointCircleFilled(int x, int y, int radius);
67 void DrawPointCircleFilled(int x, int y, int radius, const Stylizer& s);
68 void DrawPointCircleFilled(int x, int y, int radius, const Color& color);
69 void DrawPointEllipse(int x, int y, int r1, int r2);
70 void DrawPointEllipse(int x, int y, int r1, int r2, const Color& color);
71 void DrawPointEllipse(int x, int y, int r1, int r2, const Stylizer& s);
72 void DrawPointEllipseFilled(int x, int y, int r1, int r2);
73 void DrawPointEllipseFilled(int x, int y, int r1, int r2, const Color& color);
74 void DrawPointEllipseFilled(int x, int y, int r1, int r2, const Stylizer& s);
75
76 // Dibuja usando caracteres de caja -------------------------------------------------
77 // Los bloques son de tamaño 1x2. La coordenada y se considera un múltiplo de 2.
78 void DrawBlockOn(int x, int y);
79 void DrawBlockOff(int x, int y);
80 void DrawBlockToggle(int x, int y);
81 void DrawBlock(int x, int y, bool value);
82 void DrawBlock(int x, int y, bool value, const Stylizer& s);
83 void DrawBlock(int x, int y, bool value, const Color& color);
84 void DrawBlockLine(int x1, int y1, int x2, int y2);
85 void DrawBlockLine(int x1, int y1, int x2, int y2, const Stylizer& s);
86 void DrawBlockLine(int x1, int y1, int x2, int y2, const Color& color);
87 void DrawBlockCircle(int x1, int y1, int radius);
88 void DrawBlockCircle(int x1, int y1, int radius, const Stylizer& s);
89 void DrawBlockCircle(int x1, int y1, int radius, const Color& color);
90 void DrawBlockCircleFilled(int x1, int y1, int radius);
91 void DrawBlockCircleFilled(int x1, int y1, int radius, const Stylizer& s);
92 void DrawBlockCircleFilled(int x1, int y1, int radius, const Color& color);
93 void DrawBlockEllipse(int x1, int y1, int r1, int r2);
94 void DrawBlockEllipse(int x1, int y1, int r1, int r2, const Stylizer& s);
95 void DrawBlockEllipse(int x1, int y1, int r1, int r2, const Color& color);
96 void DrawBlockEllipseFilled(int x1, int y1, int r1, int r2);
97 void DrawBlockEllipseFilled(int x1,
98 int y1,
99 int r1,
100 int r2,
101 const Stylizer& s);
102 void DrawBlockEllipseFilled(int x1,
103 int y1,
104 int r1,
105 int r2,
106 const Color& color);
107
108 // Dibuja usando caracteres normales ----------------------------------------------
109 // Dibuja usando un carácter de tamaño 2x4 en la posición (x,y)
110 // La coordenada x se considera un múltiplo de 2.
111 // La coordenada y se considera un múltiplo de 4.
112 void DrawText(int x, int y, std::string_view value);
113 void DrawText(int x, int y, std::string_view value, const Color& color);
114 void DrawText(int x, int y, std::string_view value, const Stylizer& style);
115
116 // Dibuja usando directamente píxeles o imágenes --------------------------------------
117 // La coordenada x se considera un múltiplo de 2.
118 // La coordenada y se considera un múltiplo de 4.
119 void DrawCell(int x, int y, const Cell&);
120 void DrawSurface(int x, int y, const Surface&);
121
122 // [Obsoleto] alias de DrawCell.
123 void DrawPixel(int x, int y, const Cell& cell) { DrawCell(x, y, cell); }
124 // [Obsoleto] alias de DrawSurface.
125 void DrawImage(int x, int y, const Surface& s) { DrawSurface(x, y, s); }
126
127 // Decorador:
128 // La coordenada x se considera un múltiplo de 2.
129 // La coordenada y se considera un múltiplo de 4.
130 void Style(int x, int y, const Stylizer& style);
131
132 private:
133 bool IsIn(int x, int y) const {
134 return x >= 0 && x < width_ && y >= 0 && y < height_;
135 }
136
137 enum CellType {
138 kCell, // Unidades de tamaño 2x4
139 kBlock, // Unidades de tamaño 2x2
140 kBraille, // Unidades de tamaño 1x1
141 };
142
143 struct CanvasCell {
144 CellType type = kCell;
145 Cell content;
146 };
147
148 struct XY {
149 int x;
150 int y;
151 bool operator==(const XY& other) const {
152 return x == other.x && y == other.y;
153 }
154 };
155
156 struct XYHash {
157 size_t operator()(const XY& xy) const {
158 constexpr size_t shift = 1024;
159 return size_t(xy.x) * shift + size_t(xy.y);
160 }
161 };
162
163 int width_ = 0;
164 int height_ = 0;
165 std::unordered_map<XY, CanvasCell, XYHash> storage_;
166};
167
168} // namespace ftxui
169
170#endif // FTXUI_DOM_CANVAS_HPP
ButtonOption Style()
El espacio de nombres ftxui:: de FTXUI.
Definition animation.hpp:11
int y
Definition elements.hpp:134
int value
Definition elements.hpp:195