From 06c023ac4412c8e793c856f026fb8ec8d9670357 Mon Sep 17 00:00:00 2001 From: Liu Chao Date: Sun, 26 Apr 2015 14:10:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E8=83=8C=E6=99=AF=E7=BB=98?= =?UTF-8?q?=E5=88=B6=E6=8E=A5=E5=8F=A3=E7=9A=84=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/LCUI/draw/background.h | 2 +- src/draw/background.c | 136 ++++++++++++++++++--------------- 2 files changed, 74 insertions(+), 64 deletions(-) diff --git a/include/LCUI/draw/background.h b/include/LCUI/draw/background.h index 325b4d77..90e76cd4 100644 --- a/include/LCUI/draw/background.h +++ b/include/LCUI/draw/background.h @@ -54,7 +54,7 @@ LCUI_API void Background_Init( LCUI_Background *bg ); */ LCUI_API void Graph_DrawBackground( LCUI_PaintContext paint, - const LCUI_Size *box_size, + const LCUI_Rect *box, LCUI_Background *bg ); diff --git a/src/draw/background.c b/src/draw/background.c index e9c345dd..1f7b52d5 100644 --- a/src/draw/background.c +++ b/src/draw/background.c @@ -61,14 +61,14 @@ void Background_Init( LCUI_Background *bg ) */ void Graph_DrawBackground( LCUI_PaintContext paint, - const LCUI_Size *box_size, + const LCUI_Rect *box, LCUI_Background *bg ) { LCUI_Graph image; LCUI_Size image_size; LCUI_Pos image_pos = { 0, 0 }; - LCUI_Rect read_rect; + LCUI_Rect read_rect, paint_rect; Graph_FillColor( &paint->canvas, bg->color ); /* 计算背景图应有的尺寸 */ @@ -81,16 +81,16 @@ void Graph_DrawBackground( mode = bg->image.width > bg->image.height; case SV_COVER: if( mode ) { - image_size.w = box_size->w; - image_size.h = bg->image.height*box_size->w; + image_size.w = box->w; + image_size.h = bg->image.height*box->w; image_size.h = 1.0*image_size.h / bg->image.width; image_pos.x = 0; - image_pos.y = (box_size->h - image_size.h) / 2; + image_pos.y = (box->h - image_size.h) / 2; } else { - image_size.h = box_size->h; - image_size.w = bg->image.width*box_size->h; + image_size.h = box->h; + image_size.w = bg->image.width*box->h; image_size.w = 1.0*image_size.w / bg->image.height; - image_pos.x = (box_size->w - image_size.w) / 2; + image_pos.x = (box->w - image_size.w) / 2; image_pos.y = 0; } break; @@ -102,7 +102,7 @@ void Graph_DrawBackground( } else { switch( bg->size.w.type ) { case SVT_SCALE: - image_size.w = box_size->w * bg->size.w.scale; + image_size.w = box->w * bg->size.w.scale; break; case SVT_PX: image_size.w = bg->size.w.px; @@ -113,7 +113,7 @@ void Graph_DrawBackground( } switch( bg->size.h.type ) { case SVT_SCALE: - image_size.h = box_size->h * bg->size.h.scale; + image_size.h = box->h * bg->size.h.scale; break; case SVT_PX: image_size.h = bg->size.h.px; @@ -128,37 +128,37 @@ void Graph_DrawBackground( switch( bg->position.value ) { case SV_TOP: case SV_TOP_CENTER: - image_pos.x = (box_size->w - image_size.w) / 2; + image_pos.x = (box->w - image_size.w) / 2; image_pos.y = 0; break; case SV_TOP_RIGHT: - image_pos.x = box_size->w - image_size.w; + image_pos.x = box->w - image_size.w; image_pos.y = 0; break; case SV_CENTER_LEFT: image_pos.x = 0; - image_pos.y = (box_size->h - image_size.h) / 2; + image_pos.y = (box->h - image_size.h) / 2; break; case SV_CENTER: case SV_CENTER_CENTER: - image_pos.x = (box_size->w - image_size.w) / 2; - image_pos.y = (box_size->h - image_size.h) / 2; + image_pos.x = (box->w - image_size.w) / 2; + image_pos.y = (box->h - image_size.h) / 2; break; case SV_CENTER_RIGHT: - image_pos.x = box_size->w - image_size.w; - image_pos.y = (box_size->h - image_size.h) / 2; + image_pos.x = box->w - image_size.w; + image_pos.y = (box->h - image_size.h) / 2; break; case SV_BOTTOM_LEFT: image_pos.x = 0; - image_pos.y = box_size->h - image_size.h; + image_pos.y = box->h - image_size.h; break; case SV_BOTTOM_CENTER: - image_pos.x = (box_size->w - image_size.w) / 2; - image_pos.y = box_size->h - image_size.h; + image_pos.x = (box->w - image_size.w) / 2; + image_pos.y = box->h - image_size.h; break; case SV_BOTTOM_RIGHT: - image_pos.x = box_size->w - image_size.w; - image_pos.y = box_size->h - image_size.h; + image_pos.x = box->w - image_size.w; + image_pos.y = box->h - image_size.h; break; case SV_TOP_LEFT: default: @@ -167,7 +167,7 @@ void Graph_DrawBackground( } else { switch( bg->position.x.type ) { case SVT_SCALE: - image_pos.x = box_size->w - image_size.w; + image_pos.x = box->w - image_size.w; image_pos.x *= bg->position.x.scale; break; case SVT_PX: @@ -177,7 +177,7 @@ void Graph_DrawBackground( } switch( bg->position.y.type ) { case SVT_SCALE: - image_pos.y = box_size->h - image_size.h; + image_pos.y = box->h - image_size.h; image_pos.y *= bg->position.y.scale; break; case SVT_PX: @@ -186,52 +186,62 @@ void Graph_DrawBackground( default:break; } } + /* 获取当前绘制区域与背景内容框的重叠区域 */ + if( !LCUIRect_GetOverlayRect( box, &paint->rect, &paint_rect ) ) { + return; + } + /* 将坐标转换为相对于背景内容框 */ + paint_rect.x -= box->x; + paint_rect.y -= box->y; /* 保存背景图像区域 */ read_rect.x = image_pos.x; read_rect.y = image_pos.y; read_rect.width = image_size.w; read_rect.height = image_size.h; /* 获取当前绘制区域与背景图像的重叠区域 */ - if( LCUIRect_GetOverlayRect( &read_rect, &paint->rect, &read_rect ) ) { - /* 转换成相对于图像的坐标 */ - read_rect.x -= image_pos.x; - read_rect.y -= image_pos.y; - /* 如果尺寸没有变化则直接引用 */ - if( image_size.w == bg->image.w && image_size.h == bg->image.h ) { - Graph_Quote( &image, &bg->image, &read_rect ); - /* 转换成相对于当前绘制区域的坐标 */ - image_pos.x -= paint->rect.x; - image_pos.y -= paint->rect.y; - Graph_Mix( &paint->canvas, &image, image_pos ); - } else { - float scale; - LCUI_Graph buffer; - LCUI_Rect quote_rect; + if( !LCUIRect_GetOverlayRect( &read_rect, &paint_rect, &read_rect ) ) { + return; + } + /* 转换成相对于图像的坐标 */ + read_rect.x -= image_pos.x; + read_rect.y -= image_pos.y; + /* 如果尺寸没有变化则直接引用 */ + if( image_size.w == bg->image.w && image_size.h == bg->image.h ) { + Graph_Quote( &image, &bg->image, &read_rect ); + /* 转换成相对于当前绘制区域的坐标 */ + image_pos.x = image_pos.x + box->x - paint->rect.x; + image_pos.y = image_pos.y + box->y - paint->rect.y; + Graph_Mix( &paint->canvas, &image, image_pos ); + } else { + float scale; + LCUI_Graph buffer; + LCUI_Rect quote_rect; - Graph_Init( &buffer ); - quote_rect = read_rect; - /* 根据宽高的缩放比例,计算实际需要引用的区域 */ - if( image_size.w != bg->image.w ) { - scale = 1.0 * bg->image.width / image_size.w; - quote_rect.x *= scale; - quote_rect.width *= scale; - } - if( image_size.h != bg->image.h ) { - scale = 1.0 * bg->image.height / image_size.h; - quote_rect.y *= scale; - quote_rect.height *= scale; - } - /* 引用源背景图像的一块区域 */ - Graph_Quote( &image, &bg->image, "e_rect ); - image_size.w = read_rect.width; - image_size.h = read_rect.height; - /* 计算相对于绘制区域的坐标 */ - image_pos.x = read_rect.x + image_pos.x - paint->rect.x; - image_pos.y = read_rect.y + image_pos.y - paint->rect.y; - /* 按比例进行缩放 */ - Graph_Zoom( &image, &buffer, FALSE, image_size ); - Graph_Mix( &paint->canvas, &buffer, image_pos ); - Graph_Free( &buffer ); + Graph_Init( &buffer ); + quote_rect = read_rect; + /* 根据宽高的缩放比例,计算实际需要引用的区域 */ + if( image_size.w != bg->image.w ) { + scale = 1.0 * bg->image.width / image_size.w; + quote_rect.x *= scale; + quote_rect.width *= scale; } + if( image_size.h != bg->image.h ) { + scale = 1.0 * bg->image.height / image_size.h; + quote_rect.y *= scale; + quote_rect.height *= scale; + } + /* 引用源背景图像的一块区域 */ + Graph_Quote( &image, &bg->image, "e_rect ); + image_size.w = read_rect.width; + image_size.h = read_rect.height; + /* 计算相对于绘制区域的坐标 */ + image_pos.x = read_rect.x + image_pos.x; + image_pos.x = image_pos.x + box->x - paint->rect.x; + image_pos.y = read_rect.y + image_pos.y; + image_pos.y = image_pos.y + box->y - paint->rect.y; + /* 按比例进行缩放 */ + Graph_Zoom( &image, &buffer, FALSE, image_size ); + Graph_Mix( &paint->canvas, &buffer, image_pos ); + Graph_Free( &buffer ); } }