Skip to content

Commit 2667d0a

Browse files
authored
button: Add chevron and hamburger symbol (#933)
1 parent 859d17b commit 2667d0a

5 files changed

Lines changed: 170 additions & 23 deletions

File tree

demo/common/overview.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,17 +205,26 @@ overview(struct nk_context *ctx)
205205

206206
nk_layout_row_static(ctx, 25, 25, 8);
207207
nk_button_symbol(ctx, NK_SYMBOL_CIRCLE_SOLID);
208-
nk_button_symbol(ctx, NK_SYMBOL_CIRCLE_OUTLINE);
209208
nk_button_symbol(ctx, NK_SYMBOL_RECT_SOLID);
209+
nk_button_symbol(ctx, NK_SYMBOL_CIRCLE_OUTLINE);
210210
nk_button_symbol(ctx, NK_SYMBOL_RECT_OUTLINE);
211211
nk_button_symbol(ctx, NK_SYMBOL_TRIANGLE_UP);
212-
nk_button_symbol(ctx, NK_SYMBOL_TRIANGLE_UP_OUTLINE);
212+
nk_button_symbol(ctx, NK_SYMBOL_TRIANGLE_RIGHT);
213213
nk_button_symbol(ctx, NK_SYMBOL_TRIANGLE_DOWN);
214-
nk_button_symbol(ctx, NK_SYMBOL_TRIANGLE_DOWN_OUTLINE);
215214
nk_button_symbol(ctx, NK_SYMBOL_TRIANGLE_LEFT);
216-
nk_button_symbol(ctx, NK_SYMBOL_TRIANGLE_LEFT_OUTLINE);
217-
nk_button_symbol(ctx, NK_SYMBOL_TRIANGLE_RIGHT);
215+
nk_button_symbol(ctx, NK_SYMBOL_TRIANGLE_UP_OUTLINE);
218216
nk_button_symbol(ctx, NK_SYMBOL_TRIANGLE_RIGHT_OUTLINE);
217+
nk_button_symbol(ctx, NK_SYMBOL_TRIANGLE_DOWN_OUTLINE);
218+
nk_button_symbol(ctx, NK_SYMBOL_TRIANGLE_LEFT_OUTLINE);
219+
nk_button_symbol(ctx, NK_SYMBOL_CHEVRON_UP);
220+
nk_button_symbol(ctx, NK_SYMBOL_CHEVRON_RIGHT);
221+
nk_button_symbol(ctx, NK_SYMBOL_CHEVRON_DOWN);
222+
nk_button_symbol(ctx, NK_SYMBOL_CHEVRON_LEFT);
223+
nk_button_symbol(ctx, NK_SYMBOL_HAMBURGER);
224+
nk_button_symbol(ctx, NK_SYMBOL_X);
225+
nk_button_symbol(ctx, NK_SYMBOL_UNDERSCORE);
226+
nk_button_symbol(ctx, NK_SYMBOL_PLUS);
227+
nk_button_symbol(ctx, NK_SYMBOL_MINUS);
219228

220229
nk_layout_row_static(ctx, 30, 100, 2);
221230
nk_button_symbol_label(ctx, NK_SYMBOL_TRIANGLE_LEFT, "prev", NK_TEXT_RIGHT);

nuklear.h

Lines changed: 78 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,11 @@ enum nk_symbol_type {
526526
NK_SYMBOL_TRIANGLE_DOWN_OUTLINE,
527527
NK_SYMBOL_TRIANGLE_LEFT_OUTLINE,
528528
NK_SYMBOL_TRIANGLE_RIGHT_OUTLINE,
529+
NK_SYMBOL_CHEVRON_UP,
530+
NK_SYMBOL_CHEVRON_RIGHT,
531+
NK_SYMBOL_CHEVRON_DOWN,
532+
NK_SYMBOL_CHEVRON_LEFT,
533+
NK_SYMBOL_HAMBURGER, /** Three horizontal lines. */
529534
NK_SYMBOL_MAX
530535
};
531536
/* =============================================================================
@@ -24531,20 +24536,32 @@ nk_draw_symbol(struct nk_command_buffer *out, enum nk_symbol_type type,
2453124536
struct nk_rect content, struct nk_color background, struct nk_color foreground,
2453224537
float border_width, const struct nk_user_font *font)
2453324538
{
24539+
/* Use the border_width as the line thickness. */
24540+
if (border_width <= 0.0f) {
24541+
border_width = 1.0f;
24542+
}
2453424543
switch (type) {
24535-
case NK_SYMBOL_X:
24544+
case NK_SYMBOL_X: {
24545+
float pad_x = content.w * 0.2f;
24546+
float pad_y = content.h * 0.2f;
24547+
float x0 = content.x + pad_x;
24548+
float y0 = content.y + pad_y;
24549+
float x1 = content.x + content.w - pad_x;
24550+
float y1 = content.y + content.h - pad_y;
24551+
nk_stroke_line(out, x0, y0, x1, y1, border_width, foreground);
24552+
nk_stroke_line(out, x1, y0, x0, y1, border_width, foreground);
24553+
} break;
2453624554
case NK_SYMBOL_UNDERSCORE:
2453724555
case NK_SYMBOL_PLUS:
2453824556
case NK_SYMBOL_MINUS: {
2453924557
/* single character text symbol */
24540-
const char *X = (type == NK_SYMBOL_X) ? "x":
24541-
(type == NK_SYMBOL_UNDERSCORE) ? "_":
24558+
const char *character = (type == NK_SYMBOL_UNDERSCORE) ? "_":
2454224559
(type == NK_SYMBOL_PLUS) ? "+": "-";
2454324560
struct nk_text text;
2454424561
text.padding = nk_vec2(0,0);
2454524562
text.background = background;
2454624563
text.text = foreground;
24547-
nk_widget_text(out, content, X, 1, &text, NK_TEXT_CENTERED, font);
24564+
nk_widget_text(out, content, character, 1, &text, NK_TEXT_CENTERED, font);
2454824565
} break;
2454924566
case NK_SYMBOL_CIRCLE_SOLID:
2455024567
case NK_SYMBOL_CIRCLE_OUTLINE:
@@ -24558,7 +24575,7 @@ nk_draw_symbol(struct nk_command_buffer *out, enum nk_symbol_type type,
2455824575
} else {
2455924576
nk_fill_circle(out, content, foreground);
2456024577
if (type == NK_SYMBOL_CIRCLE_OUTLINE)
24561-
nk_fill_circle(out, nk_shrink_rect(content, 1), background);
24578+
nk_fill_circle(out, nk_shrink_rect(content, border_width), background);
2456224579
}
2456324580
} break;
2456424581
case NK_SYMBOL_TRIANGLE_UP:
@@ -24587,6 +24604,58 @@ nk_draw_symbol(struct nk_command_buffer *out, enum nk_symbol_type type,
2458724604
nk_stroke_triangle(out, points[0].x, points[0].y, points[1].x, points[1].y,
2458824605
points[2].x, points[2].y, border_width, foreground);
2458924606
} break;
24607+
case NK_SYMBOL_CHEVRON_UP:
24608+
case NK_SYMBOL_CHEVRON_RIGHT:
24609+
case NK_SYMBOL_CHEVRON_DOWN:
24610+
case NK_SYMBOL_CHEVRON_LEFT: {
24611+
struct nk_vec2 points[3];
24612+
switch (type) {
24613+
case NK_SYMBOL_CHEVRON_RIGHT:
24614+
points[0].x = content.x;
24615+
points[0].y = content.y;
24616+
points[1].x = content.x + content.w;
24617+
points[1].y = content.y + content.h * 0.5f;
24618+
points[2].x = content.x;
24619+
points[2].y = content.y + content.h;
24620+
break;
24621+
case NK_SYMBOL_CHEVRON_LEFT:
24622+
points[0].x = content.x + content.w;
24623+
points[0].y = content.y;
24624+
points[1].x = content.x;
24625+
points[1].y = content.y + content.h * 0.5f;
24626+
points[2].x = content.x + content.w;
24627+
points[2].y = content.y + content.h;
24628+
break;
24629+
case NK_SYMBOL_CHEVRON_UP:
24630+
points[0].x = content.x;
24631+
points[0].y = content.y + content.h;
24632+
points[1].x = content.x + content.w * 0.5f;
24633+
points[1].y = content.y;
24634+
points[2].x = content.x + content.w;
24635+
points[2].y = content.y + content.h;
24636+
break;
24637+
case NK_SYMBOL_CHEVRON_DOWN:
24638+
points[0].x = content.x;
24639+
points[0].y = content.y;
24640+
points[1].x = content.x + content.w * 0.5f;
24641+
points[1].y = content.y + content.h;
24642+
points[2].x = content.x + content.w;
24643+
points[2].y = content.y;
24644+
break;
24645+
default:
24646+
break;
24647+
}
24648+
nk_stroke_line(out, points[0].x, points[0].y, points[1].x, points[1].y, border_width, foreground);
24649+
nk_stroke_line(out, points[1].x, points[1].y, points[2].x, points[2].y, border_width, foreground);
24650+
} break;
24651+
case NK_SYMBOL_HAMBURGER: {
24652+
float y2 = content.y + content.h * 0.5f;
24653+
float y3 = content.y + content.h - border_width;
24654+
float x1 = content.x + content.w;
24655+
nk_stroke_line(out, content.x, content.y, x1, content.y, border_width, foreground);
24656+
nk_stroke_line(out, content.x, y2, x1, y2, border_width, foreground);
24657+
nk_stroke_line(out, content.x, y3, x1, y3, border_width, foreground);
24658+
} break;
2459024659
default:
2459124660
case NK_SYMBOL_NONE:
2459224661
case NK_SYMBOL_MAX: break;
@@ -24741,7 +24810,7 @@ nk_draw_button_symbol(struct nk_command_buffer *out,
2474124810
else sym = style->text_normal;
2474224811

2474324812
sym = nk_rgb_factor(sym, style->color_factor_text);
24744-
nk_draw_symbol(out, type, *content, bg, sym, 1, font);
24813+
nk_draw_symbol(out, type, *content, bg, sym, style->border, font);
2474524814
}
2474624815
NK_LIB nk_bool
2474724816
nk_do_button_symbol(nk_flags *state,
@@ -24832,7 +24901,7 @@ nk_draw_button_text_symbol(struct nk_command_buffer *out,
2483224901
sym = nk_rgb_factor(sym, style->color_factor_text);
2483324902
text.text = nk_rgb_factor(text.text, style->color_factor_text);
2483424903
text.padding = nk_vec2(0,0);
24835-
nk_draw_symbol(out, type, *symbol, style->text_background, sym, 0, font);
24904+
nk_draw_symbol(out, type, *symbol, style->text_background, sym, style->border, font);
2483624905
nk_widget_text(out, *label, str, len, &text, NK_TEXT_CENTERED, font);
2483724906
}
2483824907
NK_LIB nk_bool
@@ -30254,7 +30323,7 @@ nk_combo_begin_symbol(struct nk_context *ctx, enum nk_symbol_type symbol, struct
3025430323
bounds.x = header.x + style->combo.content_padding.x;
3025530324
bounds.w = (button.x - style->combo.content_padding.y) - bounds.x;
3025630325
nk_draw_symbol(&win->buffer, symbol, bounds, sym_background, symbol_color,
30257-
1.0f, style->font);
30326+
style->combo.border, style->font);
3025830327

3025930328
/* draw open/close button */
3026030329
nk_draw_button_symbol(&win->buffer, &bounds, &content, ctx->last_widget_state,
@@ -30357,7 +30426,7 @@ nk_combo_begin_symbol_text(struct nk_context *ctx, const char *selected, int len
3035730426
image.h = header.h - 2 * style->combo.content_padding.y;
3035830427
image.w = image.h;
3035930428
nk_draw_symbol(&win->buffer, symbol, image, text.background, symbol_color,
30360-
1.0f, style->font);
30429+
style->combo.border, style->font);
3036130430

3036230431
/* draw label */
3036330432
text.padding = nk_vec2(0,0);

src/nuklear.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,11 @@ enum nk_symbol_type {
336336
NK_SYMBOL_TRIANGLE_DOWN_OUTLINE,
337337
NK_SYMBOL_TRIANGLE_LEFT_OUTLINE,
338338
NK_SYMBOL_TRIANGLE_RIGHT_OUTLINE,
339+
NK_SYMBOL_CHEVRON_UP,
340+
NK_SYMBOL_CHEVRON_RIGHT,
341+
NK_SYMBOL_CHEVRON_DOWN,
342+
NK_SYMBOL_CHEVRON_LEFT,
343+
NK_SYMBOL_HAMBURGER, /** Three horizontal lines. */
339344
NK_SYMBOL_MAX
340345
};
341346
/* =============================================================================

src/nuklear_button.c

Lines changed: 71 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,32 @@ nk_draw_symbol(struct nk_command_buffer *out, enum nk_symbol_type type,
1111
struct nk_rect content, struct nk_color background, struct nk_color foreground,
1212
float border_width, const struct nk_user_font *font)
1313
{
14+
/* Use the border_width as the line thickness. */
15+
if (border_width <= 0.0f) {
16+
border_width = 1.0f;
17+
}
1418
switch (type) {
15-
case NK_SYMBOL_X:
19+
case NK_SYMBOL_X: {
20+
float pad_x = content.w * 0.2f;
21+
float pad_y = content.h * 0.2f;
22+
float x0 = content.x + pad_x;
23+
float y0 = content.y + pad_y;
24+
float x1 = content.x + content.w - pad_x;
25+
float y1 = content.y + content.h - pad_y;
26+
nk_stroke_line(out, x0, y0, x1, y1, border_width, foreground);
27+
nk_stroke_line(out, x1, y0, x0, y1, border_width, foreground);
28+
} break;
1629
case NK_SYMBOL_UNDERSCORE:
1730
case NK_SYMBOL_PLUS:
1831
case NK_SYMBOL_MINUS: {
1932
/* single character text symbol */
20-
const char *X = (type == NK_SYMBOL_X) ? "x":
21-
(type == NK_SYMBOL_UNDERSCORE) ? "_":
33+
const char *character = (type == NK_SYMBOL_UNDERSCORE) ? "_":
2234
(type == NK_SYMBOL_PLUS) ? "+": "-";
2335
struct nk_text text;
2436
text.padding = nk_vec2(0,0);
2537
text.background = background;
2638
text.text = foreground;
27-
nk_widget_text(out, content, X, 1, &text, NK_TEXT_CENTERED, font);
39+
nk_widget_text(out, content, character, 1, &text, NK_TEXT_CENTERED, font);
2840
} break;
2941
case NK_SYMBOL_CIRCLE_SOLID:
3042
case NK_SYMBOL_CIRCLE_OUTLINE:
@@ -38,7 +50,7 @@ nk_draw_symbol(struct nk_command_buffer *out, enum nk_symbol_type type,
3850
} else {
3951
nk_fill_circle(out, content, foreground);
4052
if (type == NK_SYMBOL_CIRCLE_OUTLINE)
41-
nk_fill_circle(out, nk_shrink_rect(content, 1), background);
53+
nk_fill_circle(out, nk_shrink_rect(content, border_width), background);
4254
}
4355
} break;
4456
case NK_SYMBOL_TRIANGLE_UP:
@@ -67,6 +79,58 @@ nk_draw_symbol(struct nk_command_buffer *out, enum nk_symbol_type type,
6779
nk_stroke_triangle(out, points[0].x, points[0].y, points[1].x, points[1].y,
6880
points[2].x, points[2].y, border_width, foreground);
6981
} break;
82+
case NK_SYMBOL_CHEVRON_UP:
83+
case NK_SYMBOL_CHEVRON_RIGHT:
84+
case NK_SYMBOL_CHEVRON_DOWN:
85+
case NK_SYMBOL_CHEVRON_LEFT: {
86+
struct nk_vec2 points[3];
87+
switch (type) {
88+
case NK_SYMBOL_CHEVRON_RIGHT:
89+
points[0].x = content.x;
90+
points[0].y = content.y;
91+
points[1].x = content.x + content.w;
92+
points[1].y = content.y + content.h * 0.5f;
93+
points[2].x = content.x;
94+
points[2].y = content.y + content.h;
95+
break;
96+
case NK_SYMBOL_CHEVRON_LEFT:
97+
points[0].x = content.x + content.w;
98+
points[0].y = content.y;
99+
points[1].x = content.x;
100+
points[1].y = content.y + content.h * 0.5f;
101+
points[2].x = content.x + content.w;
102+
points[2].y = content.y + content.h;
103+
break;
104+
case NK_SYMBOL_CHEVRON_UP:
105+
points[0].x = content.x;
106+
points[0].y = content.y + content.h;
107+
points[1].x = content.x + content.w * 0.5f;
108+
points[1].y = content.y;
109+
points[2].x = content.x + content.w;
110+
points[2].y = content.y + content.h;
111+
break;
112+
case NK_SYMBOL_CHEVRON_DOWN:
113+
points[0].x = content.x;
114+
points[0].y = content.y;
115+
points[1].x = content.x + content.w * 0.5f;
116+
points[1].y = content.y + content.h;
117+
points[2].x = content.x + content.w;
118+
points[2].y = content.y;
119+
break;
120+
default:
121+
break;
122+
}
123+
nk_stroke_line(out, points[0].x, points[0].y, points[1].x, points[1].y, border_width, foreground);
124+
nk_stroke_line(out, points[1].x, points[1].y, points[2].x, points[2].y, border_width, foreground);
125+
} break;
126+
case NK_SYMBOL_HAMBURGER: {
127+
float y2 = content.y + content.h * 0.5f;
128+
float y3 = content.y + content.h - border_width;
129+
float x1 = content.x + content.w;
130+
nk_stroke_line(out, content.x, content.y, x1, content.y, border_width, foreground);
131+
nk_stroke_line(out, content.x, y2, x1, y2, border_width, foreground);
132+
nk_stroke_line(out, content.x, y3, x1, y3, border_width, foreground);
133+
} break;
70134
default:
71135
case NK_SYMBOL_NONE:
72136
case NK_SYMBOL_MAX: break;
@@ -221,7 +285,7 @@ nk_draw_button_symbol(struct nk_command_buffer *out,
221285
else sym = style->text_normal;
222286

223287
sym = nk_rgb_factor(sym, style->color_factor_text);
224-
nk_draw_symbol(out, type, *content, bg, sym, 1, font);
288+
nk_draw_symbol(out, type, *content, bg, sym, style->border, font);
225289
}
226290
NK_LIB nk_bool
227291
nk_do_button_symbol(nk_flags *state,
@@ -312,7 +376,7 @@ nk_draw_button_text_symbol(struct nk_command_buffer *out,
312376
sym = nk_rgb_factor(sym, style->color_factor_text);
313377
text.text = nk_rgb_factor(text.text, style->color_factor_text);
314378
text.padding = nk_vec2(0,0);
315-
nk_draw_symbol(out, type, *symbol, style->text_background, sym, 0, font);
379+
nk_draw_symbol(out, type, *symbol, style->text_background, sym, style->border, font);
316380
nk_widget_text(out, *label, str, len, &text, NK_TEXT_CENTERED, font);
317381
}
318382
NK_LIB nk_bool

src/nuklear_combo.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ nk_combo_begin_symbol(struct nk_context *ctx, enum nk_symbol_type symbol, struct
332332
bounds.x = header.x + style->combo.content_padding.x;
333333
bounds.w = (button.x - style->combo.content_padding.y) - bounds.x;
334334
nk_draw_symbol(&win->buffer, symbol, bounds, sym_background, symbol_color,
335-
1.0f, style->font);
335+
style->combo.border, style->font);
336336

337337
/* draw open/close button */
338338
nk_draw_button_symbol(&win->buffer, &bounds, &content, ctx->last_widget_state,
@@ -435,7 +435,7 @@ nk_combo_begin_symbol_text(struct nk_context *ctx, const char *selected, int len
435435
image.h = header.h - 2 * style->combo.content_padding.y;
436436
image.w = image.h;
437437
nk_draw_symbol(&win->buffer, symbol, image, text.background, symbol_color,
438-
1.0f, style->font);
438+
style->combo.border, style->font);
439439

440440
/* draw label */
441441
text.padding = nk_vec2(0,0);

0 commit comments

Comments
 (0)