/* 将矩形数据追加至队列 */ static int RectQueue_Add( LCUI_Queue* queue, LCUI_Rect rect ) { int i, flag = 0; LCUI_Rect *rect_ptr, *cur_rect_ptr; LCUI_Queue rect_buff; if( rect.width <= 0 || rect.height <= 0 ) { return -1; } DEBUG_MSG("add new rect: %d,%d,%d,%d, current total: %d\n", rect.x, rect.y, rect.width, rect.height, queue->total_num); Queue_Init( &rect_buff, sizeof(LCUI_Rect), NULL ); for (i=0; i<queue->total_num; ++i) { cur_rect_ptr = (LCUI_Rect*)Queue_Get( queue, i ); if( !cur_rect_ptr ) { break; } DEBUG_MSG("[%d] rect: %d,%d,%d,%d\n", i, cur_rect_ptr->x, cur_rect_ptr->y, cur_rect_ptr->width, cur_rect_ptr->height); /* 如果矩形无效,或者被新增的矩形区域包含,则删除 */ if ( cur_rect_ptr->width <= 0 || cur_rect_ptr->height <= 0 || LCUIRect_IncludeRect( rect, *cur_rect_ptr) ) { Queue_Delete ( queue, i ); continue; } /* 如果与现有的矩形相同,或被现有的矩形包含 */ if( LCUIRect_Equal( rect, *cur_rect_ptr) || LCUIRect_IncludeRect( *cur_rect_ptr, rect ) ) { flag = 1; break; } /* 如果新增的矩形与队列中的矩形不重叠 */ if( !LCUIRect_Overlay(rect, *cur_rect_ptr) ) { continue; } continue; // 暂时不对相交的矩形进行分割 DEBUG_MSG("[%d] rect overlay, start cut\n", i); /* 根据当前区域,分割新区域 */ LCUIRect_Cut( *cur_rect_ptr, rect, &rect_buff ); for( i=0; i<rect_buff.total_num; ++i ) { rect_ptr = (LCUI_Rect*)Queue_Get( &rect_buff, i ); if( !rect_ptr ) { break; } DEBUG_MSG("[%d] add child rect: %d,%d,%d,%d\n", i, rect_ptr->x, rect_ptr->y, rect_ptr->width, rect_ptr->height); RectQueue_Add( queue, *rect_ptr ); } flag = 1; break; } /* 销毁队列 */ Queue_Destroy( &rect_buff ); if ( flag == 0 ) { return Queue_Add( queue, &rect ); } return -1; }
LCUI_API int GraphLayer_GetGraph( LCUI_GraphLayer *ctnr, LCUI_Graph *graph_buff, LCUI_Rect rect ) { int i, total; uchar_t tmp_alpha, alpha; LCUI_Pos pos, glayer_pos; LCUI_GraphLayer *glayer; LCUI_Queue glayerQ; LCUI_Rect valid_area; LCUI_Graph tmp_graph; /* 检测这个区域是否有效 */ if (rect.x < 0 || rect.y < 0) { return -1; } if (rect.x + rect.width > ctnr->graph.w || rect.y + rect.height > ctnr->graph.h ) { return -1; } if (rect.width <= 0 || rect.height <= 0) { return -2; } Graph_Init( &tmp_graph ); Queue_Init( &glayerQ, 0, NULL); Queue_UsingPointer( &glayerQ ); graph_buff->color_type = COLOR_TYPE_RGB; /* 为graph_buff分配合适尺寸的内存空间 */ Graph_Create( graph_buff, rect.width, rect.height ); /* 获取rect区域内的图层列表 */ GraphLayer_GetLayers( ctnr, rect, &glayerQ ); total = Queue_GetTotal( &glayerQ ); //_DEBUG_MSG( "total: %d\n", total ); /* 若记录数为零,则表明该区域没有图层 */ if( total <= 0 ) { /* 若没有父图层,则填充白色 */ if( ctnr == NULL ) { Graph_FillColor( graph_buff, RGB(255,255,255) ); } else { /* 否则使用父图层的图形 */ Graph_Cut( &ctnr->graph, rect, graph_buff ); } /* 销毁记录 */ Queue_Destroy( &glayerQ ); return 0; } /* 从顶层到底层遍历图层,排除被其它图层完全遮挡或者自身完全透明的图层 */ for(i=total-1; i>=0; --i) { glayer = (LCUI_GraphLayer*)Queue_Get( &glayerQ, i ); valid_area = GraphLayer_GetValidRect( ctnr, glayer ); glayer_pos = GraphLayer_GetGlobalPos( ctnr, glayer ); valid_area.x += glayer_pos.x; valid_area.y += glayer_pos.y; alpha = GraphLayer_GetRealAlpha( glayer ); /* 当前图层的透明度小于255的话,就跳过 */ if( alpha < 255 ) { continue; } /* 如果色彩类型为RGB,没有alpha通道 */ if( glayer->graph.color_type == COLOR_TYPE_RGB ) { /* 如果该图层的有效区域包含目标区域 */ if( LCUIRect_IncludeRect(valid_area, rect) ) { /* 移除底层的图层,因为已经被完全遮挡 */ for(total=i-1;total>=0; --total) { Queue_DeletePointer( &glayerQ, 0 ); } goto skip_loop; } } } skip_loop: total = Queue_GetTotal( &glayerQ ); //_DEBUG_MSG( "total: %d\n", total ); if(i <= 0) { if( ctnr == NULL ) { Graph_FillColor( graph_buff, RGB(255,255,255) ); } else { Graph_Cut ( &ctnr->graph, rect, graph_buff ); } } /* 获取图层列表中的图层 */ for(i=0; i<total; ++i) { glayer = (LCUI_GraphLayer*)Queue_Get( &glayerQ, i ); //_DEBUG_MSG("%p = Queue_Get( %p, %d )\n", glayer, &glayerQ, i); if( !glayer ) { continue; } /* 锁上该图层的互斥锁 */ Graph_Lock( &glayer->graph ); /* 获取该图层的有效区域及全局坐标 */ pos = GraphLayer_GetGlobalPos( ctnr, glayer ); valid_area = GraphLayer_GetValidRect( ctnr, glayer ); /* 引用该图层的有效区域内的图像 */ Graph_Quote( &tmp_graph, &glayer->graph, valid_area ); //_DEBUG_MSG("valid area: %d,%d,%d,%d, pos: %d,%d, size: %d,%d\n", // valid_area.x, valid_area.y, valid_area.width, valid_area.height, // pos.x, pos.y, glayer->graph.w, glayer->graph.h // ); /* 获取相对坐标 */ pos.x = pos.x - rect.x + valid_area.x; pos.y = pos.y - rect.y + valid_area.y; //_DEBUG_MSG("mix pos: %d,%d\n", pos.x, pos.y); /* 如果该图层没有继承父图层的透明度 */ if( !glayer->inherit_alpha ) { /* 直接叠加至graph_buff */ Graph_Mix( graph_buff, &tmp_graph, pos ); } else { /* 否则,计算该图层应有的透明度 */ alpha = GraphLayer_GetRealAlpha( glayer ); /* 备份该图层的全局透明度 */ tmp_alpha = glayer->graph.alpha; /* 将实际透明度作为全局透明度,参与图像叠加 */ glayer->graph.alpha = alpha; Graph_Mix( graph_buff, &tmp_graph, pos ); /* 还原全局透明度 */ glayer->graph.alpha = tmp_alpha; } /* 解锁图层 */ Graph_Unlock( &glayer->graph ); } Queue_Destroy( &glayerQ ); return 0; }