From e4448f5881dea9ff6e0e181452d10b48186ae98e Mon Sep 17 00:00:00 2001 From: vurtun Date: Mon, 15 Feb 2016 19:03:21 +0100 Subject: [PATCH] Added additional widget utility functions --- demo/overview.c | 18 +++---- zahnrad.c | 124 ++++++++++++++++++++++++++++++++++++++++++++++-- zahnrad.h | 68 ++++++++++++++++---------- 3 files changed, 173 insertions(+), 37 deletions(-) diff --git a/demo/overview.c b/demo/overview.c index 6e7f561..8e53e29 100644 --- a/demo/overview.c +++ b/demo/overview.c @@ -439,7 +439,7 @@ demo_window(struct zr_context *ctx) id = 0; index = -1; zr_layout_row_dynamic(ctx, 100, 1); - zr_layout_peek(&bounds, ctx); + bounds = zr_widget_bounds(ctx); zr_chart_begin(ctx, ZR_CHART_LINES, 32, -1.0f, 1.0f); for (i = 0; i < 32; ++i) { zr_flags res = zr_chart_push(ctx, (float)cos(id)); @@ -464,7 +464,7 @@ demo_window(struct zr_context *ctx) /* column chart */ zr_layout_row_dynamic(ctx, 100, 1); - zr_layout_peek(&bounds, ctx); + bounds = zr_widget_bounds(ctx); zr_chart_begin(ctx, ZR_CHART_COLUMN, 32, 0.0f, 1.0f); for (i = 0; i < 32; ++i) { zr_flags res = zr_chart_push(ctx, (float)fabs(sin(id))); @@ -498,7 +498,7 @@ demo_window(struct zr_context *ctx) /* menu contextual */ zr_layout_row_static(ctx, 30, 150, 1); - zr_layout_peek(&bounds, ctx); + bounds = zr_widget_bounds(ctx); zr_label(ctx, "Right click me for menu", ZR_TEXT_LEFT); if (zr_contextual_begin(ctx, &menu, 0, zr_vec2(100, 300), bounds)) { @@ -523,7 +523,7 @@ demo_window(struct zr_context *ctx) zr_layout_row_push(ctx, 100); zr_label(ctx, "Right Click here:", ZR_TEXT_LEFT); zr_layout_row_push(ctx, 50); - zr_layout_peek(&bounds, ctx); + bounds = zr_widget_bounds(ctx); zr_button_color(ctx, color, ZR_BUTTON_DEFAULT); zr_layout_row_end(ctx); @@ -567,7 +567,7 @@ demo_window(struct zr_context *ctx) /* tooltip */ zr_layout_row_static(ctx, 30, 150, 1); - zr_layout_peek(&bounds, ctx); + bounds = zr_widget_bounds(ctx); zr_label(ctx, "Hover me for tooltip", ZR_TEXT_LEFT); if (zr_input_is_mouse_hovering_rect(in, bounds)) zr_tooltip(ctx, "This is a tooltip"); @@ -830,7 +830,7 @@ demo_window(struct zr_context *ctx) } /* scaler */ - zr_layout_peek(&bounds, ctx); + bounds = zr_widget_bounds(ctx); zr_spacing(ctx, 1); if ((zr_input_is_mouse_hovering_rect(in, bounds) || zr_input_is_mouse_prev_hovering_rect(in, bounds)) && @@ -853,7 +853,7 @@ demo_window(struct zr_context *ctx) } /* scaler */ - zr_layout_peek(&bounds, ctx); + bounds = zr_widget_bounds(ctx); zr_spacing(ctx, 1); if ((zr_input_is_mouse_hovering_rect(in, bounds) || zr_input_is_mouse_prev_hovering_rect(in, bounds)) && @@ -911,7 +911,7 @@ demo_window(struct zr_context *ctx) /* scaler */ zr_layout_row_dynamic(ctx, 8, 1); - zr_layout_peek(&bounds, ctx); + bounds = zr_widget_bounds(ctx); zr_spacing(ctx, 1); if ((zr_input_is_mouse_hovering_rect(in, bounds) || zr_input_is_mouse_prev_hovering_rect(in, bounds)) && @@ -937,7 +937,7 @@ demo_window(struct zr_context *ctx) { /* scaler */ zr_layout_row_dynamic(ctx, 8, 1); - zr_layout_peek(&bounds, ctx); + bounds = zr_widget_bounds(ctx); if ((zr_input_is_mouse_hovering_rect(in, bounds) || zr_input_is_mouse_prev_hovering_rect(in, bounds)) && zr_input_is_mouse_down(in, ZR_BUTTON_LEFT)) diff --git a/zahnrad.c b/zahnrad.c index dab2dc0..620182c 100644 --- a/zahnrad.c +++ b/zahnrad.c @@ -7477,6 +7477,30 @@ zr_window_has_focus(const struct zr_context *ctx) return ctx->current == ctx->active; } +int +zr_window_is_hovered(struct zr_context *ctx) +{ + ZR_ASSERT(ctx); + ZR_ASSERT(ctx->current); + if (!ctx || !ctx->current) return 0; + return zr_input_is_mouse_hovering_rect(&ctx->input, ctx->current->bounds); +} + +int +zr_window_is_any_hovered(struct zr_context *ctx) +{ + struct zr_window *iter; + ZR_ASSERT(ctx); + if (!ctx) return 0; + iter = ctx->begin; + while (iter) { + if (zr_input_is_mouse_hovering_rect(&ctx->input, iter->bounds)) + return 1; + iter = iter->next; + } + return 0; +} + int zr_window_is_collapsed(struct zr_context *ctx, const char *name) { @@ -7573,23 +7597,39 @@ zr_window_collapse(struct zr_context *ctx, const char *name, void zr_window_collapse_if(struct zr_context *ctx, const char *name, enum zr_collapse_states c, int cond) +{ + ZR_ASSERT(ctx); + if (!ctx || !cond) return; + zr_window_collapse(ctx, name, c); +} + +void +zr_window_show(struct zr_context *ctx, const char *name, enum zr_show_states s) { int title_len; zr_hash title_hash; struct zr_window *win; ZR_ASSERT(ctx); - if (!ctx || !cond) return; + if (!ctx) return; title_len = (int)zr_strsiz(name); title_hash = zr_murmur_hash(name, (int)title_len, ZR_WINDOW_TITLE); win = zr_find_window(ctx, title_hash); if (!win) return; - - if (c == ZR_MINIMIZED) + if (s == ZR_HIDDEN) win->flags |= ZR_WINDOW_HIDDEN; else win->flags &= ~(zr_flags)ZR_WINDOW_HIDDEN; } +void +zr_window_show_if(struct zr_context *ctx, const char *name, + enum zr_show_states s, int cond) +{ + ZR_ASSERT(ctx); + if (!ctx || !cond) return; + zr_window_show(ctx, name, s); +} + void zr_window_set_focus(struct zr_context *ctx, const char *name) { @@ -8746,7 +8786,7 @@ zr_panel_alloc_space(struct zr_rect *bounds, const struct zr_context *ctx) layout->row.index++; } -void +static void zr_layout_peek(struct zr_rect *bounds, struct zr_context *ctx) { float y; @@ -8921,6 +8961,82 @@ zr_layout_pop(struct zr_context *ctx) * WIDGETS * * --------------------------------------------------------------*/ +struct zr_rect +zr_widget_bounds(struct zr_context *ctx) +{ + struct zr_rect bounds; + ZR_ASSERT(ctx); + ZR_ASSERT(ctx->current); + if (!ctx || !ctx->current) + return zr_rect(0,0,0,0); + zr_layout_peek(&bounds, ctx); + return bounds; +} + +struct zr_vec2 +zr_widget_position(struct zr_context *ctx) +{ + struct zr_rect bounds; + ZR_ASSERT(ctx); + ZR_ASSERT(ctx->current); + if (!ctx || !ctx->current) return zr_vec2(0,0); + zr_layout_peek(&bounds, ctx); + return zr_vec2(bounds.x, bounds.y); +} + +struct zr_vec2 +zr_widget_size(struct zr_context *ctx) +{ + struct zr_rect bounds; + ZR_ASSERT(ctx); + ZR_ASSERT(ctx->current); + if (!ctx || !ctx->current) return zr_vec2(0,0); + zr_layout_peek(&bounds, ctx); + return zr_vec2(bounds.w, bounds.h); +} + +int +zr_widget_is_hovered(struct zr_context *ctx) +{ + int ret; + struct zr_rect bounds; + ZR_ASSERT(ctx); + ZR_ASSERT(ctx->current); + if (!ctx || !ctx->current) return 0; + zr_layout_peek(&bounds, ctx); + ret = (ctx->active == ctx->current); + ret = ret && zr_input_is_mouse_hovering_rect(&ctx->input, bounds); + return ret; +} + +int +zr_widget_is_mouse_clicked(struct zr_context *ctx, enum zr_buttons btn) +{ + int ret; + struct zr_rect bounds; + ZR_ASSERT(ctx); + ZR_ASSERT(ctx->current); + if (!ctx || !ctx->current) return 0; + zr_layout_peek(&bounds, ctx); + ret = (ctx->active == ctx->current); + ret = ret && zr_input_mouse_clicked(&ctx->input, btn, bounds); + return ret; +} + +int +zr_widget_has_mouse_click_down(struct zr_context *ctx, enum zr_buttons btn, int down) +{ + int ret; + struct zr_rect bounds; + ZR_ASSERT(ctx); + ZR_ASSERT(ctx->current); + if (!ctx || !ctx->current) return 0; + zr_layout_peek(&bounds, ctx); + ret = (ctx->active == ctx->current); + ret = ret && zr_input_has_mouse_click_down_in_rect(&ctx->input, btn, bounds, down); + return ret; +} + void zr_spacing(struct zr_context *ctx, int cols) { diff --git a/zahnrad.h b/zahnrad.h index 904114f..edd4a69 100644 --- a/zahnrad.h +++ b/zahnrad.h @@ -913,7 +913,7 @@ enum zr_style_components { /* default all properites inside the configuration struct */ ZR_DEFAULT_ROUNDING = 0x04, /* default all rounding values inside the configuration struct */ - ZR_DEFAULT_ALL = 0xFFFF + ZR_DEFAULT_ALL = ZR_DEFAULT_COLOR|ZR_DEFAULT_PROPERTIES|ZR_DEFAULT_ROUNDING /* default the complete configuration struct */ }; @@ -1099,6 +1099,11 @@ enum zr_collapse_states { ZR_MAXIMIZED = zr_true }; +enum zr_show_states { + ZR_HIDDEN = zr_false, + ZR_SHOWN = zr_true +}; + enum zr_widget_state { ZR_WIDGET_INVALID, /* The widget cannot be seen and is completly out of view */ @@ -1308,6 +1313,21 @@ struct zr_clipboard { /* copy callback for the edit box */ }; +struct zr_convert_config { + float global_alpha; + /* global alpha modifier */ + float line_thickness; + /* line thickness should generally default to 1*/ + enum zr_anti_aliasing line_AA; + /* line anti-aliasing flag can be turned off if you are thight on memory */ + enum zr_anti_aliasing shape_AA; + /* shape anti-aliasing flag can be turned off if you are thight on memory */ + unsigned int circle_segment_count; + /* number of segments used for circle and curves: default to 22 */ + struct zr_draw_null_texture null; + /* handle to texture with a white pixel to draw text */ +}; + struct zr_canvas { float global_alpha; /* alpha modifier for all shapes */ @@ -1389,9 +1409,11 @@ void zr_free(struct zr_context*); void zr_set_user_data(struct zr_context*, zr_handle handle); #endif -/* window */ +/*-------------------------------------------------------------- + * WINDOW + * -------------------------------------------------------------*/ int zr_begin(struct zr_context*, struct zr_panel*, const char *title, - struct zr_rect bounds, unsigned int flags); + struct zr_rect bounds, zr_flags flags); void zr_end(struct zr_context*); struct zr_rect zr_window_get_bounds(const struct zr_context*); @@ -1405,6 +1427,8 @@ struct zr_vec2 zr_window_get_content_region_max(struct zr_context*); struct zr_vec2 zr_window_get_content_region_size(struct zr_context*); struct zr_command_buffer* zr_window_get_canvas(struct zr_context*); int zr_window_has_focus(const struct zr_context*); +int zr_window_is_hovered(struct zr_context*); +int zr_window_is_any_hovered(struct zr_context*); int zr_window_is_collapsed(struct zr_context*, const char *name); int zr_window_is_closed(struct zr_context*, const char *name); int zr_window_is_active(struct zr_context*, const char *name); @@ -1417,6 +1441,9 @@ void zr_window_collapse(struct zr_context*, const char *name, enum zr_collapse_states); void zr_window_collapse_if(struct zr_context*, const char *name, enum zr_collapse_states, int cond); +void zr_window_show(struct zr_context*, const char *name, enum zr_show_states); +void zr_window_show_if(struct zr_context*, const char *name, + enum zr_show_states, int cond); /*-------------------------------------------------------------- * DRAWING @@ -1427,22 +1454,6 @@ void zr_window_collapse_if(struct zr_context*, const char *name, const struct zr_command* zr__next(struct zr_context*, const struct zr_command*); const struct zr_command* zr__begin(struct zr_context*); -/* vertex command drawing */ -struct zr_convert_config { - float global_alpha; - /* global alpha modifier */ - float line_thickness; - /* line thickness should generally default to 1*/ - enum zr_anti_aliasing line_AA; - /* line anti-aliasing flag can be turned off if you are thight on memory */ - enum zr_anti_aliasing shape_AA; - /* shape anti-aliasing flag can be turned off if you are thight on memory */ - unsigned int circle_segment_count; - /* number of segments used for circle and curves: default to 22 */ - struct zr_draw_null_texture null; - /* handle to texture with a white pixel to draw text */ -}; - void zr_convert(struct zr_context*, struct zr_buffer *cmds, struct zr_buffer *vertices, struct zr_buffer *elements, const struct zr_convert_config*); @@ -1457,6 +1468,7 @@ const struct zr_draw_command* zr__draw_next(const struct zr_draw_command*, /*-------------------------------------------------------------- * INPUT * -------------------------------------------------------------*/ +/* input acquiring */ void zr_input_begin(struct zr_context*); void zr_input_motion(struct zr_context*, int x, int y); void zr_input_key(struct zr_context*, enum zr_keys, int down); @@ -1498,10 +1510,8 @@ void zr_reset_font_height(struct zr_context*); void zr_reset(struct zr_context*); /*-------------------------------------------------------------- - * Layout + * LAYOUT * -------------------------------------------------------------*/ -void zr_layout_peek(struct zr_rect *bounds, struct zr_context*); - /* columns based layouting with generated position and width and fixed height*/ void zr_layout_row_dynamic(struct zr_context*, float height, int cols); void zr_layout_row_static(struct zr_context*, float height, int item_width, int cols); @@ -1536,10 +1546,19 @@ int zr_layout_push(struct zr_context*, enum zr_layout_node_type, const char *tit void zr_layout_pop(struct zr_context*); /*-------------------------------------------------------------- - * Widgets + * WIDGETS * -------------------------------------------------------------*/ +/* base widget calls for custom widgets (is used by all widgets internally) */ enum zr_widget_state zr_widget(struct zr_rect*, const struct zr_context*); enum zr_widget_state zr_widget_fitting(struct zr_rect*, struct zr_context*); + +/* utilities (working on the next widget) */ +struct zr_rect zr_widget_bounds(struct zr_context*); +struct zr_vec2 zr_widget_position(struct zr_context*); +struct zr_vec2 zr_widget_size(struct zr_context*); +int zr_widget_is_hovered(struct zr_context*); +int zr_widget_is_mouse_clicked(struct zr_context*, enum zr_buttons); +int zr_widget_has_mouse_click_down(struct zr_context*, enum zr_buttons, int down); void zr_spacing(struct zr_context*, int cols); void zr_seperator(struct zr_context*); @@ -1603,8 +1622,9 @@ zr_flags zr_chart_push(struct zr_context*, float); void zr_chart_end(struct zr_context*); /*-------------------------------------------------------------- - * Popups + * POPUPS * -------------------------------------------------------------*/ +/* normal blocking popups */ int zr_popup_begin(struct zr_context*, struct zr_panel*, enum zr_popup_type, const char*, zr_flags, struct zr_rect bounds); void zr_popup_close(struct zr_context*);