textbuttons and text in buttons

master
Alessandro Mauri 2 years ago
parent 27e5ead579
commit 0948708050
  1. 4
      def_style.h
  2. 11
      test/main.c
  3. 59
      ugui.c
  4. 1
      ugui.h

@ -28,11 +28,11 @@ static const ug_style_t default_style = {
.act = RGB_FORMAT(0x440044),
.bg = RGB_FORMAT(0x006600),
.fg = RGB_FORMAT(0xffff00),
.sel = RGB_FORMAT(0xff0000),
.sel = RGB_FORMAT(0x0a0aff),
.br = RGB_FORMAT(0xff00ff),
},
.font_size = SIZE_PX(10),
.border = SIZE_PX(11),
.border = SIZE_PX(1),
},
};

@ -209,13 +209,14 @@ int main(void)
ug_layout_row(ctx);
ug_layout_row(ctx);
ug_element_button(ctx, "button 1", "", (ug_div_t){SQUARE(SIZE_MM(10))});
ug_element_button(ctx, "button 2", "", (ug_div_t){SQUARE(SIZE_MM(10))});
ug_element_button(ctx, "button 1", "hey", (ug_div_t){SQUARE(SIZE_MM(10))});
ug_element_button(ctx, "button 2", "lol", (ug_div_t){SQUARE(SIZE_MM(10))});
ug_layout_next_row(ctx);
ug_element_button(ctx, "button 3", "", (ug_div_t){SQUARE(SIZE_MM(10))});
ug_element_button(ctx, "button 4", "", (ug_div_t){SQUARE(SIZE_MM(10))});
ug_element_button(ctx, "button 3", "L", (ug_div_t){SQUARE(SIZE_MM(10))});
ug_element_button(ctx, "button 4", "69", (ug_div_t){SQUARE(SIZE_MM(10))});
ug_layout_next_row(ctx);
ug_element_button(ctx, "button 5", "", (ug_div_t){SQUARE(SIZE_MM(10))});
ug_element_button(ctx, "button 5", "lmao", (ug_div_t){SQUARE(SIZE_MM(10))});
ug_element_textbtn(ctx, "text button 1", "foo", (ug_div_t){SQUARE(SIZE_MM(10))});
//ug_element_button(ctx, "button 6", "", (ug_div_t){SQUARE(SIZE_MM(10)),.x=SIZE_PX(-10)});
ug_frame_end(ctx);

@ -1283,24 +1283,47 @@ void draw_elements(ug_ctx_t *ctx, ug_container_t *cnt)
for (int i = 0; i < cnt->elem_stack.idx; i++) {
ug_element_t *e = &(cnt->elem_stack.items[i]);
ug_color_t col = RGB_FORMAT(0), bcol = RGB_FORMAT(0);
ug_color_t txtcol = RGB_FORMAT(0);
ug_rect_t r = e->rca;
int eb;
int eb = 0, ts = 0;
unsigned char draw_bg = 0, draw_fg = 0, draw_txt = 0;
switch (e->type) {
case UG_ELEM_BUTTON:
// TODO: draw borders, different color for selected element
col = cnt->hover_elem == e->id ? s->btn.color.act : s->btn.color.bg;
bcol = cnt->selected_elem == e->id ? s->btn.color.sel : s->btn.color.br;
txtcol = s->btn.color.fg;
ts = SZ_INT(s->btn.font_size);
eb = SZ_INT(ctx->style_px->btn.border);
draw_bg = draw_fg = draw_txt = 1;
break;
case UG_ELEM_TXTBTN:
ts = SZ_INT(s->btn.font_size);
txtcol = s->color.fg;
draw_txt = 1;
default: break;
}
if (draw_bg)
push_rect_command(ctx, &r, bcol);
if (draw_fg) {
r = e->rca;
r.x += eb;
r.y += eb;
r.w -= 2*eb;
r.h -= 2*eb;
push_rect_command(ctx, &r, col);
break;
default: break;
}
if (draw_txt) {
push_text_command(ctx,
(ug_vec2_t){
.x = e->rca.x,
.y = e->rca.y+e->rca.h/2
},
ts,
txtcol,
e->btn.txt);
}
}
}
@ -1332,3 +1355,33 @@ int ug_element_button(ug_ctx_t *ctx, const char *name, const char *txt, ug_div_t
return handle_element(ctx, cp, elem);
}
// text-only button
int ug_element_textbtn(ug_ctx_t *ctx, const char *name, const char *txt, ug_div_t dim)
{
TEST_CTX(ctx);
TEST_STR(name);
TEST_STR(txt);
ug_container_t *cp;
GET_SELECTED_CONTAINER(ctx, cp);
ug_id_t id = hash(name, strlen(name));
ug_element_t *elem = get_element(cp, id);
// FIXME: we don't always need to do everything
elem->id = id;
elem->type = UG_ELEM_TXTBTN;
elem->rect = div_to_rect(ctx, &dim);
elem->name = name;
elem->btn.txt = txt;
// FIXME: what about error codes?
if (position_element(ctx, cp, elem)) {
DELETE_FROM_STACK(cp->elem_stack, elem);
return -1;
}
return handle_element(ctx, cp, elem);
}

@ -260,6 +260,7 @@ int ug_layout_next_column(ug_ctx_t *ctx);
// elements
int ug_element_button(ug_ctx_t *ctx, const char *name, const char *txt, ug_div_t dim);
int ug_element_textbtn(ug_ctx_t *ctx, const char *name, const char *txt, ug_div_t dim);
// Input functions
int ug_input_mousemove(ug_ctx_t *ctx, int x, int y);

Loading…
Cancel
Save