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 bounds.x = (float)x;
84 bounds.y = (float)y;
85 bounds.w = (float)w;
86 bounds.h = (float)nk_iceilf(nk_null_rect.h);
87
88 ret = nk_popup_begin(ctx, NK_POPUP_DYNAMIC,
89 "__##Tooltip##__", NK_WINDOW_NO_SCROLLBAR|NK_WINDOW_BORDER, bounds);
90 if (ret) win->layout->flags &= ~(nk_flags)NK_WINDOW_ROM;
91 win->popup.type = NK_PANEL_TOOLTIP;
92 ctx->current->layout->type = NK_PANEL_TOOLTIP;
93 return ret;
94}
95
96NK_API void
97nk_tooltip_end(struct nk_context *ctx)
98{
99 NK_ASSERT(ctx);
100 NK_ASSERT(ctx->current);
101 if (!ctx || !ctx->current) return;
102 ctx->current->seq--;
103 nk_popup_close(ctx);
104 nk_popup_end(ctx);
105}
106
110NK_API void
111nk_do_tooltip(struct nk_context* ctx, const char* text, struct nk_rect bounds)
112{
113 NK_ASSERT(ctx);
114 if (nk_input_is_mouse_hovering_rect(&ctx->input, bounds)) {
115 nk_tooltip_offset(ctx, text, ctx->style.window.tooltip_origin, ctx->style.window.tooltip_offset);
116 }
117}
118
123NK_API void
124nk_do_tooltip_delay(struct nk_context* ctx, const char* text, struct nk_rect bounds, float* timer)
125{
126 NK_ASSERT(ctx);
127 if (nk_input_is_mouse_hovering_still_delay_rect(ctx, bounds, timer, ctx->style.window.tooltip_delay)) {
128 nk_tooltip_offset(ctx, text, ctx->style.window.tooltip_origin, ctx->style.window.tooltip_offset);
129 }
130}
131
137NK_API void
138nk_do_tooltip_delay_clicked(struct nk_context* ctx, const char* text, struct nk_rect bounds, float* timer, nk_bool* clicked)
139{
140 NK_ASSERT(ctx);
141 if (nk_input_is_mouse_hovering_still_delay_clicked_rect(ctx, bounds, timer, ctx->style.window.tooltip_delay, clicked)) {
142 nk_tooltip_offset(ctx, text, ctx->style.window.tooltip_origin, ctx->style.window.tooltip_offset);
143 }
144}
145
146NK_API void
147nk_tooltip_offset(struct nk_context *ctx, const char *text, enum nk_tooltip_pos position, struct nk_vec2 offset)
148{
149 const struct nk_style *style;
150 struct nk_vec2 padding;
151
152 int text_len;
153 float text_width;
154 float text_height;
155
156 NK_ASSERT(ctx);
157 NK_ASSERT(ctx->current);
158 NK_ASSERT(ctx->current->layout);
159 NK_ASSERT(text);
160 if (!ctx || !ctx->current || !ctx->current->layout || !text)
161 return;
162
163 /* fetch configuration data */
164 style = &ctx->style;
165 padding = style->window.padding;
166
167 /* calculate size of the text and tooltip */
168 text_len = nk_strlen(text);
169 text_width = style->font->width(style->font->userdata,
170 style->font->height, text, text_len);
171 text_width += (4 * padding.x);
172 text_height = (style->font->height + 2 * padding.y);
173
174 /* execute tooltip and fill with text */
175 if (nk_tooltip_begin_offset(ctx, (float)text_width, position, offset)) {
176 nk_layout_row_dynamic(ctx, (float)text_height, 1);
177 nk_text(ctx, text, text_len, NK_TEXT_LEFT);
178 nk_tooltip_end(ctx);
179 }
180}
181
182NK_API void
183nk_tooltip(struct nk_context *ctx, const char *text)
184{
185 NK_ASSERT(ctx);
186 nk_tooltip_offset(ctx, text, ctx->style.window.tooltip_origin, ctx->style.window.tooltip_offset);
187}
188#ifdef NK_INCLUDE_STANDARD_VARARGS
189NK_API void
190nk_tooltipf_offset(struct nk_context *ctx, enum nk_tooltip_pos position, struct nk_vec2 offset, const char *fmt, ...)
191{
192 va_list args;
193 va_start(args, fmt);
194 nk_tooltipfv_offset(ctx, position, offset, fmt, args);
195 va_end(args);
196}
197NK_API void
198nk_tooltipf(struct nk_context *ctx, const char *fmt, ...)
199{
200 va_list args;
201 va_start(args, fmt);
202 nk_tooltipfv(ctx, fmt, args);
203 va_end(args);
204}
205NK_API void
206nk_tooltipfv_offset(struct nk_context *ctx, enum nk_tooltip_pos position, struct nk_vec2 offset, const char *fmt, va_list args)
207{
208 char buf[256];
209 nk_strfmt(buf, NK_LEN(buf), fmt, args);
210 nk_tooltip_offset(ctx, buf, position, offset);
211}
212NK_API void
213nk_tooltipfv(struct nk_context *ctx, const char *fmt, va_list args)
214{
215 char buf[256];
216 nk_strfmt(buf, NK_LEN(buf), fmt, args);
217 nk_tooltip(ctx, buf);
218}
219#endif
220
221
nk_text_width_f width
!< max height of the font
Definition nuklear.h:4052
float height
!< user provided font handle
Definition nuklear.h:4051