gempyre 1.8.1
Loading...
Searching...
No Matches
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#include <functional>
9#include <vector>
10
11#include <gempyre.h>
12#include <gempyre_bitmap.h> // for compatibility, not really needed, fwd declaration is sufficient
13
24#ifdef WINDOWS_EXPORT
25 #ifndef GEMPYRE_EX
26 #define GGEMPYRE_EX __declspec( dllexport )
27 //#else
28 // #define GEMPYRE_EX
29 #endif
30#endif
31
32namespace Gempyre {
33
35class FrameComposer;
36class CanvasData;
37class Bitmap;
38using CanvasDataPtr = std::shared_ptr<CanvasData>;
39
41class GEMPYRE_EX CanvasElement : public Element {
42public:
44 using Command = std::variant<std::string, double, int>;
46 using CommandList = std::vector<Command>;
48 using DrawCallback = std::function<void()>;
49
51 enum class DrawNotify{NoKick, Kick};
52
55
58
61
65 CanvasElement(Ui& ui, std::string_view id);
66
71 CanvasElement(Ui& ui, std::string_view id, const Element& parent);
72
76 CanvasElement(Ui& ui, const Element& parent);
77
80
83
88 std::string add_image(std::string_view url, const std::function<void (std::string_view id)>& loaded = nullptr);
89
96 void paint_image(std::string_view imageId, int x, int y, const Element::Rect& clippingRect = {0, 0, 0, 0}) const;
97
103 void paint_image(std::string_view imageId, const Element::Rect& targetRect, const Element::Rect& clippingRect = {0, 0, 0, 0}) const;
104
107 void draw(const CommandList& canvasCommands);
108
111 void draw(const FrameComposer& frameComposer);
112
115 void draw(const Bitmap& bmp) {draw(0, 0, bmp);}
116
121 void draw(int x, int y, const Bitmap& bmp);
122
134 void draw_completed(const DrawCallback& drawCompletedCallback, DrawNotify kick = DrawNotify::NoKick);
135
138 void erase(bool resized = false);
139private:
140 friend class Bitmap;
141 void paint(const CanvasDataPtr& canvas, int x, int y, bool as_draw);
142private:
143 CanvasDataPtr m_tile{};
144 int m_width{0};
145 int m_height{0};
146};
147
150public:
156 FrameComposer(FrameComposer&& other) = default;
158 FrameComposer(const FrameComposer& other) = default;
160 FrameComposer stroke_rect(const Gempyre::Element::Rect& r) {return push({"strokeRect", r.x, r.y, r.width, r.height});}
162 FrameComposer stroke_rect(double x, double y, double w, double h) {return push({"strokeRect", x, y, w, h});}
164 FrameComposer clear_rect(const Gempyre::Element::Rect& r) {return push({"clearRect", r.x, r.y, r.width, r.height});}
166 FrameComposer clear_rect(double x, double y, double w, double h) {return push({"clearRect", x, y, w, h});}
168 FrameComposer fill_rect(const Gempyre::Element::Rect& r) {return push({"fillRect", r.x, r.y, r.width, r.height});}
170 FrameComposer fill_rect(double x, double y, double w, double h) {return push({"fillRect", x, y, w, h});}
172 FrameComposer fill_text(std::string_view text, double x, double y) {return push({"fillText", std::string{text}, x, y});}
174 FrameComposer stroke_text(std::string_view text, double x, double y) {return push({"strokeText", std::string{text}, x, y});}
176 FrameComposer arc(double x, double y, double r, double sAngle, double eAngle) {
177 return push({"arc", x, y, r, sAngle, eAngle});}
179 FrameComposer ellipse(double x, double y, double radiusX, double radiusY, double rotation, double startAngle, double endAngle) {
180 return push({"ellipse", x, y, radiusX, radiusY, rotation, startAngle, endAngle});}
182 FrameComposer begin_path() {return push({"beginPath"});}
184 FrameComposer close_path() {return push({"closePath"});}
186 FrameComposer line_to(double x, double y) {return push({"lineTo", x, y});}
188 FrameComposer move_to(double x, double y) {return push({"moveTo", x, y});}
190 FrameComposer bezier_curve_to(double cp1x, double cp1y, double cp2x, double cp2y, double x, double y) {
191 return push({"bezierCurveTo", cp1x, cp1y, cp2x, cp2y, x, y});}
193 FrameComposer quadratic_curve_to(double cpx, double cpy, double x, double y) {
194 return push({"quadraticCurveTo", cpx, cpy, x, y});}
196 FrameComposer arc_to(double x1, double y1, double x2, double y2, double radius) {
197 return push({"arcTo", x1, y1, x2, y2, radius});}
199 FrameComposer rect(const Gempyre::Element::Rect& r) {return push({"rect", r.x, r.y, r.width, r.height});}
201 FrameComposer rect(double x, double y, double w, double h) {return push({"rect", x, y, w, h});}
203 FrameComposer stroke() {return push({"stroke"});}
205 FrameComposer fill() {return push({"fill"});}
207 FrameComposer fill_style(std::string_view color) {return push({"fillStyle", std::string{color}});}
209 FrameComposer stroke_style(std::string_view color) {return push({"strokeStyle", std::string{color}});}
211 FrameComposer line_width(double width) {return push({"lineWidth", width});}
213 FrameComposer font(std::string_view style) {return push({"font", std::string{style}});}
215 FrameComposer text_align(std::string_view align) {return push({"textAlign", std::string{align}});}
217 FrameComposer save() {return push({"save"});}
219 FrameComposer restore() {return push({"restore"});}
221 FrameComposer rotate(double angle) {return push({"rotate", angle});}
223 FrameComposer translate(double x, double y) {return push({"translate", x, y});}
225 FrameComposer scale(const double x, double y) {return push({"scale", x, y});}
227 FrameComposer draw_image(std::string_view id, double x, double y) {return push({"drawImage", std::string{id}, x, y});}
229 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});}
231 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});}
233 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});}
235 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});}
237 FrameComposer text_baseline(std::string_view textBaseline) {return push({"textBaseline", std::string{textBaseline}});}
239 [[nodiscard]] const Gempyre::CanvasElement::CommandList& composed() const {return m_composition;}
240private:
241 FrameComposer push(const std::initializer_list<Gempyre::CanvasElement::Command>& list) {m_composition.insert(m_composition.end(), list); return *this;}
243};
244
245
246}
247
248#endif // GEMPYRE_GRAPHICS_H
Bitmap for Gempyre Graphics.
Definition gempyre_bitmap.h:242
Graphics element.
Definition gempyre_graphics.h:41
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:44
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.
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:115
std::function< void()> DrawCallback
Function type for draw notifies.
Definition gempyre_graphics.h:48
CanvasElement & operator=(const CanvasElement &other)
Copy operator.
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:51
~CanvasElement()
Destructor.
CanvasElement & operator=(CanvasElement &&other)
Move operator.
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:46
Represents all HTML elements on UI.
Definition gempyre.h:84
wrap up Javascript draw commands.
Definition gempyre_graphics.h:149
FrameComposer restore()
Visit the Mozilla documentation
Definition gempyre_graphics.h:219
FrameComposer fill_rect(double x, double y, double w, double h)
Visit the Mozilla documentation
Definition gempyre_graphics.h:170
FrameComposer stroke_rect(double x, double y, double w, double h)
Visit the Mozilla documentation
Definition gempyre_graphics.h:162
FrameComposer clear_rect(const Gempyre::Element::Rect &r)
Visit the Mozilla documentation
Definition gempyre_graphics.h:164
FrameComposer translate(double x, double y)
Visit the Mozilla documentation
Definition gempyre_graphics.h:223
FrameComposer arc_to(double x1, double y1, double x2, double y2, double radius)
Visit the Mozilla documentation
Definition gempyre_graphics.h:196
FrameComposer fill_rect(const Gempyre::Element::Rect &r)
Visit the Mozilla documentation
Definition gempyre_graphics.h:168
FrameComposer scale(const double x, double y)
Visit the Mozilla documentation
Definition gempyre_graphics.h:225
FrameComposer begin_path()
Visit the Mozilla documentation
Definition gempyre_graphics.h:182
FrameComposer fill_style(std::string_view color)
Visit the Mozilla documentation
Definition gempyre_graphics.h:207
FrameComposer save()
Visit the Mozilla documentation
Definition gempyre_graphics.h:217
FrameComposer draw_image(std::string_view id, double x, double y, double w, double h)
Visit the Mozilla documentation
Definition gempyre_graphics.h:231
FrameComposer line_to(double x, double y)
Visit the Mozilla documentation
Definition gempyre_graphics.h:186
FrameComposer font(std::string_view style)
Visit the Mozilla documentation
Definition gempyre_graphics.h:213
FrameComposer bezier_curve_to(double cp1x, double cp1y, double cp2x, double cp2y, double x, double y)
Visit the Mozilla documentation
Definition gempyre_graphics.h:190
FrameComposer text_align(std::string_view align)
Visit the Mozilla documentation
Definition gempyre_graphics.h:215
FrameComposer arc(double x, double y, double r, double sAngle, double eAngle)
Visit the Mozilla documentation
Definition gempyre_graphics.h:176
FrameComposer stroke_style(std::string_view color)
Visit the Mozilla documentation
Definition gempyre_graphics.h:209
FrameComposer quadratic_curve_to(double cpx, double cpy, double x, double y)
Visit the Mozilla documentation
Definition gempyre_graphics.h:193
FrameComposer stroke()
Visit the Mozilla documentation
Definition gempyre_graphics.h:203
FrameComposer line_width(double width)
Visit the Mozilla documentation
Definition gempyre_graphics.h:211
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:227
FrameComposer fill()
Visit the Mozilla documentation
Definition gempyre_graphics.h:205
FrameComposer fill_text(std::string_view text, double x, double y)
Visit the Mozilla documentation
Definition gempyre_graphics.h:172
FrameComposer clear_rect(double x, double y, double w, double h)
Visit the Mozilla documentation
Definition gempyre_graphics.h:166
FrameComposer ellipse(double x, double y, double radiusX, double radiusY, double rotation, double startAngle, double endAngle)
Visit the Mozilla documentation
Definition gempyre_graphics.h:179
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:235
FrameComposer move_to(double x, double y)
Visit the Mozilla documentation
Definition gempyre_graphics.h:188
FrameComposer rect(const Gempyre::Element::Rect &r)
Visit the Mozilla documentation
Definition gempyre_graphics.h:199
FrameComposer close_path()
Visit the Mozilla documentation
Definition gempyre_graphics.h:184
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:233
FrameComposer stroke_text(std::string_view text, double x, double y)
Visit the Mozilla documentation
Definition gempyre_graphics.h:174
const Gempyre::CanvasElement::CommandList & composed() const
Get command list composed.
Definition gempyre_graphics.h:239
FrameComposer rotate(double angle)
Visit the Mozilla documentation
Definition gempyre_graphics.h:221
FrameComposer(Gempyre::CanvasElement::CommandList &lst)
Construct from CommandList.
Definition gempyre_graphics.h:154
FrameComposer()
Constructor.
Definition gempyre_graphics.h:152
FrameComposer draw_image(std::string_view id, const Gempyre::Element::Rect &rect)
Visit the Mozilla documentation
Definition gempyre_graphics.h:229
FrameComposer text_baseline(std::string_view textBaseline)
Visit the Mozilla documentation
Definition gempyre_graphics.h:237
FrameComposer(FrameComposer &&other)=default
Move constructor.
FrameComposer stroke_rect(const Gempyre::Element::Rect &r)
Visit the Mozilla documentation
Definition gempyre_graphics.h:160
FrameComposer rect(double x, double y, double w, double h)
Visit the Mozilla documentation
Definition gempyre_graphics.h:201
The application UI.
Definition gempyre.h:260
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