Skip to content

Commit

Permalink
Added support for 9-slice/9-patch textures for various widgets and in…
Browse files Browse the repository at this point in the history
…cremented version to 4.01.6.
  • Loading branch information
Michael-Kelley committed Mar 2, 2020
1 parent 7fe20af commit 505c0f9
Show file tree
Hide file tree
Showing 20 changed files with 26,376 additions and 25,845 deletions.
51,832 changes: 26,049 additions & 25,783 deletions nuklear.h

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nuklear",
"version": "4.01.5",
"version": "4.01.6",
"repo": "Immediate-Mode-UI/Nuklear",
"description": "A small ANSI C gui toolkit",
"keywords": ["gl", "ui", "toolkit"],
Expand Down
31 changes: 24 additions & 7 deletions src/nuklear.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,8 @@ struct nk_rect {float x,y,w,h;};
struct nk_recti {short x,y,w,h;};
typedef char nk_glyph[NK_UTF_SIZE];
typedef union {void *ptr; int id;} nk_handle;
struct nk_image {nk_handle handle;unsigned short w,h;unsigned short region[4];};
struct nk_image {nk_handle handle; nk_ushort w, h; nk_ushort region[4];};
struct nk_9slice {struct nk_image img; nk_ushort l, t, r, b;};
struct nk_cursor {struct nk_image img; struct nk_vec2 size, offset;};
struct nk_scroll {nk_uint x, y;};

Expand Down Expand Up @@ -3477,9 +3478,21 @@ NK_API struct nk_image nk_image_handle(nk_handle);
NK_API struct nk_image nk_image_ptr(void*);
NK_API struct nk_image nk_image_id(int);
NK_API int nk_image_is_subimage(const struct nk_image* img);
NK_API struct nk_image nk_subimage_ptr(void*, unsigned short w, unsigned short h, struct nk_rect sub_region);
NK_API struct nk_image nk_subimage_id(int, unsigned short w, unsigned short h, struct nk_rect sub_region);
NK_API struct nk_image nk_subimage_handle(nk_handle, unsigned short w, unsigned short h, struct nk_rect sub_region);
NK_API struct nk_image nk_subimage_ptr(void*, nk_ushort w, nk_ushort h, struct nk_rect sub_region);
NK_API struct nk_image nk_subimage_id(int, nk_ushort w, nk_ushort h, struct nk_rect sub_region);
NK_API struct nk_image nk_subimage_handle(nk_handle, nk_ushort w, nk_ushort h, struct nk_rect sub_region);
/* =============================================================================
*
* 9-SLICE
*
* ============================================================================= */
NK_API struct nk_9slice nk_9slice_handle(nk_handle, nk_ushort l, nk_ushort t, nk_ushort r, nk_ushort b);
NK_API struct nk_9slice nk_9slice_ptr(void*, nk_ushort l, nk_ushort t, nk_ushort r, nk_ushort b);
NK_API struct nk_9slice nk_9slice_id(int, nk_ushort l, nk_ushort t, nk_ushort r, nk_ushort b);
NK_API int nk_9slice_is_sub9slice(const struct nk_9slice* img);
NK_API struct nk_9slice nk_sub9slice_ptr(void*, nk_ushort w, nk_ushort h, struct nk_rect sub_region, nk_ushort l, nk_ushort t, nk_ushort r, nk_ushort b);
NK_API struct nk_9slice nk_sub9slice_id(int, nk_ushort w, nk_ushort h, struct nk_rect sub_region, nk_ushort l, nk_ushort t, nk_ushort r, nk_ushort b);
NK_API struct nk_9slice nk_sub9slice_handle(nk_handle, nk_ushort w, nk_ushort h, struct nk_rect sub_region, nk_ushort l, nk_ushort t, nk_ushort r, nk_ushort b);
/* =============================================================================
*
* MATH
Expand Down Expand Up @@ -4368,6 +4381,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_9slice(struct nk_command_buffer*, struct nk_rect, const struct nk_9slice*, 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);
NK_API void nk_push_custom(struct nk_command_buffer*, struct nk_rect, nk_command_custom_callback, nk_handle usr);
Expand Down Expand Up @@ -4585,12 +4599,14 @@ NK_API void nk_draw_list_push_userdata(struct nk_draw_list*, nk_handle userdata)
* ===============================================================*/
enum nk_style_item_type {
NK_STYLE_ITEM_COLOR,
NK_STYLE_ITEM_IMAGE
NK_STYLE_ITEM_IMAGE,
NK_STYLE_ITEM_9SLICE
};

union nk_style_item_data {
struct nk_image image;
struct nk_color color;
struct nk_image image;
struct nk_9slice slice;
};

