From c98153b59a45d51722b5ec1c2f4b6422f1dd7dc8 Mon Sep 17 00:00:00 2001 From: vurtun Date: Wed, 20 Jan 2016 19:36:40 +0100 Subject: [PATCH] removed need to pass string id to combobox --- demo/demo.c | 12 ++++++------ example/demo/demo.c | 6 +++--- zahnrad.c | 45 ++++++++++++++++++++++++++++----------------- zahnrad.h | 8 ++++---- 4 files changed, 41 insertions(+), 30 deletions(-) diff --git a/demo/demo.c b/demo/demo.c index 909092b..ee58742 100644 --- a/demo/demo.c +++ b/demo/demo.c @@ -260,7 +260,7 @@ demo_window(struct zr_layout *layout, struct zr_context *ctx, enum theme *theme) zr_layout_row_static(ctx, 30, 80, 2); zr_label(ctx, "Theme:", ZR_TEXT_LEFT); - if (zr_combo_begin_text(ctx, &combo, "themes", themes[*theme], 200)) { + if (zr_combo_begin_text(ctx, &combo, themes[*theme], 200)) { zr_layout_row_dynamic(ctx, 25, 1); *theme = zr_combo_item(ctx, themes[THEME_BLACK], ZR_TEXT_CENTERED) ? THEME_BLACK : *theme; *theme = zr_combo_item(ctx, themes[THEME_WHITE], ZR_TEXT_CENTERED) ? THEME_WHITE : *theme; @@ -464,7 +464,7 @@ demo_window(struct zr_layout *layout, struct zr_context *ctx, enum theme *theme) /* default combobox */ zr_layout_row_static(ctx, 30, 200, 1); - if (zr_combo_begin_text(ctx, &combo, "Weapons", weapons[current_weapon], 200)) { + if (zr_combo_begin_text(ctx, &combo, weapons[current_weapon], 200)) { size_t i = 0; zr_layout_row_dynamic(ctx, 25, 1); for (i = 0; i < LEN(weapons); ++i) { @@ -475,7 +475,7 @@ demo_window(struct zr_layout *layout, struct zr_context *ctx, enum theme *theme) } /* slider color combobox */ - if (zr_combo_begin_color(ctx, &combo, "Color", combo_color, 200)) { + if (zr_combo_begin_color(ctx, &combo, combo_color, 200)) { float ratios[] = {0.15f, 0.85f}; zr_layout_row(ctx, ZR_DYNAMIC, 30, 2, ratios); zr_label(ctx, "R", ZR_TEXT_LEFT); @@ -492,7 +492,7 @@ demo_window(struct zr_layout *layout, struct zr_context *ctx, enum theme *theme) /* progressbar combobox */ sum = x + y + z + w; sprintf(buffer, "%lu", sum); - if (zr_combo_begin_text(ctx, &combo, "Progressbar", buffer, 200)) { + if (zr_combo_begin_text(ctx, &combo, buffer, 200)) { zr_layout_row_dynamic(ctx, 30, 1); zr_progress(ctx, &x, 100, ZR_MODIFIABLE); zr_progress(ctx, &y, 100, ZR_MODIFIABLE); @@ -504,7 +504,7 @@ demo_window(struct zr_layout *layout, struct zr_context *ctx, enum theme *theme) /* checkbox combobox */ sum = (size_t)(check_values[0] + check_values[1] + check_values[2] + check_values[3] + check_values[4]); sprintf(buffer, "%lu", sum); - if (zr_combo_begin_text(ctx, &combo, "Checkbox", buffer, 200)) { + if (zr_combo_begin_text(ctx, &combo, buffer, 200)) { zr_layout_row_dynamic(ctx, 30, 1); zr_checkbox(ctx, weapons[0], &check_values[0]); zr_checkbox(ctx, weapons[1], &check_values[1]); @@ -515,7 +515,7 @@ demo_window(struct zr_layout *layout, struct zr_context *ctx, enum theme *theme) /* chart combobox */ sprintf(buffer, "%.1f", chart_selection); - if (zr_combo_begin_text(ctx, &combo, "Charts", buffer, 250)) { + if (zr_combo_begin_text(ctx, &combo, buffer, 250)) { size_t i = 0; static const float values[]={30.0f,15.0f,25.0f,10.0f,20.0f,40.0f}; zr_layout_row_dynamic(ctx, 150, 1); diff --git a/example/demo/demo.c b/example/demo/demo.c index 753f32a..c7784f9 100644 --- a/example/demo/demo.c +++ b/example/demo/demo.c @@ -390,7 +390,7 @@ basic_demo(struct zr_context *ctx, struct icons *img) *------------------------------------------------*/ ui_header(ctx, "Combo box"); ui_widget(ctx, 40, 22); - if (zr_combo_begin_text(ctx, &combo, "items", items[selected_item], 200)) { + if (zr_combo_begin_text(ctx, &combo, items[selected_item], 200)) { zr_layout_row_dynamic(ctx, 35, 1); for (i = 0; i < 3; ++i) if (zr_combo_item(ctx, items[i], ZR_TEXT_LEFT)) @@ -399,7 +399,7 @@ basic_demo(struct zr_context *ctx, struct icons *img) } ui_widget(ctx, 40, 22); - if (zr_combo_begin_icon(ctx, &combo, "pictures", items[selected_icon], zr_image_id(img->images[selected_icon]), 200)) { + if (zr_combo_begin_icon(ctx, &combo, items[selected_icon], zr_image_id(img->images[selected_icon]), 200)) { zr_layout_row_dynamic(ctx, 35, 1); for (i = 0; i < 3; ++i) if (zr_combo_item_icon(ctx, zr_image_id(img->images[i]), items[i], ZR_TEXT_RIGHT)) @@ -477,7 +477,7 @@ grid_demo(struct zr_context *ctx) zr_checkbox(ctx, "Check me", &check); zr_label(ctx, "Combobox:", ZR_TEXT_RIGHT); - if (zr_combo_begin_text(ctx, &combo, "combo", items[selected_item], 200)) { + if (zr_combo_begin_text(ctx, &combo, items[selected_item], 200)) { zr_layout_row_dynamic(ctx, 30, 1); for (i = 0; i < 3; ++i) if (zr_combo_item(ctx, items[i], ZR_TEXT_LEFT)) diff --git a/zahnrad.c b/zahnrad.c index 5a178f0..597199c 100644 --- a/zahnrad.c +++ b/zahnrad.c @@ -107,6 +107,7 @@ struct zr_popup { zr_hash name; int active; + unsigned combo_count; unsigned con_count, con_old; unsigned active_con; }; @@ -153,11 +154,13 @@ struct zr_window { struct zr_command_buffer buffer; /* command buffer for queuing drawing calls */ + /* frame window state */ + struct zr_layout *layout; + /* persistent widget state */ struct zr_value property; struct zr_popup popup; struct zr_edit_state edit; - struct zr_layout *layout; struct zr_table *tables; unsigned short table_count; @@ -6835,7 +6838,7 @@ zr_begin(struct zr_context *ctx, struct zr_layout *layout, ZR_ASSERT(ctx); ZR_ASSERT(title); - ZR_ASSERT(!ctx->current); + ZR_ASSERT(!ctx->current && "if this triggers you missed a `zr_end` call"); if (!ctx || ctx->current || !title) return 0; @@ -7508,9 +7511,7 @@ zr_layout_begin(struct zr_context *ctx, const char *title) static void zr_layout_end(struct zr_context *ctx) { - struct zr_input *in; - const struct zr_style *config; - struct zr_command_buffer *out; + /* local read only style variables */ float scrollbar_size; struct zr_vec2 item_padding; struct zr_vec2 item_spacing; @@ -7518,8 +7519,12 @@ zr_layout_end(struct zr_context *ctx) struct zr_vec2 scaler_size; struct zr_rect footer = {0,0,0,0}; + /* pointers to subsystems */ + struct zr_input *in; struct zr_window *window; struct zr_layout *layout; + struct zr_command_buffer *out; + const struct zr_style *config; ZR_ASSERT(ctx); ZR_ASSERT(ctx->current); @@ -7527,7 +7532,6 @@ zr_layout_end(struct zr_context *ctx) if (!ctx || !ctx->current || !ctx->current->layout) return; - /* cache configuration data */ window = ctx->current; layout = window->layout; config = &ctx->style; @@ -7551,6 +7555,8 @@ zr_layout_end(struct zr_context *ctx) layout->height = layout->at_y - layout->bounds.y; layout->height = MIN(layout->height, layout->bounds.h); if ((layout->offset->x == 0) || (layout->flags & ZR_WINDOW_NO_SCROLLBAR)) { + /* special case for dynamic windows without horizontal scrollbar + * or hidden scrollbars */ footer.x = window->bounds.x; footer.y = window->bounds.y + layout->height + item_spacing.y; footer.w = window->bounds.w + scrollbar_size; @@ -7558,6 +7564,8 @@ zr_layout_end(struct zr_context *ctx) footer.h = 0; if ((layout->offset->x == 0) && !(layout->flags & ZR_WINDOW_NO_SCROLLBAR)) { + /* special case for windows like combobox, menu require draw call + * to fill the empty scrollbar background */ struct zr_rect bounds; bounds.x = layout->bounds.x + layout->width; bounds.y = layout->clip.y; @@ -7566,6 +7574,7 @@ zr_layout_end(struct zr_context *ctx) zr_draw_rect(out, bounds, 0, config->colors[ZR_COLOR_WINDOW]); } } else { + /* dynamic window with visible scrollbars and therefore bigger footer */ footer.x = window->bounds.x; footer.w = window->bounds.w + scrollbar_size; footer.h = layout->footer_h; @@ -7743,6 +7752,7 @@ zr_layout_end(struct zr_context *ctx) window->popup.con_old = window->popup.con_count; window->popup.con_count = 0; } + window->popup.combo_count = 0; /* helper to make sure you have a 'zr_layout_push' * for every 'zr_layout_pop' */ ZR_ASSERT(!layout->row.tree_depth); @@ -10320,13 +10330,13 @@ zr_contextual_end(struct zr_context *ctx) * --------------------------------------------------------------*/ static int zr_combo_begin(struct zr_layout *layout, struct zr_context *ctx, struct zr_window *win, - const char *id, int height, int is_clicked, struct zr_rect header) + int height, int is_clicked, struct zr_rect header) { int is_open = 0; int is_active = 0; struct zr_rect body; struct zr_window *popup; - zr_hash hash = zr_murmur_hash(id, (int)zr_strsiz(id), ZR_WINDOW_COMBO); + zr_hash hash; ZR_ASSERT(ctx); ZR_ASSERT(ctx->current); @@ -10334,12 +10344,13 @@ zr_combo_begin(struct zr_layout *layout, struct zr_context *ctx, struct zr_windo if (!ctx || !ctx->current || !ctx->current->layout) return 0; + popup = win->popup.win; body.x = header.x; body.w = header.w; body.y = header.y + header.h-1; body.h = (float)height; - popup = win->popup.win; + hash = win->popup.combo_count++; is_open = (popup && (popup->flags & ZR_WINDOW_COMBO)); is_active = (popup && (win->popup.name == hash) && win->popup.type == ZR_WINDOW_COMBO); if ((is_clicked && is_open && !is_active) || (is_open && !is_active) || @@ -10354,7 +10365,7 @@ zr_combo_begin(struct zr_layout *layout, struct zr_context *ctx, struct zr_windo int zr_combo_begin_text(struct zr_context *ctx, struct zr_layout *layout, - const char *id, const char *selected, int height) + const char *selected, int height) { const struct zr_input *in; struct zr_window *win; @@ -10411,12 +10422,12 @@ zr_combo_begin_text(struct zr_context *ctx, struct zr_layout *layout, sym.border_width = 1.0f; zr_draw_symbol(&win->buffer, &sym, bounds, &ctx->style.font); } - return zr_combo_begin(layout, ctx, win, id, height, is_active, header); + return zr_combo_begin(layout, ctx, win, height, is_active, header); } int zr_combo_begin_color(struct zr_context *ctx, struct zr_layout *layout, - const char *id, struct zr_color color, int height) + struct zr_color color, int height) { enum zr_widget_status state; enum zr_widget_state s; @@ -10471,12 +10482,12 @@ zr_combo_begin_color(struct zr_context *ctx, struct zr_layout *layout, sym.border_width = 1.0f; zr_draw_symbol(&win->buffer, &sym, bounds, &ctx->style.font); } - return zr_combo_begin(layout, ctx, win, id, height, is_active, header); + return zr_combo_begin(layout, ctx, win, height, is_active, header); } int zr_combo_begin_image(struct zr_context *ctx, struct zr_layout *layout, - const char *id, struct zr_image img, int height) + struct zr_image img, int height) { struct zr_window *win; const struct zr_input *in; @@ -10529,11 +10540,11 @@ zr_combo_begin_image(struct zr_context *ctx, struct zr_layout *layout, sym.border_width = 1.0f; zr_draw_symbol(&win->buffer, &sym, bounds, &ctx->style.font); } - return zr_combo_begin(layout, ctx, win, id, height, is_active, header); + return zr_combo_begin(layout, ctx, win, height, is_active, header); } int -zr_combo_begin_icon(struct zr_context *ctx, struct zr_layout *layout, const char *id, +zr_combo_begin_icon(struct zr_context *ctx, struct zr_layout *layout, const char *selected, struct zr_image img, int height) { struct zr_window *win; @@ -10598,7 +10609,7 @@ zr_combo_begin_icon(struct zr_context *ctx, struct zr_layout *layout, const char sym.border_width = 1.0f; zr_draw_symbol(&win->buffer, &sym, bounds, &ctx->style.font); } - return zr_combo_begin(layout, ctx, win, id, height, is_active, header); + return zr_combo_begin(layout, ctx, win, height, is_active, header); } int zr_combo_item(struct zr_context *ctx, const char *title, enum zr_text_align align) diff --git a/zahnrad.h b/zahnrad.h index 09c6853..98af585 100644 --- a/zahnrad.h +++ b/zahnrad.h @@ -1522,13 +1522,13 @@ void zr_popup_close(struct zr_context*); void zr_popup_end(struct zr_context*); /* abstract combo box */ -int zr_combo_begin_text(struct zr_context*, struct zr_layout*, const char *id, +int zr_combo_begin_text(struct zr_context*, struct zr_layout*, const char *selected, int max_height); -int zr_combo_begin_color(struct zr_context*, struct zr_layout*, const char *id, +int zr_combo_begin_color(struct zr_context*, struct zr_layout*, struct zr_color color, int max_height); -int zr_combo_begin_image(struct zr_context*, struct zr_layout*, const char *id, +int zr_combo_begin_image(struct zr_context*, struct zr_layout*, struct zr_image img, int max_height); -int zr_combo_begin_icon(struct zr_context*, struct zr_layout*, const char *id, +int zr_combo_begin_icon(struct zr_context*, struct zr_layout*, const char *selected, struct zr_image img, int height); int zr_combo_item(struct zr_context*, const char*, enum zr_text_align); int zr_combo_item_icon(struct zr_context*, struct zr_image, const char*,