diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index cf9f0d10..0585aefa 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -9,6 +9,7 @@ If you have an idea for new features just [open an issue](https://github.com/vur * Add (insert your favorite platform or render backend here) demo implementation (some possibilities: DirectX 9/DirectX 10/DirectX 11 and win32 with OpenGL) * Add clipboard user callbacks back into all demos * Add additional widgets + * Add color picker widget (using the multi-color draw command) * Find a better way to change the style or group style options * Add support for multiple pointers for touch input devices (probably requires to rewrite mouse handling in `struct zr_input`) * Extend xlib demo to support image drawing with arbitrary image width and height @@ -18,8 +19,8 @@ If you have an idea for new features just [open an issue](https://github.com/vur * Add tables with scaleable column width * Rewrite the chart API to support a better range of charts (maybe take notes from Javascript chart frameworks) * Create an API to allow scaling between groups (maybe extend and convert the demo example) - * Add multiple Tab support (maybe use `zr_group` and add a header) * Come up with a better way to provide and create widget and window styles + * Add multiple Tab support (maybe use `zr_group` and add a header) * Extend context to not only support overlapping windows but tiled windows as well ## Bugs diff --git a/demo/allegro5_complex/allegro.c b/demo/allegro5_complex/allegro.c index bc4ec8c9..75a585e6 100644 --- a/demo/allegro5_complex/allegro.c +++ b/demo/allegro5_complex/allegro.c @@ -355,6 +355,7 @@ main(int argc, char *argv[]) usrfnt = device_init(&dev, font_path, 14, zr_font_default_glyph_ranges()); + memset(&gui, 0, sizeof(gui)); zr_init(&gui.ctx, &alloc, &usrfnt); } diff --git a/demo/demo.c b/demo/demo.c index 365b8751..29af801f 100644 --- a/demo/demo.c +++ b/demo/demo.c @@ -60,6 +60,11 @@ zr_labelf(struct zr_context *ctx, enum zr_text_align align, const char *fmt, ... va_end(args); } +/* =============================================================== + * + * CONTROL WINDOW + * + * ===============================================================*/ static void set_style(struct zr_context *ctx, enum theme theme) { @@ -348,104 +353,1975 @@ control_window(struct zr_context *ctx, struct demo *gui) return !zr_window_is_closed(ctx, "Control"); } +/* =============================================================== + * + * SIMPLE WINDOW + * + * ===============================================================*/ +static void +simple_window(struct zr_context *ctx) +{ + /* simple demo window */ + struct zr_panel layout; + if (zr_begin(ctx, &layout, "Show", zr_rect(420, 350, 200, 200), + ZR_WINDOW_BORDER|ZR_WINDOW_MOVABLE|ZR_WINDOW_SCALABLE| + ZR_WINDOW_CLOSABLE|ZR_WINDOW_MINIMIZABLE|ZR_WINDOW_TITLE)) + { + enum {EASY, HARD}; + static int op = EASY; + static int property = 20; -#if 0 -#include "simple.c" -#include "overview.c" -#include "extended.c" -#include "nodedit.c" -#endif + zr_layout_row_static(ctx, 30, 80, 1); + if (zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT)) { + /* event handling */ + } + zr_layout_row_dynamic(ctx, 30, 2); + if (zr_option(ctx, "easy", op == EASY)) op = EASY; + if (zr_option(ctx, "hard", op == HARD)) op = HARD; + + zr_layout_row_dynamic(ctx, 22, 1); + zr_property_int(ctx, "Compression:", 0, &property, 100, 10, 1); + } + zr_end(ctx); +} + +/* =============================================================== + * + * DEMO WINDOW + * + * ===============================================================*/ +static void +demo_window(struct zr_context *ctx) +{ + struct zr_panel menu; + + /* window flags */ + static int show_menu = zr_true; + static int titlebar = zr_true; + static int border = zr_true; + static int resize = zr_true; + static int moveable = zr_true; + static int no_scrollbar = zr_false; + static zr_flags window_flags = 0; + static int minimizable = zr_true; + static int close = zr_true; + + /* popups */ + static enum zr_style_header_align header_align = ZR_HEADER_RIGHT; + static int show_app_about = zr_false; + struct zr_panel layout; + + /* window flags */ + window_flags = 0; + ctx->style.header.align = header_align; + if (border) window_flags |= ZR_WINDOW_BORDER; + if (resize) window_flags |= ZR_WINDOW_SCALABLE; + if (moveable) window_flags |= ZR_WINDOW_MOVABLE; + if (no_scrollbar) window_flags |= ZR_WINDOW_NO_SCROLLBAR; + if (minimizable) window_flags |= ZR_WINDOW_MINIMIZABLE; + if (close) window_flags |= ZR_WINDOW_CLOSABLE; + + if (zr_begin(ctx, &layout, "Demo", zr_rect(10, 10, 400, 750), window_flags)) + { + if (show_menu) + { + /* menubar */ + enum menu_states {MENU_DEFAULT, MENU_TEST}; + static zr_size mprog = 60; + static int mslider = 10; + static int mcheck = zr_true; + + zr_menubar_begin(ctx); + zr_layout_row_begin(ctx, ZR_STATIC, 25, 2); + zr_layout_row_push(ctx, 45); + if (zr_menu_text_begin(ctx, &menu, "MENU", 120)) + { + static size_t prog = 40; + static int slider = 10; + static int check = zr_true; + + zr_layout_row_dynamic(ctx, 25, 1); + zr_progress(ctx, &prog, 100, ZR_MODIFIABLE); + zr_slider_int(ctx, 0, &slider, 16, 1); + zr_checkbox(ctx, "check", &check); + + if (zr_menu_item(ctx, ZR_TEXT_CENTERED, "Hide")) + show_menu = zr_false; + if (zr_menu_item(ctx, ZR_TEXT_CENTERED, "About")) + show_app_about = zr_true; + zr_menu_end(ctx); + } + zr_layout_row_push(ctx, 60); + zr_progress(ctx, &mprog, 100, ZR_MODIFIABLE); + zr_slider_int(ctx, 0, &mslider, 16, 1); + zr_checkbox(ctx, "check", &mcheck); + zr_menubar_end(ctx); + } + + if (show_app_about) + { + /* about popup */ + struct zr_panel popup; + static struct zr_rect s = {20, 100, 300, 190}; + if (zr_popup_begin(ctx, &popup, ZR_POPUP_STATIC, "About", ZR_WINDOW_CLOSABLE, s)) + { + zr_layout_row_dynamic(ctx, 20, 1); + zr_label(ctx, "Zahnrad", ZR_TEXT_LEFT); + zr_label(ctx, "By Micha Mettke", ZR_TEXT_LEFT); + zr_label(ctx, "Zahnrad is licensed under the zlib License.", ZR_TEXT_LEFT); + zr_label(ctx, "See LICENSE for more information", ZR_TEXT_LEFT); + zr_popup_end(ctx); + } else show_app_about = zr_false; + } + + /* window flags */ + if (zr_layout_push(ctx, ZR_LAYOUT_TAB, "Window", ZR_MINIMIZED)) { + zr_layout_row_dynamic(ctx, 30, 2); + zr_checkbox(ctx, "Titlebar", &titlebar); + zr_checkbox(ctx, "Menu", &show_menu); + zr_checkbox(ctx, "Border", &border); + zr_checkbox(ctx, "Resizable", &resize); + zr_checkbox(ctx, "Moveable", &moveable); + zr_checkbox(ctx, "No Scrollbar", &no_scrollbar); + zr_checkbox(ctx, "Minimizable", &minimizable); + zr_checkbox(ctx, "Closeable", &close); + zr_layout_pop(ctx); + } + + if (zr_layout_push(ctx, ZR_LAYOUT_TAB, "Widgets", ZR_MINIMIZED)) + { + enum options {A,B,C}; + static int checkbox; + static int option; + if (zr_layout_push(ctx, ZR_LAYOUT_NODE, "Text", ZR_MINIMIZED)) + { + /* Text Widgets */ + zr_layout_row_dynamic(ctx, 20, 1); + zr_label(ctx, "Label aligned left", ZR_TEXT_LEFT); + zr_label(ctx, "Label aligned centered", ZR_TEXT_CENTERED); + zr_label(ctx, "Label aligned right", ZR_TEXT_RIGHT); + zr_label_colored(ctx, "Blue text", ZR_TEXT_LEFT, zr_rgb(0,0,255)); + zr_label_colored(ctx, "Yellow text", ZR_TEXT_LEFT, zr_rgb(255,255,0)); + zr_text(ctx, "Text without /0", 15, ZR_TEXT_RIGHT); + + zr_layout_row_static(ctx, 100, 200, 1); + zr_label_wrap(ctx, "This is a very long line to hopefully get this text to be wrapped into multiple lines to show line wrapping"); + zr_layout_row_dynamic(ctx, 100, 1); + zr_label_wrap(ctx, "This is another long text to show dynamic window changes on multiline text"); + zr_layout_pop(ctx); + } + + if (zr_layout_push(ctx, ZR_LAYOUT_NODE, "Button", ZR_MINIMIZED)) + { + /* Buttons Widgets */ + zr_layout_row_static(ctx, 30, 100, 3); + if (zr_button_text(ctx, "Button", ZR_BUTTON_DEFAULT)) + fprintf(stdout, "Button pressed!\n"); + if (zr_button_text(ctx, "Repeater", ZR_BUTTON_REPEATER)) + fprintf(stdout, "Repeater is being pressed!\n"); + zr_button_color(ctx, zr_rgb(0,0,255), ZR_BUTTON_DEFAULT); + + zr_layout_row_static(ctx, 20, 20, 8); + zr_button_symbol(ctx, ZR_SYMBOL_CIRCLE, ZR_BUTTON_DEFAULT); + zr_button_symbol(ctx, ZR_SYMBOL_CIRCLE_FILLED, ZR_BUTTON_DEFAULT); + zr_button_symbol(ctx, ZR_SYMBOL_RECT, ZR_BUTTON_DEFAULT); + zr_button_symbol(ctx, ZR_SYMBOL_RECT_FILLED, ZR_BUTTON_DEFAULT); + zr_button_symbol(ctx, ZR_SYMBOL_TRIANGLE_UP, ZR_BUTTON_DEFAULT); + zr_button_symbol(ctx, ZR_SYMBOL_TRIANGLE_DOWN, ZR_BUTTON_DEFAULT); + zr_button_symbol(ctx, ZR_SYMBOL_TRIANGLE_LEFT, ZR_BUTTON_DEFAULT); + zr_button_symbol(ctx, ZR_SYMBOL_TRIANGLE_RIGHT, ZR_BUTTON_DEFAULT); + + zr_layout_row_static(ctx, 30, 100, 2); + zr_button_text_symbol(ctx, ZR_SYMBOL_TRIANGLE_LEFT, "prev", ZR_TEXT_RIGHT, ZR_BUTTON_DEFAULT); + zr_button_text_symbol(ctx, ZR_SYMBOL_TRIANGLE_RIGHT, "next", ZR_TEXT_LEFT, ZR_BUTTON_DEFAULT); + zr_layout_pop(ctx); + } + + if (zr_layout_push(ctx, ZR_LAYOUT_NODE, "Basic", ZR_MINIMIZED)) + { + /* Basic widgets */ + static int int_slider = 5; + static float float_slider = 2.5f; + static size_t prog_value = 40; + static float property_float = 2; + static int property_int = 10; + static int property_neg = 10; + + static float range_float_min = 0; + static float range_float_max = 100; + static float range_float_value = 50; + static int range_int_min = 0; + static int range_int_value = 2048; + static int range_int_max = 4096; + static const float ratio[] = {120, 150}; + + zr_layout_row_static(ctx, 30, 100, 1); + zr_checkbox(ctx, "Checkbox", &checkbox); + + zr_layout_row_static(ctx, 30, 80, 3); + option = zr_option(ctx, "optionA", option == A) ? A : option; + option = zr_option(ctx, "optionB", option == B) ? B : option; + option = zr_option(ctx, "optionC", option == C) ? C : option; + + + zr_layout_row(ctx, ZR_STATIC, 30, 2, ratio); + zr_labelf(ctx, ZR_TEXT_LEFT, "Slider int"); + zr_slider_int(ctx, 0, &int_slider, 10, 1); + + zr_label(ctx, "Slider float", ZR_TEXT_LEFT); + zr_slider_float(ctx, 0, &float_slider, 5.0, 0.5f); + zr_labelf(ctx, ZR_TEXT_LEFT, "Progressbar" , prog_value); + zr_progress(ctx, &prog_value, 100, ZR_MODIFIABLE); + + zr_layout_row(ctx, ZR_STATIC, 25, 2, ratio); + zr_label(ctx, "Property float:", ZR_TEXT_LEFT); + zr_property_float(ctx, "Float:", 0, &property_float, 64.0f, 0.1f, 0.2f); + zr_label(ctx, "Property int:", ZR_TEXT_LEFT); + zr_property_int(ctx, "Int:", 0, &property_int, 100.0f, 1, 1); + zr_label(ctx, "Property neg:", ZR_TEXT_LEFT); + zr_property_int(ctx, "Neg:", -10, &property_neg, 10, 1, 1); + + + zr_layout_row_dynamic(ctx, 25, 1); + zr_label(ctx, "Range:", ZR_TEXT_LEFT); + zr_layout_row_dynamic(ctx, 25, 3); + zr_property_float(ctx, "#min:", 0, &range_float_min, range_float_max, 1.0f, 0.2f); + zr_property_float(ctx, "#float:", range_float_min, &range_float_value, range_float_max, 1.0f, 0.2f); + zr_property_float(ctx, "#max:", range_float_min, &range_float_max, 100, 1.0f, 0.2f); + + zr_property_int(ctx, "#min:", INT_MIN, &range_int_min, range_int_max, 1, 10); + zr_property_int(ctx, "#neg:", range_int_min, &range_int_value, range_int_max, 1, 10); + zr_property_int(ctx, "#max:", range_int_min, &range_int_max, INT_MAX, 1, 10); + + zr_layout_pop(ctx); + } + + if (zr_layout_push(ctx, ZR_LAYOUT_NODE, "Selectable", ZR_MINIMIZED)) + { + if (zr_layout_push(ctx, ZR_LAYOUT_NODE, "List", ZR_MINIMIZED)) + { + static int selected[4] = {zr_false, zr_false, zr_true, zr_false}; + zr_layout_row_static(ctx, 18, 100, 1); + zr_selectable(ctx, "Selectable", ZR_TEXT_LEFT, &selected[0]); + zr_selectable(ctx, "Selectable", ZR_TEXT_LEFT, &selected[1]); + zr_label(ctx, "Not Selectable", ZR_TEXT_LEFT); + zr_selectable(ctx, "Selectable", ZR_TEXT_LEFT, &selected[2]); + zr_selectable(ctx, "Selectable", ZR_TEXT_LEFT, &selected[3]); + zr_layout_pop(ctx); + } + if (zr_layout_push(ctx, ZR_LAYOUT_NODE, "Grid", ZR_MINIMIZED)) + { + int i; + static int selected[16] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1}; + zr_layout_row_static(ctx, 50, 50, 4); + for (i = 0; i < 16; ++i) { + if (zr_selectable(ctx, "Z", ZR_TEXT_CENTERED, &selected[i])) { + int x = (i % 4), y = i / 4; + if (x > 0) selected[i - 1] ^= 1; + if (x < 3) selected[i + 1] ^= 1; + if (y > 0) selected[i - 4] ^= 1; + if (y < 3) selected[i + 4] ^= 1; + } + } + zr_layout_pop(ctx); + } + zr_layout_pop(ctx); + } + + if (zr_layout_push(ctx, ZR_LAYOUT_NODE, "Combo", ZR_MINIMIZED)) + { + /* Combobox Widgets */ + enum color_mode {COL_RGB, COL_HSV}; + static float chart_selection = 8.0f; + static const char *weapons[] = {"Fist","Pistol","Shotgun","Plasma","BFG"}; + static size_t current_weapon = 0; + static int check_values[5]; + static float position[3]; + static int col_mode = COL_RGB; + static struct zr_color combo_color = {130, 50, 50, 255}; + static struct zr_color combo_color2 = {130, 180, 50, 255}; + static size_t x = 20, y = 40, z = 10, w = 90; + + char buffer[64]; + size_t sum = 0; + struct zr_panel combo; + + /* default combobox */ + zr_layout_row_static(ctx, 25, 200, 1); + if (zr_combo_begin_text(ctx, &combo, weapons[current_weapon], 200)) { + size_t i = 0; + zr_layout_row_dynamic(ctx, 25, 1); + for (i = 0; i < LEN(weapons); ++i) { + if (zr_combo_item(ctx, weapons[i], ZR_TEXT_LEFT)) + current_weapon = i; + } + zr_combo_end(ctx); + } + + /* slider color combobox */ + if (zr_combo_begin_color(ctx, &combo, combo_color, 200)) { + float ratios[] = {0.15f, 0.85f}; + zr_layout_row(ctx, ZR_DYNAMIC, 30, 2, ratios); + zr_label(ctx, "R:", ZR_TEXT_LEFT); + combo_color.r = (zr_byte)zr_slide_int(ctx, 0, combo_color.r, 255, 5); + zr_label(ctx, "G:", ZR_TEXT_LEFT); + combo_color.g = (zr_byte)zr_slide_int(ctx, 0, combo_color.g, 255, 5); + zr_label(ctx, "B:", ZR_TEXT_LEFT); + combo_color.b = (zr_byte)zr_slide_int(ctx, 0, combo_color.b, 255, 5); + zr_label(ctx, "A:", ZR_TEXT_LEFT); + combo_color.a = (zr_byte)zr_slide_int(ctx, 0, combo_color.a , 255, 5); + zr_combo_end(ctx); + } + + /* property color combobox */ + if (zr_combo_begin_color(ctx, &combo, combo_color2, 200)) { + zr_layout_row_dynamic(ctx, 25, 2); + col_mode = zr_option(ctx, "RGB", col_mode == COL_RGB) ? COL_RGB : col_mode; + col_mode = zr_option(ctx, "HSV", col_mode == COL_HSV) ? COL_HSV : col_mode; + zr_layout_row_dynamic(ctx, 25, 1); + if (col_mode == COL_RGB) { + combo_color2.r = (zr_byte)zr_propertyi(ctx, "#R:", 0, combo_color2.r, 255, 1,1); + combo_color2.g = (zr_byte)zr_propertyi(ctx, "#G:", 0, combo_color2.g, 255, 1,1); + combo_color2.b = (zr_byte)zr_propertyi(ctx, "#B:", 0, combo_color2.b, 255, 1,1); + combo_color2.a = (zr_byte)zr_propertyi(ctx, "#A:", 0, combo_color2.a, 255, 1,1); + } else { + zr_byte tmp[4]; + zr_color_hsva_bv(tmp, combo_color2); + tmp[0] = (zr_byte)zr_propertyi(ctx, "#H:", 0, tmp[0], 255, 1,1); + tmp[1] = (zr_byte)zr_propertyi(ctx, "#S:", 0, tmp[1], 255, 1,1); + tmp[2] = (zr_byte)zr_propertyi(ctx, "#V:", 0, tmp[2], 255, 1,1); + tmp[3] = (zr_byte)zr_propertyi(ctx, "#A:", 0, tmp[3], 255, 1,1); + combo_color2 = zr_hsva_bv(tmp); + } + zr_combo_end(ctx); + } + + /* progressbar combobox */ + sum = x + y + z + w; + sprintf(buffer, "%lu", sum); + if (zr_combo_begin_text(ctx, &combo, buffer, 200)) { + zr_layout_row_dynamic(ctx, 30, 1); + zr_progress(ctx, &x, 100, ZR_MODIFIABLE); + zr_progress(ctx, &y, 100, ZR_MODIFIABLE); + zr_progress(ctx, &z, 100, ZR_MODIFIABLE); + zr_progress(ctx, &w, 100, ZR_MODIFIABLE); + zr_combo_end(ctx); + } + + /* checkbox combobox */ + sum = (size_t)(check_values[0] + check_values[1] + check_values[2] + check_values[3] + check_values[4]); + sprintf(buffer, "%lu", sum); + if (zr_combo_begin_text(ctx, &combo, buffer, 200)) { + zr_layout_row_dynamic(ctx, 30, 1); + zr_checkbox(ctx, weapons[0], &check_values[0]); + zr_checkbox(ctx, weapons[1], &check_values[1]); + zr_checkbox(ctx, weapons[2], &check_values[2]); + zr_checkbox(ctx, weapons[3], &check_values[3]); + zr_combo_end(ctx); + } + + /* complex text combobox */ + sprintf(buffer, "%.2f, %.2f, %.2f", position[0], position[1],position[2]); + if (zr_combo_begin_text(ctx, &combo, buffer, 200)) { + zr_layout_row_dynamic(ctx, 25, 1); + zr_property_float(ctx, "#X:", -1024.0f, &position[0], 1024.0f, 1,0.5f); + zr_property_float(ctx, "#Y:", -1024.0f, &position[1], 1024.0f, 1,0.5f); + zr_property_float(ctx, "#Z:", -1024.0f, &position[2], 1024.0f, 1,0.5f); + zr_combo_end(ctx); + } + + /* chart combobox */ + sprintf(buffer, "%.1f", chart_selection); + if (zr_combo_begin_text(ctx, &combo, buffer, 250)) { + size_t i = 0; + static const float values[]={30.0f,15.0f,25.0f,10.0f,20.0f,40.0f}; + zr_layout_row_dynamic(ctx, 150, 1); + zr_chart_begin(ctx, ZR_CHART_COLUMN, LEN(values), 0, 50); + for (i = 0; i < LEN(values); ++i) { + zr_flags res = zr_chart_push(ctx, values[i]); + if (res & ZR_CHART_CLICKED) { + chart_selection = values[i]; + zr_combo_close(ctx); + } + } + zr_chart_end(ctx); + zr_combo_end(ctx); + } + zr_layout_pop(ctx); + } + + if (zr_layout_push(ctx, ZR_LAYOUT_NODE, "Input", ZR_MINIMIZED)) + { + static const float ratio[] = {120, 150}; + static char field_buffer[64]; + static char box_buffer[512]; + static char text[9][64]; + static size_t text_len[9]; + static size_t field_len; + static size_t box_len; + zr_flags active; + + zr_layout_row(ctx, ZR_STATIC, 25, 2, ratio); + zr_label(ctx, "Default:", ZR_TEXT_LEFT); + + zr_edit_string(ctx, ZR_EDIT_CURSOR, text[0], &text_len[0], 64, zr_filter_default); + zr_label(ctx, "Int:", ZR_TEXT_LEFT); + zr_edit_string(ctx, ZR_EDIT_CURSOR, text[1], &text_len[1], 64, zr_filter_decimal); + zr_label(ctx, "Float:", ZR_TEXT_LEFT); + zr_edit_string(ctx, ZR_EDIT_CURSOR, text[2], &text_len[2], 64, zr_filter_float); + zr_label(ctx, "Hex:", ZR_TEXT_LEFT); + zr_edit_string(ctx, ZR_EDIT_CURSOR, text[4], &text_len[4], 64, zr_filter_hex); + zr_label(ctx, "Octal:", ZR_TEXT_LEFT); + zr_edit_string(ctx, ZR_EDIT_CURSOR, text[5], &text_len[5], 64, zr_filter_oct); + zr_label(ctx, "Binary:", ZR_TEXT_LEFT); + zr_edit_string(ctx, ZR_EDIT_CURSOR, text[6], &text_len[6], 64, zr_filter_binary); + + zr_label(ctx, "Password:", ZR_TEXT_LEFT); + { + size_t i = 0; + size_t old_len = text_len[8]; + char buffer[64]; + for (i = 0; i < text_len[8]; ++i) buffer[i] = '*'; + zr_edit_string(ctx, ZR_EDIT_SIMPLE, buffer, &text_len[8], 64, zr_filter_default); + if (old_len < text_len[8]) + memcpy(&text[8][old_len], &buffer[old_len], text_len[8] - old_len); + } + + zr_label(ctx, "Field:", ZR_TEXT_LEFT); + zr_edit_string(ctx, ZR_EDIT_FIELD, field_buffer, &field_len, 64, zr_filter_default); + + zr_label(ctx, "Box:", ZR_TEXT_LEFT); + zr_layout_row_static(ctx, 180, 278, 1); + zr_edit_string(ctx, ZR_EDIT_BOX, box_buffer, &box_len, 512, zr_filter_default); + + zr_layout_row(ctx, ZR_STATIC, 25, 2, ratio); + active = zr_edit_string(ctx, ZR_EDIT_FIELD|ZR_EDIT_SIGCOMIT, text[7], &text_len[7], 64, zr_filter_ascii); + if (zr_button_text(ctx, "Submit", ZR_BUTTON_DEFAULT) || + (active & ZR_EDIT_COMMITED)) + { + text[7][text_len[7]] = '\n'; + text_len[7]++; + memcpy(&box_buffer[box_len], &text[7], text_len[7]); + box_len += text_len[7]; + text_len[7] = 0; + } + zr_layout_row_end(ctx); + zr_layout_pop(ctx); + } + zr_layout_pop(ctx); + } + + if (zr_layout_push(ctx, ZR_LAYOUT_TAB, "Chart", ZR_MINIMIZED)) + { + float id = 0; + static int col_index = -1; + static int line_index = -1; + float step = (2*3.141592654f) / 32; + + int i; + int index = -1; + struct zr_rect bounds; + + /* line chart */ + id = 0; + index = -1; + zr_layout_row_dynamic(ctx, 100, 1); + bounds = zr_widget_bounds(ctx); + zr_chart_begin(ctx, ZR_CHART_LINES, 32, -1.0f, 1.0f); + for (i = 0; i < 32; ++i) { + zr_flags res = zr_chart_push(ctx, (float)cos(id)); + if (res & ZR_CHART_HOVERING) + index = (int)i; + if (res & ZR_CHART_CLICKED) + line_index = (int)i; + id += step; + } + zr_chart_end(ctx); + + if (index != -1) { + char buffer[ZR_MAX_NUMBER_BUFFER]; + float val = (float)cos((float)index*step); + sprintf(buffer, "Value: %.2f", val); + zr_tooltip(ctx, buffer); + } + if (line_index != -1) { + zr_layout_row_dynamic(ctx, 20, 1); + zr_labelf(ctx, ZR_TEXT_LEFT, "Selected value: %.2f", (float)cos((float)index*step)); + } + + /* column chart */ + zr_layout_row_dynamic(ctx, 100, 1); + bounds = zr_widget_bounds(ctx); + zr_chart_begin(ctx, ZR_CHART_COLUMN, 32, 0.0f, 1.0f); + for (i = 0; i < 32; ++i) { + zr_flags res = zr_chart_push(ctx, (float)fabs(sin(id))); + if (res & ZR_CHART_HOVERING) + index = (int)i; + if (res & ZR_CHART_CLICKED) + col_index = (int)i; + id += step; + } + zr_chart_end(ctx); + + if (index != -1) { + char buffer[ZR_MAX_NUMBER_BUFFER]; + sprintf(buffer, "Value: %.2f", (float)fabs(sin(step * (float)index))); + zr_tooltip(ctx, buffer); + } + if (col_index != -1) { + zr_layout_row_dynamic(ctx, 20, 1); + zr_labelf(ctx, ZR_TEXT_LEFT, "Selected value: %.2f", (float)fabs(sin(step * (float)col_index))); + } + zr_layout_pop(ctx); + } + + if (zr_layout_push(ctx, ZR_LAYOUT_TAB, "Popup", ZR_MINIMIZED)) + { + static struct zr_color color = {255,0,0, 255}; + static int select[4]; + static int popup_active; + const struct zr_input *in = &ctx->input; + struct zr_rect bounds; + + /* menu contextual */ + zr_layout_row_static(ctx, 30, 150, 1); + bounds = zr_widget_bounds(ctx); + zr_label(ctx, "Right click me for menu", ZR_TEXT_LEFT); + + if (zr_contextual_begin(ctx, &menu, 0, zr_vec2(100, 300), bounds)) { + static size_t prog = 40; + static int slider = 10; + + zr_layout_row_dynamic(ctx, 25, 1); + zr_checkbox(ctx, "Menu", &show_menu); + zr_progress(ctx, &prog, 100, ZR_MODIFIABLE); + zr_slider_int(ctx, 0, &slider, 16, 1); + if (zr_contextual_item(ctx, "About", ZR_TEXT_CENTERED)) + show_app_about = zr_true; + zr_selectable(ctx, select[0]?"Unselect":"Select", ZR_TEXT_LEFT, &select[0]); + zr_selectable(ctx, select[1]?"Unselect":"Select", ZR_TEXT_LEFT, &select[1]); + zr_selectable(ctx, select[2]?"Unselect":"Select", ZR_TEXT_LEFT, &select[2]); + zr_selectable(ctx, select[3]?"Unselect":"Select", ZR_TEXT_LEFT, &select[3]); + zr_contextual_end(ctx); + } + + /* color contextual */ + zr_layout_row_begin(ctx, ZR_STATIC, 30, 2); + zr_layout_row_push(ctx, 100); + zr_label(ctx, "Right Click here:", ZR_TEXT_LEFT); + zr_layout_row_push(ctx, 50); + bounds = zr_widget_bounds(ctx); + zr_button_color(ctx, color, ZR_BUTTON_DEFAULT); + zr_layout_row_end(ctx); + + if (zr_contextual_begin(ctx, &menu, 0, zr_vec2(350, 60), bounds)) { + zr_layout_row_dynamic(ctx, 30, 4); + color.r = (zr_byte)zr_propertyi(ctx, "#r", 0, color.r, 255, 1, 1); + color.g = (zr_byte)zr_propertyi(ctx, "#g", 0, color.g, 255, 1, 1); + color.b = (zr_byte)zr_propertyi(ctx, "#b", 0, color.b, 255, 1, 1); + color.a = (zr_byte)zr_propertyi(ctx, "#a", 0, color.a, 255, 1, 1); + zr_contextual_end(ctx); + } + + /* popup */ + zr_layout_row_begin(ctx, ZR_STATIC, 30, 2); + zr_layout_row_push(ctx, 100); + zr_label(ctx, "Popup:", ZR_TEXT_LEFT); + zr_layout_row_push(ctx, 50); + if (zr_button_text(ctx, "Popup", ZR_BUTTON_DEFAULT)) + popup_active = 1; + zr_layout_row_end(ctx); + + if (popup_active) + { + static struct zr_rect s = {20, 100, 220, 150}; + if (zr_popup_begin(ctx, &menu, ZR_POPUP_STATIC, "Error", ZR_WINDOW_DYNAMIC, s)) + { + zr_layout_row_dynamic(ctx, 25, 1); + zr_label(ctx, "A terrible error as occured", ZR_TEXT_LEFT); + zr_layout_row_dynamic(ctx, 25, 2); + if (zr_button_text(ctx, "OK", ZR_BUTTON_DEFAULT)) { + popup_active = 0; + zr_popup_close(ctx); + } + if (zr_button_text(ctx, "Cancel", ZR_BUTTON_DEFAULT)) { + popup_active = 0; + zr_popup_close(ctx); + } + zr_popup_end(ctx); + } else popup_active = zr_false; + } + + /* tooltip */ + zr_layout_row_static(ctx, 30, 150, 1); + bounds = zr_widget_bounds(ctx); + zr_label(ctx, "Hover me for tooltip", ZR_TEXT_LEFT); + if (zr_input_is_mouse_hovering_rect(in, bounds)) + zr_tooltip(ctx, "This is a tooltip"); + + zr_layout_pop(ctx); + } + + if (zr_layout_push(ctx, ZR_LAYOUT_TAB, "Layout", ZR_MINIMIZED)) + { + if (zr_layout_push(ctx, ZR_LAYOUT_NODE, "Widget", ZR_MINIMIZED)) + { + float ratio_two[] = {0.2f, 0.6f, 0.2f}; + float width_two[] = {100, 200, 50}; + + zr_layout_row_dynamic(ctx, 30, 1); + zr_label(ctx, "Dynamic fixed column layout with generated position and size:", ZR_TEXT_LEFT); + zr_layout_row_dynamic(ctx, 30, 3); + zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT); + + zr_layout_row_dynamic(ctx, 30, 1); + zr_label(ctx, "static fixed column layout with generated position and size:", ZR_TEXT_LEFT); + zr_layout_row_static(ctx, 30, 100, 3); + zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT); + + zr_layout_row_dynamic(ctx, 30, 1); + zr_label(ctx, "Dynamic array-based custom column layout with generated position and custom size:",ZR_TEXT_LEFT); + zr_layout_row(ctx, ZR_DYNAMIC, 30, 3, ratio_two); + zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT); + + zr_layout_row_dynamic(ctx, 30, 1); + zr_label(ctx, "Static array-based custom column layout with generated position and custom size:",ZR_TEXT_LEFT ); + zr_layout_row(ctx, ZR_STATIC, 30, 3, width_two); + zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT); + + zr_layout_row_dynamic(ctx, 30, 1); + zr_label(ctx, "Dynamic immediate mode custom column layout with generated position and custom size:",ZR_TEXT_LEFT); + zr_layout_row_begin(ctx, ZR_DYNAMIC, 30, 3); + zr_layout_row_push(ctx, 0.2f); + zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT); + zr_layout_row_push(ctx, 0.6f); + zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT); + zr_layout_row_push(ctx, 0.2f); + zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT); + zr_layout_row_end(ctx); + + zr_layout_row_dynamic(ctx, 30, 1); + zr_label(ctx, "Static immmediate mode custom column layout with generated position and custom size:", ZR_TEXT_LEFT); + zr_layout_row_begin(ctx, ZR_STATIC, 30, 3); + zr_layout_row_push(ctx, 100); + zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT); + zr_layout_row_push(ctx, 200); + zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT); + zr_layout_row_push(ctx, 50); + zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT); + zr_layout_row_end(ctx); + + zr_layout_row_dynamic(ctx, 30, 1); + zr_label(ctx, "Static free space with custom position and custom size:", ZR_TEXT_LEFT); + zr_layout_space_begin(ctx, ZR_STATIC, 120, 4); + zr_layout_space_push(ctx, zr_rect(100, 0, 100, 30)); + zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT); + zr_layout_space_push(ctx, zr_rect(0, 15, 100, 30)); + zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT); + zr_layout_space_push(ctx, zr_rect(200, 15, 100, 30)); + zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT); + zr_layout_space_push(ctx, zr_rect(100, 30, 100, 30)); + zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT); + zr_layout_space_end(ctx); + zr_layout_pop(ctx); + } + + if (zr_layout_push(ctx, ZR_LAYOUT_NODE, "Group", ZR_MINIMIZED)) + { + static int group_titlebar = zr_false; + static int group_border = zr_true; + static int group_no_scrollbar = zr_false; + static int group_width = 320; + static int group_height = 200; + struct zr_panel tab; + + zr_flags group_flags = 0; + if (group_border) group_flags |= ZR_WINDOW_BORDER; + if (group_no_scrollbar) group_flags |= ZR_WINDOW_NO_SCROLLBAR; + if (group_titlebar) group_flags |= ZR_WINDOW_TITLE; + + zr_layout_row_dynamic(ctx, 30, 3); + zr_checkbox(ctx, "Titlebar", &group_titlebar); + zr_checkbox(ctx, "Border", &group_border); + zr_checkbox(ctx, "No Scrollbar", &group_no_scrollbar); + + zr_layout_row_begin(ctx, ZR_STATIC, 22, 2); + zr_layout_row_push(ctx, 50); + zr_label(ctx, "size:", ZR_TEXT_LEFT); + zr_layout_row_push(ctx, 130); + zr_property_int(ctx, "#Width:", 100, &group_width, 500, 10, 1); + zr_layout_row_push(ctx, 130); + zr_property_int(ctx, "#Height:", 100, &group_height, 500, 10, 1); + zr_layout_row_end(ctx); + + zr_layout_row_static(ctx, (float)group_height, group_width, 2); + if (zr_group_begin(ctx, &tab, "Group", group_flags)) { + int i = 0; + static int selected[16]; + zr_layout_row_static(ctx, 18, 100, 1); + for (i = 0; i < 16; ++i) + zr_selectable(ctx, (selected[i]) ? "Selected": "Unselected", ZR_TEXT_CENTERED, &selected[i]); + zr_group_end(ctx); + } + zr_layout_pop(ctx); + } + + if (zr_layout_push(ctx, ZR_LAYOUT_NODE, "Simple", ZR_MINIMIZED)) + { + struct zr_panel tab; + zr_layout_row_dynamic(ctx, 300, 2); + if (zr_group_begin(ctx, &tab, "Group_Without_Border", 0)) { + int i = 0; + char buffer[64]; + zr_layout_row_static(ctx, 18, 150, 1); + for (i = 0; i < 64; ++i) { + sprintf(buffer, "0x%02x", i); + zr_labelf(ctx, ZR_TEXT_LEFT, "%s: scrollable region", buffer); + } + zr_group_end(ctx); + } + if (zr_group_begin(ctx, &tab, "Group_With_Border", ZR_WINDOW_BORDER)) { + int i = 0; + char buffer[64]; + zr_layout_row_dynamic(ctx, 25, 2); + for (i = 0; i < 64; ++i) { + sprintf(buffer, "%08d", ((((i%7)*10)^32))+(64+(i%2)*2)); + zr_button_text(ctx, buffer, ZR_BUTTON_DEFAULT); + } + zr_group_end(ctx); + } + zr_layout_pop(ctx); + } + + if (zr_layout_push(ctx, ZR_LAYOUT_NODE, "Complex", ZR_MINIMIZED)) + { + int i; + struct zr_panel tab; + zr_layout_space_begin(ctx, ZR_STATIC, 500, 64); + zr_layout_space_push(ctx, zr_rect(0,0,150,500)); + if (zr_group_begin(ctx, &tab, "Group_left", ZR_WINDOW_BORDER)) { + static int selected[32]; + zr_layout_row_static(ctx, 18, 100, 1); + for (i = 0; i < 32; ++i) + zr_selectable(ctx, (selected[i]) ? "Selected": "Unselected", ZR_TEXT_CENTERED, &selected[i]); + zr_group_end(ctx); + } + + zr_layout_space_push(ctx, zr_rect(160,0,150,240)); + if (zr_group_begin(ctx, &tab, "Group_top", ZR_WINDOW_BORDER)) { + zr_layout_row_dynamic(ctx, 25, 1); + zr_button_text(ctx, "#FFAA", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "#FFBB", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "#FFCC", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "#FFDD", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "#FFEE", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "#FFFF", ZR_BUTTON_DEFAULT); + zr_group_end(ctx); + } + + zr_layout_space_push(ctx, zr_rect(160,250,150,250)); + if (zr_group_begin(ctx, &tab, "Group_buttom", ZR_WINDOW_BORDER)) { + zr_layout_row_dynamic(ctx, 25, 1); + zr_button_text(ctx, "#FFAA", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "#FFBB", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "#FFCC", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "#FFDD", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "#FFEE", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "#FFFF", ZR_BUTTON_DEFAULT); + zr_group_end(ctx); + } + + zr_layout_space_push(ctx, zr_rect(320,0,150,150)); + if (zr_group_begin(ctx, &tab, "Group_right_top", ZR_WINDOW_BORDER)) { + static int selected[4]; + zr_layout_row_static(ctx, 18, 100, 1); + for (i = 0; i < 4; ++i) + zr_selectable(ctx, (selected[i]) ? "Selected": "Unselected", ZR_TEXT_CENTERED, &selected[i]); + zr_group_end(ctx); + } + + zr_layout_space_push(ctx, zr_rect(320,160,150,150)); + if (zr_group_begin(ctx, &tab, "Group_right_center", ZR_WINDOW_BORDER)) { + static int selected[4]; + zr_layout_row_static(ctx, 18, 100, 1); + for (i = 0; i < 4; ++i) + zr_selectable(ctx, (selected[i]) ? "Selected": "Unselected", ZR_TEXT_CENTERED, &selected[i]); + zr_group_end(ctx); + } + + zr_layout_space_push(ctx, zr_rect(320,320,150,150)); + if (zr_group_begin(ctx, &tab, "Group_right_bottom", ZR_WINDOW_BORDER)) { + static int selected[4]; + zr_layout_row_static(ctx, 18, 100, 1); + for (i = 0; i < 4; ++i) + zr_selectable(ctx, (selected[i]) ? "Selected": "Unselected", ZR_TEXT_CENTERED, &selected[i]); + zr_group_end(ctx); + } + zr_layout_space_end(ctx); + zr_layout_pop(ctx); + } + + if (zr_layout_push(ctx, ZR_LAYOUT_NODE, "Splitter", ZR_MINIMIZED)) + { + const struct zr_input *in = &ctx->input; + zr_layout_row_static(ctx, 20, 320, 1); + zr_label(ctx, "Use slider and spinner to change tile size", ZR_TEXT_LEFT); + zr_label(ctx, "Drag the space between tiles to change tile ratio", ZR_TEXT_LEFT); + + if (zr_layout_push(ctx, ZR_LAYOUT_NODE, "Vertical", ZR_MINIMIZED)) + { + static float a = 100, b = 100, c = 100; + struct zr_rect bounds; + struct zr_panel sub; + + float row_layout[5]; + row_layout[0] = a; + row_layout[1] = 8; + row_layout[2] = b; + row_layout[3] = 8; + row_layout[4] = c; + + /* header */ + zr_layout_row_static(ctx, 30, 100, 2); + zr_label(ctx, "left:", ZR_TEXT_LEFT); + zr_slider_float(ctx, 10.0f, &a, 200.0f, 10.0f); + + zr_label(ctx, "middle:", ZR_TEXT_LEFT); + zr_slider_float(ctx, 10.0f, &b, 200.0f, 10.0f); + + zr_label(ctx, "right:", ZR_TEXT_LEFT); + zr_slider_float(ctx, 10.0f, &c, 200.0f, 10.0f); + + /* tiles */ + zr_layout_row(ctx, ZR_STATIC, 200, 5, row_layout); + zr_push_property(ctx, ZR_PROPERTY_ITEM_SPACING, zr_vec2(0, 4)); + + /* left space */ + if (zr_group_begin(ctx, &sub, "left", ZR_WINDOW_NO_SCROLLBAR|ZR_WINDOW_BORDER|ZR_WINDOW_NO_SCROLLBAR)) { + zr_layout_row_dynamic(ctx, 25, 1); + zr_button_text(ctx, "#FFAA", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "#FFBB", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "#FFCC", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "#FFDD", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "#FFEE", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "#FFFF", ZR_BUTTON_DEFAULT); + zr_group_end(ctx); + } + + /* scaler */ + bounds = zr_widget_bounds(ctx); + zr_spacing(ctx, 1); + if ((zr_input_is_mouse_hovering_rect(in, bounds) || + zr_input_is_mouse_prev_hovering_rect(in, bounds)) && + zr_input_is_mouse_down(in, ZR_BUTTON_LEFT)) + { + a = row_layout[0] + in->mouse.delta.x; + b = row_layout[2] - in->mouse.delta.x; + } + + /* middle space */ + if (zr_group_begin(ctx, &sub, "center", ZR_WINDOW_BORDER|ZR_WINDOW_NO_SCROLLBAR)) { + zr_layout_row_dynamic(ctx, 25, 1); + zr_button_text(ctx, "#FFAA", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "#FFBB", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "#FFCC", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "#FFDD", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "#FFEE", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "#FFFF", ZR_BUTTON_DEFAULT); + zr_group_end(ctx); + } + + /* scaler */ + bounds = zr_widget_bounds(ctx); + zr_spacing(ctx, 1); + if ((zr_input_is_mouse_hovering_rect(in, bounds) || + zr_input_is_mouse_prev_hovering_rect(in, bounds)) && + zr_input_is_mouse_down(in, ZR_BUTTON_LEFT)) + { + b = (row_layout[2] + in->mouse.delta.x); + c = (row_layout[4] - in->mouse.delta.x); + } + + /* right space */ + if (zr_group_begin(ctx, &sub, "right", ZR_WINDOW_BORDER|ZR_WINDOW_NO_SCROLLBAR)) { + zr_layout_row_dynamic(ctx, 25, 1); + zr_button_text(ctx, "#FFAA", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "#FFBB", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "#FFCC", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "#FFDD", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "#FFEE", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "#FFFF", ZR_BUTTON_DEFAULT); + zr_group_end(ctx); + } + + zr_pop_property(ctx); + zr_layout_pop(ctx); + } + + if (zr_layout_push(ctx, ZR_LAYOUT_NODE, "Horizontal", ZR_MINIMIZED)) + { + static float a = 100, b = 100, c = 100; + struct zr_panel sub; + struct zr_rect bounds; + + /* header */ + zr_layout_row_static(ctx, 30, 100, 2); + zr_label(ctx, "top:", ZR_TEXT_LEFT); + zr_slider_float(ctx, 10.0f, &a, 200.0f, 10.0f); + + zr_label(ctx, "middle:", ZR_TEXT_LEFT); + zr_slider_float(ctx, 10.0f, &b, 200.0f, 10.0f); + + zr_label(ctx, "bottom:", ZR_TEXT_LEFT); + zr_slider_float(ctx, 10.0f, &c, 200.0f, 10.0f); + + /* top space */ + zr_layout_row_dynamic(ctx, a, 1); + if (zr_group_begin(ctx, &sub, "top", ZR_WINDOW_NO_SCROLLBAR|ZR_WINDOW_BORDER)) { + zr_layout_row_dynamic(ctx, 25, 3); + zr_button_text(ctx, "#FFAA", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "#FFBB", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "#FFCC", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "#FFDD", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "#FFEE", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "#FFFF", ZR_BUTTON_DEFAULT); + zr_group_end(ctx); + } + + /* scaler */ + zr_layout_row_dynamic(ctx, 8, 1); + bounds = zr_widget_bounds(ctx); + zr_spacing(ctx, 1); + if ((zr_input_is_mouse_hovering_rect(in, bounds) || + zr_input_is_mouse_prev_hovering_rect(in, bounds)) && + zr_input_is_mouse_down(in, ZR_BUTTON_LEFT)) + { + a = a + in->mouse.delta.y; + b = b - in->mouse.delta.y; + } + + /* middle space */ + zr_layout_row_dynamic(ctx, b, 1); + if (zr_group_begin(ctx, &sub, "middle", ZR_WINDOW_NO_SCROLLBAR|ZR_WINDOW_BORDER)) { + zr_layout_row_dynamic(ctx, 25, 3); + zr_button_text(ctx, "#FFAA", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "#FFBB", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "#FFCC", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "#FFDD", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "#FFEE", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "#FFFF", ZR_BUTTON_DEFAULT); + zr_group_end(ctx); + } + + { + /* scaler */ + zr_layout_row_dynamic(ctx, 8, 1); + bounds = zr_widget_bounds(ctx); + if ((zr_input_is_mouse_hovering_rect(in, bounds) || + zr_input_is_mouse_prev_hovering_rect(in, bounds)) && + zr_input_is_mouse_down(in, ZR_BUTTON_LEFT)) + { + b = b + in->mouse.delta.y; + c = c - in->mouse.delta.y; + } + } + + /* bottom space */ + zr_layout_row_dynamic(ctx, c, 1); + if (zr_group_begin(ctx, &sub, "bottom", ZR_WINDOW_NO_SCROLLBAR|ZR_WINDOW_BORDER)) { + zr_layout_row_dynamic(ctx, 25, 3); + zr_button_text(ctx, "#FFAA", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "#FFBB", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "#FFCC", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "#FFDD", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "#FFEE", ZR_BUTTON_DEFAULT); + zr_button_text(ctx, "#FFFF", ZR_BUTTON_DEFAULT); + zr_group_end(ctx); + } + zr_layout_pop(ctx); + } + zr_layout_pop(ctx); + } + zr_layout_pop(ctx); + } + } + zr_end(ctx); +} + +/* =============================================================== + * + * CUSTOM WIDGET + * + * ===============================================================*/ +static int +ui_piemenu(struct zr_context *ctx, struct zr_vec2 pos, float radius, + struct zr_image *icons, int item_count) +{ + int ret = -1; + struct zr_rect total_space; + struct zr_panel popup; + struct zr_rect bounds; + int active_item = 0; + + /* hide popup background */ + struct zr_color border; + zr_push_color(ctx, ZR_COLOR_WINDOW, zr_rgba(0,0,0,0)); + border = ctx->style.colors[ZR_COLOR_BORDER]; + zr_push_color(ctx, ZR_COLOR_BORDER, zr_rgba(0,0,0,0)); + + /* pie menu popup */ + total_space = zr_window_get_content_region(ctx); + zr_popup_begin(ctx, &popup, ZR_POPUP_STATIC, "piemenu", ZR_WINDOW_NO_SCROLLBAR, + zr_rect(pos.x - total_space.x - radius, pos.y - radius - total_space.y, + 2*radius,2*radius)); + + total_space = zr_window_get_content_region(ctx); + zr_layout_row_dynamic(ctx, total_space.h, 1); + { + int i = 0; + struct zr_command_buffer* out = zr_window_get_canvas(ctx); + const struct zr_input *in = &ctx->input; + { + /* allocate complete popup space for the menu */ + enum zr_widget_state state; + total_space = zr_window_get_content_region(ctx); + total_space.x = total_space.y = 0; + state = zr_widget(&bounds, ctx); + } + + /* outer circle */ + zr_draw_circle(out, bounds, zr_rgb(50,50,50)); + { + /* circle buttons */ + float step = (2 * 3.141592654f) / (float)(MAX(1,item_count)); + float a_min = 0; float a_max = step; + + struct zr_vec2 center = zr_vec2(bounds.x + bounds.w / 2.0f, bounds.y + bounds.h / 2.0f); + struct zr_vec2 drag = zr_vec2(in->mouse.pos.x - center.x, in->mouse.pos.y - center.y); + float angle = (float)atan2(drag.y, drag.x); + if (angle < -0.0f) angle += 2.0f * 3.141592654f; + active_item = (int)(angle/step); + + for (i = 0; i < item_count; ++i) { + struct zr_rect content; + float rx, ry, dx, dy, a; + zr_draw_arc(out, center.x, center.y, (bounds.w/2.0f), + a_min, a_max, (active_item == i) ? zr_rgb(45,100,255): zr_rgb(60,60,60)); + + /* seperator line */ + rx = bounds.w/2.0f; ry = 0; + dx = rx * (float)cos(a_min) - ry * (float)sin(a_min); + dy = rx * (float)sin(a_min) + ry * (float)cos(a_min); + zr_draw_line(out, center.x, center.y, + center.x + dx, center.y + dy, zr_rgb(50,50,50)); + + /* button content */ + a = a_min + (a_max - a_min)/2.0f; + rx = bounds.w/2.5f; ry = 0; + content.w = 30; content.h = 30; + content.x = center.x + ((rx * (float)cos(a) - ry * (float)sin(a)) - content.w/2.0f); + content.y = center.y + (rx * (float)sin(a) + ry * (float)cos(a) - content.h/2.0f); + zr_draw_image(out, content, &icons[i]); + a_min = a_max; a_max += step; + } + } + { + /* inner circle */ + struct zr_rect inner; + inner.x = bounds.x + bounds.w/2 - bounds.w/4; + inner.y = bounds.y + bounds.h/2 - bounds.h/4; + inner.w = bounds.w/2; inner.h = bounds.h/2; + zr_draw_circle(out, inner, zr_rgb(45,45,45)); + + /* active icon content */ + bounds.w = inner.w / 2.0f; + bounds.h = inner.h / 2.0f; + bounds.x = inner.x + inner.w/2 - bounds.w/2; + bounds.y = inner.y + inner.h/2 - bounds.h/2; + zr_draw_image(out, bounds, &icons[active_item]); + } + } + zr_layout_space_end(ctx); + zr_popup_end(ctx); + + zr_reset_colors(ctx); + zr_reset_properties(ctx); + + if (!zr_input_is_mouse_down(&ctx->input, ZR_BUTTON_RIGHT)) + return active_item; + else return ret; +} + +/* =============================================================== + * + * GRID + * + * ===============================================================*/ +static void +grid_demo(struct zr_context *ctx) +{ + static char text[3][64]; + static size_t text_len[3]; + static const char *items[] = {"Item 0","item 1","item 2"}; + static int selected_item = 0; + static int check = 1; + struct zr_panel layout; + + int i; + struct zr_panel combo; + ctx->style.font.height = 20; + if (zr_begin(ctx, &layout, "Grid Demo", zr_rect(600, 350, 275, 250), + ZR_WINDOW_TITLE|ZR_WINDOW_BORDER|ZR_WINDOW_MOVABLE| + ZR_WINDOW_BORDER_HEADER|ZR_WINDOW_NO_SCROLLBAR)) + { + ctx->style.font.height = 18; + zr_layout_row_dynamic(ctx, 30, 2); + zr_label(ctx, "Floating point:", ZR_TEXT_RIGHT); + zr_edit_string(ctx, ZR_EDIT_FIELD, text[0], &text_len[0], 64, zr_filter_float); + zr_label(ctx, "Hexadeximal:", ZR_TEXT_RIGHT); + zr_edit_string(ctx, ZR_EDIT_FIELD, text[1], &text_len[1], 64, zr_filter_hex); + zr_label(ctx, "Binary:", ZR_TEXT_RIGHT); + zr_edit_string(ctx, ZR_EDIT_FIELD, text[2], &text_len[2], 64, zr_filter_binary); + zr_label(ctx, "Checkbox:", ZR_TEXT_RIGHT); + zr_checkbox(ctx, "Check me", &check); + zr_label(ctx, "Combobox:", ZR_TEXT_RIGHT); + + if (zr_combo_begin_text(ctx, &combo, items[selected_item], 200)) { + zr_layout_row_dynamic(ctx, 30, 1); + for (i = 0; i < 3; ++i) + if (zr_combo_item(ctx, items[i], ZR_TEXT_LEFT)) + selected_item = i; + zr_combo_end(ctx); + } + } + zr_end(ctx); + ctx->style.font.height = 14; +} + +/* =============================================================== + * + * BUTTON DEMO + * + * ===============================================================*/ +static void +ui_header(struct zr_context *ctx, const char *title) +{ + zr_reset_font_height(ctx); + zr_push_font_height(ctx, 18); + zr_layout_row_dynamic(ctx, 20, 1); + zr_label(ctx, title, ZR_TEXT_LEFT); +} static void -compiled_window(struct zr_context *ctx) +ui_widget(struct zr_context *ctx, float height, float font_height) { - static int init = 0; - static char memory[4*1024]; - static struct zr_buffer program; + static const float ratio[] = {0.15f, 0.85f}; + zr_reset_font_height(ctx); + zr_push_font_height(ctx, font_height); + zr_layout_row(ctx, ZR_DYNAMIC, height, 2, ratio); + zr_spacing(ctx, 1); +} + +static void +ui_widget_centered(struct zr_context *ctx, float height, float font_height) +{ + static const float ratio[] = {0.15f, 0.50f, 0.35f}; + zr_reset_font_height(ctx); + zr_push_font_height(ctx, font_height); + zr_layout_row(ctx, ZR_DYNAMIC, height, 3, ratio); + zr_spacing(ctx, 1); +} + +static void +button_demo(struct zr_context *ctx, struct icons *img) +{ + struct zr_panel layout; + struct zr_panel menu; + static int option = 1; + static int toggle0 = 1; + static int toggle1 = 0; + static int toggle2 = 1; + + ctx->style.font.height = 20; + zr_begin(ctx, &layout, "Button Demo", zr_rect(50,50,255,610), + ZR_WINDOW_BORDER|ZR_WINDOW_MOVABLE|ZR_WINDOW_BORDER_HEADER|ZR_WINDOW_TITLE); + + /*------------------------------------------------ + * MENU + *------------------------------------------------*/ + zr_menubar_begin(ctx); + { + /* toolbar */ + zr_layout_row_static(ctx, 40, 40, 4); + if (zr_menu_icon_begin(ctx, &menu, "Music", img->play, 120)) + { + /* settings */ + zr_layout_row_dynamic(ctx, 25, 1); + zr_menu_item_icon(ctx, img->play, "Play", ZR_TEXT_RIGHT); + zr_menu_item_icon(ctx, img->stop, "Stop", ZR_TEXT_RIGHT); + zr_menu_item_icon(ctx, img->pause, "Pause", ZR_TEXT_RIGHT); + zr_menu_item_icon(ctx, img->next, "Next", ZR_TEXT_RIGHT); + zr_menu_item_icon(ctx, img->prev, "Prev", ZR_TEXT_RIGHT); + zr_menu_end(ctx); + } + zr_button_image(ctx, img->tools, ZR_BUTTON_DEFAULT); + zr_button_image(ctx, img->cloud, ZR_BUTTON_DEFAULT); + zr_button_image(ctx, img->pen, ZR_BUTTON_DEFAULT); + } + zr_menubar_end(ctx); + + /*------------------------------------------------ + * BUTTON + *------------------------------------------------*/ + ui_header(ctx, "Push buttons"); + ui_widget(ctx, 35, 22); + if (zr_button_text(ctx, "Push me", ZR_BUTTON_DEFAULT)) + fprintf(stdout, "pushed!\n"); + ui_widget(ctx, 35, 22); + if (zr_button_text_image(ctx, img->rocket, "Styled", ZR_TEXT_CENTERED, ZR_BUTTON_DEFAULT)) + fprintf(stdout, "rocket!\n"); + + /*------------------------------------------------ + * REPEATER + *------------------------------------------------*/ + ui_header(ctx, "Repeater"); + ui_widget(ctx, 35, 22); + if (zr_button_text(ctx, "Press me", ZR_BUTTON_REPEATER)) + fprintf(stdout, "pressed!\n"); + + /*------------------------------------------------ + * TOGGLE + *------------------------------------------------*/ + ui_header(ctx, "Toggle buttons"); + ui_widget(ctx, 35, 22); + if (zr_button_text_image(ctx, (toggle0) ? img->checked: img->unchecked, + "Toggle", ZR_TEXT_LEFT, ZR_BUTTON_DEFAULT)) toggle0 = !toggle0; + + ui_widget(ctx, 35, 22); + if (zr_button_text_image(ctx, (toggle1) ? img->checked: img->unchecked, + "Toggle", ZR_TEXT_LEFT, ZR_BUTTON_DEFAULT)) toggle1 = !toggle1; + + ui_widget(ctx, 35, 22); + if (zr_button_text_image(ctx, (toggle2) ? img->checked: img->unchecked, + "Toggle", ZR_TEXT_LEFT, ZR_BUTTON_DEFAULT)) toggle2 = !toggle2; + + /*------------------------------------------------ + * RADIO + *------------------------------------------------*/ + ui_header(ctx, "Radio buttons"); + ui_widget(ctx, 35, 22); + if (zr_button_text_symbol(ctx, (option == 0)?ZR_SYMBOL_CIRCLE_FILLED:ZR_SYMBOL_CIRCLE, + "Select", ZR_TEXT_LEFT, ZR_BUTTON_DEFAULT)) option = 0; + ui_widget(ctx, 35, 22); + if (zr_button_text_symbol(ctx, (option == 1)?ZR_SYMBOL_CIRCLE_FILLED:ZR_SYMBOL_CIRCLE, + "Select", ZR_TEXT_LEFT, ZR_BUTTON_DEFAULT)) option = 1; + ui_widget(ctx, 35, 22); + if (zr_button_text_symbol(ctx, (option == 2)?ZR_SYMBOL_CIRCLE_FILLED:ZR_SYMBOL_CIRCLE, + "Select", ZR_TEXT_LEFT, ZR_BUTTON_DEFAULT)) option = 2; + + /*------------------------------------------------ + * CONTEXTUAL + *------------------------------------------------*/ + if (zr_contextual_begin(ctx, &menu, ZR_WINDOW_NO_SCROLLBAR, zr_vec2(120, 200), zr_window_get_bounds(ctx))) { + ctx->style.font.height = 18; + zr_layout_row_dynamic(ctx, 25, 1); + if (zr_contextual_item_icon(ctx, img->copy, "Clone", ZR_TEXT_RIGHT)) + fprintf(stdout, "pressed clone!\n"); + if (zr_contextual_item_icon(ctx, img->del, "Delete", ZR_TEXT_RIGHT)) + fprintf(stdout, "pressed delete!\n"); + if (zr_contextual_item_icon(ctx, img->convert, "Convert", ZR_TEXT_RIGHT)) + fprintf(stdout, "pressed convert!\n"); + if (zr_contextual_item_icon(ctx, img->edit, "Edit", ZR_TEXT_RIGHT)) + fprintf(stdout, "pressed edit!\n"); + zr_contextual_end(ctx); + } + ctx->style.font.height = 14; + zr_end(ctx); +} + +/* =============================================================== + * + * BASIC DEMO + * + * ===============================================================*/ +static void +basic_demo(struct zr_context *ctx, struct icons *img) +{ + static int image_active; + static int check0 = 1; + static int check1 = 0; + static int slider = 30; + static size_t prog = 80; + static int selected_item = 0; + static int selected_image = 3; + static int selected_icon = 0; + static const char *items[] = {"Item 0","item 1","item 2"}; + static int piemenu_active = 0; + + int i = 0; + struct zr_panel layout; + struct zr_panel combo; + ctx->style.font.height = 20; + zr_begin(ctx, &layout, "Basic Demo", zr_rect(320, 50, 275, 610), + ZR_WINDOW_BORDER|ZR_WINDOW_MOVABLE|ZR_WINDOW_BORDER_HEADER|ZR_WINDOW_TITLE); + + /*------------------------------------------------ + * POPUP BUTTON + *------------------------------------------------*/ + ui_header(ctx, "Popup & Scrollbar & Images"); + ui_widget(ctx, 35, 22); + if (zr_button_text_image(ctx, img->directory, + "Images", ZR_TEXT_CENTERED, ZR_BUTTON_DEFAULT)) + image_active = !image_active; + + /*------------------------------------------------ + * SELECTED IMAGE + *------------------------------------------------*/ + ui_header(ctx, "Selected Image"); + ui_widget_centered(ctx, 100, 22); + zr_image(ctx, img->images[selected_image]); + + /*------------------------------------------------ + * IMAGE POPUP + *------------------------------------------------*/ + if (image_active) { + struct zr_panel popup; + if (zr_popup_begin(ctx, &popup, ZR_POPUP_STATIC, "Image Popup", 0, zr_rect(265, 0, 320, 220))) { + zr_layout_row_static(ctx, 82, 82, 3); + for (i = 0; i < 9; ++i) { + if (zr_button_image(ctx, img->images[i], ZR_BUTTON_DEFAULT)) { + selected_image = i; + image_active = 0; + zr_popup_close(ctx); + } + } + zr_popup_end(ctx); + } + } + /*------------------------------------------------ + * COMBOBOX + *------------------------------------------------*/ + ui_header(ctx, "Combo box"); + ui_widget(ctx, 40, 22); + if (zr_combo_begin_text(ctx, &combo, items[selected_item], 200)) { + zr_layout_row_dynamic(ctx, 35, 1); + for (i = 0; i < 3; ++i) + if (zr_combo_item(ctx, items[i], ZR_TEXT_LEFT)) + selected_item = i; + zr_combo_end(ctx); + } + + ui_widget(ctx, 40, 22); + if (zr_combo_begin_icon(ctx, &combo, items[selected_icon], img->images[selected_icon], 200)) { + zr_layout_row_dynamic(ctx, 35, 1); + for (i = 0; i < 3; ++i) + if (zr_combo_item_icon(ctx, img->images[i], items[i], ZR_TEXT_RIGHT)) + selected_icon = i; + zr_combo_end(ctx); + } + + /*------------------------------------------------ + * CHECKBOX + *------------------------------------------------*/ + ui_header(ctx, "Checkbox"); + ui_widget(ctx, 30, 22); + zr_checkbox(ctx, "Flag 1", &check0); + ui_widget(ctx, 30, 22); + zr_checkbox(ctx, "Flag 2", &check1); + + /*------------------------------------------------ + * PROGRESSBAR + *------------------------------------------------*/ + ui_header(ctx, "Progressbar"); + ui_widget(ctx, 35, 22); + zr_progress(ctx, &prog, 100, zr_true); + + /*------------------------------------------------ + * SLIDER + *------------------------------------------------*/ + ui_header(ctx, "Slider"); + ui_widget(ctx, 35, 22); + zr_slider_int(ctx, 0, &slider, 100, 10); + + /*------------------------------------------------ + * PIEMENU + *------------------------------------------------*/ + if (zr_input_is_mouse_down(&ctx->input, ZR_BUTTON_RIGHT) && + zr_input_is_mouse_hovering_rect(&ctx->input, layout.bounds)) + piemenu_active = 1; + + if (piemenu_active) { + int ret = ui_piemenu(ctx, zr_vec2(WINDOW_WIDTH/2-140, WINDOW_HEIGHT/2-140), 140, &img->menu[0], 6); + if (ret != -1) { + fprintf(stdout, "piemenu selected: %d\n", ret); + piemenu_active = 0; + } + } + ctx->style.font.height = 14; + zr_end(ctx); +} + +/* =============================================================== + * + * NODE EDITOR + * + * ===============================================================*/ +struct node { + int ID; + char name[32]; + struct zr_rect bounds; + float value; + struct zr_color color; + int input_count; + int output_count; + struct node *next; + struct node *prev; +}; + +struct node_link { + int input_id; + int input_slot; + int output_id; + int output_slot; + struct zr_vec2 in; + struct zr_vec2 out; +}; + +struct node_linking { + int active; + struct node *node; + int input_id; + int input_slot; +}; + +struct node_editor { + struct node node_buf[32]; + struct node_link links[64]; + struct node *begin; + struct node *end; + int node_count; + int link_count; + struct zr_rect bounds; + struct node *selected; + int show_grid; + struct zr_vec2 scrolling; + struct node_linking linking; +}; + +static void +node_editor_push(struct node_editor *editor, struct node *node) +{ + if (!editor->begin) { + node->next = NULL; + node->prev = NULL; + editor->begin = node; + editor->end = node; + } else { + node->prev = editor->end; + if (editor->end) + editor->end->next = node; + node->next = NULL; + editor->end = node; + } +} + +static void +node_editor_pop(struct node_editor *editor, struct node *node) +{ + if (node->next) + node->next->prev = node->prev; + if (node->prev) + node->prev->next = node->next; + if (editor->end == node) + editor->end = node->prev; + if (editor->begin == node) + editor->begin = node->next; + node->next = NULL; + node->prev = NULL; +} + +static struct node* +node_editor_find(struct node_editor *editor, int ID) +{ + struct node *iter = editor->begin; + while (iter) { + if (iter->ID == ID) + return iter; + iter = iter->next; + } + return NULL; +} + +static void +node_editor_add(struct node_editor *editor, const char *name, struct zr_rect bounds, + struct zr_color col, int in_count, int out_count) +{ + static int IDs = 0; + struct node *node; + assert((zr_size)editor->node_count < LEN(editor->node_buf)); + node = &editor->node_buf[editor->node_count++]; + node->ID = IDs++; + node->value = 0; + node->color = zr_rgb(255, 0, 0); + node->input_count = in_count; + node->output_count = out_count; + node->color = col; + strcpy(node->name, name); + node->bounds = bounds; + node_editor_push(editor, node); +} + +static void +node_editor_link(struct node_editor *editor, int in_id, int in_slot, + int out_id, int out_slot) +{ + struct node_link *link; + assert((zr_size)editor->link_count < LEN(editor->links)); + link = &editor->links[editor->link_count++]; + link->input_id = in_id; + link->input_slot = in_slot; + link->output_id = out_id; + link->output_slot = out_slot; +} + +static void +node_editor_demo(struct zr_context *ctx, struct node_editor *nodedit) +{ + int n = 0; + struct zr_rect total_space; + const struct zr_input *in = &ctx->input; + struct zr_command_buffer *canvas; + struct node *updated = 0; + struct zr_panel layout; + + if (zr_begin(ctx, &layout, "Node Editor", zr_rect(50, 50, 650, 650), + ZR_WINDOW_BORDER|ZR_WINDOW_NO_SCROLLBAR|ZR_WINDOW_CLOSABLE|ZR_WINDOW_MOVABLE)) + { + /* allocate complete window space */ + canvas = zr_window_get_canvas(ctx); + total_space = zr_window_get_content_region(ctx); + zr_layout_space_begin(ctx, ZR_STATIC, total_space.h, nodedit->node_count); + { + struct zr_panel node, menu; + struct node *it = nodedit->begin; + struct zr_rect size = zr_layout_space_bounds(ctx); + + if (nodedit->show_grid) { + /* display grid */ + float x, y; + const float grid_size = 32.0f; + const struct zr_color grid_color = zr_rgb(50, 50, 50); + for (x = (float)fmod(size.x - nodedit->scrolling.x, grid_size); x < size.w; x += grid_size) + zr_draw_line(canvas, x+size.x, size.y, x+size.x, size.y+size.h, grid_color); + for (y = (float)fmod(size.y - nodedit->scrolling.y, grid_size); y < size.h; y += grid_size) + zr_draw_line(canvas, size.x, y+size.y, size.x+size.w, y+size.y, grid_color); + } + + /* execute each node as a moveable group */ + while (it) { + /* calculate scrolled node window position and size */ + zr_layout_space_push(ctx, zr_rect(it->bounds.x - nodedit->scrolling.x, + it->bounds.y - nodedit->scrolling.y, it->bounds.w, it->bounds.h)); + + /* execute node window */ + if (zr_group_begin(ctx, &node, it->name, ZR_WINDOW_MOVABLE|ZR_WINDOW_NO_SCROLLBAR|ZR_WINDOW_BORDER|ZR_WINDOW_TITLE)) + { + /* always have last selected node on top */ + if (zr_input_mouse_clicked(in, ZR_BUTTON_LEFT, node.bounds) && + (!(it->prev && zr_input_mouse_clicked(in, ZR_BUTTON_LEFT, + zr_layout_space_rect_to_screen(ctx, node.bounds)))) && + nodedit->end != it) + { + updated = it; + } + + /* ================= NODE CONTENT =====================*/ + zr_layout_row_dynamic(ctx, 25, 1); + zr_button_color(ctx, it->color, ZR_BUTTON_DEFAULT); + it->color.r = (zr_byte)zr_propertyi(ctx, "#R:", 0, it->color.r, 255, 1,1); + it->color.g = (zr_byte)zr_propertyi(ctx, "#G:", 0, it->color.g, 255, 1,1); + it->color.b = (zr_byte)zr_propertyi(ctx, "#B:", 0, it->color.b, 255, 1,1); + it->color.a = (zr_byte)zr_propertyi(ctx, "#A:", 0, it->color.a, 255, 1,1); + /* ====================================================*/ + zr_group_end(ctx); + } + { + /* node connector and linking */ + float space; + struct zr_rect bounds; + bounds = zr_layout_space_rect_to_local(ctx, node.bounds); + bounds.x += nodedit->scrolling.x; + bounds.y += nodedit->scrolling.y; + it->bounds = bounds; + + /* output connector */ + space = node.bounds.h / (float)((it->output_count) + 1); + for (n = 0; n < it->output_count; ++n) { + struct zr_rect circle; + circle.x = node.bounds.x + node.bounds.w-4; + circle.y = node.bounds.y + space * (float)(n+1); + circle.w = 8; circle.h = 8; + zr_draw_circle(canvas, circle, zr_rgb(100, 100, 100)); + + /* start linking process */ + if (zr_input_has_mouse_click_down_in_rect(in, ZR_BUTTON_LEFT, circle, zr_true)) { + nodedit->linking.active = zr_true; + nodedit->linking.node = it; + nodedit->linking.input_id = it->ID; + nodedit->linking.input_slot = n; + } + + /* draw curve from linked node slot to mouse position */ + if (nodedit->linking.active && nodedit->linking.node == it && + nodedit->linking.input_slot == n) { + struct zr_vec2 l0 = zr_vec2(circle.x + 3, circle.y + 3); + struct zr_vec2 l1 = in->mouse.pos; + zr_draw_curve(canvas, l0.x, l0.y, l0.x + 50.0f, l0.y, + l1.x - 50.0f, l1.y, l1.x, l1.y, zr_rgb(100, 100, 100)); + } + } + + /* input connector */ + space = node.bounds.h / (float)((it->input_count) + 1); + for (n = 0; n < it->input_count; ++n) { + struct zr_rect circle; + circle.x = node.bounds.x-4; + circle.y = node.bounds.y + space * (float)(n+1); + circle.w = 8; circle.h = 8; + zr_draw_circle(canvas, circle, zr_rgb(100, 100, 100)); + if (zr_input_is_mouse_released(in, ZR_BUTTON_LEFT) && + zr_input_is_mouse_hovering_rect(in, circle) && + nodedit->linking.active && nodedit->linking.node != it) { + nodedit->linking.active = zr_false; + node_editor_link(nodedit, nodedit->linking.input_id, + nodedit->linking.input_slot, it->ID, n); + } + } + } + it = it->next; + } + + /* reset linking connection */ + if (nodedit->linking.active && zr_input_is_mouse_released(in, ZR_BUTTON_LEFT)) { + nodedit->linking.active = zr_false; + nodedit->linking.node = NULL; + fprintf(stdout, "linking failed\n"); + } + + /* draw each link */ + for (n = 0; n < nodedit->link_count; ++n) { + struct node_link *link = &nodedit->links[n]; + struct node *ni = node_editor_find(nodedit, link->input_id); + struct node *no = node_editor_find(nodedit, link->output_id); + float spacei = node.bounds.h / (float)((ni->output_count) + 1); + float spaceo = node.bounds.h / (float)((no->input_count) + 1); + struct zr_vec2 l0 = zr_layout_space_to_screen(ctx, + zr_vec2(ni->bounds.x + ni->bounds.w, 3.0f + ni->bounds.y + spacei * (float)(link->input_slot+1))); + struct zr_vec2 l1 = zr_layout_space_to_screen(ctx, + zr_vec2(no->bounds.x, 3.0f + no->bounds.y + spaceo * (float)(link->output_slot+1))); + + l0.x -= nodedit->scrolling.x; + l0.y -= nodedit->scrolling.y; + l1.x -= nodedit->scrolling.x; + l1.y -= nodedit->scrolling.y; + zr_draw_curve(canvas, l0.x, l0.y, l0.x + 50.0f, l0.y, + l1.x - 50.0f, l1.y, l1.x, l1.y, zr_rgb(100, 100, 100)); + } + + if (updated) { + /* reshuffle nodes to have last recently selected node on top */ + node_editor_pop(nodedit, updated); + node_editor_push(nodedit, updated); + } + + /* node selection */ + if (zr_input_mouse_clicked(in, ZR_BUTTON_LEFT, zr_layout_space_bounds(ctx))) { + it = nodedit->begin; + nodedit->selected = NULL; + nodedit->bounds = zr_rect(in->mouse.pos.x, in->mouse.pos.y, 100, 200); + while (it) { + struct zr_rect b = zr_layout_space_rect_to_screen(ctx, it->bounds); + b.x -= nodedit->scrolling.x; + b.y -= nodedit->scrolling.y; + if (zr_input_is_mouse_hovering_rect(in, b)) + nodedit->selected = it; + it = it->next; + } + } + + /* contextual menu */ + if (zr_contextual_begin(ctx, &menu, 0, zr_vec2(100, 220), zr_window_get_bounds(ctx))) { + const char *grid_option[] = {"Show Grid", "Hide Grid"}; + zr_layout_row_dynamic(ctx, 25, 1); + if (zr_contextual_item(ctx, "New", ZR_TEXT_CENTERED)) + node_editor_add(nodedit, "New", zr_rect(400, 260, 180, 220), + zr_rgb(255, 255, 255), 1, 2); + if (zr_contextual_item(ctx, grid_option[nodedit->show_grid],ZR_TEXT_CENTERED)) + nodedit->show_grid = !nodedit->show_grid; + zr_contextual_end(ctx); + } + } + zr_layout_space_end(ctx); + + /* window content scrolling */ + if (zr_input_is_mouse_hovering_rect(in, zr_window_get_bounds(ctx)) && + zr_input_is_mouse_down(in, ZR_BUTTON_MIDDLE)) { + nodedit->scrolling.x += in->mouse.delta.x; + nodedit->scrolling.y += in->mouse.delta.y; + } + } + zr_end(ctx); +} + +static void +node_editor_init(struct node_editor *editor) +{ + memset(editor, 0, sizeof(*editor)); + editor->begin = NULL; + editor->end = NULL; + node_editor_add(editor, "Source", zr_rect(40, 10, 180, 220), zr_rgb(255, 0, 0), 0, 1); + node_editor_add(editor, "Source", zr_rect(40, 260, 180, 220), zr_rgb(0, 255, 0), 0, 1); + node_editor_add(editor, "Combine", zr_rect(400, 100, 180, 220), zr_rgb(0,0,255), 2, 2); + node_editor_link(editor, 0, 0, 2, 0); + node_editor_link(editor, 1, 0, 2, 1); + editor->show_grid = zr_true; +} + +/* =============================================================== + * + * RECORDED WINDOW + * + * ===============================================================*/ +static void +record_window(struct zr_context *ctx, struct zr_buffer *buffer) +{ + /* Recording a UI begins by calling `zr_recording_begin` and ends with + * `zr_recording_end`. All supported API call between these two calls + * are saved inside the buffer and can later be replayed with `zr_exec`. + * IMPORTANT: all functions which control the UI like `zr_begin`, + * `zr_group_begin` or `zr_layout_push` and many more will return true + * so to make sure that the complete UI is being recored. Please make sure + * that no side effect can occur because of it while recording.*/ + struct zr_panel layout; + zr_recording_begin(ctx, buffer); + if (zr_begin(ctx, &layout, "Recorded", zr_rect(420, 350, 200, 350), + ZR_WINDOW_BORDER|ZR_WINDOW_MOVABLE|ZR_WINDOW_SCALABLE| + ZR_WINDOW_CLOSABLE|ZR_WINDOW_MINIMIZABLE|ZR_WINDOW_TITLE)) + { + zr_layout_row_static(ctx, 30, 150, 1); + zr_button_text(ctx, "Test", ZR_BUTTON_DEFAULT); + zr_slide_float(ctx, 0, 5, 10, 1); + zr_prog(ctx, 20, 100, ZR_MODIFIABLE); + zr_propertyi(ctx, "Compression:", 0, 20, 100, 10, 1); + zr_select(ctx, "Select me", ZR_TEXT_CENTERED|ZR_TEXT_MIDDLE, 0); + zr_check(ctx, "Check me", zr_true); + zr_layout_row_static(ctx, 30, 50, 3); + zr_option(ctx, "RGB", 1); + zr_option(ctx, "HSV", 0); + zr_option(ctx, "HEX", 0); + if (zr_layout_push(ctx, ZR_LAYOUT_TAB, "Tab", ZR_MINIMIZED)) { + zr_layout_row_static(ctx, 30, 150, 1); + zr_button_text(ctx, "Test", ZR_BUTTON_DEFAULT); + zr_layout_pop(ctx); + } + } + zr_end(ctx); + zr_recording_end(ctx); +} + +static void +replay_window(struct zr_context *ctx, struct zr_buffer *record) +{ + int count = 0; + union zr_event evt_memory[32]; + struct zr_buffer events, runtime; + char runtime_memory[4 * sizeof(struct zr_panel)]; /* widget identifier (auto generated) */ enum widget_ids { WIDGET_BUTTON, + WIDGET_SLIDER, + WIDGET_PROGRESS, + WIDGET_PROPERTY, + WIDGET_SELECT, + WIDGET_CHECK, + WIDGET_OPTION_RGB, + WIDGET_OPTION_HSV, + WIDGET_OPTION_HEX, WIDGET_TAB, WIDGET_BUTTON2 }; + ctx->next_id = 0; - if (!init) { - /* RECORD UI */ - struct zr_panel layout; - zr_buffer_init_fixed(&program, memory, sizeof(memory)); - zr_recording_begin(ctx, &program); - { - if (zr_begin(ctx, &layout, "Compiled", zr_rect(420, 350, 200, 200), - ZR_WINDOW_BORDER|ZR_WINDOW_MOVABLE|ZR_WINDOW_SCALABLE| - ZR_WINDOW_CLOSABLE|ZR_WINDOW_MINIMIZABLE|ZR_WINDOW_TITLE)) - { - zr_layout_row_static(ctx, 30, 150, 1); - zr_button_text(ctx, "Test", ZR_BUTTON_DEFAULT); - if (zr_layout_push(ctx, ZR_LAYOUT_TAB, "Tab", ZR_MINIMIZED)) { - zr_button_text(ctx, "Test", ZR_BUTTON_DEFAULT); - zr_layout_pop(ctx); + {const union zr_event *evt; + /* To execute a previously recorded UI you need to provide two buffer. The + * first one is to store events into and the other is for runtime memory. + * For event memory I would recommend using a fixed size `zr_event` array + * since the number of events generated is small most of the time. Runtime + * memory is only used for `zr_panel`s so only the max number of panels + * to run the UI is needed.*/ + zr_buffer_init_fixed(&events, evt_memory, sizeof(evt_memory)); + zr_buffer_init_fixed(&runtime, runtime_memory, sizeof(runtime_memory)); + + /* If you use zr_exec for executing a fixed UI by handling events you can use + * the event count to query if the frame needs to be drawn or not. Which can + * bring a huge performance boost. (NOTICE: some functions like zr_begin generate + * heartbeat events which do not have any impact and are only used while using + * the API in immediate mode and can easily be ignored). You can also use + * an event mask to only specify which events should be generated and ignore + * all other. This example here uses a default event mask by passing `0` to + * zr_exec which allows all events, but you could also create your own: + * + * struct zr_event_mask mask; + * zr_event_mask_begin(&mask); + * zr_event_mask_add(&mask, ZR_EVENT_WINDOW, ZR_EVENT_WINDOW_MOVED); + * zr_event_mask_add(&mask, ZR_EVENT_WINDOW, ZR_EVENT_WINDOW_CLOSED|ZR_EVENT_WINDOW_RESIZED); + * zr_event_mask_add(&mask, ZR_EVENT_BUTTON, ZR_EVENT_BUTTON_CLICKED); + * zr_event_mask_add(&mask, ZR_EVENT_PROGRESS, ZR_EVENT_PROGRESS_CHANGED); + * zr_event_mask_add(&mask, ZR_EVENT_PROPERTY, ZR_EVENT_PROPERTY_CHANGED); + * zr_event_mask_end(&mask); + * zr_exec(ctx, &events, &count, &mask, record, &runtime); + */ + zr_exec(ctx, &events, &count, 0, record, &runtime); + + /* This foreach loop is guaranteed to work with an `zr_event` array or a memory + * buffer. You could also use the event count generated by `zr_exec` and + * run over `events` by a typical for loop if you use an array but you are + * forced to use `zr_foreach_event` if you use a growing buffer. */ + zr_foreach_event(evt, &events) + { + switch (evt->base.type) { + case ZR_EVENT_WINDOW: { + if (evt->win.evt == ZR_EVENT_WINDOW_HIDDEN) + fprintf(stdout, "window hidden\n"); + else if (evt->win.evt == ZR_EVENT_WINDOW_EXPOSED) + fprintf(stdout, "window exposed\n"); + else if (evt->win.evt == ZR_EVENT_WINDOW_MOVED) + fprintf(stdout, "window moved: (%d, %d)\n", evt->win.data[0], evt->win.data[1]); + else if (evt->win.evt == ZR_EVENT_WINDOW_RESIZED) + fprintf(stdout, "window resized: (%d, %d)\n", evt->win.data[0], evt->win.data[1]); + else if (evt->win.evt == ZR_EVENT_WINDOW_MINIMIZED) + fprintf(stdout, "window minimized\n"); + else if (evt->win.evt == ZR_EVENT_WINDOW_MAXIMIZED) + fprintf(stdout, "window maximized\n"); + else if (evt->win.evt == ZR_EVENT_WINDOW_FOCUS) + fprintf(stdout, "window focused\n"); + else if (evt->win.evt == ZR_EVENT_WINDOW_HOVERED) + fprintf(stdout, "window hovered\n"); + } break; + + case ZR_EVENT_BUTTON: { + if (evt->base.id == WIDGET_BUTTON) { + if (evt->button.evt == ZR_EVENT_BUTTON_CLICKED) + fprintf(stdout, "button pressed\n"); + else if (evt->button.evt == ZR_EVENT_BUTTON_HOVERED) + fprintf(stdout, "button hovered\n"); + } else if (evt->base.id == WIDGET_BUTTON2) { + if (evt->button.evt == ZR_EVENT_BUTTON_CLICKED) + fprintf(stdout, "button2 pressed\n"); + else if (evt->button.evt == ZR_EVENT_BUTTON_HOVERED) + fprintf(stdout, "button2 hovered\n"); + } + } break; + + case ZR_EVENT_SELECT: { + if (evt->select.evt == ZR_EVENT_SELECT_TOGGLED) + fprintf(stdout, "select toggled\n"); + else if (evt->select.evt == ZR_EVENT_SELECT_HOVERED) + fprintf(stdout, "selected hovered\n"); + } break; + + case ZR_EVENT_SLIDER: { + if (evt->base.id == WIDGET_SLIDER) { + if (evt->slider.evt == ZR_EVENT_SLIDER_CHANGED) + fprintf(stdout, "slider changed value to: %.2f\n", evt->slider.value); + else if (evt->slider.evt == ZR_EVENT_SLIDER_HOVERED) + fprintf(stdout, "slider hovered\n"); + } + } break; + + case ZR_EVENT_PROPERTY: { + const char *property_states[] = {"Default", "Edit", "Drag"}; + if (evt->base.id == WIDGET_PROPERTY) { + if (evt->property.evt == ZR_EVENT_PROPERTY_CHANGED) + fprintf(stdout, "property changed value to: %.2f\n", evt->property.value); + else if (evt->property.evt == ZR_EVENT_PROPERTY_HOVERED) + fprintf(stdout, "property hovered\n"); + else if (evt->property.evt == ZR_EVENT_PROPERTY_STATE_CHANGED) + fprintf(stdout, "property state changed to: %s\n", property_states[evt->property.state]); + } + } break; + + case ZR_EVENT_CHECKBOX: { + if (evt->property.evt == ZR_EVENT_CHECK_TOGGLED) + fprintf(stdout, "checkbox toggled to value: %d\n", evt->checkbox.value); + else if (evt->property.evt == ZR_EVENT_CHECK_HOVERED) + fprintf(stdout, "checkbox hovered\n"); + } break; + + case ZR_EVENT_OPTION: { + if (evt->option.evt == ZR_EVENT_OPTION_TOGGLED) { + /* look up option elements */ + zr_element rgb = zr_element_lookup(record, WIDGET_OPTION_RGB); + zr_element hsv = zr_element_lookup(record, WIDGET_OPTION_HSV); + zr_element hex = zr_element_lookup(record, WIDGET_OPTION_HEX); + + /* make sure only *one* option is selected */ + if (evt->base.id == WIDGET_OPTION_RGB) { + zr_element_set_int(record, hsv, ZR_OPTION_ACTIVE, 0); + zr_element_set_int(record, hex, ZR_OPTION_ACTIVE, 0); + } else if (evt->base.id == WIDGET_OPTION_HSV) { + zr_element_set_int(record, rgb, ZR_OPTION_ACTIVE, 0); + zr_element_set_int(record, hex, ZR_OPTION_ACTIVE, 0); + } else { + zr_element_set_int(record, rgb, ZR_OPTION_ACTIVE, 0); + zr_element_set_int(record, hsv, ZR_OPTION_ACTIVE, 0); } } - zr_end(ctx); + } break; } - zr_recording_end(ctx); - init = 1; - } - { - /* REPLAY UI */ - int count = 0; - struct zr_event evt_memory[256]; - struct zr_buffer events, runtime; - char runtime_memory[4 * sizeof(struct zr_panel)]; - - const struct zr_event *evt; - zr_buffer_init_fixed(&events, evt_memory, sizeof(evt_memory)); - zr_buffer_init_fixed(&runtime, runtime_memory, sizeof(runtime_memory)); - zr_exec(ctx, &events, &count, &program, &runtime); - zr_foreach_event(evt, &events) - { - switch (evt->id) { - case WIDGET_BUTTON: { - if (evt->type == ZR_EVENT_ACTIVE) - fprintf(stdout, "test button pressed\n"); - } break; - case WIDGET_BUTTON2: { - if (evt->type == ZR_EVENT_ACTIVE) - fprintf(stdout, "test button2 pressed\n"); - } break; - } - } - } + }} } +/* =============================================================== + * + * DEMO ENTRY + * + * ===============================================================*/ static int run_demo(struct demo *gui) { int ret = 1; + static int init = 0; + static char record_memory[4*1024]; + static struct zr_buffer record; + static struct node_editor nodedit; struct zr_context *ctx = &gui->ctx; -#if 0 - static int init = 0; - static struct node_editor nodedit; if (!init) { memset(&nodedit, 0, sizeof(nodedit)); + zr_buffer_init_fixed(&record, record_memory, sizeof(record_memory)); + record_window(ctx, &record); node_editor_init(&nodedit); init = 1; } -#endif - /* windows */ - compiled_window(ctx); -#if 0 simple_window(ctx); + replay_window(ctx, &record); demo_window(ctx); node_editor_demo(ctx, &nodedit); #ifndef DEMO_DO_NOT_DRAW_IMAGES grid_demo(ctx); button_demo(ctx, &gui->icons); basic_demo(ctx, &gui->icons); -#endif #endif ret = control_window(ctx, gui); diff --git a/demo/extended.c b/demo/extended.c deleted file mode 100644 index 6844519d..00000000 --- a/demo/extended.c +++ /dev/null @@ -1,414 +0,0 @@ -/* =============================================================== - * - * CUSTOM WIDGET - * - * ===============================================================*/ -static int -ui_piemenu(struct zr_context *ctx, struct zr_vec2 pos, float radius, - struct zr_image *icons, int item_count) -{ - int ret = -1; - struct zr_rect total_space; - struct zr_panel popup; - struct zr_rect bounds; - int active_item = 0; - - /* hide popup background */ - struct zr_color border; - zr_push_color(ctx, ZR_COLOR_WINDOW, zr_rgba(0,0,0,0)); - border = ctx->style.colors[ZR_COLOR_BORDER]; - zr_push_color(ctx, ZR_COLOR_BORDER, zr_rgba(0,0,0,0)); - - /* pie menu popup */ - total_space = zr_window_get_content_region(ctx); - zr_popup_begin(ctx, &popup, ZR_POPUP_STATIC, "piemenu", ZR_WINDOW_NO_SCROLLBAR, - zr_rect(pos.x - total_space.x - radius, pos.y - radius - total_space.y, - 2*radius,2*radius)); - - total_space = zr_window_get_content_region(ctx); - zr_layout_row_dynamic(ctx, total_space.h, 1); - { - int i = 0; - struct zr_command_buffer* out = zr_window_get_canvas(ctx); - const struct zr_input *in = &ctx->input; - { - /* allocate complete popup space for the menu */ - enum zr_widget_state state; - total_space = zr_window_get_content_region(ctx); - total_space.x = total_space.y = 0; - state = zr_widget(&bounds, ctx); - } - - /* outer circle */ - zr_draw_circle(out, bounds, zr_rgb(50,50,50)); - { - /* circle buttons */ - float step = (2 * 3.141592654f) / (float)(MAX(1,item_count)); - float a_min = 0; float a_max = step; - - struct zr_vec2 center = zr_vec2(bounds.x + bounds.w / 2.0f, bounds.y + bounds.h / 2.0f); - struct zr_vec2 drag = zr_vec2(in->mouse.pos.x - center.x, in->mouse.pos.y - center.y); - float angle = (float)atan2(drag.y, drag.x); - if (angle < -0.0f) angle += 2.0f * 3.141592654f; - active_item = (int)(angle/step); - - for (i = 0; i < item_count; ++i) { - struct zr_rect content; - float rx, ry, dx, dy, a; - zr_draw_arc(out, center.x, center.y, (bounds.w/2.0f), - a_min, a_max, (active_item == i) ? zr_rgb(45,100,255): zr_rgb(60,60,60)); - - /* seperator line */ - rx = bounds.w/2.0f; ry = 0; - dx = rx * (float)cos(a_min) - ry * (float)sin(a_min); - dy = rx * (float)sin(a_min) + ry * (float)cos(a_min); - zr_draw_line(out, center.x, center.y, - center.x + dx, center.y + dy, zr_rgb(50,50,50)); - - /* button content */ - a = a_min + (a_max - a_min)/2.0f; - rx = bounds.w/2.5f; ry = 0; - content.w = 30; content.h = 30; - content.x = center.x + ((rx * (float)cos(a) - ry * (float)sin(a)) - content.w/2.0f); - content.y = center.y + (rx * (float)sin(a) + ry * (float)cos(a) - content.h/2.0f); - zr_draw_image(out, content, &icons[i]); - a_min = a_max; a_max += step; - } - } - { - /* inner circle */ - struct zr_rect inner; - inner.x = bounds.x + bounds.w/2 - bounds.w/4; - inner.y = bounds.y + bounds.h/2 - bounds.h/4; - inner.w = bounds.w/2; inner.h = bounds.h/2; - zr_draw_circle(out, inner, zr_rgb(45,45,45)); - - /* active icon content */ - bounds.w = inner.w / 2.0f; - bounds.h = inner.h / 2.0f; - bounds.x = inner.x + inner.w/2 - bounds.w/2; - bounds.y = inner.y + inner.h/2 - bounds.h/2; - zr_draw_image(out, bounds, &icons[active_item]); - } - } - zr_layout_space_end(ctx); - zr_popup_end(ctx); - - zr_reset_colors(ctx); - zr_reset_properties(ctx); - - if (!zr_input_is_mouse_down(&ctx->input, ZR_BUTTON_RIGHT)) - return active_item; - else return ret; -} - -/* =============================================================== - * - * GRID - * - * ===============================================================*/ -static void -grid_demo(struct zr_context *ctx) -{ - static char text[3][64]; - static size_t text_len[3]; - static const char *items[] = {"Item 0","item 1","item 2"}; - static int selected_item = 0; - static int check = 1; - struct zr_panel layout; - - int i; - struct zr_panel combo; - ctx->style.font.height = 20; - if (zr_begin(ctx, &layout, "Grid Demo", zr_rect(600, 350, 275, 250), - ZR_WINDOW_TITLE|ZR_WINDOW_BORDER|ZR_WINDOW_MOVABLE| - ZR_WINDOW_BORDER_HEADER|ZR_WINDOW_NO_SCROLLBAR)) - { - ctx->style.font.height = 18; - zr_layout_row_dynamic(ctx, 30, 2); - zr_label(ctx, "Floating point:", ZR_TEXT_RIGHT); - zr_edit_string(ctx, ZR_EDIT_FIELD, text[0], &text_len[0], 64, zr_filter_float); - zr_label(ctx, "Hexadeximal:", ZR_TEXT_RIGHT); - zr_edit_string(ctx, ZR_EDIT_FIELD, text[1], &text_len[1], 64, zr_filter_hex); - zr_label(ctx, "Binary:", ZR_TEXT_RIGHT); - zr_edit_string(ctx, ZR_EDIT_FIELD, text[2], &text_len[2], 64, zr_filter_binary); - zr_label(ctx, "Checkbox:", ZR_TEXT_RIGHT); - zr_checkbox(ctx, "Check me", &check); - zr_label(ctx, "Combobox:", ZR_TEXT_RIGHT); - - if (zr_combo_begin_text(ctx, &combo, items[selected_item], 200)) { - zr_layout_row_dynamic(ctx, 30, 1); - for (i = 0; i < 3; ++i) - if (zr_combo_item(ctx, items[i], ZR_TEXT_LEFT)) - selected_item = i; - zr_combo_end(ctx); - } - } - zr_end(ctx); - ctx->style.font.height = 14; -} - -/* =============================================================== - * - * BUTTON DEMO - * - * ===============================================================*/ -static void -ui_header(struct zr_context *ctx, const char *title) -{ - zr_reset_font_height(ctx); - zr_push_font_height(ctx, 18); - zr_layout_row_dynamic(ctx, 20, 1); - zr_label(ctx, title, ZR_TEXT_LEFT); -} - -static void -ui_widget(struct zr_context *ctx, float height, float font_height) -{ - static const float ratio[] = {0.15f, 0.85f}; - zr_reset_font_height(ctx); - zr_push_font_height(ctx, font_height); - zr_layout_row(ctx, ZR_DYNAMIC, height, 2, ratio); - zr_spacing(ctx, 1); -} - -static void -ui_widget_centered(struct zr_context *ctx, float height, float font_height) -{ - static const float ratio[] = {0.15f, 0.50f, 0.35f}; - zr_reset_font_height(ctx); - zr_push_font_height(ctx, font_height); - zr_layout_row(ctx, ZR_DYNAMIC, height, 3, ratio); - zr_spacing(ctx, 1); -} - -static void -button_demo(struct zr_context *ctx, struct icons *img) -{ - struct zr_panel layout; - struct zr_panel menu; - static int option = 1; - static int toggle0 = 1; - static int toggle1 = 0; - static int toggle2 = 1; - - ctx->style.font.height = 20; - zr_begin(ctx, &layout, "Button Demo", zr_rect(50,50,255,610), - ZR_WINDOW_BORDER|ZR_WINDOW_MOVABLE|ZR_WINDOW_BORDER_HEADER|ZR_WINDOW_TITLE); - - /*------------------------------------------------ - * MENU - *------------------------------------------------*/ - zr_menubar_begin(ctx); - { - /* toolbar */ - zr_layout_row_static(ctx, 40, 40, 4); - if (zr_menu_icon_begin(ctx, &menu, "Music", img->play, 120)) - { - /* settings */ - zr_layout_row_dynamic(ctx, 25, 1); - zr_menu_item_icon(ctx, img->play, "Play", ZR_TEXT_RIGHT); - zr_menu_item_icon(ctx, img->stop, "Stop", ZR_TEXT_RIGHT); - zr_menu_item_icon(ctx, img->pause, "Pause", ZR_TEXT_RIGHT); - zr_menu_item_icon(ctx, img->next, "Next", ZR_TEXT_RIGHT); - zr_menu_item_icon(ctx, img->prev, "Prev", ZR_TEXT_RIGHT); - zr_menu_end(ctx); - } - zr_button_image(ctx, img->tools, ZR_BUTTON_DEFAULT); - zr_button_image(ctx, img->cloud, ZR_BUTTON_DEFAULT); - zr_button_image(ctx, img->pen, ZR_BUTTON_DEFAULT); - } - zr_menubar_end(ctx); - - /*------------------------------------------------ - * BUTTON - *------------------------------------------------*/ - ui_header(ctx, "Push buttons"); - ui_widget(ctx, 35, 22); - if (zr_button_text(ctx, "Push me", ZR_BUTTON_DEFAULT)) - fprintf(stdout, "pushed!\n"); - ui_widget(ctx, 35, 22); - if (zr_button_text_image(ctx, img->rocket, "Styled", ZR_TEXT_CENTERED, ZR_BUTTON_DEFAULT)) - fprintf(stdout, "rocket!\n"); - - /*------------------------------------------------ - * REPEATER - *------------------------------------------------*/ - ui_header(ctx, "Repeater"); - ui_widget(ctx, 35, 22); - if (zr_button_text(ctx, "Press me", ZR_BUTTON_REPEATER)) - fprintf(stdout, "pressed!\n"); - - /*------------------------------------------------ - * TOGGLE - *------------------------------------------------*/ - ui_header(ctx, "Toggle buttons"); - ui_widget(ctx, 35, 22); - if (zr_button_text_image(ctx, (toggle0) ? img->checked: img->unchecked, - "Toggle", ZR_TEXT_LEFT, ZR_BUTTON_DEFAULT)) toggle0 = !toggle0; - - ui_widget(ctx, 35, 22); - if (zr_button_text_image(ctx, (toggle1) ? img->checked: img->unchecked, - "Toggle", ZR_TEXT_LEFT, ZR_BUTTON_DEFAULT)) toggle1 = !toggle1; - - ui_widget(ctx, 35, 22); - if (zr_button_text_image(ctx, (toggle2) ? img->checked: img->unchecked, - "Toggle", ZR_TEXT_LEFT, ZR_BUTTON_DEFAULT)) toggle2 = !toggle2; - - /*------------------------------------------------ - * RADIO - *------------------------------------------------*/ - ui_header(ctx, "Radio buttons"); - ui_widget(ctx, 35, 22); - if (zr_button_text_symbol(ctx, (option == 0)?ZR_SYMBOL_CIRCLE_FILLED:ZR_SYMBOL_CIRCLE, - "Select", ZR_TEXT_LEFT, ZR_BUTTON_DEFAULT)) option = 0; - ui_widget(ctx, 35, 22); - if (zr_button_text_symbol(ctx, (option == 1)?ZR_SYMBOL_CIRCLE_FILLED:ZR_SYMBOL_CIRCLE, - "Select", ZR_TEXT_LEFT, ZR_BUTTON_DEFAULT)) option = 1; - ui_widget(ctx, 35, 22); - if (zr_button_text_symbol(ctx, (option == 2)?ZR_SYMBOL_CIRCLE_FILLED:ZR_SYMBOL_CIRCLE, - "Select", ZR_TEXT_LEFT, ZR_BUTTON_DEFAULT)) option = 2; - - /*------------------------------------------------ - * CONTEXTUAL - *------------------------------------------------*/ - if (zr_contextual_begin(ctx, &menu, ZR_WINDOW_NO_SCROLLBAR, zr_vec2(120, 200), zr_window_get_bounds(ctx))) { - ctx->style.font.height = 18; - zr_layout_row_dynamic(ctx, 25, 1); - if (zr_contextual_item_icon(ctx, img->copy, "Clone", ZR_TEXT_RIGHT)) - fprintf(stdout, "pressed clone!\n"); - if (zr_contextual_item_icon(ctx, img->del, "Delete", ZR_TEXT_RIGHT)) - fprintf(stdout, "pressed delete!\n"); - if (zr_contextual_item_icon(ctx, img->convert, "Convert", ZR_TEXT_RIGHT)) - fprintf(stdout, "pressed convert!\n"); - if (zr_contextual_item_icon(ctx, img->edit, "Edit", ZR_TEXT_RIGHT)) - fprintf(stdout, "pressed edit!\n"); - zr_contextual_end(ctx); - } - ctx->style.font.height = 14; - zr_end(ctx); -} - -/* =============================================================== - * - * BASIC DEMO - * - * ===============================================================*/ -static void -basic_demo(struct zr_context *ctx, struct icons *img) -{ - static int image_active; - static int check0 = 1; - static int check1 = 0; - static int slider = 30; - static size_t prog = 80; - static int selected_item = 0; - static int selected_image = 3; - static int selected_icon = 0; - static const char *items[] = {"Item 0","item 1","item 2"}; - static int piemenu_active = 0; - - int i = 0; - struct zr_panel layout; - struct zr_panel combo; - ctx->style.font.height = 20; - zr_begin(ctx, &layout, "Basic Demo", zr_rect(320, 50, 275, 610), - ZR_WINDOW_BORDER|ZR_WINDOW_MOVABLE|ZR_WINDOW_BORDER_HEADER|ZR_WINDOW_TITLE); - - /*------------------------------------------------ - * POPUP BUTTON - *------------------------------------------------*/ - ui_header(ctx, "Popup & Scrollbar & Images"); - ui_widget(ctx, 35, 22); - if (zr_button_text_image(ctx, img->directory, - "Images", ZR_TEXT_CENTERED, ZR_BUTTON_DEFAULT)) - image_active = !image_active; - - /*------------------------------------------------ - * SELECTED IMAGE - *------------------------------------------------*/ - ui_header(ctx, "Selected Image"); - ui_widget_centered(ctx, 100, 22); - zr_image(ctx, img->images[selected_image]); - - /*------------------------------------------------ - * IMAGE POPUP - *------------------------------------------------*/ - if (image_active) { - struct zr_panel popup; - if (zr_popup_begin(ctx, &popup, ZR_POPUP_STATIC, "Image Popup", 0, zr_rect(265, 0, 320, 220))) { - zr_layout_row_static(ctx, 82, 82, 3); - for (i = 0; i < 9; ++i) { - if (zr_button_image(ctx, img->images[i], ZR_BUTTON_DEFAULT)) { - selected_image = i; - image_active = 0; - zr_popup_close(ctx); - } - } - zr_popup_end(ctx); - } - } - /*------------------------------------------------ - * COMBOBOX - *------------------------------------------------*/ - ui_header(ctx, "Combo box"); - ui_widget(ctx, 40, 22); - if (zr_combo_begin_text(ctx, &combo, items[selected_item], 200)) { - zr_layout_row_dynamic(ctx, 35, 1); - for (i = 0; i < 3; ++i) - if (zr_combo_item(ctx, items[i], ZR_TEXT_LEFT)) - selected_item = i; - zr_combo_end(ctx); - } - - ui_widget(ctx, 40, 22); - if (zr_combo_begin_icon(ctx, &combo, items[selected_icon], img->images[selected_icon], 200)) { - zr_layout_row_dynamic(ctx, 35, 1); - for (i = 0; i < 3; ++i) - if (zr_combo_item_icon(ctx, img->images[i], items[i], ZR_TEXT_RIGHT)) - selected_icon = i; - zr_combo_end(ctx); - } - - /*------------------------------------------------ - * CHECKBOX - *------------------------------------------------*/ - ui_header(ctx, "Checkbox"); - ui_widget(ctx, 30, 22); - zr_checkbox(ctx, "Flag 1", &check0); - ui_widget(ctx, 30, 22); - zr_checkbox(ctx, "Flag 2", &check1); - - /*------------------------------------------------ - * PROGRESSBAR - *------------------------------------------------*/ - ui_header(ctx, "Progressbar"); - ui_widget(ctx, 35, 22); - zr_progress(ctx, &prog, 100, zr_true); - - /*------------------------------------------------ - * SLIDER - *------------------------------------------------*/ - ui_header(ctx, "Slider"); - ui_widget(ctx, 35, 22); - zr_slider_int(ctx, 0, &slider, 100, 10); - - /*------------------------------------------------ - * PIEMENU - *------------------------------------------------*/ - if (zr_input_is_mouse_down(&ctx->input, ZR_BUTTON_RIGHT) && - zr_input_is_mouse_hovering_rect(&ctx->input, layout.bounds)) - piemenu_active = 1; - - if (piemenu_active) { - int ret = ui_piemenu(ctx, zr_vec2(WINDOW_WIDTH/2-140, WINDOW_HEIGHT/2-140), 140, &img->menu[0], 6); - if (ret != -1) { - fprintf(stdout, "piemenu selected: %d\n", ret); - piemenu_active = 0; - } - } - ctx->style.font.height = 14; - zr_end(ctx); -} - - diff --git a/demo/nodedit.c b/demo/nodedit.c deleted file mode 100644 index b956aad8..00000000 --- a/demo/nodedit.c +++ /dev/null @@ -1,321 +0,0 @@ -struct node { - int ID; - char name[32]; - struct zr_rect bounds; - float value; - struct zr_color color; - int input_count; - int output_count; - struct node *next; - struct node *prev; -}; - -struct node_link { - int input_id; - int input_slot; - int output_id; - int output_slot; - struct zr_vec2 in; - struct zr_vec2 out; -}; - -struct node_linking { - int active; - struct node *node; - int input_id; - int input_slot; -}; - -struct node_editor { - struct node node_buf[32]; - struct node_link links[64]; - struct node *begin; - struct node *end; - int node_count; - int link_count; - struct zr_rect bounds; - struct node *selected; - int show_grid; - struct zr_vec2 scrolling; - struct node_linking linking; -}; - -static void -node_editor_push(struct node_editor *editor, struct node *node) -{ - if (!editor->begin) { - node->next = NULL; - node->prev = NULL; - editor->begin = node; - editor->end = node; - } else { - node->prev = editor->end; - if (editor->end) - editor->end->next = node; - node->next = NULL; - editor->end = node; - } -} - -static void -node_editor_pop(struct node_editor *editor, struct node *node) -{ - if (node->next) - node->next->prev = node->prev; - if (node->prev) - node->prev->next = node->next; - if (editor->end == node) - editor->end = node->prev; - if (editor->begin == node) - editor->begin = node->next; - node->next = NULL; - node->prev = NULL; -} - -static struct node* -node_editor_find(struct node_editor *editor, int ID) -{ - struct node *iter = editor->begin; - while (iter) { - if (iter->ID == ID) - return iter; - iter = iter->next; - } - return NULL; -} - -static void -node_editor_add(struct node_editor *editor, const char *name, struct zr_rect bounds, - struct zr_color col, int in_count, int out_count) -{ - static int IDs = 0; - struct node *node; - assert((zr_size)editor->node_count < LEN(editor->node_buf)); - node = &editor->node_buf[editor->node_count++]; - node->ID = IDs++; - node->value = 0; - node->color = zr_rgb(255, 0, 0); - node->input_count = in_count; - node->output_count = out_count; - node->color = col; - strcpy(node->name, name); - node->bounds = bounds; - node_editor_push(editor, node); -} - -static void -node_editor_link(struct node_editor *editor, int in_id, int in_slot, - int out_id, int out_slot) -{ - struct node_link *link; - assert((zr_size)editor->link_count < LEN(editor->links)); - link = &editor->links[editor->link_count++]; - link->input_id = in_id; - link->input_slot = in_slot; - link->output_id = out_id; - link->output_slot = out_slot; -} - -static void -node_editor_demo(struct zr_context *ctx, struct node_editor *nodedit) -{ - int n = 0; - struct zr_rect total_space; - const struct zr_input *in = &ctx->input; - struct zr_command_buffer *canvas; - struct node *updated = 0; - struct zr_panel layout; - - if (zr_begin(ctx, &layout, "Node Editor", zr_rect(50, 50, 650, 650), - ZR_WINDOW_BORDER|ZR_WINDOW_NO_SCROLLBAR|ZR_WINDOW_CLOSABLE|ZR_WINDOW_MOVABLE)) - { - /* allocate complete window space */ - canvas = zr_window_get_canvas(ctx); - total_space = zr_window_get_content_region(ctx); - zr_layout_space_begin(ctx, ZR_STATIC, total_space.h, nodedit->node_count); - { - struct zr_panel node, menu; - struct node *it = nodedit->begin; - struct zr_rect size = zr_layout_space_bounds(ctx); - - if (nodedit->show_grid) { - /* display grid */ - float x, y; - const float grid_size = 32.0f; - const struct zr_color grid_color = zr_rgb(50, 50, 50); - for (x = (float)fmod(size.x - nodedit->scrolling.x, grid_size); x < size.w; x += grid_size) - zr_draw_line(canvas, x+size.x, size.y, x+size.x, size.y+size.h, grid_color); - for (y = (float)fmod(size.y - nodedit->scrolling.y, grid_size); y < size.h; y += grid_size) - zr_draw_line(canvas, size.x, y+size.y, size.x+size.w, y+size.y, grid_color); - } - - /* execute each node as a moveable group */ - while (it) { - /* calculate scrolled node window position and size */ - zr_layout_space_push(ctx, zr_rect(it->bounds.x - nodedit->scrolling.x, - it->bounds.y - nodedit->scrolling.y, it->bounds.w, it->bounds.h)); - - /* execute node window */ - if (zr_group_begin(ctx, &node, it->name, ZR_WINDOW_MOVABLE|ZR_WINDOW_NO_SCROLLBAR|ZR_WINDOW_BORDER|ZR_WINDOW_TITLE)) - { - /* always have last selected node on top */ - if (zr_input_mouse_clicked(in, ZR_BUTTON_LEFT, node.bounds) && - (!(it->prev && zr_input_mouse_clicked(in, ZR_BUTTON_LEFT, - zr_layout_space_rect_to_screen(ctx, node.bounds)))) && - nodedit->end != it) - { - updated = it; - } - - /* ================= NODE CONTENT =====================*/ - zr_layout_row_dynamic(ctx, 25, 1); - zr_button_color(ctx, it->color, ZR_BUTTON_DEFAULT); - it->color.r = (zr_byte)zr_propertyi(ctx, "#R:", 0, it->color.r, 255, 1,1); - it->color.g = (zr_byte)zr_propertyi(ctx, "#G:", 0, it->color.g, 255, 1,1); - it->color.b = (zr_byte)zr_propertyi(ctx, "#B:", 0, it->color.b, 255, 1,1); - it->color.a = (zr_byte)zr_propertyi(ctx, "#A:", 0, it->color.a, 255, 1,1); - /* ====================================================*/ - zr_group_end(ctx); - } - { - /* node connector and linking */ - float space; - struct zr_rect bounds; - bounds = zr_layout_space_rect_to_local(ctx, node.bounds); - bounds.x += nodedit->scrolling.x; - bounds.y += nodedit->scrolling.y; - it->bounds = bounds; - - /* output connector */ - space = node.bounds.h / (float)((it->output_count) + 1); - for (n = 0; n < it->output_count; ++n) { - struct zr_rect circle; - circle.x = node.bounds.x + node.bounds.w-4; - circle.y = node.bounds.y + space * (float)(n+1); - circle.w = 8; circle.h = 8; - zr_draw_circle(canvas, circle, zr_rgb(100, 100, 100)); - - /* start linking process */ - if (zr_input_has_mouse_click_down_in_rect(in, ZR_BUTTON_LEFT, circle, zr_true)) { - nodedit->linking.active = zr_true; - nodedit->linking.node = it; - nodedit->linking.input_id = it->ID; - nodedit->linking.input_slot = n; - } - - /* draw curve from linked node slot to mouse position */ - if (nodedit->linking.active && nodedit->linking.node == it && - nodedit->linking.input_slot == n) { - struct zr_vec2 l0 = zr_vec2(circle.x + 3, circle.y + 3); - struct zr_vec2 l1 = in->mouse.pos; - zr_draw_curve(canvas, l0.x, l0.y, l0.x + 50.0f, l0.y, - l1.x - 50.0f, l1.y, l1.x, l1.y, zr_rgb(100, 100, 100)); - } - } - - /* input connector */ - space = node.bounds.h / (float)((it->input_count) + 1); - for (n = 0; n < it->input_count; ++n) { - struct zr_rect circle; - circle.x = node.bounds.x-4; - circle.y = node.bounds.y + space * (float)(n+1); - circle.w = 8; circle.h = 8; - zr_draw_circle(canvas, circle, zr_rgb(100, 100, 100)); - if (zr_input_is_mouse_released(in, ZR_BUTTON_LEFT) && - zr_input_is_mouse_hovering_rect(in, circle) && - nodedit->linking.active && nodedit->linking.node != it) { - nodedit->linking.active = zr_false; - node_editor_link(nodedit, nodedit->linking.input_id, - nodedit->linking.input_slot, it->ID, n); - } - } - } - it = it->next; - } - - /* reset linking connection */ - if (nodedit->linking.active && zr_input_is_mouse_released(in, ZR_BUTTON_LEFT)) { - nodedit->linking.active = zr_false; - nodedit->linking.node = NULL; - fprintf(stdout, "linking failed\n"); - } - - /* draw each link */ - for (n = 0; n < nodedit->link_count; ++n) { - struct node_link *link = &nodedit->links[n]; - struct node *ni = node_editor_find(nodedit, link->input_id); - struct node *no = node_editor_find(nodedit, link->output_id); - float spacei = node.bounds.h / (float)((ni->output_count) + 1); - float spaceo = node.bounds.h / (float)((no->input_count) + 1); - struct zr_vec2 l0 = zr_layout_space_to_screen(ctx, - zr_vec2(ni->bounds.x + ni->bounds.w, 3.0f + ni->bounds.y + spacei * (float)(link->input_slot+1))); - struct zr_vec2 l1 = zr_layout_space_to_screen(ctx, - zr_vec2(no->bounds.x, 3.0f + no->bounds.y + spaceo * (float)(link->output_slot+1))); - - l0.x -= nodedit->scrolling.x; - l0.y -= nodedit->scrolling.y; - l1.x -= nodedit->scrolling.x; - l1.y -= nodedit->scrolling.y; - zr_draw_curve(canvas, l0.x, l0.y, l0.x + 50.0f, l0.y, - l1.x - 50.0f, l1.y, l1.x, l1.y, zr_rgb(100, 100, 100)); - } - - if (updated) { - /* reshuffle nodes to have last recently selected node on top */ - node_editor_pop(nodedit, updated); - node_editor_push(nodedit, updated); - } - - /* node selection */ - if (zr_input_mouse_clicked(in, ZR_BUTTON_LEFT, zr_layout_space_bounds(ctx))) { - it = nodedit->begin; - nodedit->selected = NULL; - nodedit->bounds = zr_rect(in->mouse.pos.x, in->mouse.pos.y, 100, 200); - while (it) { - struct zr_rect b = zr_layout_space_rect_to_screen(ctx, it->bounds); - b.x -= nodedit->scrolling.x; - b.y -= nodedit->scrolling.y; - if (zr_input_is_mouse_hovering_rect(in, b)) - nodedit->selected = it; - it = it->next; - } - } - - /* contextual menu */ - if (zr_contextual_begin(ctx, &menu, 0, zr_vec2(100, 220), zr_window_get_bounds(ctx))) { - const char *grid_option[] = {"Show Grid", "Hide Grid"}; - zr_layout_row_dynamic(ctx, 25, 1); - if (zr_contextual_item(ctx, "New", ZR_TEXT_CENTERED)) - node_editor_add(nodedit, "New", zr_rect(400, 260, 180, 220), - zr_rgb(255, 255, 255), 1, 2); - if (zr_contextual_item(ctx, grid_option[nodedit->show_grid],ZR_TEXT_CENTERED)) - nodedit->show_grid = !nodedit->show_grid; - zr_contextual_end(ctx); - } - } - zr_layout_space_end(ctx); - - /* window content scrolling */ - if (zr_input_is_mouse_hovering_rect(in, zr_window_get_bounds(ctx)) && - zr_input_is_mouse_down(in, ZR_BUTTON_MIDDLE)) { - nodedit->scrolling.x += in->mouse.delta.x; - nodedit->scrolling.y += in->mouse.delta.y; - } - } - zr_end(ctx); -} - -static void -node_editor_init(struct node_editor *editor) -{ - memset(editor, 0, sizeof(*editor)); - editor->begin = NULL; - editor->end = NULL; - node_editor_add(editor, "Source", zr_rect(40, 10, 180, 220), zr_rgb(255, 0, 0), 0, 1); - node_editor_add(editor, "Source", zr_rect(40, 260, 180, 220), zr_rgb(0, 255, 0), 0, 1); - node_editor_add(editor, "Combine", zr_rect(400, 100, 180, 220), zr_rgb(0,0,255), 2, 2); - node_editor_link(editor, 0, 0, 2, 0); - node_editor_link(editor, 1, 0, 2, 1); - editor->show_grid = zr_true; -} - diff --git a/demo/overview.c b/demo/overview.c deleted file mode 100644 index 18eccf26..00000000 --- a/demo/overview.c +++ /dev/null @@ -1,973 +0,0 @@ -/* =============================================================== - * - * DEMO - * - * ===============================================================*/ -static void -demo_window(struct zr_context *ctx) -{ - struct zr_panel menu; - - /* window flags */ - static int show_menu = zr_true; - static int titlebar = zr_true; - static int border = zr_true; - static int resize = zr_true; - static int moveable = zr_true; - static int no_scrollbar = zr_false; - static zr_flags window_flags = 0; - static int minimizable = zr_true; - static int close = zr_true; - - /* popups */ - static enum zr_style_header_align header_align = ZR_HEADER_RIGHT; - static int show_app_about = zr_false; - struct zr_panel layout; - - /* window flags */ - window_flags = 0; - ctx->style.header.align = header_align; - if (border) window_flags |= ZR_WINDOW_BORDER; - if (resize) window_flags |= ZR_WINDOW_SCALABLE; - if (moveable) window_flags |= ZR_WINDOW_MOVABLE; - if (no_scrollbar) window_flags |= ZR_WINDOW_NO_SCROLLBAR; - if (minimizable) window_flags |= ZR_WINDOW_MINIMIZABLE; - if (close) window_flags |= ZR_WINDOW_CLOSABLE; - - if (zr_begin(ctx, &layout, "Demo", zr_rect(10, 10, 400, 750), window_flags)) - { - if (show_menu) - { - /* menubar */ - enum menu_states {MENU_DEFAULT, MENU_TEST}; - static zr_size mprog = 60; - static int mslider = 10; - static int mcheck = zr_true; - - zr_menubar_begin(ctx); - zr_layout_row_begin(ctx, ZR_STATIC, 25, 2); - zr_layout_row_push(ctx, 45); - if (zr_menu_text_begin(ctx, &menu, "MENU", 120)) - { - static size_t prog = 40; - static int slider = 10; - static int check = zr_true; - - zr_layout_row_dynamic(ctx, 25, 1); - zr_progress(ctx, &prog, 100, ZR_MODIFIABLE); - zr_slider_int(ctx, 0, &slider, 16, 1); - zr_checkbox(ctx, "check", &check); - - if (zr_menu_item(ctx, ZR_TEXT_CENTERED, "Hide")) - show_menu = zr_false; - if (zr_menu_item(ctx, ZR_TEXT_CENTERED, "About")) - show_app_about = zr_true; - zr_menu_end(ctx); - } - zr_layout_row_push(ctx, 60); - zr_progress(ctx, &mprog, 100, ZR_MODIFIABLE); - zr_slider_int(ctx, 0, &mslider, 16, 1); - zr_checkbox(ctx, "check", &mcheck); - zr_menubar_end(ctx); - } - - if (show_app_about) - { - /* about popup */ - struct zr_panel popup; - static struct zr_rect s = {20, 100, 300, 190}; - if (zr_popup_begin(ctx, &popup, ZR_POPUP_STATIC, "About", ZR_WINDOW_CLOSABLE, s)) - { - zr_layout_row_dynamic(ctx, 20, 1); - zr_label(ctx, "Zahnrad", ZR_TEXT_LEFT); - zr_label(ctx, "By Micha Mettke", ZR_TEXT_LEFT); - zr_label(ctx, "Zahnrad is licensed under the zlib License.", ZR_TEXT_LEFT); - zr_label(ctx, "See LICENSE for more information", ZR_TEXT_LEFT); - zr_popup_end(ctx); - } else show_app_about = zr_false; - } - - /* window flags */ - if (zr_layout_push(ctx, ZR_LAYOUT_TAB, "Window", ZR_MINIMIZED)) { - zr_layout_row_dynamic(ctx, 30, 2); - zr_checkbox(ctx, "Titlebar", &titlebar); - zr_checkbox(ctx, "Menu", &show_menu); - zr_checkbox(ctx, "Border", &border); - zr_checkbox(ctx, "Resizable", &resize); - zr_checkbox(ctx, "Moveable", &moveable); - zr_checkbox(ctx, "No Scrollbar", &no_scrollbar); - zr_checkbox(ctx, "Minimizable", &minimizable); - zr_checkbox(ctx, "Closeable", &close); - zr_layout_pop(ctx); - } - - if (zr_layout_push(ctx, ZR_LAYOUT_TAB, "Widgets", ZR_MINIMIZED)) - { - enum options {A,B,C}; - static int checkbox; - static int option; - if (zr_layout_push(ctx, ZR_LAYOUT_NODE, "Text", ZR_MINIMIZED)) - { - /* Text Widgets */ - zr_layout_row_dynamic(ctx, 20, 1); - zr_label(ctx, "Label aligned left", ZR_TEXT_LEFT); - zr_label(ctx, "Label aligned centered", ZR_TEXT_CENTERED); - zr_label(ctx, "Label aligned right", ZR_TEXT_RIGHT); - zr_label_colored(ctx, "Blue text", ZR_TEXT_LEFT, zr_rgb(0,0,255)); - zr_label_colored(ctx, "Yellow text", ZR_TEXT_LEFT, zr_rgb(255,255,0)); - zr_text(ctx, "Text without /0", 15, ZR_TEXT_RIGHT); - - zr_layout_row_static(ctx, 100, 200, 1); - zr_label_wrap(ctx, "This is a very long line to hopefully get this text to be wrapped into multiple lines to show line wrapping"); - zr_layout_row_dynamic(ctx, 100, 1); - zr_label_wrap(ctx, "This is another long text to show dynamic window changes on multiline text"); - zr_layout_pop(ctx); - } - - if (zr_layout_push(ctx, ZR_LAYOUT_NODE, "Button", ZR_MINIMIZED)) - { - /* Buttons Widgets */ - zr_layout_row_static(ctx, 30, 100, 3); - if (zr_button_text(ctx, "Button", ZR_BUTTON_DEFAULT)) - fprintf(stdout, "Button pressed!\n"); - if (zr_button_text(ctx, "Repeater", ZR_BUTTON_REPEATER)) - fprintf(stdout, "Repeater is being pressed!\n"); - zr_button_color(ctx, zr_rgb(0,0,255), ZR_BUTTON_DEFAULT); - - zr_layout_row_static(ctx, 20, 20, 8); - zr_button_symbol(ctx, ZR_SYMBOL_CIRCLE, ZR_BUTTON_DEFAULT); - zr_button_symbol(ctx, ZR_SYMBOL_CIRCLE_FILLED, ZR_BUTTON_DEFAULT); - zr_button_symbol(ctx, ZR_SYMBOL_RECT, ZR_BUTTON_DEFAULT); - zr_button_symbol(ctx, ZR_SYMBOL_RECT_FILLED, ZR_BUTTON_DEFAULT); - zr_button_symbol(ctx, ZR_SYMBOL_TRIANGLE_UP, ZR_BUTTON_DEFAULT); - zr_button_symbol(ctx, ZR_SYMBOL_TRIANGLE_DOWN, ZR_BUTTON_DEFAULT); - zr_button_symbol(ctx, ZR_SYMBOL_TRIANGLE_LEFT, ZR_BUTTON_DEFAULT); - zr_button_symbol(ctx, ZR_SYMBOL_TRIANGLE_RIGHT, ZR_BUTTON_DEFAULT); - - zr_layout_row_static(ctx, 30, 100, 2); - zr_button_text_symbol(ctx, ZR_SYMBOL_TRIANGLE_LEFT, "prev", ZR_TEXT_RIGHT, ZR_BUTTON_DEFAULT); - zr_button_text_symbol(ctx, ZR_SYMBOL_TRIANGLE_RIGHT, "next", ZR_TEXT_LEFT, ZR_BUTTON_DEFAULT); - zr_layout_pop(ctx); - } - - if (zr_layout_push(ctx, ZR_LAYOUT_NODE, "Basic", ZR_MINIMIZED)) - { - /* Basic widgets */ - static int int_slider = 5; - static float float_slider = 2.5f; - static size_t prog_value = 40; - static float property_float = 2; - static int property_int = 10; - static int property_neg = 10; - - static float range_float_min = 0; - static float range_float_max = 100; - static float range_float_value = 50; - static int range_int_min = 0; - static int range_int_value = 2048; - static int range_int_max = 4096; - static const float ratio[] = {120, 150}; - - zr_layout_row_static(ctx, 30, 100, 1); - zr_checkbox(ctx, "Checkbox", &checkbox); - - zr_layout_row_static(ctx, 30, 80, 3); - option = zr_option(ctx, "optionA", option == A) ? A : option; - option = zr_option(ctx, "optionB", option == B) ? B : option; - option = zr_option(ctx, "optionC", option == C) ? C : option; - - - zr_layout_row(ctx, ZR_STATIC, 30, 2, ratio); - zr_labelf(ctx, ZR_TEXT_LEFT, "Slider int"); - zr_slider_int(ctx, 0, &int_slider, 10, 1); - - zr_label(ctx, "Slider float", ZR_TEXT_LEFT); - zr_slider_float(ctx, 0, &float_slider, 5.0, 0.5f); - zr_labelf(ctx, ZR_TEXT_LEFT, "Progressbar" , prog_value); - zr_progress(ctx, &prog_value, 100, ZR_MODIFIABLE); - - zr_layout_row(ctx, ZR_STATIC, 25, 2, ratio); - zr_label(ctx, "Property float:", ZR_TEXT_LEFT); - zr_property_float(ctx, "Float:", 0, &property_float, 64.0f, 0.1f, 0.2f); - zr_label(ctx, "Property int:", ZR_TEXT_LEFT); - zr_property_int(ctx, "Int:", 0, &property_int, 100.0f, 1, 1); - zr_label(ctx, "Property neg:", ZR_TEXT_LEFT); - zr_property_int(ctx, "Neg:", -10, &property_neg, 10, 1, 1); - - - zr_layout_row_dynamic(ctx, 25, 1); - zr_label(ctx, "Range:", ZR_TEXT_LEFT); - zr_layout_row_dynamic(ctx, 25, 3); - zr_property_float(ctx, "#min:", 0, &range_float_min, range_float_max, 1.0f, 0.2f); - zr_property_float(ctx, "#float:", range_float_min, &range_float_value, range_float_max, 1.0f, 0.2f); - zr_property_float(ctx, "#max:", range_float_min, &range_float_max, 100, 1.0f, 0.2f); - - zr_property_int(ctx, "#min:", INT_MIN, &range_int_min, range_int_max, 1, 10); - zr_property_int(ctx, "#neg:", range_int_min, &range_int_value, range_int_max, 1, 10); - zr_property_int(ctx, "#max:", range_int_min, &range_int_max, INT_MAX, 1, 10); - - zr_layout_pop(ctx); - } - - if (zr_layout_push(ctx, ZR_LAYOUT_NODE, "Selectable", ZR_MINIMIZED)) - { - if (zr_layout_push(ctx, ZR_LAYOUT_NODE, "List", ZR_MINIMIZED)) - { - static int selected[4] = {zr_false, zr_false, zr_true, zr_false}; - zr_layout_row_static(ctx, 18, 100, 1); - zr_selectable(ctx, "Selectable", ZR_TEXT_LEFT, &selected[0]); - zr_selectable(ctx, "Selectable", ZR_TEXT_LEFT, &selected[1]); - zr_label(ctx, "Not Selectable", ZR_TEXT_LEFT); - zr_selectable(ctx, "Selectable", ZR_TEXT_LEFT, &selected[2]); - zr_selectable(ctx, "Selectable", ZR_TEXT_LEFT, &selected[3]); - zr_layout_pop(ctx); - } - if (zr_layout_push(ctx, ZR_LAYOUT_NODE, "Grid", ZR_MINIMIZED)) - { - int i; - static int selected[16] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1}; - zr_layout_row_static(ctx, 50, 50, 4); - for (i = 0; i < 16; ++i) { - if (zr_selectable(ctx, "Z", ZR_TEXT_CENTERED, &selected[i])) { - int x = (i % 4), y = i / 4; - if (x > 0) selected[i - 1] ^= 1; - if (x < 3) selected[i + 1] ^= 1; - if (y > 0) selected[i - 4] ^= 1; - if (y < 3) selected[i + 4] ^= 1; - } - } - zr_layout_pop(ctx); - } - zr_layout_pop(ctx); - } - - if (zr_layout_push(ctx, ZR_LAYOUT_NODE, "Combo", ZR_MINIMIZED)) - { - /* Combobox Widgets */ - enum color_mode {COL_RGB, COL_HSV}; - static float chart_selection = 8.0f; - static const char *weapons[] = {"Fist","Pistol","Shotgun","Plasma","BFG"}; - static size_t current_weapon = 0; - static int check_values[5]; - static float position[3]; - static int col_mode = COL_RGB; - static struct zr_color combo_color = {130, 50, 50, 255}; - static struct zr_color combo_color2 = {130, 180, 50, 255}; - static size_t x = 20, y = 40, z = 10, w = 90; - - char buffer[64]; - size_t sum = 0; - struct zr_panel combo; - - /* default combobox */ - zr_layout_row_static(ctx, 25, 200, 1); - if (zr_combo_begin_text(ctx, &combo, weapons[current_weapon], 200)) { - size_t i = 0; - zr_layout_row_dynamic(ctx, 25, 1); - for (i = 0; i < LEN(weapons); ++i) { - if (zr_combo_item(ctx, weapons[i], ZR_TEXT_LEFT)) - current_weapon = i; - } - zr_combo_end(ctx); - } - - /* slider color combobox */ - if (zr_combo_begin_color(ctx, &combo, combo_color, 200)) { - float ratios[] = {0.15f, 0.85f}; - zr_layout_row(ctx, ZR_DYNAMIC, 30, 2, ratios); - zr_label(ctx, "R:", ZR_TEXT_LEFT); - combo_color.r = (zr_byte)zr_slide_int(ctx, 0, combo_color.r, 255, 5); - zr_label(ctx, "G:", ZR_TEXT_LEFT); - combo_color.g = (zr_byte)zr_slide_int(ctx, 0, combo_color.g, 255, 5); - zr_label(ctx, "B:", ZR_TEXT_LEFT); - combo_color.b = (zr_byte)zr_slide_int(ctx, 0, combo_color.b, 255, 5); - zr_label(ctx, "A:", ZR_TEXT_LEFT); - combo_color.a = (zr_byte)zr_slide_int(ctx, 0, combo_color.a , 255, 5); - zr_combo_end(ctx); - } - - /* property color combobox */ - if (zr_combo_begin_color(ctx, &combo, combo_color2, 200)) { - zr_layout_row_dynamic(ctx, 25, 2); - col_mode = zr_option(ctx, "RGB", col_mode == COL_RGB) ? COL_RGB : col_mode; - col_mode = zr_option(ctx, "HSV", col_mode == COL_HSV) ? COL_HSV : col_mode; - zr_layout_row_dynamic(ctx, 25, 1); - if (col_mode == COL_RGB) { - combo_color2.r = (zr_byte)zr_propertyi(ctx, "#R:", 0, combo_color2.r, 255, 1,1); - combo_color2.g = (zr_byte)zr_propertyi(ctx, "#G:", 0, combo_color2.g, 255, 1,1); - combo_color2.b = (zr_byte)zr_propertyi(ctx, "#B:", 0, combo_color2.b, 255, 1,1); - combo_color2.a = (zr_byte)zr_propertyi(ctx, "#A:", 0, combo_color2.a, 255, 1,1); - } else { - zr_byte tmp[4]; - zr_color_hsva_bv(tmp, combo_color2); - tmp[0] = (zr_byte)zr_propertyi(ctx, "#H:", 0, tmp[0], 255, 1,1); - tmp[1] = (zr_byte)zr_propertyi(ctx, "#S:", 0, tmp[1], 255, 1,1); - tmp[2] = (zr_byte)zr_propertyi(ctx, "#V:", 0, tmp[2], 255, 1,1); - tmp[3] = (zr_byte)zr_propertyi(ctx, "#A:", 0, tmp[3], 255, 1,1); - combo_color2 = zr_hsva_bv(tmp); - } - zr_combo_end(ctx); - } - - /* progressbar combobox */ - sum = x + y + z + w; - sprintf(buffer, "%lu", sum); - if (zr_combo_begin_text(ctx, &combo, buffer, 200)) { - zr_layout_row_dynamic(ctx, 30, 1); - zr_progress(ctx, &x, 100, ZR_MODIFIABLE); - zr_progress(ctx, &y, 100, ZR_MODIFIABLE); - zr_progress(ctx, &z, 100, ZR_MODIFIABLE); - zr_progress(ctx, &w, 100, ZR_MODIFIABLE); - zr_combo_end(ctx); - } - - /* checkbox combobox */ - sum = (size_t)(check_values[0] + check_values[1] + check_values[2] + check_values[3] + check_values[4]); - sprintf(buffer, "%lu", sum); - if (zr_combo_begin_text(ctx, &combo, buffer, 200)) { - zr_layout_row_dynamic(ctx, 30, 1); - zr_checkbox(ctx, weapons[0], &check_values[0]); - zr_checkbox(ctx, weapons[1], &check_values[1]); - zr_checkbox(ctx, weapons[2], &check_values[2]); - zr_checkbox(ctx, weapons[3], &check_values[3]); - zr_combo_end(ctx); - } - - /* complex text combobox */ - sprintf(buffer, "%.2f, %.2f, %.2f", position[0], position[1],position[2]); - if (zr_combo_begin_text(ctx, &combo, buffer, 200)) { - zr_layout_row_dynamic(ctx, 25, 1); - zr_property_float(ctx, "#X:", -1024.0f, &position[0], 1024.0f, 1,0.5f); - zr_property_float(ctx, "#Y:", -1024.0f, &position[1], 1024.0f, 1,0.5f); - zr_property_float(ctx, "#Z:", -1024.0f, &position[2], 1024.0f, 1,0.5f); - zr_combo_end(ctx); - } - - /* chart combobox */ - sprintf(buffer, "%.1f", chart_selection); - if (zr_combo_begin_text(ctx, &combo, buffer, 250)) { - size_t i = 0; - static const float values[]={30.0f,15.0f,25.0f,10.0f,20.0f,40.0f}; - zr_layout_row_dynamic(ctx, 150, 1); - zr_chart_begin(ctx, ZR_CHART_COLUMN, LEN(values), 0, 50); - for (i = 0; i < LEN(values); ++i) { - zr_flags res = zr_chart_push(ctx, values[i]); - if (res & ZR_CHART_CLICKED) { - chart_selection = values[i]; - zr_combo_close(ctx); - } - } - zr_chart_end(ctx); - zr_combo_end(ctx); - } - zr_layout_pop(ctx); - } - - if (zr_layout_push(ctx, ZR_LAYOUT_NODE, "Input", ZR_MINIMIZED)) - { - static const float ratio[] = {120, 150}; - static char field_buffer[64]; - static char box_buffer[512]; - static char text[9][64]; - static size_t text_len[9]; - static size_t field_len; - static size_t box_len; - zr_flags active; - - zr_layout_row(ctx, ZR_STATIC, 25, 2, ratio); - zr_label(ctx, "Default:", ZR_TEXT_LEFT); - - zr_edit_string(ctx, ZR_EDIT_CURSOR, text[0], &text_len[0], 64, zr_filter_default); - zr_label(ctx, "Int:", ZR_TEXT_LEFT); - zr_edit_string(ctx, ZR_EDIT_CURSOR, text[1], &text_len[1], 64, zr_filter_decimal); - zr_label(ctx, "Float:", ZR_TEXT_LEFT); - zr_edit_string(ctx, ZR_EDIT_CURSOR, text[2], &text_len[2], 64, zr_filter_float); - zr_label(ctx, "Hex:", ZR_TEXT_LEFT); - zr_edit_string(ctx, ZR_EDIT_CURSOR, text[4], &text_len[4], 64, zr_filter_hex); - zr_label(ctx, "Octal:", ZR_TEXT_LEFT); - zr_edit_string(ctx, ZR_EDIT_CURSOR, text[5], &text_len[5], 64, zr_filter_oct); - zr_label(ctx, "Binary:", ZR_TEXT_LEFT); - zr_edit_string(ctx, ZR_EDIT_CURSOR, text[6], &text_len[6], 64, zr_filter_binary); - - zr_label(ctx, "Password:", ZR_TEXT_LEFT); - { - size_t i = 0; - size_t old_len = text_len[8]; - char buffer[64]; - for (i = 0; i < text_len[8]; ++i) buffer[i] = '*'; - zr_edit_string(ctx, ZR_EDIT_SIMPLE, buffer, &text_len[8], 64, zr_filter_default); - if (old_len < text_len[8]) - memcpy(&text[8][old_len], &buffer[old_len], text_len[8] - old_len); - } - - zr_label(ctx, "Field:", ZR_TEXT_LEFT); - zr_edit_string(ctx, ZR_EDIT_FIELD, field_buffer, &field_len, 64, zr_filter_default); - - zr_label(ctx, "Box:", ZR_TEXT_LEFT); - zr_layout_row_static(ctx, 300, 278, 1); - zr_edit_string(ctx, ZR_EDIT_BOX, box_buffer, &box_len, 512, zr_filter_default); - - zr_layout_row(ctx, ZR_STATIC, 25, 2, ratio); - active = zr_edit_string(ctx, ZR_EDIT_FIELD|ZR_EDIT_SIGCOMIT, text[7], &text_len[7], 64, zr_filter_ascii); - if (zr_button_text(ctx, "Submit", ZR_BUTTON_DEFAULT) || - (active & ZR_EDIT_COMMITED)) - { - text[7][text_len[7]] = '\n'; - text_len[7]++; - memcpy(&box_buffer[box_len], &text[7], text_len[7]); - box_len += text_len[7]; - text_len[7] = 0; - } - zr_layout_row_end(ctx); - zr_layout_pop(ctx); - } - zr_layout_pop(ctx); - } - - if (zr_layout_push(ctx, ZR_LAYOUT_TAB, "Chart", ZR_MINIMIZED)) - { - float id = 0; - static int col_index = -1; - static int line_index = -1; - float step = (2*3.141592654f) / 32; - - int i; - int index = -1; - struct zr_rect bounds; - - /* line chart */ - id = 0; - index = -1; - zr_layout_row_dynamic(ctx, 100, 1); - bounds = zr_widget_bounds(ctx); - zr_chart_begin(ctx, ZR_CHART_LINES, 32, -1.0f, 1.0f); - for (i = 0; i < 32; ++i) { - zr_flags res = zr_chart_push(ctx, (float)cos(id)); - if (res & ZR_CHART_HOVERING) - index = (int)i; - if (res & ZR_CHART_CLICKED) - line_index = (int)i; - id += step; - } - zr_chart_end(ctx); - - if (index != -1) { - char buffer[ZR_MAX_NUMBER_BUFFER]; - float val = (float)cos((float)index*step); - sprintf(buffer, "Value: %.2f", val); - zr_tooltip(ctx, buffer); - } - if (line_index != -1) { - zr_layout_row_dynamic(ctx, 20, 1); - zr_labelf(ctx, ZR_TEXT_LEFT, "Selected value: %.2f", (float)cos((float)index*step)); - } - - /* column chart */ - zr_layout_row_dynamic(ctx, 100, 1); - bounds = zr_widget_bounds(ctx); - zr_chart_begin(ctx, ZR_CHART_COLUMN, 32, 0.0f, 1.0f); - for (i = 0; i < 32; ++i) { - zr_flags res = zr_chart_push(ctx, (float)fabs(sin(id))); - if (res & ZR_CHART_HOVERING) - index = (int)i; - if (res & ZR_CHART_CLICKED) - col_index = (int)i; - id += step; - } - zr_chart_end(ctx); - - if (index != -1) { - char buffer[ZR_MAX_NUMBER_BUFFER]; - sprintf(buffer, "Value: %.2f", (float)fabs(sin(step * (float)index))); - zr_tooltip(ctx, buffer); - } - if (col_index != -1) { - zr_layout_row_dynamic(ctx, 20, 1); - zr_labelf(ctx, ZR_TEXT_LEFT, "Selected value: %.2f", (float)fabs(sin(step * (float)col_index))); - } - zr_layout_pop(ctx); - } - - if (zr_layout_push(ctx, ZR_LAYOUT_TAB, "Popup", ZR_MINIMIZED)) - { - static struct zr_color color = {255,0,0, 255}; - static int select[4]; - static int popup_active; - const struct zr_input *in = &ctx->input; - struct zr_rect bounds; - - /* menu contextual */ - zr_layout_row_static(ctx, 30, 150, 1); - bounds = zr_widget_bounds(ctx); - zr_label(ctx, "Right click me for menu", ZR_TEXT_LEFT); - - if (zr_contextual_begin(ctx, &menu, 0, zr_vec2(100, 300), bounds)) { - static size_t prog = 40; - static int slider = 10; - - zr_layout_row_dynamic(ctx, 25, 1); - zr_checkbox(ctx, "Menu", &show_menu); - zr_progress(ctx, &prog, 100, ZR_MODIFIABLE); - zr_slider_int(ctx, 0, &slider, 16, 1); - if (zr_contextual_item(ctx, "About", ZR_TEXT_CENTERED)) - show_app_about = zr_true; - zr_selectable(ctx, select[0]?"Unselect":"Select", ZR_TEXT_LEFT, &select[0]); - zr_selectable(ctx, select[1]?"Unselect":"Select", ZR_TEXT_LEFT, &select[1]); - zr_selectable(ctx, select[2]?"Unselect":"Select", ZR_TEXT_LEFT, &select[2]); - zr_selectable(ctx, select[3]?"Unselect":"Select", ZR_TEXT_LEFT, &select[3]); - zr_contextual_end(ctx); - } - - /* color contextual */ - zr_layout_row_begin(ctx, ZR_STATIC, 30, 2); - zr_layout_row_push(ctx, 100); - zr_label(ctx, "Right Click here:", ZR_TEXT_LEFT); - zr_layout_row_push(ctx, 50); - bounds = zr_widget_bounds(ctx); - zr_button_color(ctx, color, ZR_BUTTON_DEFAULT); - zr_layout_row_end(ctx); - - if (zr_contextual_begin(ctx, &menu, 0, zr_vec2(350, 60), bounds)) { - zr_layout_row_dynamic(ctx, 30, 4); - color.r = (zr_byte)zr_propertyi(ctx, "#r", 0, color.r, 255, 1, 1); - color.g = (zr_byte)zr_propertyi(ctx, "#g", 0, color.g, 255, 1, 1); - color.b = (zr_byte)zr_propertyi(ctx, "#b", 0, color.b, 255, 1, 1); - color.a = (zr_byte)zr_propertyi(ctx, "#a", 0, color.a, 255, 1, 1); - zr_contextual_end(ctx); - } - - /* popup */ - zr_layout_row_begin(ctx, ZR_STATIC, 30, 2); - zr_layout_row_push(ctx, 100); - zr_label(ctx, "Popup:", ZR_TEXT_LEFT); - zr_layout_row_push(ctx, 50); - if (zr_button_text(ctx, "Popup", ZR_BUTTON_DEFAULT)) - popup_active = 1; - zr_layout_row_end(ctx); - - if (popup_active) - { - static struct zr_rect s = {20, 100, 220, 150}; - if (zr_popup_begin(ctx, &menu, ZR_POPUP_STATIC, "Error", ZR_WINDOW_DYNAMIC, s)) - { - zr_layout_row_dynamic(ctx, 25, 1); - zr_label(ctx, "A terrible error as occured", ZR_TEXT_LEFT); - zr_layout_row_dynamic(ctx, 25, 2); - if (zr_button_text(ctx, "OK", ZR_BUTTON_DEFAULT)) { - popup_active = 0; - zr_popup_close(ctx); - } - if (zr_button_text(ctx, "Cancel", ZR_BUTTON_DEFAULT)) { - popup_active = 0; - zr_popup_close(ctx); - } - zr_popup_end(ctx); - } else popup_active = zr_false; - } - - /* tooltip */ - zr_layout_row_static(ctx, 30, 150, 1); - bounds = zr_widget_bounds(ctx); - zr_label(ctx, "Hover me for tooltip", ZR_TEXT_LEFT); - if (zr_input_is_mouse_hovering_rect(in, bounds)) - zr_tooltip(ctx, "This is a tooltip"); - - zr_layout_pop(ctx); - } - - if (zr_layout_push(ctx, ZR_LAYOUT_TAB, "Layout", ZR_MINIMIZED)) - { - if (zr_layout_push(ctx, ZR_LAYOUT_NODE, "Widget", ZR_MINIMIZED)) - { - float ratio_two[] = {0.2f, 0.6f, 0.2f}; - float width_two[] = {100, 200, 50}; - - zr_layout_row_dynamic(ctx, 30, 1); - zr_label(ctx, "Dynamic fixed column layout with generated position and size:", ZR_TEXT_LEFT); - zr_layout_row_dynamic(ctx, 30, 3); - zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT); - - zr_layout_row_dynamic(ctx, 30, 1); - zr_label(ctx, "static fixed column layout with generated position and size:", ZR_TEXT_LEFT); - zr_layout_row_static(ctx, 30, 100, 3); - zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT); - - zr_layout_row_dynamic(ctx, 30, 1); - zr_label(ctx, "Dynamic array-based custom column layout with generated position and custom size:",ZR_TEXT_LEFT); - zr_layout_row(ctx, ZR_DYNAMIC, 30, 3, ratio_two); - zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT); - - zr_layout_row_dynamic(ctx, 30, 1); - zr_label(ctx, "Static array-based custom column layout with generated position and custom size:",ZR_TEXT_LEFT ); - zr_layout_row(ctx, ZR_STATIC, 30, 3, width_two); - zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT); - - zr_layout_row_dynamic(ctx, 30, 1); - zr_label(ctx, "Dynamic immediate mode custom column layout with generated position and custom size:",ZR_TEXT_LEFT); - zr_layout_row_begin(ctx, ZR_DYNAMIC, 30, 3); - zr_layout_row_push(ctx, 0.2f); - zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT); - zr_layout_row_push(ctx, 0.6f); - zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT); - zr_layout_row_push(ctx, 0.2f); - zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT); - zr_layout_row_end(ctx); - - zr_layout_row_dynamic(ctx, 30, 1); - zr_label(ctx, "Static immmediate mode custom column layout with generated position and custom size:", ZR_TEXT_LEFT); - zr_layout_row_begin(ctx, ZR_STATIC, 30, 3); - zr_layout_row_push(ctx, 100); - zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT); - zr_layout_row_push(ctx, 200); - zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT); - zr_layout_row_push(ctx, 50); - zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT); - zr_layout_row_end(ctx); - - zr_layout_row_dynamic(ctx, 30, 1); - zr_label(ctx, "Static free space with custom position and custom size:", ZR_TEXT_LEFT); - zr_layout_space_begin(ctx, ZR_STATIC, 120, 4); - zr_layout_space_push(ctx, zr_rect(100, 0, 100, 30)); - zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT); - zr_layout_space_push(ctx, zr_rect(0, 15, 100, 30)); - zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT); - zr_layout_space_push(ctx, zr_rect(200, 15, 100, 30)); - zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT); - zr_layout_space_push(ctx, zr_rect(100, 30, 100, 30)); - zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT); - zr_layout_space_end(ctx); - zr_layout_pop(ctx); - } - - if (zr_layout_push(ctx, ZR_LAYOUT_NODE, "Group", ZR_MINIMIZED)) - { - static int group_titlebar = zr_false; - static int group_border = zr_true; - static int group_no_scrollbar = zr_false; - static int group_width = 320; - static int group_height = 200; - struct zr_panel tab; - - zr_flags group_flags = 0; - if (group_border) group_flags |= ZR_WINDOW_BORDER; - if (group_no_scrollbar) group_flags |= ZR_WINDOW_NO_SCROLLBAR; - if (group_titlebar) group_flags |= ZR_WINDOW_TITLE; - - zr_layout_row_dynamic(ctx, 30, 3); - zr_checkbox(ctx, "Titlebar", &group_titlebar); - zr_checkbox(ctx, "Border", &group_border); - zr_checkbox(ctx, "No Scrollbar", &group_no_scrollbar); - - zr_layout_row_begin(ctx, ZR_STATIC, 22, 2); - zr_layout_row_push(ctx, 50); - zr_label(ctx, "size:", ZR_TEXT_LEFT); - zr_layout_row_push(ctx, 130); - zr_property_int(ctx, "#Width:", 100, &group_width, 500, 10, 1); - zr_layout_row_push(ctx, 130); - zr_property_int(ctx, "#Height:", 100, &group_height, 500, 10, 1); - zr_layout_row_end(ctx); - - zr_layout_row_static(ctx, (float)group_height, group_width, 2); - if (zr_group_begin(ctx, &tab, "Group", group_flags)) { - int i = 0; - static int selected[16]; - zr_layout_row_static(ctx, 18, 100, 1); - for (i = 0; i < 16; ++i) - zr_selectable(ctx, (selected[i]) ? "Selected": "Unselected", ZR_TEXT_CENTERED, &selected[i]); - zr_group_end(ctx); - } - zr_layout_pop(ctx); - } - - if (zr_layout_push(ctx, ZR_LAYOUT_NODE, "Simple", ZR_MINIMIZED)) - { - struct zr_panel tab; - zr_layout_row_dynamic(ctx, 300, 2); - if (zr_group_begin(ctx, &tab, "Group_Without_Border", 0)) { - int i = 0; - char buffer[64]; - zr_layout_row_static(ctx, 18, 150, 1); - for (i = 0; i < 64; ++i) { - sprintf(buffer, "0x%02x", i); - zr_labelf(ctx, ZR_TEXT_LEFT, "%s: scrollable region", buffer); - } - zr_group_end(ctx); - } - if (zr_group_begin(ctx, &tab, "Group_With_Border", ZR_WINDOW_BORDER)) { - int i = 0; - char buffer[64]; - zr_layout_row_dynamic(ctx, 25, 2); - for (i = 0; i < 64; ++i) { - sprintf(buffer, "%08d", ((((i%7)*10)^32))+(64+(i%2)*2)); - zr_button_text(ctx, buffer, ZR_BUTTON_DEFAULT); - } - zr_group_end(ctx); - } - zr_layout_pop(ctx); - } - - if (zr_layout_push(ctx, ZR_LAYOUT_NODE, "Complex", ZR_MINIMIZED)) - { - int i; - struct zr_panel tab; - zr_layout_space_begin(ctx, ZR_STATIC, 500, 64); - zr_layout_space_push(ctx, zr_rect(0,0,150,500)); - if (zr_group_begin(ctx, &tab, "Group_left", ZR_WINDOW_BORDER)) { - static int selected[32]; - zr_layout_row_static(ctx, 18, 100, 1); - for (i = 0; i < 32; ++i) - zr_selectable(ctx, (selected[i]) ? "Selected": "Unselected", ZR_TEXT_CENTERED, &selected[i]); - zr_group_end(ctx); - } - - zr_layout_space_push(ctx, zr_rect(160,0,150,240)); - if (zr_group_begin(ctx, &tab, "Group_top", ZR_WINDOW_BORDER)) { - zr_layout_row_dynamic(ctx, 25, 1); - zr_button_text(ctx, "#FFAA", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "#FFBB", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "#FFCC", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "#FFDD", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "#FFEE", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "#FFFF", ZR_BUTTON_DEFAULT); - zr_group_end(ctx); - } - - zr_layout_space_push(ctx, zr_rect(160,250,150,250)); - if (zr_group_begin(ctx, &tab, "Group_buttom", ZR_WINDOW_BORDER)) { - zr_layout_row_dynamic(ctx, 25, 1); - zr_button_text(ctx, "#FFAA", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "#FFBB", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "#FFCC", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "#FFDD", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "#FFEE", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "#FFFF", ZR_BUTTON_DEFAULT); - zr_group_end(ctx); - } - - zr_layout_space_push(ctx, zr_rect(320,0,150,150)); - if (zr_group_begin(ctx, &tab, "Group_right_top", ZR_WINDOW_BORDER)) { - static int selected[4]; - zr_layout_row_static(ctx, 18, 100, 1); - for (i = 0; i < 4; ++i) - zr_selectable(ctx, (selected[i]) ? "Selected": "Unselected", ZR_TEXT_CENTERED, &selected[i]); - zr_group_end(ctx); - } - - zr_layout_space_push(ctx, zr_rect(320,160,150,150)); - if (zr_group_begin(ctx, &tab, "Group_right_center", ZR_WINDOW_BORDER)) { - static int selected[4]; - zr_layout_row_static(ctx, 18, 100, 1); - for (i = 0; i < 4; ++i) - zr_selectable(ctx, (selected[i]) ? "Selected": "Unselected", ZR_TEXT_CENTERED, &selected[i]); - zr_group_end(ctx); - } - - zr_layout_space_push(ctx, zr_rect(320,320,150,150)); - if (zr_group_begin(ctx, &tab, "Group_right_bottom", ZR_WINDOW_BORDER)) { - static int selected[4]; - zr_layout_row_static(ctx, 18, 100, 1); - for (i = 0; i < 4; ++i) - zr_selectable(ctx, (selected[i]) ? "Selected": "Unselected", ZR_TEXT_CENTERED, &selected[i]); - zr_group_end(ctx); - } - zr_layout_space_end(ctx); - zr_layout_pop(ctx); - } - - if (zr_layout_push(ctx, ZR_LAYOUT_NODE, "Splitter", ZR_MINIMIZED)) - { - const struct zr_input *in = &ctx->input; - zr_layout_row_static(ctx, 20, 320, 1); - zr_label(ctx, "Use slider and spinner to change tile size", ZR_TEXT_LEFT); - zr_label(ctx, "Drag the space between tiles to change tile ratio", ZR_TEXT_LEFT); - - if (zr_layout_push(ctx, ZR_LAYOUT_NODE, "Vertical", ZR_MINIMIZED)) - { - static float a = 100, b = 100, c = 100; - struct zr_rect bounds; - struct zr_panel sub; - - float row_layout[5]; - row_layout[0] = a; - row_layout[1] = 8; - row_layout[2] = b; - row_layout[3] = 8; - row_layout[4] = c; - - /* header */ - zr_layout_row_static(ctx, 30, 100, 2); - zr_label(ctx, "left:", ZR_TEXT_LEFT); - zr_slider_float(ctx, 10.0f, &a, 200.0f, 10.0f); - - zr_label(ctx, "middle:", ZR_TEXT_LEFT); - zr_slider_float(ctx, 10.0f, &b, 200.0f, 10.0f); - - zr_label(ctx, "right:", ZR_TEXT_LEFT); - zr_slider_float(ctx, 10.0f, &c, 200.0f, 10.0f); - - /* tiles */ - zr_layout_row(ctx, ZR_STATIC, 200, 5, row_layout); - zr_push_property(ctx, ZR_PROPERTY_ITEM_SPACING, zr_vec2(0, 4)); - - /* left space */ - if (zr_group_begin(ctx, &sub, "left", ZR_WINDOW_NO_SCROLLBAR|ZR_WINDOW_BORDER|ZR_WINDOW_NO_SCROLLBAR)) { - zr_layout_row_dynamic(ctx, 25, 1); - zr_button_text(ctx, "#FFAA", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "#FFBB", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "#FFCC", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "#FFDD", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "#FFEE", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "#FFFF", ZR_BUTTON_DEFAULT); - zr_group_end(ctx); - } - - /* scaler */ - bounds = zr_widget_bounds(ctx); - zr_spacing(ctx, 1); - if ((zr_input_is_mouse_hovering_rect(in, bounds) || - zr_input_is_mouse_prev_hovering_rect(in, bounds)) && - zr_input_is_mouse_down(in, ZR_BUTTON_LEFT)) - { - a = row_layout[0] + in->mouse.delta.x; - b = row_layout[2] - in->mouse.delta.x; - } - - /* middle space */ - if (zr_group_begin(ctx, &sub, "center", ZR_WINDOW_BORDER|ZR_WINDOW_NO_SCROLLBAR)) { - zr_layout_row_dynamic(ctx, 25, 1); - zr_button_text(ctx, "#FFAA", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "#FFBB", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "#FFCC", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "#FFDD", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "#FFEE", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "#FFFF", ZR_BUTTON_DEFAULT); - zr_group_end(ctx); - } - - /* scaler */ - bounds = zr_widget_bounds(ctx); - zr_spacing(ctx, 1); - if ((zr_input_is_mouse_hovering_rect(in, bounds) || - zr_input_is_mouse_prev_hovering_rect(in, bounds)) && - zr_input_is_mouse_down(in, ZR_BUTTON_LEFT)) - { - b = (row_layout[2] + in->mouse.delta.x); - c = (row_layout[4] - in->mouse.delta.x); - } - - /* right space */ - if (zr_group_begin(ctx, &sub, "right", ZR_WINDOW_BORDER|ZR_WINDOW_NO_SCROLLBAR)) { - zr_layout_row_dynamic(ctx, 25, 1); - zr_button_text(ctx, "#FFAA", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "#FFBB", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "#FFCC", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "#FFDD", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "#FFEE", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "#FFFF", ZR_BUTTON_DEFAULT); - zr_group_end(ctx); - } - - zr_pop_property(ctx); - zr_layout_pop(ctx); - } - - if (zr_layout_push(ctx, ZR_LAYOUT_NODE, "Horizontal", ZR_MINIMIZED)) - { - static float a = 100, b = 100, c = 100; - struct zr_panel sub; - struct zr_rect bounds; - - /* header */ - zr_layout_row_static(ctx, 30, 100, 2); - zr_label(ctx, "top:", ZR_TEXT_LEFT); - zr_slider_float(ctx, 10.0f, &a, 200.0f, 10.0f); - - zr_label(ctx, "middle:", ZR_TEXT_LEFT); - zr_slider_float(ctx, 10.0f, &b, 200.0f, 10.0f); - - zr_label(ctx, "bottom:", ZR_TEXT_LEFT); - zr_slider_float(ctx, 10.0f, &c, 200.0f, 10.0f); - - /* top space */ - zr_layout_row_dynamic(ctx, a, 1); - if (zr_group_begin(ctx, &sub, "top", ZR_WINDOW_NO_SCROLLBAR|ZR_WINDOW_BORDER)) { - zr_layout_row_dynamic(ctx, 25, 3); - zr_button_text(ctx, "#FFAA", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "#FFBB", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "#FFCC", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "#FFDD", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "#FFEE", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "#FFFF", ZR_BUTTON_DEFAULT); - zr_group_end(ctx); - } - - /* scaler */ - zr_layout_row_dynamic(ctx, 8, 1); - bounds = zr_widget_bounds(ctx); - zr_spacing(ctx, 1); - if ((zr_input_is_mouse_hovering_rect(in, bounds) || - zr_input_is_mouse_prev_hovering_rect(in, bounds)) && - zr_input_is_mouse_down(in, ZR_BUTTON_LEFT)) - { - a = a + in->mouse.delta.y; - b = b - in->mouse.delta.y; - } - - /* middle space */ - zr_layout_row_dynamic(ctx, b, 1); - if (zr_group_begin(ctx, &sub, "middle", ZR_WINDOW_NO_SCROLLBAR|ZR_WINDOW_BORDER)) { - zr_layout_row_dynamic(ctx, 25, 3); - zr_button_text(ctx, "#FFAA", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "#FFBB", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "#FFCC", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "#FFDD", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "#FFEE", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "#FFFF", ZR_BUTTON_DEFAULT); - zr_group_end(ctx); - } - - { - /* scaler */ - zr_layout_row_dynamic(ctx, 8, 1); - bounds = zr_widget_bounds(ctx); - if ((zr_input_is_mouse_hovering_rect(in, bounds) || - zr_input_is_mouse_prev_hovering_rect(in, bounds)) && - zr_input_is_mouse_down(in, ZR_BUTTON_LEFT)) - { - b = b + in->mouse.delta.y; - c = c - in->mouse.delta.y; - } - } - - /* bottom space */ - zr_layout_row_dynamic(ctx, c, 1); - if (zr_group_begin(ctx, &sub, "bottom", ZR_WINDOW_NO_SCROLLBAR|ZR_WINDOW_BORDER)) { - zr_layout_row_dynamic(ctx, 25, 3); - zr_button_text(ctx, "#FFAA", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "#FFBB", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "#FFCC", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "#FFDD", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "#FFEE", ZR_BUTTON_DEFAULT); - zr_button_text(ctx, "#FFFF", ZR_BUTTON_DEFAULT); - zr_group_end(ctx); - } - zr_layout_pop(ctx); - } - zr_layout_pop(ctx); - } - zr_layout_pop(ctx); - } - } - zr_end(ctx); -} - - - diff --git a/demo/simple.c b/demo/simple.c deleted file mode 100644 index 019c342e..00000000 --- a/demo/simple.c +++ /dev/null @@ -1,27 +0,0 @@ -static void -simple_window(struct zr_context *ctx) -{ - /* simple demo window */ - struct zr_panel layout; - if (zr_begin(ctx, &layout, "Show", zr_rect(420, 350, 200, 200), - ZR_WINDOW_BORDER|ZR_WINDOW_MOVABLE|ZR_WINDOW_SCALABLE| - ZR_WINDOW_CLOSABLE|ZR_WINDOW_MINIMIZABLE|ZR_WINDOW_TITLE)) - { - enum {EASY, HARD}; - static int op = EASY; - static int property = 20; - - zr_layout_row_static(ctx, 30, 80, 1); - if (zr_button_text(ctx, "button", ZR_BUTTON_DEFAULT)) { - /* event handling */ - } - zr_layout_row_dynamic(ctx, 30, 2); - if (zr_option(ctx, "easy", op == EASY)) op = EASY; - if (zr_option(ctx, "hard", op == HARD)) op = HARD; - - zr_layout_row_dynamic(ctx, 22, 1); - zr_property_int(ctx, "Compression:", 0, &property, 100, 10, 1); - } - zr_end(ctx); -} - diff --git a/demo/x11/xlib.c b/demo/x11/xlib.c index abda4719..0eb40fce 100644 --- a/demo/x11/xlib.c +++ b/demo/x11/xlib.c @@ -39,6 +39,8 @@ #define DEMO_DO_NOT_DRAW_IMAGES #include "../demo.c" +#if 0 + #ifdef __clang__ #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wsign-conversion" @@ -79,6 +81,8 @@ #pragma warning (pop) #endif +#endif + typedef struct XFont XFont; typedef struct XSurface XSurface; typedef struct XWindow XWindow; @@ -483,6 +487,8 @@ surface_del(XSurface *surf) free(surf); } +#if 0 + static XSurface* surface_load(const char *filename, Display *dpy, int screen, Window root) { @@ -513,6 +519,8 @@ icon_load(const char *filename, Display *dpy, int screen, Window root) return zr_image_ptr(surf); } +#endif + static void input_key(struct XWindow *xw, struct zr_context *ctx, XEvent *evt, int down) { diff --git a/zahnrad.c b/zahnrad.c index 534cdbfd..dd768b7a 100644 --- a/zahnrad.c +++ b/zahnrad.c @@ -142,6 +142,8 @@ struct zr_window { /* window size and position */ zr_flags flags; /* window flags modifing its behavior */ + zr_flags old_flags; + /* window flags from the last frame */ struct zr_scroll scrollbar; /* scrollbar x- and y-offset */ struct zr_command_buffer buffer; @@ -192,11 +194,17 @@ enum zr_stack_operation { ZR_STACK_POP = ZR_FLAG(1), ZR_STACK_INC = ZR_FLAG(2), ZR_STACK_DEC = ZR_FLAG(3), - ZR_STACK_GEN = ZR_FLAG(4) + ZR_STACK_GEN = ZR_FLAG(4), + ZR_STACK_TXT = ZR_FLAG(5) +}; + +struct zr_param_header { + unsigned short op; + unsigned short next; }; union zr_param { - unsigned int op; + struct zr_param_header h; void *ptr; const char *cheat; struct zr_color color; @@ -236,7 +244,7 @@ enum zr_param_types { ZR_OP(reset_properties, ZR_STACK_NOP, ZR_FMT("o"))\ ZR_OP(reset_font_height, ZR_STACK_NOP, ZR_FMT("o"))\ ZR_OP(reset, ZR_STACK_NOP, ZR_FMT("o"))\ - ZR_OP(begin, ZR_STACK_PUSH|ZR_STACK_INC, ZR_FMT("opcffffg"))\ + ZR_OP(begin, ZR_STACK_PUSH|ZR_STACK_INC|ZR_STACK_TXT, ZR_FMT("opcffffg"))\ ZR_OP(end, ZR_STACK_POP|ZR_STACK_DEC, ZR_FMT("o"))\ ZR_OP(layout_row_dynamic, ZR_STACK_NOP, ZR_FMT("ofi"))\ ZR_OP(layout_row_static, ZR_STACK_NOP, ZR_FMT("ofii"))\ @@ -247,30 +255,34 @@ enum zr_param_types { ZR_OP(layout_space_push, ZR_STACK_NOP, ZR_FMT("of"))\ ZR_OP(layout_space_end, ZR_STACK_NOP, ZR_FMT("o"))\ ZR_OP(layout_push, ZR_STACK_INC|ZR_STACK_GEN, ZR_FMT("ohici"))\ - ZR_OP(layout_pop, ZR_STACK_DEC, ZR_FMT("oc"))\ - ZR_OP(text, ZR_STACK_NOP, ZR_FMT("ocugl"))\ - ZR_OP(text_wrap, ZR_STACK_NOP, ZR_FMT("ocul"))\ + ZR_OP(layout_pop, ZR_STACK_DEC|ZR_STACK_TXT, ZR_FMT("oc"))\ + ZR_OP(text, ZR_STACK_NOP|ZR_STACK_TXT, ZR_FMT("ocugl"))\ + ZR_OP(text_wrap, ZR_STACK_NOP|ZR_STACK_TXT, ZR_FMT("ocul"))\ ZR_OP(image, ZR_STACK_GEN, ZR_FMT("ohmffffff"))\ - ZR_OP(button_text, ZR_STACK_GEN, ZR_FMT("ohci"))\ + ZR_OP(button_text, ZR_STACK_GEN|ZR_STACK_TXT, ZR_FMT("ohci"))\ ZR_OP(button_color, ZR_STACK_GEN, ZR_FMT("ohli"))\ ZR_OP(button_symbol, ZR_STACK_GEN, ZR_FMT("ohii"))\ ZR_OP(button_image, ZR_STACK_GEN, ZR_FMT("ohmffffffi"))\ - ZR_OP(button_text_symbol, ZR_STACK_GEN, ZR_FMT("ohici"))\ - ZR_OP(button_text_image, ZR_STACK_GEN, ZR_FMT("ohmffffffcgi"))\ - ZR_OP(check, ZR_STACK_GEN, ZR_FMT("ohi"))\ - ZR_OP(option, ZR_STACK_GEN, ZR_FMT("ohi"))\ - ZR_OP(select, ZR_STACK_GEN, ZR_FMT("ohfi"))\ + ZR_OP(button_text_symbol, ZR_STACK_GEN|ZR_STACK_TXT, ZR_FMT("ohici"))\ + ZR_OP(button_text_image, ZR_STACK_GEN|ZR_STACK_TXT, ZR_FMT("ohmffffffcgi"))\ + ZR_OP(check, ZR_STACK_GEN, ZR_FMT("ohci"))\ + ZR_OP(option, ZR_STACK_GEN, ZR_FMT("ohci"))\ + ZR_OP(select, ZR_STACK_GEN, ZR_FMT("ohcfi"))\ ZR_OP(slider, ZR_STACK_GEN, ZR_FMT("ohffff"))\ ZR_OP(progress, ZR_STACK_GEN, ZR_FMT("ohiii"))\ - ZR_OP(property, ZR_STACK_GEN, ZR_FMT("ohcfffffp"))\ - ZR_OP(group_begin, ZR_STACK_PUSH, ZR_FMT("ohpcg"))\ + ZR_OP(property, ZR_STACK_GEN|ZR_STACK_TXT, ZR_FMT("ohcfffffp"))\ + ZR_OP(group_begin, ZR_STACK_PUSH|ZR_STACK_TXT, ZR_FMT("ohpcg"))\ ZR_OP(group_end, ZR_STACK_POP, ZR_FMT("o"))\ + ZR_OP(chart_begin, ZR_STACK_NOP, ZR_FMT("ohiiff"))\ + ZR_OP(chart_push, ZR_STACK_NOP, ZR_FMT("ohf"))\ + ZR_OP(chart_end, ZR_STACK_NOP, ZR_FMT("o"))\ enum zr_opcodes { #define ZR_OPCODE(a, b, c) ZR_OP_##a, ZR_OPCODES(ZR_OPCODE) #undef ZR_OPCODE - ZR_OP_list_end + ZR_OP_list_end, + ZR_OP_MAX }; struct zr_event_queue { @@ -6002,12 +6014,6 @@ zr_widget_edit(struct zr_command_buffer *out, struct zr_rect r, * PROPERTY * * ===============================================================*/ -enum zr_property_state { - ZR_PROPERTY_DEFAULT, - ZR_PROPERTY_EDIT, - ZR_PROPERTY_DRAG -}; - struct zr_property { float border_size; struct zr_vec2 padding; @@ -7402,6 +7408,7 @@ zr_event_queue_setup(struct zr_event_queue *q, const struct zr_event_mask *mask) q->mask.flags[ZR_EVENT_SLIDER] = ZR_EVENT_SLIDER_ALL; q->mask.flags[ZR_EVENT_PROGRESS] = ZR_EVENT_PROGRESS_ALL; q->mask.flags[ZR_EVENT_PROPERTY] = ZR_EVENT_PROPERTY_ALL; + q->mask.flags[ZR_EVENT_CHART] = ZR_EVENT_CHART_ALL; } else q->mask = *mask; q->skip = 0; q->window = -1; @@ -7434,19 +7441,21 @@ zr_event_queue_allows(struct zr_event_queue *q, return zr_event_mask_has(&q->mask, type, flags); } -static void +static int zr_event_queue_push(struct zr_event_queue *q, zr_hash id, enum zr_event_type type, union zr_event *evt) { ZR_ASSERT(q); ZR_ASSERT(evt); - if (!q || !evt) return; + if (!q || !evt) return 0; if (zr_event_queue_allows(q, type, evt->button.evt)) { evt->base.id = id; evt->base.type = type; evt->base.window = q->window; zr_buffer_push(&q->memory, ZR_BUFFER_FRONT, evt, sizeof(*evt), zr_event_align); + return 1; } + return 0; } /* ---------------------------------------------------------------- @@ -7548,11 +7557,18 @@ zr_find_window(struct zr_context *ctx, zr_hash hash) static void zr_insert_window(struct zr_context *ctx, struct zr_window *win) { + const struct zr_window *iter; struct zr_window *end; ZR_ASSERT(ctx); ZR_ASSERT(win); if (!win || !ctx) return; + iter = ctx->begin; + while (iter) { + if (iter == win) return; + iter = iter->next; + } + if (!ctx->begin) { win->next = 0; win->prev = 0; @@ -7591,9 +7607,11 @@ static int zr_op_begin(struct zr_context *ctx, union zr_param *p, struct zr_event_queue *queue) { struct zr_window *win; + struct zr_rect old_bounds; zr_hash title_hash; int title_len; int ret = 0; + int res = 0; struct zr_panel *layout = (struct zr_panel*)p[0].ptr; const char *title = p[1].cheat; @@ -7620,13 +7638,24 @@ zr_op_begin(struct zr_context *ctx, union zr_param *p, struct zr_event_queue *qu win->bounds = bounds; win->name = title_hash; win->popup.win = 0; + + /* initial expose event */ + {union zr_event evt; + evt.win.evt = ZR_EVENT_WINDOW_EXPOSED; + evt.win.data[0] = (int)win->bounds.x; + evt.win.data[1] = (int)win->bounds.y; + ret += zr_event_queue_push(queue, win->name, ZR_EVENT_WINDOW, &evt);} } else { /* update public window flags */ win->flags &= ~(zr_flags)(ZR_WINDOW_PRIVATE-1); win->flags |= flags; win->seq++; } - if (win->flags & ZR_WINDOW_HIDDEN) return 0; + if (win->flags & ZR_WINDOW_HIDDEN) { + queue->skip = zr_true; + ctx->current = win; + return 0; + } /* overlapping window */ if (!(win->flags & ZR_WINDOW_SUB) && !(win->flags & ZR_WINDOW_HIDDEN)) @@ -7695,12 +7724,19 @@ zr_op_begin(struct zr_context *ctx, union zr_param *p, struct zr_event_queue *qu } } - if (!iter) { + if (!iter && ctx->end != win) { /* current window is active in that position so transfer to top * at the highest priority in stack */ zr_remove_window(ctx, win); zr_insert_window(ctx, win); + /* queue focus event */ + {union zr_event evt; + evt.win.evt = ZR_EVENT_WINDOW_FOCUS; + evt.win.data[0] = (int)win->bounds.x; + evt.win.data[1] = (int)win->bounds.y; + ret += zr_event_queue_push(queue, win->name, ZR_EVENT_WINDOW, &evt);} + /* NOTE: I am not really sure if activating a window should directly * happen on that frame or the following frame. Directly would simplify * clicking window closing/minimizing but could cause wrong behavior. @@ -7716,33 +7752,115 @@ zr_op_begin(struct zr_context *ctx, union zr_param *p, struct zr_event_queue *qu win->layout = layout; ctx->current = win; - ret = zr_panel_begin(ctx, title); + old_bounds = win->bounds; + res = zr_panel_begin(ctx, title); layout->offset = &win->scrollbar; - if (ret) { + /* event queueing */ + if (res) { + /* heartbeat event used for the immediate mode API */ union zr_event evt; evt.win.evt = ZR_EVENT_WINDOW_ACTIVE; evt.win.data[0] = (int)win->bounds.x; evt.win.data[1] = (int)win->bounds.y; - zr_event_queue_push(queue, win->name, ZR_EVENT_WINDOW, &evt); - ret++; + ret += zr_event_queue_push(queue, win->name, ZR_EVENT_WINDOW, &evt); } else queue->skip = 1; + + if (win->flags & ZR_WINDOW_MINIMIZABLE) { + if (win->old_flags & ZR_WINDOW_MINIMIZED && !(layout->flags & ZR_WINDOW_MINIMIZED)) { + /* maximized window event */ + union zr_event evt; + evt.win.evt = ZR_EVENT_WINDOW_MAXIMIZED; + evt.win.data[0] = (int)win->bounds.x; + evt.win.data[1] = (int)win->bounds.y; + ret += zr_event_queue_push(queue, win->name, ZR_EVENT_WINDOW, &evt); + } else if (!(win->old_flags & ZR_WINDOW_MINIMIZED) && (layout->flags & ZR_WINDOW_MINIMIZED)) { + /* minimized window event */ + union zr_event evt; + evt.win.evt = ZR_EVENT_WINDOW_MINIMIZED; + evt.win.data[0] = (int)win->bounds.x; + evt.win.data[1] = (int)win->bounds.y; + ret += zr_event_queue_push(queue, win->name, ZR_EVENT_WINDOW, &evt); + } + } + if (win->flags & ZR_WINDOW_CLOSABLE) { + if (win->old_flags & ZR_WINDOW_HIDDEN && !(layout->flags & ZR_WINDOW_HIDDEN)) { + /* exposed window event */ + union zr_event evt; + evt.win.evt = ZR_EVENT_WINDOW_EXPOSED; + evt.win.data[0] = (int)win->bounds.x; + evt.win.data[1] = (int)win->bounds.y; + ret += zr_event_queue_push(queue, win->name, ZR_EVENT_WINDOW, &evt); + } else if (!(win->old_flags & ZR_WINDOW_HIDDEN) && (layout->flags & ZR_WINDOW_HIDDEN)) { + /* closed window event */ + union zr_event evt; + evt.win.evt = ZR_EVENT_WINDOW_CLOSED; + evt.win.data[0] = (int)win->bounds.x; + evt.win.data[1] = (int)win->bounds.y; + ret += zr_event_queue_push(queue, win->name, ZR_EVENT_WINDOW, &evt); + } + } + if (!(win->flags & ZR_WINDOW_HIDDEN) && !(win->flags & ZR_WINDOW_MINIMIZED) && + zr_input_is_mouse_hovering_rect(&ctx->input, win->bounds)) + { + /* hovered window event */ + union zr_event evt; + evt.win.evt = ZR_EVENT_WINDOW_HOVERED; + evt.win.data[0] = (int)win->bounds.x; + evt.win.data[1] = (int)win->bounds.y; + ret += zr_event_queue_push(queue, win->name, ZR_EVENT_WINDOW, &evt); + } + if (win->flags & ZR_WINDOW_MOVABLE) { + if (old_bounds.x > win->bounds.x || old_bounds.x < win->bounds.x || + old_bounds.y > win->bounds.y || old_bounds.y < win->bounds.y) + { + /* moved window event */ + union zr_event evt; + evt.win.evt = ZR_EVENT_WINDOW_MOVED; + evt.win.data[0] = (int)win->bounds.x; + evt.win.data[1] = (int)win->bounds.y; + ret += zr_event_queue_push(queue, win->name, ZR_EVENT_WINDOW, &evt); + } + } return ret; } static int zr_op_end(struct zr_context *ctx, union zr_param *p, struct zr_event_queue *queue) { + int ret = 0; + struct zr_window *win; + struct zr_rect old_bounds; + + ZR_UNUSED(p); ZR_ASSERT(ctx); ZR_ASSERT(ctx->current); ZR_ASSERT(ctx->current->layout); if (!ctx || !ctx->current) return 0; + if (ctx->current->flags & ZR_WINDOW_HIDDEN) { + ctx->current = 0; + return 0; + } - ZR_UNUSED(p); - ZR_UNUSED(queue); + win = ctx->current; + old_bounds = win->bounds; zr_panel_end(ctx); + + if (win->flags & ZR_WINDOW_SCALABLE) { + if (old_bounds.w > win->bounds.w || old_bounds.w < win->bounds.w || + old_bounds.h > win->bounds.h || old_bounds.h < win->bounds.h) + { + /* scaled window event */ + union zr_event evt; + evt.win.evt = ZR_EVENT_WINDOW_RESIZED; + evt.win.data[0] = (int)win->bounds.w; + evt.win.data[1] = (int)win->bounds.h; + ret += zr_event_queue_push(queue, win->name, ZR_EVENT_WINDOW, &evt); + } + } + ctx->current->old_flags = ctx->current->flags; ctx->current = 0; - return 0; + return ret; } struct zr_rect @@ -8105,7 +8223,8 @@ zr_header_toggle(struct zr_context *ctx, struct zr_window_header *header, zr_rune active, zr_rune inactive, enum zr_style_header_align align, int state) { int ret = zr_header_button(ctx, header,(state) ? active : inactive, align); - if (ret) return !state; + if (ret) + return !state; else return state; } @@ -9305,8 +9424,12 @@ zr_op_layout_push(struct zr_context *ctx, union zr_param *p, in = (!(layout->flags & ZR_WINDOW_ROM)) ? &ctx->input: 0; in = (in && widget_state == ZR_WIDGET_VALID) ? &ctx->input : 0; if (zr_button_behavior(&ws, header, in, ZR_BUTTON_DEFAULT)) { + union zr_event evt; *state = (*state == ZR_MAXIMIZED) ? ZR_MINIMIZED : ZR_MAXIMIZED; toggled = zr_true; + evt.tab.evt = (*state == ZR_MAXIMIZED) ? + ZR_EVENT_TAB_MAXIMIZED: ZR_EVENT_TAB_MINIMIZED; + ret += zr_event_queue_push(queue, id, ZR_EVENT_TAB, &evt); } { @@ -9370,8 +9493,7 @@ zr_op_layout_push(struct zr_context *ctx, union zr_param *p, layout->row.tree_depth++; evt.tab.evt = ZR_EVENT_TAB_ACTIVE; - zr_event_queue_push(queue, id, ZR_EVENT_TAB, &evt); - ret++; + ret += zr_event_queue_push(queue, id, ZR_EVENT_TAB, &evt); } else queue->skip = 1; return ret; } @@ -9785,8 +9907,16 @@ zr_op_button_text(struct zr_context *ctx, union zr_param *p, if (clicked) { union zr_event evt; evt.button.evt = ZR_EVENT_BUTTON_CLICKED; - zr_event_queue_push(queue, id, ZR_EVENT_BUTTON, &evt); - ret++; + ret += zr_event_queue_push(queue, id, ZR_EVENT_BUTTON, &evt); + } + if (ws == ZR_HOVERED) { + union zr_event evt; + evt.button.evt = ZR_EVENT_BUTTON_HOVERED; + ret += zr_event_queue_push(queue, id, ZR_EVENT_BUTTON, &evt); + } else if (ws == ZR_ACTIVE) { + union zr_event evt; + evt.button.evt = ZR_EVENT_BUTTON_PRESSED; + ret += zr_event_queue_push(queue, id, ZR_EVENT_BUTTON, &evt); } return ret; } @@ -9826,11 +9956,20 @@ zr_op_button_color(struct zr_context *ctx, union zr_param *p, button.active = color; clicked = zr_do_button(&ws, &win->buffer, bounds, &button, in, behavior, &bounds); + /* queue events */ if (clicked) { union zr_event evt; evt.button.evt = ZR_EVENT_BUTTON_CLICKED; - zr_event_queue_push(queue, id, ZR_EVENT_BUTTON, &evt); - ret++; + ret += zr_event_queue_push(queue, id, ZR_EVENT_BUTTON, &evt); + } + if (ws == ZR_HOVERED) { + union zr_event evt; + evt.button.evt = ZR_EVENT_BUTTON_HOVERED; + ret += zr_event_queue_push(queue, id, ZR_EVENT_BUTTON, &evt); + } else if (ws == ZR_ACTIVE) { + union zr_event evt; + evt.button.evt = ZR_EVENT_BUTTON_PRESSED; + ret += zr_event_queue_push(queue, id, ZR_EVENT_BUTTON, &evt); } return ret; } @@ -9873,11 +10012,20 @@ zr_op_button_symbol(struct zr_context *ctx, union zr_param *p, clicked = zr_do_button_symbol(&ws, &win->buffer, bounds, symbol, behavior, &button, in, &config->font); + /* queue events */ if (clicked) { union zr_event evt; evt.button.evt = ZR_EVENT_BUTTON_CLICKED; - zr_event_queue_push(queue, id, ZR_EVENT_BUTTON, &evt); - ret++; + ret += zr_event_queue_push(queue, id, ZR_EVENT_BUTTON, &evt); + } + if (ws == ZR_HOVERED) { + union zr_event evt; + evt.button.evt = ZR_EVENT_BUTTON_HOVERED; + ret += zr_event_queue_push(queue, id, ZR_EVENT_BUTTON, &evt); + } else if (ws == ZR_ACTIVE) { + union zr_event evt; + evt.button.evt = ZR_EVENT_BUTTON_PRESSED; + ret += zr_event_queue_push(queue, id, ZR_EVENT_BUTTON, &evt); } return ret; } @@ -9917,11 +10065,20 @@ zr_op_button_image(struct zr_context *ctx, union zr_param *p, button.padding = zr_vec2(0,0); clicked = zr_do_button_image(&ws, &win->buffer, bounds, img, behavior, &button, in); + /* queue events */ if (clicked) { union zr_event evt; evt.button.evt = ZR_EVENT_BUTTON_CLICKED; - zr_event_queue_push(queue, id, ZR_EVENT_BUTTON, &evt); - ret++; + ret += zr_event_queue_push(queue, id, ZR_EVENT_BUTTON, &evt); + } + if (ws == ZR_HOVERED) { + union zr_event evt; + evt.button.evt = ZR_EVENT_BUTTON_HOVERED; + ret += zr_event_queue_push(queue, id, ZR_EVENT_BUTTON, &evt); + } else if (ws == ZR_ACTIVE) { + union zr_event evt; + evt.button.evt = ZR_EVENT_BUTTON_PRESSED; + ret += zr_event_queue_push(queue, id, ZR_EVENT_BUTTON, &evt); } return ret; } @@ -9967,11 +10124,20 @@ zr_op_button_text_symbol(struct zr_context *ctx, union zr_param *p, clicked = zr_do_button_text_symbol(&ws, &win->buffer, bounds, symbol, text, align, behavior, &button, &config->font, in); + /* queue events */ if (clicked) { union zr_event evt; evt.button.evt = ZR_EVENT_BUTTON_CLICKED; - zr_event_queue_push(queue, id, ZR_EVENT_BUTTON, &evt); - ret++; + ret += zr_event_queue_push(queue, id, ZR_EVENT_BUTTON, &evt); + } + if (ws == ZR_HOVERED) { + union zr_event evt; + evt.button.evt = ZR_EVENT_BUTTON_HOVERED; + ret += zr_event_queue_push(queue, id, ZR_EVENT_BUTTON, &evt); + } else if (ws == ZR_ACTIVE) { + union zr_event evt; + evt.button.evt = ZR_EVENT_BUTTON_PRESSED; + ret += zr_event_queue_push(queue, id, ZR_EVENT_BUTTON, &evt); } return ret; } @@ -10019,11 +10185,20 @@ zr_op_button_text_image(struct zr_context *ctx, union zr_param *p, clicked = zr_do_button_text_image(&ws, &win->buffer, bounds, img, text, align, behavior, &button, &config->font, in); + /* queue events */ if (clicked) { union zr_event evt; evt.button.evt = ZR_EVENT_BUTTON_CLICKED; - zr_event_queue_push(queue, id, ZR_EVENT_BUTTON, &evt); - ret++; + ret += zr_event_queue_push(queue, id, ZR_EVENT_BUTTON, &evt); + } + if (ws == ZR_HOVERED) { + union zr_event evt; + evt.button.evt = ZR_EVENT_BUTTON_HOVERED; + ret += zr_event_queue_push(queue, id, ZR_EVENT_BUTTON, &evt); + } else if (ws == ZR_ACTIVE) { + union zr_event evt; + evt.button.evt = ZR_EVENT_BUTTON_PRESSED; + ret += zr_event_queue_push(queue, id, ZR_EVENT_BUTTON, &evt); } return ret; } @@ -10032,7 +10207,10 @@ static int zr_op_select(struct zr_context *ctx, union zr_param *p, struct zr_event_queue *queue) { struct zr_window *win; + struct zr_panel *layout; const struct zr_style *config; + const struct zr_input *in; + enum zr_widget_state state; zr_hash id = p[0].hash; const char *str = p[1].cheat; @@ -10053,14 +10231,19 @@ zr_op_select(struct zr_context *ctx, union zr_param *p, struct zr_event_queue *q return value; win = ctx->current; - zr_panel_alloc_space(&bounds, ctx); + layout = win->layout; config = &ctx->style; - item_padding = zr_get_property(ctx, ZR_PROPERTY_ITEM_PADDING); + state = zr_widget(&bounds, ctx); + if (!state) return 0; + in = (state == ZR_WIDGET_ROM || layout->flags & ZR_WINDOW_ROM) ? 0 : &ctx->input; old_value = value; + item_padding = zr_get_property(ctx, ZR_PROPERTY_ITEM_PADDING); background = (!value) ? config->colors[ZR_COLOR_WINDOW]: config->colors[ZR_COLOR_SELECTABLE]; - if (zr_input_is_mouse_click_in_rect(&ctx->input, ZR_BUTTON_LEFT, bounds)) { + + /* update selectable */ + if (in && zr_input_is_mouse_click_in_rect(&ctx->input, ZR_BUTTON_LEFT, bounds)) { background = config->colors[ZR_COLOR_SELECTABLE_HOVER]; if (zr_input_has_mouse_click_in_rect(&ctx->input, ZR_BUTTON_LEFT, bounds)) { if (!zr_input_is_mouse_down(&ctx->input, ZR_BUTTON_LEFT)) @@ -10068,6 +10251,7 @@ zr_op_select(struct zr_context *ctx, union zr_param *p, struct zr_event_queue *q } } + /* draw selectable */ text.padding.x = item_padding.x; text.padding.y = item_padding.y; text.background = background; @@ -10078,13 +10262,19 @@ zr_op_select(struct zr_context *ctx, union zr_param *p, struct zr_event_queue *q zr_widget_text(&win->buffer, bounds, str, zr_strsiz(str), &text, align|ZR_TEXT_MIDDLE, &config->font); + /* queue events */ + if (in && zr_input_is_mouse_hovering_rect(in, bounds)) { + union zr_event evt; + evt.select.evt = ZR_EVENT_SELECT_HOVERED; + evt.select.value = value; + ret += zr_event_queue_push(queue, id, ZR_EVENT_SELECT, &evt); + } if (old_value != value) { union zr_event evt; evt.select.evt = ZR_EVENT_SELECT_TOGGLED; evt.select.value = value; - zr_event_queue_push(queue, id, ZR_EVENT_SLIDER, &evt); + ret += zr_event_queue_push(queue, id, ZR_EVENT_SELECT, &evt); p[3].i = value; - ret++; } return ret; } @@ -10155,9 +10345,14 @@ zr_op_check(struct zr_context *ctx, union zr_param *p, union zr_event evt; evt.checkbox.evt = ZR_EVENT_CHECK_TOGGLED; evt.checkbox.value = active; - zr_event_queue_push(queue, id, ZR_EVENT_CHECKBOX, &evt); + ret += zr_event_queue_push(queue, id, ZR_EVENT_CHECKBOX, &evt); p[2].i = active; - ret++; + } + if (ws == ZR_HOVERED) { + union zr_event evt; + evt.checkbox.evt = ZR_EVENT_CHECK_HOVERED; + evt.checkbox.value = active; + ret += zr_event_queue_push(queue, id, ZR_EVENT_CHECKBOX, &evt); } return ret; } @@ -10172,7 +10367,7 @@ zr_op_option(struct zr_context *ctx, union zr_param *p, const struct zr_style *config; zr_hash id = p[0].hash; - const char *text = p[1].ptr; + const char *text = p[1].cheat; int is_active = p[2].i; int old; @@ -10207,9 +10402,15 @@ zr_op_option(struct zr_context *ctx, union zr_param *p, union zr_event evt; evt.option.evt = ZR_EVENT_OPTION_TOGGLED; evt.option.value = is_active; - zr_event_queue_push(queue, id, ZR_EVENT_OPTION, &evt); + ret += zr_event_queue_push(queue, id, ZR_EVENT_OPTION, &evt); p[2].i = is_active; - ret++; + } + + if (ws == ZR_HOVERED) { + union zr_event evt; + evt.option.evt = ZR_EVENT_OPTION_HOVERED; + evt.option.value = is_active; + ret += zr_event_queue_push(queue, id, ZR_EVENT_OPTION, &evt); } return ret; } @@ -10266,9 +10467,14 @@ zr_op_slider(struct zr_context *ctx, union zr_param *p, union zr_event evt; evt.slider.evt = ZR_EVENT_SLIDER_CHANGED; evt.slider.value = new_value; - zr_event_queue_push(queue, id, ZR_EVENT_SLIDER, &evt); + ret += zr_event_queue_push(queue, id, ZR_EVENT_SLIDER, &evt); p[2].f = new_value; - ret++; + } + if (ws == ZR_HOVERED) { + union zr_event evt; + evt.slider.evt = ZR_EVENT_SLIDER_HOVERED; + evt.slider.value = new_value; + ret += zr_event_queue_push(queue, id, ZR_EVENT_SLIDER, &evt); } return ret; } @@ -10323,9 +10529,14 @@ zr_op_progress(struct zr_context *ctx, union zr_param *p, union zr_event evt; evt.progress.evt = ZR_EVENT_PROGRESS_CHANGED; evt.progress.value = cur_value; - zr_event_queue_push(queue, id, ZR_EVENT_PROGRESS, &evt); + ret += zr_event_queue_push(queue, id, ZR_EVENT_PROGRESS, &evt); p[1].ui = (unsigned int)cur_value; - ret++; + } + if (ws == ZR_HOVERED) { + union zr_event evt; + evt.progress.evt = ZR_EVENT_PROGRESS_HOVERED; + evt.progress.value = cur_value; + ret += zr_event_queue_push(queue, id, ZR_EVENT_PROGRESS, &evt); } return ret; } @@ -10548,11 +10759,11 @@ zr_op_property(struct zr_context *ctx, union zr_param *p, struct zr_vec2 item_padding; int *state = 0; - int old_state = 0; zr_hash hash = 0; char *buffer = 0; zr_size *len = 0; zr_size *cursor = 0; + enum zr_property_state old_state; char dummy_buffer[ZR_MAX_NUMBER_BUFFER]; int dummy_state = ZR_PROPERTY_DEFAULT; @@ -10601,7 +10812,7 @@ zr_op_property(struct zr_context *ctx, union zr_param *p, prop.hover = config->colors[ZR_COLOR_PROPERTY_HOVER]; prop.active = config->colors[ZR_COLOR_PROPERTY_ACTIVE]; prop.text = config->colors[ZR_COLOR_TEXT]; - old_state = *state; + old_state = (enum zr_property_state)*state; new_val = zr_do_property(&ws, &win->buffer, bounds, name, min, val, max, step, inc_per_pixel, buffer, len, state, cursor, &prop, filter, i, &config->font); @@ -10614,17 +10825,33 @@ zr_op_property(struct zr_context *ctx, union zr_param *p, win->property.state = *state; win->property.name = hash; } + /* check if previously active property is now unactive */ if (*state == ZR_PROPERTY_DEFAULT && old_state != ZR_PROPERTY_DEFAULT) win->property.active = 0; + /* queue events */ + if (ws == ZR_HOVERED) { + union zr_event evt; + evt.property.evt = ZR_EVENT_PROPERTY_HOVERED; + evt.property.value = new_val; + evt.property.state = (enum zr_property_state)*state; + ret += zr_event_queue_push(queue, id, ZR_EVENT_PROPERTY, &evt); + } if (new_val > val || new_val < val) { union zr_event evt; evt.property.evt = ZR_EVENT_PROPERTY_CHANGED; evt.property.value = new_val; - zr_event_queue_push(queue, id, ZR_EVENT_PROPERTY, &evt); + evt.property.state = (enum zr_property_state)*state; + ret += zr_event_queue_push(queue, id, ZR_EVENT_PROPERTY, &evt); p[3].f = new_val; - ret++; + } + if (*state != old_state) { + union zr_event evt; + evt.property.evt = ZR_EVENT_PROPERTY_STATE_CHANGED; + evt.property.value = new_val; + evt.property.state = (enum zr_property_state)*state; + ret += zr_event_queue_push(queue, id, ZR_EVENT_PROPERTY, &evt); } return ret; } @@ -10634,27 +10861,33 @@ zr_op_property(struct zr_context *ctx, union zr_param *p, * CHART * * --------------------------------------------------------------*/ -void -zr_chart_begin(struct zr_context *ctx, enum zr_chart_type type, - int count, float min_value, float max_value) +static int +zr_op_chart_begin(struct zr_context *ctx, union zr_param *p, + struct zr_event_queue *queue) { - struct zr_command_buffer *out; - const struct zr_style *config; - struct zr_chart *chart; struct zr_window *win; + struct zr_command_buffer *out; + struct zr_chart *chart; + const struct zr_style *config; + + const enum zr_chart_type type = (const enum zr_chart_type)p[1].i; + const int count = p[2].i; + const float min_value = p[3].f; + const float max_value = p[4].f; struct zr_rect bounds = {0, 0, 0, 0}; struct zr_vec2 item_padding; struct zr_color color; ZR_ASSERT(ctx); + ZR_ASSERT(queue); ZR_ASSERT(ctx->current); ZR_ASSERT(ctx->current->layout); - if (!ctx || !ctx->current || !ctx->current->layout) return; + if (!ctx || !ctx->current || !ctx->current->layout) return 0; if (!zr_widget(&bounds, ctx)) { chart = &ctx->current->layout->chart; zr_zero(chart, sizeof(*chart)); - return; + return 0; } win = ctx->current; @@ -10683,6 +10916,7 @@ zr_chart_begin(struct zr_context *ctx, enum zr_chart_type type, chart->w = ZR_MAX(chart->w, 2 * item_padding.x); chart->h = ZR_MAX(chart->h, 2 * item_padding.y); chart->last.x = 0; chart->last.y = 0; + return 0; } static zr_flags @@ -10806,10 +11040,17 @@ zr_chart_push_column(const struct zr_context *ctx, struct zr_window *win, return ret; } -zr_flags -zr_chart_push(struct zr_context *ctx, float value) +static int +zr_op_chart_push(struct zr_context *ctx, union zr_param *p, + struct zr_event_queue *queue) { + int ret = 0; struct zr_window *win; + zr_flags flags; + + const zr_hash id = p[0].hash; + const float value = p[1].f; + ZR_ASSERT(ctx); ZR_ASSERT(ctx->current); if (!ctx || !ctx->current) @@ -10818,24 +11059,45 @@ zr_chart_push(struct zr_context *ctx, float value) win = ctx->current; switch (win->layout->chart.type) { case ZR_CHART_LINES: - return zr_chart_push_line(ctx, win, &win->layout->chart, value); + flags = zr_chart_push_line(ctx, win, &win->layout->chart, value); break; case ZR_CHART_COLUMN: - return zr_chart_push_column(ctx, win, &win->layout->chart, value); + flags = zr_chart_push_column(ctx, win, &win->layout->chart, value); break; default: case ZR_CHART_MAX: - return zr_false; + flags = 0; } + + /* queue events */ + if (flags & ZR_CHART_HOVERING) { + union zr_event evt; + evt.chart.evt = ZR_EVENT_CHART_HOVERED; + evt.chart.value_index = win->layout->chart.index-1; + evt.chart.value = value; + ret += zr_event_queue_push(queue, id, ZR_EVENT_CHART, &evt); + } + if (flags & ZR_CHART_CLICKED) { + union zr_event evt; + evt.chart.evt = ZR_EVENT_CHART_SELECTED; + evt.chart.value_index = win->layout->chart.index-1; + evt.chart.value = value; + ret += zr_event_queue_push(queue, id, ZR_EVENT_CHART, &evt); + } + return ret; } -void -zr_chart_end(struct zr_context *ctx) +static int +zr_op_chart_end(struct zr_context *ctx, union zr_param *p, + struct zr_event_queue *queue) { struct zr_window *win; struct zr_chart *chart; + + ZR_UNUSED(p); + ZR_UNUSED(queue); ZR_ASSERT(ctx); ZR_ASSERT(ctx->current); if (!ctx || !ctx->current) - return; + return 0; win = ctx->current; chart = &win->layout->chart; @@ -10848,6 +11110,7 @@ zr_chart_end(struct zr_context *ctx) chart->y = 0; chart->w = 0; chart->h = 0; + return 0; } /* ------------------------------------------------------------- @@ -11995,6 +12258,19 @@ zr_menu_end(struct zr_context *ctx) * * =============================================================== */ static int zr_op_handle(struct zr_context*, union zr_param*, struct zr_event_queue*); +struct zr_instruction { + const char *name; + zr_flags flags; + int(*func)(struct zr_context*, union zr_param *in, struct zr_event_queue*); + const char *fmt; + unsigned short argc; +}; +static const struct zr_instruction zr_op_table[ZR_OP_MAX] = { +#define ZR_OPCODE(a, b, c) {"zr_op_"#a, b, zr_op_##a, c}, + ZR_OPCODES(ZR_OPCODE) +#undef ZR_OPCODE + {0,0,0,"o",1} +}; void zr_load_default_style(struct zr_context *ctx, zr_flags flags) @@ -12004,7 +12280,8 @@ zr_load_default_style(struct zr_context *ctx, zr_flags flags) struct zr_event_queue queue; ZR_ASSERT(ctx); - p[0].op = ZR_OP_load_default_style; + p[0].h.op = ZR_OP_load_default_style; + p[0].h.next = zr_op_table[p[0].h.op].argc; p[1].flags = flags; zr_event_queue_init_fixed(&queue, 0, &evt, 1); @@ -12020,7 +12297,8 @@ zr_push_property(struct zr_context *ctx, enum zr_style_properties id, struct zr_event_queue queue; ZR_ASSERT(ctx); - p[0].op = ZR_OP_push_property; + p[0].h.op = ZR_OP_push_property; + p[0].h.next = zr_op_table[p[0].h.op].argc; p[1].i = (int)id; p[3].f = value.x; p[4].f = value.y; @@ -12038,7 +12316,8 @@ zr_push_color(struct zr_context *ctx, enum zr_style_colors id, struct zr_event_queue queue; ZR_ASSERT(ctx); - p[0].op = ZR_OP_push_color; + p[0].h.op = ZR_OP_push_color; + p[0].h.next = zr_op_table[p[0].h.op].argc; p[1].i = (int)id; p[2].color = color; @@ -12054,7 +12333,8 @@ zr_push_font_height(struct zr_context *ctx, float font_height) struct zr_event_queue queue; ZR_ASSERT(ctx); - p[0].op = ZR_OP_push_font_height; + p[0].h.op = ZR_OP_push_font_height; + p[0].h.next = zr_op_table[p[0].h.op].argc; p[1].f = font_height; zr_event_queue_init_fixed(&queue, 0, &evt, 1); @@ -12069,7 +12349,8 @@ zr_pop_color(struct zr_context *ctx) struct zr_event_queue queue; ZR_ASSERT(ctx); - p.op = ZR_OP_pop_color; + p.h.op = ZR_OP_pop_color; + p.h.next = zr_op_table[p.h.op].argc; zr_event_queue_init_fixed(&queue, 0, &evt, 1); zr_op_handle(ctx, &p, &queue); } @@ -12082,7 +12363,8 @@ zr_pop_property(struct zr_context *ctx) struct zr_event_queue queue; ZR_ASSERT(ctx); - p.op = ZR_OP_pop_property; + p.h.op = ZR_OP_pop_property; + p.h.next = zr_op_table[p.h.op].argc; zr_event_queue_init_fixed(&queue, 0, &evt, 1); zr_op_handle(ctx, &p, &queue); } @@ -12095,7 +12377,8 @@ zr_pop_font_height(struct zr_context *ctx) struct zr_event_queue queue; ZR_ASSERT(ctx); - p.op = ZR_OP_pop_font_height; + p.h.op = ZR_OP_pop_font_height; + p.h.next = zr_op_table[p.h.op].argc; zr_event_queue_init_fixed(&queue, 0, &evt, 1); zr_op_handle(ctx, &p, &queue); } @@ -12108,7 +12391,8 @@ zr_reset_colors(struct zr_context *ctx) struct zr_event_queue queue; ZR_ASSERT(ctx); - p.op = ZR_OP_reset_colors; + p.h.op = ZR_OP_reset_colors; + p.h.next = zr_op_table[p.h.op].argc; zr_event_queue_init_fixed(&queue, 0, &evt, 1); zr_op_handle(ctx, &p, &queue); } @@ -12121,7 +12405,8 @@ zr_reset_properties(struct zr_context *ctx) struct zr_event_queue queue; ZR_ASSERT(ctx); - p.op = ZR_OP_reset_properties; + p.h.op = ZR_OP_reset_properties; + p.h.next = zr_op_table[p.h.op].argc; zr_event_queue_init_fixed(&queue, 0, &evt, 1); zr_op_handle(ctx, &p, &queue); } @@ -12134,7 +12419,8 @@ zr_reset_font_height(struct zr_context *ctx) struct zr_event_queue queue; ZR_ASSERT(ctx); - p.op = ZR_OP_reset_font_height; + p.h.op = ZR_OP_reset_font_height; + p.h.next = zr_op_table[p.h.op].argc; zr_event_queue_init_fixed(&queue, 0, &evt, 1); zr_op_handle(ctx, &p, &queue); } @@ -12147,7 +12433,8 @@ zr_reset(struct zr_context *ctx) struct zr_event_queue queue; ZR_ASSERT(ctx); - p.op = ZR_OP_reset; + p.h.op = ZR_OP_reset; + p.h.next = zr_op_table[p.h.op].argc; zr_event_queue_init_fixed(&queue, 0, &evt, 1); zr_op_handle(ctx, &p, &queue); } @@ -12166,7 +12453,8 @@ zr_begin(struct zr_context *ctx, struct zr_panel *layout, const char *title, ZR_ASSERT(title); ZR_ASSERT(layout); - p[0].op = ZR_OP_begin; + p[0].h.op = ZR_OP_begin; + p[0].h.next = zr_op_table[p[0].h.op].argc; p[1].ptr = layout; p[2].cheat = title; p[3].f = bounds.x; @@ -12191,10 +12479,16 @@ zr_end(struct zr_context *ctx) union zr_event evt; union zr_param p; struct zr_event_queue queue; + struct zr_event_mask mask; ZR_ASSERT(ctx); - p.op = ZR_OP_end; - zr_event_queue_init_fixed(&queue, 0, &evt, 1); + p.h.op = ZR_OP_end; + p.h.next = zr_op_table[p.h.op].argc; + + zr_event_mask_begin(&mask); + zr_event_mask_add(&mask, ZR_EVENT_WINDOW, ZR_EVENT_WINDOW_RESIZED); + zr_event_mask_end(&mask); + zr_event_queue_init_fixed(&queue, &mask, &evt, 1); zr_op_handle(ctx, &p, &queue); } @@ -12208,7 +12502,8 @@ zr_layout_row_dynamic(struct zr_context *ctx, float height, int cols) ZR_ASSERT(ctx); ZR_ASSERT(ctx->current); - p[0].op = ZR_OP_layout_row_dynamic; + p[0].h.op = ZR_OP_layout_row_dynamic; + p[0].h.next = zr_op_table[p[0].h.op].argc; p[1].f = height; p[2].i = cols; @@ -12227,7 +12522,8 @@ zr_layout_row_static(struct zr_context *ctx, float height, ZR_ASSERT(ctx); ZR_ASSERT(ctx->current); - p[0].op = ZR_OP_layout_row_static; + p[0].h.op = ZR_OP_layout_row_static; + p[0].h.next = zr_op_table[p[0].h.op].argc; p[1].f = height; p[2].i = item_width; p[3].i = cols; @@ -12247,7 +12543,8 @@ zr_layout_row_begin(struct zr_context *ctx, enum zr_layout_format fmt, ZR_ASSERT(ctx); ZR_ASSERT(ctx->current); - p[0].op = ZR_OP_layout_row_begin; + p[0].h.op = ZR_OP_layout_row_begin; + p[0].h.next = zr_op_table[p[0].h.op].argc; p[1].i = (int)fmt; p[2].f = row_height; p[3].i = cols; @@ -12266,7 +12563,8 @@ zr_layout_row_push(struct zr_context *ctx, float value) ZR_ASSERT(ctx); ZR_ASSERT(ctx->current); - p[0].op = ZR_OP_layout_row_push; + p[0].h.op = ZR_OP_layout_row_push; + p[0].h.next = zr_op_table[p[0].h.op].argc; p[1].f = value; zr_event_queue_init_fixed(&queue, 0, &evt, 1); @@ -12281,7 +12579,8 @@ zr_layout_row_end(struct zr_context *ctx) struct zr_event_queue queue; ZR_ASSERT(ctx); - p.op = ZR_OP_layout_row_end; + p.h.op = ZR_OP_layout_row_end; + p.h.next = zr_op_table[p.h.op].argc; zr_event_queue_init_fixed(&queue, 0, &evt, 1); zr_op_handle(ctx, &p, &queue); } @@ -12297,7 +12596,8 @@ zr_layout_space_begin(struct zr_context *ctx, enum zr_layout_format fmt, ZR_ASSERT(ctx); ZR_ASSERT(ctx->current); - p[0].op = ZR_OP_layout_space_begin; + p[0].h.op = ZR_OP_layout_space_begin; + p[0].h.next = zr_op_table[p[0].h.op].argc; p[1].i = (int)fmt; p[2].f = height; p[3].i = widget_count; @@ -12316,7 +12616,8 @@ zr_layout_space_push(struct zr_context *ctx, struct zr_rect bounds) ZR_ASSERT(ctx); ZR_ASSERT(ctx->current); - p[0].op = ZR_OP_layout_space_push; + p[0].h.op = ZR_OP_layout_space_push; + p[0].h.next = zr_op_table[p[0].h.op].argc; p[1].f = bounds.x; p[2].f = bounds.y; p[3].f = bounds.w; @@ -12334,7 +12635,8 @@ zr_layout_space_end(struct zr_context *ctx) struct zr_event_queue queue; ZR_ASSERT(ctx); - p.op = ZR_OP_layout_space_end; + p.h.op = ZR_OP_layout_space_end; + p.h.next = zr_op_table[p.h.op].argc; zr_event_queue_init_fixed(&queue, 0, &evt, 1); zr_op_handle(ctx, &p, &queue); } @@ -12352,7 +12654,8 @@ zr_layout_push(struct zr_context *ctx, enum zr_layout_node_type type, ZR_ASSERT(ctx); ZR_ASSERT(ctx->current); - p[0].op = ZR_OP_layout_push; + p[0].h.op = ZR_OP_layout_push; + p[0].h.next = zr_op_table[p[0].h.op].argc; p[1].hash = ctx->next_id++; p[2].i = (int)type; p[3].cheat = title; @@ -12378,7 +12681,8 @@ zr_layout_pop(struct zr_context *ctx) struct zr_event_queue queue; ZR_ASSERT(ctx); - p.op = ZR_OP_layout_pop; + p.h.op = ZR_OP_layout_pop; + p.h.next = zr_op_table[p.h.op].argc; zr_event_queue_init_fixed(&queue, 0, &evt, 1); zr_op_handle(ctx, &p, &queue); } @@ -12395,7 +12699,8 @@ zr_text_colored(struct zr_context *ctx, const char *str, zr_size len, ZR_ASSERT(ctx->current); ZR_ASSERT(str); - p[0].op = ZR_OP_text; + p[0].h.op = ZR_OP_text; + p[0].h.next = zr_op_table[p[0].h.op].argc; p[1].cheat = str; p[2].ui = (unsigned int)len; p[3].flags = alignment; @@ -12419,7 +12724,8 @@ zr_group_begin(struct zr_context *ctx, struct zr_panel *layout, ZR_ASSERT(title); ZR_ASSERT(layout); - p[0].op = ZR_OP_group_begin; + p[0].h.op = ZR_OP_group_begin; + p[0].h.next = zr_op_table[p[0].h.op].argc; p[1].hash = ctx->next_id++; p[2].ptr = layout; p[3].cheat = title; @@ -12443,7 +12749,8 @@ zr_group_end(struct zr_context *ctx) struct zr_event_queue queue; ZR_ASSERT(ctx); - p.op = ZR_OP_group_end; + p.h.op = ZR_OP_group_end; + p.h.next = zr_op_table[p.h.op].argc; zr_event_queue_init_fixed(&queue, 0, &evt, 1); zr_op_handle(ctx, &p, &queue); } @@ -12460,10 +12767,11 @@ zr_text_wrap_colored(struct zr_context *ctx, const char *str, zr_size len, ZR_ASSERT(ctx->current); ZR_ASSERT(str); - p[0].op = ZR_OP_text_wrap; + p[0].h.op = ZR_OP_text_wrap; + p[0].h.next = zr_op_table[p[0].h.op].argc; p[1].cheat = str; p[2].ui = (unsigned int)len; - p[4].color = color; + p[3].color = color; zr_event_queue_init_fixed(&queue, 0, &evt, 1); zr_op_handle(ctx, p, &queue); @@ -12504,7 +12812,8 @@ zr_image(struct zr_context *ctx, struct zr_image img) ZR_ASSERT(ctx); ZR_ASSERT(ctx->current); - p[0].op = ZR_OP_image; + p[0].h.op = ZR_OP_image; + p[0].h.next = zr_op_table[p[0].h.op].argc; p[1].hash = ctx->next_id++; p[2].img = img.handle; p[3].f = img.region[0]; @@ -12532,7 +12841,8 @@ zr_button_text(struct zr_context *ctx, const char *title, ZR_ASSERT(ctx->current); ZR_ASSERT(title); - p[0].op = ZR_OP_button_text; + p[0].h.op = ZR_OP_button_text; + p[0].h.next = zr_op_table[p[0].h.op].argc; p[1].hash = ctx->next_id++; p[2].cheat = title; p[3].i = (int)behavior; @@ -12543,7 +12853,6 @@ zr_button_text(struct zr_context *ctx, const char *title, zr_event_queue_init_fixed(&queue, &mask, &evt, 1); evt_count = zr_op_handle(ctx, p, &queue); - if (ctx->op_buffer) return 1; return evt_count; } @@ -12560,7 +12869,8 @@ zr_button_color(struct zr_context *ctx, struct zr_color color, ZR_ASSERT(ctx); ZR_ASSERT(ctx->current); - p[0].op = ZR_OP_button_color; + p[0].h.op = ZR_OP_button_color; + p[0].h.next = zr_op_table[p[0].h.op].argc; p[1].hash = ctx->next_id++; p[2].color = color; p[3].i = (int)behavior; @@ -12571,7 +12881,6 @@ zr_button_color(struct zr_context *ctx, struct zr_color color, zr_event_queue_init_fixed(&queue, &mask, &evt, 1); evt_count = zr_op_handle(ctx, p, &queue); - if (ctx->op_buffer) return 1; return evt_count; } @@ -12588,7 +12897,8 @@ zr_button_symbol(struct zr_context *ctx, enum zr_symbol_type symbol, ZR_ASSERT(ctx); ZR_ASSERT(ctx->current); - p[0].op = ZR_OP_button_symbol; + p[0].h.op = ZR_OP_button_symbol; + p[0].h.next = zr_op_table[p[0].h.op].argc; p[1].hash = ctx->next_id++; p[2].i = (int)symbol; p[3].i = (int)behavior; @@ -12599,7 +12909,6 @@ zr_button_symbol(struct zr_context *ctx, enum zr_symbol_type symbol, zr_event_queue_init_fixed(&queue, &mask, &evt, 1); evt_count = zr_op_handle(ctx, p, &queue); - if (ctx->op_buffer) return 1; return evt_count; } @@ -12616,7 +12925,8 @@ zr_button_image(struct zr_context *ctx, struct zr_image img, ZR_ASSERT(ctx); ZR_ASSERT(ctx->current); - p[0].op = ZR_OP_button_image; + p[0].h.op = ZR_OP_button_image; + p[0].h.next = zr_op_table[p[0].h.op].argc; p[1].hash = ctx->next_id++; p[2].img = img.handle; p[3].f = img.region[0]; @@ -12633,7 +12943,6 @@ zr_button_image(struct zr_context *ctx, struct zr_image img, zr_event_queue_init_fixed(&queue, &mask, &evt, 1); evt_count = zr_op_handle(ctx, p, &queue); - if (ctx->op_buffer) return 1; return evt_count; } @@ -12650,7 +12959,8 @@ zr_button_text_symbol(struct zr_context *ctx, enum zr_symbol_type symbol, ZR_ASSERT(ctx); ZR_ASSERT(ctx->current); - p[0].op = ZR_OP_button_text_symbol; + p[0].h.op = ZR_OP_button_text_symbol; + p[0].h.next = zr_op_table[p[0].h.op].argc; p[1].hash = ctx->next_id++; p[2].i = (int)symbol; p[3].cheat = text; @@ -12663,7 +12973,6 @@ zr_button_text_symbol(struct zr_context *ctx, enum zr_symbol_type symbol, zr_event_queue_init_fixed(&queue, &mask, &evt, 1); evt_count = zr_op_handle(ctx, p, &queue); - if (ctx->op_buffer) return 1; return evt_count; } @@ -12680,7 +12989,8 @@ zr_button_text_image(struct zr_context *ctx, struct zr_image img, const char *te ZR_ASSERT(ctx); ZR_ASSERT(ctx->current); - p[0].op = ZR_OP_button_text_image; + p[0].h.op = ZR_OP_button_text_image; + p[0].h.next = zr_op_table[p[0].h.op].argc; p[1].hash = ctx->next_id++; p[2].img = img.handle; p[3].f = img.region[0]; @@ -12699,7 +13009,6 @@ zr_button_text_image(struct zr_context *ctx, struct zr_image img, const char *te zr_event_queue_init_fixed(&queue, &mask, &evt, 1); evt_count = zr_op_handle(ctx, p, &queue); - if (ctx->op_buffer) return 1; return evt_count; } @@ -12715,8 +13024,9 @@ zr_check(struct zr_context *ctx, const char *title, int active) ZR_ASSERT(ctx); ZR_ASSERT(ctx->current); - p[ZR_ELEMENT_OP].op = ZR_OP_check; - p[ZR_ELEMENT_ID].hash = ctx->next_id++; + p[0].h.op = ZR_OP_check; + p[0].h.next = zr_op_table[p[0].h.op].argc; + p[1].hash = ctx->next_id++; p[2].cheat = title; p[ZR_CHECKBOX_ACTIVE].i = active; @@ -12755,8 +13065,9 @@ zr_option(struct zr_context *ctx, const char *title, int active) ZR_ASSERT(ctx); ZR_ASSERT(ctx->current); - p[ZR_ELEMENT_OP].op = ZR_OP_option; - p[ZR_ELEMENT_ID].hash = ctx->next_id++; + p[0].h.op = ZR_OP_option; + p[0].h.next = zr_op_table[p[0].h.op].argc; + p[1].hash = ctx->next_id++; p[2].cheat = title; p[ZR_OPTION_ACTIVE].i = active; @@ -12792,8 +13103,9 @@ zr_select(struct zr_context *ctx, const char *title, zr_flags alignment, int val ZR_ASSERT(ctx); ZR_ASSERT(ctx->current); - p[ZR_ELEMENT_OP].op = ZR_OP_select; - p[ZR_ELEMENT_ID].hash = ctx->next_id++; + p[0].h.op = ZR_OP_select; + p[0].h.next = zr_op_table[p[0].h.op].argc; + p[1].hash = ctx->next_id++; p[2].cheat = title; p[ZR_SELECT_ALIGN].flags = alignment; p[ZR_SELECT_ACTIVE].i = value; @@ -12834,8 +13146,9 @@ zr_slide_float(struct zr_context *ctx, float min, float val, float max, float st ZR_ASSERT(ctx); ZR_ASSERT(ctx->current); - p[ZR_ELEMENT_OP].op = ZR_OP_slider; - p[ZR_ELEMENT_ID].hash = ctx->next_id++; + p[0].h.op = ZR_OP_slider; + p[0].h.next = zr_op_table[p[0].h.op].argc; + p[1].hash = ctx->next_id++; p[ZR_SLIDER_MIN].f = min; p[ZR_SLIDER_VALUE].f = val; p[ZR_SLIDER_MAX].f = max; @@ -12905,8 +13218,9 @@ zr_prog(struct zr_context *ctx, zr_size cur, zr_size max, int modifyable) ZR_ASSERT(ctx); ZR_ASSERT(ctx->current); - p[ZR_ELEMENT_OP].op = ZR_OP_progress; - p[ZR_ELEMENT_ID].hash = ctx->next_id++; + p[0].h.op = ZR_OP_progress; + p[0].h.next = zr_op_table[p[0].h.op].argc; + p[1].hash = ctx->next_id++; p[ZR_PROGRESS_VALUE].ui = (unsigned int)cur; p[ZR_PROGRESS_MAX].ui = (unsigned int)max; p[ZR_PROGRESS_MODIFY].i = modifyable; @@ -12949,8 +13263,9 @@ zr_property(struct zr_context *ctx, const char *name, ZR_ASSERT(ctx); ZR_ASSERT(ctx->current); - p[ZR_ELEMENT_OP].op = ZR_OP_property; - p[ZR_ELEMENT_ID].hash = ctx->next_id++; + p[0].h.op = ZR_OP_property; + p[0].h.next = zr_op_table[p[0].h.op].argc; + p[1].hash = ctx->next_id++; p[2].cheat = name; p[ZR_PROPERTY_MIN].f = min; p[ZR_PROPERTY_VALUE].f = val; @@ -13003,24 +13318,84 @@ zr_propertyi(struct zr_context *ctx, const char *name, int min, int val, int max return val; } +void +zr_chart_begin(struct zr_context *ctx, enum zr_chart_type type, + int num, float min, float max) +{ + union zr_event evt; + union zr_param p[6]; + struct zr_event_queue queue; + + ZR_ASSERT(ctx); + ZR_ASSERT(ctx->current); + + p[0].h.op = ZR_OP_chart_begin; + p[0].h.next = zr_op_table[p[0].h.op].argc; + p[1].hash = ctx->next_id; + p[2].i = (int)type; + p[3].i = num; + p[4].f = min; + p[5].f = max; + zr_event_queue_init_fixed(&queue, 0, &evt, 1); + zr_op_handle(ctx, p, &queue); +} + +zr_flags +zr_chart_push(struct zr_context *ctx, float value) +{ + int i; + int evt_count; + zr_flags flags = 0; + union zr_event evt[2]; + union zr_param p[10]; + struct zr_event_queue queue; + struct zr_event_mask mask; + + ZR_ASSERT(ctx); + ZR_ASSERT(ctx->current); + + p[0].h.op = ZR_OP_chart_push; + p[0].h.next = zr_op_table[p[0].h.op].argc; + p[1].hash = ctx->next_id; + p[2].f = value; + + zr_event_mask_begin(&mask); + zr_event_mask_add(&mask, ZR_EVENT_CHART, ZR_EVENT_CHART_HOVERED); + zr_event_mask_add(&mask, ZR_EVENT_CHART, ZR_EVENT_CHART_SELECTED); + zr_event_mask_end(&mask); + zr_event_queue_init_fixed(&queue, &mask, &evt, 2); + + evt_count = zr_op_handle(ctx, p, &queue); + if (!evt_count) return 0; + for (i = 0; i < evt_count; ++i) { + if (evt[i].chart.evt == ZR_EVENT_CHART_HOVERED) + flags |= ZR_CHART_HOVERING; + else if (evt[i].chart.evt == ZR_EVENT_CHART_SELECTED) + flags |= ZR_CHART_CLICKED; + } + return flags; +} + +void +zr_chart_end(struct zr_context *ctx) +{ + union zr_event evt; + union zr_param p; + struct zr_event_queue queue; + + ZR_ASSERT(ctx); + ctx->next_id++; + p.h.op = ZR_OP_chart_end; + p.h.next = zr_op_table[p.h.op].argc; + zr_event_queue_init_fixed(&queue, 0, &evt, 1); + zr_op_handle(ctx, &p, &queue); +} + /* ============================================================== * * RETAIN * * =============================================================== */ -static const struct zr_instruction { - const char *name; - zr_flags flags; - int(*func)(struct zr_context*, union zr_param *in, struct zr_event_queue*); - const char *fmt; - int argc; -} zr_op_table[] = { -#define ZR_OPCODE(a, b, c) {"zr_op_"#a, b, zr_op_##a, c}, - ZR_OPCODES(ZR_OPCODE) -#undef ZR_OPCODE - {0,0,0,0,1} -}; - void zr_recording_begin(struct zr_context *ctx, struct zr_buffer *buffer) { @@ -13042,7 +13417,8 @@ zr_recording_end(struct zr_context *ctx) ZR_ASSERT(ctx->op_buffer); if (!ctx || !ctx->op_buffer) return; - p[0].op = ZR_OP_list_end; + p[0].h.op = ZR_OP_list_end; + p[0].h.next = zr_op_table[p[0].h.op].argc; zr_event_queue_init_fixed(&queue, 0, &evt, 1); zr_op_handle(ctx, p, &queue); @@ -13054,12 +13430,43 @@ zr_recording_end(struct zr_context *ctx) static void zr_store_op(struct zr_buffer *buffer, union zr_param *p, int count) { + int i = 0; const zr_size align = ZR_ALIGNOF(union zr_param); - const zr_size size = sizeof(union zr_param) * (zr_size)count; - union zr_param *mem = zr_buffer_alloc(buffer, ZR_BUFFER_FRONT, size, align); - ZR_ASSERT(mem); - if (!mem) return; - zr_memcopy(mem, p, size); + const zr_size fixed_size = sizeof(union zr_param) * (zr_size)count; + const struct zr_instruction *op = &zr_op_table[p[0].h.op]; + union zr_param *mem; + for (i = 0; i < op->argc; ++i) { + if (op->fmt[i] == 'c') + break; + } + + if (i == op->argc) { + /* default fixed size operation */ + mem = (union zr_param*)zr_buffer_alloc(buffer, ZR_BUFFER_FRONT, fixed_size, align); + ZR_ASSERT(mem); + if (!mem) return; + zr_memcopy(mem, p, fixed_size); + } else { + /* While the approach of using zr_param for arguments is quite + * nice for the normal uses cases like using the API without replay + * it gets complicated otherwise. The problem here are strings. The way + * I decided to handle them is to just save them inside the operation + * and introduce operations with dynamic size. The other way of string + * handling would have been to force the user the set string pointers + * but that would limit and require a lot of work from the user. */ + zr_size text_len = zr_strsiz(p[i].cheat); + zr_size dyn_count = (text_len/sizeof(union zr_param)) + 1; + zr_size dyn_size = fixed_size + sizeof(union zr_param) * dyn_count; + + mem = (union zr_param*)zr_buffer_alloc(buffer, ZR_BUFFER_FRONT, dyn_size, align); + ZR_ASSERT(mem); + if (!mem) return; + + p[0].h.next += (unsigned short)dyn_count; + zr_memcopy(mem, p, fixed_size); + zr_memcopy(mem+count, p[i].cheat, text_len+1); + p[i].cheat = (const char*)(mem+count); + } } static int @@ -13070,9 +13477,9 @@ zr_op_handle(struct zr_context *ctx, union zr_param *p, const struct zr_instruction *op; ZR_ASSERT(ctx); ZR_ASSERT(p); - ZR_ASSERT(p[0].op <= ZR_OP_list_end); + ZR_ASSERT(p[0].h.op <= ZR_OP_list_end); - op = &zr_op_table[p[0].op]; + op = &zr_op_table[p[0].h.op]; if (ctx->op_buffer) zr_store_op(ctx->op_buffer, p, op->argc); if (op->func) @@ -13085,6 +13492,7 @@ zr_exec(struct zr_context *ctx, struct zr_buffer *events, int *event_count, const struct zr_event_mask *event_mask, struct zr_buffer *program, struct zr_buffer *runtime) { + int i = 0; int depth = 0; int skip = 0; int until = 0; @@ -13102,9 +13510,9 @@ zr_exec(struct zr_context *ctx, struct zr_buffer *events, zr_event_queue_init(&queue, event_mask, events); p = (union zr_param*)zr_buffer_memory(program); while (1) { - const struct zr_instruction *op = &zr_op_table[p[0].op]; - if (p[0].op == ZR_OP_list_end) break; - if (p[0].op == ZR_OP_begin) + const struct zr_instruction *op = &zr_op_table[p[0].h.op]; + if (p[0].h.op == ZR_OP_list_end) break; + if (p[0].h.op == ZR_OP_begin) queue.window++; /* push/pop runtime stack for panels */ @@ -13116,15 +13524,24 @@ zr_exec(struct zr_context *ctx, struct zr_buffer *events, static const zr_size panel_align = ZR_ALIGNOF(struct zr_panel); p[1].ptr = zr_buffer_alloc(runtime, ZR_BUFFER_FRONT, sizeof(struct zr_panel), panel_align); - ZR_ASSERT(p[1].ptr); + ZR_ASSERT(p[1].ptr && "if this hits you need more runtime memory"); if (!p[1].ptr) return 0; } - /* operation execution */ + /* Fix string parameter to point to correct memory + * @optimize: add another index to instruction for O(1) access */ + for (i = 0; (op->flags & ZR_STACK_TXT) && i < op->argc; ++i) { + if (op->fmt[i] == 'c') { + p[i].cheat = (const char*)(p + op->argc); + break; + } + } + + /* execute operation */ ZR_ASSERT(op->func); if (op->flags & ZR_STACK_GEN) p[1].hash = ctx->next_id++; - if (!skip || (skip && p[0].op == ZR_OP_end)) + if (!skip || (skip && p[0].h.op == ZR_OP_end)) count += op->func(ctx, p+1, &queue); /* conditional jumps */ @@ -13138,11 +13555,10 @@ zr_exec(struct zr_context *ctx, struct zr_buffer *events, queue.skip = zr_false; /* next operation */ - ZR_ASSERT(p->op < ZR_OP_list_end); - p += zr_op_table[p->op].argc; + p += p->h.next; + ZR_ASSERT(p->h.op <= ZR_OP_list_end); ZR_ASSERT((char*)p < ((char*)program->memory.ptr + program->allocated)); - if ((char*)p >= ((char*)program->memory.ptr + program->allocated)) - break; + if ((char*)p >= ((char*)program->memory.ptr + program->allocated)) break; } if (event_count) *event_count = count; *events = queue.memory; @@ -13186,19 +13602,19 @@ zr_element_lookup(const struct zr_buffer *program, zr_hash id) { zr_element elem = 0; const union zr_param *p; - p = (const union zr_param*)zr_buffer_memory_const(program); + const union zr_param *begin; + p = begin = (const union zr_param*)zr_buffer_memory_const(program); while (1) { - const struct zr_instruction *op = &zr_op_table[p[0].op]; - if (p[0].op == ZR_OP_list_end) break; + const struct zr_instruction *op = &zr_op_table[p[0].h.op]; + if (p[0].h.op == ZR_OP_list_end) break; if (op->argc >= 2 && op->fmt[1] == 'h') { if (p[1].hash == id) - return elem; + return (zr_element)(p - begin); } /* next operation */ - ZR_ASSERT(p->op < ZR_OP_list_end); - p += zr_op_table[p->op].argc; - elem += (zr_element)zr_op_table[p->op].argc; + ZR_ASSERT(p->h.op < ZR_OP_list_end); + p += p[0].h.next; ZR_ASSERT((const zr_byte*)p < ((const zr_byte*)program->memory.ptr + program->allocated)); if ((const zr_byte*)p >= ((const zr_byte*)program->memory.ptr + program->allocated)) break; @@ -13220,7 +13636,7 @@ zr_element_is_valid(const struct zr_buffer *program, zr_element elem) return 0; p += elem; - if (p->op >= ZR_OP_list_end) return 0; + if (p->h.op >= ZR_OP_list_end) return 0; else return 1; } @@ -13232,7 +13648,7 @@ zr_element_is_property_valid(const struct zr_buffer *program, const struct zr_instruction *op; /* make sure property is inside element */ - op = &zr_op_table[p[0].op]; + op = &zr_op_table[p[0].h.op]; ZR_ASSERT(op->argc > property); if (op->argc <= property) return 0; @@ -13274,7 +13690,7 @@ zr_element_get_property(struct zr_buffer *program, if (!program || !program->allocated || property < 0) return 0; if (!zr_element_is_valid(program, elem)) return 0; - p = (union zr_param*)zr_buffer_memory(program); + p = (union zr_param*)zr_buffer_memory(program) + elem; if (!zr_element_is_property_valid(program, p, property, fmt)) return 0; return p + property; } @@ -13329,6 +13745,16 @@ zr_element_get_flags(const struct zr_buffer *program, zr_element elem, int prope return p->flags; } +const void* +zr_element_get_ptr(const struct zr_buffer *program, zr_element elem, int property) +{ + const union zr_param *p; + p = zr_element_get_property_const(program, elem, property, 'p'); + ZR_ASSERT(p); + if (!p) return 0; + return p->ptr; +} + void zr_element_set_int(struct zr_buffer *program, zr_element elem, int property, int value) @@ -13384,3 +13810,14 @@ zr_element_set_flags(struct zr_buffer *program, zr_element elem, p->flags = value; } +void +zr_element_set_ptr(struct zr_buffer *program, zr_element elem, + int property, void *value) +{ + union zr_param *p; + p = zr_element_get_property(program, elem, property, 'p'); + ZR_ASSERT(p); + if (!p) return; + p->ptr = value; +} + diff --git a/zahnrad.h b/zahnrad.h index f929d055..6b12a189 100644 --- a/zahnrad.h +++ b/zahnrad.h @@ -1085,6 +1085,12 @@ const char *zr_edit_box_get_selection(zr_size *len, struct zr_edit_box*); /*============================================================== * EVENTS * =============================================================*/ +enum zr_property_state { + ZR_PROPERTY_DEFAULT, + ZR_PROPERTY_EDIT, + ZR_PROPERTY_DRAG +}; + enum zr_event_type { ZR_EVENT_WINDOW, ZR_EVENT_TAB, @@ -1095,85 +1101,80 @@ enum zr_event_type { ZR_EVENT_SLIDER, ZR_EVENT_PROGRESS, ZR_EVENT_PROPERTY, + ZR_EVENT_CHART, ZR_EVENT_MAX }; -enum zr_window_event_type { - ZR_EVENT_WINDOW_NONE = 0, - ZR_EVENT_WINDOW_ACTIVE = ZR_FLAG(0), - ZR_EVENT_WINDOW_EXPOSED = ZR_FLAG(1), - ZR_EVENT_WINDOW_HIDDEN = ZR_FLAG(2), - ZR_EVENT_WINDOW_MOVED = ZR_FLAG(3), - ZR_EVENT_WINDOW_RESIZED = ZR_FLAG(4), - ZR_EVENT_WINDOW_MINIMIZED = ZR_FLAG(5), - ZR_EVENT_WINDOW_MAXIMIZED = ZR_FLAG(6), - ZR_EVENT_WINDOW_HOVERED = ZR_FLAG(7), - ZR_EVENT_WINDOW_CLOSE = ZR_FLAG(9), - ZR_EVENT_WINDOW_ALL = ZR_FLAG(10)-1 -}; +/* window events */ +#define ZR_EVENT_WINDOW_NONE 0, +#define ZR_EVENT_WINDOW_ACTIVE ZR_FLAG(0) +#define ZR_EVENT_WINDOW_EXPOSED ZR_FLAG(1) +#define ZR_EVENT_WINDOW_HIDDEN ZR_FLAG(2) +#define ZR_EVENT_WINDOW_MOVED ZR_FLAG(3) +#define ZR_EVENT_WINDOW_RESIZED ZR_FLAG(4) +#define ZR_EVENT_WINDOW_MINIMIZED ZR_FLAG(5) +#define ZR_EVENT_WINDOW_MAXIMIZED ZR_FLAG(6) +#define ZR_EVENT_WINDOW_HOVERED ZR_FLAG(7) +#define ZR_EVENT_WINDOW_CLOSED ZR_FLAG(9) +#define ZR_EVENT_WINDOW_FOCUS ZR_FLAG(10) +#define ZR_EVENT_WINDOW_ALL (ZR_FLAG(11)-1) -enum zr_button_event_type { - ZR_EVENT_BUTTON_NONE = 0, - ZR_EVENT_BUTTON_HOVERED = ZR_FLAG(0), - ZR_EVENT_BUTTON_CLICKED = ZR_FLAG(2), - ZR_EVENT_BUTTON_PRESSED = ZR_FLAG(3), - ZR_EVENT_BUTTON_ALL = ZR_FLAG(4)-1 -}; +/* button events */ +#define ZR_EVENT_BUTTON_NONE 0 +#define ZR_EVENT_BUTTON_HOVERED ZR_FLAG(0) +#define ZR_EVENT_BUTTON_CLICKED ZR_FLAG(2) +#define ZR_EVENT_BUTTON_PRESSED ZR_FLAG(3) +#define ZR_EVENT_BUTTON_ALL (ZR_FLAG(4)-1) -enum zr_option_event_type { - ZR_EVENT_OPTION_NONE = 0, - ZR_EVENT_OPTION_HOVERED = ZR_FLAG(0), - ZR_EVENT_OPTION_TOGGLED = ZR_FLAG(2), - ZR_EVENT_OPTION_ALL = ZR_FLAG(3)-1 -}; +/* option events */ +#define ZR_EVENT_OPTION_NONE 0 +#define ZR_EVENT_OPTION_HOVERED ZR_FLAG(0) +#define ZR_EVENT_OPTION_TOGGLED ZR_FLAG(2) +#define ZR_EVENT_OPTION_ALL (ZR_FLAG(3)-1) -enum zr_checkbox_event_type { - ZR_EVENT_CHECK_NONE = 0, - ZR_EVENT_CHECK_HOVERED = ZR_FLAG(0), - ZR_EVENT_CHECK_TOGGLED = ZR_FLAG(2), - ZR_EVENT_CHECK_ALL = ZR_FLAG(3)-1 -}; +/* checkbox events */ +#define ZR_EVENT_CHECK_NONE 0 +#define ZR_EVENT_CHECK_HOVERED ZR_FLAG(0) +#define ZR_EVENT_CHECK_TOGGLED ZR_FLAG(2) +#define ZR_EVENT_CHECK_ALL (ZR_FLAG(3)-1) -enum zr_select_event_type { - ZR_EVENT_SELECT_NONE = 0, - ZR_EVENT_SELECT_HOVERED = ZR_FLAG(0), - ZR_EVENT_SELECT_TOGGLED = ZR_FLAG(2), - ZR_EVENT_SELECT_ALL = ZR_FLAG(3)-1 -}; +/* select events */ +#define ZR_EVENT_SELECT_NONE 0 +#define ZR_EVENT_SELECT_HOVERED ZR_FLAG(0) +#define ZR_EVENT_SELECT_TOGGLED ZR_FLAG(2) +#define ZR_EVENT_SELECT_ALL (ZR_FLAG(3)-1) -enum zr_slider_event_type { - ZR_EVENT_SLIDER_NONE = 0, - ZR_EVENT_SLIDER_HOVERED = ZR_FLAG(0), - ZR_EVENT_SLIDER_CHANGED = ZR_FLAG(2), - ZR_EVENT_SLIDER_ALL = ZR_FLAG(3)-1 -}; +/* slider events */ +#define ZR_EVENT_SLIDER_NONE 0 +#define ZR_EVENT_SLIDER_HOVERED ZR_FLAG(0) +#define ZR_EVENT_SLIDER_CHANGED ZR_FLAG(2) +#define ZR_EVENT_SLIDER_ALL (ZR_FLAG(3)-1) -enum zr_progress_event_type { - ZR_EVENT_PROGRESS_NONE = 0, - ZR_EVENT_PROGRESS_HOVERED = ZR_FLAG(0), - ZR_EVENT_PROGRESS_CHANGED = ZR_FLAG(2), - ZR_EVENT_PROGRESS_ALL = ZR_FLAG(3)-1 -}; +/* progressbar events */ +#define ZR_EVENT_PROGRESS_NONE 0 +#define ZR_EVENT_PROGRESS_HOVERED ZR_FLAG(0) +#define ZR_EVENT_PROGRESS_CHANGED ZR_FLAG(2) +#define ZR_EVENT_PROGRESS_ALL (ZR_FLAG(3)-1) -enum zr_property_event_type { - ZR_EVENT_PROPERTY_NONE = 0, - ZR_EVENT_PROPERTY_HOVERED = ZR_FLAG(0), - ZR_EVENT_PROPERTY_CHANGED = ZR_FLAG(2), - ZR_EVENT_PROPERTY_INCREMENTED = ZR_FLAG(3), - ZR_EVENT_PROPERTY_DECREMENTED = ZR_FLAG(4), - ZR_EVENT_PROPERTY_DRAGGED = ZR_FLAG(5), - ZR_EVENT_PROPERTY_ACTIVATED = ZR_FLAG(6), - ZR_EVENT_PROPERTY_DEACTIVED = ZR_FLAG(7), - ZR_EVENT_PROPERTY_ALL = ZR_FLAG(8)-1 -}; +/* property events */ +#define ZR_EVENT_PROPERTY_NONE 0 +#define ZR_EVENT_PROPERTY_HOVERED ZR_FLAG(0) +#define ZR_EVENT_PROPERTY_CHANGED ZR_FLAG(2) +#define ZR_EVENT_PROPERTY_STATE_CHANGED ZR_FLAG(3) +#define ZR_EVENT_PROPERTY_ALL (ZR_FLAG(8)-1) -enum zr_tab_event_type { - ZR_EVENT_TAB_NONE = 0, - ZR_EVENT_TAB_ACTIVE = ZR_FLAG(0), - ZR_EVENT_TAB_MINIMIZED = ZR_FLAG(1), - ZR_EVENT_TAB_MAXIMIZED = ZR_FLAG(2), - ZR_EVENT_TAB_ALL = ZR_FLAG(3)-1 -}; +/* tab events */ +#define ZR_EVENT_TAB_NONE 0 +#define ZR_EVENT_TAB_ACTIVE ZR_FLAG(0) +#define ZR_EVENT_TAB_MINIMIZED ZR_FLAG(1) +#define ZR_EVENT_TAB_MAXIMIZED ZR_FLAG(2) +#define ZR_EVENT_TAB_ALL (ZR_FLAG(3)-1) + +/* chart events */ +#define ZR_EVENT_CHART_NONE 0 +#define ZR_EVENT_CHART_HOVERED ZR_FLAG(1) +#define ZR_EVENT_CHART_SELECTED ZR_FLAG(2) +#define ZR_EVENT_CHART_ALL (ZR_FLAG(3)-1) struct zr_event_header { zr_hash id; @@ -1183,53 +1184,61 @@ struct zr_event_header { struct zr_event_window { struct zr_event_header base; - enum zr_window_event_type evt; + zr_flags evt; int data[2]; }; struct zr_event_button { struct zr_event_header base; - enum zr_button_event_type evt; + zr_flags evt; }; struct zr_event_tab { struct zr_event_header base; - enum zr_tab_event_type evt; + zr_flags evt; }; struct zr_event_checkbox { struct zr_event_header base; - enum zr_checkbox_event_type evt; + zr_flags evt; int value; }; struct zr_event_option { struct zr_event_header base; - enum zr_option_event_type evt; + zr_flags evt; int value; }; struct zr_event_select { struct zr_event_header base; - enum zr_select_event_type evt; + zr_flags evt; int value; }; struct zr_event_slider { struct zr_event_header base; - enum zr_slider_event_type evt; + zr_flags evt; float value; }; struct zr_event_progress { struct zr_event_header base; - enum zr_progress_event_type evt; + zr_flags evt; zr_size value; }; struct zr_event_property { struct zr_event_header base; - enum zr_property_event_type evt; + zr_flags evt; + enum zr_property_state state; + float value; +}; + +struct zr_event_chart { + struct zr_event_header base; + zr_flags evt; + int value_index; float value; }; @@ -1244,6 +1253,7 @@ union zr_event { struct zr_event_option option; struct zr_event_select select; struct zr_event_property property; + struct zr_event_chart chart; }; struct zr_event_mask {unsigned short flags[ZR_EVENT_MAX];}; @@ -1256,9 +1266,6 @@ int zr_event_mask_has(struct zr_event_mask*, enum zr_event_type, zr_flags); /*============================================================== * ELEMENT * =============================================================*/ -#define ZR_ELEMENT_OP 0 -#define ZR_ELEMENT_ID 1 - /* checkbox element properties */ #define ZR_CHECKBOX_ACTIVE 3 @@ -1296,12 +1303,14 @@ float zr_element_get_float(const struct zr_buffer*, zr_element, int property); unsigned zr_element_get_uint(const struct zr_buffer*, zr_element, int property); struct zr_color zr_element_get_color(const struct zr_buffer*, zr_element, int property); zr_flags zr_element_get_flags(const struct zr_buffer*, zr_element, int property); +const void* zr_element_get_ptr(const struct zr_buffer*, zr_element, int property); void zr_element_set_int(struct zr_buffer*, zr_element, int property, int); void zr_element_set_float(struct zr_buffer*, zr_element, int property, float); void zr_element_set_uint(struct zr_buffer*, zr_element, int property, unsigned int); void zr_element_set_color(struct zr_buffer*, zr_element, int property, struct zr_color); void zr_element_set_flags(struct zr_buffer*, zr_element, int property, zr_flags); +void zr_element_set_ptr(struct zr_buffer*, zr_element, int property, void*); /*============================================================== * WINDOW