1 /**
2  * @file lv_demo_vector_graphic.c
3  *
4  */
5 
6 /*********************
7  *      INCLUDES
8  *********************/
9 #include "lv_demo_vector_graphic.h"
10 
11 #if LV_USE_DEMO_VECTOR_GRAPHIC
12 
13 /*********************
14  *      DEFINES
15  *********************/
16 #define WIDTH 640
17 #define HEIGHT 480
18 
19 /**********************
20  *      TYPEDEFS
21  **********************/
22 
23 /**********************
24  *  STATIC PROTOTYPES
25  **********************/
draw_pattern(lv_vector_dsc_t * ctx,lv_vector_path_t * path)26 static void draw_pattern(lv_vector_dsc_t * ctx, lv_vector_path_t * path)
27 {
28     lv_vector_path_clear(path);
29     lv_vector_dsc_identity(ctx);
30 
31     lv_fpoint_t pts[] = {{200, 200}, {300, 200}, {300, 300}, {200, 300}};
32     lv_vector_path_move_to(path, &pts[0]);
33     lv_vector_path_line_to(path, &pts[1]);
34     lv_vector_path_quad_to(path, &pts[2], &pts[3]);
35     lv_vector_path_close(path);
36 
37     lv_draw_image_dsc_t img_dsc;
38     lv_draw_image_dsc_init(&img_dsc);
39 
40     LV_IMAGE_DECLARE(img_demo_vector_avatar);
41     img_dsc.header = img_demo_vector_avatar.header;
42     img_dsc.src = &img_demo_vector_avatar;
43 
44     lv_vector_dsc_set_fill_image(ctx, &img_dsc);
45     lv_vector_dsc_translate(ctx, 250, 250);
46     lv_vector_dsc_rotate(ctx, 25);
47     lv_vector_dsc_translate(ctx, -250, -250);
48     lv_vector_dsc_add_path(ctx, path); // draw a path
49 }
50 
draw_gradient(lv_vector_dsc_t * ctx,lv_vector_path_t * path)51 static void draw_gradient(lv_vector_dsc_t * ctx, lv_vector_path_t * path)
52 {
53     lv_vector_path_clear(path);
54     lv_vector_dsc_identity(ctx);
55 
56     lv_fpoint_t pts[] = {{400, 200}, {600, 200}, {400, 400}};
57     lv_vector_path_move_to(path, &pts[0]);
58     lv_vector_path_quad_to(path, &pts[1], &pts[2]);
59     lv_vector_path_close(path);
60 
61     lv_gradient_stop_t stops[2];
62     lv_memzero(stops, sizeof(stops));
63     stops[0].color = lv_color_hex(0xff0000);
64     stops[0].opa = LV_OPA_COVER;
65     stops[0].frac = 0;
66     stops[1].color = lv_color_hex(0x00ff00);
67     stops[1].opa = LV_OPA_COVER;
68     stops[1].frac = 255;
69 
70     lv_matrix_t mt;
71     lv_matrix_identity(&mt);
72     lv_matrix_rotate(&mt, 30);
73     lv_vector_dsc_set_fill_transform(ctx, &mt);
74 
75     lv_vector_dsc_set_fill_linear_gradient(ctx, 200, 200, 400, 400);
76     lv_vector_dsc_set_fill_gradient_color_stops(ctx, stops, 2);
77     lv_vector_dsc_set_fill_gradient_spread(ctx, LV_VECTOR_GRADIENT_SPREAD_PAD);
78     lv_vector_dsc_add_path(ctx, path); // draw a path
79 }
80 
draw_radial_gradient(lv_vector_dsc_t * ctx,lv_vector_path_t * path)81 static void draw_radial_gradient(lv_vector_dsc_t * ctx, lv_vector_path_t * path)
82 {
83     lv_vector_path_clear(path);
84     lv_vector_dsc_identity(ctx);
85 
86     lv_fpoint_t pts[] = {{400, 50}, {500, 50}, {500, 200}, {400, 200}};
87     lv_vector_path_move_to(path, &pts[0]);
88     lv_vector_path_line_to(path, &pts[1]);
89     lv_vector_path_line_to(path, &pts[2]);
90     lv_vector_path_line_to(path, &pts[3]);
91     lv_vector_path_close(path);
92 
93     lv_gradient_stop_t stops[2];
94     lv_memzero(stops, sizeof(stops));
95     stops[0].color = lv_color_hex(0xff0000);
96     stops[0].opa = LV_OPA_COVER;
97     stops[0].frac = 0;
98     stops[1].color = lv_color_hex(0x0000ff);
99     stops[1].opa = LV_OPA_COVER;
100     stops[1].frac = 255;
101 
102     lv_vector_dsc_set_fill_radial_gradient(ctx, 450, 100, 20);
103     lv_vector_dsc_set_fill_gradient_color_stops(ctx, stops, 2);
104     lv_vector_dsc_set_fill_gradient_spread(ctx, LV_VECTOR_GRADIENT_SPREAD_REFLECT);
105     lv_vector_dsc_add_path(ctx, path); // draw a path
106 }
107 
draw_shapes(lv_vector_dsc_t * ctx,lv_vector_path_t * path)108 static void draw_shapes(lv_vector_dsc_t * ctx, lv_vector_path_t * path)
109 {
110     lv_vector_path_clear(path);
111     lv_vector_dsc_identity(ctx);
112 
113     lv_fpoint_t pts[] = {{50, 50}, {200, 200}, {50, 200}};
114     lv_vector_path_move_to(path, &pts[0]);
115     lv_vector_path_line_to(path, &pts[1]);
116     lv_vector_path_line_to(path, &pts[2]);
117     lv_vector_path_close(path);
118     lv_vector_dsc_set_fill_color(ctx, lv_color_make(0xFF, 0x00, 0x00));
119     lv_vector_dsc_scale(ctx, 0.5, 0.5);
120     lv_vector_dsc_add_path(ctx, path); // draw a path
121 
122     lv_vector_path_clear(path);
123     lv_vector_dsc_identity(ctx);
124 
125     lv_area_t rect = {300, 300, 400, 400};
126     lv_vector_path_append_rect(path, &rect, 50, 60);
127     lv_vector_dsc_set_fill_color(ctx, lv_color_make(0x00, 0x80, 0xff));
128     lv_vector_dsc_skew(ctx, 5, 0);
129     lv_vector_dsc_add_path(ctx, path); // draw a path
130 
131     lv_vector_path_clear(path);
132     lv_vector_dsc_identity(ctx);
133 
134     lv_area_t rect2 = {100, 300, 200, 400};
135     lv_vector_path_append_rect(path, &rect2, 10, 10);
136     lv_vector_dsc_set_fill_color(ctx, lv_color_make(0x80, 0x00, 0x80));
137 
138     lv_vector_path_t * path2 = lv_vector_path_create(LV_VECTOR_PATH_QUALITY_MEDIUM);
139     lv_fpoint_t p = {50, 420};
140     lv_vector_path_append_circle(path2, &p, 50, 30);
141     lv_vector_path_append_path(path, path2);
142 
143     lv_vector_dsc_add_path(ctx, path); // draw a path
144 
145     lv_vector_path_delete(path2);
146 }
147 
draw_lines(lv_vector_dsc_t * ctx,lv_vector_path_t * path)148 static void draw_lines(lv_vector_dsc_t * ctx, lv_vector_path_t * path)
149 {
150     lv_vector_path_clear(path);
151     lv_vector_dsc_identity(ctx);
152 
153     lv_fpoint_t pts[] = {{50, 50}, {200, 200}, {250, 300}, {350, 150}};
154 
155     lv_vector_path_move_to(path, &pts[0]);
156     lv_vector_path_cubic_to(path, &pts[1], &pts[2], &pts[3]);
157 
158     lv_vector_dsc_set_stroke_color(ctx, lv_color_make(0x00, 0xff, 0x00));
159     lv_vector_dsc_set_stroke_opa(ctx, LV_OPA_COVER);
160     lv_vector_dsc_set_fill_opa(ctx, LV_OPA_0);
161     lv_vector_dsc_set_stroke_width(ctx, 8.0f);
162 
163     float dashes[] = {10, 15, 20, 12};
164     lv_vector_dsc_set_stroke_dash(ctx, dashes, 4);
165 
166     lv_vector_dsc_add_path(ctx, path); // draw a path
167 
168     lv_vector_dsc_set_stroke_opa(ctx, LV_OPA_0);
169     lv_vector_dsc_set_fill_opa(ctx, LV_OPA_COVER);
170 }
171 
draw_blend(lv_vector_dsc_t * ctx,lv_vector_path_t * path)172 static void draw_blend(lv_vector_dsc_t * ctx, lv_vector_path_t * path)
173 {
174     lv_vector_path_clear(path);
175     lv_vector_dsc_identity(ctx);
176 
177     lv_fpoint_t pts[] = {{200, 200}, {400, 200}, {450, 350}, {350, 150}};
178 
179     lv_vector_path_move_to(path, &pts[0]);
180     lv_vector_path_cubic_to(path, &pts[1], &pts[2], &pts[3]);
181     lv_vector_path_close(path);
182     lv_vector_dsc_set_fill_color(ctx, lv_color_make(0xFF, 0x00, 0xFF));
183     lv_vector_dsc_set_blend_mode(ctx, LV_VECTOR_BLEND_SCREEN);
184 
185     lv_vector_dsc_add_path(ctx, path); // draw a path
186 }
187 
draw_arc(lv_vector_dsc_t * ctx,lv_vector_path_t * path)188 static void draw_arc(lv_vector_dsc_t * ctx, lv_vector_path_t * path)
189 {
190     lv_vector_path_clear(path);
191     lv_vector_dsc_identity(ctx);
192 
193     lv_area_t rect = {100, 0, 150, 50};
194     lv_vector_dsc_set_fill_color(ctx, lv_color_lighten(lv_color_black(), 50));
195     lv_vector_clear_area(ctx, &rect); // clear screen
196 
197     lv_fpoint_t p = {100, 50}; /* Center */
198     lv_vector_dsc_set_stroke_color(ctx, lv_color_make(0x00, 0xff, 0xff));
199     lv_vector_dsc_set_stroke_opa(ctx, LV_OPA_COVER);
200     lv_vector_dsc_set_stroke_width(ctx, 2.0f);
201     lv_vector_dsc_set_stroke_dash(ctx, NULL, 0);
202     lv_vector_dsc_set_fill_color32(ctx, lv_color32_make(0xFF, 0x00, 0x00, 0x80));
203 
204     lv_vector_path_move_to(path, &p);
205     lv_fpoint_t temp = {p.x + 50, p.y};
206     lv_vector_path_line_to(path, &temp);
207     lv_vector_path_append_arc(path, &p, 50, 0, -90, false);
208     lv_vector_path_line_to(path, &p);
209     lv_vector_path_close(path);
210 
211     lv_vector_dsc_add_path(ctx, path);
212 
213     /* Below code has same effect as above one. */
214     lv_vector_path_clear(path);
215     lv_vector_path_append_arc(path, &p, 50, 45, 45, true);
216     lv_vector_dsc_add_path(ctx, path); // draw a path
217 }
218 
draw_vector(lv_layer_t * layer)219 static void draw_vector(lv_layer_t * layer)
220 {
221     lv_vector_dsc_t * ctx = lv_vector_dsc_create(layer);
222 
223     lv_area_t rect = {0, 100, 300, 300};
224     lv_vector_dsc_set_fill_color(ctx, lv_color_lighten(lv_color_black(), 50));
225     lv_vector_clear_area(ctx, &rect); // clear screen
226 
227     lv_vector_path_t * path = lv_vector_path_create(LV_VECTOR_PATH_QUALITY_MEDIUM);
228 
229     draw_shapes(ctx, path);
230     draw_lines(ctx, path);
231     draw_pattern(ctx, path);
232     draw_radial_gradient(ctx, path);
233     draw_gradient(ctx, path);
234     draw_blend(ctx, path);
235     draw_arc(ctx, path);
236     lv_draw_vector(ctx); // submit draw
237     lv_vector_path_delete(path);
238     lv_vector_dsc_delete(ctx);
239 }
240 
delete_event_cb(lv_event_t * e)241 static void delete_event_cb(lv_event_t * e)
242 {
243     lv_obj_t * obj = lv_event_get_target(e);
244     lv_draw_buf_t * draw_buf = lv_canvas_get_draw_buf(obj);
245     lv_draw_buf_destroy(draw_buf);
246 }
247 
event_cb(lv_event_t * e)248 static void event_cb(lv_event_t * e)
249 {
250     lv_layer_t * layer = lv_event_get_layer(e);
251 
252     draw_vector(layer);
253 }
254 
255 /**********************
256  *  STATIC VARIABLES
257  **********************/
258 
259 /**********************
260  *      MACROS
261  **********************/
262 
263 /**********************
264  *   GLOBAL FUNCTIONS
265  **********************/
266 
lv_demo_vector_graphic_not_buffered(void)267 void lv_demo_vector_graphic_not_buffered(void)
268 {
269     lv_obj_add_event_cb(lv_screen_active(), event_cb, LV_EVENT_DRAW_MAIN, NULL);
270 }
271 
lv_demo_vector_graphic_buffered(void)272 void lv_demo_vector_graphic_buffered(void)
273 {
274     lv_draw_buf_t * draw_buf = lv_draw_buf_create(WIDTH, HEIGHT, LV_COLOR_FORMAT_ARGB8888, LV_STRIDE_AUTO);
275     lv_draw_buf_clear(draw_buf, NULL);
276 
277     lv_obj_t * canvas = lv_canvas_create(lv_screen_active());
278     lv_canvas_set_draw_buf(canvas, draw_buf);
279     lv_obj_add_event_cb(canvas, delete_event_cb, LV_EVENT_DELETE, NULL);
280 
281     lv_layer_t layer;
282     lv_canvas_init_layer(canvas, &layer);
283     draw_vector(&layer);
284 
285     lv_canvas_finish_layer(canvas, &layer);
286 }
287 
288 /**********************
289  *   STATIC FUNCTIONS
290  **********************/
291 #else
292 
lv_demo_vector_graphic_not_buffered(void)293 void lv_demo_vector_graphic_not_buffered(void)
294 {
295     /*fallback for online examples*/
296     lv_obj_t * label = lv_label_create(lv_screen_active());
297     lv_label_set_text(label, "Vector graphics is not enabled");
298     lv_obj_center(label);
299 }
300 
lv_demo_vector_graphic_buffered(void)301 void lv_demo_vector_graphic_buffered(void)
302 {
303     /*fallback for online examples*/
304     lv_obj_t * label = lv_label_create(lv_screen_active());
305     lv_label_set_text(label, "Vector graphics is not enabled");
306     lv_obj_center(label);
307 }
308 
309 #endif
310