Skip to content

Commit

Permalink
Added all alignments to toggleables
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Ahnstedt committed Nov 26, 2023
1 parent 8abee24 commit 657e525
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 47 deletions.
10 changes: 5 additions & 5 deletions demo/common/overview.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ overview(struct nk_context *ctx)
{
enum options {A,B,C};
static int checkbox_left_text_left;
static int checkbox_left_text_right;
static int checkbox_centered_text_right;
static int checkbox_right_text_right;
static int checkbox_right_text_left;
static int option_left;
Expand Down Expand Up @@ -213,10 +213,10 @@ overview(struct nk_context *ctx)
static const float ratio[] = {120, 150};

nk_layout_row_dynamic(ctx, 0, 1);
nk_checkbox_label(ctx, "Checkbox Left Text Left", &checkbox_left_text_left, NK_WIDGET_LEFT, NK_TEXT_LEFT);
nk_checkbox_label(ctx, "Checkbox Left Text Right", &checkbox_left_text_right, NK_WIDGET_LEFT, NK_TEXT_RIGHT);
nk_checkbox_label(ctx, "Checkbox Right Text Right", &checkbox_right_text_right, NK_WIDGET_RIGHT, NK_TEXT_RIGHT);
nk_checkbox_label(ctx, "Checkbox Right Text Left", &checkbox_right_text_left, NK_WIDGET_RIGHT, NK_TEXT_LEFT);
nk_checkbox_label(ctx, "CheckLeft TextLeft", &checkbox_left_text_left, NK_WIDGET_LEFT, NK_TEXT_LEFT);
nk_checkbox_label(ctx, "CheckCenter TextRight", &checkbox_centered_text_right, NK_WIDGET_ALIGN_CENTERED | NK_WIDGET_ALIGN_MIDDLE, NK_TEXT_RIGHT);
nk_checkbox_label(ctx, "CheckRight TextRight", &checkbox_right_text_right, NK_WIDGET_LEFT, NK_TEXT_RIGHT);
nk_checkbox_label(ctx, "CheckRight TextLeft", &checkbox_right_text_left, NK_WIDGET_RIGHT, NK_TEXT_LEFT);

nk_layout_row_static(ctx, 30, 80, 3);
option_left = nk_option_label(ctx, "optionA", option_left == A, NK_WIDGET_LEFT, NK_TEXT_LEFT) ? A : option_left;
Expand Down
53 changes: 32 additions & 21 deletions nuklear.h
Original file line number Diff line number Diff line change
Expand Up @@ -24811,7 +24811,6 @@ nk_do_toggle(nk_flags *state,
const struct nk_user_font *font, nk_flags widget_alignment, nk_flags text_alignment)
{
int was_active;
struct nk_rect allocated_space;
struct nk_rect bounds;
struct nk_rect select;
struct nk_rect cursor;
Expand All @@ -24826,13 +24825,6 @@ nk_do_toggle(nk_flags *state,
r.w = NK_MAX(r.w, font->height + 2 * style->padding.x);
r.h = NK_MAX(r.h, font->height + 2 * style->padding.y);

allocated_space = r;

if (widget_alignment & NK_WIDGET_ALIGN_RIGHT) {
r.x = r.x + r.w - font->height - style->padding.x;
r.w = font->height;
}

/* add additional touch padding for touch screen devices */
bounds.x = r.x - style->touch_padding.x;
bounds.y = r.y - style->touch_padding.y;
Expand All @@ -24842,27 +24834,44 @@ nk_do_toggle(nk_flags *state,
/* calculate the selector space */
select.w = font->height;
select.h = select.w;
select.y = r.y + r.h/2.0f - select.h/2.0f;
select.x = r.x;

/* calculate the bounds of the cursor inside the selector */
cursor.x = select.x + style->padding.x + style->border;
cursor.y = select.y + style->padding.y + style->border;
cursor.w = select.w - (2 * style->padding.x + 2 * style->border);
cursor.h = select.h - (2 * style->padding.y + 2 * style->border);

label.y = select.y;
label.h = select.w;
if (widget_alignment & NK_WIDGET_ALIGN_RIGHT) {
select.x = r.x + r.w - font->height;

/* label in front of the selector */
label.x = allocated_space.x;
label.w = allocated_space.w - select.w - style->spacing * 2;
} else {
label.x = r.x;
label.w = r.w - select.w - style->spacing * 2;
} else if (widget_alignment & NK_WIDGET_ALIGN_CENTERED) {
select.x = (r.x + r.w) / 2;

/* label in front of selector */
label.x = r.x;
label.w = r.w / 2 - select.w - style->spacing * 2;
} else { /* Default: NK_WIDGET_ALIGN_LEFT */
select.x = r.x;

/* label behind the selector */
label.x = select.x + select.w + style->spacing;
label.w = NK_MAX(r.x + r.w, label.x) - label.x;
}

if (widget_alignment & NK_WIDGET_ALIGN_TOP) {
select.y = r.y;
} else if (widget_alignment & NK_WIDGET_ALIGN_BOTTOM) {
select.y = r.y + r.h - select.h - 2 * style->padding.y;
} else { /* Default: NK_WIDGET_ALIGN_MIDDLE */
select.y = r.y + r.h/2.0f - select.h/2.0f;
}

label.y = select.y;
label.h = select.w;

/* calculate the bounds of the cursor inside the selector */
cursor.x = select.x + style->padding.x + style->border;
cursor.y = select.y + style->padding.y + style->border;
cursor.w = select.w - (2 * style->padding.x + 2 * style->border);
cursor.h = select.h - (2 * style->padding.y + 2 * style->border);

/* update selector */
was_active = *active;
*active = nk_toggle_behavior(in, bounds, state, *active);
Expand Down Expand Up @@ -30013,6 +30022,8 @@ nk_tooltipfv(struct nk_context *ctx, const char *fmt, va_list args)
/// - [y]: Minor version with non-breaking API and library changes
/// - [z]: Patch version with no direct changes to the API
///
/// - 2023/11/26 (5.0.0) - BREAKING CHANGE: Added alignment to check boxes and radio buttons. They
/// all now require a widget and text alignment parameter.
/// - 2023/10/11 (4.11.0) - Added nk_widget_disable_begin() and nk_widget_disable_end()
/// - 2022/12/23 (4.10.6) - Fix incorrect glyph index in nk_font_bake()
/// - 2022/12/17 (4.10.5) - Fix nk_font_bake_pack() using TTC font offset incorrectly
Expand Down
51 changes: 30 additions & 21 deletions src/nuklear_toggle.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ nk_do_toggle(nk_flags *state,
const struct nk_user_font *font, nk_flags widget_alignment, nk_flags text_alignment)
{
int was_active;
struct nk_rect allocated_space;
struct nk_rect bounds;
struct nk_rect select;
struct nk_rect cursor;
Expand All @@ -130,13 +129,6 @@ nk_do_toggle(nk_flags *state,
r.w = NK_MAX(r.w, font->height + 2 * style->padding.x);
r.h = NK_MAX(r.h, font->height + 2 * style->padding.y);

allocated_space = r;

if (widget_alignment & NK_WIDGET_ALIGN_RIGHT) {
r.x = r.x + r.w - font->height - style->padding.x;
r.w = font->height;
}

/* add additional touch padding for touch screen devices */
bounds.x = r.x - style->touch_padding.x;
bounds.y = r.y - style->touch_padding.y;
Expand All @@ -146,27 +138,44 @@ nk_do_toggle(nk_flags *state,
/* calculate the selector space */
select.w = font->height;
select.h = select.w;
select.y = r.y + r.h/2.0f - select.h/2.0f;
select.x = r.x;

/* calculate the bounds of the cursor inside the selector */
cursor.x = select.x + style->padding.x + style->border;
cursor.y = select.y + style->padding.y + style->border;
cursor.w = select.w - (2 * style->padding.x + 2 * style->border);
cursor.h = select.h - (2 * style->padding.y + 2 * style->border);

label.y = select.y;
label.h = select.w;
if (widget_alignment & NK_WIDGET_ALIGN_RIGHT) {
select.x = r.x + r.w - font->height;

/* label in front of the selector */
label.x = allocated_space.x;
label.w = allocated_space.w - select.w - style->spacing * 2;
} else {
label.x = r.x;
label.w = r.w - select.w - style->spacing * 2;
} else if (widget_alignment & NK_WIDGET_ALIGN_CENTERED) {
select.x = (r.x + r.w) / 2;

/* label in front of selector */
label.x = r.x;
label.w = r.w / 2 - select.w - style->spacing * 2;
} else { /* Default: NK_WIDGET_ALIGN_LEFT */
select.x = r.x;

/* label behind the selector */
label.x = select.x + select.w + style->spacing;
label.w = NK_MAX(r.x + r.w, label.x) - label.x;
}

if (widget_alignment & NK_WIDGET_ALIGN_TOP) {
select.y = r.y;
} else if (widget_alignment & NK_WIDGET_ALIGN_BOTTOM) {
select.y = r.y + r.h - select.h - 2 * style->padding.y;
} else { /* Default: NK_WIDGET_ALIGN_MIDDLE */
select.y = r.y + r.h/2.0f - select.h/2.0f;
}

label.y = select.y;
label.h = select.w;

/* calculate the bounds of the cursor inside the selector */
cursor.x = select.x + style->padding.x + style->border;
cursor.y = select.y + style->padding.y + style->border;
cursor.w = select.w - (2 * style->padding.x + 2 * style->border);
cursor.h = select.h - (2 * style->padding.y + 2 * style->border);

/* update selector */
was_active = *active;
*active = nk_toggle_behavior(in, bounds, state, *active);
Expand Down

0 comments on commit 657e525

Please sign in to comment.