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/image.hpp" // for Pixel, Image
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 Pixel GetPixel(int x, int y) const;
47
48 using Stylizer = std::function<void(Pixel&)>;
49
50 // Draws using braille characters --------------------------------------------
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 // Draw using box characters -------------------------------------------------
74 // Block are of size 1x2. y is considered to be a multiple of 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 // Draw using normal characters ----------------------------------------------
106 // Draw using character of size 2x4 at position (x,y)
107 // x is considered to be a multiple of 2.
108 // y is considered to be a multiple of 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 // Draw using directly pixels or images --------------------------------------
114 // x is considered to be a multiple of 2.
115 // y is considered to be a multiple of 4.
116 void DrawPixel(int x, int y, const Pixel&);
117 void DrawImage(int x, int y, const Image&);
118
119 // Decorator:
120 // x is considered to be a multiple of 2.
121 // y is considered to be a multiple of 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
void DrawImage(int x, int y, const Image &)
Draw a predefined image, with top-left corner at the given coordinate You can supply negative coordin...
void DrawBlockLine(int x1, int y1, int x2, int y2)
Draw a line made of block characters.
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.
void DrawText(int x, int y, const std::string &value)
Draw a piece of text.
Canvas()=default
std::function< void(Pixel &)> Stylizer
Definition canvas.hpp:48
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.
Pixel GetPixel(int x, int y) const
Get the content of a cell.
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 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.
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 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 Style(int x, int y, const Stylizer &style)
Modify a pixel at a given location.
void DrawPointToggle(int x, int y)
Toggle a braille dot. A filled one will be erased, and the other will be drawn.
void DrawPixel(int x, int y, const Pixel &)
Directly draw a predefined pixel at the given coordinate.
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 Pixel.
Definition image.hpp:17
A Unicode character and its associated style.
Definition pixel.hpp:15
The FTXUI ftxui:: namespace.
Definition animation.hpp:10