FTXUI 6.1.9
C++ functional terminal UI.
Chargement...
Recherche...
Aucune correspondance
canvas.hpp
Aller à la documentation de ce fichier.
1// Copyright 2021 Arthur Sonzogni. Tous droits réservés.
2// L'utilisation de ce code source est régie par la licence MIT qui peut être trouvée dans
3// le fichier LICENSE.
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/image.hpp" // for Pixel, Image
14
15#ifdef DrawText
16// Solution de contournement pour WinUsr.h (via Windows.h) définissant des macros qui causent des problèmes.
17// https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-drawtext
18#undef DrawText
19#endif
20
21namespace ftxui {
22
23/// @brief Canvas est un tampon dessinable associé aux opérations de dessin.
24///
25/// Canvas est une zone de dessin qui peut être utilisée pour créer des graphiques complexes. Il
26/// prend en charge le dessin de points, de lignes, de cercles, d'ellipses, de texte et d'images à l'aide de
27/// caractères braille, de blocs ou de caractères normaux.
28///
29/// Note: Un terminal contient des cellules. Une cellule est une unité de:
30/// - caractères braille 2x4 (1x1 pixel)
31/// - caractères de bloc 2x2 (2x2 pixels)
32/// - caractères normaux 2x4 (2x4 pixels)
33///
34/// Vous devez multiplier la coordonnée x par 2 et la coordonnée y par 4 pour
35/// obtenir la position correcte dans le terminal.
36///
37/// @ingroup dom
38struct Canvas {
39 public:
40 Canvas() = default;
41 Canvas(int width, int height);
42
43 // Getters:
44 int width() const { return width_; }
45 int height() const { return height_; }
46 Pixel GetPixel(int x, int y) const;
47
48 using Stylizer = std::function<void(Pixel&)>;
49
50 // Dessine en utilisant des caractères braille --------------------------------------------
51 void DrawPointOn(int x, int y);
52 void DrawPointOff(int x, int y);
53 void DrawPointToggle(int x, int y);
54 void DrawPoint(int x, int y, bool value);
55 void DrawPoint(int x, int y, bool value, const Stylizer& s);
56 void DrawPoint(int x, int y, bool value, const Color& color);
57 void DrawPointLine(int x1, int y1, int x2, int y2);
58 void DrawPointLine(int x1, int y1, int x2, int y2, const Stylizer& s);
59 void DrawPointLine(int x1, int y1, int x2, int y2, const Color& color);
60 void DrawPointCircle(int x, int y, int radius);
61 void DrawPointCircle(int x, int y, int radius, const Stylizer& s);
62 void DrawPointCircle(int x, int y, int radius, const Color& color);
63 void DrawPointCircleFilled(int x, int y, int radius);
64 void DrawPointCircleFilled(int x, int y, int radius, const Stylizer& s);
65 void DrawPointCircleFilled(int x, int y, int radius, const Color& color);
66 void DrawPointEllipse(int x, int y, int r1, int r2);
67 void DrawPointEllipse(int x, int y, int r1, int r2, const Color& color);
68 void DrawPointEllipse(int x, int y, int r1, int r2, const Stylizer& s);
69 void DrawPointEllipseFilled(int x, int y, int r1, int r2);
70 void DrawPointEllipseFilled(int x, int y, int r1, int r2, const Color& color);
71 void DrawPointEllipseFilled(int x, int y, int r1, int r2, const Stylizer& s);
72
73 // Dessine en utilisant des caractères de boîte -------------------------------------------------
74 // Les blocs sont de taille 1x2. y est considéré comme un multiple de 2.
75 void DrawBlockOn(int x, int y);
76 void DrawBlockOff(int x, int y);
77 void DrawBlockToggle(int x, int y);
78 void DrawBlock(int x, int y, bool value);
79 void DrawBlock(int x, int y, bool value, const Stylizer& s);
80 void DrawBlock(int x, int y, bool value, const Color& color);
81 void DrawBlockLine(int x1, int y1, int x2, int y2);
82 void DrawBlockLine(int x1, int y1, int x2, int y2, const Stylizer& s);
83 void DrawBlockLine(int x1, int y1, int x2, int y2, const Color& color);
84 void DrawBlockCircle(int x1, int y1, int radius);
85 void DrawBlockCircle(int x1, int y1, int radius, const Stylizer& s);
86 void DrawBlockCircle(int x1, int y1, int radius, const Color& color);
87 void DrawBlockCircleFilled(int x1, int y1, int radius);
88 void DrawBlockCircleFilled(int x1, int y1, int radius, const Stylizer& s);
89 void DrawBlockCircleFilled(int x1, int y1, int radius, const Color& color);
90 void DrawBlockEllipse(int x1, int y1, int r1, int r2);
91 void DrawBlockEllipse(int x1, int y1, int r1, int r2, const Stylizer& s);
92 void DrawBlockEllipse(int x1, int y1, int r1, int r2, const Color& color);
93 void DrawBlockEllipseFilled(int x1, int y1, int r1, int r2);
94 void DrawBlockEllipseFilled(int x1,
95 int y1,
96 int r1,
97 int r2,
98 const Stylizer& s);
99 void DrawBlockEllipseFilled(int x1,
100 int y1,
101 int r1,
102 int r2,
103 const Color& color);
104
105 // Dessine en utilisant des caractères normaux ----------------------------------------------
106 // Dessine en utilisant un caractère de taille 2x4 à la position (x,y)
107 // x est considéré comme un multiple de 2.
108 // y est considéré comme un multiple de 4.
109 void DrawText(int x, int y, const std::string& value);
110 void DrawText(int x, int y, const std::string& value, const Color& color);
111 void DrawText(int x, int y, const std::string& value, const Stylizer& style);
112
113 // Dessine directement des pixels ou des images --------------------------------------
114 // x est considéré comme un multiple de 2.
115 // y est considéré comme un multiple de 4.
116 void DrawPixel(int x, int y, const Pixel&);
117 void DrawImage(int x, int y, const Image&);
118
119 // Décorateur:
120 // x est considéré comme un multiple de 2.
121 // y est considéré comme un multiple de 4.
122 void Style(int x, int y, const Stylizer& style);
123
124 private:
125 bool IsIn(int x, int y) const {
126 return x >= 0 && x < width_ && y >= 0 && y < height_;
127 }
128
129 enum CellType {
130 kCell, // Units of size 2x4
131 kBlock, // Units of size 2x2
132 kBraille, // Units of size 1x1
133 };
134
135 struct Cell {
136 CellType type = kCell;
137 Pixel content;
138 };
139
140 struct XY {
141 int x;
142 int y;
143 bool operator==(const XY& other) const {
144 return x == other.x && y == other.y;
145 }
146 };
147
148 struct XYHash {
149 size_t operator()(const XY& xy) const {
150 constexpr size_t shift = 1024;
151 return size_t(xy.x) * shift + size_t(xy.y);
152 }
153 };
154
155 int width_ = 0;
156 int height_ = 0;
157 std::unordered_map<XY, Cell, XYHash> storage_;
158};
159
160} // namespace ftxui
161
162#endif // FTXUI_DOM_CANVAS_HPP
ButtonOption Style()
void DrawImage(int x, int y, const Image &)
Dessine une image prédéfinie, avec le coin supérieur gauche à la coordonnée donnée Vous pouvez fourni...
void DrawBlockLine(int x1, int y1, int x2, int y2)
Dessine une ligne de caractères de bloc.
void DrawPointEllipseFilled(int x, int y, int r1, int r2)
Dessine une ellipse remplie de points braille.
void DrawPointLine(int x1, int y1, int x2, int y2)
Dessine une ligne de points braille.
void DrawText(int x, int y, const std::string &value)
Dessine un morceau de texte.
Canvas()=default
std::function< void(Pixel &)> Stylizer
Definition canvas.hpp:48
void DrawBlockOn(int x, int y)
Dessine un bloc.
void DrawPointCircleFilled(int x, int y, int radius)
Dessine un cercle rempli de points braille.
void DrawPointOn(int x, int y)
Dessine un point braille.
void DrawPointOff(int x, int y)
Efface un point braille.
Pixel GetPixel(int x, int y) const
Récupère le contenu d'une cellule.
void DrawBlockEllipseFilled(int x1, int y1, int r1, int r2)
Dessine une ellipse remplie de caractères de bloc.
void DrawPointEllipse(int x, int y, int r1, int r2)
Dessine une ellipse de points braille.
void DrawPoint(int x, int y, bool value)
Dessine un point braille.
void DrawBlockEllipse(int x1, int y1, int r1, int r2)
Dessine une ellipse de caractères de bloc.
void DrawBlockToggle(int x, int y)
Inverse un bloc. S'il est rempli, il sera effacé. S'il est vide, il sera rempli.
void DrawBlockCircle(int x1, int y1, int radius)
Dessine un cercle de caractères de bloc.
void DrawBlockCircleFilled(int x1, int y1, int radius)
Dessine un cercle rempli de caractères de bloc.
void DrawPointCircle(int x, int y, int radius)
Dessine un cercle de points braille.
int height() const
Definition canvas.hpp:45
void DrawBlockOff(int x, int y)
Efface un bloc.
int width() const
Definition canvas.hpp:44
void DrawBlock(int x, int y, bool value)
Dessine un bloc.
void DrawPointToggle(int x, int y)
Inverse un point braille. Un point rempli sera effacé, et un point vide sera dessiné.
void DrawPixel(int x, int y, const Pixel &)
Dessine directement un pixel prédéfini à la coordonnée donnée.
Canvas est un tampon dessinable associé aux opérations de dessin.
Definition canvas.hpp:38
Color est une classe qui représente une couleur dans l'interface utilisateur du terminal.
Definition color.hpp:21
Une grille rectangulaire de pixels.
Definition image.hpp:17
Un caractère Unicode et son style associé.
Definition pixel.hpp:15
L'espace de noms FTXUI ftxui::
Definition animation.hpp:10