summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJaakko Keränen <jaakko.keranen@iki.fi>2020-09-05 16:46:42 +0300
committerJaakko Keränen <jaakko.keranen@iki.fi>2020-09-05 16:46:42 +0300
commite242cee1974fc265e005d05f8849af9f2cfb9483 (patch)
treec931ac6ad99ebf97a920eaa1b966b5becc10627b /src
parent8e2828efc6d9c88f39592f4ae1c709937b8b9f46 (diff)
Widget: More granularity for drawing widgets
Allow draw background and children separately.
Diffstat (limited to 'src')
-rw-r--r--src/ui/widget.c36
-rw-r--r--src/ui/widget.h3
2 files changed, 32 insertions, 7 deletions
diff --git a/src/ui/widget.c b/src/ui/widget.c
index cd02d5e4..11f1f2c0 100644
--- a/src/ui/widget.c
+++ b/src/ui/widget.c
@@ -184,12 +184,14 @@ static int widestChild_Widget_(const iWidget *d) {
184} 184}
185 185
186static void setWidth_Widget_(iWidget *d, int width) { 186static void setWidth_Widget_(iWidget *d, int width) {
187 iAssert(width >= 0);
187 if (~d->flags & fixedWidth_WidgetFlag || d->flags & collapse_WidgetFlag) { 188 if (~d->flags & fixedWidth_WidgetFlag || d->flags & collapse_WidgetFlag) {
188 d->rect.size.x = width; 189 d->rect.size.x = width;
189 } 190 }
190} 191}
191 192
192static void setHeight_Widget_(iWidget *d, int height) { 193static void setHeight_Widget_(iWidget *d, int height) {
194 iAssert(height >= 0);
193 if (~d->flags & fixedHeight_WidgetFlag || d->flags & collapse_WidgetFlag) { 195 if (~d->flags & fixedHeight_WidgetFlag || d->flags & collapse_WidgetFlag) {
194 d->rect.size.y = height; 196 d->rect.size.y = height;
195 } 197 }
@@ -203,14 +205,16 @@ iLocalDef iBool isCollapsed_Widget_(const iWidget *d) {
203iLocalDef iRect innerRect_Widget_(const iWidget *d) { 205iLocalDef iRect innerRect_Widget_(const iWidget *d) {
204 return init_Rect(d->padding[0], 206 return init_Rect(d->padding[0],
205 d->padding[1], 207 d->padding[1],
206 width_Rect(d->rect) - d->padding[0] - d->padding[2], 208 iMaxi(0, width_Rect(d->rect) - d->padding[0] - d->padding[2]),
207 height_Rect(d->rect) - d->padding[1] - d->padding[3]); 209 iMaxi(0, height_Rect(d->rect) - d->padding[1] - d->padding[3]));
208} 210}
209 211
210iRect innerBounds_Widget(const iWidget *d) { 212iRect innerBounds_Widget(const iWidget *d) {
211 return adjusted_Rect(bounds_Widget(d), 213 iRect ib = adjusted_Rect(bounds_Widget(d),
212 init_I2(d->padding[0], d->padding[1]), 214 init_I2(d->padding[0], d->padding[1]),
213 init_I2(-d->padding[2], -d->padding[3])); 215 init_I2(-d->padding[2], -d->padding[3]));
216 ib.size = max_I2(zero_I2(), ib.size);
217 return ib;
214} 218}
215 219
216void arrange_Widget(iWidget *d) { 220void arrange_Widget(iWidget *d) {
@@ -265,7 +269,7 @@ void arrange_Widget(iWidget *d) {
265 subv_I2(&avail, child->rect.size); 269 subv_I2(&avail, child->rect.size);
266 } 270 }
267 } 271 }
268 avail = divi_I2(avail, expCount); 272 avail = divi_I2(max_I2(zero_I2(), avail), expCount);
269 iForEach(ObjectList, j, d->children) { 273 iForEach(ObjectList, j, d->children) {
270 iWidget *child = as_Widget(j.object); 274 iWidget *child = as_Widget(j.object);
271 if (isCollapsed_Widget_(child)) continue; 275 if (isCollapsed_Widget_(child)) continue;
@@ -508,7 +512,7 @@ iBool processEvent_Widget(iWidget *d, const SDL_Event *ev) {
508 return iFalse; 512 return iFalse;
509} 513}
510 514
511void draw_Widget(const iWidget *d) { 515void drawBackground_Widget(const iWidget *d) {
512 if (d->flags & hidden_WidgetFlag) return; 516 if (d->flags & hidden_WidgetFlag) return;
513 if (d->bgColor >= 0 || d->frameColor >= 0) { 517 if (d->bgColor >= 0 || d->frameColor >= 0) {
514 const iRect rect = bounds_Widget(d); 518 const iRect rect = bounds_Widget(d);
@@ -521,6 +525,10 @@ void draw_Widget(const iWidget *d) {
521 drawRectThickness_Paint(&p, rect, gap_UI / 4, d->frameColor); 525 drawRectThickness_Paint(&p, rect, gap_UI / 4, d->frameColor);
522 } 526 }
523 } 527 }
528}
529
530void drawChildren_Widget(const iWidget *d) {
531 if (d->flags & hidden_WidgetFlag) return;
524 iConstForEach(ObjectList, i, d->children) { 532 iConstForEach(ObjectList, i, d->children) {
525 const iWidget *child = constAs_Widget(i.object); 533 const iWidget *child = constAs_Widget(i.object);
526 if (~child->flags & keepOnTop_WidgetFlag && ~child->flags & hidden_WidgetFlag) { 534 if (~child->flags & keepOnTop_WidgetFlag && ~child->flags & hidden_WidgetFlag) {
@@ -535,6 +543,11 @@ void draw_Widget(const iWidget *d) {
535 } 543 }
536} 544}
537 545
546void draw_Widget(const iWidget *d) {
547 drawBackground_Widget(d);
548 drawChildren_Widget(d);
549}
550
538iAny *addChild_Widget(iWidget *d, iAnyObject *child) { 551iAny *addChild_Widget(iWidget *d, iAnyObject *child) {
539 return addChildPos_Widget(d, child, back_WidgetAddPos); 552 return addChildPos_Widget(d, child, back_WidgetAddPos);
540} 553}
@@ -610,6 +623,15 @@ iAny *findChild_Widget(const iWidget *d, const char *id) {
610 return NULL; 623 return NULL;
611} 624}
612 625
626iAny *findParentClass_Widget(const iWidget *d, const iAnyClass *class) {
627 if (!d) return NULL;
628 iWidget *i = d->parent;
629 while (i && !isInstance_Object(i, class)) {
630 i = i->parent;
631 }
632 return i;
633}
634
613size_t childCount_Widget(const iWidget *d) { 635size_t childCount_Widget(const iWidget *d) {
614 if (!d->children) return 0; 636 if (!d->children) return 0;
615 return size_ObjectList(d->children); 637 return size_ObjectList(d->children);
diff --git a/src/ui/widget.h b/src/ui/widget.h
index 3f03fc07..b1b7a1d7 100644
--- a/src/ui/widget.h
+++ b/src/ui/widget.h
@@ -135,9 +135,12 @@ iRect innerBounds_Widget (const iWidget *);
135iInt2 localCoord_Widget (const iWidget *, iInt2 coord); 135iInt2 localCoord_Widget (const iWidget *, iInt2 coord);
136iBool contains_Widget (const iWidget *, iInt2 coord); 136iBool contains_Widget (const iWidget *, iInt2 coord);
137iAny * findChild_Widget (const iWidget *, const char *id); 137iAny * findChild_Widget (const iWidget *, const char *id);
138iAny * findParentClass_Widget(const iWidget *, const iAnyClass *class);
138iAny * findFocusable_Widget(const iWidget *startFrom, enum iWidgetFocusDir focusDir); 139iAny * findFocusable_Widget(const iWidget *startFrom, enum iWidgetFocusDir focusDir);
139size_t childCount_Widget (const iWidget *); 140size_t childCount_Widget (const iWidget *);
140void draw_Widget (const iWidget *); 141void draw_Widget (const iWidget *);
142void drawBackground_Widget(const iWidget *);
143void drawChildren_Widget (const iWidget *);
141 144
142iLocalDef iObjectList *children_Widget(iAnyObject *d) { 145iLocalDef iObjectList *children_Widget(iAnyObject *d) {
143 iAssert(isInstance_Object(d, &Class_Widget)); 146 iAssert(isInstance_Object(d, &Class_Widget));