Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions demo/common/overview.c
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,9 @@ overview(struct nk_context *ctx)
const struct nk_input *in = &ctx->input;
struct nk_rect bounds;

/* seconds */
static float delay_timer = 0.0;

/* menu contextual */
nk_layout_row_static(ctx, 30, 160, 1);
bounds = nk_widget_bounds(ctx);
Expand Down Expand Up @@ -846,6 +849,17 @@ overview(struct nk_context *ctx)
if (nk_input_is_mouse_hovering_rect(in, bounds)) {
nk_tooltip(ctx, "This is a default tooltip");
}

bounds = nk_widget_bounds(ctx);
nk_label(ctx, "Hover motionless for a default delayed tooltip", NK_TEXT_LEFT);
nk_do_tooltip_delay(ctx, "This is a delayed tooltip", bounds, &delay_timer);

bounds = nk_widget_bounds(ctx);
nk_label(ctx, "Hover motionless longer a custom delayed tooltip", NK_TEXT_LEFT);
if (nk_input_is_mouse_hovering_still_delay_rect(ctx, bounds, &delay_timer, 1.5)) {
nk_tooltip(ctx, "This is a custom delayed tooltip");
}

bounds = nk_widget_bounds(ctx);
nk_label(ctx, "Hover for Gnome-like tooltip", NK_TEXT_LEFT);
if (nk_input_is_mouse_hovering_rect(in, bounds)) {
Expand Down
76 changes: 75 additions & 1 deletion nuklear.h
Original file line number Diff line number Diff line change
Expand Up @@ -3823,6 +3823,8 @@ NK_API void nk_contextual_end(struct nk_context*);
* ============================================================================= */
NK_API void nk_tooltip(struct nk_context*, const char*);
NK_API void nk_tooltip_offset(struct nk_context *ctx, const char *text, enum nk_tooltip_pos position, struct nk_vec2 offset);
NK_API void nk_do_tooltip(struct nk_context*, const char*, struct nk_rect);
NK_API void nk_do_tooltip_delay(struct nk_context*, const char*, struct nk_rect, float*);
#ifdef NK_INCLUDE_STANDARD_VARARGS
NK_API void nk_tooltipf(struct nk_context*, NK_PRINTF_FORMAT_STRING const char*, ...) NK_PRINTF_VARARG_FUNC(2);
NK_API void nk_tooltipfv(struct nk_context*, NK_PRINTF_FORMAT_STRING const char*, va_list) NK_PRINTF_VALIST_FUNC(2);
Expand Down Expand Up @@ -4948,6 +4950,9 @@ NK_API nk_bool nk_input_is_mouse_click_down_in_rect(const struct nk_input *i, en
NK_API nk_bool nk_input_any_mouse_click_in_rect(const struct nk_input*, struct nk_rect);
NK_API nk_bool nk_input_is_mouse_prev_hovering_rect(const struct nk_input*, struct nk_rect);
NK_API nk_bool nk_input_is_mouse_hovering_rect(const struct nk_input*, struct nk_rect);
NK_API nk_bool nk_input_is_mouse_hovering_still_rect(const struct nk_input*, struct nk_rect);
NK_API nk_bool nk_input_is_mouse_hovering_delay_rect(const struct nk_context*, struct nk_rect, float*, float);
NK_API nk_bool nk_input_is_mouse_hovering_still_delay_rect(const struct nk_context*, struct nk_rect, float*, float);
NK_API nk_bool nk_input_is_mouse_moved(const struct nk_input*);
NK_API nk_bool nk_input_mouse_clicked(const struct nk_input*, enum nk_buttons, struct nk_rect);
NK_API nk_bool nk_input_is_mouse_down(const struct nk_input*, enum nk_buttons);
Expand Down Expand Up @@ -5583,6 +5588,7 @@ struct nk_style_window {

enum nk_tooltip_pos tooltip_origin;
struct nk_vec2 tooltip_offset;
float tooltip_delay;
};

struct nk_style {
Expand Down Expand Up @@ -18437,6 +18443,55 @@ nk_input_is_mouse_hovering_rect(const struct nk_input *i, struct nk_rect rect)
return NK_INBOX(i->mouse.pos.x, i->mouse.pos.y, rect.x, rect.y, rect.w, rect.h);
}
NK_API nk_bool
nk_input_is_mouse_hovering_still_rect(const struct nk_input *i, struct nk_rect rect)
{
if (!i) return nk_false;
return (NK_INBOX(i->mouse.pos.x, i->mouse.pos.y, rect.x, rect.y, rect.w, rect.h) &&
!nk_input_is_mouse_moved(i));
}
NK_API nk_bool
nk_input_is_mouse_hovering_delay_rect(const struct nk_context *ctx, struct nk_rect rect, float* timer, float delay)
{
NK_ASSERT(ctx);
if (!ctx) {
return nk_false;
} else {
const struct nk_input* i = &ctx->input;
if (NK_INBOX(i->mouse.pos.x, i->mouse.pos.y, rect.x, rect.y, rect.w, rect.h)) {
*timer += ctx->delta_time_seconds;
return *timer >= delay;
} else if (NK_INBOX(i->mouse.prev.x, i->mouse.prev.y, rect.x, rect.y, rect.w, rect.h)) {
*timer = 0;
}
return nk_false;
}

}
NK_API nk_bool
nk_input_is_mouse_hovering_still_delay_rect(const struct nk_context *ctx, struct nk_rect rect, float* timer, float delay)
{
NK_ASSERT(ctx);
if (!ctx) {
return nk_false;
} else {
const struct nk_input* i = &ctx->input;
if (NK_INBOX(i->mouse.pos.x, i->mouse.pos.y, rect.x, rect.y, rect.w, rect.h)) {
/* once it triggers, moving within the bounds should not make it disappear */
if (*timer >= delay) {
return nk_true;
}
if (!nk_input_is_mouse_moved(i)) {
*timer += ctx->delta_time_seconds;
return *timer >= delay;
}
*timer = 0;
} else if (NK_INBOX(i->mouse.prev.x, i->mouse.prev.y, rect.x, rect.y, rect.w, rect.h)) {
*timer = 0;
}
return nk_false;
}
}
NK_API nk_bool
nk_input_is_mouse_prev_hovering_rect(const struct nk_input *i, struct nk_rect rect)
{
if (!i) return nk_false;
Expand Down Expand Up @@ -19232,13 +19287,14 @@ nk_style_from_table(struct nk_context *ctx, const struct nk_color *table)
win->tooltip_padding = nk_vec2(4,4);

/* default tooltip just down and to the right of the cursor
* so it doesn't cover the text
* so it doesn't cover the text and a default delay of 0.5 seconds
*
* TODO might be worth consolidating tooltip styling
* into its own style structure, though it is a
* type of window...*/
win->tooltip_origin = NK_TOP_LEFT;
win->tooltip_offset = nk_vec2(12, 12);
win->tooltip_delay = 0.5f;
}
NK_API void
nk_style_set_font(struct nk_context *ctx, const struct nk_user_font *font)
Expand Down Expand Up @@ -30768,6 +30824,24 @@ nk_tooltip_end(struct nk_context *ctx)
nk_popup_end(ctx);
}

NK_API void
nk_do_tooltip(struct nk_context* ctx, const char* text, struct nk_rect bounds)
{
NK_ASSERT(ctx);
if (nk_input_is_mouse_hovering_rect(&ctx->input, bounds)) {
nk_tooltip_offset(ctx, text, ctx->style.window.tooltip_origin, ctx->style.window.tooltip_offset);
}
}

NK_API void
nk_do_tooltip_delay(struct nk_context* ctx, const char* text, struct nk_rect bounds, float* timer)
{
NK_ASSERT(ctx);
if (nk_input_is_mouse_hovering_still_delay_rect(ctx, bounds, timer, ctx->style.window.tooltip_delay)) {
nk_tooltip_offset(ctx, text, ctx->style.window.tooltip_origin, ctx->style.window.tooltip_offset);
}
}

NK_API void
nk_tooltip_offset(struct nk_context *ctx, const char *text, enum nk_tooltip_pos position, struct nk_vec2 offset)
{
Expand Down
6 changes: 6 additions & 0 deletions src/nuklear.h
Original file line number Diff line number Diff line change
Expand Up @@ -3607,6 +3607,8 @@ NK_API void nk_contextual_end(struct nk_context*);
* ============================================================================= */
NK_API void nk_tooltip(struct nk_context*, const char*);
NK_API void nk_tooltip_offset(struct nk_context *ctx, const char *text, enum nk_tooltip_pos position, struct nk_vec2 offset);
NK_API void nk_do_tooltip(struct nk_context*, const char*, struct nk_rect);
NK_API void nk_do_tooltip_delay(struct nk_context*, const char*, struct nk_rect, float*);
#ifdef NK_INCLUDE_STANDARD_VARARGS
NK_API void nk_tooltipf(struct nk_context*, NK_PRINTF_FORMAT_STRING const char*, ...) NK_PRINTF_VARARG_FUNC(2);
NK_API void nk_tooltipfv(struct nk_context*, NK_PRINTF_FORMAT_STRING const char*, va_list) NK_PRINTF_VALIST_FUNC(2);
Expand Down Expand Up @@ -4732,6 +4734,9 @@ NK_API nk_bool nk_input_is_mouse_click_down_in_rect(const struct nk_input *i, en
NK_API nk_bool nk_input_any_mouse_click_in_rect(const struct nk_input*, struct nk_rect);
NK_API nk_bool nk_input_is_mouse_prev_hovering_rect(const struct nk_input*, struct nk_rect);
NK_API nk_bool nk_input_is_mouse_hovering_rect(const struct nk_input*, struct nk_rect);
NK_API nk_bool nk_input_is_mouse_hovering_still_rect(const struct nk_input*, struct nk_rect);
NK_API nk_bool nk_input_is_mouse_hovering_delay_rect(const struct nk_context*, struct nk_rect, float*, float);
NK_API nk_bool nk_input_is_mouse_hovering_still_delay_rect(const struct nk_context*, struct nk_rect, float*, float);
NK_API nk_bool nk_input_is_mouse_moved(const struct nk_input*);
NK_API nk_bool nk_input_mouse_clicked(const struct nk_input*, enum nk_buttons, struct nk_rect);
NK_API nk_bool nk_input_is_mouse_down(const struct nk_input*, enum nk_buttons);
Expand Down Expand Up @@ -5367,6 +5372,7 @@ struct nk_style_window {

enum nk_tooltip_pos tooltip_origin;
struct nk_vec2 tooltip_offset;
float tooltip_delay;
};

struct nk_style {
Expand Down
49 changes: 49 additions & 0 deletions src/nuklear_input.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,55 @@ nk_input_is_mouse_hovering_rect(const struct nk_input *i, struct nk_rect rect)
return NK_INBOX(i->mouse.pos.x, i->mouse.pos.y, rect.x, rect.y, rect.w, rect.h);
}
NK_API nk_bool
nk_input_is_mouse_hovering_still_rect(const struct nk_input *i, struct nk_rect rect)
{
if (!i) return nk_false;
return (NK_INBOX(i->mouse.pos.x, i->mouse.pos.y, rect.x, rect.y, rect.w, rect.h) &&
!nk_input_is_mouse_moved(i));
}
NK_API nk_bool
nk_input_is_mouse_hovering_delay_rect(const struct nk_context *ctx, struct nk_rect rect, float* timer, float delay)
{
NK_ASSERT(ctx);
if (!ctx) {
return nk_false;
} else {
const struct nk_input* i = &ctx->input;
if (NK_INBOX(i->mouse.pos.x, i->mouse.pos.y, rect.x, rect.y, rect.w, rect.h)) {
*timer += ctx->delta_time_seconds;
return *timer >= delay;
} else if (NK_INBOX(i->mouse.prev.x, i->mouse.prev.y, rect.x, rect.y, rect.w, rect.h)) {
*timer = 0;
}
return nk_false;
}

}
NK_API nk_bool
nk_input_is_mouse_hovering_still_delay_rect(const struct nk_context *ctx, struct nk_rect rect, float* timer, float delay)
{
NK_ASSERT(ctx);
if (!ctx) {
return nk_false;
} else {
const struct nk_input* i = &ctx->input;
if (NK_INBOX(i->mouse.pos.x, i->mouse.pos.y, rect.x, rect.y, rect.w, rect.h)) {
/* once it triggers, moving within the bounds should not make it disappear */
if (*timer >= delay) {
return nk_true;
}
if (!nk_input_is_mouse_moved(i)) {
*timer += ctx->delta_time_seconds;
return *timer >= delay;
}
*timer = 0;
} else if (NK_INBOX(i->mouse.prev.x, i->mouse.prev.y, rect.x, rect.y, rect.w, rect.h)) {
*timer = 0;
}
return nk_false;
}
}
NK_API nk_bool
nk_input_is_mouse_prev_hovering_rect(const struct nk_input *i, struct nk_rect rect)
{
if (!i) return nk_false;
Expand Down
3 changes: 2 additions & 1 deletion src/nuklear_style.c
Original file line number Diff line number Diff line change
Expand Up @@ -722,13 +722,14 @@ nk_style_from_table(struct nk_context *ctx, const struct nk_color *table)
win->tooltip_padding = nk_vec2(4,4);

/* default tooltip just down and to the right of the cursor
* so it doesn't cover the text
* so it doesn't cover the text and a default delay of 0.5 seconds
*
* TODO might be worth consolidating tooltip styling
* into its own style structure, though it is a
* type of window...*/
win->tooltip_origin = NK_TOP_LEFT;
win->tooltip_offset = nk_vec2(12, 12);
win->tooltip_delay = 0.5f;
}
NK_API void
nk_style_set_font(struct nk_context *ctx, const struct nk_user_font *font)
Expand Down
18 changes: 18 additions & 0 deletions src/nuklear_tooltip.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,24 @@ nk_tooltip_end(struct nk_context *ctx)
nk_popup_end(ctx);
}

NK_API void
nk_do_tooltip(struct nk_context* ctx, const char* text, struct nk_rect bounds)
{
NK_ASSERT(ctx);
if (nk_input_is_mouse_hovering_rect(&ctx->input, bounds)) {
nk_tooltip_offset(ctx, text, ctx->style.window.tooltip_origin, ctx->style.window.tooltip_offset);
}
}

NK_API void
nk_do_tooltip_delay(struct nk_context* ctx, const char* text, struct nk_rect bounds, float* timer)
{
NK_ASSERT(ctx);
if (nk_input_is_mouse_hovering_still_delay_rect(ctx, bounds, timer, ctx->style.window.tooltip_delay)) {
nk_tooltip_offset(ctx, text, ctx->style.window.tooltip_origin, ctx->style.window.tooltip_offset);
}
}

NK_API void
nk_tooltip_offset(struct nk_context *ctx, const char *text, enum nk_tooltip_pos position, struct nk_vec2 offset)
{
Expand Down
Loading