mirror of
https://github.com/lc-soft/LCUI.git
synced 2026-12-01 16:56:34 +01:00
Update widget scaling support
This commit is contained in:
@@ -52,9 +52,15 @@ typedef enum LCUI_DensityLevel {
|
||||
/** 转换成单位为 px 的度量值 */
|
||||
LCUI_API float LCUIMetrics_Compute( float value, LCUI_StyleType type );
|
||||
|
||||
/** 将矩形中的度量值的单位转换为 px */
|
||||
LCUI_API void LCUIMetrics_ComputeRectActual( LCUI_Rect *dst, const LCUI_RectF *src );
|
||||
|
||||
/** 转换成单位为 px 的实际度量值 */
|
||||
LCUI_API int LCUIMetrics_ComputeActual( float value, LCUI_StyleType type );
|
||||
|
||||
/** 获取当前的全局缩放比例 */
|
||||
LCUI_API float LCUIMetrics_GetScale( void );
|
||||
|
||||
/** 设置密度 */
|
||||
LCUI_API void LCUIMetrics_SetDensity( float density );
|
||||
|
||||
|
||||
+22
-15
@@ -132,12 +132,14 @@ void LCUIDisplay_Update( void )
|
||||
|
||||
void LCUIDisplay_Render( void )
|
||||
{
|
||||
float scale;
|
||||
LinkedListNode *sn, *rn;
|
||||
LCUI_PaintContext paint;
|
||||
|
||||
if( !display.is_working ) {
|
||||
return;
|
||||
}
|
||||
scale = LCUIMetrics_GetScale();
|
||||
/* 遍历当前的 surface 记录列表 */
|
||||
for( LinkedList_Each( sn, &display.surfaces ) ) {
|
||||
SurfaceRecord record = sn->data;
|
||||
@@ -150,11 +152,17 @@ void LCUIDisplay_Render( void )
|
||||
record->rendered = FALSE;
|
||||
/* 在 surface 上逐个重绘无效区域 */
|
||||
for( LinkedList_Each( rn, &record->rects ) ) {
|
||||
paint = Surface_BeginPaint( surface, rn->data );
|
||||
LCUI_Rect rect;
|
||||
LCUI_Rect *r = rn->data;
|
||||
rect.x = r->x * scale;
|
||||
rect.y = r->y * scale;
|
||||
rect.width = r->width * scale;
|
||||
rect.height = r->height * scale;
|
||||
paint = Surface_BeginPaint( surface, &rect );
|
||||
if( !paint ) {
|
||||
continue;
|
||||
}
|
||||
DEBUG_MSG( "[%s]: render rect: (%d,%d,%d,%d)\n",
|
||||
_DEBUG_MSG( "[%s]: render rect: (%d,%d,%d,%d)\n",
|
||||
record->widget->type,
|
||||
paint->rect.x, paint->rect.y,
|
||||
paint->rect.width, paint->rect.height );
|
||||
@@ -254,28 +262,23 @@ LCUI_Surface LCUIDisplay_GetSurfaceByHandle( void *handle )
|
||||
/** 将 widget 与 sruface 进行绑定 */
|
||||
static void LCUIDisplay_BindSurface( LCUI_Widget widget )
|
||||
{
|
||||
LCUI_RectF *rect;
|
||||
LCUI_Rect rect;
|
||||
SurfaceRecord record;
|
||||
int width, height;
|
||||
|
||||
if( LCUIDisplay_GetBindSurface( widget ) ) {
|
||||
return;
|
||||
}
|
||||
rect = &widget->box.graph;
|
||||
record = NEW( SurfaceRecordRec, 1 );
|
||||
record->surface = Surface_New();
|
||||
record->widget = widget;
|
||||
record->rendered = FALSE;
|
||||
LinkedList_Init( &record->rects );
|
||||
Surface_SetCaptionW( record->surface, widget->title );
|
||||
LCUIMetrics_ComputeRectActual( &rect, &widget->box.graph );
|
||||
if( widget->style->sheet[key_top].is_valid &&
|
||||
widget->style->sheet[key_left].is_valid ) {
|
||||
Surface_Move( record->surface, roundi( rect->x ),
|
||||
roundi( rect->y ) );
|
||||
Surface_Move( record->surface, rect.x, rect.y );
|
||||
}
|
||||
width = roundi( rect->width );
|
||||
height = roundi( rect->height );
|
||||
Surface_Resize( record->surface, width, height );
|
||||
Surface_Resize( record->surface, rect.width, rect.height );
|
||||
if( widget->computed_style.visible ) {
|
||||
Surface_Show( record->surface );
|
||||
} else {
|
||||
@@ -398,15 +401,17 @@ void LCUIDisplay_HideRectBorder( void )
|
||||
/** 设置显示区域的尺寸,仅在窗口化、全屏模式下有效 */
|
||||
void LCUIDisplay_SetSize( int width, int height )
|
||||
{
|
||||
float scale;
|
||||
LCUI_Widget root;
|
||||
LCUI_Surface surface;
|
||||
if( display.mode == LCDM_SEAMLESS ) {
|
||||
return;
|
||||
}
|
||||
root = LCUIWidget_GetRoot();
|
||||
scale = LCUIMetrics_GetScale();
|
||||
surface = LCUIDisplay_GetBindSurface( root );
|
||||
Surface_Resize( surface, width, height );
|
||||
Widget_Resize( root, (float)width, (float)height );
|
||||
Widget_Resize( root, width / scale, height / scale );
|
||||
}
|
||||
|
||||
int LCUIDisplay_GetWidth( void )
|
||||
@@ -619,13 +624,15 @@ static void OnResize( LCUI_Event e, void *arg )
|
||||
{
|
||||
LCUI_Widget widget;
|
||||
LCUI_DisplayEvent dpy_ev = arg;
|
||||
float width = (float)( dpy_ev->resize.width );
|
||||
float height = (float)( dpy_ev->resize.height );
|
||||
LOG( "[display] resize: (%.2f,%.2f)\n", width, height );
|
||||
float scale = LCUIMetrics_GetScale();
|
||||
float width = dpy_ev->resize.width / scale;
|
||||
float height = dpy_ev->resize.height / scale;
|
||||
widget = LCUIDisplay_GetBindWidget( dpy_ev->surface );
|
||||
if( widget ) {
|
||||
Widget_Resize( widget, width, height );
|
||||
}
|
||||
LOG( "[display] resize: (%d,%d)\n",
|
||||
dpy_ev->resize.width, dpy_ev->resize.height );
|
||||
}
|
||||
|
||||
int LCUIDisplay_BindEvent( int event_id,
|
||||
|
||||
@@ -65,6 +65,19 @@ int LCUIMetrics_ComputeActual( float value, LCUI_StyleType type )
|
||||
return roundi( LCUIMetrics_Compute( value, type ) * metrics.scale );
|
||||
}
|
||||
|
||||
void LCUIMetrics_ComputeRectActual( LCUI_Rect *dst, const LCUI_RectF *src )
|
||||
{
|
||||
dst->x = LCUIMetrics_ComputeActual( src->x, SVT_PX );
|
||||
dst->y = LCUIMetrics_ComputeActual( src->y, SVT_PX );
|
||||
dst->width = LCUIMetrics_ComputeActual( src->width, SVT_PX );
|
||||
dst->height = LCUIMetrics_ComputeActual( src->height, SVT_PX );
|
||||
}
|
||||
|
||||
float LCUIMetrics_GetScale( void )
|
||||
{
|
||||
return metrics.scale;
|
||||
}
|
||||
|
||||
static float ComputeDensityByLevel( LCUI_DensityLevel level )
|
||||
{
|
||||
float density = metrics.dpi / 96.0f;
|
||||
|
||||
+15
-11
@@ -256,6 +256,8 @@ int Widget_ConvertArea( LCUI_Widget w, LCUI_Rect *in_rect,
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define ComputeActualPX(VAL) LCUIMetrics_ComputeActual( VAL, SVT_PX )
|
||||
|
||||
void Widget_Render( LCUI_Widget w, LCUI_PaintContext paint )
|
||||
{
|
||||
LinkedListNode *node;
|
||||
@@ -305,10 +307,10 @@ void Widget_Render( LCUI_Widget w, LCUI_PaintContext paint )
|
||||
content_left = w->box.padding.x - w->box.graph.x;
|
||||
content_top = w->box.padding.y - w->box.graph.y;
|
||||
/* 获取内容框 */
|
||||
content_rect.x = roundi( content_left );
|
||||
content_rect.y = roundi( content_top );
|
||||
content_rect.width = roundi( w->box.padding.width );
|
||||
content_rect.height = roundi( w->box.padding.height );
|
||||
content_rect.x = ComputeActualPX( content_left );
|
||||
content_rect.y = ComputeActualPX( content_top );
|
||||
content_rect.width = ComputeActualPX( w->box.padding.width );
|
||||
content_rect.height = ComputeActualPX( w->box.padding.height );
|
||||
/* 获取内容框与脏矩形重叠的区域 */
|
||||
has_overlay = LCUIRect_GetOverlayRect(
|
||||
&content_rect, &paint->rect, &content_rect
|
||||
@@ -324,8 +326,8 @@ void Widget_Render( LCUI_Widget w, LCUI_PaintContext paint )
|
||||
if( has_content_graph ) {
|
||||
child_paint.with_alpha = TRUE;
|
||||
content_graph.color_type = COLOR_TYPE_ARGB;
|
||||
Graph_Create( &content_graph,
|
||||
content_rect.width, content_rect.height );
|
||||
Graph_Create( &content_graph, content_rect.width,
|
||||
content_rect.height );
|
||||
} else {
|
||||
child_paint.with_alpha = paint->with_alpha;
|
||||
/* 引用该区域的位图,作为内容框的位图 */
|
||||
@@ -333,6 +335,7 @@ void Widget_Render( LCUI_Widget w, LCUI_PaintContext paint )
|
||||
}
|
||||
/* 按照显示顺序,从底到顶,递归遍历子级部件 */
|
||||
LinkedList_ForEachReverse( node, &w->children_show ) {
|
||||
LCUI_RectF rect;
|
||||
LCUI_Rect child_rect;
|
||||
LCUI_Widget child = node->data;
|
||||
if( !child->computed_style.visible ||
|
||||
@@ -340,10 +343,11 @@ void Widget_Render( LCUI_Widget w, LCUI_PaintContext paint )
|
||||
continue;
|
||||
}
|
||||
/* 转换子部件区域,由相对于内容框转换为相对于当前脏矩形 */
|
||||
child_rect.x = roundi( child->box.graph.x + content_left );
|
||||
child_rect.y = roundi( child->box.graph.y + content_top );
|
||||
child_rect.width = roundi( child->box.graph.width );
|
||||
child_rect.height = roundi( child->box.graph.height );
|
||||
rect.x = child->box.graph.x + content_left;
|
||||
rect.y = child->box.graph.y + content_top;
|
||||
rect.width = child->box.graph.width;
|
||||
rect.height = child->box.graph.height;
|
||||
LCUIMetrics_ComputeRectActual( &child_rect, &rect );
|
||||
child_rect.x -= paint->rect.x;
|
||||
child_rect.y -= paint->rect.y;
|
||||
/* 获取与内容框重叠的区域,作为子部件的绘制区域 */
|
||||
@@ -362,7 +366,7 @@ void Widget_Render( LCUI_Widget w, LCUI_PaintContext paint )
|
||||
/* 将绘制区域转换为相对于子部件 */
|
||||
child_paint.rect.x -= child_rect.x;
|
||||
child_paint.rect.y -= child_rect.y;
|
||||
DEBUG_MSG( "[%s]: canvas_rect:(%d,%d,%d,%d)\n", w->type,
|
||||
_DEBUG_MSG( "[%s]: canvas_rect:(%d,%d,%d,%d)\n", w->type,
|
||||
canvas_rect.x, canvas_rect.y,
|
||||
canvas_rect.width, canvas_rect.height );
|
||||
/* 在内容位图中引用所需的区域,作为子部件的画布 */
|
||||
|
||||
@@ -464,7 +464,6 @@ static void OnWMGetMinMaxInfo( LCUI_Event e, void *arg )
|
||||
if( style->max_height >= 0 ) {
|
||||
mminfo->ptMaxTrackSize.y = (LONG)style->max_height;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static void OnWMSize( LCUI_Event e, void *arg )
|
||||
|
||||
Reference in New Issue
Block a user