Nuklear
Minimal-state, immediate-mode graphical user interface toolkit written in ANSI C.
Loading...
Searching...
No Matches
nuklear_tooltip.c
1#include "nuklear.h"
2#include "nuklear_internal.h"
3
4/* ===============================================================
5 *
6 * TOOLTIP
7 *
8 * ===============================================================*/
9NK_API nk_bool
10nk_tooltip_begin(struct nk_context *ctx, float width)
11{
12 NK_ASSERT(ctx);
13 return nk_tooltip_begin_offset(ctx, width, ctx->style.window.tooltip_origin, ctx->style.window.tooltip_offset);
14}
15
16NK_API nk_bool
17nk_tooltip_begin_offset(struct nk_context *ctx, float width, enum nk_tooltip_pos position, struct nk_vec2 offset)
18{
19 int x,y,w,h;
20 struct nk_window *win;
21 const struct nk_input *in;
22 struct nk_rect bounds;
23 int ret;
24
25 NK_ASSERT(ctx);
26 NK_ASSERT(ctx->current);
27 NK_ASSERT(ctx->current->layout);
28 if (!ctx || !ctx->current || !ctx->current->layout)
29 return 0;
30
31 /* make sure that no nonblocking popup is currently active */
32 win = ctx->current;
33 in = &ctx->input;
34 if (win->popup.win && ((int)win->popup.type & (int)NK_PANEL_SET_NONBLOCK))
35 return 0;
36
37 w = nk_iceilf(width);
38 h = (int)NK_MAX(win->layout->row.min_height, ctx->style.font->height+2*ctx->style.window.padding.y);
39
40 /* Default origin is top left, plus user offset */
41 x = nk_ifloorf(in->mouse.pos.x + 1) - (int)win->layout->clip.x + (int)offset.x;
42 y = nk_ifloorf(in->mouse.pos.y + 1) - (int)win->layout->clip.y + (int)offset.y;
43
44 /* Adjust origin based on enum */
45 switch (position) {
46 case NK_TOP_LEFT:
47 /* no change */
48 break;
49 case NK_TOP_CENTER:
50 x -= w/2;
51 break;
52 case NK_TOP_RIGHT:
53 x -= w;
54 break;
55
56 case NK_MIDDLE_LEFT:
57 y -= h/2;
58 break;
59 case NK_MIDDLE_CENTER:
60 x -= w/2;
61 y -= h/2;
62 break;
63 case NK_MIDDLE_RIGHT:
64 x -= w;
65 y -= h/2;
66 break;
67
68 case NK_BOTTOM_LEFT:
69 y -= h;
70 break;
71 case NK_BOTTOM_CENTER:
72 x -= w/2;
73 y -= h;
74 break;
75 case NK_BOTTOM_RIGHT:
76 x -= w;
77 y -= h;
78 break;
79 default:
80 NK_ASSERT(0 && "Invalid tooltip position");
81 }
82
83 {struct nk_rect screen;
84 struct nk_rect anchor;
85 float known_h = (float)h;
86 float clip_x = win->layout->clip.x;
87 float clip_y = win->layout->clip.y;
88
89 if (win->popup.win && win->popup.type == NK_PANEL_TOOLTIP &&
90 win->popup.last_h > known_h)
91 known_h = win->popup.last_h;
92
93 screen.x = (float)x + clip_x;
94 screen.y = (float)y + clip_y;
95 screen.w = (float)w;
96 screen.h = known_h;
97 anchor.x = in->mouse.pos.x;
98 anchor.y = in->mouse.pos.y;
99 anchor.w = 1;
100 anchor.h = 1;
101 screen = nk_fit_popup_rect(ctx, screen, anchor, NK_POPUP_FIT_TOOLTIP, known_h, nk_false);
102
103 bounds.x = screen.x - clip_x;
104 bounds.y = screen.y - clip_y;
105 bounds.w = screen.w;
106 /* keep a large max so NK_POPUP_DYNAMIC can grow; the fitter only
107 * shrinks h when the tooltip cannot fit on the surface at all */
108 bounds.h = (screen.h + 0.5f < known_h) ? screen.h : (float)nk_iceilf(nk_null_rect.h);}
109
110 ret = nk_popup_begin(ctx, NK_POPUP_DYNAMIC,
111 "__##Tooltip##__", NK_WINDOW_NO_SCROLLBAR|NK_WINDOW_BORDER, bounds);
112 if (ret) win->layout->flags &= ~(nk_flags)NK_WINDOW_ROM;
113 win->popup.type = NK_PANEL_TOOLTIP;
114 ctx->current->layout->type = NK_PANEL_TOOLTIP;
115 return ret;
116}
117
118NK_API void
119nk_tooltip_end(struct nk_context *ctx)
120{
121 NK_ASSERT(ctx);
122 NK_ASSERT(ctx->current);
123 if (!ctx || !ctx->current) return;
124 ctx->current->seq--;
125 nk_popup_close(ctx);
126 nk_popup_end(ctx);
127}
128
132NK_API void
133nk_do_tooltip(struct nk_context* ctx, const char* text, struct nk_rect bounds)
134{
135 NK_ASSERT(ctx);
136 if (nk_input_is_mouse_hovering_rect(&ctx->input, bounds)) {
137 nk_tooltip_offset(ctx, text, ctx->style.window.tooltip_origin, ctx->style.window.tooltip_offset);
138 }
139}
140
145NK_API void
146nk_do_tooltip_delay(struct nk_context* ctx, const char* text, struct nk_rect bounds, float* timer)
147{
148 NK_ASSERT(ctx);
149 if (nk_input_is_mouse_hovering_still_delay_rect(ctx, bounds, timer, ctx->style.window.tooltip_delay)) {
150 nk_tooltip_offset(ctx, text, ctx->style.window.tooltip_origin, ctx->style.window.tooltip_offset);
151 }
152}
153
159NK_API void
160nk_do_tooltip_delay_clicked(struct nk_context* ctx, const char* text, struct nk_rect bounds, float* timer, nk_bool* clicked)
161{
162 NK_ASSERT(ctx);
163 if (nk_input_is_mouse_hovering_still_delay_clicked_rect(ctx, bounds, timer, ctx->style.window.tooltip_delay, clicked)) {
164 nk_tooltip_offset(ctx, text, ctx->style.window.tooltip_origin, ctx->style.window.tooltip_offset);
165 }
166}
167
168NK_API void
169nk_tooltip_offset(struct nk_context *ctx, const char *text, enum nk_tooltip_pos position, struct nk_vec2 offset)
170{
171 const struct nk_style *style;
172 struct nk_vec2 padding;
173
174 int text_len;
175 float text_width;
176 float text_height;
177
178 NK_ASSERT(ctx);
179 NK_ASSERT(ctx->current);
180 NK_ASSERT(ctx->current->layout);
181 NK_ASSERT(text);
182 if (!ctx || !ctx->current || !ctx->current->layout || !text)
183 return;
184
185 /* fetch configuration data */
186 style = &ctx->style;
187 padding = style->window.padding;
188
189 /* calculate size of the text and tooltip */
190 text_len = nk_strlen(text);
191 text_width = style->font->width(style->font->userdata,
192 style->font->height, text, text_len);
193 /* outer bounds: nk_panel_begin subtracts popup padding and border */
194 text_width += 2 * style->window.popup_padding.x;
195 text_width += 2 * style->window.popup_border;
196 text_height = (style->font->height + 2 * padding.y);
197
198 /* execute tooltip and fill with text */
199 if (nk_tooltip_begin_offset(ctx, (float)text_width, position, offset)) {
200 nk_layout_row_dynamic(ctx, (float)text_height, 1);
201 nk_text(ctx, text, text_len, NK_TEXT_LEFT);
202 nk_tooltip_end(ctx);
203 }
204}
205
206NK_API void
207nk_tooltip(struct nk_context *ctx, const char *text)
208{
209 NK_ASSERT(ctx);
210 nk_tooltip_offset(ctx, text, ctx->style.window.tooltip_origin, ctx->style.window.tooltip_offset);
211}
212#ifdef NK_INCLUDE_STANDARD_VARARGS
213NK_API void
214nk_tooltipf_offset(struct nk_context *ctx, enum nk_tooltip_pos position, struct nk_vec2 offset, const char *fmt, ...)
215{
216 va_list args;
217 va_start(args, fmt);
218 nk_tooltipfv_offset(ctx, position, offset, fmt, args);
219 va_end(args);
220}
221NK_API void
222nk_tooltipf(struct nk_context *ctx, const char *fmt, ...)
223{
224 va_list args;
225 va_start(args, fmt);
226 nk_tooltipfv(ctx, fmt, args);
227 va_end(args);
228}
229NK_API void
230nk_tooltipfv_offset(struct nk_context *ctx, enum nk_tooltip_pos position, struct nk_vec2 offset, const char *fmt, va_list args)
231{
232 char buf[256];
233 nk_strfmt(buf, NK_LEN(buf), fmt, args);
234 nk_tooltip_offset(ctx, buf, position, offset);
235}
236NK_API void
237nk_tooltipfv(struct nk_context *ctx, const char *fmt, va_list args)
238{
239 char buf[256];
240 nk_strfmt(buf, NK_LEN(buf), fmt, args);
241 nk_tooltip(ctx, buf);
242}
243#endif
244
245
float last_h
last frame's content height; used to flip without a gap
Definition nuklear.h:5642
nk_text_width_f width
!< max height of the font
Definition nuklear.h:4119
float height
!< user provided font handle
Definition nuklear.h:4118