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 (via 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 Canvasは、描画操作に関連付けられた描画可能なバッファです。
25///
26/// Canvasは、複雑なグラフィックを作成するために使用できる描画領域です。
27/// 点、線、円、楕円、テキスト、および画像を点字、ブロック、または通常の文字を使用して描画する
28/// ことができます。
29///
30/// 注: ターミナルにはセルが含まれています。セルは以下の単位です。
31/// - 2x4点字文字 (1x1ピクセル)
32/// - 2x2ブロック文字 (2x2ピクセル)
33/// - 2x4通常文字 (2x4ピクセル)
34///
35/// ターミナルで正しい位置を取得するには、x座標を2倍し、y座標を4倍する必要があります。
36///
37/// @ingroup dom
38struct FTXUI_EXPORT(DOM) 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 Cell GetCell(int x, int y) const;
47 // [非推奨] GetCellのエイリアス。
48 Cell GetPixel(int x, int y) const { return GetCell(x, y); }
49
50 using Stylizer = std::function<void(Cell&)>;
51
52 // 点字文字を使用して描画します --------------------------------------------
53 void DrawPointOn(int x, int y);
54 void DrawPointOff(int x, int y);
55 void DrawPointToggle(int x, int y);
56 void DrawPoint(int x, int y, bool value);
57 void DrawPoint(int x, int y, bool value, const Stylizer& s);
58 void DrawPoint(int x, int y, bool value, const Color& color);
59 void DrawPointLine(int x1, int y1, int x2, int y2);
60 void DrawPointLine(int x1, int y1, int x2, int y2, const Stylizer& s);
61 void DrawPointLine(int x1, int y1, int x2, int y2, const Color& color);
62 void DrawPointCircle(int x, int y, int radius);
63 void DrawPointCircle(int x, int y, int radius, const Stylizer& s);
64 void DrawPointCircle(int x, int y, int radius, const Color& color);
65 void DrawPointCircleFilled(int x, int y, int radius);
66 void DrawPointCircleFilled(int x, int y, int radius, const Stylizer& s);
67 void DrawPointCircleFilled(int x, int y, int radius, const Color& color);
68 void DrawPointEllipse(int x, int y, int r1, int r2);
69 void DrawPointEllipse(int x, int y, int r1, int r2, const Color& color);
70 void DrawPointEllipse(int x, int y, int r1, int r2, const Stylizer& s);
71 void DrawPointEllipseFilled(int x, int y, int r1, int r2);
72 void DrawPointEllipseFilled(int x, int y, int r1, int r2, const Color& color);
73 void DrawPointEllipseFilled(int x, int y, int r1, int r2, const Stylizer& s);
74
75 // ボックス文字を使用した描画 -------------------------------------------------
76 // ブロックは1x2のサイズです。yは2の倍数と見なされます。 void DrawBlockOn(int x, int y);
77 void DrawBlockOn(int x, int y);
78 void DrawBlockOff(int x, int y);
79 void DrawBlockToggle(int x, int y);
80 void DrawBlock(int x, int y, bool value);
81 void DrawBlock(int x, int y, bool value, const Stylizer& s);
82 void DrawBlock(int x, int y, bool value, const Color& color);
83 void DrawBlockLine(int x1, int y1, int x2, int y2);
84 void DrawBlockLine(int x1, int y1, int x2, int y2, const Stylizer& s);
85 void DrawBlockLine(int x1, int y1, int x2, int y2, const Color& color);
86 void DrawBlockCircle(int x1, int y1, int radius);
87 void DrawBlockCircle(int x1, int y1, int radius, const Stylizer& s);
88 void DrawBlockCircle(int x1, int y1, int radius, const Color& color);
89 void DrawBlockCircleFilled(int x1, int y1, int radius);
90 void DrawBlockCircleFilled(int x1, int y1, int radius, const Stylizer& s);
91 void DrawBlockCircleFilled(int x1, int y1, int radius, const Color& color);
92 void DrawBlockEllipse(int x1, int y1, int r1, int r2);
93 void DrawBlockEllipse(int x1, int y1, int r1, int r2, const Stylizer& s);
94 void DrawBlockEllipse(int x1, int y1, int r1, int r2, const Color& color);
95 void DrawBlockEllipseFilled(int x1, int y1, int r1, int r2);
96 void DrawBlockEllipseFilled(int x1,
97 int y1,
98 int r1,
99 int r2,
100 const Stylizer& s);
101 void DrawBlockEllipseFilled(int x1,
102 int y1,
103 int r1,
104 int r2,
105 const Color& color);
106
107 // 通常文字を使用した描画 ----------------------------------------------
108 // (x,y)の位置に2x4サイズの文字を使用して描画します。
109 // xは2の倍数と見なされます。
110 // yは4の倍数と見なされます。 void DrawText(int x, int y, const std::string& value);
111 void DrawText(int x, int y, std::string_view value);
112 void DrawText(int x, int y, std::string_view value, const Color& color);
113 void DrawText(int x, int y, std::string_view value, const Stylizer& style);
114
115 // ピクセルまたは画像を直接使用した描画 --------------------------------------
116 // xは2の倍数と見なされます。
117 // yは4の倍数と見なされます。 void DrawPixel(int x, int y, const Pixel&);
118 void DrawCell(int x, int y, const Cell&);
119 void DrawSurface(int x, int y, const Surface&);
120
121 // [非推奨] DrawCellのエイリアス。
122 void DrawPixel(int x, int y, const Cell& cell) { DrawCell(x, y, cell); }
123 // [非推奨] DrawSurfaceのエイリアス。
124 void DrawImage(int x, int y, const Surface& s) { DrawSurface(x, y, s); }
125
126 // デコレータ:
127 // xは2の倍数と見なされます。
128 // yは4の倍数と見なされます。 void Style(int x, int y, const Stylizer& style);
129 void Style(int x, int y, const Stylizer& style);
130
131 private:
132 bool IsIn(int x, int y) const {
133 return x >= 0 && x < width_ && y >= 0 && y < height_;
134 }
135
136 enum CellType {
137 kCell, // サイズ2x4の単位
138 kBlock, // サイズ2x2の単位
139 kBraille, // サイズ1x1の単位
140 };
141
142 struct CanvasCell {
143 CellType type = kCell;
144 Cell content;
145 };
146
147 struct XY {
148 int x;
149 int y;
150 bool operator==(const XY& other) const {
151 return x == other.x && y == other.y;
152 }
153 };
154
155 struct XYHash {
156 size_t operator()(const XY& xy) const {
157 constexpr size_t shift = 1024;
158 return size_t(xy.x) * shift + size_t(xy.y);
159 }
160 };
161
162 int width_ = 0;
163 int height_ = 0;
164 std::unordered_map<XY, CanvasCell, XYHash> storage_;
165};
166
167} // namespace ftxui
168
169#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