awtk/demos/demo_vg_app.c

400 lines
10 KiB
C
Raw Normal View History

2018-03-19 07:19:12 +08:00
/**
* File: demo2_app.c
2018-05-15 09:31:58 +08:00
* Author: AWTK Develop Team
2018-03-19 07:19:12 +08:00
* Brief: demo paint
*
2019-01-07 10:58:36 +08:00
* Copyright (c) 2018 - 2019 Guangzhou ZHIYUAN Electronics Co.,Ltd.
2018-03-19 07:19:12 +08:00
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* License file for more details.
*
*/
/**
* History:
* ================================================================
* 2018-03-18 Li XianJing <xianjimli@hotmail.com> created
*
*/
2018-10-13 18:40:00 +08:00
#include "base/timer.h"
#include "widgets/window.h"
2018-03-19 12:03:19 +08:00
#include "base/image_manager.h"
2018-12-01 11:41:23 +08:00
#include "canvas_widget/canvas_widget.h"
2018-03-19 07:19:12 +08:00
static ret_t on_paint_rect(void* ctx, event_t* e) {
paint_event_t* evt = (paint_event_t*)e;
canvas_t* c = evt->c;
canvas_set_fill_color(c, color_init(0, 0, 0xff, 0));
canvas_fill_rect(c, 10, 10, 100, 100);
return RET_OK;
}
2018-08-12 17:26:37 +08:00
static ret_t on_paint_patch9(void* ctx, event_t* e) {
2018-03-19 07:19:12 +08:00
rect_t r;
bitmap_t img;
2018-03-19 07:19:12 +08:00
paint_event_t* evt = (paint_event_t*)e;
canvas_t* c = evt->c;
2019-01-03 11:37:28 +08:00
image_manager_get_bitmap(image_manager(), "btn_blue_n", &img);
2018-03-19 12:03:19 +08:00
2018-06-22 12:01:36 +08:00
r = rect_init(10, 10, 200, 30);
2018-08-12 17:26:37 +08:00
canvas_draw_image_patch9(c, &img, &r);
2018-03-19 17:17:14 +08:00
2018-06-22 12:01:36 +08:00
r = rect_init(10, 70, 200, 60);
2018-08-12 17:26:37 +08:00
canvas_draw_image_patch9(c, &img, &r);
2018-03-19 12:03:19 +08:00
2018-06-22 12:01:36 +08:00
r = rect_init(10, 150, 20, 60);
2018-08-12 17:26:37 +08:00
canvas_draw_image_patch9(c, &img, &r);
2018-03-19 12:03:19 +08:00
return RET_OK;
}
static ret_t on_paint_default(void* ctx, event_t* e) {
rect_t s;
rect_t r;
bitmap_t img;
paint_event_t* evt = (paint_event_t*)e;
canvas_t* c = evt->c;
2019-01-03 11:37:28 +08:00
image_manager_get_bitmap(image_manager(), "earth", &img);
2018-03-19 17:17:14 +08:00
2018-06-22 12:01:36 +08:00
s = rect_init(0, 0, img.w, img.h);
r = rect_init(10, 220, img.w / 2, img.h / 2);
2018-03-19 17:17:14 +08:00
canvas_draw_image(c, &img, &s, &r);
2018-06-22 12:01:36 +08:00
s = rect_init(0, 0, img.w, img.h);
r = rect_init(60, 220, img.w, img.h);
2018-03-19 17:17:14 +08:00
canvas_draw_image(c, &img, &s, &r);
2018-06-22 12:01:36 +08:00
s = rect_init(0, 0, img.w, img.h);
r = rect_init(160, 220, img.w * 2, img.h * 2);
2018-03-19 17:17:14 +08:00
canvas_draw_image(c, &img, &s, &r);
return RET_OK;
}
static ret_t on_paint_line(void* ctx, event_t* e) {
color_t fill_color = color_init(0xe0, 0xe0, 0xe0, 0xff);
color_t stroke_color = color_init(0x80, 0x80, 0x80, 0xff);
paint_event_t* evt = (paint_event_t*)e;
canvas_t* c = evt->c;
canvas_set_fill_color(c, fill_color);
canvas_set_stroke_color(c, stroke_color);
2018-03-19 07:19:12 +08:00
canvas_set_stroke_color(c, stroke_color);
canvas_draw_line(c, 10, 10, 100, 300);
return RET_OK;
}
2018-04-18 16:58:51 +08:00
static ret_t on_paint_points(void* ctx, event_t* e) {
2018-04-20 21:49:47 +08:00
point_t points[] = {{100, 100}, {100, 101}, {100, 102}, {100, 103}};
2018-04-18 16:58:51 +08:00
color_t fill_color = color_init(0xe0, 0xe0, 0xe0, 0xff);
color_t stroke_color = color_init(0x80, 0x80, 0x80, 0xff);
paint_event_t* evt = (paint_event_t*)e;
canvas_t* c = evt->c;
canvas_set_fill_color(c, fill_color);
canvas_set_stroke_color(c, stroke_color);
canvas_draw_points(c, points, ARRAY_SIZE(points));
return RET_OK;
}
2018-08-12 17:26:37 +08:00
static ret_t on_paint_patch3_h(void* ctx, event_t* e) {
rect_t r;
bitmap_t img;
paint_event_t* evt = (paint_event_t*)e;
canvas_t* c = evt->c;
2019-01-03 11:37:28 +08:00
image_manager_get_bitmap(image_manager(), "btn_blue_n", &img);
2018-06-22 12:01:36 +08:00
r = rect_init(10, 10, 100, 60);
2018-08-12 17:26:37 +08:00
canvas_draw_image_patch3_x(c, &img, &r);
2018-06-22 12:01:36 +08:00
r = rect_init(10, 80, 10, 60);
2018-08-12 17:26:37 +08:00
canvas_draw_image_patch3_x(c, &img, &r);
2018-06-22 12:01:36 +08:00
r = rect_init(10, 160, 300, 50);
2018-08-12 17:26:37 +08:00
canvas_draw_image_patch3_x(c, &img, &r);
2018-06-22 12:01:36 +08:00
r = rect_init(10, 220, 300, 20);
2018-08-12 17:26:37 +08:00
canvas_draw_image_patch3_x(c, &img, &r);
return RET_OK;
}
2018-08-12 17:26:37 +08:00
static ret_t on_paint_patch3_v(void* ctx, event_t* e) {
rect_t r;
bitmap_t img;
paint_event_t* evt = (paint_event_t*)e;
canvas_t* c = evt->c;
2019-01-03 11:37:28 +08:00
image_manager_get_bitmap(image_manager(), "btn_blue_n", &img);
2018-06-22 12:01:36 +08:00
r = rect_init(10, 10, 60, 100);
2018-08-12 17:26:37 +08:00
canvas_draw_image_patch3_x(c, &img, &r);
2018-06-22 12:01:36 +08:00
r = rect_init(80, 10, 60, 10);
2018-08-12 17:26:37 +08:00
canvas_draw_image_patch3_x(c, &img, &r);
2018-06-22 12:01:36 +08:00
r = rect_init(160, 10, 50, 300);
2018-08-12 17:26:37 +08:00
canvas_draw_image_patch3_x(c, &img, &r);
2018-06-22 12:01:36 +08:00
r = rect_init(220, 10, 20, 300);
2018-08-12 17:26:37 +08:00
canvas_draw_image_patch3_x(c, &img, &r);
2018-03-19 17:17:14 +08:00
2018-03-19 07:19:12 +08:00
return RET_OK;
}
2018-03-31 13:48:18 +08:00
static ret_t on_paint_repeat_x(void* ctx, event_t* e) {
2018-03-21 21:34:54 +08:00
rect_t r;
bitmap_t img;
paint_event_t* evt = (paint_event_t*)e;
canvas_t* c = evt->c;
2019-01-03 11:37:28 +08:00
image_manager_get_bitmap(image_manager(), "btn_blue_n", &img);
2018-03-21 21:34:54 +08:00
2018-06-22 12:01:36 +08:00
r = rect_init(10, 10, 10, img.h);
2018-03-31 13:48:18 +08:00
canvas_draw_image_repeat_x(c, &img, &r);
2018-06-22 12:01:36 +08:00
r = rect_init(10, 60, img.w, img.h);
2018-03-31 13:48:18 +08:00
canvas_draw_image_repeat_x(c, &img, &r);
2018-06-22 12:01:36 +08:00
r = rect_init(10, 160, img.w * 2, img.h * 2);
2018-03-31 13:48:18 +08:00
canvas_draw_image_repeat_x(c, &img, &r);
2018-03-21 21:34:54 +08:00
return RET_OK;
}
2018-03-31 13:48:18 +08:00
static ret_t on_paint_repeat_y(void* ctx, event_t* e) {
2018-03-21 21:34:54 +08:00
rect_t r;
bitmap_t img;
paint_event_t* evt = (paint_event_t*)e;
canvas_t* c = evt->c;
2019-01-03 11:37:28 +08:00
image_manager_get_bitmap(image_manager(), "btn_blue_n", &img);
2018-03-21 21:34:54 +08:00
2018-06-22 12:01:36 +08:00
r = rect_init(10, 10, img.w, 10);
2018-03-31 13:48:18 +08:00
canvas_draw_image_repeat_y(c, &img, &r);
2018-03-21 21:34:54 +08:00
2018-06-22 12:01:36 +08:00
r = rect_init(90, 10, img.w, img.h);
2018-03-31 13:48:18 +08:00
canvas_draw_image_repeat_y(c, &img, &r);
2018-06-22 12:01:36 +08:00
r = rect_init(160, 10, img.w * 2, img.h * 2);
2018-03-31 13:48:18 +08:00
canvas_draw_image_repeat_y(c, &img, &r);
2018-03-21 21:34:54 +08:00
return RET_OK;
}
2018-03-31 13:48:18 +08:00
static ret_t on_paint_repeat(void* ctx, event_t* e) {
2018-03-21 21:34:54 +08:00
rect_t r;
bitmap_t img;
paint_event_t* evt = (paint_event_t*)e;
canvas_t* c = evt->c;
2019-01-03 11:37:28 +08:00
image_manager_get_bitmap(image_manager(), "btn_blue_n", &img);
2018-03-21 21:34:54 +08:00
2018-06-22 12:01:36 +08:00
r = rect_init(10, 10, img.w / 2, img.h / 2);
2018-03-31 13:48:18 +08:00
canvas_draw_image_repeat(c, &img, &r);
2018-03-21 21:34:54 +08:00
2018-06-22 12:01:36 +08:00
r = rect_init(50, 50, img.w, img.h);
2018-03-31 13:48:18 +08:00
canvas_draw_image_repeat(c, &img, &r);
2018-06-22 12:01:36 +08:00
r = rect_init(100, 100, img.w * 2 + 10, img.h * 2 + 10);
2018-03-31 13:48:18 +08:00
canvas_draw_image_repeat(c, &img, &r);
2018-03-21 21:34:54 +08:00
return RET_OK;
}
static ret_t on_paint_scale_x(void* ctx, event_t* e) {
rect_t r;
bitmap_t img;
paint_event_t* evt = (paint_event_t*)e;
canvas_t* c = evt->c;
2019-01-03 11:37:28 +08:00
image_manager_get_bitmap(image_manager(), "btn_blue_n", &img);
2018-06-22 12:01:36 +08:00
r = rect_init(10, 10, img.w, img.h / 2);
2018-03-31 13:48:18 +08:00
canvas_draw_image_scale_w(c, &img, &r);
2018-06-22 12:01:36 +08:00
r = rect_init(50, 50, img.w, img.h);
2018-03-31 13:48:18 +08:00
canvas_draw_image_scale_w(c, &img, &r);
2018-06-22 12:01:36 +08:00
r = rect_init(100, 100, img.w * 2, img.h);
2018-03-31 13:48:18 +08:00
canvas_draw_image_scale_w(c, &img, &r);
return RET_OK;
}
static ret_t on_paint_scale_y(void* ctx, event_t* e) {
rect_t r;
bitmap_t img;
paint_event_t* evt = (paint_event_t*)e;
canvas_t* c = evt->c;
2019-01-03 11:37:28 +08:00
image_manager_get_bitmap(image_manager(), "btn_blue_n", &img);
2018-06-22 12:01:36 +08:00
r = rect_init(10, 10, img.w / 2, img.h);
2018-03-31 13:48:18 +08:00
canvas_draw_image_scale_h(c, &img, &r);
2018-06-22 12:01:36 +08:00
r = rect_init(50, 50, img.w, img.h);
2018-03-31 13:48:18 +08:00
canvas_draw_image_scale_h(c, &img, &r);
2018-06-22 12:01:36 +08:00
r = rect_init(100, 100, img.w, img.h * 2);
2018-03-31 13:48:18 +08:00
canvas_draw_image_scale_h(c, &img, &r);
return RET_OK;
}
static ret_t on_paint_scale(void* ctx, event_t* e) {
rect_t r;
bitmap_t img;
paint_event_t* evt = (paint_event_t*)e;
canvas_t* c = evt->c;
2019-01-03 11:37:28 +08:00
image_manager_get_bitmap(image_manager(), "btn_blue_n", &img);
2018-06-22 12:01:36 +08:00
r = rect_init(10, 10, img.w / 2, img.h);
canvas_draw_image_scale(c, &img, &r);
2018-06-22 12:01:36 +08:00
r = rect_init(50, 50, img.w, img.h);
canvas_draw_image_scale(c, &img, &r);
2018-06-22 12:01:36 +08:00
r = rect_init(100, 100, img.w, img.h * 2);
canvas_draw_image_scale(c, &img, &r);
return RET_OK;
}
static ret_t on_paint_center(void* ctx, event_t* e) {
rect_t r;
bitmap_t img;
paint_event_t* evt = (paint_event_t*)e;
canvas_t* c = evt->c;
2019-01-03 11:37:28 +08:00
image_manager_get_bitmap(image_manager(), "btn_blue_n", &img);
2018-06-22 12:01:36 +08:00
r = rect_init(10, 10, img.w / 2, img.h);
canvas_draw_image_center(c, &img, &r);
2018-06-22 12:01:36 +08:00
r = rect_init(50, 50, img.w, img.h);
canvas_draw_image_center(c, &img, &r);
2018-06-22 12:01:36 +08:00
r = rect_init(100, 100, img.w, img.h * 2);
canvas_draw_image_center(c, &img, &r);
return RET_OK;
}
2018-12-07 12:42:54 +08:00
#include "vg_common.inc"
2018-04-25 17:38:51 +08:00
2018-03-24 21:37:40 +08:00
static ret_t on_paint_vg(void* ctx, event_t* e) {
paint_event_t* evt = (paint_event_t*)e;
canvas_t* c = evt->c;
2018-09-27 14:21:45 +08:00
vgcanvas_t* vg = canvas_get_vgcanvas(c);
2018-03-24 21:37:40 +08:00
2018-10-13 18:40:00 +08:00
vgcanvas_save(vg);
2018-04-24 17:38:22 +08:00
vgcanvas_set_line_width(vg, 1);
2018-03-25 20:13:13 +08:00
vgcanvas_set_stroke_color(vg, color_init(0, 0xff, 0, 0xff));
2018-04-24 17:38:22 +08:00
vgcanvas_set_fill_color(vg, color_init(0xff, 0, 0, 0xff));
2018-04-29 16:51:54 +08:00
2018-04-24 17:38:22 +08:00
draw_basic_shapes(vg, FALSE);
vgcanvas_translate(vg, 0, 50);
draw_basic_shapes(vg, TRUE);
vgcanvas_translate(vg, 0, 50);
2018-04-25 15:14:30 +08:00
stroke_lines(vg);
vgcanvas_translate(vg, 0, 50);
draw_image(vg);
2018-04-25 17:38:51 +08:00
vgcanvas_translate(vg, 50, 100);
draw_matrix(vg);
2018-04-26 15:01:19 +08:00
vgcanvas_translate(vg, 0, 100);
draw_text(vg);
2018-10-13 18:40:00 +08:00
vgcanvas_restore(vg);
2018-04-25 17:38:51 +08:00
2018-03-24 21:37:40 +08:00
return RET_OK;
}
2018-09-19 14:13:57 +08:00
static ret_t on_paint_vg_simple(void* ctx, event_t* e) {
bitmap_t img;
paint_event_t* evt = (paint_event_t*)e;
canvas_t* c = evt->c;
2018-09-27 14:21:45 +08:00
vgcanvas_t* vg = canvas_get_vgcanvas(c);
2019-01-03 11:37:28 +08:00
image_manager_get_bitmap(image_manager(), "rgba", &img);
2018-09-19 14:13:57 +08:00
canvas_set_fill_color(c, color_init(0x11, 0x22, 0x33, 0xff));
canvas_fill_rect(c, 0, 0, 10, 10);
rect_t s = rect_init(0, 0, img.w, img.h);
rect_t r = rect_init(10, 10, img.w, img.h);
canvas_draw_image(c, &img, &s, &r);
2018-10-13 18:40:00 +08:00
vgcanvas_save(vg);
2018-09-19 14:13:57 +08:00
vgcanvas_translate(vg, 0, 100);
vgcanvas_set_line_width(vg, 1);
vgcanvas_set_fill_color(vg, color_init(0xff, 0, 0, 0xff));
vgcanvas_rect(vg, 5, 5, 100, 100);
vgcanvas_fill(vg);
vgcanvas_set_fill_color(vg, color_init(0, 0xff, 0, 0xff));
vgcanvas_rect(vg, 110, 5, 100, 100);
vgcanvas_fill(vg);
vgcanvas_set_fill_color(vg, color_init(0, 0, 0xff, 0xff));
vgcanvas_rect(vg, 215, 5, 100, 100);
vgcanvas_fill(vg);
vgcanvas_translate(vg, 0, 105);
vgcanvas_set_stroke_color(vg, color_init(0xff, 0, 0, 0xff));
vgcanvas_rect(vg, 5, 5, 100, 100);
vgcanvas_stroke(vg);
vgcanvas_set_stroke_color(vg, color_init(0, 0xff, 0, 0xff));
vgcanvas_rect(vg, 110, 5, 100, 100);
vgcanvas_stroke(vg);
2018-09-26 15:40:55 +08:00
2018-09-19 14:13:57 +08:00
vgcanvas_set_stroke_color(vg, color_init(0, 0, 0xff, 0xff));
vgcanvas_rect(vg, 215, 5, 100, 100);
vgcanvas_stroke(vg);
2018-09-26 15:40:55 +08:00
2018-09-27 13:56:00 +08:00
vgcanvas_translate(vg, 0, 128);
2019-01-03 11:37:28 +08:00
image_manager_get_bitmap(image_manager(), "rgb", &img);
2018-09-19 14:13:57 +08:00
vgcanvas_draw_image(vg, &img, 0, 0, img.w, img.h, 0, 0, img.w, img.h);
2019-01-03 11:37:28 +08:00
image_manager_get_bitmap(image_manager(), "rgba", &img);
2018-09-19 14:13:57 +08:00
vgcanvas_draw_image(vg, &img, 0, 0, img.w, img.h, 100, 0, img.w, img.h);
2018-09-27 14:21:45 +08:00
2019-01-03 11:37:28 +08:00
image_manager_get_bitmap(image_manager(), "switch", &img);
2018-09-27 13:56:00 +08:00
vgcanvas_draw_image(vg, &img, 0, 0, img.w, img.h, 200, 0, img.w, img.h);
2018-09-27 14:21:45 +08:00
2018-09-27 13:56:00 +08:00
vgcanvas_translate(vg, 0, 60);
vgcanvas_rounded_rect(vg, 0, 0, img.w, img.h, 5);
vgcanvas_paint(vg, FALSE, &img);
2018-09-19 14:13:57 +08:00
2018-09-27 14:21:45 +08:00
vg = canvas_get_vgcanvas(c);
vgcanvas_translate(vg, 160, 0);
2018-09-27 13:56:00 +08:00
vgcanvas_rounded_rect(vg, 0, 0, img.w, img.h, 5);
2018-09-26 15:40:55 +08:00
vgcanvas_paint(vg, FALSE, &img);
vgcanvas_restore(vg);
return RET_OK;
}
2018-10-13 18:40:00 +08:00
static ret_t on_timer(const timer_info_t* timer) {
widget_t* widget = WIDGET(timer->ctx);
widget_invalidate(widget, NULL);
return RET_REPEAT;
}
2018-03-19 07:19:12 +08:00
ret_t application_init() {
widget_t* win = window_create(NULL, 0, 0, 0, 0);
2018-12-01 11:41:23 +08:00
widget_t* canvas = canvas_widget_create(win, 0, 0, win->w, win->h);
2018-03-19 07:19:12 +08:00
2018-10-16 09:50:42 +08:00
// widget_on(canvas, EVT_PAINT, on_paint_vg_simple, NULL);
2018-10-13 18:40:00 +08:00
widget_on(canvas, EVT_PAINT, on_paint_vg, NULL);
timer_add(on_timer, win, 500);
2018-03-19 07:19:12 +08:00
return RET_OK;
}