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#include "ftxui/util/export.hpp"
15
16#ifdef DrawText
17// Workaround for WinUsr.h (via Windows.h) defining macros that break things.
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 is a drawable buffer associated with drawing operations.
25///
26/// Canvas is a drawable area that can be used to create complex graphics. It
27/// supports drawing points, lines, circles, ellipses, text, and images using
28/// braille, block, or normal characters.
29///
30/// Note: A terminal contains cells. A cells is a unit of:
31/// - 2x4 braille characters (1x1 pixel)
32/// - 2x2 block characters (2x2 pixels)
33/// - 2x4 normal characters (2x4 pixels)
34///
35/// You need to multiply the x coordinate by 2 and the y coordinate by 4 to
36/// get the correct position in the 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 // [Deprecated] alias for GetCell.
49 Cell GetPixel(int x, int y) const { return GetCell(x, y); }
50
51 using Stylizer = std::function<void(Cell&)>;
52
53 // Draws using braille characters --------------------------------------------
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 // Draw using box characters -------------------------------------------------
77 // Block are of size 1x2. y is considered to be a multiple of 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 // Draw using normal characters ----------------------------------------------
109 // Draw using character of size 2x4 at position (x,y)
110 // x is considered to be a multiple of 2.
111 // y is considered to be a multiple of 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 // Draw using directly pixels or images --------------------------------------
117 // x is considered to be a multiple of 2.
118 // y is considered to be a multiple of 4.
119 void DrawCell(int x, int y, const Cell&);
120 void DrawSurface(int x, int y, const Surface&);
121
122 // [Deprecated] alias for DrawCell.
123 void DrawPixel(int x, int y, const Cell& cell) { DrawCell(x, y, cell); }
124 // [Deprecated] alias for DrawSurface.
125 void DrawImage(int x, int y, const Surface& s) { DrawSurface(x, y, s); }
126
127 // Decorator:
128 // x is considered to be a multiple of 2.
129 // y is considered to be a multiple of 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, // Units of size 2x4
139 kBlock, // Units of size 2x2
140 kBraille, // Units of size 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()
The FTXUI ftxui:: namespace.
Definition animation.hpp:11
int y
Definition elements.hpp:126
int value
Definition elements.hpp:178