struct nk_style_item {
Expand Down Expand Up @@ -5016,8 +5032,9 @@ struct nk_style {
struct nk_style_window window;
};

NK_API struct nk_style_item nk_style_item_image(struct nk_image img);
NK_API struct nk_style_item nk_style_item_color(struct nk_color);
NK_API struct nk_style_item nk_style_item_image(struct nk_image img);
NK_API struct nk_style_item nk_style_item_9slice(struct nk_9slice slice);
NK_API struct nk_style_item nk_style_item_hide(void);

/*==============================================================
Expand Down
106 changes: 106 additions & 0 deletions src/nuklear_9slice.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#include "nuklear.h"
#include "nuklear_internal.h"

/* ===============================================================
*
* 9-SLICE
*
* ===============================================================*/
NK_API struct nk_9slice
nk_sub9slice_ptr(void *ptr, nk_ushort w, nk_ushort h, struct nk_rect rgn, nk_ushort l, nk_ushort t, nk_ushort r, nk_ushort b)
{
struct nk_9slice s;
nk_zero(&s, sizeof(s));
struct nk_image *i = (struct nk_image*)&s;
i->handle.ptr = ptr;
i->w = w; i->h = h;
i->region[0] = (nk_ushort)rgn.x;
i->region[1] = (nk_ushort)rgn.y;
i->region[2] = (nk_ushort)rgn.w;
i->region[3] = (nk_ushort)rgn.h;
s.l = l; s.t = t; s.r = r; s.b = b;
return s;
}
NK_API struct nk_9slice
nk_sub9slice_id(int id, nk_ushort w, nk_ushort h, struct nk_rect rgn, nk_ushort l, nk_ushort t, nk_ushort r, nk_ushort b)
{
struct nk_9slice s;
nk_zero(&s, sizeof(s));
struct nk_image *i = (struct nk_image*)&s;
i->handle.id = id;
i->w = w; i->h = h;
i->region[0] = (nk_ushort)rgn.x;
i->region[1] = (nk_ushort)rgn.y;
i->region[2] = (nk_ushort)rgn.w;
i->region[3] = (nk_ushort)rgn.h;
s.l = l; s.t = t; s.r = r; s.b = b;
return s;
}
NK_API struct nk_9slice
nk_sub9slice_handle(nk_handle handle, nk_ushort w, nk_ushort h, struct nk_rect rgn, nk_ushort l, nk_ushort t, nk_ushort r, nk_ushort b)
{
struct nk_9slice s;
nk_zero(&s, sizeof(s));
struct nk_image *i = (struct nk_image*)&s;
i->handle = handle;
i->w = w; i->h = h;
i->region[0] = (nk_ushort)rgn.x;
i->region[1] = (nk_ushort)rgn.y;
i->region[2] = (nk_ushort)rgn.w;
i->region[3] = (nk_ushort)rgn.h;
s.l = l; s.t = t; s.r = r; s.b = b;
return s;
}
NK_API struct nk_9slice
nk_9slice_handle(nk_handle handle, nk_ushort l, nk_ushort t, nk_ushort r, nk_ushort b)
{
struct nk_9slice s;
nk_zero(&s, sizeof(s));
struct nk_image *i = (struct nk_image*)&s;
i->handle = handle;
i->w = 0; i->h = 0;
i->region[0] = 0;
i->region[1] = 0;
i->region[2] = 0;
i->region[3] = 0;
s.l = l; s.t = t; s.r = r; s.b = b;
return s;
}
NK_API struct nk_9slice
nk_9slice_ptr(void *ptr, nk_ushort l, nk_ushort t, nk_ushort r, nk_ushort b)
{
struct nk_9slice s;
nk_zero(&s, sizeof(s));
struct nk_image *i = (struct nk_image*)&s;
NK_ASSERT(ptr);
i->handle.ptr = ptr;
i->w = 0; i->h = 0;
i->region[0] = 0;
i->region[1] = 0;
i->region[2] = 0;
i->region[3] = 0;
s.l = l; s.t = t; s.r = r; s.b = b;
return s;
}
NK_API struct nk_9slice
nk_9slice_id(int id, nk_ushort l, nk_ushort t, nk_ushort r, nk_ushort b)
{
struct nk_9slice s;
nk_zero(&s, sizeof(s));
struct nk_image *i = (struct nk_image*)&s;
i->handle.id = id;
i->w = 0; i->h = 0;
i->region[0] = 0;
i->region[1] = 0;
i->region[2] = 0;
i->region[3] = 0;
s.l = l; s.t = t; s.r = r; s.b = b;
return s;
}
NK_API int
nk_9slice_is_sub9slice(const struct nk_9slice* slice)
{
NK_ASSERT(slice);
return !(slice->img.w == 0 && slice->img.h == 0);
}

2 changes: 2 additions & 0 deletions src/nuklear_button.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ nk_draw_button(struct nk_command_buffer *out,

if (background->type == NK_STYLE_ITEM_IMAGE) {
nk_draw_image(out, *bounds, &background->data.image, nk_white);
} else if (background->type == NK_STYLE_ITEM_9SLICE) {
nk_draw_9slice(out, *bounds, &background->data.slice, nk_white);
} else {
nk_fill_rect(out, *bounds, style->rounding, background->data.color);
nk_stroke_rect(out, *bounds, style->rounding, style->border, style->border_color);
Expand Down
6 changes: 4 additions & 2 deletions src/nuklear_chart.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ nk_chart_begin_colored(struct nk_context *ctx, enum nk_chart_type type,

/* draw chart background */
background = &style->background;
if (background->type == NK_STYLE_ITEM_IMAGE) {
if (background->type == NK_STYLE_ITEM_IMAGE)
nk_draw_image(&win->buffer, bounds, &background->data.image, nk_white);
} else {
else if (background->type == NK_STYLE_ITEM_9SLICE)
nk_draw_9slice(&win->buffer, bounds, &background->data.slice, nk_white);
else {
nk_fill_rect(&win->buffer, bounds, style->rounding, style->border_color);
nk_fill_rect(&win->buffer, nk_shrink_rect(bounds, style->border),
style->rounding, style->background.data.color);
Expand Down
34 changes: 25 additions & 9 deletions src/nuklear_combo.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,11 @@ nk_combo_begin_text(struct nk_context *ctx, const char *selected, int len,
text.text = style->combo.label_normal;
}
if (background->type == NK_STYLE_ITEM_IMAGE) {
text.background = nk_rgba(0,0,0,0);
text.background = nk_rgba(0, 0, 0, 0);
nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
} else if (background->type == NK_STYLE_ITEM_9SLICE) {
text.background = nk_rgba(0, 0, 0, 0);
nk_draw_9slice(&win->buffer, header, &background->data.slice, nk_white);
} else {
text.background = background->data.color;
nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
Expand Down Expand Up @@ -169,9 +172,11 @@ nk_combo_begin_color(struct nk_context *ctx, struct nk_color color, struct nk_ve
background = &style->combo.hover;
else background = &style->combo.normal;

if (background->type == NK_STYLE_ITEM_IMAGE) {
nk_draw_image(&win->buffer, header, &background->data.image,nk_white);
} else {
if (background->type == NK_STYLE_ITEM_IMAGE)
nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
else if (background->type == NK_STYLE_ITEM_9SLICE)
nk_draw_9slice(&win->buffer, header, &background->data.slice, nk_white);
else {
nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color);
}
Expand Down Expand Up @@ -254,8 +259,11 @@ nk_combo_begin_symbol(struct nk_context *ctx, enum nk_symbol_type symbol, struct
}

if (background->type == NK_STYLE_ITEM_IMAGE) {
sym_background = nk_rgba(0,0,0,0);
sym_background = nk_rgba(0, 0, 0, 0);
nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
} else if (background->type == NK_STYLE_ITEM_9SLICE) {
sym_background = nk_rgba(0, 0, 0, 0);
nk_draw_9slice(&win->buffer, header, &background->data.slice, nk_white);
} else {
sym_background = background->data.color;
nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
Expand Down Expand Up @@ -343,8 +351,11 @@ nk_combo_begin_symbol_text(struct nk_context *ctx, const char *selected, int len
text.text = style->combo.label_normal;
}
if (background->type == NK_STYLE_ITEM_IMAGE) {
text.background = nk_rgba(0,0,0,0);
text.background = nk_rgba(0, 0, 0, 0);
nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
} else if (background->type == NK_STYLE_ITEM_9SLICE) {
text.background = nk_rgba(0, 0, 0, 0);
nk_draw_9slice(&win->buffer, header, &background->data.slice, nk_white);
} else {
text.background = background->data.color;
nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
Expand Down Expand Up @@ -429,9 +440,11 @@ nk_combo_begin_image(struct nk_context *ctx, struct nk_image img, struct nk_vec2
background = &style->combo.hover;
else background = &style->combo.normal;

if (background->type == NK_STYLE_ITEM_IMAGE) {
if (background->type == NK_STYLE_ITEM_IMAGE)
nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
} else {
else if (background->type == NK_STYLE_ITEM_9SLICE)
nk_draw_9slice(&win->buffer, header, &background->data.slice, nk_white);
else {
nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color);
}
Expand Down Expand Up @@ -512,8 +525,11 @@ nk_combo_begin_image_text(struct nk_context *ctx, const char *selected, int len,
text.text = style->combo.label_normal;
}
if (background->type == NK_STYLE_ITEM_IMAGE) {
text.background = nk_rgba(0,0,0,0);
text.background = nk_rgba(0, 0, 0, 0);
nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
} else if (background->type == NK_STYLE_ITEM_9SLICE) {
text.background = nk_rgba(0, 0, 0, 0);
nk_draw_9slice(&win->buffer, header, &background->data.slice, nk_white);
} else {
text.background = background->data.color;
nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
Expand Down
74 changes: 74 additions & 0 deletions src/nuklear_draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,80 @@ nk_draw_image(struct nk_command_buffer *b, struct nk_rect r,
cmd->col = col;
}
NK_API void
nk_draw_9slice(struct nk_command_buffer *b, struct nk_rect r,
const struct nk_9slice *slc, struct nk_color col)
{
const struct nk_image *slcimg = (const struct nk_image*)slc;
nk_ushort rgnX, rgnY, rgnW, rgnH;
rgnX = slcimg->region[0];
rgnY = slcimg->region[1];
rgnW = slcimg->region[2];
rgnH = slcimg->region[3];

/* top-left */
struct nk_image img = {slcimg->handle, slcimg->w, slcimg->h,
{rgnX, rgnY, slc->l, slc->t}};
nk_draw_image(b,
nk_rect(r.x, r.y, (float)slc->l, (float)slc->t),
&img, col);

/* top-center */
img = (struct nk_image){slcimg->handle, slcimg->w, slcimg->h,
{(nk_ushort)(rgnX + slc->l), rgnY, (nk_ushort)(rgnW - slc->l - slc->r), slc->t}};
nk_draw_image(b,
nk_rect(r.x + (float)slc->l, r.y, (float)(r.w - slc->l - slc->r), (float)slc->t),
&img, col);

/* top-right */
img = (struct nk_image){slcimg->handle, slcimg->w, slcimg->h,
{(nk_ushort)(rgnX + rgnW - slc->r), rgnY, slc->r, slc->t}};
nk_draw_image(b,
nk_rect(r.x + r.w - (float)slc->r, r.y, (float)slc->r, (float)slc->t),
&img, col);

/* center-left */
img = (struct nk_image){slcimg->handle, slcimg->w, slcimg->h,
{rgnX, (nk_ushort)(rgnY + slc->t), slc->l, (nk_ushort)(rgnH - slc->t - slc->b)}};
nk_draw_image(b,
nk_rect(r.x, r.y + (float)slc->t, (float)slc->l, (float)(r.h - slc->t - slc->b)),
&img, col);

/* center */
img = (struct nk_image){slcimg->handle, slcimg->w, slcimg->h,
{(nk_ushort)(rgnX + slc->l), (nk_ushort)(rgnY + slc->t), (nk_ushort)(rgnW - slc->l - slc->r), (nk_ushort)(rgnH - slc->t - slc->b)}};
nk_draw_image(b,
nk_rect(r.x + (float)slc->l, r.y + (float)slc->t, (float)(r.w - slc->l - slc->r), (float)(r.h - slc->t - slc->b)),
&img, col);

/* center-right */
img = (struct nk_image){slcimg->handle, slcimg->w, slcimg->h,
{(nk_ushort)(rgnX + rgnW - slc->r), (nk_ushort)(rgnY + slc->t), slc->r, (nk_ushort)(rgnH - slc->t - slc->b)}};
nk_draw_image(b,
nk_rect(r.x + r.w - (float)slc->r, r.y + (float)slc->t, (float)slc->r, (float)(r.h - slc->t - slc->b)),
&img, col);

/* bottom-left */
img = (struct nk_image){slcimg->handle, slcimg->w, slcimg->h,
{rgnX, (nk_ushort)(rgnY + rgnH - slc->b), slc->l, slc->b}};
nk_draw_image(b,
nk_rect(r.x, r.y + r.h - (float)slc->b, (float)slc->l, (float)slc->b},
&img, col);

/* bottom-center */
img = (struct nk_image){slcimg->handle, slcimg->w, slcimg->h,
{(nk_ushort)(rgnX + slc->l), (nk_ushort)(rgnY + rgnH - slc->b), (nk_ushort)(rgnW - slc->l - slc->r), slc->b}};
nk_draw_image(b,
nk_rect(r.x + (float)slc->l, r.y + r.h - (float)slc->b, (float)(r.w - slc->l - slc->r), (float)slc->b),
&img, col);

/* bottom-right */
img = (struct nk_image){slcimg->handle, slcimg->w, slcimg->h,
{(nk_ushort)(rgnX + rgnW - slc->r), (nk_ushort)(rgnY + rgnH - slc->b), slc->r, slc->b}};
nk_draw_image(b,
nk_rect(r.x + r.w - (float)slc->r, r.y + r.h - (float)slc->b, (float)slc->r, (float)slc->b),
&img, col);
}
NK_API void
nk_push_custom(struct nk_command_buffer *b, struct nk_rect r,
nk_command_custom_callback cb, nk_handle usr)
{
Expand Down
Loading

0 comments on commit 505c0f9

Please sign in to comment.