Skip to content

Commit 43de49a

Browse files
committed
Simplify and combine functions as discussed...
Combine position and offset parameters a single call using the latter as the origin. Credit to @sleeptightAnsiC for the obvious idea.
1 parent 83d9a60 commit 43de49a

3 files changed

Lines changed: 74 additions & 164 deletions

File tree

nuklear.h

Lines changed: 37 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -3828,14 +3828,13 @@ NK_API void nk_contextual_end(struct nk_context*);
38283828
*
38293829
* ============================================================================= */
38303830
NK_API void nk_tooltip(struct nk_context*, const char*);
3831-
NK_API void nk_tooltip_positioned(struct nk_context*, const char*, enum nk_tooltip_pos);
3832-
NK_API void nk_tooltip_offset(struct nk_context*, const char*, struct nk_vec2);
3831+
NK_API void nk_tooltip_pos_offset(struct nk_context *ctx, const char *text, enum nk_tooltip_pos position, struct nk_vec2 offset);
38333832
#ifdef NK_INCLUDE_STANDARD_VARARGS
38343833
NK_API void nk_tooltipf(struct nk_context*, NK_PRINTF_FORMAT_STRING const char*, ...) NK_PRINTF_VARARG_FUNC(2);
38353834
NK_API void nk_tooltipfv(struct nk_context*, NK_PRINTF_FORMAT_STRING const char*, va_list) NK_PRINTF_VALIST_FUNC(2);
38363835
#endif
38373836
NK_API nk_bool nk_tooltip_begin(struct nk_context*, float width);
3838-
NK_API nk_bool nk_tooltip_begin_offset(struct nk_context*, float, struct nk_vec2);
3837+
NK_API nk_bool nk_tooltip_begin_pos_offset(struct nk_context*, float, enum nk_tooltip_pos, struct nk_vec2);
38393838
NK_API void nk_tooltip_end(struct nk_context*);
38403839
/* =============================================================================
38413840
*
@@ -30611,11 +30610,11 @@ NK_API nk_bool
3061130610
nk_tooltip_begin(struct nk_context *ctx, float width)
3061230611
{
3061330612
struct nk_vec2 offset = {0};
30614-
return nk_tooltip_begin_offset(ctx, width, offset);
30613+
return nk_tooltip_begin_pos_offset(ctx, width, NK_TOP_LEFT, offset);
3061530614
}
3061630615

3061730616
NK_API nk_bool
30618-
nk_tooltip_begin_offset(struct nk_context *ctx, float width, struct nk_vec2 offset)
30617+
nk_tooltip_begin_pos_offset(struct nk_context *ctx, float width, enum nk_tooltip_pos position, struct nk_vec2 offset)
3061930618
{
3062030619
int x,y,w,h;
3062130620
struct nk_window *win;
@@ -30636,14 +30635,43 @@ nk_tooltip_begin_offset(struct nk_context *ctx, float width, struct nk_vec2 offs
3063630635
return 0;
3063730636

3063830637
w = nk_iceilf(width);
30639-
h = nk_iceilf(nk_null_rect.h);
30638+
/* Should I use text_height? Any difference? */
30639+
h = ctx->current->layout->row.min_height;
30640+
30641+
/* Default origin is top left, plus user offset */
3064030642
x = nk_ifloorf(in->mouse.pos.x + 1) - (int)win->layout->clip.x + offset.x;
3064130643
y = nk_ifloorf(in->mouse.pos.y + 1) - (int)win->layout->clip.y + offset.y;
3064230644

30645+
/* Adjust origin based on enum */
30646+
switch (position) {
30647+
case NK_TOP_LEFT:
30648+
/* no change */
30649+
break;
30650+
case NK_BOTTOM_LEFT:
30651+
y -= h;
30652+
break;
30653+
case NK_TOP_RIGHT:
30654+
x -= w;
30655+
break;
30656+
case NK_BOTTOM_RIGHT:
30657+
x -= w;
30658+
y -= h;
30659+
break;
30660+
case NK_TOP_MIDDLE:
30661+
x -= w/2;
30662+
break;
30663+
case NK_BOTTOM_MIDDLE:
30664+
x -= w/2;
30665+
y -= h;
30666+
break;
30667+
default:
30668+
NK_ASSERT(0 && "Invalid tooltip position");
30669+
}
30670+
3064330671
bounds.x = (float)x;
3064430672
bounds.y = (float)y;
3064530673
bounds.w = (float)w;
30646-
bounds.h = (float)h;
30674+
bounds.h = (float)nk_iceilf(nk_null_rect.h);
3064730675

