From 8f3280e339329fa9400a4d2bca9a259beaaac48a Mon Sep 17 00:00:00 2001 From: vurtun Date: Sat, 15 Aug 2015 12:00:01 +0200 Subject: [PATCH] some demo cleanup --- demo/demo.c | 763 +++++++++++++++++++++++++++++++--------------------- gui.c | 116 ++++---- gui.h | 91 +++---- 3 files changed, 546 insertions(+), 424 deletions(-) diff --git a/demo/demo.c b/demo/demo.c index 6086c318..3128def0 100644 --- a/demo/demo.c +++ b/demo/demo.c @@ -3,8 +3,78 @@ #define WINDOW_WIDTH 800 #define WINDOW_HEIGHT 600 +#include + +#define WEAPON_MAP(WEAPON)\ + WEAPON(FIST, "Fist")\ + WEAPON(PISTOL, "Pistol")\ + WEAPON(SHOTGUN, "Shotgun")\ + WEAPON(RAILGUN, "Railgun")\ + WEAPON(BFG, "BFG") + +#define MENU_FILE_ITEMS(ITEM)\ + ITEM(OPEN, "Open")\ + ITEM(CLOSE, "Close")\ + ITEM(QUIT, "Quit") + +#define MENU_EDIT_ITEMS(ITEM)\ + ITEM(COPY, "Copy")\ + ITEM(CUT, "Cut")\ + ITEM(DELETE, "Delete")\ + ITEM(PASTE, "Paste") + +enum weapon_types { +#define WEAPON(id, name) WEAPON_##id, + WEAPON_MAP(WEAPON) +#undef WEAPON + WEAPON_MAX +}; +enum menu_file_items { +#define ITEM(id, name) MENU_FILE_##id, + MENU_FILE_ITEMS(ITEM) +#undef ITEM + MENU_FILE_MAX +}; +enum menu_edit_items { +#define ITEM(id, name) MENU_EDIT_##id, + MENU_EDIT_ITEMS(ITEM) +#undef ITEM + MENU_EDIT_MAX +}; + +static const char *weapons[] = { +#define WEAPON(id,name) name, + WEAPON_MAP(WEAPON) +#undef WEAPON +}; +static const char *file_items[] = { +#define ITEM(id,name) name, + MENU_FILE_ITEMS(ITEM) +#undef ITEM +}; +static const char *edit_items[] = { +#define ITEM(id,name) name, + MENU_EDIT_ITEMS(ITEM) +#undef ITEM +}; + +struct input { + gui_char in_buf[MAX_BUFFER]; + gui_size in_len; + gui_state in_active; +}; + +/* ================================================================= + * + * CUSTOM WIDGET + * + * ================================================================= + */ +/* ----------------------------------------------------------------- + * TREE WIDGET + * ----------------------------------------------------------------- */ struct tree_node { - gui_tree_node_state state; + gui_state state; const char *name; struct tree_node *parent; struct tree_node *children[8]; @@ -14,89 +84,55 @@ struct tree_node { struct test_tree { struct tree_node root; struct tree_node *clipboard[16]; + struct tree_node nodes[8]; int count; }; -struct state { - /* input buffer */ - gui_char input_buffer[MAX_BUFFER]; - struct gui_edit_box input; - gui_char in_buf[MAX_BUFFER]; - gui_size in_len; - gui_bool in_active; +static void +tree_init(struct test_tree *tree) +{ + /* this is just test data */ + tree->root.state = GUI_NODE_ACTIVE; + tree->root.name = "Primitives"; + tree->root.parent = NULL; + tree->root.count = 2; + tree->root.children[0] = &tree->nodes[0]; + tree->root.children[1] = &tree->nodes[4]; - /* menubar state */ - gui_bool file_open; - gui_bool edit_open; + tree->nodes[0].state = 0; + tree->nodes[0].name = "Boxes"; + tree->nodes[0].parent = &tree->root; + tree->nodes[0].count = 2; + tree->nodes[0].children[0] = &tree->nodes[1]; + tree->nodes[0].children[1] = &tree->nodes[2]; - /* widgets state */ - gui_size menu_item; - gui_bool scaleable; - gui_bool checkbox; - gui_float slider; - gui_size progressbar; - gui_int spinner; - gui_bool spinner_active; - gui_size item_current; - gui_size shelf_selection; - gui_bool toggle; - gui_int option; + tree->nodes[1].state = 0; + tree->nodes[1].name = "Box0"; + tree->nodes[1].parent = &tree->nodes[0]; + tree->nodes[1].count = 0; - gui_bool popup; - gui_size cur; - gui_size op; + tree->nodes[2].state = 0; + tree->nodes[2].name = "Box1"; + tree->nodes[2].parent = &tree->nodes[0]; + tree->nodes[2].count = 0; - /* combo */ - struct gui_color combo_color; - gui_size combo_prog[4]; - gui_bool combo_sel[4]; - gui_size sel_item; - gui_bool col_act; - gui_bool sel_act; - gui_bool box_act; - gui_bool prog_act; + tree->nodes[4].state = GUI_NODE_ACTIVE; + tree->nodes[4].name = "Cylinders"; + tree->nodes[4].parent = &tree->root; + tree->nodes[4].count = 2; + tree->nodes[4].children[0] = &tree->nodes[5]; + tree->nodes[4].children[1] = &tree->nodes[6]; - /* tree */ - struct test_tree tree; - struct tree_node nodes[8]; + tree->nodes[5].state = 0; + tree->nodes[5].name = "Cylinder0"; + tree->nodes[5].parent = &tree->nodes[4]; + tree->nodes[5].count = 0; - /* tabs */ - enum gui_node_state config_tab; - enum gui_node_state widget_tab; - enum gui_node_state combo_tab; - enum gui_node_state style_tab; - enum gui_node_state round_tab; - enum gui_node_state color_tab; - enum gui_node_state flag_tab; - - /* scrollbars */ - struct gui_vec2 shelf_scrollbar; - struct gui_vec2 table_scrollbar; - struct gui_vec2 tree_scrollbar; - - /* color picker */ - gui_bool picker_active; - gui_bool spinner_r_active; - gui_bool spinner_g_active; - gui_bool spinner_b_active; - gui_bool spinner_a_active; - gui_size current_color; - struct gui_color color; - -}; - -struct demo_gui { - gui_bool running; - void *memory; - const struct gui_input *input; - struct gui_command_queue queue; - struct gui_config config; - struct gui_font font; - struct gui_panel panel; - struct gui_panel sub; - struct state state; - gui_size w, h; -}; + tree->nodes[6].state = 0; + tree->nodes[6].name = "Cylinder1"; + tree->nodes[6].parent = &tree->nodes[4]; + tree->nodes[6].count = 0; +} static void tree_remove_node(struct tree_node *parent, struct tree_node *child) @@ -179,15 +215,253 @@ upload_tree(struct test_tree *base, struct gui_tree *tree, struct tree_node *nod return 1; } +/* ----------------------------------------------------------------- + * COLOR PICKER POPUP + * ----------------------------------------------------------------- */ +struct color_picker { + gui_state active; + struct gui_color color; + gui_state r, g, b, a; + gui_size index; +}; + +static gui_bool +color_picker(struct gui_panel_layout *panel, struct color_picker* control, + const char *name, struct gui_color *color) +{ + int i; + gui_byte *iter; + gui_bool ret = gui_true; + struct gui_panel_layout popup; + gui_panel_popup_begin(panel, &popup, GUI_POPUP_STATIC, gui_rect(20, 100, 220, 280), gui_vec2(0,0)); + { + if (gui_panel_header(&popup, "Color Picker", GUI_CLOSEABLE, GUI_CLOSEABLE, GUI_HEADER_LEFT)) { + gui_panel_popup_close(&popup); + return gui_false; + } + gui_panel_row_dynamic(&popup, 30, 2); + gui_panel_label(&popup, name, GUI_TEXT_LEFT); + gui_panel_button_color(&popup, control->color, GUI_BUTTON_DEFAULT); + + iter = &control->color.r; + gui_panel_row_dynamic(&popup, 30, 2); + for (i = 0; i < 4; ++i, iter++) { + gui_float t = *iter; + t = gui_panel_slider(&popup, 0, t, 255, 10); + *iter = (gui_byte)t; + *iter = (gui_byte)gui_panel_spinner(&popup, 0, *iter, 255, 1, NULL); + } + + gui_panel_row_dynamic(&popup, 30, 3); + gui_panel_spacing(&popup, 1); + if (gui_panel_button_text(&popup, "ok", GUI_BUTTON_DEFAULT)) { + gui_panel_popup_close(&popup); + *color = control->color; + ret = gui_false; + } + if (gui_panel_button_text(&popup, "cancel", GUI_BUTTON_DEFAULT)) { + gui_panel_popup_close(&popup); + ret = gui_false; + } + } + gui_panel_popup_end(panel, &popup); + control->active = (gui_state)ret; + return ret; +} + +/* ----------------------------------------------------------------- + * LABEL + * ----------------------------------------------------------------- */ +static void +gui_panel_labelf(struct gui_panel_layout *panel, enum gui_text_align align, const gui_char *fmt, ...) +{ + gui_char buffer[1024]; + va_list args; + va_start(args, fmt); + vsnprintf(buffer, sizeof(buffer), fmt, args); + gui_panel_label(panel, buffer, align); + va_end(args); + buffer[1023] = 0; +} + +/* ----------------------------------------------------------------- + * COMBOBOXES + * ----------------------------------------------------------------- */ +struct combobox { + gui_size selected; + gui_state active; + struct gui_vec2 scrollbar; +}; + +struct check_combo_box { + gui_bool values[4]; + gui_state active; + struct gui_vec2 scrollbar; +}; + +struct prog_combo_box { + gui_state active; + struct gui_vec2 scrollbar; +}; + +struct color_combo_box { + gui_state active; + struct gui_color color; + struct gui_vec2 scrollbar; +}; + +static void +combo_box(struct gui_panel_layout *panel, struct combobox *combo, + const char**values, gui_size count) +{ + gui_panel_combo(panel, values, count, &combo->selected, 30, + &combo->active, &combo->scrollbar); +} + +static void +prog_combo_box(struct gui_panel_layout *panel, gui_size *values, gui_size count, + struct prog_combo_box *demo) +{ + gui_size i = 0; + gui_int sum = 0; + gui_char buffer[64]; + struct gui_panel_layout combo; + memset(&combo, 0, sizeof(combo)); + for (i = 0; i < count; ++i) + sum += (gui_int)values[i]; + + sprintf(buffer, "%d", sum); + gui_panel_combo_begin(panel, &combo, buffer, &demo->active, demo->scrollbar); + { + gui_panel_row_dynamic(&combo, 30, 1); + for (i = 0; i < count; ++i) + values[i] = gui_panel_progress(&combo, values[i], 100, gui_true); + } + demo->scrollbar = gui_panel_combo_end(panel, &combo); +} + +static void +color_combo_box(struct gui_panel_layout *panel, struct color_combo_box *demo) +{ + /* color slider progressbar */ + gui_char buffer[32]; + struct gui_panel_layout combo; + memset(&combo, 0, sizeof(combo)); + sprintf(buffer, "#%02x%02x%02x%02x", demo->color.r, demo->color.g, + demo->color.b, demo->color.a); + gui_panel_combo_begin(panel, &combo, buffer, &demo->active, demo->scrollbar); + { + int i; + const char *color_names[] = {"R:", "G:", "B:", "A:"}; + gui_float ratios[] = {0.15f, 0.85f}; + gui_byte *iter = &demo->color.r; + gui_panel_row(&combo, GUI_DYNAMIC, 30, 2, ratios); + for (i = 0; i < 4; ++i, iter++) { + gui_float t = *iter; + gui_panel_label(&combo, color_names[i], GUI_TEXT_LEFT); + t = gui_panel_slider(&combo, 0, t, 255, 5); + *iter = (gui_byte)t; + } + } + demo->scrollbar = gui_panel_combo_end(panel, &combo); +} + +static void +check_combo_box(struct gui_panel_layout *panel, gui_bool *values, gui_size count, + struct check_combo_box *demo) +{ + /* checkbox combobox */ + gui_int sum = 0; + gui_size i = 0; + gui_char buffer[64]; + struct gui_panel_layout combo; + memset(&combo, 0, sizeof(combo)); + for (i = 0; i < count; ++i) + sum += (gui_int)values[i]; + + sprintf(buffer, "%d", sum); + gui_panel_combo_begin(panel, &combo, buffer, &demo->active, demo->scrollbar); + { + gui_panel_row_dynamic(&combo, 30, 1); + for (i = 0; i < count; ++i) + values[i] = gui_panel_check(&combo, weapons[i], values[i]); + } + demo->scrollbar = gui_panel_combo_end(panel, &combo); +} + +/* ================================================================= + * + * DEMO + * + * ================================================================= + */ +struct state { + gui_char edit_buffer[MAX_BUFFER]; + struct gui_edit_box edit; + struct color_picker picker; + struct check_combo_box checkcom; + struct prog_combo_box progcom; + struct color_combo_box colcom; + struct combobox combo; + struct test_tree test; + + /* widgets state */ + gui_size prog_values[4]; + gui_bool check_values[WEAPON_MAX]; + gui_bool scaleable; + gui_bool checkbox; + gui_float slider; + gui_size progressbar; + gui_int spinner; + gui_state spinner_active; + gui_size item_current; + gui_size shelf_selection; + gui_bool toggle; + gui_int option; + gui_state popup; + gui_size cur; + gui_size op; + + /* subpanels */ + struct gui_vec2 shelf; + struct gui_vec2 table; + struct gui_vec2 tree; + struct gui_vec2 menu; + + /* open/close state */ + gui_state file_open; + gui_state edit_open; + gui_state config_tab; + gui_state widget_tab; + gui_state combo_tab; + gui_state style_tab; + gui_state round_tab; + gui_state color_tab; + gui_state flag_tab; +}; + +struct demo_gui { + gui_bool running; + void *memory; + const struct gui_input *input; + struct gui_command_queue queue; + struct gui_config config; + struct gui_font font; + struct gui_panel panel; + struct gui_panel sub; + struct state state; + gui_size w, h; +}; + +/* ----------------------------------------------------------------- + * WIDGETS + * ----------------------------------------------------------------- */ static void widget_panel(struct gui_panel_layout *panel, struct state *demo) { - const char *items[] = {"Fist", "Pistol", "Shotgun", "Railgun", "BFG"}; - /* Labels */ gui_panel_row_dynamic(panel, 30, 1); demo->scaleable = gui_panel_check(panel, "Scaleable Layout", demo->scaleable); - if (!demo->scaleable) gui_panel_row_static(panel, 30, 150, 1); gui_panel_label(panel, "text left", GUI_TEXT_LEFT); @@ -201,133 +475,50 @@ widget_panel(struct gui_panel_layout *panel, struct state *demo) fprintf(stdout, "right triangle button pressed!\n"); if (gui_panel_button_text_triangle(panel,GUI_LEFT,"previous",GUI_TEXT_RIGHT,GUI_BUTTON_DEFAULT)) fprintf(stdout, "left triangle button pressed!\n"); - demo->toggle = gui_panel_button_toggle(panel, "toggle", demo->toggle); - demo->checkbox = gui_panel_check(panel, "checkbox", demo->checkbox); + /* checkbox + radio buttons */ + demo->checkbox = gui_panel_check(panel, "checkbox", demo->checkbox); if (!demo->scaleable) gui_panel_row_static(panel, 30, 75, 2); else gui_panel_row_dynamic(panel, 30, 2); - - if (gui_panel_option(panel, "option 0", demo->option == 0)) demo->option = 0; - if (gui_panel_option(panel, "option 1", demo->option == 1)) demo->option = 1; + if (gui_panel_option(panel, "option 0", demo->option == 0)) + demo->option = 0; + if (gui_panel_option(panel, "option 1", demo->option == 1)) + demo->option = 1; { - /* templated row layout */ - char buffer[MAX_BUFFER]; - if (demo->scaleable) { - const gui_float ratio[] = {0.8f, 0.2f}; - gui_panel_row(panel, GUI_DYNAMIC, 30, 2, ratio); - demo->slider = gui_panel_slider(panel, 0, demo->slider, 10, 1.0f); - sprintf(buffer, "%.2f", demo->slider); - gui_panel_label(panel, buffer, GUI_TEXT_LEFT); - demo->progressbar = gui_panel_progress(panel, demo->progressbar, 100, gui_true); - sprintf(buffer, "%lu", demo->progressbar); - gui_panel_label(panel, buffer, GUI_TEXT_LEFT); - } else { - const gui_float ratio[] = {150.0f, 30.0f}; - gui_panel_row(panel, GUI_STATIC, 30, 2, ratio); - demo->slider = gui_panel_slider(panel, 0, demo->slider, 10, 1.0f); - sprintf(buffer, "%.2f", demo->slider); - gui_panel_label(panel, buffer, GUI_TEXT_LEFT); - demo->progressbar = gui_panel_progress(panel, demo->progressbar, 100, gui_true); - sprintf(buffer, "%lu", demo->progressbar); - gui_panel_label(panel, buffer, GUI_TEXT_LEFT); - } + /* retain mode custom row layout */ + const gui_float ratio[] = {0.8f, 0.2f}; + const gui_float pixel[] = {150.0f, 30.0f}; + enum gui_panel_row_layout_format fmt = (demo->scaleable) ? GUI_DYNAMIC : GUI_STATIC; + gui_panel_row(panel, fmt, 30, 2, (fmt == GUI_DYNAMIC) ? ratio: pixel); + demo->slider = gui_panel_slider(panel, 0, demo->slider, 10, 1.0f); + gui_panel_labelf(panel, GUI_TEXT_LEFT, "%.2f", demo->slider); + demo->progressbar = gui_panel_progress(panel, demo->progressbar, 100, gui_true); + gui_panel_labelf(panel, GUI_TEXT_LEFT, "%lu", demo->progressbar); } - if (!demo->scaleable) - gui_panel_row_static(panel, 30, 150, 1); + /* item selection */ + if (!demo->scaleable) gui_panel_row_static(panel, 30, 150, 1); else gui_panel_row_dynamic(panel, 30, 1); + demo->item_current = gui_panel_selector(panel, weapons, LEN(weapons), demo->item_current); + combo_box(panel, &demo->combo, weapons, LEN(weapons)); + prog_combo_box(panel, demo->prog_values, LEN(demo->prog_values), &demo->progcom); + color_combo_box(panel, &demo->colcom); + check_combo_box(panel, demo->check_values, LEN(demo->check_values), &demo->checkcom); + demo->spinner = gui_panel_spinner(panel, 0, demo->spinner, 250, 10, &demo->spinner_active); - demo->item_current = gui_panel_selector(panel, items, LEN(items), demo->item_current); - gui_panel_combo(panel, items, LEN(items), &demo->sel_item, 30, &demo->sel_act, gui_vec2(0,0)); { - /* progressbar combobox */ - gui_int sum; - gui_char buffer[64]; - struct gui_panel_layout combo; - memset(&combo, 0, sizeof(combo)); - - sum = (gui_int)(demo->combo_prog[0] + demo->combo_prog[1]); - sum += (gui_int)(demo->combo_prog[2] + demo->combo_prog[3]); - sprintf(buffer, "%d", sum); - gui_panel_combo_begin(panel, &combo, buffer, &demo->prog_act, gui_vec2(0,0)); + /* immediate mode custom row layout */ + enum gui_panel_row_layout_format fmt = (demo->scaleable) ? GUI_DYNAMIC : GUI_STATIC; + gui_panel_row_begin(panel, fmt, 30, 2); { - gui_panel_row_dynamic(&combo, 30, 1); - demo->combo_prog[0] = gui_panel_progress(&combo, demo->combo_prog[0], 100, gui_true); - demo->combo_prog[1] = gui_panel_progress(&combo, demo->combo_prog[1], 100, gui_true); - demo->combo_prog[2] = gui_panel_progress(&combo, demo->combo_prog[2], 100, gui_true); - demo->combo_prog[3] = gui_panel_progress(&combo, demo->combo_prog[3], 100, gui_true); - } - gui_panel_combo_end(panel, &combo); - } - { - /* color slider progressbar */ - gui_char buffer[32]; - struct gui_panel_layout combo; - memset(&combo, 0, sizeof(combo)); - sprintf(buffer, "#%02x%02x%02x%02x", demo->combo_color.r, demo->combo_color.g, - demo->combo_color.b, demo->combo_color.a); - gui_panel_combo_begin(panel, &combo, buffer, &demo->col_act, gui_vec2(0,0)); - { - int i; - const char *color_names[] = {"R:", "G:", "B:", "A:"}; - gui_float ratios[] = {0.15f, 0.85f}; - gui_byte *iter = &demo->combo_color.r; - gui_panel_row(&combo, GUI_DYNAMIC, 30, 2, ratios); - for (i = 0; i < 4; ++i, iter++) { - gui_float t = *iter; - gui_panel_label(&combo, color_names[i], GUI_TEXT_LEFT); - t = gui_panel_slider(&combo, 0, t, 255, 5); - *iter = (gui_byte)t; - } - } - gui_panel_combo_end(panel, &combo); - } - { - /* checkbox combobox */ - gui_int sum; - gui_char buffer[64]; - struct gui_panel_layout combo; - memset(&combo, 0, sizeof(combo)); - sum = demo->combo_sel[0] + demo->combo_sel[1]; - sum += demo->combo_sel[2] + demo->combo_sel[3]; - sprintf(buffer, "%d", sum); - gui_panel_combo_begin(panel, &combo, buffer, &demo->box_act, gui_vec2(0,0)); - { - gui_panel_row_dynamic(&combo, 30, 1); - demo->combo_sel[0] = gui_panel_check(&combo, items[0], demo->combo_sel[0]); - demo->combo_sel[1] = gui_panel_check(&combo, items[1], demo->combo_sel[1]); - demo->combo_sel[2] = gui_panel_check(&combo, items[2], demo->combo_sel[2]); - demo->combo_sel[3] = gui_panel_check(&combo, items[3], demo->combo_sel[3]); - } - gui_panel_combo_end(panel, &combo); - } - demo->spinner= gui_panel_spinner(panel, 0, demo->spinner, 250, 10, &demo->spinner_active); - demo->in_len = gui_panel_edit(panel, demo->in_buf, demo->in_len, MAX_BUFFER, - &demo->in_active, NULL, GUI_INPUT_DEFAULT); - - if (demo->scaleable) { - gui_panel_row_begin(panel, GUI_DYNAMIC, 30, 2); - { - gui_panel_row_push(panel, 0.7f); - gui_panel_editbox(panel, &demo->input); - gui_panel_row_push(panel, 0.3f); + gui_panel_row_push(panel,(fmt == GUI_DYNAMIC) ? 0.7f : 100); + gui_panel_editbox(panel, &demo->edit); + gui_panel_row_push(panel, (fmt == GUI_DYNAMIC) ? 0.3f : 80); if (gui_panel_button_text(panel, "submit", GUI_BUTTON_DEFAULT)) { - gui_edit_box_clear(&demo->input); - fprintf(stdout, "command executed!\n"); - } - } - gui_panel_row_end(panel); - } else { - gui_panel_row_begin(panel, GUI_STATIC, 30, 2); - { - gui_panel_row_push(panel, 100); - gui_panel_editbox(panel, &demo->input); - gui_panel_row_push(panel, 80); - if (gui_panel_button_text(panel, "submit", GUI_BUTTON_DEFAULT)) { - gui_edit_box_clear(&demo->input); + gui_edit_box_clear(&demo->edit); fprintf(stdout, "command executed!\n"); } } @@ -365,6 +556,9 @@ table_panel(struct gui_panel_layout *panel) gui_panel_table_end(panel); } +/* ----------------------------------------------------------------- + * STYLE + * ----------------------------------------------------------------- */ static void update_flags(struct gui_panel_layout *panel) { @@ -415,34 +609,6 @@ round_tab(struct gui_panel_layout *panel, struct gui_config *config) } } -static struct gui_color -color_picker(struct gui_panel_layout *panel, struct state *control, - const char *name, struct gui_color color) -{ - int i; - gui_byte *iter; - gui_bool *active[4]; - - active[0] = &control->spinner_r_active; - active[1] = &control->spinner_g_active; - active[2] = &control->spinner_b_active; - active[3] = &control->spinner_a_active; - - gui_panel_row_dynamic(panel, 30, 2); - gui_panel_label(panel, name, GUI_TEXT_LEFT); - gui_panel_button_color(panel, color, GUI_BUTTON_DEFAULT); - - iter = &color.r; - gui_panel_row_dynamic(panel, 30, 2); - for (i = 0; i < 4; ++i, iter++) { - gui_float t = *iter; - t = gui_panel_slider(panel, 0, t, 255, 10); - *iter = (gui_byte)t; - *iter = (gui_byte)gui_panel_spinner(panel, 0, *iter, 255, 1, active[i]); - } - return color; -} - static void color_tab(struct gui_panel_layout *panel, struct state *control, struct gui_config *config) { @@ -460,33 +626,25 @@ color_tab(struct gui_panel_layout *panel, struct state *control, struct gui_conf "Shelf:", "Shelf Text:", "Shelf Active:", "Shelf Active Text:", "Scaler:", "Layout Scaler" }; - - if (control->picker_active) { - control->color = color_picker(panel,control,labels[control->current_color], control->color); - gui_panel_row_dynamic(panel, 30, 3); - gui_panel_spacing(panel, 1); - if (gui_panel_button_text(panel, "ok", GUI_BUTTON_DEFAULT)) { - config->colors[control->current_color] = control->color; - control->picker_active = gui_false; - } - if (gui_panel_button_text(panel, "cancel", GUI_BUTTON_DEFAULT)) - control->picker_active = gui_false; - } else { - gui_panel_row_dynamic(panel, 30, 2); - for (i = 0; i < GUI_COLOR_COUNT; ++i) { - struct gui_color c = config->colors[i]; - gui_panel_label(panel, labels[i], GUI_TEXT_LEFT); - if (gui_panel_button_color(panel, c, GUI_BUTTON_DEFAULT)) { - if (!control->picker_active) { - control->picker_active = gui_true; - control->color = config->colors[i]; - control->current_color = i; - } else continue; - } + gui_panel_row_dynamic(panel, 30, 2); + for (i = 0; i < GUI_COLOR_COUNT; ++i) { + struct gui_color c = config->colors[i]; + gui_panel_label(panel, labels[i], GUI_TEXT_LEFT); + if (gui_panel_button_color(panel, c, GUI_BUTTON_DEFAULT)) { + control->picker.active = gui_true; + control->picker.color = config->colors[i]; + control->picker.index = i; } } + if (control->picker.active) { + color_picker(panel, &control->picker, labels[control->picker.index], + &config->colors[control->picker.index]); + } } +/* ----------------------------------------------------------------- + * COPY & PASTE + * ----------------------------------------------------------------- */ static void copy(gui_handle handle, const char *text, gui_size size) { @@ -510,6 +668,9 @@ paste(gui_handle handle, struct gui_edit_box *box) gui_edit_box_add(box, text, len); } +/* ----------------------------------------------------------------- + * INIT + * ----------------------------------------------------------------- */ static void init_demo(struct demo_gui *gui, struct gui_font *font) { @@ -531,75 +692,26 @@ init_demo(struct demo_gui *gui, struct gui_font *font) &gui->queue, config, gui->input); /* widget state */ + tree_init(&win->test); clip.userdata.ptr = NULL, clip.copy = copy; clip.paste = paste; - gui_edit_box_init_fixed(&win->input, win->input_buffer, MAX_BUFFER, &clip, NULL); + gui_edit_box_init_fixed(&win->edit, win->edit_buffer, MAX_BUFFER, &clip, NULL); - win->config_tab = GUI_MINIMIZED; - win->widget_tab = GUI_MINIMIZED; - win->combo_tab = GUI_MINIMIZED; - win->style_tab = GUI_MINIMIZED; - win->round_tab = GUI_MINIMIZED; - win->color_tab = GUI_MINIMIZED; - win->flag_tab = GUI_MINIMIZED; - - win->combo_prog[0] = 30; - win->combo_prog[1] = 80; - win->combo_prog[2] = 70; - win->combo_prog[3] = 50; + win->prog_values[0] = 30; + win->prog_values[1] = 80; + win->prog_values[2] = 70; + win->prog_values[3] = 50; win->scaleable = gui_true; win->slider = 2.0f; win->progressbar = 50; win->spinner = 100; - - { - /* test tree data */ - struct test_tree *tree = &win->tree; - tree->root.state = GUI_NODE_ACTIVE; - tree->root.name = "Primitives"; - tree->root.parent = NULL; - tree->root.count = 2; - tree->root.children[0] = &win->nodes[0]; - tree->root.children[1] = &win->nodes[4]; - - win->nodes[0].state = 0; - win->nodes[0].name = "Boxes"; - win->nodes[0].parent = &tree->root; - win->nodes[0].count = 2; - win->nodes[0].children[0] = &win->nodes[1]; - win->nodes[0].children[1] = &win->nodes[2]; - - win->nodes[1].state = 0; - win->nodes[1].name = "Box0"; - win->nodes[1].parent = &win->nodes[0]; - win->nodes[1].count = 0; - - win->nodes[2].state = 0; - win->nodes[2].name = "Box1"; - win->nodes[2].parent = &win->nodes[0]; - win->nodes[2].count = 0; - - win->nodes[4].state = GUI_NODE_ACTIVE; - win->nodes[4].name = "Cylinders"; - win->nodes[4].parent = &tree->root; - win->nodes[4].count = 2; - win->nodes[4].children[0] = &win->nodes[5]; - win->nodes[4].children[1] = &win->nodes[6]; - - win->nodes[5].state = 0; - win->nodes[5].name = "Cylinder0"; - win->nodes[5].parent = &win->nodes[4]; - win->nodes[5].count = 0; - - win->nodes[6].state = 0; - win->nodes[6].name = "Cylinder1"; - win->nodes[6].parent = &win->nodes[4]; - win->nodes[6].count = 0; - } } +/* ----------------------------------------------------------------- + * RUN + * ----------------------------------------------------------------- */ static void run_demo(struct demo_gui *gui) { @@ -608,25 +720,49 @@ run_demo(struct demo_gui *gui) struct gui_panel_layout tab; struct gui_config *config = &gui->config; static const char *shelfs[] = {"Histogram", "Lines"}; - enum {EASY, HARD}; /* first panel */ gui_panel_begin(&layout, &gui->panel); { - /* header + menubar */ + /* header */ gui->running = !gui_panel_header(&layout, "Demo", GUI_CLOSEABLE|GUI_MINIMIZABLE, GUI_CLOSEABLE, GUI_HEADER_RIGHT); + + /* menubar */ gui_panel_menubar_begin(&layout); { - const gui_char *file_items[] = {"Open", "Close", "Quit"}; - const gui_char *edit_items[] = {"Copy", "Cut", "Delete", "Paste"}; + gui_int sel; gui_panel_row_begin(&layout, GUI_STATIC, 25, 2); gui_panel_row_push(&layout, config->font.width(config->font.userdata, "__FILE__", 8)); - gui_panel_menu(&layout, "FILE", file_items, LEN(file_items), 20, 100, + + sel = gui_panel_menu(&layout, "FILE", file_items, LEN(file_items), 25, 100, &state->file_open, gui_vec2(0,0)); + switch (sel) { + case MENU_FILE_OPEN: + fprintf(stdout, "[Menu:File] open clicked!\n"); break; + case MENU_FILE_CLOSE: + fprintf(stdout, "[Menu:File] close clicked!\n"); break; + case MENU_FILE_QUIT: + fprintf(stdout, "[Menu:File] quit clicked!\n"); break; + case GUI_NONE: + default: break; + } + gui_panel_row_push(&layout, config->font.width(config->font.userdata, "__EDIT__", 8)); - gui_panel_menu(&layout, "EDIT", edit_items, LEN(edit_items), 20, 100, + sel = gui_panel_menu(&layout, "EDIT", edit_items, LEN(edit_items), 25, 100, &state->edit_open, gui_vec2(0,0)); + switch (sel) { + case MENU_EDIT_COPY: + fprintf(stdout, "[Menu:Edit] copy clicked!\n"); break; + case MENU_EDIT_CUT: + fprintf(stdout, "[Menu:Edit] cut clicked!\n"); break; + case MENU_EDIT_DELETE: + fprintf(stdout, "[Menu:Edit] delete clicked!\n"); break; + case MENU_EDIT_PASTE: + fprintf(stdout, "[Menu:Edit] paste clicked!\n"); break; + case GUI_NONE: + default: break; + } gui_panel_row_end(&layout); } gui_panel_menubar_end(&layout); @@ -686,23 +822,23 @@ run_demo(struct demo_gui *gui) /* shelf + graphes */ gui_panel_row_dynamic(&layout, 180, 1); state->shelf_selection = gui_panel_shelf_begin(&layout, &tab, shelfs, - LEN(shelfs), state->shelf_selection, state->shelf_scrollbar); + LEN(shelfs), state->shelf_selection, state->shelf); graph_panel(&tab, state->shelf_selection); - state->shelf_scrollbar = gui_panel_shelf_end(&layout, &tab); + state->shelf = gui_panel_shelf_end(&layout, &tab); /* tables */ gui_panel_row_dynamic(&layout, 180, 1); - gui_panel_group_begin(&layout, &tab, "Table", state->table_scrollbar); + gui_panel_group_begin(&layout, &tab, "Table", state->table); table_panel(&tab); - state->table_scrollbar = gui_panel_group_end(&layout, &tab); + state->table = gui_panel_group_end(&layout, &tab); { /* tree */ struct gui_tree tree; gui_panel_row_dynamic(&layout, 250, 1); - gui_panel_tree_begin(&layout, &tree, "Tree", 20, state->tree_scrollbar); - upload_tree(&state->tree, &tree, &state->tree.root); - state->tree_scrollbar = gui_panel_tree_end(&layout, &tree); + gui_panel_tree_begin(&layout, &tree, "Tree", 20, state->tree); + upload_tree(&state->test, &tree, &state->test.root); + state->tree = gui_panel_tree_end(&layout, &tree); } } gui_panel_end(&layout, &gui->panel); @@ -710,6 +846,7 @@ run_demo(struct demo_gui *gui) /* second panel */ gui_panel_begin(&layout, &gui->sub); { + enum {EASY, HARD}; gui_panel_header(&layout, "Show", GUI_CLOSEABLE, 0, GUI_HEADER_LEFT); gui_panel_row_static(&layout, 30, 80, 1); if (gui_panel_button_text(&layout, "button", GUI_BUTTON_DEFAULT)) { diff --git a/gui.c b/gui.c index 765f5ae4..19f90e8b 100644 --- a/gui.c +++ b/gui.c @@ -1667,12 +1667,11 @@ gui_button_text(struct gui_command_buffer *o, gui_float x, gui_float y, if (!o || !b || !f) return gui_false; - /* general drawing and logic button */ + /* basic button drawing and logic */ font_color = b->content; bg_color = b->background; button_w = MAX(w, (2 * b->border + 2 * b->rounding)); button_h = MAX(h, f->height + 2 * b->padding.y); - if (i && GUI_INBOX(i->mouse_pos.x, i->mouse_pos.y, x, y, button_w, button_h)) { font_color = b->highlight_content; bg_color = b->highlight; @@ -2151,10 +2150,8 @@ gui_editbox(struct gui_command_buffer *out, gui_float x, gui_float y, gui_float begin = gui_edit_buffer_at(&box->buffer, (gui_int)min, &unicode, &l); end = gui_edit_buffer_at(&box->buffer, (gui_int)maxi, &unicode, &l); box->clip.copy(box->clip.userdata, begin, (gui_size)(end - begin)); - - /* cut text out of the editbox */ - if (gui_input_pressed(in, GUI_KEY_CUT)) { - } + if (gui_input_pressed(in, GUI_KEY_CUT)) + gui_edit_box_remove(box); } else { /* copy or cut complete buffer */ box->clip.copy(box->clip.userdata, buffer, len); @@ -2342,7 +2339,7 @@ gui_filter_binary(gui_long unicode) gui_size gui_edit_filtered(struct gui_command_buffer *out, gui_float x, gui_float y, gui_float w, - gui_float h, gui_char *buffer, gui_size len, gui_size max, gui_bool *active, + gui_float h, gui_char *buffer, gui_size len, gui_size max, gui_state *active, gui_size *cursor, const struct gui_edit *field, gui_filter filter, const struct gui_input *in, const struct gui_font *font) { @@ -2360,7 +2357,7 @@ gui_edit_filtered(struct gui_command_buffer *out, gui_float x, gui_float y, gui_ gui_size gui_edit(struct gui_command_buffer *out, gui_float x, gui_float y, gui_float w, - gui_float h, gui_char *buffer, gui_size len, gui_size max, gui_bool *active, + gui_float h, gui_char *buffer, gui_size len, gui_size max, gui_state *active, gui_size *cursor, const struct gui_edit *field, enum gui_input_filter f, const struct gui_input *in, const struct gui_font *font) { @@ -2400,7 +2397,6 @@ gui_scrollbar_vertical(struct gui_command_buffer *out, gui_float x, gui_float y, if (!out || !s) return 0; /* scrollbar background */ - scroll_w = MAX(w, 1); scroll_h = MAX(h, 2 * scroll_w); if (target <= scroll_h) return 0; @@ -2562,7 +2558,7 @@ gui_scrollbar_horizontal(struct gui_command_buffer *out, gui_float x, gui_float static gui_int gui_spinner_base(struct gui_command_buffer *out, gui_float x, gui_float y, gui_float w, gui_float h, const struct gui_spinner *s, gui_char *buffer, gui_size *len, - enum gui_input_filter filter, gui_bool *active, + enum gui_input_filter filter, gui_state *active, const struct gui_input *in, const struct gui_font *font) { gui_int ret = 0; @@ -2570,7 +2566,7 @@ gui_spinner_base(struct gui_command_buffer *out, gui_float x, gui_float y, gui_f gui_float button_x, button_y; gui_float button_w, button_h; gui_bool button_up_clicked, button_down_clicked; - gui_bool is_active = (active) ? *active : gui_false; + gui_state is_active = (active) ? *active : gui_false; struct gui_edit field; gui_float field_x, field_y; @@ -2626,13 +2622,13 @@ gui_spinner_base(struct gui_command_buffer *out, gui_float x, gui_float y, gui_f gui_int gui_spinner(struct gui_command_buffer *out, gui_float x, gui_float y, gui_float w, gui_float h, const struct gui_spinner *s, gui_int min, gui_int value, - gui_int max, gui_int step, gui_bool *active, const struct gui_input *in, + gui_int max, gui_int step, gui_state *active, const struct gui_input *in, const struct gui_font *font) { gui_int res; gui_char string[GUI_MAX_NUMBER_BUFFER]; gui_size len, old_len; - gui_bool is_active; + gui_state is_active; GUI_ASSERT(out); GUI_ASSERT(s); @@ -3118,7 +3114,7 @@ gui_panel_begin(struct gui_panel_layout *layout, struct gui_panel *panel) layout->row.height, 0, c->colors[GUI_COLOR_PANEL]); } - /* draw top header border line */ + /* draw top border line */ if (layout->flags & GUI_PANEL_BORDER) { gui_command_buffer_push_line(out, layout->x, layout->y, layout->x + layout->w, layout->y, c->colors[GUI_COLOR_BORDER]); @@ -3377,8 +3373,8 @@ gui_panel_end(struct gui_panel_layout *layout, struct gui_panel *panel) struct gui_rect clip; clip.x = MAX(0, (layout->x - 1)); clip.y = MAX(0, (layout->y - 1)); - clip.w = layout->w+1; - clip.h = layout->h+1; + clip.w = layout->w + 1; + clip.h = layout->h + 1; gui_command_buffer_push_scissor(out, clip.x, clip.y, clip.w,clip.h); } @@ -3583,7 +3579,6 @@ gui_panel_header_begin(struct gui_panel_layout *layout) layout->height -= layout->footer_h; gui_command_buffer_push_rect(out, layout->x, layout->y, layout->w, layout->header.h, 0, c->colors[GUI_COLOR_HEADER]); - } static gui_bool @@ -4295,7 +4290,7 @@ gui_panel_alloc_space(struct gui_rect *bounds, struct gui_panel_layout *layout) gui_bool gui_panel_layout_push(struct gui_panel_layout *layout, enum gui_panel_layout_node_type type, - const char *title, enum gui_node_state *state) + const char *title, gui_state *state) { const struct gui_config *config; struct gui_command_buffer *out; @@ -4466,17 +4461,17 @@ gui_panel_widget(struct gui_rect *bounds, struct gui_panel_layout *layout) GUI_ASSERT(layout->config); GUI_ASSERT(layout->buffer); - if (!layout) return GUI_INVALID; - if (!layout->valid || !layout->config || !layout->buffer) return GUI_INVALID; + if (!layout) return GUI_WIDGET_INVALID; + if (!layout->valid || !layout->config || !layout->buffer) return GUI_WIDGET_INVALID; /* allocated space for the panel and check if the widget needs to be updated */ gui_panel_alloc_space(bounds, layout); c = &layout->clip; if (!GUI_INTERSECT(c->x, c->y, c->w, c->h, bounds->x, bounds->y, bounds->w, bounds->h)) - return GUI_INVALID; + return GUI_WIDGET_INVALID; if (!GUI_CONTAINS(bounds->x, bounds->y, bounds->w, bounds->h, c->x, c->y, c->w, c->h)) - return GUI_ROM; - return GUI_VALID; + return GUI_WIDGET_ROM; + return GUI_WIDGET_VALID; } void @@ -4573,7 +4568,7 @@ gui_panel_button_text(struct gui_panel_layout *layout, const char *str, enum gui_widget_state state; state = gui_panel_button(&button, &bounds, layout); if (!state) return gui_false; - i = (state == GUI_ROM || layout->flags & GUI_PANEL_ROM) ? 0 : layout->input; + i = (state == GUI_WIDGET_ROM || layout->flags & GUI_PANEL_ROM) ? 0 : layout->input; config = layout->config; button.background = config->colors[GUI_COLOR_BUTTON]; @@ -4598,7 +4593,7 @@ gui_panel_button_fitting(struct gui_panel_layout *layout, const char *str, struct gui_vec2 padding; state = gui_panel_widget(&bounds, layout); if (!state) return gui_false; - i = (state == GUI_ROM || layout->flags & GUI_PANEL_ROM) ? 0 : layout->input; + i = (state == GUI_WIDGET_ROM || layout->flags & GUI_PANEL_ROM) ? 0 : layout->input; config = layout->config; padding = gui_config_property(config, GUI_PROPERTY_PADDING); @@ -4630,7 +4625,7 @@ gui_panel_button_color(struct gui_panel_layout *layout, enum gui_widget_state state; state = gui_panel_button(&button, &bounds, layout); if (!state) return gui_false; - i = (state == GUI_ROM || layout->flags & GUI_PANEL_ROM) ? 0 : layout->input; + i = (state == GUI_WIDGET_ROM || layout->flags & GUI_PANEL_ROM) ? 0 : layout->input; button.background = color; button.foreground = color; @@ -4653,7 +4648,7 @@ gui_panel_button_triangle(struct gui_panel_layout *layout, enum gui_heading head enum gui_widget_state state; state = gui_panel_button(&button, &bounds, layout); if (!state) return gui_false; - i = (state == GUI_ROM || layout->flags & GUI_PANEL_ROM) ? 0 : layout->input; + i = (state == GUI_WIDGET_ROM || layout->flags & GUI_PANEL_ROM) ? 0 : layout->input; config = layout->config; button.rounding = config->rounding[GUI_ROUNDING_BUTTON]; @@ -4678,7 +4673,7 @@ gui_panel_button_image(struct gui_panel_layout *layout, struct gui_image image, enum gui_widget_state state; state = gui_panel_button(&button, &bounds, layout); if (!state) return gui_false; - i = (state == GUI_ROM || layout->flags & GUI_PANEL_ROM) ? 0 : layout->input; + i = (state == GUI_WIDGET_ROM || layout->flags & GUI_PANEL_ROM) ? 0 : layout->input; config = layout->config; button.rounding = config->rounding[GUI_ROUNDING_BUTTON]; @@ -4702,7 +4697,7 @@ gui_panel_button_toggle(struct gui_panel_layout *layout, const char *str, gui_bo enum gui_widget_state state; state = gui_panel_button(&button, &bounds, layout); if (!state) return value; - i = (state == GUI_ROM || layout->flags & GUI_PANEL_ROM) ? 0 : layout->input; + i = (state == GUI_WIDGET_ROM || layout->flags & GUI_PANEL_ROM) ? 0 : layout->input; config = layout->config; button.rounding = config->rounding[GUI_ROUNDING_BUTTON]; @@ -4736,7 +4731,7 @@ gui_panel_button_text_triangle(struct gui_panel_layout *layout, enum gui_heading enum gui_widget_state state; state = gui_panel_button(&button, &bounds, layout); if (!state) return gui_false; - i = (state == GUI_ROM || layout->flags & GUI_PANEL_ROM) ? 0 : layout->input; + i = (state == GUI_WIDGET_ROM || layout->flags & GUI_PANEL_ROM) ? 0 : layout->input; config = layout->config; button.rounding = config->rounding[GUI_ROUNDING_BUTTON]; @@ -4761,7 +4756,7 @@ gui_panel_button_text_image(struct gui_panel_layout *layout, struct gui_image im enum gui_widget_state state; state = gui_panel_button(&button, &bounds, layout); if (!state) return gui_false; - i = (state == GUI_ROM || layout->flags & GUI_PANEL_ROM) ? 0 : layout->input; + i = (state == GUI_WIDGET_ROM || layout->flags & GUI_PANEL_ROM) ? 0 : layout->input; config = layout->config; button.rounding = config->rounding[GUI_ROUNDING_BUTTON]; @@ -4805,7 +4800,7 @@ gui_panel_check(struct gui_panel_layout *layout, const char *text, gui_bool is_a enum gui_widget_state state; state = gui_panel_toggle_base(&toggle, &bounds, layout); if (!state) return is_active; - i = (state == GUI_ROM || layout->flags & GUI_PANEL_ROM) ? 0 : layout->input; + i = (state == GUI_WIDGET_ROM || layout->flags & GUI_PANEL_ROM) ? 0 : layout->input; config = layout->config; toggle.rounding = config->rounding[GUI_ROUNDING_CHECK]; @@ -4827,7 +4822,7 @@ gui_panel_option(struct gui_panel_layout *layout, const char *text, gui_bool is_ enum gui_widget_state state; state = gui_panel_toggle_base(&toggle, &bounds, layout); if (!state) return is_active; - i = (state == GUI_ROM || layout->flags & GUI_PANEL_ROM) ? 0 : layout->input; + i = (state == GUI_WIDGET_ROM || layout->flags & GUI_PANEL_ROM) ? 0 : layout->input; config = layout->config; toggle.cursor = config->colors[GUI_COLOR_OPTION_ACTIVE]; @@ -4864,7 +4859,7 @@ gui_panel_slider(struct gui_panel_layout *layout, gui_float min_value, gui_float enum gui_widget_state state; state = gui_panel_widget(&bounds, layout); if (!state) return value; - i = (state == GUI_ROM || layout->flags & GUI_PANEL_ROM) ? 0 : layout->input; + i = (state == GUI_WIDGET_ROM || layout->flags & GUI_PANEL_ROM) ? 0 : layout->input; config = layout->config; item_padding = gui_config_property(config, GUI_PROPERTY_ITEM_PADDING); @@ -4891,7 +4886,7 @@ gui_panel_progress(struct gui_panel_layout *layout, gui_size cur_value, gui_size enum gui_widget_state state; state = gui_panel_widget(&bounds, layout); if (!state) return cur_value; - i = (state == GUI_ROM || layout->flags & GUI_PANEL_ROM) ? 0 : layout->input; + i = (state == GUI_WIDGET_ROM || layout->flags & GUI_PANEL_ROM) ? 0 : layout->input; config = layout->config; item_padding = gui_config_property(config, GUI_PROPERTY_ITEM_PADDING); @@ -4938,14 +4933,14 @@ gui_panel_editbox(struct gui_panel_layout *layout, struct gui_edit_box *box) state = gui_panel_edit_base(&bounds, &field, layout); if (!state) return; - i = (state == GUI_ROM || layout->flags & GUI_PANEL_ROM) ? 0 : layout->input; + i = (state == GUI_WIDGET_ROM || layout->flags & GUI_PANEL_ROM) ? 0 : layout->input; gui_editbox(layout->buffer, bounds.x, bounds.y, bounds.w, bounds.h, box, &field, i, &config->font); } gui_size gui_panel_edit(struct gui_panel_layout *layout, gui_char *buffer, gui_size len, - gui_size max, gui_bool *active, gui_size *cursor, enum gui_input_filter filter) + gui_size max, gui_state *active, gui_size *cursor, enum gui_input_filter filter) { struct gui_rect bounds; struct gui_edit field; @@ -4955,14 +4950,14 @@ gui_panel_edit(struct gui_panel_layout *layout, gui_char *buffer, gui_size len, state = gui_panel_edit_base(&bounds, &field, layout); if (!state) return len; - i = (state == GUI_ROM || layout->flags & GUI_PANEL_ROM) ? 0 : layout->input; + i = (state == GUI_WIDGET_ROM || layout->flags & GUI_PANEL_ROM) ? 0 : layout->input; return gui_edit(layout->buffer, bounds.x, bounds.y, bounds.w, bounds.h, buffer, len, max, active, cursor, &field, filter, i, &config->font); } gui_size gui_panel_edit_filtered(struct gui_panel_layout *layout, gui_char *buffer, gui_size len, - gui_size max, gui_bool *active, gui_size *cursor, gui_filter filter) + gui_size max, gui_state *active, gui_size *cursor, gui_filter filter) { struct gui_rect bounds; struct gui_edit field; @@ -4972,7 +4967,7 @@ gui_panel_edit_filtered(struct gui_panel_layout *layout, gui_char *buffer, gui_s state = gui_panel_edit_base(&bounds, &field, layout); if (!state) return len; - i = (state == GUI_ROM || layout->flags & GUI_PANEL_ROM) ? 0 : layout->input; + i = (state == GUI_WIDGET_ROM || layout->flags & GUI_PANEL_ROM) ? 0 : layout->input; return gui_edit_filtered(layout->buffer, bounds.x, bounds.y, bounds.w, bounds.h, buffer, len, max, active, cursor, &field, filter, i, &config->font); @@ -4980,7 +4975,7 @@ gui_panel_edit_filtered(struct gui_panel_layout *layout, gui_char *buffer, gui_s gui_int gui_panel_spinner(struct gui_panel_layout *layout, gui_int min, gui_int value, - gui_int max, gui_int step, gui_bool *active) + gui_int max, gui_int step, gui_state *active) { struct gui_rect bounds; struct gui_spinner spinner; @@ -4992,7 +4987,7 @@ gui_panel_spinner(struct gui_panel_layout *layout, gui_int min, gui_int value, enum gui_widget_state state; state = gui_panel_widget(&bounds, layout); if (!state) return value; - i = (state == GUI_ROM || layout->flags & GUI_PANEL_ROM) ? 0 : layout->input; + i = (state == GUI_WIDGET_ROM || layout->flags & GUI_PANEL_ROM) ? 0 : layout->input; config = layout->config; out = layout->buffer; @@ -5025,7 +5020,7 @@ gui_panel_selector(struct gui_panel_layout *layout, const char *items[], enum gui_widget_state state; state = gui_panel_widget(&bounds, layout); if (!state) return item_current; - i = (state == GUI_ROM || layout->flags & GUI_PANEL_ROM) ? 0 : layout->input; + i = (state == GUI_WIDGET_ROM || layout->flags & GUI_PANEL_ROM) ? 0 : layout->input; GUI_ASSERT(items); GUI_ASSERT(item_count); @@ -5493,7 +5488,7 @@ gui_panel_popup_end(struct gui_panel_layout *parent, struct gui_panel_layout *po static gui_bool gui_panel_popup_nonblock_begin(struct gui_panel_layout *parent, - struct gui_panel_layout *popup, gui_bool *active, gui_bool is_active, + struct gui_panel_layout *popup, gui_state *active, gui_state is_active, struct gui_rect body, struct gui_vec2 offset) { /* deactivate popup if user clicked outside the popup*/ @@ -5550,7 +5545,7 @@ gui_panel_popup_nonblock_end(struct gui_panel_layout *parent, struct gui_panel_l */ void gui_panel_combo_begin(struct gui_panel_layout *parent, struct gui_panel_layout *combo, - const char *selected, gui_bool *active, struct gui_vec2 offset) + const char *selected, gui_state *active, struct gui_vec2 offset) { const struct gui_input *in; const struct gui_config *config; @@ -5558,7 +5553,7 @@ gui_panel_combo_begin(struct gui_panel_layout *parent, struct gui_panel_layout * struct gui_vec2 item_padding; struct gui_rect header; gui_float button_w; - gui_bool is_active; + gui_state is_active; GUI_ASSERT(parent); GUI_ASSERT(combo); @@ -5672,7 +5667,7 @@ gui_panel_combo_close(struct gui_panel_layout *combo) void gui_panel_combo(struct gui_panel_layout *layout, const char **entries, gui_size count, gui_size *current, gui_size row_height, - gui_bool *active, struct gui_vec2 scrollbar) + gui_state *active, struct gui_vec2 *scrollbar) { gui_size i; struct gui_panel_layout combo; @@ -5684,7 +5679,7 @@ gui_panel_combo(struct gui_panel_layout *layout, const char **entries, if (!count) return; gui_zero(&combo, sizeof(combo)); - gui_panel_combo_begin(layout, &combo, entries[*current], active, scrollbar); + gui_panel_combo_begin(layout, &combo, entries[*current], active, *scrollbar); gui_panel_row_dynamic(&combo, (gui_float)row_height, 1); for (i = 0; i < count; ++i) { if (i == *current) continue; @@ -5694,7 +5689,7 @@ gui_panel_combo(struct gui_panel_layout *layout, const char **entries, *current = i; } } - gui_panel_combo_end(layout, &combo); + *scrollbar = gui_panel_combo_end(layout, &combo); } /* * ------------------------------------------------------------- @@ -5705,12 +5700,12 @@ gui_panel_combo(struct gui_panel_layout *layout, const char **entries, */ void gui_panel_menu_begin(struct gui_panel_layout *parent, struct gui_panel_layout *menu, - const char *title, gui_float width, gui_bool *active, struct gui_vec2 offset) + const char *title, gui_float width, gui_state *active, struct gui_vec2 offset) { const struct gui_input *in; const struct gui_config *config; struct gui_rect header; - gui_bool is_active; + gui_state is_active; GUI_ASSERT(parent); GUI_ASSERT(menu); @@ -5783,7 +5778,7 @@ gui_panel_menu_end(struct gui_panel_layout *parent, struct gui_panel_layout *men gui_int gui_panel_menu(struct gui_panel_layout *layout, const char *title, const char **entries, gui_size count, gui_size row_height, - gui_float width, gui_bool *active, struct gui_vec2 scrollbar) + gui_float width, gui_state *active, struct gui_vec2 scrollbar) { gui_size i; gui_int sel = -1; @@ -5818,7 +5813,7 @@ gui_panel_menu(struct gui_panel_layout *layout, const char *title, */ void gui_panel_tree_begin(struct gui_panel_layout *p, struct gui_tree *tree, - const char *title, gui_float height, struct gui_vec2 offset) + const char *title, gui_float height, struct gui_vec2 offset) { struct gui_vec2 padding; const struct gui_config *config; @@ -5841,7 +5836,7 @@ gui_panel_tree_begin(struct gui_panel_layout *p, struct gui_tree *tree, static enum gui_tree_node_operation gui_panel_tree_node(struct gui_tree *tree, enum gui_tree_node_symbol symbol, - const char *title, struct gui_image *img, gui_tree_node_state *state) + const char *title, struct gui_image *img, gui_state *state) { struct gui_text text; struct gui_rect bounds = {0,0,0,0}; @@ -5956,7 +5951,7 @@ gui_panel_tree_node(struct gui_tree *tree, enum gui_tree_node_symbol symbol, enum gui_tree_node_operation gui_panel_tree_begin_node(struct gui_tree *tree, const char *title, - gui_tree_node_state *state) + gui_state *state) { enum gui_tree_node_operation op; GUI_ASSERT(tree); @@ -5975,7 +5970,7 @@ gui_panel_tree_begin_node(struct gui_tree *tree, const char *title, enum gui_tree_node_operation gui_panel_tree_begin_node_icon(struct gui_tree *tree, const char *title, - struct gui_image img, gui_tree_node_state *state) + struct gui_image img, gui_state *state) { enum gui_tree_node_operation op; GUI_ASSERT(tree); @@ -5993,13 +5988,12 @@ gui_panel_tree_begin_node_icon(struct gui_tree *tree, const char *title, } enum gui_tree_node_operation -gui_panel_tree_leaf(struct gui_tree *tree, const char *title, - gui_tree_node_state *state) +gui_panel_tree_leaf(struct gui_tree *tree, const char *title, gui_state *state) {return gui_panel_tree_node(tree, GUI_TREE_NODE_BULLET, title, 0, state);} enum gui_tree_node_operation gui_panel_tree_leaf_icon(struct gui_tree *tree, const char *title, struct gui_image img, - gui_tree_node_state *state) + gui_state *state) {return gui_panel_tree_node(tree, GUI_TREE_NODE_BULLET, title, &img, state);} void @@ -6289,7 +6283,7 @@ gui_panel_shelf_end(struct gui_panel_layout *p, struct gui_panel_layout *s) * =============================================================== */ void gui_layout_begin(struct gui_layout *layout, struct gui_rect bounds, - enum gui_layout_state state) + gui_state state) { GUI_ASSERT(layout); if (!layout) return; @@ -6387,7 +6381,7 @@ gui_layout_set_pos(struct gui_layout *layout, gui_size x, gui_size y) } void -gui_layout_set_state(struct gui_layout *layout, enum gui_layout_state state) +gui_layout_set_state(struct gui_layout *layout, gui_state state) { GUI_ASSERT(layout); if (!layout) return; diff --git a/gui.h b/gui.h index 6c1a8ae0..bb95b389 100644 --- a/gui.h +++ b/gui.h @@ -35,14 +35,12 @@ extern "C" { #define GUI_MAX_ATTRIB_STACK 32 /* defines the number of temporary configuration attribute changes that can be stored */ -/* -Since the gui uses ANSI C which does not guarantee to have fixed types, you need -to set the appropriate size of each type. However if your developer environment -supports fixed size types by the header you can just uncomment the define -to automatically set the correct size for each type in the library: -*/ -#define GUI_USE_FIXED_TYPES -#ifdef GUI_USE_FIXED_TYPES +/* Compiler switches */ +#define GUI_COMPILE_WITH_FIXED_TYPES 1 +/* setting this define to 1 adds the header for fixed sized types + * if 0 each type has to be set to the correct size*/ + +#if GUI_COMPILE_WITH_FIXED_TYPES #include typedef char gui_char; typedef int32_t gui_int; @@ -55,6 +53,7 @@ typedef uint16_t gui_ushort; typedef uint32_t gui_uint; typedef uint64_t gui_ulong; typedef uint32_t gui_flags; +typedef gui_flags gui_state; typedef uint8_t gui_byte; typedef uint32_t gui_flag; typedef uint64_t gui_size; @@ -71,6 +70,7 @@ typedef unsigned short gui_ushort; typedef unsigned int gui_uint; typedef unsigned long gui_ulong; typedef unsigned int gui_flags; +typedef gui_flags gui_state; typedef unsigned char gui_byte; typedef unsigned int gui_flag; typedef unsigned long gui_size; @@ -88,6 +88,8 @@ struct gui_key {gui_bool down, clicked;}; typedef gui_char gui_glyph[GUI_UTF_SIZE]; typedef union {void *ptr; gui_int id;} gui_handle; struct gui_image {gui_handle handle; struct gui_rect region;}; +enum gui_widget_states {GUI_INACTIVE = gui_false, GUI_AYOUT_ACTIVE = gui_true}; +enum gui_collapse_states {GUI_MINIMIZED = gui_false, GUI_MAXIMIZED = gui_true}; /* Callbacks */ struct gui_font; @@ -96,7 +98,6 @@ typedef gui_bool(*gui_filter)(gui_long unicode); typedef gui_size(*gui_text_width_f)(gui_handle, const gui_char*, gui_size); typedef void(*gui_paste_f)(gui_handle, struct gui_edit_box*); typedef void(*gui_copy_f)(gui_handle, const char*, gui_size size); - /* * ============================================================== * @@ -849,7 +850,7 @@ typedef struct gui_buffer gui_edit_buffer; struct gui_edit_box { gui_edit_buffer buffer; /* glyph buffer to add text into */ - gui_bool active; + gui_state active; /* flag indicating if the buffer is currently being modified */ gui_size cursor; /* current glyph (not byte) cursor position */ @@ -1354,7 +1355,7 @@ void gui_editbox(struct gui_command_buffer*, gui_float x, gui_float y, gui_float - font structure for text drawing */ gui_size gui_edit(struct gui_command_buffer*, gui_float x, gui_float y, gui_float w, - gui_float h, gui_char*, gui_size, gui_size max, gui_bool*, + gui_float h, gui_char*, gui_size, gui_size max, gui_state*, gui_size *cursor, const struct gui_edit*, enum gui_input_filter filter, const struct gui_input*, const struct gui_font*); /* this function executes a editbox widget @@ -1376,7 +1377,7 @@ gui_size gui_edit(struct gui_command_buffer*, gui_float x, gui_float y, gui_floa */ gui_size gui_edit_filtered(struct gui_command_buffer*, gui_float x, gui_float y, gui_float w, gui_float h, gui_char*, gui_size, - gui_size max, gui_bool*, gui_size *cursor, + gui_size max, gui_state*, gui_size *cursor, const struct gui_edit*, gui_filter filter, const struct gui_input*, const struct gui_font*); /* this function executes a editbox widget @@ -1398,7 +1399,7 @@ gui_size gui_edit_filtered(struct gui_command_buffer*, gui_float x, gui_float y, */ gui_int gui_spinner(struct gui_command_buffer*, gui_float x, gui_float y, gui_float w, gui_float h, const struct gui_spinner*, gui_int min, gui_int value, - gui_int max, gui_int step, gui_bool *active, + gui_int max, gui_int step, gui_state *active, const struct gui_input*, const struct gui_font*); /* this function executes a integer spinner widget Input: @@ -1746,9 +1747,9 @@ void gui_config_reset(struct gui_config*); */ enum gui_widget_state { - GUI_INVALID, /* The widget cannot be seen and is completly out of view */ - GUI_VALID, /* The widget is completly inside the panel and can be updated + drawn */ - GUI_ROM /* The widget is partially visible and cannot be updated */ + GUI_WIDGET_INVALID, /* The widget cannot be seen and is completly out of view */ + GUI_WIDGET_VALID, /* The widget is completly inside the panel and can be updated + drawn */ + GUI_WIDGET_ROM /* The widget is partially visible and cannot be updated */ }; enum gui_panel_flags { @@ -1822,11 +1823,6 @@ enum gui_panel_row_layout_type { /* retain mode widget specific widget pixel width layout */ }; -enum gui_node_state { - GUI_MINIMIZED = gui_false, - GUI_MAXIMIZED = gui_true -}; - enum gui_panel_layout_node_type { GUI_LAYOUT_NODE, /* a node is a space which can be minimized or maximized */ @@ -2228,7 +2224,7 @@ void gui_panel_row_space_push(struct gui_panel_layout*, struct gui_rect); void gui_panel_row_space_end(struct gui_panel_layout*); /* this functions finishes the scaleable space filling process */ gui_bool gui_panel_layout_push(struct gui_panel_layout*, enum gui_panel_layout_node_type, - const char *title, enum gui_node_state*); + const char *title, gui_state*); /* this functions pushes either a tree node, collapseable header or tab into * the current panel layout Input: @@ -2476,7 +2472,7 @@ gui_size gui_panel_progress(struct gui_panel_layout*, gui_size cur, gui_size max void gui_panel_editbox(struct gui_panel_layout*, struct gui_edit_box*); /* this function creates an editbox with copy & paste functionality and text buffering */ gui_size gui_panel_edit(struct gui_panel_layout*, gui_char *buffer, gui_size len, - gui_size max, gui_bool *active, gui_size *cursor, + gui_size max, gui_state *active, gui_size *cursor, enum gui_input_filter); /* this function creates an editbox to updated/insert user text input Input: @@ -2490,7 +2486,7 @@ gui_size gui_panel_edit(struct gui_panel_layout*, gui_char *buffer, gui_size len - current state of the editbox with active(gui_true) or inactive(gui_false) */ gui_size gui_panel_edit_filtered(struct gui_panel_layout*, gui_char *buffer, - gui_size len, gui_size max, gui_bool *active, + gui_size len, gui_size max, gui_state *active, gui_size *cursor, gui_filter); /* this function creates an editbox to updated/insert filtered user text input Input: @@ -2504,7 +2500,7 @@ gui_size gui_panel_edit_filtered(struct gui_panel_layout*, gui_char *buffer, - current state of the editbox with active(gui_true) or inactive(gui_false) */ gui_int gui_panel_spinner(struct gui_panel_layout*, gui_int min, gui_int value, - gui_int max, gui_int step, gui_bool *active); + gui_int max, gui_int step, gui_state *active); /* this function creates a integer spinner widget Input: - min value that will not be underflown @@ -2550,9 +2546,8 @@ gui_size gui_panel_selector(struct gui_panel_layout*, const char *items[], gui_panel_group_begin -- adds a scrollable fixed space inside the panel gui_panel_group_end -- ends the scrollable space */ - void gui_panel_group_begin(struct gui_panel_layout*, struct gui_panel_layout *tab, - const char *title, struct gui_vec2 offset); + const char *title, struct gui_vec2); /* this function adds a grouped subpanel into the parent panel IMPORTANT: You need to set the height of the group with panel_row_layout Input: @@ -2636,8 +2631,10 @@ enum gui_popup_type { GUI_POPUP_DYNAMIC /* dynamically growing popup with maximum height */ }; -gui_flags gui_panel_popup_begin(struct gui_panel_layout *parent, struct gui_panel_layout *popup, - enum gui_popup_type, struct gui_rect bounds, struct gui_vec2 offset); +gui_flags gui_panel_popup_begin(struct gui_panel_layout *parent, + struct gui_panel_layout *popup, + enum gui_popup_type, struct gui_rect bounds, + struct gui_vec2 offset); /* this function adds a grouped subpanel into the parent panel Input: - popup position and size of the popup (NOTE: local position) @@ -2766,7 +2763,7 @@ gui_int gui_panel_graph_callback(struct gui_panel_layout*, enum gui_graph_type, */ void gui_panel_combo(struct gui_panel_layout*, const char **entries, gui_size count, gui_size *current, gui_size row_height, - gui_bool *active, struct gui_vec2 scrollbar); + gui_state *active, struct gui_vec2 *scrollbar); /* this function creates a standart text based combobox Input: - parent panel layout the combo box will be placed into @@ -2782,7 +2779,7 @@ void gui_panel_combo(struct gui_panel_layout*, const char **entries, */ void gui_panel_combo_begin(struct gui_panel_layout *parent, struct gui_panel_layout *combo, const char *selected, - gui_bool *active, struct gui_vec2 offset); + gui_state *active, struct gui_vec2 offset); /* this function begins the combobox build up process Input: - parent panel layout the combo box will be placed into @@ -2812,9 +2809,10 @@ struct gui_vec2 gui_panel_combo_end(struct gui_panel_layout *parent, gui_panel_menu_end -- ends the menu item build up process gui_panel_menu -- shorthand retain mode array version */ +#define GUI_NONE (-1) gui_int gui_panel_menu(struct gui_panel_layout*, const gui_char *title, const char **entries, gui_size count, gui_size row_height, - gui_float width, gui_bool *active, struct gui_vec2 scrollbar); + gui_float width, gui_state *active, struct gui_vec2 scrollbar); /* this function creates a standart text based combobox Input: - parent panel layout the combo box will be placed into @@ -2829,7 +2827,7 @@ gui_int gui_panel_menu(struct gui_panel_layout*, const gui_char *title, */ void gui_panel_menu_begin(struct gui_panel_layout *parent, struct gui_panel_layout *menu, const char *title, - gui_float width, gui_bool *active, struct gui_vec2 offset); + gui_float width, gui_state *active, struct gui_vec2 offset); /* this function begins the menu build up process Input: - parent panel layout the menu will be placed into @@ -2841,7 +2839,7 @@ void gui_panel_menu_begin(struct gui_panel_layout *parent, void gui_panel_menu_close(struct gui_panel_layout *menu); /* this function closes a opened menu */ struct gui_vec2 gui_panel_menu_end(struct gui_panel_layout *parent, - struct gui_panel_layout *menu); + struct gui_panel_layout *menu); /* this function ends the menu build up process */ /* * -------------------------------------------------------------- @@ -2869,7 +2867,6 @@ struct gui_vec2 gui_panel_menu_end(struct gui_panel_layout *parent, gui_panel_tree_leaf_icon -- adds a leaf icon node to a prev opended node gui_panel_tree_end -- ends the tree build up process */ -typedef gui_flags gui_tree_node_state; enum gui_tree_nodes_states { GUI_NODE_ACTIVE = 0x01, /* the node is currently opened */ @@ -2902,7 +2899,8 @@ struct gui_tree { }; void gui_panel_tree_begin(struct gui_panel_layout*, struct gui_tree*, - const char*, gui_float row_height, struct gui_vec2 offset); + const char*, gui_float row_height, + struct gui_vec2 scrollbar); /* this function begins the tree building process Input: - title describing the tree or NULL @@ -2912,7 +2910,7 @@ void gui_panel_tree_begin(struct gui_panel_layout*, struct gui_tree*, - tree build up state structure */ enum gui_tree_node_operation gui_panel_tree_begin_node(struct gui_tree*, const char*, - gui_tree_node_state*); + gui_state*); /* this function begins a parent node Input: - title of the node @@ -2922,7 +2920,7 @@ enum gui_tree_node_operation gui_panel_tree_begin_node(struct gui_tree*, const c */ enum gui_tree_node_operation gui_panel_tree_begin_node_icon(struct gui_tree*, const char*, struct gui_image, - gui_tree_node_state*); + gui_state*); /* this function begins a text icon parent node Input: - title of the node @@ -2934,7 +2932,7 @@ enum gui_tree_node_operation gui_panel_tree_begin_node_icon(struct gui_tree*, void gui_panel_tree_end_node(struct gui_tree*); /* this function ends a parent node */ enum gui_tree_node_operation gui_panel_tree_leaf(struct gui_tree*, const char*, - gui_tree_node_state*); + gui_state*); /* this function pushes a leaf node to the tree Input: - title of the node @@ -2944,7 +2942,7 @@ enum gui_tree_node_operation gui_panel_tree_leaf(struct gui_tree*, const char*, */ enum gui_tree_node_operation gui_panel_tree_leaf_icon(struct gui_tree*, const char*, struct gui_image, - gui_tree_node_state*); + gui_state*); /* this function pushes a leaf icon node to the tree Input: - title of the node @@ -3084,19 +3082,12 @@ struct gui_layout_slot { /* scaleable state */ }; -enum gui_layout_state { - GUI_LAYOUT_INACTIVE = gui_false, - /* all panels inside the layout can NOT be modified by user input */ - GUI_LAYOUT_ACTIVE = gui_true - /* all panels inside the layout can be modified by user input */ -}; - struct gui_layout { gui_float scaler_width; /* width of the scaling line between slots */ struct gui_rect bounds; /* bounds of the layout inside the window */ - enum gui_layout_state active; + gui_state active; /* flag indicating if the layout is from the user modifyable */ struct gui_layout_slot slots[GUI_SLOT_MAX]; /* each slot inside the panel layout */ @@ -3111,7 +3102,7 @@ void gui_panel_begin_tiled(struct gui_panel_layout*, struct gui_panel*, - input structure holding all user generated state changes */ void gui_layout_begin(struct gui_layout*, struct gui_rect bounds, - enum gui_layout_state); + gui_state); /* this function start the definition of the layout slots Input: - position (width/height) of the layout in the window @@ -3158,7 +3149,7 @@ void gui_layout_set_pos(struct gui_layout*, gui_size x, gui_size y); Input: - position (x/y) of the layout in the window */ -void gui_layout_set_state(struct gui_layout*, enum gui_layout_state); +void gui_layout_set_state(struct gui_layout*, gui_state); /* this function changes the user modifiable layout state Input: - new state of the layout with either active or inactive