FTXUI 6.1.9
C++ functional terminal UI.
载入中...
搜索中...
未找到
canvas.hpp
浏览该文件的文档.
1// Copyright 2021 Arthur Sonzogni. 保留所有权利。
2// 此源代码的使用受 MIT 许可协议的约束,该协议可在以下文件中找到:
3// 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// 解决 WinUsr.h(通过 Windows.h)定义破坏性宏的问题。
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 是一个与绘图操作相关的可绘制缓冲区。
24///
25/// Canvas 是一个可绘制区域,可用于创建复杂的图形。它
26/// 支持使用点、线、圆、椭圆、文本和图像进行绘制,使用
27/// 盲文、块或普通字符。
28///
29/// 注意:终端包含单元格。一个单元格是以下单位:
30/// - 2x4 盲文字符(1x1 像素)
31/// - 2x2 块字符(2x2 像素)
32/// - 2x4 普通字符(2x4 像素)
33///
34/// 您需要将 x 坐标乘以 2,将 y 坐标乘以 4,以
35/// 获取终端中的正确位置。
36///
37/// @ingroup dom
38struct Canvas {
39 public:
40 Canvas() = default;
41 Canvas(int width, int height);
42
43 // 获取器:
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 // 使用盲文字符绘图 --------------------------------------------
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 // 使用方块字符绘图 -------------------------------------------------
74 // 块的大小为 1x2。y 被认为是 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 // 使用普通字符绘图 ----------------------------------------------
106 // 在位置 (x,y) 处绘制大小为 2x4 的字符
107 // x 被认为是 2 的倍数。
108 // y 被认为是 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 // 直接使用像素或图像绘图 --------------------------------------
114 // x 被认为是 2 的倍数。
115 // y 被认为是 4 的倍数。
116 void DrawPixel(int x, int y, const Pixel&);
117 void DrawImage(int x, int y, const Image&);
118
119 // 装饰器:
120 // x 被认为是 2 的倍数。
121 // y 被认为是 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, // 大小为 2x4 的单位
131 kBlock, // 大小为 2x2 的单位
132 kBraille, // 大小为 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 &)
在给定坐标处绘制预定义图像,左上角位于该坐标 您可以提供负坐标来随意对齐图像 - 只会绘制“可见”部分
void DrawBlockLine(int x1, int y1, int x2, int y2)
绘制由块字符组成的线条。
void DrawPointEllipseFilled(int x, int y, int r1, int r2)
绘制由盲文点组成的实心椭圆。
void DrawPointLine(int x1, int y1, int x2, int y2)
绘制由盲文点组成的线条。
void DrawText(int x, int y, const std::string &value)
绘制一段文本。
Canvas()=default
std::function< void(Pixel &)> Stylizer
void DrawBlockOn(int x, int y)
绘制一个块。
void DrawPointCircleFilled(int x, int y, int radius)
绘制由盲文点组成的实心圆。
void DrawPointOn(int x, int y)
绘制一个盲文点。
void DrawPointOff(int x, int y)
擦除一个盲文点。
Pixel GetPixel(int x, int y) const
获取单元格的内容。
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 DrawBlockToggle(int x, int y)
切换一个块。如果已填充,则擦除。如果为空, 将被填充。
void DrawBlockCircle(int x1, int y1, int radius)
绘制由块字符组成的圆。
void DrawBlockCircleFilled(int x1, int y1, int radius)
绘制由块字符组成的实心圆。
void DrawPointCircle(int x, int y, int radius)
绘制由盲文点组成的圆。
int height() const
void DrawBlockOff(int x, int y)
擦除一个块。
int width() const
void DrawBlock(int x, int y, bool value)
绘制一个块。
void DrawPointToggle(int x, int y)
切换盲文点。已填充的将被擦除,另一个将被绘制。
void DrawPixel(int x, int y, const Pixel &)
在给定坐标处直接绘制预定义像素
Canvas 是一个与绘图操作相关的可绘制缓冲区。
Color 是一个表示终端用户界面中颜色的类。
像素的矩形网格。
一个 Unicode 字符及其相关样式。
#include "ftxui/component/component_base.hpp" // 用于 ComponentBase