Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend window's panel clipping area to take into account the padding #704

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions src/nuklear_panel.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,100 @@ nk_panel_begin(struct nk_context *ctx, const char *title, enum nk_panel_type pan
layout->clip = layout->bounds;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -156,6 +156,8 @@ nk_panel_begin(struct nk_context *ctx, const char *title, enum nk_panel_type pan
     layout->bounds = win->bounds;
     layout->bounds.x += panel_padding.x;
     layout->bounds.w -= 2*panel_padding.x;
+    layout->bounds.y += panel_padding.y;
+    layout->bounds.h -= 2*panel_padding.y;
     if (win->flags & NK_WINDOW_BORDER) {
         layout->border = nk_panel_get_border(style, win->flags, panel_type);
         layout->bounds = nk_shrink_rect(layout->bounds, layout->border);
@@ -317,102 +319,12 @@ nk_panel_begin(struct nk_context *ctx, const char *title, enum nk_panel_type pan
     /* set clipping rectangle */
     {struct nk_rect clip;
     layout->clip = layout->bounds;
+    layout->clip.x -= panel_padding.x;
+    layout->clip.w += 2*panel_padding.x;
+    layout->clip.y -= panel_padding.y;
+    layout->clip.h += 2*panel_padding.y;
     nk_unify(&clip, &win->buffer.clip, layout->clip.x, layout->clip.y,
         layout->clip.x + layout->clip.w, layout->clip.y + layout->clip.h);

Try these changes instead, instead of changing the clip after nk_unify.
Fixes the issue with nk_tree while doing the thing you intended.

(Note that with vertical bounds changed like this, windows' heights in user code may need slight increase to accommodate; e.g. in one of my use case I need to increase the default window height to hide vertical scrollbar again)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also while looking at this I've noticed that in nk_panel_get_padding/nk_panel_get_border/nk_panel_get_border_color, case NK_PANEL_TOOLTIP uses menu_* instead of tooltip_* in the current code base. Might as well fix this while we're at it.

Edit: patch here

--- a/src/nuklear_panel.c
+++ b/src/nuklear_panel.c
@@ -42,7 +42,7 @@ nk_panel_get_padding(const struct nk_style *style, enum nk_panel_type type)
     case NK_PANEL_CONTEXTUAL: return style->window.contextual_padding;
     case NK_PANEL_COMBO: return style->window.combo_padding;
     case NK_PANEL_MENU: return style->window.menu_padding;
-    case NK_PANEL_TOOLTIP: return style->window.menu_padding;}
+    case NK_PANEL_TOOLTIP: return style->window.tooltip_padding;}
 }
 NK_LIB float
 nk_panel_get_border(const struct nk_style *style, nk_flags flags,
@@ -57,7 +57,7 @@ nk_panel_get_border(const struct nk_style *style, nk_flags flags,
         case NK_PANEL_CONTEXTUAL: return style->window.contextual_border;
         case NK_PANEL_COMBO: return style->window.combo_border;
         case NK_PANEL_MENU: return style->window.menu_border;
-        case NK_PANEL_TOOLTIP: return style->window.menu_border;
+        case NK_PANEL_TOOLTIP: return style->window.tooltip_border;
     }} else return 0;
 }
 NK_LIB struct nk_color
@@ -71,7 +71,7 @@ nk_panel_get_border_color(const struct nk_style *style, enum nk_panel_type type)
     case NK_PANEL_CONTEXTUAL: return style->window.contextual_border_color;
     case NK_PANEL_COMBO: return style->window.combo_border_color;
     case NK_PANEL_MENU: return style->window.menu_border_color;
-    case NK_PANEL_TOOLTIP: return style->window.menu_border_color;}
+    case NK_PANEL_TOOLTIP: return style->window.tooltip_border_color;}
 }
 NK_LIB nk_bool
 nk_panel_is_sub(enum nk_panel_type type)

Hopefully this doesn't break anything.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found out that the layout->bounds.y and layout->bounds.h change breaks group layout.

Can be fixed by

--- a/src/nuklear_group.c
+++ b/src/nuklear_group.c
@@ -82,10 +82,10 @@ nk_group_scrolled_end(struct nk_context *ctx)
     /* dummy window */
     nk_zero_struct(pan);
     panel_padding = nk_panel_get_padding(&ctx->style, NK_PANEL_GROUP);
-    pan.bounds.y = g->bounds.y - (g->header_height + g->menu.h);
+    pan.bounds.y = g->bounds.y - (g->header_height + g->menu.h) - panel_padding.y;
     pan.bounds.x = g->bounds.x - panel_padding.x;
     pan.bounds.w = g->bounds.w + 2 * panel_padding.x;
-    pan.bounds.h = g->bounds.h + g->header_height + g->menu.h;
+    pan.bounds.h = g->bounds.h + g->header_height + g->menu.h + 2 * panel_padding.y;
     if (g->flags & NK_WINDOW_BORDER) {
         pan.bounds.x -= g->border;
         pan.bounds.y -= g->border;
@@ -106,7 +106,7 @@ nk_group_scrolled_end(struct nk_context *ctx)

     /* make sure group has correct clipping rectangle */
     nk_unify(&clip, &parent->clip, pan.bounds.x, pan.bounds.y,
-        pan.bounds.x + pan.bounds.w, pan.bounds.y + pan.bounds.h + panel_padding.x);
+        pan.bounds.x + pan.bounds.w, pan.bounds.y + pan.bounds.h);
     nk_push_scissor(&pan.buffer, clip);
     nk_end(ctx);

