Loading...
Searching...
No Matches
canvas.hpp
Go to the documentation of this file.
// Copyright 2021 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
#ifndef FTXUI_DOM_CANVAS_HPP
#define FTXUI_DOM_CANVAS_HPP
#include <cstddef> // for size_t
#include <functional> // for function
#include <string> // for string
#include <unordered_map> // for unordered_map
#include "ftxui/screen/color.hpp" // for Color
#include "ftxui/screen/image.hpp" // for Pixel, Image
#ifdef DrawText
// Workaround for WinUsr.h (via Windows.h) defining macros that break things.
// https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-drawtext
#undef DrawText
#endif
namespace ftxui {
struct Canvas {
public:
Canvas() = default;
Canvas(int width, int height);
// Getters:
int width() const { return width_; }
int height() const { return height_; }
Pixel GetPixel(int x, int y) const;
using Stylizer = std::function<void(Pixel&)>;
// Draws using braille characters --------------------------------------------
void DrawPointOn(int x, int y);
void DrawPointOff(int x, int y);
void DrawPointToggle(int x, int y);
void DrawPoint(int x, int y, bool value);
void DrawPoint(int x, int y, bool value, const Stylizer& s);
void DrawPoint(int x, int y, bool value, const Color& color);
void DrawPointLine(int x1, int y1, int x2, int y2);
void DrawPointLine(int x1, int y1, int x2, int y2, const Stylizer& s);
void DrawPointLine(int x1, int y1, int x2, int y2, const Color& color);
void DrawPointCircle(int x, int y, int radius);
void DrawPointCircle(int x, int y, int radius, const Stylizer& s);
void DrawPointCircle(int x, int y, int radius, const Color& color);
void DrawPointCircleFilled(int x, int y, int radius);
void DrawPointCircleFilled(int x, int y, int radius, const Stylizer& s);
void DrawPointCircleFilled(int x, int y, int radius, const Color& color);
void DrawPointEllipse(int x, int y, int r1, int r2);
void DrawPointEllipse(int x, int y, int r1, int r2, const Color& color);
void DrawPointEllipse(int x, int y, int r1, int r2, const Stylizer& s);
void DrawPointEllipseFilled(int x, int y, int r1, int r2);
void DrawPointEllipseFilled(int x, int y, int r1, int r2, const Color& color);
void DrawPointEllipseFilled(int x, int y, int r1, int r2, const Stylizer& s);
// Draw using box characters -------------------------------------------------
// Block are of size 1x2. y is considered to be a multiple of 2.
void DrawBlockOn(int x, int y);
void DrawBlockOff(int x, int y);
void DrawBlockToggle(int x, int y);
void DrawBlock(int x, int y, bool value);
void DrawBlock(int x, int y, bool value, const Stylizer& s);
void DrawBlock(int x, int y, bool value, const Color& color);
void DrawBlockLine(int x1, int y1, int x2, int y2);
void DrawBlockLine(int x1, int y1, int x2, int y2, const Stylizer& s);
void DrawBlockLine(int x1, int y1, int x2, int y2, const Color& color);
void DrawBlockCircle(int x1, int y1, int radius);
void DrawBlockCircle(int x1, int y1, int radius, const Stylizer& s);
void DrawBlockCircle(int x1, int y1, int radius, const Color& color);
void DrawBlockCircleFilled(int x1, int y1, int radius);
void DrawBlockCircleFilled(int x1, int y1, int radius, const Stylizer& s);
void DrawBlockCircleFilled(int x1, int y1, int radius, const Color& color);
void DrawBlockEllipse(int x1, int y1, int r1, int r2);
void DrawBlockEllipse(int x1, int y1, int r1, int r2, const Stylizer& s);
void DrawBlockEllipse(int x1, int y1, int r1, int r2, const Color& color);
void DrawBlockEllipseFilled(int x1, int y1, int r1, int r2);
void DrawBlockEllipseFilled(int x1,
int y1,
int r1,
int r2,
const Stylizer& s);
void DrawBlockEllipseFilled(int x1,
int y1,
int r1,
int r2,
const Color& color);
// Draw using normal characters ----------------------------------------------
// Draw using character of size 2x4 at position (x,y)
// x is considered to be a multiple of 2.
// y is considered to be a multiple of 4.
void DrawText(int x, int y, const std::string& value);
void DrawText(int x, int y, const std::string& value, const Color& color);
void DrawText(int x, int y, const std::string& value, const Stylizer& style);
// Draw using directly pixels or images --------------------------------------
// x is considered to be a multiple of 2.
// y is considered to be a multiple of 4.
void DrawPixel(int x, int y, const Pixel&);
void DrawImage(int x, int y, const Image&);
// Decorator:
// x is considered to be a multiple of 2.
// y is considered to be a multiple of 4.
void Style(int x, int y, const Stylizer& style);
private:
bool IsIn(int x, int y) const {
return x >= 0 && x < width_ && y >= 0 && y < height_;
}
enum CellType {
kCell, // Units of size 2x4
kBlock, // Units of size 2x2
kBraille, // Units of size 1x1
};
struct Cell {
CellType type = kCell;
Pixel content;
};
struct XY {
int x;
int y;
bool operator==(const XY& other) const {
return x == other.x && y == other.y;
}
};
struct XYHash {
size_t operator()(const XY& xy) const {
constexpr size_t shift = 1024;
return size_t(xy.x) * shift + size_t(xy.y);
}
};
int width_ = 0;
int height_ = 0;
std::unordered_map<XY, Cell, XYHash> storage_;
};
} // namespace ftxui
#endif // FTXUI_DOM_CANVAS_HPP
ftxui::Color
A class representing terminal colors.
Definition color.hpp:20
ftxui::Image
A rectangular grid of Pixel.
Definition image.hpp:17
color.hpp
image.hpp
ftxui
Definition animation.hpp:10
ftxui::Make
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:26
ftxui::color
Decorator color(Color)
Decorate using a foreground color.
Definition color.cpp:110
ftxui::Canvas
Definition canvas.hpp:23
ftxui::Canvas::DrawImage
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
ftxui::Canvas::DrawBlockLine
void DrawBlockLine(int x1, int y1, int x2, int y2)
Draw a line made of block characters.
Definition canvas.cpp:531
ftxui::Canvas::DrawPointEllipseFilled
void DrawPointEllipseFilled(int x, int y, int r1, int r2)
Draw a filled ellipse made of braille dots.
Definition canvas.cpp:374
ftxui::Canvas::DrawPointLine
void DrawPointLine(int x1, int y1, int x2, int y2)
Draw a line made of braille dots.
Definition canvas.cpp:191
ftxui::Canvas::DrawText
void DrawText(int x, int y, const std::string &value)
Draw a piece of text.
Definition canvas.cpp:785
ftxui::Canvas::Canvas
Canvas()=default
ftxui::Canvas::Stylizer
std::function< void(Pixel &)> Stylizer
Definition canvas.hpp:33
ftxui::Canvas::DrawBlockOn
void DrawBlockOn(int x, int y)
Draw a block.
Definition canvas.cpp:468
ftxui::Canvas::DrawPointCircleFilled
void DrawPointCircleFilled(int x, int y, int radius)
Draw a filled circle made of braille dots.
Definition canvas.cpp:276
ftxui::Canvas::DrawPointOn
void DrawPointOn(int x, int y)
Draw a braille dot.
Definition canvas.cpp:137
ftxui::Canvas::DrawPointOff
void DrawPointOff(int x, int y)
Erase a braille dot.
Definition canvas.cpp:154
ftxui::Canvas::GetPixel
Pixel GetPixel(int x, int y) const
Get the content of a cell.
Definition canvas.cpp:98
ftxui::Canvas::DrawBlockEllipseFilled
void DrawBlockEllipseFilled(int x1, int y1, int r1, int r2)
Draw a filled ellipse made of block characters.
Definition canvas.cpp:719
ftxui::Canvas::DrawPointEllipse
void DrawPointEllipse(int x, int y, int r1, int r2)
Draw an ellipse made of braille dots.
Definition canvas.cpp:310
ftxui::Canvas::DrawPoint
void DrawPoint(int x, int y, bool value)
Draw a braille dot.
Definition canvas.cpp:107
ftxui::Canvas::DrawBlockEllipse
void DrawBlockEllipse(int x1, int y1, int r1, int r2)
Draw an ellipse made of block characters.
Definition canvas.cpp:653
ftxui::Canvas::DrawBlockToggle
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
ftxui::Canvas::DrawBlockCircle
void DrawBlockCircle(int x1, int y1, int radius)
Draw a circle made of block characters.
Definition canvas.cpp:592
ftxui::Canvas::DrawBlockCircleFilled
void DrawBlockCircleFilled(int x1, int y1, int radius)
Draw a filled circle made of block characters.
Definition canvas.cpp:619
ftxui::Canvas::DrawPointCircle
void DrawPointCircle(int x, int y, int radius)
Draw a circle made of braille dots.
Definition canvas.cpp:249
ftxui::Canvas::height
int height() const
Definition canvas.hpp:30
ftxui::Canvas::DrawBlockOff
void DrawBlockOff(int x, int y)
Erase a block.
Definition canvas.cpp:488
ftxui::Canvas::width
int width() const
Definition canvas.hpp:29
ftxui::Canvas::DrawBlock
void DrawBlock(int x, int y, bool value)
Draw a block.
Definition canvas.cpp:438
ftxui::Canvas::Style
void Style(int x, int y, const Stylizer &style)
Modify a pixel at a given location.
Definition canvas.cpp:861
ftxui::Canvas::DrawPointToggle
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
ftxui::Canvas::DrawPixel
void DrawPixel(int x, int y, const Pixel &)
Directly draw a predefined pixel at the given coordinate.
Definition canvas.cpp:827
ftxui::Pixel
A Unicode character and its associated style.
Definition pixel.hpp:15