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

Add a way to specify a stroke type, Center/Inner/Outter #703

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 deletions src/nuklear.h
Original file line number Diff line number Diff line change
Expand Up @@ -4323,13 +4323,15 @@ 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;
unsigned short line_thickness;
short x, y;
unsigned short w, h;
struct nk_color color;
enum nk_stroke_type stroke_type;
};

struct nk_command_rect_filled {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions src/nuklear_button.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
27 changes: 27 additions & 0 deletions src/nuklear_draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/nuklear_edit.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}}

Expand Down
26 changes: 25 additions & 1 deletion src/nuklear_vertex.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down