FTXUI 7.0.3
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// 解決 WinUsr.h (透過 Windows.h) 定義會導致問題的宏。
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 畫布是與繪圖操作相關聯的可繪製緩衝區。
25///
26/// 畫布是一個可繪製區域,可用於創建複雜的圖形。它支援使用
27/// 盲文、塊狀或普通字符繪製點、線條、圓形、橢圓、文本和圖像。
28///
29/// 注意:終端包含單元格。一個單元格的單位是:
30/// - 2x4 盲文字符 (1x1 像素)
31/// - 2x2 塊狀字符 (2x2 像素)
32/// - 2x4 普通字符 (2x4 像素)
33///
34/// 您需要將 x 座標乘以 2,將 y 座標乘以 4,才能在終端中獲得正確的位置。
35///
36/// @ingroup dom
37struct FTXUI_EXPORT(DOM) Canvas {
38 public:
39 Canvas() = default;
40 Canvas(int width, int height);
41
42 // 取值器:
43 int width() const { return width_; }
44 int height() const { return height_; }
45 Cell GetCell(int x, int y) const;
46 // [已棄用] GetCell 的別名。
47 Cell GetPixel(int x, int y) const { return GetCell(x, y); }
48
49 using Stylizer = std::function<void(Cell&)>;
50
51 // 使用點字字元繪製 --------------------------------------------
52 void DrawPointOn(int x, int y);
53 void DrawPointOff(int x, int y);
54 void DrawPointToggle(int x, int y);
55 void DrawPoint(int x, int y, bool value);
56 void DrawPoint(int x, int y, bool value, const Stylizer& s);
57 void DrawPoint(int x, int y, bool value, const Color& color);
58 void DrawPointLine(int x1, int y1, int x2, int y2);
59 void DrawPointLine(int x1, int y1, int x2, int y2, const Stylizer& s);
60 void DrawPointLine(int x1, int y1, int x2, int y2, const Color& color);
61 void DrawPointCircle(int x, int y, int radius);
62 void DrawPointCircle(int x, int y, int radius, const Stylizer& s);
63 void DrawPointCircle(int x, int y, int radius, const Color& color);
64 void DrawPointCircleFilled(int x, int y, int radius);
65 void DrawPointCircleFilled(int x, int y, int radius, const Stylizer& s);
66 void DrawPointCircleFilled(int x, int y, int radius, const Color& color);
67 void DrawPointEllipse(int x, int y, int r1, int r2);
68 void DrawPointEllipse(int x, int y, int r1, int r2, const Color& color);
69 void DrawPointEllipse(int x, int y, int r1, int r2, const Stylizer& s);
70 void DrawPointEllipseFilled(int x, int y, int r1, int r2);
71 void DrawPointEllipseFilled(int x, int y, int r1, int r2, const Color& color);
72 void DrawPointEllipseFilled(int x, int y, int r1, int r2, const Stylizer& s);
73
74 // 使用方塊字符繪製 -------------------------------------------------
75 // 塊狀字符的大小為 1x2。y 被認為是 2 的倍數。 void DrawBlockOn(int x, int y);
76 void DrawBlockOn(int x, int y);
77 void DrawBlockOff(int x, int y);
78 void DrawBlockToggle(int x, int y);
79 void DrawBlock(int x, int y, bool value);
80 void DrawBlock(int x, int y, bool value, const Stylizer& s);
81 void DrawBlock(int x, int y, bool value, const Color& color);
82 void DrawBlockLine(int x1, int y1, int x2, int y2);
83 void DrawBlockLine(int x1, int y1, int x2, int y2, const Stylizer& s);
84 void DrawBlockLine(int x1, int y1, int x2, int y2, const Color& color);
85 void DrawBlockCircle(int x1, int y1, int radius);
86 void DrawBlockCircle(int x1, int y1, int radius, const Stylizer& s);
87 void DrawBlockCircle(int x1, int y1, int radius, const Color& color);
88 void DrawBlockCircleFilled(int x1, int y1, int radius);
89 void DrawBlockCircleFilled(int x1, int y1, int radius, const Stylizer& s);
90 void DrawBlockCircleFilled(int x1, int y1, int radius, const Color& color);
91 void DrawBlockEllipse(int x1, int y1, int r1, int r2);
92 void DrawBlockEllipse(int x1, int y1, int r1, int r2, const Stylizer& s);
93 void DrawBlockEllipse(int x1, int y1, int r1, int r2, const Color& color);
94 void DrawBlockEllipseFilled(int x1, int y1, int r1, int r2);
95 void DrawBlockEllipseFilled(int x1,
96 int y1,
97 int r1,
98 int r2,
99 const Stylizer& s);
100 void DrawBlockEllipseFilled(int x1,
101 int y1,
102 int r1,
103 int r2,
104 const Color& color);
105
106 // 使用普通字符繪製 ----------------------------------------------
107 // 在 (x,y) 位置使用 2x4 大小的字符繪製
108 // x 被認為是 2 的倍數。
109 // y 被認為是 4 的倍數。 void DrawText(int x, int y, const std::string& value);
110 void DrawText(int x, int y, std::string_view value);
111 void DrawText(int x, int y, std::string_view value, const Color& color);
112 void DrawText(int x, int y, std::string_view value, const Stylizer& style);
113
114 // 直接使用像素或圖像繪製 --------------------------------------
115 // x 被認為是 2 的倍數。
116 // y 被認為是 4 的倍數。 void DrawPixel(int x, int y, const Pixel&);
117 void DrawCell(int x, int y, const Cell&);
118 void DrawSurface(int x, int y, const Surface&);
119
120 // [已棄用] DrawCell 的別名。
121 void DrawPixel(int x, int y, const Cell& cell) { DrawCell(x, y, cell); }
122 // [已棄用] DrawSurface 的別名。
123 void DrawImage(int x, int y, const Surface& s) { DrawSurface(x, y, s); }
124
125 // 裝飾器:
126 // x 被認為是 2 的倍數。
127 // y 被認為是 4 的倍數。 void Style(int x, int y, const Stylizer& style);
128 void Style(int x, int y, const Stylizer& style);
129
130 private:
131 bool IsIn(int x, int y) const {
132 return x >= 0 && x < width_ && y >= 0 && y < height_;
133 }
134
135 enum CellType {
136 kCell, // 大小為 2x4 的單位
137 kBlock, // 大小為 2x2 的單位
138 kBraille, // 大小為 1x1 的單位
139 };
140
141 struct CanvasCell {
142 CellType type = kCell;
143 Cell content;
144 };
145
146 struct XY {
147 int x;
148 int y;
149 bool operator==(const XY& other) const {
150 return x == other.x && y == other.y;
151 }
152 };
153
154 struct XYHash {
155 size_t operator()(const XY& xy) const {
156 constexpr size_t shift = 1024;
157 return size_t(xy.x) * shift + size_t(xy.y);
158 }
159 };
160
161 int width_ = 0;
162 int height_ = 0;
163 std::unordered_map<XY, CanvasCell, XYHash> storage_;
164};
165
166} // namespace ftxui
167
168#endif // FTXUI_DOM_CANVAS_HPP
ButtonOption Style()
FTXUI ftxui:: 命名空間
Definition animation.hpp:11
int y
Definition elements.hpp:131
int value
Definition elements.hpp:192