FTXUI  5.0.0
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/image.hpp" // for Pixel, Image
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);
80 int y1,
81 int r1,
82 int r2,
83 const Stylizer& s);
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 // Draw using directly pixels or images --------------------------------------
99 // x is considered to be a multiple of 2.
100 // y is considered to be a multiple of 4.
101 void DrawPixel(int x, int y, const Pixel&);
102 void DrawImage(int x, int y, const Image&);
103
104 // Decorator:
105 // x is considered to be a multiple of 2.
106 // y is considered to be a multiple of 4.
107 void Style(int x, int y, const Stylizer& style);
108
109 private:
110 bool IsIn(int x, int y) const {
111 return x >= 0 && x < width_ && y >= 0 && y < height_;
112 }
113
114 enum CellType {
115 kCell, // Units of size 2x4
116 kBlock, // Units of size 2x2
117 kBraille, // Units of size 1x1
118 };
119
120 struct Cell {
121 CellType type = kCell;
122 Pixel content;
123 };
124
125 struct XY {
126 int x;
127 int y;
128 bool operator==(const XY& other) const {
129 return x == other.x && y == other.y;
130 }
131 };
132
133 struct XYHash {
134 size_t operator()(const XY& xy) const {
135 constexpr size_t shift = 1024;
136 return size_t(xy.x) * shift + size_t(xy.y);
137 }
138 };
139
140 int width_ = 0;
141 int height_ = 0;
142 std::unordered_map<XY, Cell, XYHash> storage_;
143};
144
145} // namespace ftxui
146
147#endif // FTXUI_DOM_CANVAS_HPP
A class representing terminal colors.
Definition color.hpp:20
A rectangular grid of Pixel.
Definition image.hpp:17
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:26
Decorator color(Color)
Decorate using a foreground color.
Definition color.cpp:110
void DrawImage(int x, int y, const Image &)
Draw a predefined image, with top-left corner at the given coordinate You can supply negative coordin...
Definition canvas.cpp:839
void DrawBlockLine(int x1, int y1, int x2, int y2)
Draw a line made of block characters.
Definition canvas.cpp:531
void DrawPointEllipseFilled(int x, int y, int r1, int r2)
Draw a filled ellipse made of braille dots.
Definition canvas.cpp:374
void DrawPointLine(int x1, int y1, int x2, int y2)
Draw a line made of braille dots.
Definition canvas.cpp:191
void DrawText(int x, int y, const std::string &value)
Draw a piece of text.
Definition canvas.cpp:785
Canvas()=default
std::function< void(Pixel &)> Stylizer
Definition canvas.hpp:33
void DrawBlockOn(int x, int y)
Draw a block.
Definition canvas.cpp:468
void DrawPointCircleFilled(int x, int y, int radius)
Draw a filled circle made of braille dots.
Definition canvas.cpp:276
void DrawPointOn(int x, int y)
Draw a braille dot.
Definition canvas.cpp:137
void DrawPointOff(int x, int y)
Erase a braille dot.
Definition canvas.cpp:154
Pixel GetPixel(int x, int y) const
Get the content of a cell.
Definition canvas.cpp:98
void DrawBlockEllipseFilled(int x1, int y1, int r1, int r2)
Draw a filled ellipse made of block characters.
Definition canvas.cpp:719
void DrawPointEllipse(int x, int y, int r1, int r2)
Draw an ellipse made of braille dots.
Definition canvas.cpp:310
void DrawPoint(int x, int y, bool value)
Draw a braille dot.
Definition canvas.cpp:107
void DrawBlockEllipse(int x1, int y1, int r1, int r2)
Draw an ellipse made of block characters.
Definition canvas.cpp:653
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:509
void DrawBlockCircle(int x1, int y1, int radius)
Draw a circle made of block characters.
Definition canvas.cpp:592
void DrawBlockCircleFilled(int x1, int y1, int radius)
Draw a filled circle made of block characters.
Definition canvas.cpp:619
void DrawPointCircle(int x, int y, int radius)
Draw a circle made of braille dots.
Definition canvas.cpp:249
int height() const
Definition canvas.hpp:30
void DrawBlockOff(int x, int y)
Erase a block.
Definition canvas.cpp:488
int width() const
Definition canvas.hpp:29
void DrawBlock(int x, int y, bool value)
Draw a block.
Definition canvas.cpp:438
void Style(int x, int y, const Stylizer &style)
Modify a pixel at a given location.
Definition canvas.cpp:861
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:172
void DrawPixel(int x, int y, const Pixel &)
Directly draw a predefined pixel at the given coordinate.
Definition canvas.cpp:827
A Unicode character and its associated style.
Definition pixel.hpp:15