void createUI(NVGcontext *vg, float w, float h) { int col; uiClear(); { int root = uiItem(); // position root element uiSetLayout(root,UI_LEFT|UI_TOP); uiSetMargins(root,50,50,0,0); uiSetSize(root,250,400); } col = column(0); uiSetLayout(col, UI_TOP|UI_HFILL); button(col, __LINE__, BND_ICONID(6,3), "Item 1", demohandler); button(col, __LINE__, BND_ICONID(6,3), "Item 2", demohandler); { int h = hgroup(col); radio(h, __LINE__, BND_ICONID(6,3), "Item 3.0", &enum1); radio(h, __LINE__, BND_ICONID(0,10), NULL, &enum1); radio(h, __LINE__, BND_ICONID(1,10), NULL, &enum1); radio(h, __LINE__, BND_ICONID(6,3), "Item 3.3", &enum1); } { int colr; int rows = row(col); int coll = vgroup(rows); label(coll, -1, "Items 4.0:"); coll = vgroup(coll); button(coll, __LINE__, BND_ICONID(6,3), "Item 4.0.0", demohandler); button(coll, __LINE__, BND_ICONID(6,3), "Item 4.0.1", demohandler); colr = vgroup(rows); uiSetFrozen(colr, option1); label(colr, -1, "Items 4.1:"); colr = vgroup(colr); slider(colr, __LINE__, "Item 4.1.0", &progress1); slider(colr,__LINE__, "Item 4.1.1", &progress2); } button(col, __LINE__, BND_ICONID(6,3), "Item 5", NULL); check(col, __LINE__, "Frozen", &option1); check(col, __LINE__, "Item 7", &option2); check(col, __LINE__, "Item 8", &option3); textbox(col, (UIhandle)textbuffer, textbuffer, 32); uiLayout(); }
int column_append(int parent, int item) { uiInsert(parent, item); // fill parent horizontally, anchor to previous item vertically uiSetLayout(item, UI_HFILL); uiSetMargins(item, 0, 1, 0, 0); return item; }
int add_menu_option(int parent, const char *name, int *choice) { int opt = radio(-1, name, choice); uiInsert(parent, opt); uiSetLayout(opt, UI_HFILL|UI_TOP); uiSetMargins(opt, 1, 1, 1, 1); return opt; }
void build_wrapdemo(int parent) { int col = uiItem(); uiInsert(parent, col); uiSetBox(col, UI_COLUMN); uiSetLayout(col, UI_FILL); const int M = 5; const int S = 100; const int T = 50; int box; box = demorect(col, "Box( UI_ROW | UI_WRAP | UI_START )\nLayout( UI_HFILL | UI_TOP )", 0.6f, UI_ROW | UI_WRAP | UI_START, UI_TOP, 0, 0, M, M, M, M); fill_wrap_row_box(box); box = demorect(col, "Box( UI_ROW | UI_WRAP | UI_MIDDLE )\nLayout( UI_HFILL | UI_TOP )", 0.6f, UI_ROW | UI_WRAP, UI_HFILL | UI_TOP, 0, 0, M, M, M, M); fill_wrap_row_box(box); box = demorect(col, "Box( UI_ROW | UI_WRAP | UI_END )\nLayout( UI_HFILL | UI_TOP )", 0.6f, UI_ROW | UI_WRAP | UI_END, UI_HFILL | UI_TOP, 0, 0, M, M, M, M); fill_wrap_row_box(box); box = demorect(col, "Box( UI_ROW | UI_WRAP | UI_JUSTIFY )\nLayout( UI_HFILL | UI_TOP )", 0.6f, UI_ROW | UI_WRAP | UI_JUSTIFY, UI_HFILL | UI_TOP, 0, 0, M, M, M, M); fill_wrap_row_box(box); box = demorect(col, "Box( UI_COLUMN | UI_WRAP | UI_START )\nLayout( UI_LEFT | UI_VFILL )", 0.6f, UI_COLUMN | UI_WRAP | UI_START, UI_LEFT | UI_VFILL, 0, 0, M, M, M, M); fill_wrap_column_box(box); }
int demorect(int parent, const char *label, float hue, int box, int layout, int w, int h, int m1, int m2, int m3, int m4) { int item = colorrect(label, nvgHSL(hue, 1.0f, 0.8f)); uiSetLayout(item, layout); uiSetBox(item, box); uiSetMargins(item, m1, m2, m3, m4); uiSetSize(item, w, h); uiInsert(parent, item); return item; }
void rowhandler(int parent, UIevent event) { int item = uiLastChild(parent); int last = uiPrevSibling(item); uiSetRelToLeft(item, last); if (last > 0) uiSetRelToRight(last, item); uiSetLayout(item, UI_LEFT|UI_RIGHT); uiSetMargins(item, (last < 0)?0:8, 0, 0, 0); }
void vgrouphandler(int parent, UIevent event) { int item = uiLastChild(parent); int last = uiPrevSibling(item); // mark the new item as positioned under the previous item uiSetRelToTop(item, last); // fill parent horizontally, anchor to previous item vertically uiSetLayout(item, UI_HFILL|UI_TOP); // if not the first item, add a margin uiSetMargins(item, 0, (last < 0)?0:-2, 0, 0); }
void build_democontent(int parent) { // some persistent variables for demonstration static float progress1 = 0.25f; static float progress2 = 0.75f; static int option1 = 1; static int option2 = 0; static int option3 = 0; int col = column(); uiInsert(parent, col); uiSetMargins(col, 10, 10, 10, 10); uiSetLayout(col, UI_TOP|UI_HFILL); column_append(col, button(BND_ICON_GHOST, "Item 1", demohandler)); if (option3) column_append(col, button(BND_ICON_GHOST, "Item 2", demohandler)); { int h = column_append(col, hbox()); hgroup_append(h, radio(BND_ICON_GHOST, "Item 3.0", &enum1)); if (option2) uiSetMargins(hgroup_append_fixed(h, radio(BND_ICON_REC, NULL, &enum1)), -1,0,0,0); uiSetMargins(hgroup_append_fixed(h, radio(BND_ICON_PLAY, NULL, &enum1)), -1,0,0,0); uiSetMargins(hgroup_append(h, radio(BND_ICON_GHOST, "Item 3.3", &enum1)), -1,0,0,0); } { int rows = column_append(col, row()); int coll = row_append(rows, vgroup()); vgroup_append(coll, label(-1, "Items 4.0:")); coll = vgroup_append(coll, vbox()); vgroup_append(coll, button(BND_ICON_GHOST, "Item 4.0.0", demohandler)); uiSetMargins(vgroup_append(coll, button(BND_ICON_GHOST, "Item 4.0.1", demohandler)),0,-2,0,0); int colr = row_append(rows, vgroup()); uiSetMargins(colr, 8, 0, 0, 0); uiSetFrozen(colr, option1); vgroup_append(colr, label(-1, "Items 4.1:")); colr = vgroup_append(colr, vbox()); vgroup_append(colr, slider("Item 4.1.0", &progress1)); uiSetMargins(vgroup_append(colr, slider("Item 4.1.1", &progress2)),0,-2,0,0); } column_append(col, button(BND_ICON_GHOST, "Item 5", NULL)); static char textbuffer[1024] = "The quick brown fox."; column_append(col, textbox(textbuffer, 1024)); column_append(col, check("Frozen", &option1)); column_append(col, check("Item 7", &option2)); column_append(col, check("Item 8", &option3)); }
void fill_wrap_row_box(int box) { const int M = 5; const int S = 100; const int T = 50; srand(303); for (int i = 0; i < 20; ++i) { float hue = (float)(rand()%360)/360.0f; int width = 10 + (rand()%5)*10; int u; switch(rand()%4) { default: break; case 0: { u = demorect(box, "Layout( UI_TOP )", hue, 0, UI_TOP, width, T, M, M, M, M); } break; case 1: { u = demorect(box, "Layout( UI_VCENTER )", hue, 0, UI_VCENTER, width, T/2, M, M, M, M); } break; case 2: { u = demorect(box, "Layout( UI_VFILL )", hue, 0, UI_VFILL, width, T, M, M, M, M); } break; case 3: { u = demorect(box, "Layout( UI_DOWN )", hue, 0, UI_DOWN, width, T/2, M, M, M, M); } break; } if (rand()%10 == 0) uiSetLayout(u, uiGetLayout(u)|UI_BREAK); } }
void fill_wrap_column_box(int box) { const int M = 5; const int S = 100; const int T = 50; srand(303); for (int i = 0; i < 20; ++i) { float hue = (float)(rand()%360)/360.0f; int height = 10 + (rand()%5)*10; int u; switch(rand()%4) { default: break; case 0: { u = demorect(box, "Layout( UI_LEFT )", hue, 0, UI_LEFT, T, height, M, M, M, M); } break; case 1: { u = demorect(box, "Layout( UI_HCENTER )", hue, 0, UI_HCENTER, T/2, height, M, M, M, M); } break; case 2: { u = demorect(box, "Layout( UI_HFILL )", hue, 0, UI_HFILL, T, height, M, M, M, M); } break; case 3: { u = demorect(box, "Layout( UI_RIGHT )", hue, 0, UI_RIGHT, T/2, height, M, M, M, M); } break; } if (rand()%10 == 0) uiSetLayout(u, uiGetLayout(u)|UI_BREAK); } }
int row_append(int parent, int item) { uiInsert(parent, item); uiSetLayout(item, UI_HFILL); return item; }
int vgroup_append(int parent, int item) { uiInsert(parent, item); // fill parent horizontally, anchor to previous item vertically uiSetLayout(item, UI_HFILL); return item; }
void draw(NVGcontext *vg, float w, float h) { bndBackground(vg, 0, 0, w, h); // some OUI stuff uiBeginLayout(); int root = panel(); // position root element uiSetSize(0,w,h); ((UIData*)uiGetHandle(root))->handler = roothandler; uiSetEvents(root, UI_SCROLL|UI_BUTTON0_DOWN); uiSetBox(root, UI_COLUMN); static int choice = -1; int menu = uiItem(); uiSetLayout(menu, UI_HFILL|UI_TOP); uiSetBox(menu, UI_ROW); uiInsert(root, menu); int opt_blendish_demo = add_menu_option(menu, "Blendish Demo", &choice); int opt_oui_demo = add_menu_option(menu, "OUI Demo", &choice); int opt_layouts = add_menu_option(menu, "UI_LAYOUT", &choice); int opt_row = add_menu_option(menu, "UI_ROW", &choice); int opt_column = add_menu_option(menu, "UI_COLUMN", &choice); int opt_wrap = add_menu_option(menu, "UI_WRAP", &choice); if (choice < 0) choice = opt_blendish_demo; int content = uiItem(); uiSetLayout(content, UI_FILL); uiInsert(root, content); if (choice == opt_blendish_demo) { int democontent = uiItem(); uiSetLayout(democontent, UI_FILL); uiInsert(content, democontent); UIData *data = (UIData *)uiAllocHandle(democontent, sizeof(UIData)); data->handler = 0; data->subtype = ST_DEMOSTUFF; } else if (choice == opt_oui_demo) { int democontent = uiItem(); uiSetLayout(democontent, UI_TOP); uiSetSize(democontent, 250, 0); uiInsert(content, democontent); build_democontent(democontent); } else if (choice == opt_layouts) { build_layoutdemo(content); } else if (choice == opt_row) { build_rowdemo(content); } else if (choice == opt_column) { build_columndemo(content); } else if (choice == opt_wrap) { build_wrapdemo(content); } uiEndLayout(); drawUI(vg, 0, BND_CORNER_NONE); #if 0 for (int i = 0; i < uiGetLastItemCount(); ++i) { if (uiRecoverItem(i) == -1) { UIitem *pitem = uiLastItemPtr(i); nvgBeginPath(vg); nvgRect(vg,pitem->margins[0],pitem->margins[1],pitem->size[0],pitem->size[1]); nvgStrokeWidth(vg, 2); nvgStrokeColor(vg, nvgRGBAf(1.0f,0.0f,0.0f,0.5f)); nvgStroke(vg); } } #endif if (choice == opt_blendish_demo) { UIvec2 cursor = uiGetCursor(); cursor.x -= w/2; cursor.y -= h/2; if (abs(cursor.x) > (w/3)) { bndJoinAreaOverlay(vg, 0, 0, w, h, 0, (cursor.x > 0)); } else if (abs(cursor.y) > (h/3)) { bndJoinAreaOverlay(vg, 0, 0, w, h, 1, (cursor.y > 0)); } } uiProcess((int)(glfwGetTime()*1000.0)); }