Skip to content

Commit 6322f53

Browse files
authored
Add support for 9-slice/9-patch (#304)
1 parent f42712c commit 6322f53

22 files changed

+983
-285
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ docs/src
88
*.tmp
99
*.swo
1010
*.swp
11+
/private/

nuklear.h

Lines changed: 490 additions & 141 deletions
Large diffs are not rendered by default.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nuklear",
3-
"version": "4.07.5",
3+
"version": "4.08.0",
44
"repo": "Immediate-Mode-UI/Nuklear",
55
"description": "A small ANSI C gui toolkit",
66
"keywords": ["gl", "ui", "toolkit"],

src/CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
/// - [yy]: Minor version with non-breaking API and library changes
99
/// - [zz]: Bug fix version with no direct changes to API
1010
///
11+
/// - 2021/08/17 (4.08.0) - Implemented 9-slice scaling support for widget styles
1112
/// - 2021/08/16 (4.07.5) - Replace usage of memset in nk_font_atlas_bake with NK_MEMSET
1213
/// - 2021/08/15 (4.07.4) - Fix conversion and sign conversion warnings
1314
/// - 2021/08/08 (4.07.3) - Fix crash when baking merged fonts

src/nuklear.h

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,8 @@ struct nk_rect {float x,y,w,h;};
259259
struct nk_recti {short x,y,w,h;};
260260
typedef char nk_glyph[NK_UTF_SIZE];
261261
typedef union {void *ptr; int id;} nk_handle;
262-
struct nk_image {nk_handle handle;unsigned short w,h;unsigned short region[4];};
262+
struct nk_image {nk_handle handle; nk_ushort w, h; nk_ushort region[4];};
263+
struct nk_nine_slice {struct nk_image img; nk_ushort l, t, r, b;};
263264
struct nk_cursor {struct nk_image img; struct nk_vec2 size, offset;};
264265
struct nk_scroll {nk_uint x, y;};
265266

@@ -3493,9 +3494,21 @@ NK_API struct nk_image nk_image_handle(nk_handle);
34933494
NK_API struct nk_image nk_image_ptr(void*);
34943495
NK_API struct nk_image nk_image_id(int);
34953496
NK_API nk_bool nk_image_is_subimage(const struct nk_image* img);
3496-
NK_API struct nk_image nk_subimage_ptr(void*, unsigned short w, unsigned short h, struct nk_rect sub_region);
3497-
NK_API struct nk_image nk_subimage_id(int, unsigned short w, unsigned short h, struct nk_rect sub_region);
3498-
NK_API struct nk_image nk_subimage_handle(nk_handle, unsigned short w, unsigned short h, struct nk_rect sub_region);
3497+
NK_API struct nk_image nk_subimage_ptr(void*, nk_ushort w, nk_ushort h, struct nk_rect sub_region);
3498+
NK_API struct nk_image nk_subimage_id(int, nk_ushort w, nk_ushort h, struct nk_rect sub_region);
3499+
NK_API struct nk_image nk_subimage_handle(nk_handle, nk_ushort w, nk_ushort h, struct nk_rect sub_region);
3500+
/* =============================================================================
3501+
*
3502+
* 9-SLICE
3503+
*
3504+
* ============================================================================= */
3505+
NK_API struct nk_nine_slice nk_nine_slice_handle(nk_handle, nk_ushort l, nk_ushort t, nk_ushort r, nk_ushort b);
3506+
NK_API struct nk_nine_slice nk_nine_slice_ptr(void*, nk_ushort l, nk_ushort t, nk_ushort r, nk_ushort b);
3507+
NK_API struct nk_nine_slice nk_nine_slice_id(int, nk_ushort l, nk_ushort t, nk_ushort r, nk_ushort b);
3508+
NK_API int nk_nine_slice_is_sub9slice(const struct nk_nine_slice* img);
3509+
NK_API struct nk_nine_slice 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);
3510+
NK_API struct nk_nine_slice 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);
3511+
NK_API struct nk_nine_slice 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);
34993512
/* =============================================================================
35003513
*
35013514
* MATH
@@ -4387,6 +4400,7 @@ NK_API void nk_fill_polygon(struct nk_command_buffer*, float*, int point_count,
43874400

43884401
/* misc */
43894402
NK_API void nk_draw_image(struct nk_command_buffer*, struct nk_rect, const struct nk_image*, struct nk_color);
4403+
NK_API void nk_draw_nine_slice(struct nk_command_buffer*, struct nk_rect, const struct nk_nine_slice*, struct nk_color);
43904404
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);
43914405
NK_API void nk_push_scissor(struct nk_command_buffer*, struct nk_rect);
43924406
NK_API void nk_push_custom(struct nk_command_buffer*, struct nk_rect, nk_command_custom_callback, nk_handle usr);
@@ -4604,12 +4618,14 @@ NK_API void nk_draw_list_push_userdata(struct nk_draw_list*, nk_handle userdata)
46044618
* ===============================================================*/
46054619
enum nk_style_item_type {
46064620
NK_STYLE_ITEM_COLOR,
4607-
NK_STYLE_ITEM_IMAGE
4621+
NK_STYLE_ITEM_IMAGE,
4622+
NK_STYLE_ITEM_NINE_SLICE
46084623
};
46094624

