Browse Source

blendish: bndLabelWidth() estimator

oui: improved layouting of spanning child items
pull/1/head
Leonard Ritter 10 years ago
parent
commit
15bfb56c17
2 changed files with 45 additions and 14 deletions
  1. +24
    -0
      blendish.h
  2. +21
    -14
      oui.h

+ 24
- 0
blendish.h View File

@@ -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;


+ 21
- 14
oui.h View File

@@ -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<<dim;
// traverse along left neighbors
@@ -853,8 +855,9 @@ UI_INLINE int uiComputeChainSize(UIitem *pkid, int dim) {
if (pitem->relto[dim] < 0) break;
pitem = uiItemPtr(pitem->relto[dim]);
pitem->visited |= 1<<dim;
if (pitem->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<<dim;
if (pitem->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<<dim))) {
size = ui_max(size, uiComputeChainSize(pkid, dim));
int ns,hs;
uiComputeChainSize(pkid, &ns, &hs, dim);
need_size = ui_max(need_size, ns);
hard_size = ui_max(hard_size, hs);
}
kid = uiNextSibling(kid);
}
pitem->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;
}
}



Loading…
Cancel
Save