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

introduce tiling window background, fix nk_image_is_subimage() #444

Closed
Closed
Show file tree
Hide file tree
Changes from 2 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
6 changes: 3 additions & 3 deletions demo/common/canvas.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ canvas_begin(struct nk_context *ctx, struct nk_canvas *canvas, nk_flags flags,
/* save style properties which will be overwritten */
canvas->panel_padding = ctx->style.window.padding;
canvas->item_spacing = ctx->style.window.spacing;
canvas->window_background = ctx->style.window.fixed_background;
canvas->window_background = ctx->style.window.image;

/* use the complete window space and set background */
ctx->style.window.spacing = nk_vec2(0,0);
ctx->style.window.padding = nk_vec2(0,0);
ctx->style.window.fixed_background = nk_style_item_color(background_color);
ctx->style.window.image = nk_style_item_color(background_color);

/* create/update window and set position + size */
if (!nk_begin(ctx, "Canvas", nk_rect(x, y, width, height), NK_WINDOW_NO_SCROLLBAR|flags))
Expand All @@ -42,7 +42,7 @@ canvas_end(struct nk_context *ctx, struct nk_canvas *canvas)
nk_end(ctx);
ctx->style.window.spacing = canvas->panel_padding;
ctx->style.window.padding = canvas->item_spacing;
ctx->style.window.fixed_background = canvas->window_background;
ctx->style.window.image = canvas->window_background;
}

static void
Expand Down
6 changes: 3 additions & 3 deletions example/canvas.c
Original file line number Diff line number Diff line change
Expand Up @@ -364,12 +364,12 @@ canvas_begin(struct nk_context *ctx, struct nk_canvas *canvas, nk_flags flags,
/* save style properties which will be overwritten */
canvas->panel_padding = ctx->style.window.padding;
canvas->item_spacing = ctx->style.window.spacing;
canvas->window_background = ctx->style.window.fixed_background;
canvas->window_background = ctx->style.window.image;

/* use the complete window space and set background */
ctx->style.window.spacing = nk_vec2(0,0);
ctx->style.window.padding = nk_vec2(0,0);
ctx->style.window.fixed_background = nk_style_item_color(background_color);
ctx->style.window.image = nk_style_item_color(background_color);

/* create/update window and set position + size */
flags = flags & ~NK_WINDOW_DYNAMIC;
Expand All @@ -390,7 +390,7 @@ canvas_end(struct nk_context *ctx, struct nk_canvas *canvas)
nk_end(ctx);
ctx->style.window.spacing = canvas->panel_padding;
ctx->style.window.padding = canvas->item_spacing;
ctx->style.window.fixed_background = canvas->window_background;
ctx->style.window.image = canvas->window_background;
}

