FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
canvas.hpp
Go to the documentation of this file.
1// 版權所有 2021 Arthur Sonzogni. 保留所有權利。
2// 本原始碼受 MIT 許可證約束,該許可證可在 LICENSE 文件中找到。
3#ifndef FTXUI_DOM_CANVAS_HPP
4#define FTXUI_DOM_CANVAS_HPP
5
6#include <cstddef> // for size_t
7#include <functional> // for function
8#include <string> // for string
9#include <unordered_map> // for unordered_map
10
11#include "ftxui/screen/color.hpp" // for Color
12#include "ftxui/screen/image.hpp" // for Pixel, Image
13
14#ifdef DrawText
15// 解決 WinUsr.h (透過 Windows.h) 定義會導致問題的宏。
16// https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-drawtext
17#undef DrawText
18#endif
19
20namespace ftxui {
21
22/// @brief 畫布是與繪圖操作相關聯的可繪製緩衝區。
23///
24/// 畫布是一個可繪製區域,可用於創建複雜的圖形。它支援使用
25/// 盲文、塊狀或普通字符繪製點、線條、圓形、橢圓、文本和圖像。
26///
27/// 注意:終端包含單元格。一個單元格的單位是:
28/// - 2x4 盲文字符 (1x1 像素)
29/// - 2x2 塊狀字符 (2x2 像素)
30/// - 2x4 普通字符 (2x4 像素)
31///
32/// 您需要將 x 座標乘以 2,將 y 座標乘以 4,才能在終端中獲得正確的位置。
33///
34/// @ingroup dom
35struct Canvas {
36 public:
37 Canvas() = default;
38 Canvas(int width, int height);
39
40 // 獲取器:
41 int width() const { return width_; }
42 int height() const { return height_; }
43 Pixel GetPixel(int x, int y) const;
44
45 using Stylizer = std::function<void(Pixel&)>;
46
47 // 使用盲文字符繪製 --------------------------------------------
48 void DrawPointOn(int x, int y);
49 void DrawPointOff(int x, int y);
50 void DrawPointToggle(int x, int y);
51 void DrawPoint(int x, int y, bool value);
52 void DrawPoint(int x, int y, bool value, const Stylizer& s);
53 void DrawPoint(int x, int y, bool value, const Color& color);
54 void DrawPointLine(int x1, int y1, int x2, int y2);
55 void DrawPointLine(int x1, int y1, int x2, int y2, const Stylizer& s);
56 void DrawPointLine(int x1, int y1, int x2, int y2, const Color& color);
57 void DrawPointCircle(int x, int y, int radius);
58 void DrawPointCircle(int x, int y, int radius, const Stylizer& s);
59 void DrawPointCircle(int x, int y, int radius, const Color& color);
60 void DrawPointCircleFilled(int x, int y, int radius);
61 void DrawPointCircleFilled(int x, int y, int radius, const Stylizer& s);
62 void DrawPointCircleFilled(int x, int y, int radius, const Color& color);
63 void DrawPointEllipse(int x, int y, int r1, int r2);
64 void DrawPointEllipse(int x, int y, int r1, int r2, const Color& color);
65 void DrawPointEllipse(int x, int y, int r1, int r2, const Stylizer& s);
66 void DrawPointEllipseFilled(int x, int y, int r1, int r2);
67 void DrawPointEllipseFilled(int x, int y, int r1, int r2, const Color& color);
68 void DrawPointEllipseFilled(int x, int y, int r1, int r2, const Stylizer& s);
69
70 // 使用方塊字符繪製 -------------------------------------------------
71 // 塊狀字符的大小為 1x2。y 被認為是 2 的倍數。 void DrawBlockOn(int x, int y);
72 void DrawBlockOff(int x, int y);
73 void DrawBlockToggle(int x, int y);
74 void DrawBlock(int x, int y, bool value);
75 void DrawBlock(int x, int y, bool value, const Stylizer& s);
76 void DrawBlock(int x, int y, bool value, const Color& color);
77 void DrawBlockLine(int x1, int y1, int x2, int y2);
78 void DrawBlockLine(int x1, int y1, int x2, int y2, const Stylizer& s);
79 void DrawBlockLine(int x1, int y1, int x2, int y2, const Color& color);
80 void DrawBlockCircle(int x1, int y1, int radius);
81 void DrawBlockCircle(int x1, int y1, int radius, const Stylizer& s);
82 void DrawBlockCircle(int x1, int y1, int radius, const Color& color);
83 void DrawBlockCircleFilled(int x1, int y1, int radius);
84 void DrawBlockCircleFilled(int x1, int y1, int radius, const Stylizer& s);
85 void DrawBlockCircleFilled(int x1, int y1, int radius, const Color& color);
86 void DrawBlockEllipse(int x1, int y1, int r1, int r2);
87 void DrawBlockEllipse(int x1, int y1, int r1, int r2, const Stylizer& s);
88 void DrawBlockEllipse(int x1, int y1, int r1, int r2, const Color& color);
89 void DrawBlockEllipseFilled(int x1, int y1, int r1, int r2);
90 void DrawBlockEllipseFilled(int x1,
91 int y1,
92 int r1,
93 int r2,
94 const Stylizer& s);
96 int y1,
97 int r1,
98 int r2,
99 const Color& color);
100
101 // 使用普通字符繪製 ----------------------------------------------
102 // 在 (x,y) 位置使用 2x4 大小的字符繪製
103 // x 被認為是 2 的倍數。
104 // y 被認為是 4 的倍數。 void DrawText(int x, int y, const std::string& value);
105 void DrawText(int x, int y, const std::string& value, const Color& color);
106 void DrawText(int x, int y, const std::string& value, const Stylizer& style);
107
108 // 直接使用像素或圖像繪製 --------------------------------------
109 // x 被認為是 2 的倍數。
110 // y 被認為是 4 的倍數。 void DrawPixel(int x, int y, const Pixel&);
111 void DrawImage(int x, int y, const Image&);
112
113 // 裝飾器:
114 // x 被認為是 2 的倍數。
115 // y 被認為是 4 的倍數。 void Style(int x, int y, const Stylizer& style);
116
117 private:
118 bool IsIn(int x, int y) const {
119 return x >= 0 && x < width_ && y >= 0 && y < height_;
120 }
121
122 enum CellType {
123 kCell, // Units of size 2x4
124 kBlock, // Units of size 2x2
125 kBraille, // Units of size 1x1
126 };
127
128 struct Cell {
129 CellType type = kCell;
130 Pixel content;
131 };
132
133 struct XY {
134 int x;
135 int y;
136 bool operator==(const XY& other) const {
137 return x == other.x && y == other.y;
138 }
139 };
140
141 struct XYHash {
142 size_t operator()(const XY& xy) const {
143 constexpr size_t shift = 1024;
144 return size_t(xy.x) * shift + size_t(xy.y);
145 }
146 };
147
148 int width_ = 0;
149 int height_ = 0;
150 std::unordered_map<XY, Cell, XYHash> storage_;
151};
152
153} // namespace ftxui
154
155#endif // FTXUI_DOM_CANVAS_HPP
void DrawImage(int x, int y, const Image &)
繪製一個預定義的圖像,左上角位於給定座標。 您可以提供負座標來隨意對齊圖像 - 只會繪製「可見」部分。
void DrawBlockEllipse(int x1, int y1, int r1, int r2, const Color &color)
void DrawBlock(int x, int y, bool value, const Stylizer &s)
void DrawBlockLine(int x1, int y1, int x2, int y2)
繪製一條由區塊字元組成的線條。
void DrawPointEllipseFilled(int x, int y, int r1, int r2, const Stylizer &s)
void DrawPointEllipseFilled(int x, int y, int r1, int r2)
void DrawBlockLine(int x1, int y1, int x2, int y2, const Stylizer &s)
void DrawPointLine(int x1, int y1, int x2, int y2)
繪製一條由盲文點組成的線條。
void DrawText(int x, int y, const std::string &value, const Color &color)
繪製一段文字。
void DrawPointEllipseFilled(int x, int y, int r1, int r2, const Color &color)
Canvas()=default
std::function< void(Pixel &)> Stylizer
Definition canvas.hpp:45
void DrawPointCircleFilled(int x, int y, int radius)
void DrawPointOn(int x, int y)
繪製一個盲文點。
void DrawBlockCircleFilled(int x1, int y1, int radius, const Stylizer &s)
void DrawBlockEllipseFilled(int x1, int y1, int r1, int r2, const Color &color)
void DrawPointOff(int x, int y)
擦除一個盲文點。
Pixel GetPixel(int x, int y) const
取得單元格的內容。
void DrawBlockCircle(int x1, int y1, int radius, const Stylizer &s)
void DrawBlockCircleFilled(int x1, int y1, int radius, const Color &color)
void DrawBlockEllipseFilled(int x1, int y1, int r1, int r2)
void DrawPointEllipse(int x, int y, int r1, int r2)
繪製一個由盲文點組成的橢圓。
void DrawPoint(int x, int y, bool value)
繪製一個盲文點。
void DrawBlockEllipse(int x1, int y1, int r1, int r2)
void DrawBlockCircle(int x1, int y1, int radius, const Color &color)
void DrawBlockToggle(int x, int y)
切換一個區塊。如果它已填滿,將被擦除;如果為空,將被填滿。
void DrawBlockCircle(int x1, int y1, int radius)
void DrawPointEllipse(int x, int y, int r1, int r2, const Stylizer &s)
void DrawBlockCircleFilled(int x1, int y1, int radius)
void DrawPointCircle(int x, int y, int radius)
繪製一個由盲文點組成的圓形。
int height() const
Definition canvas.hpp:42
void DrawBlockOff(int x, int y)
擦除一個區塊。
int width() const
Definition canvas.hpp:41
void DrawBlock(int x, int y, bool value)
繪製一個區塊。
void DrawPointCircleFilled(int x, int y, int radius, const Stylizer &s)
void DrawPointToggle(int x, int y)
切換一個盲文點。如果它已填滿,將被擦除;如果為空,則繪製它。
畫布是與繪圖操作相關聯的可繪製緩衝區。
Definition canvas.hpp:35
Color 是一個在終端使用者介面中表示顏色的類別。
Definition color.hpp:20
像素的矩形網格。
Definition image.hpp:17
一個 Unicode 字元及其相關樣式。
Definition pixel.hpp:14
FTXUI 的 ftxui:: 命名空間
Definition animation.hpp:10