First pass at smart popup positioning when it's clipped by window

This commit is contained in:
Robert Winkler
2026-11-26 01:36:11 -05:00
committed by Rob Loach
parent bab18497df
commit fb492abf08
13 changed files with 461 additions and 14 deletions
+4
View File
@@ -556,6 +556,8 @@ nk_glfw3_init(GLFWwindow *win, enum nk_glfw_init_state init_state,
glfw.ctx.clip.copy = nk_glfw3_clipboard_copy;
glfw.ctx.clip.paste = nk_glfw3_clipboard_paste;
glfw.ctx.clip.userdata = nk_handle_ptr(0);
glfwGetWindowSize(win, &glfw.width, &glfw.height);
nk_set_display_size(&glfw.ctx, (float)glfw.width, (float)glfw.height);
glfw.last_button_click = 0;
{struct nk_glfw_device *dev = &glfw.ogl;
@@ -608,6 +610,8 @@ nk_glfw3_new_frame(void)
glfwGetFramebufferSize(win, &glfw.display_width, &glfw.display_height);
glfw.fb_scale.x = (float)glfw.display_width/(float)glfw.width;
glfw.fb_scale.y = (float)glfw.display_height/(float)glfw.height;
/* logical window size, not framebuffer size */
nk_set_display_size(ctx, (float)glfw.width, (float)glfw.height);
nk_input_begin(ctx);
for (i = 0; i < glfw.text_len; ++i)
+2
View File
@@ -844,6 +844,7 @@ nk_rawfb_init(void *fb, void *tex_mem, const unsigned int w, const unsigned int
free(rawfb);
return NULL;
}
nk_set_display_size(&rawfb->ctx, (float)w, (float)h);
nk_font_atlas_init_default(&rawfb->atlas);
nk_font_atlas_begin(&rawfb->atlas);
@@ -1025,6 +1026,7 @@ nk_rawfb_resize_fb(struct rawfb_context *rawfb,
rawfb->fb.pixels = fb;
rawfb->fb.pitch = pitch;
rawfb->fb.pl = pl;
nk_set_display_size(&rawfb->ctx, (float)w, (float)h);
}
NK_API void
@@ -397,6 +397,9 @@ nk_sdl_init(SDL_Window *win, SDL_Renderer *renderer, struct nk_allocator allocat
sdl->win = win;
sdl->renderer = renderer;
nk_init(&sdl->ctx, &sdl->allocator, 0);
{int dw = 0, dh = 0;
if (SDL_GetWindowSize(win, &dw, &dh))
nk_set_display_size(&sdl->ctx, (float)dw, (float)dh);}
sdl->ctx.userdata = nk_handle_ptr((void*)sdl);
sdl->ctx.clip.copy = nk_sdl_clipboard_copy;
sdl->ctx.clip.paste = nk_sdl_clipboard_paste;
@@ -575,6 +578,10 @@ nk_sdl_handle_event(struct nk_context* ctx, SDL_Event *evt)
case SDL_EVENT_MOUSE_WHEEL:
nk_input_scroll(ctx, nk_vec2(evt->wheel.x, evt->wheel.y));
return 1;
case SDL_EVENT_WINDOW_RESIZED:
nk_set_display_size(ctx, (float)evt->window.data1, (float)evt->window.data2);
return 1;
}
return 0;
}
+224 -7
View File
@@ -577,6 +577,8 @@ enum nk_symbol_type {
* \ref nk_clear | Called at the end of the frame to reset and prepare the context for the next frame
* \ref nk_free | Shutdown and free all memory allocated inside the context
* \ref nk_set_user_data| Utility function to pass user data to draw command
* \ref nk_set_display_size | Sets the backend surface size used to keep popups on-screen
* \ref nk_set_display_bounds | Sets a display rectangle (non-zero origin) used to keep popups on-screen
*/
#ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
@@ -705,6 +707,48 @@ NK_API void nk_free(struct nk_context*);
*/
NK_API void nk_set_user_data(struct nk_context*, nk_handle handle);
#endif
/**
* \brief Sets the size of the surface nuklear is drawn into.
*
* \details
* Comboboxes, menus, contextuals and tooltips use this to stay fully visible
* instead of being clipped by the OS window / framebuffer. Explicit
* `nk_popup_begin` rects are not moved.
*
* Coordinates must match widget and mouse space (logical window pixels, not
* framebuffer pixels on HiDPI). A width or height of 0 disables fitting and is
* the default until this is called.
*
* The value is retained. Call once after `nk_init*` and again whenever the
* surface size changes. It does not need to be set every frame.
*
* ```c
* void nk_set_display_size(struct nk_context *ctx, float width, float height);
* ```
*
* \param[in] ctx Must point to a previously initialized `nk_context` struct
* \param[in] width Surface width in the same space as `nk_input_motion`
* \param[in] height Surface height in the same space as `nk_input_motion`
*/
NK_API void nk_set_display_size(struct nk_context *ctx, float width, float height);
/**
* \brief Sets the rectangle of the surface nuklear is drawn into.
*
* \details
* Same as `nk_set_display_size` but allows a non-zero origin, for example when
* the UI is drawn into a sub-rectangle of a window. An empty rectangle
* (`w == 0` or `h == 0`) disables fitting.
*
* ```c
* void nk_set_display_bounds(struct nk_context *ctx, struct nk_rect bounds);
* ```
*
* \param[in] ctx Must point to a previously initialized `nk_context` struct
* \param[in] bounds Surface rectangle in the same space as `nk_input_motion`
*/
NK_API void nk_set_display_bounds(struct nk_context *ctx, struct nk_rect bounds);
/* =============================================================================
*
* INPUT
@@ -5994,6 +6038,9 @@ struct nk_context {
enum nk_button_behavior button_behavior;
struct nk_configuration_stacks stacks;
float delta_time_seconds;
/** surface nuklear is drawn into, in widget/mouse space.
* `w == 0` or `h == 0` means unset (popup fitting disabled). */
struct nk_rect display_bounds;
/* private:
should only be accessed if you
@@ -6359,6 +6406,12 @@ NK_LIB void nk_panel_alloc_space(struct nk_rect *bounds, const struct nk_context
NK_LIB void nk_layout_peek(struct nk_rect *bounds, const struct nk_context *ctx);
/* popup */
enum nk_popup_fit {
NK_POPUP_FIT_FLIP, /* combo, menu: flip around the trigger */
NK_POPUP_FIT_SLIDE, /* contextual: slide to stay on-screen */
NK_POPUP_FIT_TOOLTIP /* tooltip: flip around cursor, then slide */
};
NK_LIB struct nk_rect nk_fit_popup_rect(const struct nk_context *ctx, struct nk_rect body, struct nk_rect anchor, enum nk_popup_fit fit, float known_h);
NK_LIB nk_bool nk_nonblock_begin(struct nk_context *ctx, nk_flags flags, struct nk_rect body, struct nk_rect header, enum nk_panel_type panel_type);
/* text */
@@ -19677,6 +19730,27 @@ nk_set_user_data(struct nk_context *ctx, nk_handle handle)
}
#endif
NK_API void
nk_set_display_size(struct nk_context *ctx, float width, float height)
{
NK_ASSERT(ctx);
if (!ctx) return;
if (width < 0) width = 0;
if (height < 0) height = 0;
ctx->display_bounds.x = 0;
ctx->display_bounds.y = 0;
ctx->display_bounds.w = width;
ctx->display_bounds.h = height;
}
NK_API void
nk_set_display_bounds(struct nk_context *ctx, struct nk_rect bounds)
{
NK_ASSERT(ctx);
if (!ctx) return;
if (bounds.w < 0) bounds.w = 0;
if (bounds.h < 0) bounds.h = 0;
ctx->display_bounds = bounds;
}
NK_API void
nk_free(struct nk_context *ctx)
{
NK_ASSERT(ctx);
@@ -20742,6 +20816,17 @@ nk_panel_end(struct nk_context *ctx)
}
window->flags = layout->flags;
/* persist actual popup height so the next frame can flip/slide without
* using the caller-supplied maximum (DYNAMIC shrinks from the top) */
if (((int)layout->type & (int)NK_PANEL_SET_POPUP) &&
(layout->flags & NK_WINDOW_DYNAMIC) &&
!(layout->flags & NK_WINDOW_MINIMIZED))
{
float bottom = layout->bounds.y + layout->bounds.h + layout->footer_height;
if (bottom > window->bounds.y)
window->bounds.h = bottom - window->bounds.y;
}
/* property garbage collector */
if (window->property.active && window->property.old != window->property.seq &&
window->property.active == window->property.prev) {
@@ -21464,6 +21549,101 @@ nk_rule_horizontal(struct nk_context *ctx, struct nk_color color, nk_bool roundi
* POPUP
*
* ===============================================================*/
NK_LIB struct nk_rect
nk_fit_popup_rect(const struct nk_context *ctx, struct nk_rect body,
struct nk_rect anchor, enum nk_popup_fit fit, float known_h)
{
struct nk_rect d;
float h;
float overlap;
float space_below;
float space_above;
float d_right;
float d_bottom;
if (!ctx) return body;
d = ctx->display_bounds;
if (d.w <= 0 || d.h <= 0) return body;
d_right = d.x + d.w;
d_bottom = d.y + d.h;
h = (known_h > 0.0f) ? known_h : body.h;
overlap = (anchor.y + anchor.h) - body.y;
/* horizontal: flip/align first, then slide, then shrink */
if (fit == NK_POPUP_FIT_FLIP) {
if (body.x + body.w > d_right)
body.x = anchor.x + anchor.w - body.w;
} else if (fit == NK_POPUP_FIT_TOOLTIP) {
if (body.x + body.w > d_right) {
float flipped = anchor.x - body.w;
if (flipped >= d.x)
body.x = flipped;
}
}
if (body.x + body.w > d_right)
body.x = d_right - body.w;
if (body.x < d.x)
body.x = d.x;
if (body.x + body.w > d_right) {
body.w = d_right - body.x;
if (body.w < 1.0f) body.w = 1.0f;
}
/* vertical: flip only when actual height is known so DYNAMIC
* shrink-from-top does not leave a gap above the trigger */
space_below = d_bottom - body.y;
space_above = anchor.y - d.y;
if (fit == NK_POPUP_FIT_FLIP) {
if (h > space_below) {
if (known_h > 0.0f && known_h <= space_above) {
body.y = anchor.y - known_h;
if (overlap > 0.0f)
body.y += overlap;
} else if (known_h <= 0.0f) {
if (space_below > 0.0f)
body.h = space_below;
} else if (space_above > space_below) {
body.y = d.y;
body.h = space_above;
if (body.h < 1.0f) body.h = 1.0f;
} else if (space_below > 0.0f) {
body.h = space_below;
}
}
} else if (fit == NK_POPUP_FIT_SLIDE) {
if (known_h > 0.0f) {
if (body.y + known_h > d_bottom)
body.y = d_bottom - known_h;
if (body.y < d.y)
body.y = d.y;
if (body.y + known_h > d_bottom) {
body.h = d_bottom - body.y;
if (body.h < 1.0f) body.h = 1.0f;
}
} else if (body.h > space_below && space_below > 0.0f) {
body.h = space_below;
}
} else {
if (known_h > 0.0f && body.y + known_h > d_bottom) {
float flipped = anchor.y - known_h;
if (flipped >= d.y)
body.y = flipped;
}
if (known_h > 0.0f) {
if (body.y + known_h > d_bottom)
body.y = d_bottom - known_h;
if (body.y < d.y)
body.y = d.y;
if (body.y + known_h > d_bottom) {
body.h = d_bottom - body.y;
if (body.h < 1.0f) body.h = 1.0f;
}
}
}
return body;
}
NK_API nk_bool
nk_popup_begin(struct nk_context *ctx, enum nk_popup_type type,
const char *title, nk_flags flags, struct nk_rect rect)
@@ -21780,8 +21960,15 @@ nk_contextual_begin(struct nk_context *ctx, nk_flags flags, struct nk_vec2 size,
body.w = size.x;
body.h = size.y;
{float known_h = (!is_clicked && popup) ? popup->bounds.h : 0;
float req_h = body.h;
body = nk_fit_popup_rect(ctx, body, body, NK_POPUP_FIT_SLIDE, known_h);
flags |= NK_WINDOW_NO_SCROLLBAR;
if (body.h + 0.5f < req_h)
flags &= ~(nk_flags)NK_WINDOW_NO_SCROLLBAR;}
/* start nonblocking contextual popup */
ret = nk_nonblock_begin(ctx, flags | NK_WINDOW_NO_SCROLLBAR, body,
ret = nk_nonblock_begin(ctx, flags, body,
null_rect, NK_PANEL_CONTEXTUAL);
if (ret) win->popup.type = NK_PANEL_CONTEXTUAL;
else {
@@ -22052,8 +22239,14 @@ nk_menu_begin(struct nk_context *ctx, struct nk_window *win,
is_active = (popup && (win->popup.name == hash) && win->popup.type == NK_PANEL_MENU);
if ((is_clicked && is_open && !is_active) || (is_open && !is_active) ||
(!is_open && !is_active && !is_clicked)) return 0;
if (!nk_nonblock_begin(ctx, NK_WINDOW_NO_SCROLLBAR, body, header, NK_PANEL_MENU))
return 0;
{nk_flags flags = NK_WINDOW_NO_SCROLLBAR;
float known_h = (is_active && popup) ? popup->bounds.h : 0;
float req_h = body.h;
body = nk_fit_popup_rect(ctx, body, header, NK_POPUP_FIT_FLIP, known_h);
if (body.h + 0.5f < req_h)
flags = 0;
if (!nk_nonblock_begin(ctx, flags, body, header, NK_PANEL_MENU))
return 0;}
win->popup.type = NK_PANEL_MENU;
win->popup.name = hash;
@@ -30372,6 +30565,8 @@ nk_combo_begin(struct nk_context *ctx, struct nk_window *win,
is_active = (popup && (win->popup.name == hash) && win->popup.type == NK_PANEL_COMBO);
if ((is_clicked && is_open && !is_active) || (is_open && !is_active) ||
(!is_open && !is_active && !is_clicked)) return 0;
{float known_h = (is_active && popup) ? popup->bounds.h : 0;
body = nk_fit_popup_rect(ctx, body, header, NK_POPUP_FIT_FLIP, known_h);}
if (!nk_nonblock_begin(ctx, 0, body,
(is_clicked && is_open)?nk_rect(0,0,0,0):header, NK_PANEL_COMBO)) return 0;
@@ -31258,10 +31453,32 @@ nk_tooltip_begin_offset(struct nk_context *ctx, float width, enum nk_tooltip_pos
NK_ASSERT(0 && "Invalid tooltip position");
}
bounds.x = (float)x;
bounds.y = (float)y;
bounds.w = (float)w;
bounds.h = (float)nk_iceilf(nk_null_rect.h);
{struct nk_rect screen;
struct nk_rect anchor;
float known_h = (float)h;
float clip_x = win->layout->clip.x;
float clip_y = win->layout->clip.y;
if (win->popup.win && win->popup.type == NK_PANEL_TOOLTIP &&
win->popup.win->bounds.h > known_h)
known_h = win->popup.win->bounds.h;
screen.x = (float)x + clip_x;
screen.y = (float)y + clip_y;
screen.w = (float)w;
screen.h = known_h;
anchor.x = in->mouse.pos.x;
anchor.y = in->mouse.pos.y;
anchor.w = 1;
anchor.h = 1;
screen = nk_fit_popup_rect(ctx, screen, anchor, NK_POPUP_FIT_TOOLTIP, known_h);
bounds.x = screen.x - clip_x;
bounds.y = screen.y - clip_y;
bounds.w = screen.w;
/* keep a large max so NK_POPUP_DYNAMIC can grow; the fitter only
* shrinks h when the tooltip cannot fit on the surface at all */
bounds.h = (screen.h + 0.5f < known_h) ? screen.h : (float)nk_iceilf(nk_null_rect.h);}
ret = nk_popup_begin(ctx, NK_POPUP_DYNAMIC,
"__##Tooltip##__", NK_WINDOW_NO_SCROLLBAR|NK_WINDOW_BORDER, bounds);
+47
View File
@@ -387,6 +387,8 @@ enum nk_symbol_type {
* \ref nk_clear | Called at the end of the frame to reset and prepare the context for the next frame
* \ref nk_free | Shutdown and free all memory allocated inside the context
* \ref nk_set_user_data| Utility function to pass user data to draw command
* \ref nk_set_display_size | Sets the backend surface size used to keep popups on-screen
* \ref nk_set_display_bounds | Sets a display rectangle (non-zero origin) used to keep popups on-screen
*/
#ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
@@ -515,6 +517,48 @@ NK_API void nk_free(struct nk_context*);
*/
NK_API void nk_set_user_data(struct nk_context*, nk_handle handle);
#endif
/**
* \brief Sets the size of the surface nuklear is drawn into.
*
* \details
* Comboboxes, menus, contextuals and tooltips use this to stay fully visible
* instead of being clipped by the OS window / framebuffer. Explicit
* `nk_popup_begin` rects are not moved.
*
* Coordinates must match widget and mouse space (logical window pixels, not
* framebuffer pixels on HiDPI). A width or height of 0 disables fitting and is
* the default until this is called.
*
* The value is retained. Call once after `nk_init*` and again whenever the
* surface size changes. It does not need to be set every frame.
*
* ```c
* void nk_set_display_size(struct nk_context *ctx, float width, float height);
* ```
*
* \param[in] ctx Must point to a previously initialized `nk_context` struct
* \param[in] width Surface width in the same space as `nk_input_motion`
* \param[in] height Surface height in the same space as `nk_input_motion`
*/
NK_API void nk_set_display_size(struct nk_context *ctx, float width, float height);
/**
* \brief Sets the rectangle of the surface nuklear is drawn into.
*
* \details
* Same as `nk_set_display_size` but allows a non-zero origin, for example when
* the UI is drawn into a sub-rectangle of a window. An empty rectangle
* (`w == 0` or `h == 0`) disables fitting.
*
* ```c
* void nk_set_display_bounds(struct nk_context *ctx, struct nk_rect bounds);
* ```
*
* \param[in] ctx Must point to a previously initialized `nk_context` struct
* \param[in] bounds Surface rectangle in the same space as `nk_input_motion`
*/
NK_API void nk_set_display_bounds(struct nk_context *ctx, struct nk_rect bounds);
/* =============================================================================
*
* INPUT
@@ -5804,6 +5848,9 @@ struct nk_context {
enum nk_button_behavior button_behavior;
struct nk_configuration_stacks stacks;
float delta_time_seconds;
/** surface nuklear is drawn into, in widget/mouse space.
* `w == 0` or `h == 0` means unset (popup fitting disabled). */
struct nk_rect display_bounds;
/* private:
should only be accessed if you
+2
View File
@@ -49,6 +49,8 @@ nk_combo_begin(struct nk_context *ctx, struct nk_window *win,
is_active = (popup && (win->popup.name == hash) && win->popup.type == NK_PANEL_COMBO);
if ((is_clicked && is_open && !is_active) || (is_open && !is_active) ||
(!is_open && !is_active && !is_clicked)) return 0;
{float known_h = (is_active && popup) ? popup->bounds.h : 0;
body = nk_fit_popup_rect(ctx, body, header, NK_POPUP_FIT_FLIP, known_h);}
if (!nk_nonblock_begin(ctx, 0, body,
(is_clicked && is_open)?nk_rect(0,0,0,0):header, NK_PANEL_COMBO)) return 0;
+21
View File
@@ -85,6 +85,27 @@ nk_set_user_data(struct nk_context *ctx, nk_handle handle)
}
#endif
NK_API void
nk_set_display_size(struct nk_context *ctx, float width, float height)
{
NK_ASSERT(ctx);
if (!ctx) return;
if (width < 0) width = 0;
if (height < 0) height = 0;
ctx->display_bounds.x = 0;
ctx->display_bounds.y = 0;
ctx->display_bounds.w = width;
ctx->display_bounds.h = height;
}
NK_API void
nk_set_display_bounds(struct nk_context *ctx, struct nk_rect bounds)
{
NK_ASSERT(ctx);
if (!ctx) return;
if (bounds.w < 0) bounds.w = 0;
if (bounds.h < 0) bounds.h = 0;
ctx->display_bounds = bounds;
}
NK_API void
nk_free(struct nk_context *ctx)
{
NK_ASSERT(ctx);
+8 -1
View File
@@ -57,8 +57,15 @@ nk_contextual_begin(struct nk_context *ctx, nk_flags flags, struct nk_vec2 size,
body.w = size.x;
body.h = size.y;
{float known_h = (!is_clicked && popup) ? popup->bounds.h : 0;
float req_h = body.h;
body = nk_fit_popup_rect(ctx, body, body, NK_POPUP_FIT_SLIDE, known_h);
flags |= NK_WINDOW_NO_SCROLLBAR;
if (body.h + 0.5f < req_h)
flags &= ~(nk_flags)NK_WINDOW_NO_SCROLLBAR;}
/* start nonblocking contextual popup */
ret = nk_nonblock_begin(ctx, flags | NK_WINDOW_NO_SCROLLBAR, body,
ret = nk_nonblock_begin(ctx, flags, body,
null_rect, NK_PANEL_CONTEXTUAL);
if (ret) win->popup.type = NK_PANEL_CONTEXTUAL;
else {
+6
View File
@@ -246,6 +246,12 @@ NK_LIB void nk_panel_alloc_space(struct nk_rect *bounds, const struct nk_context
NK_LIB void nk_layout_peek(struct nk_rect *bounds, const struct nk_context *ctx);
/* popup */
enum nk_popup_fit {
NK_POPUP_FIT_FLIP, /* combo, menu: flip around the trigger */
NK_POPUP_FIT_SLIDE, /* contextual: slide to stay on-screen */
NK_POPUP_FIT_TOOLTIP /* tooltip: flip around cursor, then slide */
};
NK_LIB struct nk_rect nk_fit_popup_rect(const struct nk_context *ctx, struct nk_rect body, struct nk_rect anchor, enum nk_popup_fit fit, float known_h);
NK_LIB nk_bool nk_nonblock_begin(struct nk_context *ctx, nk_flags flags, struct nk_rect body, struct nk_rect header, enum nk_panel_type panel_type);
/* text */
+8 -2
View File
@@ -102,8 +102,14 @@ nk_menu_begin(struct nk_context *ctx, struct nk_window *win,
is_active = (popup && (win->popup.name == hash) && win->popup.type == NK_PANEL_MENU);
if ((is_clicked && is_open && !is_active) || (is_open && !is_active) ||
(!is_open && !is_active && !is_clicked)) return 0;
if (!nk_nonblock_begin(ctx, NK_WINDOW_NO_SCROLLBAR, body, header, NK_PANEL_MENU))
return 0;
{nk_flags flags = NK_WINDOW_NO_SCROLLBAR;
float known_h = (is_active && popup) ? popup->bounds.h : 0;
float req_h = body.h;
body = nk_fit_popup_rect(ctx, body, header, NK_POPUP_FIT_FLIP, known_h);
if (body.h + 0.5f < req_h)
flags = 0;
if (!nk_nonblock_begin(ctx, flags, body, header, NK_PANEL_MENU))
return 0;}
win->popup.type = NK_PANEL_MENU;
win->popup.name = hash;
+11
View File
@@ -586,6 +586,17 @@ nk_panel_end(struct nk_context *ctx)
}
window->flags = layout->flags;
/* persist actual popup height so the next frame can flip/slide without
* using the caller-supplied maximum (DYNAMIC shrinks from the top) */
if (((int)layout->type & (int)NK_PANEL_SET_POPUP) &&
(layout->flags & NK_WINDOW_DYNAMIC) &&
!(layout->flags & NK_WINDOW_MINIMIZED))
{
float bottom = layout->bounds.y + layout->bounds.h + layout->footer_height;
if (bottom > window->bounds.y)
window->bounds.h = bottom - window->bounds.y;
}
/* property garbage collector */
if (window->property.active && window->property.old != window->property.seq &&
window->property.active == window->property.prev) {
+95
View File
@@ -6,6 +6,101 @@
* POPUP
*
* ===============================================================*/
NK_LIB struct nk_rect
nk_fit_popup_rect(const struct nk_context *ctx, struct nk_rect body,
struct nk_rect anchor, enum nk_popup_fit fit, float known_h)
{
struct nk_rect d;
float h;
float overlap;
float space_below;
float space_above;
float d_right;
float d_bottom;
if (!ctx) return body;
d = ctx->display_bounds;
if (d.w <= 0 || d.h <= 0) return body;
d_right = d.x + d.w;
d_bottom = d.y + d.h;
h = (known_h > 0.0f) ? known_h : body.h;
overlap = (anchor.y + anchor.h) - body.y;
/* horizontal: flip/align first, then slide, then shrink */
if (fit == NK_POPUP_FIT_FLIP) {
if (body.x + body.w > d_right)
body.x = anchor.x + anchor.w - body.w;
} else if (fit == NK_POPUP_FIT_TOOLTIP) {
if (body.x + body.w > d_right) {
float flipped = anchor.x - body.w;
if (flipped >= d.x)
body.x = flipped;
}
}
if (body.x + body.w > d_right)
body.x = d_right - body.w;
if (body.x < d.x)
body.x = d.x;
if (body.x + body.w > d_right) {
body.w = d_right - body.x;
if (body.w < 1.0f) body.w = 1.0f;
}
/* vertical: flip only when actual height is known so DYNAMIC
* shrink-from-top does not leave a gap above the trigger */
space_below = d_bottom - body.y;
space_above = anchor.y - d.y;
if (fit == NK_POPUP_FIT_FLIP) {
if (h > space_below) {
if (known_h > 0.0f && known_h <= space_above) {
body.y = anchor.y - known_h;
if (overlap > 0.0f)
body.y += overlap;
} else if (known_h <= 0.0f) {
if (space_below > 0.0f)
body.h = space_below;
} else if (space_above > space_below) {
body.y = d.y;
body.h = space_above;
if (body.h < 1.0f) body.h = 1.0f;
} else if (space_below > 0.0f) {
body.h = space_below;
}
}
} else if (fit == NK_POPUP_FIT_SLIDE) {
if (known_h > 0.0f) {
if (body.y + known_h > d_bottom)
body.y = d_bottom - known_h;
if (body.y < d.y)
body.y = d.y;
if (body.y + known_h > d_bottom) {
body.h = d_bottom - body.y;
if (body.h < 1.0f) body.h = 1.0f;
}
} else if (body.h > space_below && space_below > 0.0f) {
body.h = space_below;
}
} else {
if (known_h > 0.0f && body.y + known_h > d_bottom) {
float flipped = anchor.y - known_h;
if (flipped >= d.y)
body.y = flipped;
}
if (known_h > 0.0f) {
if (body.y + known_h > d_bottom)
body.y = d_bottom - known_h;
if (body.y < d.y)
body.y = d.y;
if (body.y + known_h > d_bottom) {
body.h = d_bottom - body.y;
if (body.h < 1.0f) body.h = 1.0f;
}
}
}
return body;
}
NK_API nk_bool
nk_popup_begin(struct nk_context *ctx, enum nk_popup_type type,
const char *title, nk_flags flags, struct nk_rect rect)
+26 -4
View File
@@ -80,10 +80,32 @@ nk_tooltip_begin_offset(struct nk_context *ctx, float width, enum nk_tooltip_pos
NK_ASSERT(0 && "Invalid tooltip position");
}
bounds.x = (float)x;
bounds.y = (float)y;
bounds.w = (float)w;
bounds.h = (float)nk_iceilf(nk_null_rect.h);
{struct nk_rect screen;
struct nk_rect anchor;
float known_h = (float)h;
float clip_x = win->layout->clip.x;
float clip_y = win->layout->clip.y;
if (win->popup.win && win->popup.type == NK_PANEL_TOOLTIP &&
win->popup.win->bounds.h > known_h)
known_h = win->popup.win->bounds.h;
screen.x = (float)x + clip_x;
screen.y = (float)y + clip_y;
screen.w = (float)w;
screen.h = known_h;
anchor.x = in->mouse.pos.x;
anchor.y = in->mouse.pos.y;
anchor.w = 1;
anchor.h = 1;
screen = nk_fit_popup_rect(ctx, screen, anchor, NK_POPUP_FIT_TOOLTIP, known_h);
bounds.x = screen.x - clip_x;
bounds.y = screen.y - clip_y;
bounds.w = screen.w;
/* keep a large max so NK_POPUP_DYNAMIC can grow; the fitter only
* shrinks h when the tooltip cannot fit on the surface at all */
bounds.h = (screen.h + 0.5f < known_h) ? screen.h : (float)nk_iceilf(nk_null_rect.h);}
ret = nk_popup_begin(ctx, NK_POPUP_DYNAMIC,
"__##Tooltip##__", NK_WINDOW_NO_SCROLLBAR|NK_WINDOW_BORDER, bounds);