And maybe this change as well (didn't break anything visibly but this change make things consistent)

--- a/src/nuklear_panel.c
+++ b/src/nuklear_panel.c
@@ -475,7 +475,7 @@ nk_panel_end(struct nk_context *ctx)
             /* horizontal scrollbar */
             nk_flags state = 0;
             scroll.x = layout->bounds.x;
-            scroll.y = layout->bounds.y + layout->bounds.h;
+            scroll.y = layout->bounds.y + layout->bounds.h + panel_padding.y;
             scroll.w = layout->bounds.w;
             scroll.h = scrollbar_size.y;

nk_unify(&clip, &win->buffer.clip, layout->clip.x, layout->clip.y,
layout->clip.x + layout->clip.w, layout->clip.y + layout->clip.h);


if (panel_type == NK_PANEL_WINDOW)
{
/* extend clipping area to allow borders to be properly drawn */
clip.x -= style->window.padding.x;
clip.y -= style->window.padding.y;
clip.w += style->window.padding.x*2;
clip.h += style->window.padding.y*2;
/* ensure clipping area doesn't exceed window bounds */
clip.x = NK_MAX(clip.x, win->bounds.x);
clip.w = NK_MIN(clip.w, win->bounds.w);
clip.y = NK_MAX(clip.y, win->bounds.y+layout->header_height);
clip.h = NK_MIN(clip.h, win->bounds.h-layout->header_height);
}
else if (panel_type == NK_PANEL_GROUP)
{
/* extend clipping area to allow borders to be properly drawn */
clip.x -= style->window.group_padding.x;
clip.y -= style->window.group_padding.y;
clip.w += style->window.group_padding.x*2;
clip.h += style->window.group_padding.y*2;
/* ensure clipping area doesn't exceed window bounds */
clip.x = NK_MAX(clip.x, win->bounds.x);
clip.w = NK_MIN(clip.w, win->bounds.w);
clip.y = NK_MAX(clip.y, win->bounds.y+layout->header_height);
clip.h = NK_MIN(clip.h, win->bounds.h-layout->header_height);
}
else if (panel_type == NK_PANEL_POPUP)
{
/* extend clipping area to allow borders to be properly drawn */
clip.x -= style->window.popup_padding.x;
clip.y -= style->window.popup_padding.y;
clip.w += style->window.popup_padding.x*2;
clip.h += style->window.popup_padding.y*2;
/* ensure clipping area doesn't exceed window bounds */
clip.x = NK_MAX(clip.x, win->bounds.x);
clip.w = NK_MIN(clip.w, win->bounds.w);
clip.y = NK_MAX(clip.y, win->bounds.y+layout->header_height);
clip.h = NK_MIN(clip.h, win->bounds.h-layout->header_height);
}
else if (panel_type == NK_PANEL_CONTEXTUAL)
{
/* extend clipping area to allow borders to be properly drawn */
clip.x -= style->window.contextual_padding.x;
clip.y -= style->window.contextual_padding.y;
clip.w += style->window.contextual_padding.x*2;
clip.h += style->window.contextual_padding.y*2;
/* ensure clipping area doesn't exceed window bounds */
clip.x = NK_MAX(clip.x, win->bounds.x);
clip.w = NK_MIN(clip.w, win->bounds.w);
clip.y = NK_MAX(clip.y, win->bounds.y+layout->header_height);
clip.h = NK_MIN(clip.h, win->bounds.h-layout->header_height);
}
else if (panel_type == NK_PANEL_MENU)
{
/* extend clipping area to allow borders to be properly drawn */
clip.x -= style->window.menu_padding.x;
clip.y -= style->window.menu_padding.y;
clip.w += style->window.menu_padding.x*2;
clip.h += style->window.menu_padding.y*2;
/* ensure clipping area doesn't exceed window bounds */
clip.x = NK_MAX(clip.x, win->bounds.x);
clip.w = NK_MIN(clip.w, win->bounds.w);
clip.y = NK_MAX(clip.y, win->bounds.y+layout->header_height);
clip.h = NK_MIN(clip.h, win->bounds.h-layout->header_height);
}
else if (panel_type == NK_PANEL_TOOLTIP)
{
/* extend clipping area to allow borders to be properly drawn */
clip.x -= style->window.tooltip_padding.x;
clip.y -= style->window.tooltip_padding.y;
clip.w += style->window.tooltip_padding.x*2;
clip.h += style->window.tooltip_padding.y*2;
/* ensure clipping area doesn't exceed window bounds */
clip.x = NK_MAX(clip.x, win->bounds.x);
clip.w = NK_MIN(clip.w, win->bounds.w);
clip.y = NK_MAX(clip.y, win->bounds.y+layout->header_height);
clip.h = NK_MIN(clip.h, win->bounds.h-layout->header_height);
}
else if (panel_type == NK_PANEL_COMBO)
{
/* extend clipping area to allow borders to be properly drawn */
clip.x -= style->window.combo_padding.x;
clip.y -= style->window.combo_padding.y;
clip.w += style->window.combo_padding.x*2;
clip.h += style->window.combo_padding.y*2;
/* ensure clipping area doesn't exceed window bounds */
clip.x = NK_MAX(clip.x, win->bounds.x);
clip.w = NK_MIN(clip.w, win->bounds.w);
clip.y = NK_MAX(clip.y, win->bounds.y+layout->header_height);
clip.h = NK_MIN(clip.h, win->bounds.h-layout->header_height);
}

nk_push_scissor(out, clip);
layout->clip = clip;}
return !(layout->flags & NK_WINDOW_HIDDEN) && !(layout->flags & NK_WINDOW_MINIMIZED);
Expand Down