int main(int argc, char *argv[])
Expand Down
6 changes: 3 additions & 3 deletions example/extended.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ ui_piemenu(struct nk_context *ctx, struct nk_vec2 pos, float radius,

/* pie menu popup */
struct nk_color border = ctx->style.window.border_color;
struct nk_style_item background = ctx->style.window.fixed_background;
ctx->style.window.fixed_background = nk_style_item_hide();
struct nk_style_item background = ctx->style.window.image;
ctx->style.window.image = nk_style_item_hide();
ctx->style.window.border_color = nk_rgba(0,0,0,0);

total_space = nk_window_get_content_region(ctx);
Expand Down Expand Up @@ -167,7 +167,7 @@ ui_piemenu(struct nk_context *ctx, struct nk_vec2 pos, float radius,
ctx->style.window.padding = nk_vec2(8,8);
nk_popup_end(ctx);

ctx->style.window.fixed_background = background;
ctx->style.window.image = background;
ctx->style.window.border_color = border;
return ret;
}
Expand Down
2 changes: 1 addition & 1 deletion example/skinning.c
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ int main(int argc, char *argv[])

/* window */
ctx.style.window.background = nk_rgb(204,204,204);
ctx.style.window.fixed_background = nk_style_item_image(media.window);
ctx.style.window.image = nk_style_item_image(media.window);
ctx.style.window.border_color = nk_rgb(67,67,67);
ctx.style.window.combo_border_color = nk_rgb(67,67,67);
ctx.style.window.contextual_border_color = nk_rgb(67,67,67);
Expand Down
37 changes: 29 additions & 8 deletions nuklear.h
Original file line number Diff line number Diff line change
Expand Up @@ -4840,6 +4840,7 @@ NK_API void nk_draw_list_fill_poly_convex(struct nk_draw_list*, const struct nk_

/* misc */
NK_API void nk_draw_list_add_image(struct nk_draw_list*, struct nk_image texture, struct nk_rect rect, struct nk_color);
NK_API void nk_draw_list_add_image_tiled(struct nk_draw_list*, struct nk_image texture, struct nk_rect rect, struct nk_color);
NK_API void nk_draw_list_add_text(struct nk_draw_list*, const struct nk_user_font*, struct nk_rect, const char *text, int len, float font_height, struct nk_color);
#ifdef NK_INCLUDE_COMMAND_USERDATA
NK_API void nk_draw_list_push_userdata(struct nk_draw_list*, nk_handle userdata);
Expand Down Expand Up @@ -5226,7 +5227,7 @@ struct nk_style_window_header {

struct nk_style_window {
struct nk_style_window_header header;
struct nk_style_item fixed_background;
struct nk_style_item image;
struct nk_color background;

struct nk_color border_color;
Expand All @@ -5246,6 +5247,7 @@ struct nk_style_window {
float tooltip_border;
float popup_border;
float min_row_height_padding;
NK_BOOL tiled_background;

float rounding;
struct nk_vec2 spacing;
Expand Down Expand Up @@ -10508,6 +10510,19 @@ nk_draw_list_add_image(struct nk_draw_list *list, struct nk_image texture,
nk_vec2(0.0f, 0.0f), nk_vec2(1.0f, 1.0f),color);
}
NK_API void
nk_draw_list_add_image_tiled(struct nk_draw_list *list, struct nk_image texture,
struct nk_rect rect, struct nk_color color)
{
NK_ASSERT(list);
if (!list) return;
/* push new command with given texture */
nk_draw_list_push_image(list, texture.handle);
/* No support for tiled subimages, texture.region is ignored */
nk_draw_list_push_rect_uv(list, nk_vec2(rect.x, rect.y),
nk_vec2(rect.x + rect.w, rect.y + rect.h), nk_vec2(0.0f, 0.0f),
nk_vec2(rect.w/(float)texture.w, rect.h/(float)texture.h), color);
}
NK_API void
nk_draw_list_add_text(struct nk_draw_list *list, const struct nk_user_font *font,
struct nk_rect rect, const char *text, int len, float font_height,
struct nk_color fg)
Expand Down Expand Up @@ -10685,7 +10700,10 @@ nk_convert(struct nk_context *ctx, struct nk_buffer *cmds,
} break;
case NK_COMMAND_IMAGE: {
const struct nk_command_image *i = (const struct nk_command_image*)cmd;
nk_draw_list_add_image(&ctx->draw_list, i->img, nk_rect(i->x, i->y, i->w, i->h), i->col);
if(ctx->style.window.tiled_background)
nk_draw_list_add_image_tiled(&ctx->draw_list, i->img, nk_rect(i->x, i->y, i->w, i->h), i->col);
else
nk_draw_list_add_image(&ctx->draw_list, i->img, nk_rect(i->x, i->y, i->w, i->h), i->col);
} break;
case NK_COMMAND_CUSTOM: {
const struct nk_command_custom *c = (const struct nk_command_custom*)cmd;
Expand Down Expand Up @@ -18654,7 +18672,7 @@ nk_style_from_table(struct nk_context *ctx, const struct nk_color *table)

/* window */
win->background = table[NK_COLOR_WINDOW];
win->fixed_background = nk_style_item_color(table[NK_COLOR_WINDOW]);
win->image = nk_style_item_color(table[NK_COLOR_WINDOW]);
win->border_color = table[NK_COLOR_BORDER];
win->popup_border_color = table[NK_COLOR_BORDER];
win->combo_border_color = table[NK_COLOR_BORDER];
Expand Down Expand Up @@ -19697,15 +19715,15 @@ nk_panel_begin(struct nk_context *ctx, const char *title, enum nk_panel_type pan
body.y = (win->bounds.y + layout->header_height);
body.h = (win->bounds.h - layout->header_height);

switch(style->window.fixed_background.type) {
switch(style->window.image.type) {
case NK_STYLE_ITEM_IMAGE:
nk_draw_image(out, body, &style->window.fixed_background.data.image, nk_white);
nk_draw_image(out, body, &style->window.image.data.image, nk_white);
break;
case NK_STYLE_ITEM_NINE_SLICE:
nk_draw_nine_slice(out, body, &style->window.fixed_background.data.slice, nk_white);
nk_draw_nine_slice(out, body, &style->window.image.data.slice, nk_white);
break;
case NK_STYLE_ITEM_COLOR:
nk_fill_rect(out, body, 0, style->window.fixed_background.data.color);
nk_fill_rect(out, body, 0, style->window.image.data.color);
break;
}
}
Expand Down Expand Up @@ -23542,7 +23560,8 @@ NK_API nk_bool
nk_image_is_subimage(const struct nk_image* img)
{
NK_ASSERT(img);
return !(img->w == 0 && img->h == 0);
return !(img->region[0] == 0 && img->region[1] == 0 &&
img->region[2] == 0 && img->region[3] == 0);
}
NK_API void
nk_image(struct nk_context *ctx, struct nk_image img)
Expand Down Expand Up @@ -29629,6 +29648,8 @@ nk_tooltipfv(struct nk_context *ctx, const char *fmt, va_list args)
/// - [y]: Minor version with non-breaking API and library changes
/// - [z]: Patch version with no direct changes to the API
///
/// - 2022/04/07 (5.0.0) - tiling window backgrounds, nk_style_window.fixed_background
/// renamed to nk_style_window.image
/// - 2022/02/03 (4.9.6) - Allow overriding the NK_INV_SQRT function, similar to NK_SIN and NK_COS
/// - 2021/12/22 (4.9.5) - Revert layout bounds not accounting for padding due to regressions
/// - 2021/12/22 (4.9.4) - Fix checking hovering when window is minimized
Expand Down
2 changes: 2 additions & 0 deletions src/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
/// - [y]: Minor version with non-breaking API and library changes
/// - [z]: Patch version with no direct changes to the API
///
/// - 2022/04/07 (5.0.0) - tiling window backgrounds, nk_style_window.fixed_background
/// renamed to nk_style_window.image
/// - 2022/02/03 (4.9.6) - Allow overriding the NK_INV_SQRT function, similar to NK_SIN and NK_COS
/// - 2021/12/22 (4.9.5) - Revert layout bounds not accounting for padding due to regressions
/// - 2021/12/22 (4.9.4) - Fix checking hovering when window is minimized
Expand Down
4 changes: 3 additions & 1 deletion src/nuklear.h
Original file line number Diff line number Diff line change
Expand Up @@ -4619,6 +4619,7 @@ NK_API void nk_draw_list_fill_poly_convex(struct nk_draw_list*, const struct nk_

/* misc */
NK_API void nk_draw_list_add_image(struct nk_draw_list*, struct nk_image texture, struct nk_rect rect, struct nk_color);
NK_API void nk_draw_list_add_image_tiled(struct nk_draw_list*, struct nk_image texture, struct nk_rect rect, struct nk_color);
NK_API void nk_draw_list_add_text(struct nk_draw_list*, const struct nk_user_font*, struct nk_rect, const char *text, int len, float font_height, struct nk_color);
#ifdef NK_INCLUDE_COMMAND_USERDATA
NK_API void nk_draw_list_push_userdata(struct nk_draw_list*, nk_handle userdata);
Expand Down Expand Up @@ -5005,7 +5006,7 @@ struct nk_style_window_header {

struct nk_style_window {
struct nk_style_window_header header;
struct nk_style_item fixed_background;
struct nk_style_item image;
struct nk_color background;

struct nk_color border_color;
Expand All @@ -5025,6 +5026,7 @@ struct nk_style_window {
float tooltip_border;
float popup_border;
float min_row_height_padding;
NK_BOOL tiled_background;

float rounding;
struct nk_vec2 spacing;
Expand Down
3 changes: 2 additions & 1 deletion src/nuklear_image.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ NK_API nk_bool
nk_image_is_subimage(const struct nk_image* img)
{
NK_ASSERT(img);
return !(img->w == 0 && img->h == 0);
return !(img->region[0] == 0 && img->region[1] == 0 &&
img->region[2] == 0 && img->region[3] == 0);
}
NK_API void
nk_image(struct nk_context *ctx, struct nk_image img)
Expand Down
8 changes: 4 additions & 4 deletions src/nuklear_panel.c
Original file line number Diff line number Diff line change
Expand Up @@ -301,15 +301,15 @@ nk_panel_begin(struct nk_context *ctx, const char *title, enum nk_panel_type pan
body.y = (win->bounds.y + layout->header_height);
body.h = (win->bounds.h - layout->header_height);

switch(style->window.fixed_background.type) {
switch(style->window.image.type) {
case NK_STYLE_ITEM_IMAGE:
nk_draw_image(out, body, &style->window.fixed_background.data.image, nk_white);
nk_draw_image(out, body, &style->window.image.data.image, nk_white);
break;
case NK_STYLE_ITEM_NINE_SLICE:
nk_draw_nine_slice(out, body, &style->window.fixed_background.data.slice, nk_white);
nk_draw_nine_slice(out, body, &style->window.image.data.slice, nk_white);
break;
case NK_STYLE_ITEM_COLOR:
nk_fill_rect(out, body, 0, style->window.fixed_background.data.color);
nk_fill_rect(out, body, 0, style->window.image.data.color);
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/nuklear_style.c
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ nk_style_from_table(struct nk_context *ctx, const struct nk_color *table)

/* window */
win->background = table[NK_COLOR_WINDOW];
win->fixed_background = nk_style_item_color(table[NK_COLOR_WINDOW]);
win->image = nk_style_item_color(table[NK_COLOR_WINDOW]);
win->border_color = table[NK_COLOR_BORDER];
win->popup_border_color = table[NK_COLOR_BORDER];
win->combo_border_color = table[NK_COLOR_BORDER];
Expand Down
18 changes: 17 additions & 1 deletion src/nuklear_vertex.c
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,19 @@ nk_draw_list_add_image(struct nk_draw_list *list, struct nk_image texture,
nk_vec2(0.0f, 0.0f), nk_vec2(1.0f, 1.0f),color);
}
NK_API void
nk_draw_list_add_image_tiled(struct nk_draw_list *list, struct nk_image texture,
struct nk_rect rect, struct nk_color color)
{
NK_ASSERT(list);
if (!list) return;
/* push new command with given texture */
nk_draw_list_push_image(list, texture.handle);
/* No support for tiled subimages, texture.region is ignored */
nk_draw_list_push_rect_uv(list, nk_vec2(rect.x, rect.y),
nk_vec2(rect.x + rect.w, rect.y + rect.h), nk_vec2(0.0f, 0.0f),
nk_vec2(rect.w/(float)texture.w, rect.h/(float)texture.h), color);
}
NK_API void
nk_draw_list_add_text(struct nk_draw_list *list, const struct nk_user_font *font,
struct nk_rect rect, const char *text, int len, float font_height,
struct nk_color fg)
Expand Down Expand Up @@ -1303,7 +1316,10 @@ nk_convert(struct nk_context *ctx, struct nk_buffer *cmds,
} break;
case NK_COMMAND_IMAGE: {
const struct nk_command_image *i = (const struct nk_command_image*)cmd;
nk_draw_list_add_image(&ctx->draw_list, i->img, nk_rect(i->x, i->y, i->w, i->h), i->col);
if(ctx->style.window.tiled_background)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tested in isolation and didn't catch, that this line is garbage.
It forces all images to be treated as tiled and breaks styling.

nk_draw_list_add_image_tiled(&ctx->draw_list, i->img, nk_rect(i->x, i->y, i->w, i->h), i->col);
else
nk_draw_list_add_image(&ctx->draw_list, i->img, nk_rect(i->x, i->y, i->w, i->h), i->col);
} break;
case NK_COMMAND_CUSTOM: {
const struct nk_command_custom *c = (const struct nk_command_custom*)cmd;
Expand Down