osmanip
Library with useful output stream tools like: color and style manipulators, progress bars and terminal graphics.
canvas.hpp
Go to the documentation of this file.
1 //====================================================
2 // File data
3 //====================================================
12 //====================================================
13 // Preprocessor settings
14 //====================================================
15 #pragma once
16 #ifndef OSMANIP_GRAPHICS_CANVAS_HPP
17 #define OSMANIP_GRAPHICS_CANVAS_HPP
18 
19 //====================================================
20 // Headers
21 //====================================================
22 
23 // STD headers
24 #include <cstdint>
25 #include <string>
26 #include <string_view>
27 #include <vector>
28 
29 namespace osm {
30 
31  //====================================================
32  // FrameStyle
33  //====================================================
38  typedef enum { EMPTY = 0, ASCII = 1, BOX = 2 } FrameStyle;
39 
40  //====================================================
41  // Canvas class
42  //====================================================
52  class Canvas {
53  public:
54 
55  // Constructors
56  explicit Canvas(uint32_t width, uint32_t height);
57 
58  // Setters
59  void enableFrame(bool frame_enabled);
60  void setFrame(FrameStyle, std::string_view feat = "");
61  void setBackground(char c, std::string_view feat = "");
62  void setWidth(uint32_t width);
63  void setHeight(uint32_t height);
64 
65  // Getters
66  char getBackground() const;
67  std::string getBackgroundFeat() const;
68  bool isFrameEnabled() const;
69  std::string getFrameFeat() const;
70  FrameStyle getFrameStyle() const;
71  uint32_t getWidth() const;
72  uint32_t getHeight() const;
73 
74  // Methods
75  void clear();
76  void put(uint32_t x, uint32_t y, char c, std::string_view feat = "");
77  void refresh();
78 
79  private:
80 
81  // Members
82  bool frame_enabled_;
83  FrameStyle frame_style_;
84  std::string frame_feat_;
85  char bg_char_;
86  std::string bg_feat_;
87  std::vector<char> char_buffer_;
88  std::vector<std::string> feat_buffer_;
89  bool already_drawn_;
90 
91  // Constants
92  static const std::vector<std::vector<std::string>> frames;
93 
94  // Methods
95  void resizeCanvas();
96 
97  protected:
98 
99  // Members
100  uint32_t width_, height_;
101  };
102 } // namespace osm
103 
104 #endif
Instances of this class are used to draw in a limited 2D space. All the functions that modify the can...
Definition: canvas.hpp:52
void clear()
Fill the canvas with the background.
Definition: canvas.cpp:186
uint32_t width_
Definition: canvas.hpp:100
std::string getBackgroundFeat() const
Get the optional feat of the background.
Definition: canvas.cpp:143
uint32_t getHeight() const
Get the height of the canvas.
Definition: canvas.cpp:127
uint32_t height_
Definition: canvas.hpp:100
std::string getFrameFeat() const
Get the frame feature (if set).
Definition: canvas.cpp:151
uint32_t getWidth() const
Get the width of the canvas.
Definition: canvas.cpp:119
void setBackground(char c, std::string_view feat="")
Set the char that fills the background and an optional feat.
Definition: canvas.cpp:70
bool isFrameEnabled() const
Return True if the frame is enabled. Otherwise return False.
Definition: canvas.cpp:180
Canvas(uint32_t width, uint32_t height)
Construct a new Canvas:: Canvas object. The size of the canvas in characters must be specified upon c...
Definition: canvas.cpp:53
void setFrame(FrameStyle, std::string_view feat="")
Set the FrameStyle of the canvas and an optional feat.
Definition: canvas.cpp:82
void enableFrame(bool frame_enabled)
Flag to frame or not the canvas. The frame doesn't increase the size taken by the canvas....
Definition: canvas.cpp:172
char getBackground() const
Get the char that fills the background.
Definition: canvas.cpp:135
void put(uint32_t x, uint32_t y, char c, std::string_view feat="")
Put a character in the canvas, given its coordinates and an optional feat. An out-of-bounds exception...
Definition: canvas.cpp:201
FrameStyle getFrameStyle() const
Get the frame style.
Definition: canvas.cpp:159
void setWidth(uint32_t width)
Set the width of the canvas.
Definition: canvas.cpp:93
void refresh()
Display the canvas in the console.
Definition: canvas.cpp:210
void setHeight(uint32_t height)
Set the height of the canvas.
Definition: canvas.cpp:104
Definition: canvas.cpp:30
FrameStyle
Enum class used to define the frame style.
Definition: canvas.hpp:38
@ ASCII
Definition: canvas.hpp:38
@ EMPTY
Definition: canvas.hpp:38
@ BOX
Definition: canvas.hpp:38
const std::string & feat(const std::unordered_map< std::string, std::string > &generic_map, const std::string &feat_string)
It takes an std::map object as the first argument and an std::string object (map key) as the second a...
Definition: common.cpp:41