Skip to content
This repository has been archived by the owner on Dec 2, 2019. It is now read-only.

Add 9-patch support for skin system #874

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
400 changes: 398 additions & 2 deletions nuklear.h

Large diffs are not rendered by default.

30 changes: 29 additions & 1 deletion src/nuklear.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ 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_nine_patch {nk_handle handle;unsigned short w,h;unsigned short region[4];unsigned short margin[4];};
struct nk_cursor {struct nk_image img; struct nk_vec2 size, offset;};
struct nk_scroll {nk_uint x, y;};

Expand Down Expand Up @@ -3480,6 +3481,19 @@ 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);
/* =============================================================================
*
* NINE PATCH
*
* ============================================================================= */
NK_API struct nk_nine_patch nk_nine_patch_handle(nk_handle, unsigned short l, unsigned short t, unsigned short r, unsigned short b);
NK_API struct nk_nine_patch nk_nine_patch_ptr(void*, unsigned short l, unsigned short t, unsigned short r, unsigned short b);
NK_API struct nk_nine_patch nk_nine_patch_id(int, unsigned short l, unsigned short t, unsigned short r, unsigned short b);
NK_API int nk_nine_patch_is_sub(const struct nk_nine_patch* np);
NK_API struct nk_nine_patch nk_sub_nine_patch_ptr(void*, unsigned short w, unsigned short h, struct nk_rect sub_region, unsigned short l, unsigned short t, unsigned short r, unsigned short b);
NK_API struct nk_nine_patch nk_sub_nine_patch_id(int, unsigned short w, unsigned short h, struct nk_rect sub_region, unsigned short l, unsigned short t, unsigned short r, unsigned short b);
NK_API struct nk_nine_patch nk_sub_nine_patch_handle(nk_handle, unsigned short w, unsigned short h, struct nk_rect sub_region, unsigned short l, unsigned short t, unsigned short r, unsigned short b);

