FTXUI 6.1.9
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
15#ifdef DrawText
16// Workaround for WinUsr.h (via Windows.h) defining macros that break things.
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 is a drawable buffer associated with drawing operations.
24///
25/// Canvas is a drawable area that can be used to create complex graphics. It
26/// supports drawing points, lines, circles, ellipses, text, and images using
27/// braille, block, or normal characters.
28///
29/// Note: A terminal contains cells. A cells is a unit of:
30/// - 2x4 braille characters (1x1 pixel)
31/// - 2x2 block characters (2x2 pixels)
32/// - 2x4 normal characters (2x4 pixels)
33///
34/// You need to multiply the x coordinate by 2 and the y coordinate by 4 to
35/// get the correct position in the 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 Cell GetCell(int x, int y) const;
47 // [Deprecated] alias for GetCell.
48 Cell GetPixel(int x, int y) const { return GetCell(x, y); }
49
50 using Stylizer = std::function<void(Cell&)>;
51
52 // Draws using braille characters --------------------------------------------
53 void DrawPointOn(int x, int y);
54 void DrawPointOff(int x, int y);
55 void DrawPointToggle(int x, int y);
56 void DrawPoint(int x, int y, bool value);
57 void DrawPoint(int x, int y, bool value, const Stylizer& s);
58 void DrawPoint(int x, int y, bool value, const Color& color);
59 void DrawPointLine(int x1, int y1, int x2, int y2);
60 void DrawPointLine(int x1, int y1, int x2, int y2, const Stylizer& s);
61 void DrawPointLine(int x1, int y1, int x2, int y2, const Color& color);
62 void DrawPointCircle(int x, int y, int radius);
63 void DrawPointCircle(int x, int y, int radius, const Stylizer& s);
64 void DrawPointCircle(int x, int y, int radius, const Color& color);
65 void DrawPointCircleFilled(int x, int y, int radius);
66 void DrawPointCircleFilled(int x, int y, int radius, const Stylizer& s);
67 void DrawPointCircleFilled(int x, int y, int radius, const Color& color);
68 void DrawPointEllipse(int x, int y, int r1, int r2);
69 void DrawPointEllipse(int x, int y, int r1, int r2, const Color& color);
70 void DrawPointEllipse(int x, int y, int r1, int r2, const Stylizer& s);
71 void DrawPointEllipseFilled(int x, int y, int r1, int r2);
72 void DrawPointEllipseFilled(int x, int y, int r1, int r2, const Color& color);
73 void DrawPointEllipseFilled(int x, int y, int r1, int r2, const Stylizer& s);
74
75 // Draw using box characters -------------------------------------------------
76 // Block are of size 1x2. y is considered to be a multiple of 2.
77 void DrawBlockOn(int x, int y);
78 void DrawBlockOff(int x, int y);
79 void DrawBlockToggle(int x, int y);
80 void DrawBlock(int x, int y, bool value);
81 void DrawBlock(int x, int y, bool value, const Stylizer& s);
82 void DrawBlock(int x, int y, bool value, const Color& color);
83 void DrawBlockLine(int x1, int y1, int x2, int y2);
84 void DrawBlockLine(int x1, int y1, int x2, int y2, const Stylizer& s);
85 void DrawBlockLine(int x1, int y1, int x2, int y2, const Color& color);
86 void DrawBlockCircle(int x1, int y1, int radius);
87 void DrawBlockCircle(int x1, int y1, int radius, const Stylizer& s);
88 void DrawBlockCircle(int x1, int y1, int radius, const Color& color);
89 void DrawBlockCircleFilled(int x1, int y1, int radius);
90 void DrawBlockCircleFilled(int x1, int y1, int radius, const Stylizer& s);
91 void DrawBlockCircleFilled(int x1, int y1, int radius, const Color& color);
92 void DrawBlockEllipse(int x1, int y1, int r1, int r2);
93 void DrawBlockEllipse(int x1, int y1, int r1, int r2, const Stylizer& s);
94 void DrawBlockEllipse(int x1, int y1, int r1, int r2, const Color& color);
95 void DrawBlockEllipseFilled(int x1, int y1, int r1, int r2);
96 void DrawBlockEllipseFilled(int x1,
97 int y1,
98 int r1,
99 int r2,
100 const Stylizer& s);
101 void DrawBlockEllipseFilled(int x1,
102 int y1,
103 int r1,
104 int r2,
105 const Color& color);
106
107 // Draw using normal characters ----------------------------------------------
108 // Draw using character of size 2x4 at position (x,y)
109 // x is considered to be a multiple of 2.
110 // y is considered to be a multiple of 4.
111 void DrawText(int x, int y, std::string_view value);
112 void DrawText(int x, int y, std::string_view value, const Color& color);
113 void DrawText(int x, int y, std::string_view value, const Stylizer& style);
114
115 // Draw using directly pixels or images --------------------------------------
116 // x is considered to be a multiple of 2.
117 // y is considered to be a multiple of 4.
118 void DrawCell(int x, int y, const Cell&);
119 void DrawSurface(int x, int y, const Surface&);
120
121 // [Deprecated] alias for DrawCell.
122 void DrawPixel(int x, int y, const Cell& cell) { DrawCell(x, y, cell); }
123 // [Deprecated] alias for DrawSurface.
124 void DrawImage(int x, int y, const Surface& s) { DrawSurface(x, y, s); }
125
126 // Decorator:
127 // x is considered to be a multiple of 2.
128 // y is considered to be a multiple of 4.
129 void Style(int x, int y, const Stylizer& style);
130
131 private:
132 bool IsIn(int x, int y) const {
133 return x >= 0 && x < width_ && y >= 0 && y < height_;
134 }
135
136 enum CellType {
137 kCell, // Units of size 2x4
138 kBlock, // Units of size 2x2
139 kBraille, // Units of size 1x1
140 };
141
142 struct CanvasCell {
143 CellType type = kCell;
144 Cell content;
145 };
146
147 struct XY {
148 int x;
149 int y;
150 bool operator==(const XY& other) const {
151 return x == other.x && y == other.y;
152 }
153 };
154
155 struct XYHash {
156 size_t operator()(const XY& xy) const {
157 constexpr size_t shift = 1024;
158 return size_t(xy.x) * shift + size_t(xy.y);
159 }
160 };
161
162 int width_ = 0;
163 int height_ = 0;
164 std::unordered_map<XY, CanvasCell, XYHash> storage_;
165};
166
167} // namespace ftxui
168
169#endif // FTXUI_DOM_CANVAS_HPP
ButtonOption Style()
std::function< void(Cell &)> Stylizer
Definition canvas.hpp:50
void DrawText(int x, int y, std::string_view value)
Draw a piece of text.
void DrawBlockLine(int x1, int y1, int x2, int y2)
Draw a line made of block characters.
Cell GetCell(int x, int y) const
Get the content of a cell.
void DrawPointEllipseFilled(int x, int y, int r1, int r2)
Draw a filled ellipse made of braille dots.
void DrawPointLine(int x1, int y1, int x2, int y2)
Draw a line made of braille dots.
Canvas()=default
void DrawPixel(int x, int y, const Cell &cell)
Definition canvas.hpp:122
void DrawBlockOn(int x, int y)
Draw a block.
void DrawPointCircleFilled(int x, int y, int radius)
Draw a filled circle made of braille dots.
void DrawPointOn(int x, int y)
Draw a braille dot.
void DrawPointOff(int x, int y)
Erase a braille dot.
void DrawCell(int x, int y, const Cell &)
Directly draw a predefined pixel at the given coordinate.
void DrawBlockEllipseFilled(int x1, int y1, int r1, int r2)
Draw a filled ellipse made of block characters.
void DrawPointEllipse(int x, int y, int r1, int r2)
Draw an ellipse made of braille dots.
void DrawSurface(int x, int y, const Surface &)
Draw a predefined image, with top-left corner at the given coordinate You can supply negative coordin...
void DrawPoint(int x, int y, bool value)
Draw a braille dot.
void DrawBlockEllipse(int x1, int y1, int r1, int r2)
Draw an ellipse made of block characters.
Cell GetPixel(int x, int y) const
Definition canvas.hpp:48
void DrawBlockToggle(int x, int y)
Toggle a block. If it is filled, it will be erased. If it is empty, it will be filled.
void DrawImage(int x, int y, const Surface &s)
Definition canvas.hpp:124
void DrawBlockCircle(int x1, int y1, int radius)
Draw a circle made of block characters.
void DrawBlockCircleFilled(int x1, int y1, int radius)
Draw a filled circle made of block characters.
void DrawPointCircle(int x, int y, int radius)
Draw a circle made of braille dots.
int height() const
Definition canvas.hpp:45
void DrawBlockOff(int x, int y)
Erase a block.
int width() const
Definition canvas.hpp:44
void DrawBlock(int x, int y, bool value)
Draw a block.
void DrawPointToggle(int x, int y)
Toggle a braille dot. A filled one will be erased, and the other will be drawn.
Canvas is a drawable buffer associated with drawing operations.
Definition canvas.hpp:38
Color is a class that represents a color in the terminal user interface.
Definition color.hpp:22
A rectangular grid of Cell.
Definition surface.hpp:17
A Unicode character and its associated style.
Definition cell.hpp:15
The FTXUI ftxui:: namespace.
Definition animation.hpp:10