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// Solution de contournement pour WinUsr.h (via Windows.h) définissant des macros qui causent des problèmes.
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 est un tampon dessinable associé aux opérations de dessin.
25///
26/// Canvas est une zone de dessin qui peut être utilisée pour créer des graphiques complexes. Il
27/// prend en charge le dessin de points, de lignes, de cercles, d'ellipses, de texte et d'images à l'aide de
28/// caractères braille, de blocs ou de caractères normaux.
29///
30/// Note: Un terminal contient des cellules. Une cellule est une unité de:
31/// - caractères braille 2x4 (1x1 pixel)
32/// - caractères de bloc 2x2 (2x2 pixels)
33/// - caractères normaux 2x4 (2x4 pixels)
34///
35/// Vous devez multiplier la coordonnée x par 2 et la coordonnée y par 4 pour
36/// obtenir la position correcte dans le terminal.
37///
38/// @ingroup dom
39struct FTXUI_EXPORT(DOM) Canvas {
40 public:
41 Canvas() = default;
42 Canvas(int width, int height);
43
44 // Accesseurs :
45 int width() const { return width_; }
46 int height() const { return height_; }
47 Cell GetCell(int x, int y) const;
48 // [Déprécié] alias pour GetCell.
49 Cell GetPixel(int x, int y) const { return GetCell(x, y); }
50
51 using Stylizer = std::function<void(Cell&)>;
52
53 // Dessine en utilisant des caractères 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 // Dessine en utilisant des caractères de boîte -------------------------------------------------
77 // Les blocs sont de taille 1x2. y est considéré comme un multiple 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 // Dessine en utilisant des caractères normaux ----------------------------------------------
109 // Dessine en utilisant un caractère de taille 2x4 à la position (x,y)
110 // x est considéré comme un multiple de 2.
111 // y est considéré comme un multiple 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 // Dessine directement des pixels ou des images --------------------------------------
117 // x est considéré comme un multiple de 2.
118 // y est considéré comme un multiple de 4.
119 void DrawCell(int x, int y, const Cell&);
120 void DrawSurface(int x, int y, const Surface&);
121
122 // [Déprécié] alias pour DrawCell.
123 void DrawPixel(int x, int y, const Cell& cell) { DrawCell(x, y, cell); }
124 // [Déprécié] alias pour DrawSurface.
125 void DrawImage(int x, int y, const Surface& s) { DrawSurface(x, y, s); }
126
127 // Décorateur:
128 // x est considéré comme un multiple de 2.
129 // y est considéré comme un multiple 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, // Unités de taille 2x4
139 kBlock, // Unités de taille 2x2
140 kBraille, // Unités de taille 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()
L'espace de noms FTXUI ftxui::
Definition animation.hpp:11
int y
Definition elements.hpp:134
int value
Definition elements.hpp:195