gempyre  1.7.1
gempyre_graphics.h
Go to the documentation of this file.
1 #ifndef GEMPYRE_GRAPHICS_H
2 #define GEMPYRE_GRAPHICS_H
3 
4 #include <initializer_list>
5 #include <variant>
6 #include <array>
7 #include <string_view>
8 
9 #include <gempyre.h>
10 #include <gempyre_bitmap.h> // for compatibility, not really needed, fwd declaration is sufficient
11 
22 #ifdef WINDOWS_EXPORT
23  #ifndef GEMPYRE_EX
24  #define GGEMPYRE_EX __declspec( dllexport )
25  //#else
26  // #define GEMPYRE_EX
27  #endif
28 #endif
29 
30 namespace Gempyre {
31 
33 class FrameComposer;
34 class CanvasData;
35 class Bitmap;
36 using CanvasDataPtr = std::shared_ptr<CanvasData>;
37 
39 class GEMPYRE_EX CanvasElement : public Element {
40 public:
42  using Command = std::variant<std::string, double, int>;
44  using CommandList = std::vector<Command>;
46  using DrawCallback = std::function<void()>;
47 
49  enum class DrawNotify{NoKick, Kick};
50 
53 
56 
59 
63  CanvasElement(Ui& ui, std::string_view id);
64 
69  CanvasElement(Ui& ui, std::string_view id, const Element& parent);
70 
74  CanvasElement(Ui& ui, const Element& parent);
75 
78 
81 
86  std::string add_image(std::string_view url, const std::function<void (std::string_view id)>& loaded = nullptr);
87 
94  void paint_image(std::string_view imageId, int x, int y, const Element::Rect& clippingRect = {0, 0, 0, 0}) const;
95 
101  void paint_image(std::string_view imageId, const Element::Rect& targetRect, const Element::Rect& clippingRect = {0, 0, 0, 0}) const;
102 
105  void draw(const CommandList& canvasCommands);
106 
109  void draw(const FrameComposer& frameComposer);
110 
113  void draw(const Bitmap& bmp) {draw(0, 0, bmp);}
114 
119  void draw(int x, int y, const Bitmap& bmp);
120 
132  void draw_completed(const DrawCallback& drawCompletedCallback, DrawNotify kick = DrawNotify::NoKick);
133 
136  void erase(bool resized = false);
137 private:
138  friend class Bitmap;
139  void paint(const CanvasDataPtr& canvas, int x, int y, bool as_draw);
140 private:
141  CanvasDataPtr m_tile{};
142  int m_width{0};
143  int m_height{0};
144 };
145 
148 public:
154  FrameComposer(FrameComposer&& other) = default;
156  FrameComposer(const FrameComposer& other) = default;
158  FrameComposer stroke_rect(const Gempyre::Element::Rect& r) {return push({"strokeRect", r.x, r.y, r.width, r.height});}
160  FrameComposer stroke_rect(double x, double y, double w, double h) {return push({"strokeRect", x, y, w, h});}
162  FrameComposer clear_rect(const Gempyre::Element::Rect& r) {return push({"clearRect", r.x, r.y, r.width, r.height});}
164  FrameComposer clear_rect(double x, double y, double w, double h) {return push({"clearRect", x, y, w, h});}
166  FrameComposer fill_rect(const Gempyre::Element::Rect& r) {return push({"fillRect", r.x, r.y, r.width, r.height});}
168  FrameComposer fill_rect(double x, double y, double w, double h) {return push({"fillRect", x, y, w, h});}
170  FrameComposer fill_text(std::string_view text, double x, double y) {return push({"fillText", std::string{text}, x, y});}
172  FrameComposer stroke_text(std::string_view text, double x, double y) {return push({"strokeText", std::string{text}, x, y});}
174  FrameComposer arc(double x, double y, double r, double sAngle, double eAngle) {
175  return push({"arc", x, y, r, sAngle, eAngle});}
177  FrameComposer ellipse(double x, double y, double radiusX, double radiusY, double rotation, double startAngle, double endAngle) {
178  return push({"ellipse", x, y, radiusX, radiusY, rotation, startAngle, endAngle});}
180  FrameComposer begin_path() {return push({"beginPath"});}
182  FrameComposer close_path() {return push({"closePath"});}
184  FrameComposer line_to(double x, double y) {return push({"lineTo", x, y});}
186  FrameComposer move_to(double x, double y) {return push({"moveTo", x, y});}
188  FrameComposer bezier_curve_to(double cp1x, double cp1y, double cp2x, double cp2y, double x, double y) {
189  return push({"bezierCurveTo", cp1x, cp1y, cp2x, cp2y, x, y});}
191  FrameComposer quadratic_curve_to(double cpx, double cpy, double x, double y) {
192  return push({"quadraticCurveTo", cpx, cpy, x, y});}
194  FrameComposer arc_to(double x1, double y1, double x2, double y2, double radius) {
195  return push({"arcTo", x1, y1, x2, y2, radius});}
197  FrameComposer rect(const Gempyre::Element::Rect& r) {return push({"rect", r.x, r.y, r.width, r.height});}
199  FrameComposer rect(double x, double y, double w, double h) {return push({"rect", x, y, w, h});}
201  FrameComposer stroke() {return push({"stroke"});}
203  FrameComposer fill() {return push({"fill"});}
205  FrameComposer fill_style(std::string_view color) {return push({"fillStyle", std::string{color}});}
207  FrameComposer stroke_style(std::string_view color) {return push({"strokeStyle", std::string{color}});}
209  FrameComposer line_width(double width) {return push({"lineWidth", width});}
211  FrameComposer font(std::string_view style) {return push({"font", std::string{style}});}
213  FrameComposer text_align(std::string_view align) {return push({"textAlign", std::string{align}});}
215  FrameComposer save() {return push({"save"});}
217  FrameComposer restore() {return push({"restore"});}
219  FrameComposer rotate(double angle) {return push({"rotate", angle});}
221  FrameComposer translate(double x, double y) {return push({"translate", x, y});}
223  FrameComposer scale(const double x, double y) {return push({"scale", x, y});}
225  FrameComposer draw_image(std::string_view id, double x, double y) {return push({"drawImage", std::string{id}, x, y});}
227  FrameComposer draw_image(std::string_view id, const Gempyre::Element::Rect& rect) {return push({"drawImageRect", std::string{id}, rect.x, rect.y, rect.width, rect.height});}
229  FrameComposer draw_image(std::string_view id, double x, double y, double w, double h) {return push({"drawImageRect", std::string{id}, x, y, w, h});}
231  FrameComposer draw_image(std::string_view id, const Gempyre::Element::Rect& clip, const Gempyre::Element::Rect& rect) {return push({"drawImageClip", std::string{id}, clip.x, clip.y, clip.width, clip.height, rect.x, rect.y, rect.width, rect.height});}
233  FrameComposer draw_image(std::string_view id, double cx, double cy, double cw, double ch, double x, double y, double w, double h) {return push({"drawImageClip", std::string{id}, cx, cy, cw, ch, x, y, w, h});}
235  FrameComposer text_baseline(std::string_view textBaseline) {return push({"textBaseline", std::string{textBaseline}});}
237  [[nodiscard]] const Gempyre::CanvasElement::CommandList& composed() const {return m_composition;}
238 private:
239  FrameComposer push(const std::initializer_list<Gempyre::CanvasElement::Command>& list) {m_composition.insert(m_composition.end(), list); return *this;}
240  Gempyre::CanvasElement::CommandList m_composition{};
241 };
242 
244 constexpr std::array<std::pair<std::string_view, uint32_t>, 147> html_colors = {{
245  {"AliceBlue", 0xF0F8FF}, {"AntiqueWhite", 0xFAEBD7}, {"Aqua", 0x00FFFF},
246  {"Aquamarine", 0x7FFFD4}, {"Azure", 0xF0FFFF}, {"Beige", 0xF5F5DC},
247  {"Bisque", 0xFFE4C4}, {"Black", 0x000000}, {"BlanchedAlmond", 0xFFEBCD},
248  {"Blue", 0x0000FF}, {"BlueViolet", 0x8A2BE2}, {"Brown", 0xA52A2A},
249  {"BurlyWood", 0xDEB887}, {"CadetBlue", 0x5F9EA0}, {"Chartreuse", 0x7FFF00},
250  {"Chocolate", 0xD2691E}, {"Coral", 0xFF7F50}, {"CornflowerBlue", 0x6495ED},
251  {"Cornsilk", 0xFFF8DC}, {"Crimson", 0xDC143C}, {"Cyan", 0x00FFFF},
252  {"DarkBlue", 0x00008B}, {"DarkCyan", 0x008B8B}, {"DarkGoldenRod", 0xB8860B},
253  {"DarkGray", 0xA9A9A9}, {"DarkGreen", 0x006400}, {"DarkKhaki", 0xBDB76B},
254  {"DarkMagenta", 0x8B008B}, {"DarkOliveGreen", 0x556B2F}, {"DarkOrange", 0xFF8C00},
255  {"DarkOrchid", 0x9932CC}, {"DarkRed", 0x8B0000}, {"DarkSalmon", 0xE9967A},
256  {"DarkSeaGreen", 0x8FBC8F}, {"DarkSlateBlue", 0x483D8B}, {"DarkSlateGray", 0x2F4F4F},
257  {"DarkTurquoise", 0x00CED1}, {"DarkViolet", 0x9400D3}, {"DeepPink", 0xFF1493},
258  {"DeepSkyBlue", 0x00BFFF}, {"DimGray", 0x696969}, {"DodgerBlue", 0x1E90FF},
259  {"FireBrick", 0xB22222}, {"FloralWhite", 0xFFFAF0}, {"ForestGreen", 0x228B22},
260  {"Fuchsia", 0xFF00FF}, {"Gainsboro", 0xDCDCDC}, {"GhostWhite", 0xF8F8FF},
261  {"Gold", 0xFFD700}, {"GoldenRod", 0xDAA520}, {"Gray", 0x808080},
262  {"Green", 0x008000}, {"GreenYellow", 0xADFF2F}, {"HoneyDew", 0xF0FFF0},
263  {"HotPink", 0xFF69B4}, {"IndianRed", 0xCD5C5C}, {"Indigo", 0x4B0082},
264  {"Ivory", 0xFFFFF0}, {"Khaki", 0xF0E68C}, {"Lavender", 0xE6E6FA},
265  {"LavenderBlush", 0xFFF0F5}, {"LawnGreen", 0x7CFC00}, {"LemonChiffon", 0xFFFACD},
266  {"LightBlue", 0xADD8E6}, {"LightCoral", 0xF08080}, {"LightCyan", 0xE0FFFF},
267  {"LightGoldenRodYellow", 0xFAFAD2}, {"LightGray", 0xD3D3D3}, {"LightGreen", 0x90EE90},
268  {"LightPink", 0xFFB6C1}, {"LightSalmon", 0xFFA07A}, {"LightSeaGreen", 0x20B2AA},
269  {"LightSkyBlue", 0x87CEFA}, {"LightSlateGray", 0x778899}, {"LightSteelBlue", 0xB0C4DE},
270  {"LightYellow", 0xFFFFE0}, {"Lime", 0x00FF00}, {"LimeGreen", 0x32CD32},
271  {"Linen", 0xFAF0E6}, {"Magenta", 0xFF00FF}, {"Maroon", 0x800000},
272  {"MediumAquaMarine", 0x66CDAA}, {"MediumBlue", 0x0000CD}, {"MediumOrchid", 0xBA55D3},
273  {"MediumPurple", 0x9370DB}, {"MediumSeaGreen", 0x3CB371}, {"MediumSlateBlue", 0x7B68EE},
274  {"MediumSpringGreen", 0x00FA9A}, {"MediumTurquoise", 0x48D1CC}, {"MediumVioletRed", 0xC71585},
275  {"MidnightBlue", 0x191970}, {"MintCream", 0xF5FFFA}, {"MistyRose", 0xFFE4E1},
276  {"Moccasin", 0xFFE4B5}, {"NavajoWhite", 0xFFDEAD}, {"Navy", 0x000080},
277  {"OldLace", 0xFDF5E6}, {"Olive", 0x808000}, {"OliveDrab", 0x6B8E23},
278  {"Orange", 0xFFA500}, {"OrangeRed", 0xFF4500}, {"Orchid", 0xDA70D6},
279  {"PaleGoldenRod", 0xEEE8AA}, {"PaleGreen", 0x98FB98}, {"PaleTurquoise", 0xAFEEEE},
280  {"PaleVioletRed", 0xDB7093}, {"PapayaWhip", 0xFFEFD5}, {"PeachPuff", 0xFFDAB9},
281  {"Peru", 0xCD853F}, {"Pink", 0xFFC0CB}, {"Plum", 0xDDA0DD},
282  {"PowderBlue", 0xB0E0E6}, {"Purple", 0x800080}, {"RebeccaPurple", 0x663399},
283  {"Red", 0xFF0000}, {"RosyBrown", 0xBC8F8F}, {"RoyalBlue", 0x4169E1},
284  {"SaddleBrown", 0x8B4513}, {"Salmon", 0xFA8072}, {"SandyBrown", 0xF4A460},
285  {"SeaGreen", 0x2E8B57}, {"SeaShell", 0xFFF5EE}, {"Sienna", 0xA0522D},
286  {"Silver", 0xC0C0C0}, {"SkyBlue", 0x87CEEB}, {"SlateBlue", 0x6A5ACD},
287  {"SlateGray", 0x708090}, {"Snow", 0xFFFAFA}, {"SpringGreen", 0x00FF7F},
288  {"SteelBlue", 0x4682B4}, {"Tan", 0xD2B48C}, {"Teal", 0x008080},
289  {"Thistle", 0xD8BFD8}, {"Tomato", 0xFF6347}, {"Turquoise", 0x40E0D0},
290  {"Violet", 0xEE82EE}, {"Wheat", 0xF5DEB3}, {"White", 0xFFFFFF},
291  {"WhiteSmoke", 0xF5F5F5}, {"Yellow", 0xFFFF00}, {"YellowGreen", 0x9ACD32}
292 }};
293 
294 
295 }
296 
297 #endif // GEMPYRE_GRAPHICS_H
Bitmap for Gempyre Graphics.
Definition: gempyre_bitmap.h:154
Graphics element.
Definition: gempyre_graphics.h:39
CanvasElement(Ui &ui, std::string_view id, const Element &parent)
Constructor to create a new CanvasElement.
CanvasElement(const CanvasElement &other)
Copy constructor.
std::variant< std::string, double, int > Command
Canvas draw command type.
Definition: gempyre_graphics.h:42
CanvasElement & operator=(CanvasElement &&other)
Move operator.
void draw(int x, int y, const Bitmap &bmp)
Draw bitmap at position.
CanvasElement(Ui &ui, const Element &parent)
Constructor to create a new CanvasElement.
std::string add_image(std::string_view url, const std::function< void(std::string_view id)> &loaded=nullptr)
Add an image into HTML DOM tree.
CanvasElement & operator=(const CanvasElement &other)
Copy operator.
void draw(const FrameComposer &frameComposer)
Draw Frame Composer.
CanvasElement(Ui &ui, std::string_view id)
Constructor of canvas id.
void paint_image(std::string_view imageId, int x, int y, const Element::Rect &clippingRect={0, 0, 0, 0}) const
Draw image at position.
void draw(const Bitmap &bmp)
Draw bitmap.
Definition: gempyre_graphics.h:113
std::function< void()> DrawCallback
Function type for draw notifies.
Definition: gempyre_graphics.h:46
void erase(bool resized=false)
erase bitmap
void draw_completed(const DrawCallback &drawCompletedCallback, DrawNotify kick=DrawNotify::NoKick)
Set a callback to be called after the draw.
DrawNotify
set initial draw,
Definition: gempyre_graphics.h:49
~CanvasElement()
Destructor.
void paint_image(std::string_view imageId, const Element::Rect &targetRect, const Element::Rect &clippingRect={0, 0, 0, 0}) const
Draw image in rectangle.
void draw(const CommandList &canvasCommands)
Draw command list - please prefer.
CanvasElement(CanvasElement &&other)
Move constructor.
std::vector< Command > CommandList
List of Canvas draw commands.
Definition: gempyre_graphics.h:44
Represents all HTML elements on UI.
Definition: gempyre.h:84
wrap up Javascript draw commands.
Definition: gempyre_graphics.h:147
FrameComposer restore()
Visit the Mozilla documentation
Definition: gempyre_graphics.h:217
FrameComposer fill_rect(double x, double y, double w, double h)
Visit the Mozilla documentation
Definition: gempyre_graphics.h:168
FrameComposer stroke_rect(double x, double y, double w, double h)
Visit the Mozilla documentation
Definition: gempyre_graphics.h:160
FrameComposer clear_rect(const Gempyre::Element::Rect &r)
Visit the Mozilla documentation
Definition: gempyre_graphics.h:162
FrameComposer translate(double x, double y)
Visit the Mozilla documentation
Definition: gempyre_graphics.h:221
FrameComposer arc_to(double x1, double y1, double x2, double y2, double radius)
Visit the Mozilla documentation
Definition: gempyre_graphics.h:194
FrameComposer fill_rect(const Gempyre::Element::Rect &r)
Visit the Mozilla documentation
Definition: gempyre_graphics.h:166
FrameComposer scale(const double x, double y)
Visit the Mozilla documentation
Definition: gempyre_graphics.h:223
FrameComposer begin_path()
Visit the Mozilla documentation
Definition: gempyre_graphics.h:180
FrameComposer fill_style(std::string_view color)
Visit the Mozilla documentation
Definition: gempyre_graphics.h:205
FrameComposer save()
Visit the Mozilla documentation
Definition: gempyre_graphics.h:215
FrameComposer draw_image(std::string_view id, double x, double y, double w, double h)
Visit the Mozilla documentation
Definition: gempyre_graphics.h:229
FrameComposer line_to(double x, double y)
Visit the Mozilla documentation
Definition: gempyre_graphics.h:184
FrameComposer font(std::string_view style)
Visit the Mozilla documentation
Definition: gempyre_graphics.h:211
FrameComposer bezier_curve_to(double cp1x, double cp1y, double cp2x, double cp2y, double x, double y)
Visit the Mozilla documentation
Definition: gempyre_graphics.h:188
FrameComposer text_align(std::string_view align)
Visit the Mozilla documentation
Definition: gempyre_graphics.h:213
FrameComposer arc(double x, double y, double r, double sAngle, double eAngle)
Visit the Mozilla documentation
Definition: gempyre_graphics.h:174
FrameComposer stroke_style(std::string_view color)
Visit the Mozilla documentation
Definition: gempyre_graphics.h:207
FrameComposer quadratic_curve_to(double cpx, double cpy, double x, double y)
Visit the Mozilla documentation
Definition: gempyre_graphics.h:191
FrameComposer stroke()
Visit the Mozilla documentation
Definition: gempyre_graphics.h:201
FrameComposer line_width(double width)
Visit the Mozilla documentation
Definition: gempyre_graphics.h:209
FrameComposer(const FrameComposer &other)=default
Copy constructor.
FrameComposer draw_image(std::string_view id, double x, double y)
Visit the Mozilla documentation
Definition: gempyre_graphics.h:225
FrameComposer fill()
Visit the Mozilla documentation
Definition: gempyre_graphics.h:203
FrameComposer fill_text(std::string_view text, double x, double y)
Visit the Mozilla documentation
Definition: gempyre_graphics.h:170
FrameComposer clear_rect(double x, double y, double w, double h)
Visit the Mozilla documentation
Definition: gempyre_graphics.h:164
FrameComposer ellipse(double x, double y, double radiusX, double radiusY, double rotation, double startAngle, double endAngle)
Visit the Mozilla documentation
Definition: gempyre_graphics.h:177
FrameComposer draw_image(std::string_view id, double cx, double cy, double cw, double ch, double x, double y, double w, double h)
Visit the Mozilla documentation
Definition: gempyre_graphics.h:233
FrameComposer move_to(double x, double y)
Visit the Mozilla documentation
Definition: gempyre_graphics.h:186
FrameComposer rect(const Gempyre::Element::Rect &r)
Visit the Mozilla documentation
Definition: gempyre_graphics.h:197
FrameComposer close_path()
Visit the Mozilla documentation
Definition: gempyre_graphics.h:182
const Gempyre::CanvasElement::CommandList & composed() const
Get command list composed.
Definition: gempyre_graphics.h:237
FrameComposer draw_image(std::string_view id, const Gempyre::Element::Rect &clip, const Gempyre::Element::Rect &rect)
Visit the Mozilla documentation
Definition: gempyre_graphics.h:231
FrameComposer stroke_text(std::string_view text, double x, double y)
Visit the Mozilla documentation
Definition: gempyre_graphics.h:172
FrameComposer rotate(double angle)
Visit the Mozilla documentation
Definition: gempyre_graphics.h:219
FrameComposer(Gempyre::CanvasElement::CommandList &lst)
Construct from CommandList.
Definition: gempyre_graphics.h:152
FrameComposer()
Constructor.
Definition: gempyre_graphics.h:150
FrameComposer draw_image(std::string_view id, const Gempyre::Element::Rect &rect)
Visit the Mozilla documentation
Definition: gempyre_graphics.h:227
FrameComposer text_baseline(std::string_view textBaseline)
Visit the Mozilla documentation
Definition: gempyre_graphics.h:235
FrameComposer(FrameComposer &&other)=default
Move constructor.
FrameComposer stroke_rect(const Gempyre::Element::Rect &r)
Visit the Mozilla documentation
Definition: gempyre_graphics.h:158
FrameComposer rect(double x, double y, double w, double h)
Visit the Mozilla documentation
Definition: gempyre_graphics.h:199
The application UI.
Definition: gempyre.h:249
constexpr std::array< std::pair< std::string_view, uint32_t >, 147 > html_colors
HTML colors.
Definition: gempyre_graphics.h:244
Rect.
Definition: gempyre_types.h:24
int width
rectangle width.
Definition: gempyre_types.h:30
int x
rectangle x coordinate.
Definition: gempyre_types.h:26
int y
rectangle y coordinate.
Definition: gempyre_types.h:28
int height
rectangle height.
Definition: gempyre_types.h:32