From 16e253eed737e65675a247bac7c5d648195b70c7 Mon Sep 17 00:00:00 2001 From: lixianjing Date: Tue, 20 Mar 2018 21:38:43 +0800 Subject: [PATCH] support draw_image_3patch_h/draw_image_3patch_v --- TODO.md | 16 +++++-- demos/demo2_app.c | 75 ++++++++++++++++++++++++++---- src/base/canvas.c | 102 +++++++++++++++++++++++++++++++++++++++++ src/base/canvas.h | 2 + tests/canvas_test.cc | 106 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 290 insertions(+), 11 deletions(-) diff --git a/TODO.md b/TODO.md index 63475447f..395ac2179 100644 --- a/TODO.md +++ b/TODO.md @@ -22,12 +22,21 @@ * api doc * 基本layout功能 * 极速模式 - -## 短期计划(顺序不定) * Qt界面文件转换器 * 微软Rc界面文件转换器 -* API doc到PDF转换工具 * draw 9patch image +* draw 3patch\_h image +* draw 3patch\_v image +* draw scale image + +## 短期计划(顺序不定) +* draw image repeat\_x repeat\_y repeat +* image support draw\_type +* theme support image\_draw\_type +* image support 565/index +* image value +* image button +* API doc到PDF转换工具 * combobox * listbox * tableview @@ -38,6 +47,7 @@ * tab控件 * python binging * jerryscript binging +* remote client * 软键盘 * 中文输入法 * 键盘导航 diff --git a/demos/demo2_app.c b/demos/demo2_app.c index 1c501e105..5dcaec0c7 100644 --- a/demos/demo2_app.c +++ b/demos/demo2_app.c @@ -23,16 +23,13 @@ #include "base/group_box.h" #include "base/image_manager.h" -static ret_t on_paint(void* ctx, event_t* e) { +static ret_t on_paint_9patch(void* ctx, event_t* e) { rect_t s; rect_t r; - color_t fill_color = color_init(0xe0, 0xe0, 0xe0, 0xff); - color_t stroke_color = color_init(0x80, 0x80, 0x80, 0xff); + bitmap_t img; 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); - bitmap_t img; + image_manager_load(default_im(), "btn_blue_n", &img); rect_init(r, 10, 10, 200, 30); @@ -44,6 +41,15 @@ static ret_t on_paint(void* ctx, event_t* e) { rect_init(r, 10, 150, 20, 60); canvas_draw_image_9patch(c, &img, &r); + return RET_OK; +} + +static ret_t on_paint_scale(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; image_manager_load(default_im(), "earth", &img); rect_init(s, 0, 0, img.w, img.h); @@ -58,10 +64,63 @@ static ret_t on_paint(void* ctx, event_t* e) { rect_init(r, 160, 220, img.w * 2, img.h * 2); 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); + canvas_set_stroke_color(c, stroke_color); canvas_draw_line(c, 10, 10, 100, 300); - canvas_translate(c, 100, 100); + return RET_OK; +} + +static ret_t on_paint_3patch_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; + image_manager_load(default_im(), "btn_blue_n", &img); + + rect_init(r, 10, 10, 100, 60); + canvas_draw_image_3patch_h(c, &img, &r); + + rect_init(r, 10, 80, 10, 60); + canvas_draw_image_3patch_h(c, &img, &r); + + rect_init(r, 10, 160, 300, 50); + canvas_draw_image_3patch_h(c, &img, &r); + + rect_init(r, 10, 220, 300, 20); + canvas_draw_image_3patch_h(c, &img, &r); + + return RET_OK; +} + +static ret_t on_paint_3patch_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; + image_manager_load(default_im(), "btn_blue_n", &img); + + rect_init(r, 10, 10, 60, 100); + canvas_draw_image_3patch_h(c, &img, &r); + + rect_init(r, 80, 10, 60, 10); + canvas_draw_image_3patch_h(c, &img, &r); + + rect_init(r, 160, 10, 50, 300); + canvas_draw_image_3patch_h(c, &img, &r); + + rect_init(r, 220, 10, 20, 300); + canvas_draw_image_3patch_h(c, &img, &r); return RET_OK; } @@ -70,7 +129,7 @@ ret_t application_init() { widget_t* win = window_create(NULL, 0, 0, 0, 0); widget_t* canvas = group_box_create(win, 0, 0, win->w, win->h); - widget_on(canvas, EVT_PAINT, on_paint, NULL); + widget_on(canvas, EVT_PAINT, on_paint_9patch, NULL); return RET_OK; } diff --git a/src/base/canvas.c b/src/base/canvas.c index 8849c14bf..9fe10a96e 100644 --- a/src/base/canvas.c +++ b/src/base/canvas.c @@ -497,6 +497,108 @@ ret_t canvas_draw_image(canvas_t* c, bitmap_t* img, rect_t* src, rect_t* dst) { return canvas_do_draw_image(c, img, src, &d); } +ret_t canvas_draw_image_3patch_v(canvas_t* c, bitmap_t* img, rect_t* dst) { + rect_t s; + rect_t d; + wh_t h = 0; + wh_t h_h = 0; + wh_t img_w = 0; + wh_t img_h = 0; + wh_t dst_w = 0; + wh_t dst_h = 0; + + return_value_if_fail(c != NULL && img != NULL && dst != NULL, RET_BAD_PARAMS); + + img_w = img->w; + img_h = img->h; + dst_w = dst->w; + dst_h = dst->h; + + canvas_translate(c, dst->x, dst->y); + + if(dst_h <= img_h) { + rect_init(s, 0, 0, img_w, img_h); + rect_init(d, 0, 0, dst_w, dst_h); + canvas_draw_image(c, img, &s, &d); + canvas_untranslate(c, dst->x, dst->y); + + return RET_OK; + } + + h = ftk_min(img_h, dst_h) / 3; + h_h = dst_h - h * 2; + + /*top*/ + rect_init(s, 0, 0, img_w, h); + rect_init(d, 0, 0, dst_w, h); + canvas_draw_image(c, img, &s, &d); + + /*middle*/ + rect_init(s, 0, h, img_w, img_h - 2 * h); + rect_init(d, 0, h, dst_w, h_h); + canvas_draw_image(c, img, &s, &d); + + /*bottom*/ + rect_init(s, 0, img_h-h, img_w, h); + rect_init(d, 0, dst_h-h, dst_w, h); + canvas_draw_image(c, img, &s, &d); + + canvas_untranslate(c, dst->x, dst->y); + + return RET_OK; +} + +ret_t canvas_draw_image_3patch_h(canvas_t* c, bitmap_t* img, rect_t* dst) { + rect_t s; + rect_t d; + wh_t w = 0; + wh_t w_w = 0; + wh_t img_w = 0; + wh_t img_h = 0; + wh_t dst_w = 0; + wh_t dst_h = 0; + + return_value_if_fail(c != NULL && img != NULL && dst != NULL, RET_BAD_PARAMS); + + img_w = img->w; + img_h = img->h; + dst_w = dst->w; + dst_h = dst->h; + + canvas_translate(c, dst->x, dst->y); + + if(dst_w <= img_w) { + rect_init(s, 0, 0, img_w, img_h); + rect_init(d, 0, 0, dst_w, dst_h); + canvas_draw_image(c, img, &s, &d); + canvas_untranslate(c, dst->x, dst->y); + + return RET_OK; + } + + w = ftk_min(img_w, dst_w) / 3; + w_w = dst_w - w * 2; + + /*left*/ + rect_init(s, 0, 0, w, img_h); + rect_init(d, 0, 0, w, dst_h); + canvas_draw_image(c, img, &s, &d); + + /*center*/ + rect_init(s, w, 0, img_w-2*w, img_h); + rect_init(d, w, 0, w_w, dst_h); + canvas_draw_image(c, img, &s, &d); + + /*right*/ + rect_init(s, img_w-w, 0, w, img_h); + rect_init(d, dst_w-w, 0, w, dst_h); + canvas_draw_image(c, img, &s, &d); + + canvas_untranslate(c, dst->x, dst->y); + + return RET_OK; +} + ret_t canvas_draw_image_9patch(canvas_t* c, bitmap_t* img, rect_t* dst) { rect_t s; rect_t d; diff --git a/src/base/canvas.h b/src/base/canvas.h index dd444e295..27f49c3a5 100644 --- a/src/base/canvas.h +++ b/src/base/canvas.h @@ -76,6 +76,8 @@ ret_t canvas_draw_char(canvas_t* c, wchar_t chr, xy_t x, xy_t y); ret_t canvas_draw_text(canvas_t* c, wchar_t* str, int32_t nr, xy_t x, xy_t y); ret_t canvas_draw_image(canvas_t* c, bitmap_t* img, rect_t* src, rect_t* dst); ret_t canvas_draw_image_at(canvas_t* c, bitmap_t* img, xy_t x, xy_t y); +ret_t canvas_draw_image_3patch_h(canvas_t* c, bitmap_t* img, rect_t* dst); +ret_t canvas_draw_image_3patch_v(canvas_t* c, bitmap_t* img, rect_t* dst); ret_t canvas_draw_image_9patch(canvas_t* c, bitmap_t* img, rect_t* dst); diff --git a/tests/canvas_test.cc b/tests/canvas_test.cc index 232493e9c..6a83da89d 100644 --- a/tests/canvas_test.cc +++ b/tests/canvas_test.cc @@ -342,3 +342,109 @@ TEST(Canvas, draw_image) { font_manager_deinit(&font_manager); lcd_destroy(lcd); } + +TEST(Canvas, draw_image_3patch_h) { + rect_t r; + rect_t d; + canvas_t c; + bitmap_t img; + font_manager_t font_manager; + font_manager_init(&font_manager); + lcd_t* lcd = lcd_log_init(800, 600); + canvas_init(&c, lcd, &font_manager); + + img.w = 32; + img.h = 32; + rect_init(r, 0, 0, 320, 480); + canvas_begin_frame(&c, &r); + + lcd_log_reset(lcd); + rect_init(d, 0, 0, img.w, img.h); + ASSERT_EQ(canvas_draw_image_3patch_h(&c, &img, &d), RET_OK); + ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,32,32,0,0,32,32);"); + + lcd_log_reset(lcd); + rect_init(d, 0, 0, img.w * 3, img.h * 2); + ASSERT_EQ(canvas_draw_image_3patch_h(&c, &img, &d), RET_OK); + ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,10,32,0,0,10,64);dg(10,0,12,32,10,0,76,64);dg(22,0,10,32,86,0,10,64);"); + + lcd_log_reset(lcd); + rect_init(d, 0, 0, img.w * 3, img.h/2); + ASSERT_EQ(canvas_draw_image_3patch_h(&c, &img, &d), RET_OK); + ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,10,32,0,0,10,16);dg(10,0,12,32,10,0,76,16);dg(22,0,10,32,86,0,10,16);"); + + canvas_end_frame(&c); + font_manager_deinit(&font_manager); + lcd_destroy(lcd); +} + +TEST(Canvas, draw_image_3patch_v) { + rect_t r; + rect_t d; + canvas_t c; + bitmap_t img; + font_manager_t font_manager; + font_manager_init(&font_manager); + lcd_t* lcd = lcd_log_init(800, 600); + canvas_init(&c, lcd, &font_manager); + + img.w = 32; + img.h = 32; + rect_init(r, 0, 0, 320, 480); + canvas_begin_frame(&c, &r); + + lcd_log_reset(lcd); + rect_init(d, 0, 0, img.w, img.h); + ASSERT_EQ(canvas_draw_image_3patch_v(&c, &img, &d), RET_OK); + ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,32,32,0,0,32,32);"); + + lcd_log_reset(lcd); + rect_init(d, 0, 0, img.w * 2, img.h * 3); + ASSERT_EQ(canvas_draw_image_3patch_v(&c, &img, &d), RET_OK); + ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,32,10,0,0,64,10);dg(0,10,32,12,0,10,64,76);dg(0,22,32,10,0,86,64,10);"); + + lcd_log_reset(lcd); + rect_init(d, 0, 0, img.w/2, img.h * 3); + ASSERT_EQ(canvas_draw_image_3patch_v(&c, &img, &d), RET_OK); + ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,32,10,0,0,16,10);dg(0,10,32,12,0,10,16,76);dg(0,22,32,10,0,86,16,10);"); + + canvas_end_frame(&c); + font_manager_deinit(&font_manager); + lcd_destroy(lcd); +} + +TEST(Canvas, draw_image_9patch) { + rect_t r; + rect_t d; + canvas_t c; + bitmap_t img; + font_manager_t font_manager; + font_manager_init(&font_manager); + lcd_t* lcd = lcd_log_init(800, 600); + canvas_init(&c, lcd, &font_manager); + + img.w = 32; + img.h = 32; + rect_init(r, 0, 0, 320, 480); + canvas_begin_frame(&c, &r); + + lcd_log_reset(lcd); + rect_init(d, 0, 0, img.w, img.h); + ASSERT_EQ(canvas_draw_image_9patch(&c, &img, &d), RET_OK); + ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,10,10,0,0,10,10);dg(22,0,10,10,22,0,10,10);dg(0,22,10,10,0,22,10,10);dg(22,22,10,10,22,22,10,10);dg(10,0,12,10,10,0,12,10);dg(10,22,12,10,10,22,12,10);dg(0,10,10,12,0,10,10,12);dg(22,10,10,12,22,10,10,12);dg(10,10,12,12,10,10,12,12);"); + + lcd_log_reset(lcd); + rect_init(d, 0, 0, img.w * 2, img.h * 3); + ASSERT_EQ(canvas_draw_image_9patch(&c, &img, &d), RET_OK); + ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,10,10,0,0,10,10);dg(22,0,10,10,54,0,10,10);dg(0,22,10,10,0,86,10,10);dg(22,22,10,10,54,86,10,10);dg(10,0,12,10,10,0,44,10);dg(10,22,12,10,10,86,44,10);dg(0,10,10,12,0,10,10,76);dg(22,10,10,12,54,10,10,76);dg(10,10,12,12,10,10,44,76);"); + + lcd_log_reset(lcd); + rect_init(d, 0, 0, img.w/2, img.h * 3); + ASSERT_EQ(canvas_draw_image_9patch(&c, &img, &d), RET_OK); + ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,5,10,0,0,5,10);dg(27,0,5,10,11,0,5,10);dg(0,22,5,10,0,86,5,10);dg(27,22,5,10,11,86,5,10);dg(5,0,22,10,5,0,6,10);dg(5,22,22,10,5,86,6,10);dg(0,10,5,12,0,10,5,76);dg(27,10,5,12,11,10,5,76);dg(5,10,22,12,5,10,6,76);"); + + canvas_end_frame(&c); + font_manager_deinit(&font_manager); + lcd_destroy(lcd); +} +