From cb96aa47dbcefa8be8db63dc8fc2607b16b2d5d1 Mon Sep 17 00:00:00 2001 From: Ton Roosendaal Date: Mon, 1 Jun 2009 16:22:53 +0000 Subject: 2.5 Added support for cached text drawing in View2D. Cache is needed to prevent the viewmatrix being set/restored on each text drawing. Adding a string: void UI_view2d_text_cache_add(View2D *v2d, float x, float y, char *str) Drawing: void UI_view2d_text_cache_draw(ARegion *ar) Nothing else needed; just make sure cache-draw is always called at end of a view2d drawing function, to clear cache memory. On todo for next: a version with a rectf boundary to clip text within. --- source/blender/editors/interface/view2d.c | 52 +++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'source/blender/editors/interface/view2d.c') diff --git a/source/blender/editors/interface/view2d.c b/source/blender/editors/interface/view2d.c index f2fc2deefbb..fc1de788f5f 100644 --- a/source/blender/editors/interface/view2d.c +++ b/source/blender/editors/interface/view2d.c @@ -1939,3 +1939,55 @@ short UI_view2d_mouse_in_scrollers (const bContext *C, View2D *v2d, int x, int y return 0; } +/* ******************* view2d text drawing cache ******************** */ + +/* assumes caches are used correctly, so for time being no local storage in v2d */ +static ListBase strings= {NULL, NULL}; + +typedef struct View2DString { + struct View2DString *next, *prev; + float col[4]; + char str[128]; + short mval[2]; +} View2DString; + + +void UI_view2d_text_cache_add(View2D *v2d, float x, float y, char *str) +{ + int mval[2]; + + UI_view2d_view_to_region(v2d, x, y, mval, mval+1); + + if(mval[0]!=V2D_IS_CLIPPED && mval[1]!=V2D_IS_CLIPPED) { + View2DString *v2s= MEM_callocN(sizeof(View2DString), "View2DString"); + + BLI_addtail(&strings, v2s); + BLI_strncpy(v2s->str, str, 128); + v2s->mval[0]= mval[0]; + v2s->mval[1]= mval[1]; + glGetFloatv(GL_CURRENT_COLOR, v2s->col); + } +} + +void UI_view2d_text_cache_draw(ARegion *ar) +{ + View2DString *v2s; + + // wmPushMatrix(); + ED_region_pixelspace(ar); + + for(v2s= strings.first; v2s; v2s= v2s->next) { + glColor3fv(v2s->col); + BLF_draw_default((float)v2s->mval[0], (float)v2s->mval[1], 0.0, v2s->str); + } + + // wmPopMatrix(); + + if(strings.first) + BLI_freelistN(&strings); +} + + +/* ******************************************************** */ + + -- cgit v1.2.3 From a117731aa23c25d699c405325c7bb7ac5680a5e7 Mon Sep 17 00:00:00 2001 From: Ton Roosendaal Date: Mon, 1 Jun 2009 17:21:03 +0000 Subject: 2.5 - Fix: text draw in fonts was slightly too low; it didn't calculate offset correctly. Now it is aligned to have number characters in center. - Fix: text clip was too wide, giving errors on extreme zoom in. - Added boundbox-clipped default text drawing for view2d: void UI_view2d_text_cache_rectf(View2D *v2d, rctf *rect, char *str) (Note; also for previous commit, this cache immediately projects, so if you change view2d while drawing, text is still on correct positions) --- source/blender/editors/interface/view2d.c | 32 ++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'source/blender/editors/interface/view2d.c') diff --git a/source/blender/editors/interface/view2d.c b/source/blender/editors/interface/view2d.c index fc1de788f5f..b363f1f6272 100644 --- a/source/blender/editors/interface/view2d.c +++ b/source/blender/editors/interface/view2d.c @@ -1949,6 +1949,7 @@ typedef struct View2DString { float col[4]; char str[128]; short mval[2]; + rcti rect; } View2DString; @@ -1959,6 +1960,7 @@ void UI_view2d_text_cache_add(View2D *v2d, float x, float y, char *str) UI_view2d_view_to_region(v2d, x, y, mval, mval+1); if(mval[0]!=V2D_IS_CLIPPED && mval[1]!=V2D_IS_CLIPPED) { + /* use calloc, rect has to be zeroe'd */ View2DString *v2s= MEM_callocN(sizeof(View2DString), "View2DString"); BLI_addtail(&strings, v2s); @@ -1969,6 +1971,20 @@ void UI_view2d_text_cache_add(View2D *v2d, float x, float y, char *str) } } +/* no clip (yet) */ +void UI_view2d_text_cache_rectf(View2D *v2d, rctf *rect, char *str) +{ + View2DString *v2s= MEM_callocN(sizeof(View2DString), "View2DString"); + + UI_view2d_to_region_no_clip(v2d, rect->xmin, rect->ymin, &v2s->rect.xmin, &v2s->rect.ymin); + UI_view2d_to_region_no_clip(v2d, rect->xmax, rect->ymax, &v2s->rect.xmax, &v2s->rect.ymax); + + BLI_addtail(&strings, v2s); + BLI_strncpy(v2s->str, str, 128); + glGetFloatv(GL_CURRENT_COLOR, v2s->col); +} + + void UI_view2d_text_cache_draw(ARegion *ar) { View2DString *v2s; @@ -1978,7 +1994,21 @@ void UI_view2d_text_cache_draw(ARegion *ar) for(v2s= strings.first; v2s; v2s= v2s->next) { glColor3fv(v2s->col); - BLF_draw_default((float)v2s->mval[0], (float)v2s->mval[1], 0.0, v2s->str); + if(v2s->rect.xmin==v2s->rect.xmax) + BLF_draw_default((float)v2s->mval[0], (float)v2s->mval[1], 0.0, v2s->str); + else { + int xofs=0, yofs; + + yofs= ceil( 0.5f*(v2s->rect.ymax - v2s->rect.ymin - BLF_height_default("28"))); + if(yofs<1) yofs= 1; + + BLF_clipping(v2s->rect.xmin-4, v2s->rect.ymin-4, v2s->rect.xmax+4, v2s->rect.ymax+4); + BLF_enable(BLF_CLIPPING); + + BLF_draw_default(v2s->rect.xmin+xofs, v2s->rect.ymin+yofs, 0.0f, v2s->str); + + BLF_disable(BLF_CLIPPING); + } } // wmPopMatrix(); -- cgit v1.2.3