3064830676
ret = nk_popup_begin(ctx, NK_POPUP_DYNAMIC,
3064930677
"__##Tooltip##__", NK_WINDOW_NO_SCROLLBAR|NK_WINDOW_BORDER, bounds);
@@ -30665,7 +30693,7 @@ nk_tooltip_end(struct nk_context *ctx)
3066530693
}
3066630694

3066730695
NK_API void
30668-
nk_tooltip_offset(struct nk_context *ctx, const char *text, struct nk_vec2 offset)
30696+
nk_tooltip_pos_offset(struct nk_context *ctx, const char *text, enum nk_tooltip_pos position, struct nk_vec2 offset)
3066930697
{
3067030698
const struct nk_style *style;
3067130699
struct nk_vec2 padding;
@@ -30693,80 +30721,7 @@ nk_tooltip_offset(struct nk_context *ctx, const char *text, struct nk_vec2 offse
3069330721
text_height = (style->font->height + 2 * padding.y);
3069430722

3069530723
/* execute tooltip and fill with text */
30696-
if (nk_tooltip_begin_offset(ctx, (float)text_width, offset)) {
30697-
nk_layout_row_dynamic(ctx, (float)text_height, 1);
30698-
nk_text(ctx, text, text_len, NK_TEXT_LEFT);
30699-
nk_tooltip_end(ctx);
30700-
}
30701-
}
30702-
30703-
NK_API void
30704-
nk_tooltip_positioned(struct nk_context *ctx, const char *text, enum nk_tooltip_pos position)
30705-
{
30706-
const struct nk_style *style;
30707-
struct nk_vec2 padding;
30708-
30709-
int text_len;
30710-
float text_width;
30711-
float text_height;
30712-
30713-
struct nk_vec2 offset = { 0 };
30714-
int w,h;
30715-
30716-
NK_ASSERT(ctx);
30717-
NK_ASSERT(ctx->current);
30718-
NK_ASSERT(ctx->current->layout);
30719-
NK_ASSERT(text);
30720-
if (!ctx || !ctx->current || !ctx->current->layout || !text)
30721-
return;
30722-
30723-
/* fetch configuration data */
30724-
style = &ctx->style;
30725-
padding = style->window.padding;
30726-
30727-
/* calculate size of the text and tooltip */
30728-
text_len = nk_strlen(text);
30729-
text_width = style->font->width(style->font->userdata,
30730-
style->font->height, text, text_len);
30731-
text_width += (4 * padding.x);
30732-
text_height = (style->font->height + 2 * padding.y);
30733-
30734-
/* Should I use text_height? Any difference? */
30735-
h = ctx->current->layout->row.min_height;
30736-
w = nk_iceilf(text_width);
30737-
30738-
switch (position) {
30739-
case NK_TOP_LEFT:
30740-
offset.x = 0;
30741-
offset.y = 0;
30742-
break;
30743-
case NK_BOTTOM_LEFT:
30744-
offset.x = 0;
30745-
offset.y = -h;
30746-
break;
30747-
case NK_TOP_RIGHT:
30748-
offset.x = -w;
30749-
offset.y = 0;
30750-
break;
30751-
case NK_BOTTOM_RIGHT:
30752-
offset.x = -w;
30753-
offset.y = -h;
30754-
break;
30755-
case NK_TOP_MIDDLE:
30756-
offset.x = -w/2;
30757-
offset.y = 0;
30758-
break;
30759-
case NK_BOTTOM_MIDDLE:
30760-
offset.x = -w/2;
30761-
offset.y = -h;
30762-
break;
30763-
default:
30764-
offset.x = 0;
30765-
offset.y = 0;
30766-
}
30767-
30768-
/* execute tooltip and fill with text */
30769-
if (nk_tooltip_begin_offset(ctx, (float)text_width, offset)) {
30724+
if (nk_tooltip_begin_pos_offset(ctx, (float)text_width, position, offset)) {
3077030725
nk_layout_row_dynamic(ctx, (float)text_height, 1);
3077130726
nk_text(ctx, text, text_len, NK_TEXT_LEFT);
3077230727
nk_tooltip_end(ctx);

src/nuklear.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3605,14 +3605,13 @@ NK_API void nk_contextual_end(struct nk_context*);
36053605
*
36063606
* ============================================================================= */
36073607
NK_API void nk_tooltip(struct nk_context*, const char*);
3608-
NK_API void nk_tooltip_positioned(struct nk_context*, const char*, enum nk_tooltip_pos);
3609-
NK_API void nk_tooltip_offset(struct nk_context*, const char*, struct nk_vec2);
3608+
NK_API void nk_tooltip_pos_offset(struct nk_context *ctx, const char *text, enum nk_tooltip_pos position, struct nk_vec2 offset);
36103609
#ifdef NK_INCLUDE_STANDARD_VARARGS
36113610
NK_API void nk_tooltipf(struct nk_context*, NK_PRINTF_FORMAT_STRING const char*, ...) NK_PRINTF_VARARG_FUNC(2);
36123611
NK_API void nk_tooltipfv(struct nk_context*, NK_PRINTF_FORMAT_STRING const char*, va_list) NK_PRINTF_VALIST_FUNC(2);
36133612
#endif
36143613
NK_API nk_bool nk_tooltip_begin(struct nk_context*, float width);
3615-
NK_API nk_bool nk_tooltip_begin_offset(struct nk_context*, float, struct nk_vec2);
3614+
NK_API nk_bool nk_tooltip_begin_pos_offset(struct nk_context*, float, enum nk_tooltip_pos, struct nk_vec2);
36163615
NK_API void nk_tooltip_end(struct nk_context*);
36173616
/* =============================================================================
36183617
*

src/nuklear_tooltip.c

Lines changed: 35 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ NK_API nk_bool
1010
nk_tooltip_begin(struct nk_context *ctx, float width)
1111
{
1212
struct nk_vec2 offset = {0};
13-
return nk_tooltip_begin_offset(ctx, width, offset);
13+
return nk_tooltip_begin_pos_offset(ctx, width, NK_TOP_LEFT, offset);
1414
}
1515

1616
NK_API nk_bool
17-
nk_tooltip_begin_offset(struct nk_context *ctx, float width, struct nk_vec2 offset)
17+
nk_tooltip_begin_pos_offset(struct nk_context *ctx, float width, enum nk_tooltip_pos position, struct nk_vec2 offset)
1818
{
1919
int x,y,w,h;
2020
struct nk_window *win;
@@ -35,14 +35,43 @@ nk_tooltip_begin_offset(struct nk_context *ctx, float width, struct nk_vec2 offs
3535
return 0;
3636

3737
w = nk_iceilf(width);
38-
h = nk_iceilf(nk_null_rect.h);
38+
/* Should I use text_height? Any difference? */
39+
h = ctx->current->layout->row.min_height;
40+
41+
/* Default origin is top left, plus user offset */
3942
x = nk_ifloorf(in->mouse.pos.x + 1) - (int)win->layout->clip.x + offset.x;
4043
y = nk_ifloorf(in->mouse.pos.y + 1) - (int)win->layout->clip.y + offset.y;
4144

45+
/* Adjust origin based on enum */
46+
switch (position) {
47+
case NK_TOP_LEFT:
48+
/* no change */
49+
break;
50+
case NK_BOTTOM_LEFT:
51+
y -= h;
52+
break;
53+
case NK_TOP_RIGHT:
54+
x -= w;
55+
break;
56+
case NK_BOTTOM_RIGHT:
57+
x -= w;
58+
y -= h;
59+
break;
60+
case NK_TOP_MIDDLE:
61+
x -= w/2;
62+
break;
63+
case NK_BOTTOM_MIDDLE:
64+
x -= w/2;
65+
y -= h;
66+
break;
67+
default:
68+
NK_ASSERT(0 && "Invalid tooltip position");
69+
}
70+
4271
bounds.x = (float)x;
4372
bounds.y = (float)y;
4473
bounds.w = (float)w;
45-
bounds.h = (float)h;
74+
bounds.h = (float)nk_iceilf(nk_null_rect.h);
4675

4776
ret = nk_popup_begin(ctx, NK_POPUP_DYNAMIC,
4877
"__##Tooltip##__", NK_WINDOW_NO_SCROLLBAR|NK_WINDOW_BORDER, bounds);
@@ -64,7 +93,7 @@ nk_tooltip_end(struct nk_context *ctx)
6493
}
6594

6695
NK_API void
67-
nk_tooltip_offset(struct nk_context *ctx, const char *text, struct nk_vec2 offset)
96+
nk_tooltip_pos_offset(struct nk_context *ctx, const char *text, enum nk_tooltip_pos position, struct nk_vec2 offset)
6897
{
6998
const struct nk_style *style;
7099
struct nk_vec2 padding;
@@ -92,80 +121,7 @@ nk_tooltip_offset(struct nk_context *ctx, const char *text, struct nk_vec2 offse
92121
text_height = (style->font->height + 2 * padding.y);
93122

94123
/* execute tooltip and fill with text */
95-
if (nk_tooltip_begin_offset(ctx, (float)text_width, offset)) {
96-
nk_layout_row_dynamic(ctx, (float)text_height, 1);
97-
nk_text(ctx, text, text_len, NK_TEXT_LEFT);
98-
nk_tooltip_end(ctx);
99-
}
100-
}
101-
102-
NK_API void
103-
nk_tooltip_positioned(struct nk_context *ctx, const char *text, enum nk_tooltip_pos position)
104-
{
105-
const struct nk_style *style;
106-
struct nk_vec2 padding;
107-
108-
int text_len;
109-
float text_width;
110-
float text_height;
111-
112-
struct nk_vec2 offset = { 0 };
113-
int w,h;
114-
115-
NK_ASSERT(ctx);
116-
NK_ASSERT(ctx->current);
117-
NK_ASSERT(ctx->current->layout);
118-
NK_ASSERT(text);
119-
if (!ctx || !ctx->current || !ctx->current->layout || !text)
120-
return;
121-
122-
/* fetch configuration data */
123-
style = &ctx->style;
124-
padding = style->window.padding;
125-
126-
/* calculate size of the text and tooltip */
127-
text_len = nk_strlen(text);
128-
text_width = style->font->width(style->font->userdata,
129-
style->font->height, text, text_len);
130-
text_width += (4 * padding.x);
131-
text_height = (style->font->height + 2 * padding.y);
132-
133-
/* Should I use text_height? Any difference? */
134-
h = ctx->current->layout->row.min_height;
135-
w = nk_iceilf(text_width);
136-
137-
switch (position) {
138-
case NK_TOP_LEFT:
139-
offset.x = 0;
140-
offset.y = 0;
141-
break;
142-
case NK_BOTTOM_LEFT:
143-
offset.x = 0;
144-
offset.y = -h;
145-
break;
146-
case NK_TOP_RIGHT:
147-
offset.x = -w;
148-
offset.y = 0;
149-
break;
150-
case NK_BOTTOM_RIGHT:
151-
offset.x = -w;
152-
offset.y = -h;
153-
break;
154-
case NK_TOP_MIDDLE:
155-
offset.x = -w/2;
156-
offset.y = 0;
157-
break;
158-
case NK_BOTTOM_MIDDLE:
159-
offset.x = -w/2;
160-
offset.y = -h;
161-
break;
162-
default:
163-
offset.x = 0;
164-
offset.y = 0;
165-
}
166-
167-
/* execute tooltip and fill with text */
168-
if (nk_tooltip_begin_offset(ctx, (float)text_width, offset)) {
124+
if (nk_tooltip_begin_pos_offset(ctx, (float)text_width, position, offset)) {
169125
nk_layout_row_dynamic(ctx, (float)text_height, 1);
170126
nk_text(ctx, text, text_len, NK_TEXT_LEFT);
171127
nk_tooltip_end(ctx);

0 commit comments

Comments
 (0)