FTXUI  2.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 <stddef.h> // 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
12namespace ftxui {
13
14struct Canvas {
15 public:
16 Canvas() {}
17 Canvas(int width, int height);
18
19 // Getters:
20 int width() const { return width_; }
21 int height() const { return height_; }
22 Pixel GetPixel(int x, int y) const;
23
24 using Stylizer = std::function<void(Pixel&)>;
25
26 // Draws using braille characters --------------------------------------------
27 void DrawPointOn(int x, int y);
28 void DrawPointOff(int x, int y);
29 void DrawPointToggle(int x, int y);
30 void DrawPoint(int x, int y, bool value);
31 void DrawPoint(int x, int y, bool value, const Stylizer& s);
32 void DrawPoint(int x, int y, bool value, const Color& color);
33 void DrawPointLine(int x1, int y1, int x2, int y2);
34 void DrawPointLine(int x1, int y1, int x2, int y2, const Stylizer& s);
35 void DrawPointLine(int x1, int y1, int x2, int y2, const Color& color);
36 void DrawPointCircle(int x, int y, int radius);
37 void DrawPointCircle(int x, int y, int radius, const Stylizer& s);
38 void DrawPointCircle(int x, int y, int radius, const Color& color);
39 void DrawPointCircleFilled(int x, int y, int radius);
40 void DrawPointCircleFilled(int x, int y, int radius, const Stylizer& s);
41 void DrawPointCircleFilled(int x, int y, int radius, const Color& color);
42 void DrawPointEllipse(int x, int y, int r1, int r2);
43 void DrawPointEllipse(int x, int y, int r1, int r2, const Color& color);
44 void DrawPointEllipse(int x, int y, int r1, int r2, const Stylizer& s);
45 void DrawPointEllipseFilled(int x, int y, int r1, int r2);
46 void DrawPointEllipseFilled(int x, int y, int r1, int r2, const Color& color);
47 void DrawPointEllipseFilled(int x, int y, int r1, int r2, const Stylizer& s);
48
49 // Draw using box characters -------------------------------------------------
50 // Block are of size 1x2. y is considered to be a multiple of 2.
51 void DrawBlockOn(int x, int y);
52 void DrawBlockOff(int x, int y);
53 void DrawBlockToggle(int x, int y);
54 void DrawBlock(int x, int y, bool value);
55 void DrawBlock(int x, int y, bool value, const Stylizer& s);
56 void DrawBlock(int x, int y, bool value, const Color& color);
57 void DrawBlockLine(int x1, int y1, int x2, int y2);
58 void DrawBlockLine(int x1, int y1, int x2, int y2, const Stylizer& s);
59 void DrawBlockLine(int x1, int y1, int x2, int y2, const Color& color);
60 void DrawBlockCircle(int x1, int y1, int radius);
61 void DrawBlockCircle(int x1, int y1, int radius, const Stylizer& s);
62 void DrawBlockCircle(int x1, int y1, int radius, const Color& color);
63 void DrawBlockCircleFilled(int x1, int y1, int radius);
64 void DrawBlockCircleFilled(int x1, int y1, int radius, const Stylizer& s);
65 void DrawBlockCircleFilled(int x1, int y1, int radius, const Color& color);
66 void DrawBlockEllipse(int x1, int y1, int r1, int r2);
67 void DrawBlockEllipse(int x1, int y1, int r1, int r2, const Stylizer& s);
68 void DrawBlockEllipse(int x1, int y1, int r1, int r2, const Color& color);
69 void DrawBlockEllipseFilled(int x1, int y1, int r1, int r2);
70 void DrawBlockEllipseFilled(int x1,
71 int y1,
72 int r1,
73 int r2,
74 const Stylizer& s);
75 void DrawBlockEllipseFilled(int x1,
76 int y1,
77 int r1,
78 int r2,
79 const Color& color);
80
81 // Draw using normal characters ----------------------------------------------
82 // Draw using character of size 2x4 at position (x,y)
83 // x is considered to be a multiple of 2.
84 // y is considered to be a multiple of 4.
85 void DrawText(int x, int y, const std::string& value);
86 void DrawText(int x, int y, const std::string& value, const Color& color);
87 void DrawText(int x, int y, const std::string& value, const Stylizer& style);
88
89 // Decorator:
90 // x is considered to be a multiple of 2.
91 // y is considered to be a multiple of 4.
92 void Style(int x, int y, const Stylizer& style);
93
94 private:
95 bool IsIn(int x, int y) const {
96 return x >= 0 && x < width_ && y >= 0 && y < height_;
97 }
98 enum CellType {
99 kBraille,
100 kBlock,
101 kText,
102 };
103 struct Cell {
104 CellType type = kText;
105 Pixel content;
106 };
107 struct XY {
108 int x;
109 int y;
110 bool operator==(const XY& other) const {
111 return x == other.x && y == other.y;
112 }
113 };
114
115 struct XYHash {
116 size_t operator()(const XY& xy) const {
117 return static_cast<size_t>(xy.x * 1024 + xy.y);
118 }
119 };
120
121 int width_ = 0;
122 int height_ = 0;
123 std::unordered_map<XY, Cell, XYHash> storage_;
124};
125
126} // namespace ftxui
127
128#endif // FTXUI_DOM_CANVAS_HPP
129
130// Copyright 2021 Arthur Sonzogni. All rights reserved.
131// Use of this source code is governed by the MIT license that can be found in
132// 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:506
void DrawPointEllipseFilled(int x, int y, int r1, int r2)
Draw a filled ellipse made of braille dots.
Definition canvas.cpp:353
void DrawPointLine(int x1, int y1, int x2, int y2)
Draw a line made of braille dots.
Definition canvas.cpp:172
void DrawText(int x, int y, const std::string &value)
Draw a piece of text.
Definition canvas.cpp:758
std::function< void(Pixel &)> Stylizer
Definition canvas.hpp:24
void DrawBlockOn(int x, int y)
Draw a block.
Definition canvas.cpp:446
void DrawPointCircleFilled(int x, int y, int radius)
Draw a filled circle made of braille dots.
Definition canvas.cpp:255
void DrawPointOn(int x, int y)
Draw a braille dot.
Definition canvas.cpp:121
void DrawPointOff(int x, int y)
Erase a braille dot.
Definition canvas.cpp:137
Pixel GetPixel(int x, int y) const
Get the content of a cell.
Definition canvas.cpp:83
void DrawBlockEllipseFilled(int x1, int y1, int r1, int r2)
Draw a filled ellipse made of block characters.
Definition canvas.cpp:692
void DrawPointEllipse(int x, int y, int r1, int r2)
Draw an ellipse made of braille dots.
Definition canvas.cpp:289
void DrawPoint(int x, int y, bool value)
Draw a braille dot.
Definition canvas.cpp:92
void DrawBlockEllipse(int x1, int y1, int r1, int r2)
Draw an ellipse made of block characters.
Definition canvas.cpp:626
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:485
void DrawBlockCircle(int x1, int y1, int radius)
Draw a circle made of block characters.
Definition canvas.cpp:565
void DrawBlockCircleFilled(int x1, int y1, int radius)
Draw a filled circle made of block characters.
Definition canvas.cpp:592
void DrawPointCircle(int x, int y, int radius)
Draw a circle made of braille dots.
Definition canvas.cpp:228
int height() const
Definition canvas.hpp:21
void DrawBlockOff(int x, int y)
Erase a block.
Definition canvas.cpp:465
int width() const
Definition canvas.hpp:20
void DrawBlock(int x, int y, bool value)
Draw a block.
Definition canvas.cpp:417
void Style(int x, int y, const Stylizer &style)
Modify a pixel at a given location.
Definition canvas.cpp:796
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:154
A unicode character and its associated style.
Definition screen.hpp:16