46104625
union nk_style_item_data {
4611-
struct nk_image image;
46124626
struct nk_color color;
4627+
struct nk_image image;
4628+
struct nk_nine_slice slice;
46134629
};
46144630

46154631
struct nk_style_item {
@@ -5035,8 +5051,9 @@ struct nk_style {
50355051
struct nk_style_window window;
50365052
};
50375053

5038-
NK_API struct nk_style_item nk_style_item_image(struct nk_image img);
50395054
NK_API struct nk_style_item nk_style_item_color(struct nk_color);
5055+
NK_API struct nk_style_item nk_style_item_image(struct nk_image img);
5056+
NK_API struct nk_style_item nk_style_item_nine_slice(struct nk_nine_slice slice);
50405057
NK_API struct nk_style_item nk_style_item_hide(void);
50415058

50425059
/*==============================================================

src/nuklear_9slice.c

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#include "nuklear.h"
2+
#include "nuklear_internal.h"
3+
4+
/* ===============================================================
5+
*
6+
* 9-SLICE
7+
*
8+
* ===============================================================*/
9+
NK_API struct nk_nine_slice
10+
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)
11+
{
12+
struct nk_nine_slice s;
13+
nk_zero(&s, sizeof(s));
14+
struct nk_image *i = &s.img;
15+
i->handle.ptr = ptr;
16+
i->w = w; i->h = h;
17+
i->region[0] = (nk_ushort)rgn.x;
18+
i->region[1] = (nk_ushort)rgn.y;
19+
i->region[2] = (nk_ushort)rgn.w;
20+
i->region[3] = (nk_ushort)rgn.h;
21+
s.l = l; s.t = t; s.r = r; s.b = b;
22+
return s;
23+
}
24+
NK_API struct nk_nine_slice
25+
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)
26+
{
27+
struct nk_nine_slice s;
28+
nk_zero(&s, sizeof(s));
29+
struct nk_image *i = &s.img;
30+
i->handle.id = id;
31+
i->w = w; i->h = h;
32+
i->region[0] = (nk_ushort)rgn.x;
33+
i->region[1] = (nk_ushort)rgn.y;
34+
i->region[2] = (nk_ushort)rgn.w;
35+
i->region[3] = (nk_ushort)rgn.h;
36+
s.l = l; s.t = t; s.r = r; s.b = b;
37+
return s;
38+
}
39+
NK_API struct nk_nine_slice
40+
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)
41+
{
42+
struct nk_nine_slice s;
43+
nk_zero(&s, sizeof(s));
44+
struct nk_image *i = &s.img;
45+
i->handle = handle;
46+
i->w = w; i->h = h;
47+
i->region[0] = (nk_ushort)rgn.x;
48+
i->region[1] = (nk_ushort)rgn.y;
49+
i->region[2] = (nk_ushort)rgn.w;
50+
i->region[3] = (nk_ushort)rgn.h;
51+
s.l = l; s.t = t; s.r = r; s.b = b;
52+
return s;
53+
}
54+
NK_API struct nk_nine_slice
55+
nk_nine_slice_handle(nk_handle handle, nk_ushort l, nk_ushort t, nk_ushort r, nk_ushort b)
56+
{
57+
struct nk_nine_slice s;
58+
nk_zero(&s, sizeof(s));
59+
struct nk_image *i = &s.img;
60+
i->handle = handle;
61+
i->w = 0; i->h = 0;
62+
i->region[0] = 0;
63+
i->region[1] = 0;
64+
i->region[2] = 0;
65+
i->region[3] = 0;
66+
s.l = l; s.t = t; s.r = r; s.b = b;
67+
return s;
68+
}
69+
NK_API struct nk_nine_slice
70+
nk_nine_slice_ptr(void *ptr, nk_ushort l, nk_ushort t, nk_ushort r, nk_ushort b)
71+
{
72+
struct nk_nine_slice s;
73+
nk_zero(&s, sizeof(s));
74+
struct nk_image *i = &s.img;
75+
NK_ASSERT(ptr);
76+
i->handle.ptr = ptr;
77+
i->w = 0; i->h = 0;
78+
i->region[0] = 0;
79+
i->region[1] = 0;
80+
i->region[2] = 0;
81+
i->region[3] = 0;
82+
s.l = l; s.t = t; s.r = r; s.b = b;
83+
return s;
84+
}
85+
NK_API struct nk_nine_slice
86+
nk_nine_slice_id(int id, nk_ushort l, nk_ushort t, nk_ushort r, nk_ushort b)
87+
{
88+
struct nk_nine_slice s;
89+
nk_zero(&s, sizeof(s));
90+
struct nk_image *i = &s.img;
91+
i->handle.id = id;
92+
i->w = 0; i->h = 0;
93+
i->region[0] = 0;
94+
i->region[1] = 0;
95+
i->region[2] = 0;
96+
i->region[3] = 0;
97+
s.l = l; s.t = t; s.r = r; s.b = b;
98+
return s;
99+
}
100+
NK_API int
101+
nk_nine_slice_is_sub9slice(const struct nk_nine_slice* slice)
102+
{
103+
NK_ASSERT(slice);
104+
return !(slice->img.w == 0 && slice->img.h == 0);
105+
}
106+

