diff options
author | Jaakko Keränen <jaakko.keranen@iki.fi> | 2020-09-05 16:46:42 +0300 |
---|---|---|
committer | Jaakko Keränen <jaakko.keranen@iki.fi> | 2020-09-05 16:46:42 +0300 |
commit | e242cee1974fc265e005d05f8849af9f2cfb9483 (patch) | |
tree | c931ac6ad99ebf97a920eaa1b966b5becc10627b /src | |
parent | 8e2828efc6d9c88f39592f4ae1c709937b8b9f46 (diff) |
Widget: More granularity for drawing widgets
Allow draw background and children separately.
Diffstat (limited to 'src')
-rw-r--r-- | src/ui/widget.c | 36 | ||||
-rw-r--r-- | src/ui/widget.h | 3 |
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 | ||
186 | static void setWidth_Widget_(iWidget *d, int width) { | 186 | static 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 | ||
192 | static void setHeight_Widget_(iWidget *d, int height) { | 193 | static 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) { | |||
203 | iLocalDef iRect innerRect_Widget_(const iWidget *d) { | 205 | iLocalDef 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 | ||
210 | iRect innerBounds_Widget(const iWidget *d) { | 212 | iRect 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 | ||
216 | void arrange_Widget(iWidget *d) { | 220 | void 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 | ||
511 | void draw_Widget(const iWidget *d) { | 515 | void 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 | |||
530 | void 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 | ||
546 | void draw_Widget(const iWidget *d) { | ||
547 | drawBackground_Widget(d); | ||
548 | drawChildren_Widget(d); | ||
549 | } | ||
550 | |||
538 | iAny *addChild_Widget(iWidget *d, iAnyObject *child) { | 551 | iAny *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 | ||
626 | iAny *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 | |||
613 | size_t childCount_Widget(const iWidget *d) { | 635 | size_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 *); | |||
135 | iInt2 localCoord_Widget (const iWidget *, iInt2 coord); | 135 | iInt2 localCoord_Widget (const iWidget *, iInt2 coord); |
136 | iBool contains_Widget (const iWidget *, iInt2 coord); | 136 | iBool contains_Widget (const iWidget *, iInt2 coord); |
137 | iAny * findChild_Widget (const iWidget *, const char *id); | 137 | iAny * findChild_Widget (const iWidget *, const char *id); |
138 | iAny * findParentClass_Widget(const iWidget *, const iAnyClass *class); | ||
138 | iAny * findFocusable_Widget(const iWidget *startFrom, enum iWidgetFocusDir focusDir); | 139 | iAny * findFocusable_Widget(const iWidget *startFrom, enum iWidgetFocusDir focusDir); |
139 | size_t childCount_Widget (const iWidget *); | 140 | size_t childCount_Widget (const iWidget *); |
140 | void draw_Widget (const iWidget *); | 141 | void draw_Widget (const iWidget *); |
142 | void drawBackground_Widget(const iWidget *); | ||
143 | void drawChildren_Widget (const iWidget *); | ||
141 | 144 | ||
142 | iLocalDef iObjectList *children_Widget(iAnyObject *d) { | 145 | iLocalDef iObjectList *children_Widget(iAnyObject *d) { |
143 | iAssert(isInstance_Object(d, &Class_Widget)); | 146 | iAssert(isInstance_Object(d, &Class_Widget)); |