diff --git a/blendish.h b/blendish.h index 6cb7dce..3b9d11d 100644 --- a/blendish.h +++ b/blendish.h @@ -343,6 +343,15 @@ void bndMenuItem(NVGcontext *ctx, // Draw a tooltip background with its lower left origin at (x,y) and size of (w,h) void bndTooltipBackground(NVGcontext *ctx, float x, float y, float w, float h); +//////////////////////////////////////////////////////////////////////////////// + +// Estimator Functions +// ------------------- +// Use these functions to estimate sizes for widgets with your NVGcontext. + +// returns the ideal width for a label with given icon and text +float bndLabelWidth(NVGcontext *ctx, int iconid, const char *label); + //////////////////////////////////////////////////////////////////////////////// // Low Level Functions @@ -988,6 +997,21 @@ void bndMenuItem(NVGcontext *ctx, //////////////////////////////////////////////////////////////////////////////// +float bndLabelWidth(NVGcontext *ctx, int iconid, const char *label) { + int w = BND_PAD_LEFT + BND_PAD_RIGHT; + if (iconid >= 0) { + w += BND_ICON_SHEET_RES; + } + if (label && (bnd_font >= 0)) { + nvgFontFaceId(ctx, bnd_font); + nvgFontSize(ctx, BND_LABEL_FONT_SIZE); + w += nvgTextBounds(ctx, 1, 1, label, NULL, NULL); + } + return w; +} + +//////////////////////////////////////////////////////////////////////////////// + void bndRoundedBox(NVGcontext *ctx, float x, float y, float w, float h, float cr0, float cr1, float cr2, float cr3) { float d; diff --git a/oui.h b/oui.h index 4c1bdb3..4e9029d 100644 --- a/oui.h +++ b/oui.h @@ -840,12 +840,14 @@ int uiGetRelToDown(int item) { } -UI_INLINE int uiComputeChainSize(UIitem *pkid, int dim) { +UI_INLINE void uiComputeChainSize(UIitem *pkid, + int *need_size, int *hard_size, int dim) { UIitem *pitem = pkid; int wdim = dim+2; - int size = 0; - if (pitem->rect.v[wdim]) - size = pitem->rect.v[wdim] + pitem->margins[dim] + pitem->margins[wdim]; + int size = pitem->rect.v[wdim] + pitem->margins[dim] + pitem->margins[wdim]; + *need_size = size; + *hard_size = pitem->size.v[dim]?size:0; + int it = 0; pitem->visited |= 1<relto[dim] < 0) break; pitem = uiItemPtr(pitem->relto[dim]); pitem->visited |= 1<rect.v[wdim]) - size += pitem->rect.v[wdim] + pitem->margins[dim] + pitem->margins[wdim]; + size = pitem->rect.v[wdim] + pitem->margins[dim] + pitem->margins[wdim]; + *need_size = (*need_size) + size; + *hard_size = (*hard_size) + (pitem->size.v[dim]?size:0); it++; assert(it<1000000); // infinite loop } @@ -865,31 +868,35 @@ UI_INLINE int uiComputeChainSize(UIitem *pkid, int dim) { if (pitem->relto[wdim] < 0) break; pitem = uiItemPtr(pitem->relto[wdim]); pitem->visited |= 1<rect.v[wdim]) - size += pitem->rect.v[wdim] + pitem->margins[dim] + pitem->margins[wdim]; + size = pitem->rect.v[wdim] + pitem->margins[dim] + pitem->margins[wdim]; + *need_size = (*need_size) + size; + *hard_size = (*hard_size) + (pitem->size.v[dim]?size:0); it++; assert(it<1000000); // infinite loop } - return size; } UI_INLINE void uiComputeSizeDim(UIitem *pitem, int dim) { int wdim = dim+2; - int size = 0; + int need_size = 0; + int hard_size = 0; int kid = pitem->firstkid; while (kid >= 0) { UIitem *pkid = uiItemPtr(kid); if (!(pkid->visited & (1<computed_size.v[dim] = size; - + pitem->computed_size.v[dim] = hard_size; + if (pitem->size.v[dim]) { pitem->rect.v[wdim] = pitem->size.v[dim]; } else { - pitem->rect.v[wdim] = size; + pitem->rect.v[wdim] = need_size; } }