src/nuklear_button.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,17 @@ nk_draw_button(struct nk_command_buffer *out,
9898
background = &style->active;
9999
else background = &style->normal;
100100

101-
if (background->type == NK_STYLE_ITEM_IMAGE) {
102-
nk_draw_image(out, *bounds, &background->data.image, nk_white);
103-
} else {
104-
nk_fill_rect(out, *bounds, style->rounding, background->data.color);
105-
nk_stroke_rect(out, *bounds, style->rounding, style->border, style->border_color);
101+
switch(background->type) {
102+
case NK_STYLE_ITEM_IMAGE:
103+
nk_draw_image(out, *bounds, &background->data.image, nk_white);
104+
break;
105+
case NK_STYLE_ITEM_NINE_SLICE:
106+
nk_draw_nine_slice(out, *bounds, &background->data.slice, nk_white);
107+
break;
108+
case NK_STYLE_ITEM_COLOR:
109+
nk_fill_rect(out, *bounds, style->rounding, background->data.color);
110+
nk_stroke_rect(out, *bounds, style->rounding, style->border, style->border_color);
111+
break;
106112
}
107113
return background;
108114
}

src/nuklear_chart.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,19 @@ nk_chart_begin_colored(struct nk_context *ctx, enum nk_chart_type type,
5656

5757
/* draw chart background */
5858
background = &style->background;
59-
if (background->type == NK_STYLE_ITEM_IMAGE) {
60-
nk_draw_image(&win->buffer, bounds, &background->data.image, nk_white);
61-
} else {
62-
nk_fill_rect(&win->buffer, bounds, style->rounding, style->border_color);
63-
nk_fill_rect(&win->buffer, nk_shrink_rect(bounds, style->border),
64-
style->rounding, style->background.data.color);
59+
60+
switch(background->type) {
61+
case NK_STYLE_ITEM_IMAGE:
62+
nk_draw_image(&win->buffer, bounds, &background->data.image, nk_white);
63+
break;
64+
case NK_STYLE_ITEM_NINE_SLICE:
65+
nk_draw_nine_slice(&win->buffer, bounds, &background->data.slice, nk_white);
66+
break;
67+
case NK_STYLE_ITEM_COLOR:
68+
nk_fill_rect(&win->buffer, bounds, style->rounding, style->border_color);
69+
nk_fill_rect(&win->buffer, nk_shrink_rect(bounds, style->border),
70+
style->rounding, style->background.data.color);
71+
break;
6572
}
6673
return 1;
6774
}

0 commit comments

Comments
 (0)