FTXUI  5.0.0
C++ functional terminal UI.
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/screen.hpp" // for Pixel
14
15#ifdef DrawText
16// Workaround for WinUsr.h (via Windows.h) defining macros that break things.
17// https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-drawtext
18#undef DrawText
19#endif
20
21namespace ftxui {
22
23struct Canvas {
24 public:
25 Canvas() = default;
26 Canvas(int width, int height);
27
28 // Getters:
29 int width() const { return width_; }
30 int height() const { return height_; }
31 Pixel GetPixel(int x, int y) const;
32
33 using Stylizer = std::function<void(Pixel&)>;
34
35 // Draws using braille characters --------------------------------------------
36 void DrawPointOn(int x, int y);
37 void DrawPointOff(int x, int y);
38 void DrawPointToggle(int x, int y);
39 void DrawPoint(int x, int y, bool value);
40 void DrawPoint(int x, int y, bool value, const Stylizer& s);
41 void DrawPoint(int x, int y, bool value, const Color& color);
42 void DrawPointLine(int x1, int y1, int x2, int y2);
43 void DrawPointLine(int x1, int y1, int x2, int y2, const Stylizer& s);
44 void DrawPointLine(int x1, int y1, int x2, int y2, const Color& color);
45 void DrawPointCircle(int x, int y, int radius);
46 void DrawPointCircle(int x, int y, int radius, const Stylizer& s);
47 void DrawPointCircle(int x, int y, int radius, const Color& color);
48 void DrawPointCircleFilled(int x, int y, int radius);
49 void DrawPointCircleFilled(int x, int y, int radius, const Stylizer& s);
50 void DrawPointCircleFilled(int x, int y, int radius, const Color& color);
51 void DrawPointEllipse(int x, int y, int r1, int r2);
52 void DrawPointEllipse(int x, int y, int r1, int r2, const Color& color);
53 void DrawPointEllipse(int x, int y, int r1, int r2, const Stylizer& s);
54 void DrawPointEllipseFilled(int x, int y, int r1, int r2);
55 void DrawPointEllipseFilled(int x, int y, int r1, int r2, const Color& color);
56 void DrawPointEllipseFilled(int x, int y, int r1, int r2, const Stylizer& s);
57
58 // Draw using box characters -------------------------------------------------
59 // Block are of size 1x2. y is considered to be a multiple of 2.
60 void DrawBlockOn(int x, int y);
61 void DrawBlockOff(int x, int y);
62 void DrawBlockToggle(int x, int y);
63 void DrawBlock(int x, int y, bool value);
64 void DrawBlock(int x, int y, bool value, const Stylizer& s);
65 void DrawBlock(int x, int y, bool value, const Color& color);
66 void DrawBlockLine(int x1, int y1, int x2, int y2);
67 void DrawBlockLine(int x1, int y1, int x2, int y2, const Stylizer& s);
68 void DrawBlockLine(int x1, int y1, int x2, int y2, const Color& color);
69 void DrawBlockCircle(int x1, int y1, int radius);
70 void DrawBlockCircle(int x1, int y1, int radius, const Stylizer& s);
71 void DrawBlockCircle(int x1, int y1, int radius, const Color& color);
72 void DrawBlockCircleFilled(int x1, int y1, int radius);
73 void DrawBlockCircleFilled(int x1, int y1, int radius, const Stylizer& s);
74 void DrawBlockCircleFilled(int x1, int y1, int radius, const Color& color);
75 void DrawBlockEllipse(int x1, int y1, int r1, int r2);
76 void DrawBlockEllipse(int x1, int y1, int r1, int r2, const Stylizer& s);
77 void DrawBlockEllipse(int x1, int y1, int r1, int r2, const Color& color);
78 void DrawBlockEllipseFilled(int x1, int y1, int r1, int r2);
79 void DrawBlockEllipseFilled(int x1,
80 int y1,
81 int r1,
82 int r2,
83 const Stylizer& s);
84 void DrawBlockEllipseFilled(int x1,
85 int y1,
86 int r1,
87 int r2,
88 const Color& color);
89
90 // Draw using normal characters ----------------------------------------------
91 // Draw using character of size 2x4 at position (x,y)
92 // x is considered to be a multiple of 2.
93 // y is considered to be a multiple of 4.
94 void DrawText(int x, int y, const std::string& value);
95 void DrawText(int x, int y, const std::string& value, const Color& color);
96 void DrawText(int x, int y, const std::string& value, const Stylizer& style);
97
98 // Decorator:
99 // x is considered to be a multiple of 2.
100 // y is considered to be a multiple of 4.
101 void Style(int x, int y, const Stylizer& style);
102
103 private:
104 bool IsIn(int x, int y) const {
105 return x >= 0 && x < width_ && y >= 0 && y < height_;
106 }
107 enum CellType {
108 kBraille,
109 kBlock,
110 kText,
111 };
112 struct Cell {
113 CellType type = kText;
114 Pixel content;
115 };
116 struct XY {
117 int x;
118 int y;
119 bool operator==(const XY& other) const {
120 return x == other.x && y == other.y;
121 }
122 };
123
124 struct XYHash {
125 size_t operator()(const XY& xy) const {
126 constexpr size_t shift = 1024;
127 return size_t(xy.x) * shift + size_t(xy.y);
128 }
129 };
130
131 int width_ = 0;
132 int height_ = 0;
133 std::unordered_map<XY, Cell, XYHash> storage_;
134};
135
136} // namespace ftxui
137
138#endif // FTXUI_DOM_CANVAS_HPP
A class representing terminal colors.
Definition: color.hpp:21
Decorator color(Color)
Decorate using a foreground color.
Definition: color.cpp:110
void DrawBlockLine(int x1, int y1, int x2, int y2)
Draw a line made of block characters.
Definition: canvas.cpp:528
void DrawPointEllipseFilled(int x, int y, int r1, int r2)
Draw a filled ellipse made of braille dots.
Definition: canvas.cpp:371
void DrawPointLine(int x1, int y1, int x2, int y2)
Draw a line made of braille dots.
Definition: canvas.cpp:188
void DrawText(int x, int y, const std::string &value)
Draw a piece of text.
Definition: canvas.cpp:782
Canvas()=default
std::function< void(Pixel &)> Stylizer
Definition: canvas.hpp:33
void DrawBlockOn(int x, int y)
Draw a block.
Definition: canvas.cpp:465
void DrawPointCircleFilled(int x, int y, int radius)
Draw a filled circle made of braille dots.
Definition: canvas.cpp:273
void DrawPointOn(int x, int y)
Draw a braille dot.
Definition: canvas.cpp:134
void DrawPointOff(int x, int y)
Erase a braille dot.
Definition: canvas.cpp:151
Pixel GetPixel(int x, int y) const
Get the content of a cell.
Definition: canvas.cpp:95
void DrawBlockEllipseFilled(int x1, int y1, int r1, int r2)
Draw a filled ellipse made of block characters.
Definition: canvas.cpp:716
void DrawPointEllipse(int x, int y, int r1, int r2)
Draw an ellipse made of braille dots.
Definition: canvas.cpp:307
void DrawPoint(int x, int y, bool value)
Draw a braille dot.
Definition: canvas.cpp:104
void DrawBlockEllipse(int x1, int y1, int r1, int r2)
Draw an ellipse made of block characters.
Definition: canvas.cpp:650
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:506
void DrawBlockCircle(int x1, int y1, int radius)
Draw a circle made of block characters.
Definition: canvas.cpp:589
void DrawBlockCircleFilled(int x1, int y1, int radius)
Draw a filled circle made of block characters.
Definition: canvas.cpp:616
void DrawPointCircle(int x, int y, int radius)
Draw a circle made of braille dots.
Definition: canvas.cpp:246
int height() const
Definition: canvas.hpp:30
void DrawBlockOff(int x, int y)
Erase a block.
Definition: canvas.cpp:485
int width() const
Definition: canvas.hpp:29
void DrawBlock(int x, int y, bool value)
Draw a block.
Definition: canvas.cpp:435
void Style(int x, int y, const Stylizer &style)
Modify a pixel at a given location.
Definition: canvas.cpp:822
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:169
A unicode character and its associated style.
Definition: screen.hpp:20