FTXUI  3.0.0
C++ functional terminal UI.
Loading...
Searching...
No Matches
canvas.hpp
Go to the documentation of this file.
1#ifndef FTXUI_DOM_CANVAS_HPP
2#define FTXUI_DOM_CANVAS_HPP
3
4#include <cstddef> // for size_t
5#include <functional> // for function
6#include <string> // for string
7#include <unordered_map> // for unordered_map
8
9#include "ftxui/screen/color.hpp" // for Color
10#include "ftxui/screen/screen.hpp" // for Pixel
11
12#ifdef DrawText
13// Workaround for WinUsr.h (via Windows.h) defining macros that break things.
14// https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-drawtext
15#undef DrawText
16#endif
17
18namespace ftxui {
19
20struct Canvas {
21 public:
22 Canvas() = default;
23 Canvas(int width, int height);
24
25 // Getters:
26 int width() const { return width_; }
27 int height() const { return height_; }
28 Pixel GetPixel(int x, int y) const;
29
30 using Stylizer = std::function<void(Pixel&)>;
31
32 // Draws using braille characters --------------------------------------------
33 void DrawPointOn(int x, int y);
34 void DrawPointOff(int x, int y);
35 void DrawPointToggle(int x, int y);
36 void DrawPoint(int x, int y, bool value);
37 void DrawPoint(int x, int y, bool value, const Stylizer& s);
38 void DrawPoint(int x, int y, bool value, const Color& color);
39 void DrawPointLine(int x1, int y1, int x2, int y2);
40 void DrawPointLine(int x1, int y1, int x2, int y2, const Stylizer& s);
41 void DrawPointLine(int x1, int y1, int x2, int y2, const Color& color);
42 void DrawPointCircle(int x, int y, int radius);
43 void DrawPointCircle(int x, int y, int radius, const Stylizer& s);
44 void DrawPointCircle(int x, int y, int radius, const Color& color);
45 void DrawPointCircleFilled(int x, int y, int radius);
46 void DrawPointCircleFilled(int x, int y, int radius, const Stylizer& s);
47 void DrawPointCircleFilled(int x, int y, int radius, const Color& color);
48 void DrawPointEllipse(int x, int y, int r1, int r2);
49 void DrawPointEllipse(int x, int y, int r1, int r2, const Color& color);
50 void DrawPointEllipse(int x, int y, int r1, int r2, const Stylizer& s);
51 void DrawPointEllipseFilled(int x, int y, int r1, int r2);
52 void DrawPointEllipseFilled(int x, int y, int r1, int r2, const Color& color);
53 void DrawPointEllipseFilled(int x, int y, int r1, int r2, const Stylizer& s);
54
55 // Draw using box characters -------------------------------------------------
56 // Block are of size 1x2. y is considered to be a multiple of 2.
57 void DrawBlockOn(int x, int y);
58 void DrawBlockOff(int x, int y);
59 void DrawBlockToggle(int x, int y);
60 void DrawBlock(int x, int y, bool value);
61 void DrawBlock(int x, int y, bool value, const Stylizer& s);
62 void DrawBlock(int x, int y, bool value, const Color& color);
63 void DrawBlockLine(int x1, int y1, int x2, int y2);
64 void DrawBlockLine(int x1, int y1, int x2, int y2, const Stylizer& s);
65 void DrawBlockLine(int x1, int y1, int x2, int y2, const Color& color);
66 void DrawBlockCircle(int x1, int y1, int radius);
67 void DrawBlockCircle(int x1, int y1, int radius, const Stylizer& s);
68 void DrawBlockCircle(int x1, int y1, int radius, const Color& color);
69 void DrawBlockCircleFilled(int x1, int y1, int radius);
70 void DrawBlockCircleFilled(int x1, int y1, int radius, const Stylizer& s);
71 void DrawBlockCircleFilled(int x1, int y1, int radius, const Color& color);
72 void DrawBlockEllipse(int x1, int y1, int r1, int r2);
73 void DrawBlockEllipse(int x1, int y1, int r1, int r2, const Stylizer& s);
74 void DrawBlockEllipse(int x1, int y1, int r1, int r2, const Color& color);
75 void DrawBlockEllipseFilled(int x1, int y1, int r1, int r2);
76 void DrawBlockEllipseFilled(int x1,
77 int y1,
78 int r1,
79 int r2,
80 const Stylizer& s);
81 void DrawBlockEllipseFilled(int x1,
82 int y1,
83 int r1,
84 int r2,
85 const Color& color);
86
87 // Draw using normal characters ----------------------------------------------
88 // Draw using character of size 2x4 at position (x,y)
89 // x is considered to be a multiple of 2.
90 // y is considered to be a multiple of 4.
91 void DrawText(int x, int y, const std::string& value);
92 void DrawText(int x, int y, const std::string& value, const Color& color);
93 void DrawText(int x, int y, const std::string& value, const Stylizer& style);
94
95 // Decorator:
96 // x is considered to be a multiple of 2.
97 // y is considered to be a multiple of 4.
98 void Style(int x, int y, const Stylizer& style);
99
100 private:
101 bool IsIn(int x, int y) const {
102 return x >= 0 && x < width_ && y >= 0 && y < height_;
103 }
104 enum CellType {
105 kBraille,
106 kBlock,
107 kText,
108 };
109 struct Cell {
110 CellType type = kText;
111 Pixel content;
112 };
113 struct XY {
114 int x;
115 int y;
116 bool operator==(const XY& other) const {
117 return x == other.x && y == other.y;
118 }
119 };
120
121 struct XYHash {
122 size_t operator()(const XY& xy) const {
123 constexpr size_t shift = 1024;
124 return size_t(xy.x) * shift + size_t(xy.y);
125 }
126 };
127
128 int width_ = 0;
129 int height_ = 0;
130 std::unordered_map<XY, Cell, XYHash> storage_;
131};
132
133} // namespace ftxui
134
135#endif // FTXUI_DOM_CANVAS_HPP
136
137// Copyright 2021 Arthur Sonzogni. All rights reserved.
138// Use of this source code is governed by the MIT license that can be found in
139// the LICENSE file.
A class representing terminal colors.
Definition color.hpp:17
Decorator color(Color)
Decorate using a foreground color.
Definition color.cpp:86
void DrawBlockLine(int x1, int y1, int x2, int y2)
Draw a line made of block characters.
Definition canvas.cpp:523
void DrawPointEllipseFilled(int x, int y, int r1, int r2)
Draw a filled ellipse made of braille dots.
Definition canvas.cpp:366
void DrawPointLine(int x1, int y1, int x2, int y2)
Draw a line made of braille dots.
Definition canvas.cpp:183
void DrawText(int x, int y, const std::string &value)
Draw a piece of text.
Definition canvas.cpp:777
Canvas()=default
std::function< void(Pixel &)> Stylizer
Definition canvas.hpp:30
void DrawBlockOn(int x, int y)
Draw a block.
Definition canvas.cpp:460
void DrawPointCircleFilled(int x, int y, int radius)
Draw a filled circle made of braille dots.
Definition canvas.cpp:268
void DrawPointOn(int x, int y)
Draw a braille dot.
Definition canvas.cpp:129
void DrawPointOff(int x, int y)
Erase a braille dot.
Definition canvas.cpp:146
Pixel GetPixel(int x, int y) const
Get the content of a cell.
Definition canvas.cpp:90
void DrawBlockEllipseFilled(int x1, int y1, int r1, int r2)
Draw a filled ellipse made of block characters.
Definition canvas.cpp:711
void DrawPointEllipse(int x, int y, int r1, int r2)
Draw an ellipse made of braille dots.
Definition canvas.cpp:302
void DrawPoint(int x, int y, bool value)
Draw a braille dot.
Definition canvas.cpp:99
void DrawBlockEllipse(int x1, int y1, int r1, int r2)
Draw an ellipse made of block characters.
Definition canvas.cpp:645
void DrawBlockToggle(int x, int y)
Toggle a block. If it is filled, it will be erased. If it is empty, it will be filled.
Definition canvas.cpp:501
void DrawBlockCircle(int x1, int y1, int radius)
Draw a circle made of block characters.
Definition canvas.cpp:584
void DrawBlockCircleFilled(int x1, int y1, int radius)
Draw a filled circle made of block characters.
Definition canvas.cpp:611
void DrawPointCircle(int x, int y, int radius)
Draw a circle made of braille dots.
Definition canvas.cpp:241
int height() const
Definition canvas.hpp:27
void DrawBlockOff(int x, int y)
Erase a block.
Definition canvas.cpp:480
int width() const
Definition canvas.hpp:26
void DrawBlock(int x, int y, bool value)
Draw a block.
Definition canvas.cpp:430
void Style(int x, int y, const Stylizer &style)
Modify a pixel at a given location.
Definition canvas.cpp:816
void DrawPointToggle(int x, int y)
Toggle a braille dot. A filled one will be erased, and the other will be drawn.
Definition canvas.cpp:164
A unicode character and its associated style.
Definition screen.hpp:16