/* =============================================================================
*
* MATH
Expand Down Expand Up @@ -4171,6 +4185,7 @@ enum nk_command_type {
NK_COMMAND_POLYLINE,
NK_COMMAND_TEXT,
NK_COMMAND_IMAGE,
NK_COMMAND_NINE_PATCH,
NK_COMMAND_CUSTOM
};

Expand Down Expand Up @@ -4313,6 +4328,14 @@ struct nk_command_image {
struct nk_color col;
};

struct nk_command_nine_patch {
struct nk_command header;
short x, y;
unsigned short w, h;
struct nk_nine_patch np;
struct nk_color col;
};

typedef void (*nk_command_custom_callback)(void *canvas, short x,short y,
unsigned short w, unsigned short h, nk_handle callback_data);
struct nk_command_custom {
Expand Down Expand Up @@ -4368,6 +4391,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_nine_patch(struct nk_command_buffer*, struct nk_rect, const struct nk_nine_patch*, 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 @@ -4571,6 +4595,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_nine_patch(struct nk_draw_list*, struct nk_nine_patch nine_patch, 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 All @@ -4585,11 +4610,13 @@ 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_NINE_PATCH
};

union nk_style_item_data {
struct nk_image image;
struct nk_nine_patch nine_patch;
struct nk_color color;
};

Expand Down Expand Up @@ -5017,6 +5044,7 @@ struct nk_style {
};

NK_API struct nk_style_item nk_style_item_image(struct nk_image img);
NK_API struct nk_style_item nk_style_item_nine_patch(struct nk_nine_patch np);
NK_API struct nk_style_item nk_style_item_color(struct nk_color);
NK_API struct nk_style_item nk_style_item_hide(void);

Expand Down
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_NINE_PATCH) {
nk_draw_nine_patch(out, *bounds, &background->data.nine_patch, 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
2 changes: 2 additions & 0 deletions src/nuklear_chart.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ nk_chart_begin_colored(struct nk_context *ctx, enum nk_chart_type type,
background = &style->background;
if (background->type == NK_STYLE_ITEM_IMAGE) {
nk_draw_image(&win->buffer, bounds, &background->data.image, nk_white);
} else if (background->type == NK_STYLE_ITEM_NINE_PATCH) {
nk_draw_nine_patch(&win->buffer, bounds, &background->data.nine_patch, 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),
Expand Down
16 changes: 16 additions & 0 deletions src/nuklear_combo.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ nk_combo_begin_text(struct nk_context *ctx, const char *selected, int len,
if (background->type == NK_STYLE_ITEM_IMAGE) {
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_NINE_PATCH) {
text.background = nk_rgba(0,0,0,0);
nk_draw_nine_patch(&win->buffer, header, &background->data.nine_patch, nk_white);
} else {
text.background = background->data.color;
nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
Expand Down Expand Up @@ -171,6 +174,8 @@ nk_combo_begin_color(struct nk_context *ctx, struct nk_color color, struct nk_ve

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_NINE_PATCH) {
nk_draw_nine_patch(&win->buffer, header, &background->data.nine_patch, 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 @@ -256,6 +261,9 @@ 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);
nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
} else if (background->type == NK_STYLE_ITEM_NINE_PATCH) {
sym_background = background->data.color;
nk_draw_nine_patch(&win->buffer, header, &background->data.nine_patch, nk_white);
} else {
sym_background = background->data.color;
nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
Expand Down Expand Up @@ -345,6 +353,9 @@ nk_combo_begin_symbol_text(struct nk_context *ctx, const char *selected, int len
if (background->type == NK_STYLE_ITEM_IMAGE) {
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_NINE_PATCH) {
text.background = nk_rgba(0,0,0,0);
nk_draw_nine_patch(&win->buffer, header, &background->data.nine_patch, nk_white);
} else {
text.background = background->data.color;
nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
Expand Down Expand Up @@ -431,6 +442,8 @@ nk_combo_begin_image(struct nk_context *ctx, struct nk_image img, struct nk_vec2

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_NINE_PATCH) {
nk_draw_nine_patch(&win->buffer, header, &background->data.nine_patch, 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 @@ -514,6 +527,9 @@ nk_combo_begin_image_text(struct nk_context *ctx, const char *selected, int len,
if (background->type == NK_STYLE_ITEM_IMAGE) {
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_NINE_PATCH) {
text.background = nk_rgba(0,0,0,0);
nk_draw_nine_patch(&win->buffer, header, &background->data.nine_patch, nk_white);
} else {
text.background = background->data.color;
nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
Expand Down
24 changes: 24 additions & 0 deletions src/nuklear_draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,30 @@ nk_draw_image(struct nk_command_buffer *b, struct nk_rect r,
cmd->img = *img;
cmd->col = col;
}

NK_API void
nk_draw_nine_patch(struct nk_command_buffer *b, struct nk_rect r,
const struct nk_nine_patch *np, struct nk_color col)
{
struct nk_command_nine_patch *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_nine_patch*)
nk_command_buffer_push(b, NK_COMMAND_NINE_PATCH, 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->np = *np;
cmd->col = 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
10 changes: 9 additions & 1 deletion src/nuklear_edit.c
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,11 @@ nk_do_edit(nk_flags *state, struct nk_command_buffer *out,
if (background->type == NK_STYLE_ITEM_COLOR) {
nk_stroke_rect(out, bounds, style->rounding, style->border, style->border_color);
nk_fill_rect(out, bounds, style->rounding, background->data.color);
} else nk_draw_image(out, bounds, &background->data.image, nk_white);}
}
else if (background->type == NK_STYLE_ITEM_NINE_PATCH) {
nk_draw_nine_patch(out, bounds, &background->data.nine_patch, nk_white);
}
else nk_draw_image(out, bounds, &background->data.image, nk_white);}

area.w = NK_MAX(0, area.w - style->cursor_size);
if (edit->active)
Expand Down Expand Up @@ -532,6 +536,8 @@ nk_do_edit(nk_flags *state, struct nk_command_buffer *out,
}
if (background->type == NK_STYLE_ITEM_IMAGE)
background_color = nk_rgba(0,0,0,0);
else if (background->type == NK_STYLE_ITEM_NINE_PATCH)
background_color = nk_rgba(0,0,0,0);
else background_color = background->data.color;


Expand Down Expand Up @@ -639,6 +645,8 @@ nk_do_edit(nk_flags *state, struct nk_command_buffer *out,
}
if (background->type == NK_STYLE_ITEM_IMAGE)
background_color = nk_rgba(0,0,0,0);
else if (background->type == NK_STYLE_ITEM_NINE_PATCH)
background_color = nk_rgba(0,0,0,0);
else background_color = background->data.color;
nk_edit_draw_text(out, style, area.x - edit->scrollbar.x,
area.y - edit->scrollbar.y, 0, begin, l, row_height, font,
Expand Down
117 changes: 117 additions & 0 deletions src/nuklear_image.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,120 @@ nk_image_color(struct nk_context *ctx, struct nk_image img, struct nk_color col)
nk_draw_image(&win->buffer, bounds, &img, col);
}

NK_API struct nk_nine_patch
nk_nine_patch_handle(nk_handle handle, unsigned short l, unsigned short t,
unsigned short r, unsigned short b)
{
struct nk_nine_patch s;
nk_zero(&s, sizeof(s));
s.handle = handle;
s.w = 0; s.h = 0;
s.region[0] = 0;
s.region[1] = 0;
s.region[2] = 0;
s.region[3] = 0;
s.margin[0] = l;
s.margin[1] = t;
s.margin[2] = r;
s.margin[3] = b;
return s;
}
NK_API struct nk_nine_patch
nk_nine_patch_ptr(void *ptr, unsigned short l, unsigned short t,
unsigned short r, unsigned short b)
{
struct nk_nine_patch s;
nk_zero(&s, sizeof(s));
NK_ASSERT(ptr);
s.handle.ptr = ptr;
s.w = 0; s.h = 0;
s.region[0] = 0;
s.region[1] = 0;
s.region[2] = 0;
s.region[3] = 0;
s.margin[0] = l;
s.margin[1] = t;
s.margin[2] = r;
s.margin[3] = b;
return s;
}
NK_API struct nk_nine_patch
nk_nine_patch_id(int id, unsigned short l, unsigned short t,
unsigned short r, unsigned short b)
{
struct nk_nine_patch s;
nk_zero(&s, sizeof(s));
s.handle.id = id;
s.w = 0; s.h = 0;
s.region[0] = 0;
s.region[1] = 0;
s.region[2] = 0;
s.region[3] = 0;
s.margin[0] = l;
s.margin[1] = t;
s.margin[2] = r;
s.margin[3] = b;
return s;
}

NK_API int
nk_nine_patch_is_sub(const struct nk_nine_patch* np)
{
NK_ASSERT(np);
return !(np->w == 0 && np->h == 0);
}

NK_API struct nk_nine_patch
nk_sub_nine_patch_ptr(void *ptr, unsigned short w, unsigned short h, struct nk_rect r,
unsigned short l, unsigned short t, unsigned short ri, unsigned short b)
{
struct nk_nine_patch s;
nk_zero(&s, sizeof(s));
s.handle.ptr = ptr;
s.w = w; s.h = h;
s.region[0] = (unsigned short)r.x;
s.region[1] = (unsigned short)r.y;
s.region[2] = (unsigned short)r.w;
s.region[3] = (unsigned short)r.h;
s.margin[0] = l;
s.margin[1] = t;
s.margin[2] = ri;
s.margin[3] = b;
return s;
}
NK_API struct nk_nine_patch
nk_sub_nine_patch_id(int id, unsigned short w, unsigned short h, struct nk_rect r,
unsigned short l, unsigned short t, unsigned short ri, unsigned short b)
{
struct nk_nine_patch s;
nk_zero(&s, sizeof(s));
s.handle.id = id;
s.w = w; s.h = h;
s.region[0] = (unsigned short)r.x;
s.region[1] = (unsigned short)r.y;
s.region[2] = (unsigned short)r.w;
s.region[3] = (unsigned short)r.h;
s.margin[0] = l;
s.margin[1] = t;
s.margin[2] = ri;
s.margin[3] = b;
return s;
}
NK_API struct nk_nine_patch
nk_sub_nine_patch_handle(nk_handle handle, unsigned short w, unsigned short h, struct nk_rect r,
unsigned short l, unsigned short t, unsigned short ri, unsigned short b)
{
struct nk_nine_patch s;
nk_zero(&s, sizeof(s));
s.handle = handle;
s.w = w; s.h = h;
s.region[0] = (unsigned short)r.x;
s.region[1] = (unsigned short)r.y;
s.region[2] = (unsigned short)r.w;
s.region[3] = (unsigned short)r.h;
s.margin[0] = l;
s.margin[1] = t;
s.margin[2] = ri;
s.margin[3] = b;
return s;
}
7 changes: 7 additions & 0 deletions src/nuklear_panel.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ nk_panel_begin(struct nk_context *ctx, const char *title, enum nk_panel_type pan
if (background->type == NK_STYLE_ITEM_IMAGE) {
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_NINE_PATCH) {
text.background = nk_rgba(0,0,0,0);
nk_draw_nine_patch(&win->buffer, header, &background->data.nine_patch, nk_white);
} else {
text.background = background->data.color;
nk_fill_rect(out, header, 0, background->data.color);
Expand Down Expand Up @@ -294,6 +297,8 @@ nk_panel_begin(struct nk_context *ctx, const char *title, enum nk_panel_type pan
body.h = (win->bounds.h - layout->header_height);
if (style->window.fixed_background.type == NK_STYLE_ITEM_IMAGE)
nk_draw_image(out, body, &style->window.fixed_background.data.image, nk_white);
else if (style->window.fixed_background.type == NK_STYLE_ITEM_NINE_PATCH)
nk_draw_nine_patch(out, body, &style->window.fixed_background.data.nine_patch, nk_white);
else nk_fill_rect(out, body, 0, style->window.fixed_background.data.color);
}

Expand Down Expand Up @@ -509,6 +514,8 @@ nk_panel_end(struct nk_context *ctx)
{const struct nk_style_item *item = &style->window.scaler;
if (item->type == NK_STYLE_ITEM_IMAGE)
nk_draw_image(out, scaler, &item->data.image, nk_white);
else if (item->type == NK_STYLE_ITEM_NINE_PATCH)
nk_draw_nine_patch(out, scaler, &item->data.nine_patch, nk_white);
else {
if (layout->flags & NK_WINDOW_SCALE_LEFT) {
nk_fill_triangle(out, scaler.x, scaler.y, scaler.x,
Expand Down
4 changes: 4 additions & 0 deletions src/nuklear_progress.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,16 @@ nk_draw_progress(struct nk_command_buffer *out, nk_flags state,
if (background->type == NK_STYLE_ITEM_COLOR) {
nk_fill_rect(out, *bounds, style->rounding, background->data.color);
nk_stroke_rect(out, *bounds, style->rounding, style->border, style->border_color);
} else if (background->type == NK_STYLE_ITEM_NINE_PATCH) {
nk_draw_nine_patch(out, *bounds, &background->data.nine_patch, nk_white);
} else nk_draw_image(out, *bounds, &background->data.image, nk_white);

/* draw cursor */
if (cursor->type == NK_STYLE_ITEM_COLOR) {
nk_fill_rect(out, *scursor, style->rounding, cursor->data.color);
nk_stroke_rect(out, *scursor, style->rounding, style->border, style->border_color);
} else if (background->type == NK_STYLE_ITEM_NINE_PATCH) {
nk_draw_nine_patch(out, *scursor, &cursor->data.nine_patch, nk_white);
} else nk_draw_image(out, *scursor, &cursor->data.image, nk_white);
}
NK_LIB nk_size
Expand Down
Loading