@@ -516,6 +516,20 @@ enum nk_popup_type {NK_POPUP_STATIC, NK_POPUP_DYNAMIC};
516516enum nk_layout_format {NK_DYNAMIC, NK_STATIC};
517517enum nk_tree_type {NK_TREE_NODE, NK_TREE_TAB};
518518
519+ enum nk_tooltip_pos {
520+ NK_TOP_LEFT,
521+ NK_TOP_CENTER,
522+ NK_TOP_RIGHT,
523+
524+ NK_MIDDLE_LEFT,
525+ NK_MIDDLE_CENTER,
526+ NK_MIDDLE_RIGHT,
527+
528+ NK_BOTTOM_LEFT,
529+ NK_BOTTOM_CENTER,
530+ NK_BOTTOM_RIGHT
531+ };
532+
519533typedef void*(*nk_plugin_alloc)(nk_handle, void *old, nk_size);
520534typedef void (*nk_plugin_free)(nk_handle, void *old);
521535typedef nk_bool(*nk_plugin_filter)(const struct nk_text_edit*, nk_rune unicode);
@@ -3827,11 +3841,15 @@ NK_API void nk_contextual_end(struct nk_context*);
38273841 *
38283842 * ============================================================================= */
38293843NK_API void nk_tooltip(struct nk_context*, const char*);
3844+ NK_API void nk_tooltip_offset(struct nk_context *ctx, const char *text, enum nk_tooltip_pos position, struct nk_vec2 offset);
38303845#ifdef NK_INCLUDE_STANDARD_VARARGS
38313846NK_API void nk_tooltipf(struct nk_context*, NK_PRINTF_FORMAT_STRING const char*, ...) NK_PRINTF_VARARG_FUNC(2);
38323847NK_API void nk_tooltipfv(struct nk_context*, NK_PRINTF_FORMAT_STRING const char*, va_list) NK_PRINTF_VALIST_FUNC(2);
3848+ NK_API void nk_tooltipf_offset(struct nk_context*, enum nk_tooltip_pos, struct nk_vec2, NK_PRINTF_FORMAT_STRING const char*, ...) NK_PRINTF_VARARG_FUNC(4);
3849+ NK_API void nk_tooltipfv_offset(struct nk_context*, enum nk_tooltip_pos, struct nk_vec2, NK_PRINTF_FORMAT_STRING const char*, va_list) NK_PRINTF_VALIST_FUNC(4);
38333850#endif
38343851NK_API nk_bool nk_tooltip_begin(struct nk_context*, float width);
3852+ NK_API nk_bool nk_tooltip_begin_offset(struct nk_context*, float, enum nk_tooltip_pos, struct nk_vec2);
38353853NK_API void nk_tooltip_end(struct nk_context*);
38363854/* =============================================================================
38373855 *
@@ -5581,6 +5599,9 @@ struct nk_style_window {
55815599 struct nk_vec2 contextual_padding;
55825600 struct nk_vec2 menu_padding;
55835601 struct nk_vec2 tooltip_padding;
5602+
5603+ enum nk_tooltip_pos tooltip_origin;
5604+ struct nk_vec2 tooltip_offset;
55845605};
55855606
55865607struct nk_style {
@@ -19228,6 +19249,15 @@ nk_style_from_table(struct nk_context *ctx, const struct nk_color *table)
1922819249 win->contextual_padding = nk_vec2(4,4);
1922919250 win->menu_padding = nk_vec2(4,4);
1923019251 win->tooltip_padding = nk_vec2(4,4);
19252+
19253+ /* default tooltip just down and to the right of the cursor
19254+ * so it doesn't cover the text
19255+ *
19256+ * TODO might be worth consolidating tooltip styling
19257+ * into its own style structure, though it is a
19258+ * type of window...*/
19259+ win->tooltip_origin = NK_TOP_LEFT;
19260+ win->tooltip_offset = nk_vec2(12, 12);
1923119261}
1923219262NK_API void
1923319263nk_style_set_font(struct nk_context *ctx, const struct nk_user_font *font)
@@ -30631,6 +30661,13 @@ nk_combobox_callback(struct nk_context *ctx,
3063130661 * ===============================================================*/
3063230662NK_API nk_bool
3063330663nk_tooltip_begin(struct nk_context *ctx, float width)
30664+ {
30665+ NK_ASSERT(ctx);
30666+ return nk_tooltip_begin_offset(ctx, width, ctx->style.window.tooltip_origin, ctx->style.window.tooltip_offset);
30667+ }
30668+
30669+ NK_API nk_bool
30670+ nk_tooltip_begin_offset(struct nk_context *ctx, float width, enum nk_tooltip_pos position, struct nk_vec2 offset)
3063430671{
3063530672 int x,y,w,h;
3063630673 struct nk_window *win;
@@ -30651,14 +30688,55 @@ nk_tooltip_begin(struct nk_context *ctx, float width)
3065130688 return 0;
3065230689
3065330690 w = nk_iceilf(width);
30654- h = nk_iceilf(nk_null_rect.h);
30655- x = nk_ifloorf(in->mouse.pos.x + 1) - (int)win->layout->clip.x;
30656- y = nk_ifloorf(in->mouse.pos.y + 1) - (int)win->layout->clip.y;
30691+ h = NK_MAX(win->layout->row.min_height, ctx->style.font->height+2*ctx->style.window.padding.y);
30692+
30693+ /* Default origin is top left, plus user offset */
30694+ x = nk_ifloorf(in->mouse.pos.x + 1) - (int)win->layout->clip.x + offset.x;
30695+ y = nk_ifloorf(in->mouse.pos.y + 1) - (int)win->layout->clip.y + offset.y;
30696+
30697+ /* Adjust origin based on enum */
30698+ switch (position) {
30699+ case NK_TOP_LEFT:
30700+ /* no change */
30701+ break;
30702+ case NK_TOP_CENTER:
30703+ x -= w/2;
30704+ break;
30705+ case NK_TOP_RIGHT:
30706+ x -= w;
30707+ break;
30708+
30709+ case NK_MIDDLE_LEFT:
30710+ y -= h/2;
30711+ break;
30712+ case NK_MIDDLE_CENTER:
30713+ x -= w/2;
30714+ y -= h/2;
30715+ break;
30716+ case NK_MIDDLE_RIGHT:
30717+ x -= w;
30718+ y -= h/2;
30719+ break;
30720+
30721+ case NK_BOTTOM_LEFT:
30722+ y -= h;
30723+ break;
30724+ case NK_BOTTOM_CENTER:
30725+ x -= w/2;
30726+ y -= h;
30727+ break;
30728+ case NK_BOTTOM_RIGHT:
30729+ x -= w;
30730+ y -= h;
30731+ break;
30732+ default:
30733+ NK_ASSERT(0 && "Invalid tooltip position");
30734+ }
3065730735
3065830736 bounds.x = (float)x;
3065930737 bounds.y = (float)y;
3066030738 bounds.w = (float)w;
30661- bounds.h = (float)h ;
30739+ bounds.h = (float)nk_iceilf(nk_null_rect.h) ;
3066230740
3066330741 ret = nk_popup_begin(ctx, NK_POPUP_DYNAMIC,
3066430742 "__##Tooltip##__", NK_WINDOW_NO_SCROLLBAR|NK_WINDOW_BORDER, bounds);
@@ -30678,8 +30756,9 @@ nk_tooltip_end(struct nk_context *ctx)
3067830756 nk_popup_close(ctx);
3067930757 nk_popup_end(ctx);
3068030758}
30759+
3068130760NK_API void
30682- nk_tooltip (struct nk_context *ctx, const char *text)
30761+ nk_tooltip_offset (struct nk_context *ctx, const char *text, enum nk_tooltip_pos position, struct nk_vec2 offset )
3068330762{
3068430763 const struct nk_style *style;
3068530764 struct nk_vec2 padding;
@@ -30707,14 +30786,29 @@ nk_tooltip(struct nk_context *ctx, const char *text)
3070730786 text_height = (style->font->height + 2 * padding.y);
3070830787
3070930788 /* execute tooltip and fill with text */
30710- if (nk_tooltip_begin (ctx, (float)text_width)) {
30789+ if (nk_tooltip_begin_offset (ctx, (float)text_width, position, offset )) {
3071130790 nk_layout_row_dynamic(ctx, (float)text_height, 1);
3071230791 nk_text(ctx, text, text_len, NK_TEXT_LEFT);
3071330792 nk_tooltip_end(ctx);
3071430793 }
3071530794}
30795+
30796+ NK_API void
30797+ nk_tooltip(struct nk_context *ctx, const char *text)
30798+ {
30799+ NK_ASSERT(ctx);
30800+ nk_tooltip_offset(ctx, text, ctx->style.window.tooltip_origin, ctx->style.window.tooltip_offset);
30801+ }
3071630802#ifdef NK_INCLUDE_STANDARD_VARARGS
3071730803NK_API void
30804+ nk_tooltipf_offset(struct nk_context *ctx, enum nk_tooltip_pos position, struct nk_vec2 offset, const char *fmt, ...)
30805+ {
30806+ va_list args;
30807+ va_start(args, fmt);
30808+ nk_tooltipfv_offset(ctx, position, offset, fmt, args);
30809+ va_end(args);
30810+ }
30811+ NK_API void
3071830812nk_tooltipf(struct nk_context *ctx, const char *fmt, ...)
3071930813{
3072030814 va_list args;
@@ -30723,6 +30817,13 @@ nk_tooltipf(struct nk_context *ctx, const char *fmt, ...)
3072330817 va_end(args);
3072430818}
3072530819NK_API void
30820+ nk_tooltipfv_offset(struct nk_context *ctx, enum nk_tooltip_pos position, struct nk_vec2 offset, const char *fmt, va_list args)
30821+ {
30822+ char buf[256];
30823+ nk_strfmt(buf, NK_LEN(buf), fmt, args);
30824+ nk_tooltip_offset(ctx, buf, position, offset);
30825+ }
30826+ NK_API void
3072630827nk_tooltipfv(struct nk_context *ctx, const char *fmt, va_list args)
3072730828{
3072830829 char buf[256];
0 commit comments