mirror of
				https://github.com/ArthurSonzogni/FTXUI.git
				synced 2025-10-31 18:48:11 +08:00 
			
		
		
		
	Feature: Canvas (#287)
Draw using braille and block characters on a grid.
This commit is contained in:
		
							
								
								
									
										137
									
								
								include/ftxui/dom/canvas.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										137
									
								
								include/ftxui/dom/canvas.hpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,137 @@ | ||||
| #ifndef FTXUI_DOM_CANVAS_HPP | ||||
| #define FTXUI_DOM_CANVAS_HPP | ||||
|  | ||||
| #include "ftxui/screen/color.hpp" | ||||
| #include "ftxui/screen/screen.hpp" | ||||
| #include <unordered_map> | ||||
| #include <functional> | ||||
|  | ||||
| namespace ftxui { | ||||
|  | ||||
| struct Canvas { | ||||
|  public: | ||||
|   Canvas() {} | ||||
|   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); | ||||
|  | ||||
|   // 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 { | ||||
|     kBraille, | ||||
|     kBlock, | ||||
|     kText, | ||||
|   }; | ||||
|   struct Cell { | ||||
|     CellType type = kText; | ||||
|     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 { return xy.x * 1024 + xy.y; } | ||||
|   }; | ||||
|  | ||||
|   int width_ = 0; | ||||
|   int height_ = 0; | ||||
|   std::unordered_map<XY, Cell, XYHash> storage_; | ||||
| }; | ||||
|  | ||||
| }  // namespace ftxui | ||||
|  | ||||
| #endif // FTXUI_DOM_CANVAS_HPP | ||||
| @@ -4,12 +4,14 @@ | ||||
| #include <functional> | ||||
| #include <memory> | ||||
|  | ||||
| #include "ftxui/dom/canvas.hpp" | ||||
| #include "ftxui/dom/flexbox_config.hpp" | ||||
| #include "ftxui/dom/node.hpp" | ||||
| #include "ftxui/screen/box.hpp" | ||||
| #include "ftxui/screen/color.hpp" | ||||
| #include "ftxui/screen/screen.hpp" | ||||
| #include "ftxui/screen/terminal.hpp" | ||||
| #include "ftxui/util/ref.hpp" | ||||
|  | ||||
| namespace ftxui { | ||||
| class Node; | ||||
| @@ -57,6 +59,7 @@ Element paragraphAlignCenter(std::string text); | ||||
| Element paragraphAlignJustify(std::string text); | ||||
| Element graph(GraphFunction); | ||||
| Element emptyElement(); | ||||
| Element ElementFrom(ConstRef<Canvas>); | ||||
|  | ||||
| // -- Decorator --- | ||||
| Element bold(Element); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Arthur Sonzogni
					Arthur Sonzogni