added align for labels & fixed selecting other panels if one panel is minimized

This commit is contained in:
vurtun 2015-04-08 11:54:33 +02:00
parent 4b749995d8
commit 3ec9a0de9f
4 changed files with 41 additions and 53 deletions

View File

@ -162,16 +162,17 @@ delfont(struct gui_font *font)
free(font);
}
static void
static gui_int
message_panel(struct gui_context *ctx, struct gui_panel *panel)
{
gui_int ret = -1;
gui_begin_panel(ctx, panel, "Error", GUI_PANEL_MOVEABLE|GUI_PANEL_BORDER);
gui_panel_layout(panel, 30, 2);
if (gui_panel_button_text(panel, "ok", GUI_BUTTON_SWITCH))
fprintf(stdout, "ok pressed!\n");
if (gui_panel_button_text(panel, "cancel", GUI_BUTTON_SWITCH))
fprintf(stdout, "cancel pressed!\n");
if (gui_panel_button_text(panel, "ok", GUI_BUTTON_SWITCH)) ret = 1;
if (gui_panel_button_text(panel, "cancel", GUI_BUTTON_SWITCH)) ret = 0;
if (ret != -1) gui_panel_hide(panel);
gui_end_panel(ctx, panel, NULL);
return ret;
}
static gui_int
@ -180,11 +181,13 @@ color_picker_panel(struct gui_context *ctx, struct gui_panel *panel, struct colo
gui_size i;
gui_int ret = -1;
gui_byte *ptr = &picker->color.r;
const char str[] = "R:G:B:A:";
gui_begin_panel(ctx, panel, "Color Picker", GUI_PANEL_MOVEABLE|GUI_PANEL_BORDER);
gui_panel_layout(panel, 30, 2);
gui_panel_layout(panel, 30, 3);
for (i = 0; i < 4; ++i) {
gui_int ivalue;
gui_float fvalue = (gui_float)*ptr;
gui_panel_text(panel, &str[i*2], 2, GUI_TEXT_CENTERED);
fvalue = gui_panel_slider(panel, 0, fvalue, 255.0f, 10.0f, GUI_HORIZONTAL);
ivalue = (gui_int)fvalue;
picker->active[i] = gui_panel_spinner(panel, 0, &ivalue, 255, 1, picker->active[i]);
@ -196,6 +199,7 @@ color_picker_panel(struct gui_context *ctx, struct gui_panel *panel, struct colo
gui_panel_seperator(panel, 1);
if (gui_panel_button_text(panel, "ok", GUI_BUTTON_SWITCH)) ret = 1;
if (gui_panel_button_text(panel, "cancel", GUI_BUTTON_SWITCH)) ret = 0;
if (ret != -1) gui_panel_hide(panel);
gui_end_panel(ctx, panel, NULL);
return ret;
}
@ -210,8 +214,8 @@ demo_panel(struct gui_context *ctx, struct gui_panel *panel, struct demo *demo)
gui_bool running;
running = gui_begin_panel(ctx, panel, "Demo",
GUI_PANEL_CLOSEABLE|GUI_PANEL_MINIMIZABLE|
GUI_PANEL_SCALEABLE|GUI_PANEL_MOVEABLE|GUI_PANEL_BORDER);
GUI_PANEL_CLOSEABLE|GUI_PANEL_MINIMIZABLE|GUI_PANEL_SCALEABLE|
GUI_PANEL_MOVEABLE|GUI_PANEL_BORDER);
/* Tabs */
gui_panel_layout(panel, 100, 1);

View File

@ -221,11 +221,11 @@ main(int argc, char *argv[])
/* ------------------------- GUI --------------------------*/
gui_begin(ctx, (gui_float)width, (gui_float)height);
running = demo_panel(ctx, panel, &demo);
message_panel(ctx, message);
if (message_panel(ctx, message) >= 0)
fprintf(stdout, "message closed\n");
if (color_picker_panel(ctx, color_panel, &picker) >= 0) {
struct gui_color c = picker.color;
fprintf(stdout, "color picked: {%u, %u, %u, %u}\n", c.r, c.g, c.b, c.a);
gui_panel_hide(color_panel);
}
gui_end(ctx, &output, NULL);
/* ---------------------------------------------------------*/

59
gui.c
View File

@ -733,6 +733,7 @@ gui_widget_text(struct gui_draw_buffer *buffer, const struct gui_text *text,
gui_float label_y;
gui_float label_w;
gui_float label_h;
gui_size text_width;
assert(buffer);
assert(text);
@ -740,50 +741,25 @@ gui_widget_text(struct gui_draw_buffer *buffer, const struct gui_text *text,
if (!buffer || !text || !font)
return;
label_x = text->x + text->pad_x;
text_width = gui_font_text_width(font, (const gui_char*)text->text, text->length);
label_y = text->y + text->pad_y;
label_w = MAX(0, text->w - 2 * text->pad_x);
label_h = MAX(0, text->h - 2 * text->pad_y);
if (text->align == GUI_TEXT_LEFT) {
label_x = text->x + text->pad_x;
label_w = MAX(0, text->w - 2 * text->pad_x);
} else if (text->align == GUI_TEXT_CENTERED) {
label_w = 2 * text->pad_x + (gui_float)text_width;
label_x = text->x + ((text->w/2) - (label_w/2));
} else if (text->align == GUI_TEXT_RIGHT) {
label_x = MAX(text->x, (text->x + text->w) - (2 * text->pad_x + (gui_float)text_width));
label_w = (gui_float)text_width + 2 * text->pad_x;
}
gui_draw_rectf(buffer, text->x, text->y, text->w, text->h, text->background);
gui_draw_string(buffer, font, label_x, label_y, label_w, label_h,
text->font, (const gui_char*)text->text, text->length);
}
gui_size
gui_widget_text_wrap(struct gui_draw_buffer *buffer, const struct gui_text *text,
const struct gui_font *font)
{
gui_size len = 0;
gui_size lines = 0;
gui_size chars = 0;
gui_float label_x, label_y;
gui_float label_w, label_h;
gui_float space;
if (!buffer || !text || !font)
return 0;
label_x = text->x + text->pad_x;
label_y = text->y + text->pad_y;
label_w = MAX(0, text->w - 2 * text->pad_x);
label_h = MAX(0, text->h - 2 * text->pad_y);
space = font->height + 2 * text->pad_y;
chars = gui_font_chars_in_space(font, (const gui_char*)text->text, text->length, label_w);
while (chars && label_h >= space) {
lines++;
gui_draw_string(buffer, font, label_x, label_y, label_w, space,
text->font, (const gui_char*)text->text + len, chars);
len += chars;
label_h -= space;
label_y += space;
chars = gui_font_chars_in_space(font, (const gui_char*)text->text + chars,
text->length - chars, label_w);
}
return lines;
}
void
gui_widget_image(struct gui_draw_buffer *buffer, const struct gui_image *image)
{
@ -1775,7 +1751,8 @@ gui_panel_alloc_space(struct gui_rect *bounds, struct gui_panel *panel)
}
void
gui_panel_text(struct gui_panel *panel, const char *str, gui_size len)
gui_panel_text(struct gui_panel *panel, const char *str, gui_size len,
enum gui_text_align alignment)
{
struct gui_rect bounds;
struct gui_text text;
@ -1800,6 +1777,7 @@ gui_panel_text(struct gui_panel *panel, const char *str, gui_size len)
text.pad_y = config->item_padding.y;
text.text = str;
text.length = len;
text.align = alignment;
text.font = config->colors[GUI_COLOR_TEXT];
text.background = config->colors[GUI_COLOR_PANEL];
gui_widget_text(panel->out, &text, panel->font);
@ -2820,8 +2798,9 @@ gui_begin_panel(struct gui_context *ctx, struct gui_panel *panel,
if (in->mouse_down && in->mouse_clicked && inpanel && cpanel != ctx->active) {
struct gui_context_panel *iter = cpanel->next;
while (iter) {
if (INBOX(in->mouse_prev.x, in->mouse_prev.y, iter->x, iter->y, iter->w, iter->h))
break;
if (!iter->panel.minimized)
if (INBOX(in->mouse_prev.x, in->mouse_prev.y, iter->x, iter->y, iter->w, iter->h))
break;
iter = iter->next;
}
if (!iter) {

11
gui.h
View File

@ -135,12 +135,19 @@ struct gui_output {
gui_size list_size;
};
enum gui_text_align {
GUI_TEXT_LEFT,
GUI_TEXT_CENTERED,
GUI_TEXT_RIGHT
};
struct gui_text {
gui_float x, y;
gui_float w, h;
gui_float pad_x, pad_y;
const char *text;
gui_size length;
enum gui_text_align align;
struct gui_color font;
struct gui_color background;
};
@ -364,8 +371,6 @@ void gui_output_end(struct gui_draw_buffer*, struct gui_draw_call_list*,
/* Widgets */
void gui_widget_text(struct gui_draw_buffer*, const struct gui_text*,
const struct gui_font*);
gui_size gui_widget_text_wrap(struct gui_draw_buffer*, const struct gui_text*,
const struct gui_font*);
void gui_widget_image(struct gui_draw_buffer*, const struct gui_image*);
gui_bool gui_widget_button_text(struct gui_draw_buffer*, const struct gui_button*,
const char *text, gui_size len, const struct gui_font*,
@ -407,7 +412,7 @@ gui_bool gui_panel_begin(struct gui_panel*, struct gui_draw_buffer*,
gui_float x, gui_float y, gui_float w, gui_float h, gui_flags);
void gui_panel_layout(struct gui_panel*, gui_float height, gui_size cols);
void gui_panel_seperator(struct gui_panel*, gui_size cols);
void gui_panel_text(struct gui_panel*, const char *str, gui_size len);
void gui_panel_text(struct gui_panel*, const char *str, gui_size len, enum gui_text_align);
gui_bool gui_panel_check(struct gui_panel*, const char*, gui_bool active);
gui_bool gui_panel_option(struct gui_panel*, const char*, gui_bool active);
gui_bool gui_panel_button_text(struct gui_panel*, const char*, enum gui_button_behavior);