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 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
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
68 changes: 59 additions & 9 deletions nuklear.h
Original file line number Diff line number Diff line change
Expand Up @@ -4439,6 +4439,7 @@ enum nk_command_type {
NK_COMMAND_POLYLINE,
NK_COMMAND_TEXT,
NK_COMMAND_IMAGE,
NK_COMMAND_IMAGE_TILED,
NK_COMMAND_CUSTOM
};

Expand Down Expand Up @@ -4636,6 +4637,7 @@ NK_API void nk_fill_polygon(struct nk_command_buffer*, float*, int point_count,

/* misc */
NK_API void nk_draw_image(struct nk_command_buffer*, struct nk_rect, const struct nk_image*, struct nk_color);
NK_API void nk_draw_image_tiled(struct nk_command_buffer*, struct nk_rect, const struct nk_image*, struct nk_color);
NK_API void nk_draw_nine_slice(struct nk_command_buffer*, struct nk_rect, const struct nk_nine_slice*, struct nk_color);
NK_API void nk_draw_text(struct nk_command_buffer*, struct nk_rect, const char *text, int len, const struct nk_user_font*, struct nk_color, struct nk_color);
NK_API void nk_push_scissor(struct nk_command_buffer*, struct nk_rect);
Expand Down Expand Up @@ -4840,6 +4842,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 +5229,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 +5249,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 @@ -9238,6 +9242,29 @@ nk_draw_image(struct nk_command_buffer *b, struct nk_rect r,
cmd->col = col;
}
NK_API void
nk_draw_image_tiled(struct nk_command_buffer *b, struct nk_rect r,
const struct nk_image *img, struct nk_color col)
{
struct nk_command_image *cmd;
NK_ASSERT(b);
if (!b) return;
if (b->use_clipping) {
const struct nk_rect *c = &b->clip;
if (c->w == 0 || c->h == 0 || !NK_INTERSECT(r.x, r.y, r.w, r.h, c->x, c->y, c->w, c->h))
return;
}

cmd = (struct nk_command_image*)
nk_command_buffer_push(b, NK_COMMAND_IMAGE_TILED, sizeof(*cmd));
if (!cmd) return;
cmd->x = (short)r.x;
cmd->y = (short)r.y;
cmd->w = (unsigned short)NK_MAX(0, r.w);
cmd->h = (unsigned short)NK_MAX(0, r.h);
cmd->img = *img;
cmd->col = col;
}
NK_API void
nk_draw_nine_slice(struct nk_command_buffer *b, struct nk_rect r,
const struct nk_nine_slice *slc, struct nk_color col)
{
Expand Down Expand Up @@ -10508,6 +10535,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,8 +10725,12 @@ 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);
} break;
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_IMAGE_TILED: {
const struct nk_command_image *i = (const struct nk_command_image*)cmd;
nk_draw_list_add_image_tiled(&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;
c->callback(&ctx->draw_list, c->x, c->y, c->w, c->h, c->callback_data);
Expand Down Expand Up @@ -18654,7 +18698,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 +19741,18 @@ 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);
if(ctx->style.window.tiled_background)
nk_draw_image_tiled(out, body, &style->window.image.data.image, nk_white);
else
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 +23589,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 +29677,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
6 changes: 5 additions & 1 deletion src/nuklear.h
Original file line number Diff line number Diff line change
Expand Up @@ -4218,6 +4218,7 @@ enum nk_command_type {
NK_COMMAND_POLYLINE,
NK_COMMAND_TEXT,
NK_COMMAND_IMAGE,
NK_COMMAND_IMAGE_TILED,
NK_COMMAND_CUSTOM
};

Expand Down Expand Up @@ -4415,6 +4416,7 @@ NK_API void nk_fill_polygon(struct nk_command_buffer*, float*, int point_count,

/* misc */
NK_API void nk_draw_image(struct nk_command_buffer*, struct nk_rect, const struct nk_image*, struct nk_color);
NK_API void nk_draw_image_tiled(struct nk_command_buffer*, struct nk_rect, const struct nk_image*, struct nk_color);
NK_API void nk_draw_nine_slice(struct nk_command_buffer*, struct nk_rect, const struct nk_nine_slice*, struct nk_color);
NK_API void nk_draw_text(struct nk_command_buffer*, struct nk_rect, const char *text, int len, const struct nk_user_font*, struct nk_color, struct nk_color);
NK_API void nk_push_scissor(struct nk_command_buffer*, struct nk_rect);
Expand Down Expand Up @@ -4619,6 +4621,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 +5008,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 +5028,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
23 changes: 23 additions & 0 deletions src/nuklear_draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,29 @@ nk_draw_image(struct nk_command_buffer *b, struct nk_rect r,
cmd->col = col;
}
NK_API void
nk_draw_image_tiled(struct nk_command_buffer *b, struct nk_rect r,
const struct nk_image *img, struct nk_color col)
{
struct nk_command_image *cmd;
NK_ASSERT(b);
if (!b) return;
if (b->use_clipping) {
const struct nk_rect *c = &b->clip;
if (c->w == 0 || c->h == 0 || !NK_INTERSECT(r.x, r.y, r.w, r.h, c->x, c->y, c->w, c->h))
return;
}

cmd = (struct nk_command_image*)
nk_command_buffer_push(b, NK_COMMAND_IMAGE_TILED, sizeof(*cmd));
if (!cmd) return;
cmd->x = (short)r.x;
cmd->y = (short)r.y;
cmd->w = (unsigned short)NK_MAX(0, r.w);
cmd->h = (unsigned short)NK_MAX(0, r.h);
cmd->img = *img;
cmd->col = col;
}
NK_API void
nk_draw_nine_slice(struct nk_command_buffer *b, struct nk_rect r,
const struct nk_nine_slice *slc, struct nk_color col)
{
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
11 changes: 7 additions & 4 deletions src/nuklear_panel.c
Original file line number Diff line number Diff line change
Expand Up @@ -301,15 +301,18 @@ 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);
if(ctx->style.window.tiled_background)
nk_draw_image_tiled(out, body, &style->window.image.data.image, nk_white);
else
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
21 changes: 19 additions & 2 deletions 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,8 +1316,12 @@ 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);
} break;
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_IMAGE_TILED: {
const struct nk_command_image *i = (const struct nk_command_image*)cmd;
nk_draw_list_add_image_tiled(&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;
c->callback(&ctx->draw_list, c->x, c->y, c->w, c->h, c->callback_data);
Expand Down