FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
canvas_animated.cpp
Go to the documentation of this file.
1// 版權所有 2021 Arthur Sonzogni. 保留所有權利。
2// 本原始碼的使用受 MIT 授權條款約束,詳情請參閱 LICENSED 文件。
3#include <cmath> // for sin, cos
4#include <ftxui/dom/elements.hpp> // for canvas, Element, separator, hbox, operator|, border
5#include <ftxui/screen/screen.hpp> // for Pixel
6#include <memory> // for allocator, shared_ptr, __shared_ptr_access
7#include <string> // for string, basic_string
8#include <utility> // for move
9#include <vector> // for vector, __alloc_traits<>::value_type
10
11#include "ftxui/component/component.hpp" // for Renderer, CatchEvent, Horizontal, Menu, Tab
12#include "ftxui/component/component_base.hpp" // for ComponentBase
13#include "ftxui/component/event.hpp" // for Event
14#include "ftxui/component/mouse.hpp" // for Mouse
15#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
16#include "ftxui/dom/canvas.hpp" // for Canvas
17#include "ftxui/screen/color.hpp" // for Color, Color::Red, Color::Blue, Color::Green, ftxui
18
19int main() {
20 using namespace ftxui;
21
22 int mouse_x = 0;
23 int mouse_y = 0;
24
25 // 使用盲文符號繪製的三角形,跟隨滑鼠移動。
26 auto renderer_line_braille = Renderer([&] {
27 auto c = Canvas(100, 100);
28 c.DrawText(0, 0, "Several lines (braille)");
29 c.DrawPointLine(mouse_x, mouse_y, 80, 10, Color::Red);
30 c.DrawPointLine(80, 10, 80, 40, Color::Blue);
31 c.DrawPointLine(80, 40, mouse_x, mouse_y, Color::Green);
32 return canvas(std::move(c));
33 });
34
35 // 使用區塊符號繪製的三角形,跟隨滑鼠移動。
36 auto renderer_line_block = Renderer([&] {
37 auto c = Canvas(100, 100);
38 c.DrawText(0, 0, "Several lines (block)");
39 c.DrawBlockLine(mouse_x, mouse_y, 80, 10, Color::Red);
40 c.DrawBlockLine(80, 10, 80, 40, Color::Blue);
41 c.DrawBlockLine(80, 40, mouse_x, mouse_y, Color::Green);
42 return canvas(std::move(c));
43 });
44
45 // 使用盲文符號繪製的圓形,跟隨滑鼠移動。
46 auto renderer_circle_braille = Renderer([&] {
47 auto c = Canvas(100, 100);
48 c.DrawText(0, 0, "A circle (braille)");
49 c.DrawPointCircle(mouse_x, mouse_y, 30);
50 return canvas(std::move(c));
51 });
52
53 // 使用區塊符號繪製的圓形,跟隨滑鼠移動。
54 auto renderer_circle_block = Renderer([&] {
55 auto c = Canvas(100, 100);
56 c.DrawText(0, 0, "A circle (block)");
57 c.DrawBlockCircle(mouse_x, mouse_y, 30);
58 return canvas(std::move(c));
59 });
60
61 // 使用盲文符號繪製的實心圓形,跟隨滑鼠移動。
62 auto renderer_circle_filled_braille = Renderer([&] {
63 auto c = Canvas(100, 100);
64 c.DrawText(0, 0, "A circle filled (braille)");
65 c.DrawPointCircleFilled(mouse_x, mouse_y, 30);
66 return canvas(std::move(c));
67 });
68
69 // 使用區塊符號繪製的實心圓形,跟隨滑鼠移動。
70 auto renderer_circle_filled_block = Renderer([&] {
71 auto c = Canvas(100, 100);
72 c.DrawText(0, 0, "A circle filled (block)");
73 c.DrawBlockCircleFilled(mouse_x, mouse_y, 30);
74 return canvas(std::move(c));
75 });
76
77 // 使用盲文符號繪製的橢圓,跟隨滑鼠移動。
78 auto renderer_ellipse_braille = Renderer([&] {
79 auto c = Canvas(100, 100);
80 c.DrawText(0, 0, "An ellipse (braille)");
81 c.DrawPointEllipse(mouse_x / 2, mouse_y / 2, mouse_x / 2, mouse_y / 2);
82 return canvas(std::move(c));
83 });
84
85 // 使用區塊符號繪製的橢圓,跟隨滑鼠移動。
86 auto renderer_ellipse_block = Renderer([&] {
87 auto c = Canvas(100, 100);
88 c.DrawText(0, 0, "An ellipse (block)");
89 c.DrawBlockEllipse(mouse_x / 2, mouse_y / 2, mouse_x / 2, mouse_y / 2);
90 return canvas(std::move(c));
91 });
92
93 // 使用盲文符號繪製的實心橢圓,跟隨滑鼠移動。
94 auto renderer_ellipse_filled_braille = Renderer([&] {
95 auto c = Canvas(100, 100);
96 c.DrawText(0, 0, "A filled ellipse (braille)");
97 c.DrawPointEllipseFilled(mouse_x / 2, mouse_y / 2, mouse_x / 2,
98 mouse_y / 2);
99 return canvas(std::move(c));
100 });
101
102 // 使用區塊符號繪製的實心橢圓,跟隨滑鼠移動。
103 auto renderer_ellipse_filled_block = Renderer([&] {
104 auto c = Canvas(100, 100);
105 c.DrawText(0, 0, "A filled ellipse (block)");
106 c.DrawBlockEllipseFilled(mouse_x / 2, mouse_y / 2, mouse_x / 2,
107 mouse_y / 2);
108 c.DrawBlockEllipse(mouse_x / 2, mouse_y / 2, mouse_x / 2, mouse_y / 2);
109 return canvas(std::move(c));
110 });
111
112 // 跟隨滑鼠移動的文字
113 auto renderer_text = Renderer([&] {
114 auto c = Canvas(100, 100);
115 c.DrawText(0, 0, "A piece of text");
116 c.DrawText(mouse_x, mouse_y, "This is a piece of text with effects",
117 [](Pixel& p) {
118 p.foreground_color = Color::Red;
119 p.underlined = true;
120 p.bold = true;
121 });
122 return canvas(std::move(c));
123 });
124
125 auto renderer_plot_1 = Renderer([&] {
126 auto c = Canvas(100, 100);
127 c.DrawText(0, 0, "A graph");
128
129 std::vector<int> ys(100);
130 for (int x = 0; x < 100; x++) {
131 float dx = float(x - mouse_x);
132 float dy = 50.f;
133 ys[x] = int(dy + 20 * cos(dx * 0.14) + 10 * sin(dx * 0.42));
134 }
135 for (int x = 1; x < 99; x++) {
136 c.DrawPointLine(x, ys[x], x + 1, ys[x + 1]);
137 }
138
139 return canvas(std::move(c));
140 });
141
142 auto renderer_plot_2 = Renderer([&] {
143 auto c = Canvas(100, 100);
144 c.DrawText(0, 0, "A symmetrical graph filled");
145 std::vector<int> ys(100);
146 for (int x = 0; x < 100; x++) {
147 ys[x] = int(30 + //
148 10 * cos(x * 0.2 - mouse_x * 0.05) + //
149 5 * sin(x * 0.4) + //
150 5 * sin(x * 0.3 - mouse_y * 0.05)); //
151 }
152 for (int x = 0; x < 100; x++) {
153 c.DrawPointLine(x, 50 + ys[x], x, 50 - ys[x], Color::Red);
154 }
155
156 return canvas(std::move(c));
157 });
158
159 auto renderer_plot_3 = Renderer([&] {
160 auto c = Canvas(100, 100);
161 c.DrawText(0, 0, "A 2D gaussian plot");
162 int size = 15;
163
164 // mouse_x = 5mx + 3*my
165 // mouse_y = 0mx + -5my + 90
166 float my = (mouse_y - 90) / -5.f;
167 float mx = (mouse_x - 3 * my) / 5.f;
168 std::vector<std::vector<float>> ys(size, std::vector<float>(size));
169 for (int y = 0; y < size; y++) {
170 for (int x = 0; x < size; x++) {
171 float dx = x - mx;
172 float dy = y - my;
173 ys[y][x] = -1.5 + 3.0 * std::exp(-0.2f * (dx * dx + dy * dy));
174 }
175 }
176 for (int y = 0; y < size; y++) {
177 for (int x = 0; x < size; x++) {
178 if (x != 0) {
179 c.DrawPointLine(
180 5 * (x - 1) + 3 * (y - 0), 90 - 5 * (y - 0) - 5 * ys[y][x - 1],
181 5 * (x - 0) + 3 * (y - 0), 90 - 5 * (y - 0) - 5 * ys[y][x]);
182 }
183 if (y != 0) {
184 c.DrawPointLine(
185 5 * (x - 0) + 3 * (y - 1), 90 - 5 * (y - 1) - 5 * ys[y - 1][x],
186 5 * (x - 0) + 3 * (y - 0), 90 - 5 * (y - 0) - 5 * ys[y][x]);
187 }
188 }
189 }
190
191 return canvas(std::move(c));
192 });
193
194 int selected_tab = 12;
195 auto tab = Container::Tab(
196 {
197 renderer_line_braille,
198 renderer_line_block,
199 renderer_circle_braille,
200 renderer_circle_block,
201 renderer_circle_filled_braille,
202 renderer_circle_filled_block,
203 renderer_ellipse_braille,
204 renderer_ellipse_block,
205 renderer_ellipse_filled_braille,
206 renderer_ellipse_filled_block,
207
208 renderer_plot_1,
209 renderer_plot_2,
210 renderer_plot_3,
211
212 renderer_text,
213 },
214 &selected_tab);
215
216 // 這會捕捉最後的滑鼠位置。
217 auto tab_with_mouse = CatchEvent(tab, [&](Event e) {
218 if (e.is_mouse()) {
219 mouse_x = (e.mouse().x - 1) * 2;
220 mouse_y = (e.mouse().y - 1) * 4;
221 }
222 return false;
223 });
224
225 std::vector<std::string> tab_titles = {
226 "line (braille)",
227 "line (block)",
228 "circle (braille)",
229 "circle (block)",
230 "circle filled (braille)",
231 "circle filled (block)",
232 "ellipse (braille)",
233 "ellipse (block)",
234 "ellipse filled (braille)",
235 "ellipse filled (block)",
236 "plot_1 simple",
237 "plot_2 filled",
238 "plot_3 3D",
239 "text",
240 };
241 auto tab_toggle = Menu(&tab_titles, &selected_tab);
242
243 auto component = Container::Horizontal({
244 tab_with_mouse,
245 tab_toggle,
246 });
247
248 // 添加一些分隔符來裝飾整個組件:
249 auto component_renderer = Renderer(component, [&] {
250 return hbox({
251 tab_with_mouse->Render(),
252 separator(),
253 tab_toggle->Render(),
254 }) |
255 border;
256 });
257
258 auto screen = ScreenInteractive::FitComponent();
259 screen.Loop(component_renderer);
260
261 return 0;
262}
int main()
auto component
Definition gallery.cpp:127
bool is_mouse() const
Definition event.hpp:107
struct Mouse mouse
Definition event.hpp:142
代表一個事件。它可以是按鍵事件、終端機大小調整,或更多...
Definition event.hpp:27
畫布是與繪圖操作相關聯的可繪製緩衝區。
Definition canvas.hpp:35
Color foreground_color
Definition pixel.hpp:48
bool bold
Definition pixel.hpp:28
bool underlined
Definition pixel.hpp:32
一個 Unicode 字元及其相關樣式。
Definition pixel.hpp:14
FTXUI 的 ftxui:: 命名空間
Definition animation.hpp:10
Element canvas(ConstRef< Canvas > canvas)
從 Canvas 或對 Canvas 的參考中產生一個元素。