diff --git a/src/nuklear.h b/src/nuklear.h index 6a30b3947..1c6ec39c9 100644 --- a/src/nuklear.h +++ b/src/nuklear.h @@ -4323,6 +4323,7 @@ struct nk_command_curve { struct nk_color color; }; +enum nk_stroke_type { NK_STROKE_CENTER, NK_STROKE_INNER, NK_STROKE_OUTER }; struct nk_command_rect { struct nk_command header; unsigned short rounding; @@ -4330,6 +4331,7 @@ struct nk_command_rect { short x, y; unsigned short w, h; struct nk_color color; + enum nk_stroke_type stroke_type; }; struct nk_command_rect_filled { @@ -4469,6 +4471,7 @@ struct nk_command_buffer { NK_API void nk_stroke_line(struct nk_command_buffer *b, float x0, float y0, float x1, float y1, float line_thickness, struct nk_color); NK_API void nk_stroke_curve(struct nk_command_buffer*, float, float, float, float, float, float, float, float, float line_thickness, struct nk_color); NK_API void nk_stroke_rect(struct nk_command_buffer*, struct nk_rect, float rounding, float line_thickness, struct nk_color); +NK_API void nk_stroke_rect_ex(struct nk_command_buffer*, struct nk_rect, float rounding, float line_thickness, struct nk_color, enum nk_stroke_type); NK_API void nk_stroke_circle(struct nk_command_buffer*, struct nk_rect, float line_thickness, struct nk_color); NK_API void nk_stroke_arc(struct nk_command_buffer*, float cx, float cy, float radius, float a_min, float a_max, float line_thickness, struct nk_color); NK_API void nk_stroke_triangle(struct nk_command_buffer*, float, float, float, float, float, float, float line_thichness, struct nk_color); @@ -5606,6 +5609,7 @@ struct nk_context { #define nk_ptr_add(t, p, i) ((t*)((void*)((nk_byte*)(p) + (i)))) #define nk_ptr_add_const(t, p, i) ((const t*)((const void*)((const nk_byte*)(p) + (i)))) #define nk_zero_struct(s) nk_zero(&s, sizeof(s)) +#define nk_div_round_closest(n, d) ((((n) < 0) == ((d) < 0)) ? (((n) + (d)/2)/(d)) : (((n) - (d)/2)/(d))) /* ============================================================== * ALIGNMENT diff --git a/src/nuklear_button.c b/src/nuklear_button.c index 6ef845997..b1360f2ee 100644 --- a/src/nuklear_button.c +++ b/src/nuklear_button.c @@ -111,16 +111,17 @@ nk_draw_button(struct nk_command_buffer *out, background = &style->active; else background = &style->normal; + struct nk_rect b = *bounds; switch (background->type) { case NK_STYLE_ITEM_IMAGE: - nk_draw_image(out, *bounds, &background->data.image, nk_rgb_factor(nk_white, style->color_factor_background)); + nk_draw_image(out, b, &background->data.image, nk_rgb_factor(nk_white, style->color_factor_background)); break; case NK_STYLE_ITEM_NINE_SLICE: - nk_draw_nine_slice(out, *bounds, &background->data.slice, nk_rgb_factor(nk_white, style->color_factor_background)); + nk_draw_nine_slice(out, b, &background->data.slice, nk_rgb_factor(nk_white, style->color_factor_background)); break; case NK_STYLE_ITEM_COLOR: - nk_fill_rect(out, *bounds, style->rounding, nk_rgb_factor(background->data.color, style->color_factor_background)); - nk_stroke_rect(out, *bounds, style->rounding, style->border, nk_rgb_factor(style->border_color, style->color_factor_background)); + nk_fill_rect(out, style->border != 0 ? nk_shrink_rect(b, style->border * 0.5) : b, style->rounding, nk_rgb_factor(background->data.color, style->color_factor_background)); + nk_stroke_rect_ex(out, b, style->rounding, style->border, nk_rgb_factor(style->border_color, style->color_factor_background), NK_STROKE_INNER); break; } return background; diff --git a/src/nuklear_draw.c b/src/nuklear_draw.c index efdf9c59b..c617ca38c 100644 --- a/src/nuklear_draw.c +++ b/src/nuklear_draw.c @@ -132,6 +132,32 @@ nk_stroke_rect(struct nk_command_buffer *b, struct nk_rect rect, struct nk_command_rect *cmd; NK_ASSERT(b); if (!b || c.a == 0 || rect.w == 0 || rect.h == 0 || line_thickness <= 0) return; + + if (b->use_clipping) { + const struct nk_rect *clip = &b->clip; + if (!NK_INTERSECT(rect.x, rect.y, rect.w, rect.h, + clip->x, clip->y, clip->w, clip->h)) return; + } + cmd = (struct nk_command_rect*) + nk_command_buffer_push(b, NK_COMMAND_RECT, sizeof(*cmd)); + if (!cmd) return; + cmd->rounding = (unsigned short)rounding; + cmd->line_thickness = (unsigned short)line_thickness; + cmd->x = (short)rect.x; + cmd->y = (short)rect.y; + cmd->w = (unsigned short)NK_MAX(0, rect.w); + cmd->h = (unsigned short)NK_MAX(0, rect.h); + cmd->color = c; + cmd->stroke_type = NK_STROKE_CENTER; +} +NK_API void +nk_stroke_rect_ex(struct nk_command_buffer *b, struct nk_rect rect, + float rounding, float line_thickness, struct nk_color c, enum nk_stroke_type stroke_type) +{ + struct nk_command_rect *cmd; + NK_ASSERT(b); + if (!b || c.a == 0 || rect.w == 0 || rect.h == 0 || line_thickness <= 0) return; + if (b->use_clipping) { const struct nk_rect *clip = &b->clip; if (!NK_INTERSECT(rect.x, rect.y, rect.w, rect.h, @@ -147,6 +173,7 @@ nk_stroke_rect(struct nk_command_buffer *b, struct nk_rect rect, cmd->w = (unsigned short)NK_MAX(0, rect.w); cmd->h = (unsigned short)NK_MAX(0, rect.h); cmd->color = c; + cmd->stroke_type = stroke_type; } NK_API void nk_fill_rect(struct nk_command_buffer *b, struct nk_rect rect, diff --git a/src/nuklear_edit.c b/src/nuklear_edit.c index a7b2bfcf1..9e8a80d55 100644 --- a/src/nuklear_edit.c +++ b/src/nuklear_edit.c @@ -340,8 +340,8 @@ nk_do_edit(nk_flags *state, struct nk_command_buffer *out, nk_draw_nine_slice(out, bounds, &background->data.slice, nk_rgb_factor(nk_white, style->color_factor)); break; case NK_STYLE_ITEM_COLOR: - nk_fill_rect(out, bounds, style->rounding, nk_rgb_factor(background->data.color, style->color_factor)); - nk_stroke_rect(out, bounds, style->rounding, style->border, nk_rgb_factor(style->border_color, style->color_factor)); + nk_fill_rect(out, style->border != 0 ? nk_shrink_rect(bounds, 0.5) : bounds, style->rounding, nk_rgb_factor(background->data.color, style->color_factor)); + nk_stroke_rect_ex(out, bounds, style->rounding, style->border, nk_rgb_factor(style->border_color, style->color_factor), NK_STROKE_INNER); break; }} diff --git a/src/nuklear_vertex.c b/src/nuklear_vertex.c index 10cff0456..d49a9fc85 100644 --- a/src/nuklear_vertex.c +++ b/src/nuklear_vertex.c @@ -1220,7 +1220,31 @@ nk_convert(struct nk_context *ctx, struct nk_buffer *cmds, config->curve_segment_count, q->line_thickness); } break; case NK_COMMAND_RECT: { - const struct nk_command_rect *r = (const struct nk_command_rect*)cmd; + struct nk_command_rect *r = (struct nk_command_rect*)cmd; + + if (r->stroke_type == NK_STROKE_INNER) + { + float hl = nk_div_round_closest(r->line_thickness, 2.0); + if (hl > 0) + { + if (config->line_AA == NK_ANTI_ALIASING_OFF) + { + r->x += hl; + r->w -= r->line_thickness; + r->y += hl; + r->h -= r->line_thickness; + } + else + { + /* TODO: i don't know yet */ + } + } + } + else + { + /* TODO: implement the rest */ + } + nk_draw_list_stroke_rect(&ctx->draw_list, nk_rect(r->x, r->y, r->w, r->h), r->color, (float)r->rounding, r->line_thickness); } break;