From 6db11ac7ce025a421ff2a2c090998072c395100a Mon Sep 17 00:00:00 2001 From: vurtun Date: Mon, 7 Dec 2015 13:46:29 +0100 Subject: [PATCH] added icon/symbol menus --- demo/demo.c | 2 +- example/demo/demo.c | 61 ++++++-- example/demo/icon/next.png | Bin 0 -> 703 bytes example/demo/icon/pause.png | Bin 0 -> 1338 bytes example/demo/icon/{settings.png => play.png} | Bin example/demo/icon/prev.png | Bin 0 -> 701 bytes example/demo/icon/stop.png | Bin 0 -> 520 bytes zahnrad.c | 142 ++++++++++++++++--- zahnrad.h | 29 +++- 9 files changed, 197 insertions(+), 37 deletions(-) create mode 100644 example/demo/icon/next.png create mode 100644 example/demo/icon/pause.png rename example/demo/icon/{settings.png => play.png} (100%) create mode 100644 example/demo/icon/prev.png create mode 100644 example/demo/icon/stop.png diff --git a/demo/demo.c b/demo/demo.c index 41ad5c3..8d0355c 100644 --- a/demo/demo.c +++ b/demo/demo.c @@ -133,7 +133,7 @@ show_test_window(struct zr_window *window, struct zr_style *config, enum theme * zr_layout_row_begin(&layout, ZR_STATIC, 25, 2); zr_layout_row_push(&layout, 45); - zr_menu_begin(&layout, &menu, "MENU", 120, &file_state); + zr_menu_text_begin(&layout, &menu, "MENU", 120, &file_state); { zr_layout_row_dynamic(&menu, 25, 1); switch (menu_state) { diff --git a/example/demo/demo.c b/example/demo/demo.c index 4b631b6..1530c1c 100644 --- a/example/demo/demo.c +++ b/example/demo/demo.c @@ -61,7 +61,11 @@ struct icons { struct zr_image rocket; struct zr_image cloud; struct zr_image pen; - struct zr_image settings; + struct zr_image play; + struct zr_image pause; + struct zr_image stop; + struct zr_image prev; + struct zr_image next; struct zr_image tools; struct zr_image directory; struct zr_image images[9]; @@ -122,21 +126,41 @@ static void button_demo(struct zr_window *window, struct zr_style *config, struct icons *img) { struct zr_context layout; + struct zr_context menu; static int option = 1; static int toggle0 = 1; static int toggle1 = 0; static int toggle2 = 1; + static int music_active = 0; zr_begin(&layout, window, "Button Demo"); /*------------------------------------------------ * MENU *------------------------------------------------*/ zr_menubar_begin(&layout); - zr_layout_row_static(&layout, 40, 40, 4); - zr_button_image(&layout, img->settings, ZR_BUTTON_DEFAULT); - zr_button_image(&layout, img->tools, ZR_BUTTON_DEFAULT); - zr_button_image(&layout, img->cloud, ZR_BUTTON_DEFAULT); - zr_button_image(&layout, img->pen, ZR_BUTTON_DEFAULT); + { + /* toolbar */ + zr_layout_row_static(&layout, 40, 40, 4); + zr_menu_icon_begin(&layout, &menu, img->play, 120, &music_active); + { + /* settings */ + zr_layout_row_dynamic(&menu, 25, 1); + if (zr_menu_item_icon(&menu, img->play, "Play", ZR_TEXT_RIGHT)) + zr_menu_close(&menu, &music_active); + if (zr_menu_item_icon(&menu, img->stop, "Stop", ZR_TEXT_RIGHT)) + zr_menu_close(&menu, &music_active); + if (zr_menu_item_icon(&menu, img->pause, "Pause", ZR_TEXT_RIGHT)) + zr_menu_close(&menu, &music_active); + if (zr_menu_item_icon(&menu, img->next, "Next", ZR_TEXT_RIGHT)) + zr_menu_close(&menu, &music_active); + if (zr_menu_item_icon(&menu, img->next, "Prev", ZR_TEXT_RIGHT)) + zr_menu_close(&menu, &music_active); + } + zr_menu_end(&layout, &menu); + zr_button_image(&layout, img->tools, ZR_BUTTON_DEFAULT); + zr_button_image(&layout, img->cloud, ZR_BUTTON_DEFAULT); + zr_button_image(&layout, img->pen, ZR_BUTTON_DEFAULT); + } zr_menubar_end(&layout); /*------------------------------------------------ @@ -521,7 +545,11 @@ main(int argc, char *argv[]) int rocket; int cloud; int pen; - int settings; + int play; + int stop; + int pause; + int next; + int prev; int tools; int directory; int images[9]; @@ -567,7 +595,11 @@ main(int argc, char *argv[]) rocket = nvgCreateImage(vg, "../icon/rocket.png", 0); cloud = nvgCreateImage(vg, "../icon/cloud.png", 0); pen = nvgCreateImage(vg, "../icon/pen.png", 0); - settings = nvgCreateImage(vg, "../icon/settings.png", 0); + play = nvgCreateImage(vg, "../icon/play.png", 0); + pause = nvgCreateImage(vg, "../icon/pause.png", 0); + stop = nvgCreateImage(vg, "../icon/stop.png", 0); + next = nvgCreateImage(vg, "../icon/next.png", 0); + prev = nvgCreateImage(vg, "../icon/prev.png", 0); tools = nvgCreateImage(vg, "../icon/tools.png", 0); directory = nvgCreateImage(vg, "../icon/directory.png", 0); for (i = 0; i < 9; ++i) { @@ -582,9 +614,14 @@ main(int argc, char *argv[]) icons.rocket = zr_image_id(rocket); icons.cloud = zr_image_id(cloud); icons.pen = zr_image_id(pen); - icons.settings = zr_image_id(settings); + icons.play = zr_image_id(play); icons.tools = zr_image_id(tools); icons.directory = zr_image_id(directory); + icons.pause = zr_image_id(pause); + icons.stop = zr_image_id(stop); + icons.prev = zr_image_id(prev); + icons.next = zr_image_id(next); + for (i = 0; i < 9; ++i) icons.images[i] = zr_image_id(images[i]); @@ -656,7 +693,11 @@ cleanup: nvgDeleteImage(vg, rocket); nvgDeleteImage(vg, cloud); nvgDeleteImage(vg, pen); - nvgDeleteImage(vg, settings); + nvgDeleteImage(vg, play); + nvgDeleteImage(vg, pause); + nvgDeleteImage(vg, stop); + nvgDeleteImage(vg, next); + nvgDeleteImage(vg, prev); nvgDeleteImage(vg, tools); nvgDeleteImage(vg, directory); diff --git a/example/demo/icon/next.png b/example/demo/icon/next.png new file mode 100644 index 0000000000000000000000000000000000000000..af0b98dd26384a0e0137fc6a8c1712377ea2d77f GIT binary patch literal 703 zcmeAS@N?(olHy`uVBq!ia0vp^4Is?H1|$#LC7uRSY)RhkE)0?&_*lPgGf;%Hz$3Dl zfq`2Xgc%uT&5-~KvX^-Jy0YKrVdoJwm2c$b^vLfjn~6dME_I22d}m^c_!)PC=b zm*Z={ym!9; zr=IE@nC9GR!1pKJ$18j7?|_%w^?mMtpRhC4|LwVztRZ;J_id(y;Jy1(wr|eL*Kpy; zW%zy2;(3AP=6$~(b_(2La8Y0oMLNQwB4Lv5pnWa8YRZ+rV(E0d5Yaz8zvv1Aq(%CZ-7u3@;TK7+V;ekbU36 z@F`#N>C`F~jyC^>f*-r@PQHHp^!Fxp2aAeuh96Q2k~WNo4hk#C?2%=t;hn%UkKy5i zSjGq04JjX3IM{(YK9n(hFm*7hU}|aZ7gvz2`NGFgm%+>+uf_0TArpiA9_9x5TMP%< zxfp6bh+>s7J7D7b^!CI44>i^8ix>}VXJ+tcc%U5Icl{GO7%dUxFNcuMknA8|N MUHx3vIVCg!0CE}m3jhEB literal 0 HcmV?d00001 diff --git a/example/demo/icon/pause.png b/example/demo/icon/pause.png new file mode 100644 index 0000000000000000000000000000000000000000..7d6367e1ab0943e76984dfa115111f2cdea3983e GIT binary patch literal 1338 zcmeAS@N?(olHy`uVBq!ia0vp^4Is?H1|$#LC7uRSY)RhkE)0?&_*lPgGf;%Hz$3Dl zfq`2Xgc%uT&5-~KvX^-Jy0YKrVdoK2UtIszj)8&YxTlL_NX4zUGj97!yNk4+cX0HG zaq6hhU`k$)*{CLX0IIqv^lKqB>Ra5tvO>|=I-FbAvB9BG}w}1l@ahKGS zxi$%8{JPNeX+qe_z1F*L-2a}DnO0x??DLzMp$8KJ6zVk>uy8QIVC6CPIPN7|#gYv*Lmml@>iudtPES_nXVJPLC%##hH;W&u%x?Ui!+<_`F-X?Zlt4 z8s_#aZ3;(HGF-^vf27dMj{*@ zfq@h6-MxEtnbH2Zm@A!cU9Qa(?rb@i%~<#Eqx#*tx3hGMuU@-$>QDaXK5>(j$X{8} zv$t&7mSQ{o{PW2(UrNrM&J=NQfn42}3)d{I?Osoq{#jpE`^Dsf2!2+kZRaX~pUbT@ z{O;D*^npPYRiCpNVNmaT zR{!|*!^EG(_4cnTAAQ`h@eKd|N|lv+_udg*uyW0gn1BAcmtpqnS0T%iU%!4GdopIaYub6P51z%Fix$sFO-+^T z|NG)os%UhlzN(>A}ptTfraWyiPo=6Ves9oKGsV|AbJzI6Nc zaLbvNDaF(61UpYm|NegWuAl2T7EBQm4BX$>f7|H(r%RLWyxOyL&N&+qjvL>&qGKWo zzLijMzz}Nck-5h4d(4e3sn-BK9vwzu}vz*?fGSlbSol}42OAGL@9Zh~Q^=Yc-(r@SM zc9g92vRktLYxv0$`fECVp asvjJlZhq71=yzZ#$KdJe=d#Wzp$P!D%r+4K literal 0 HcmV?d00001 diff --git a/example/demo/icon/settings.png b/example/demo/icon/play.png similarity index 100% rename from example/demo/icon/settings.png rename to example/demo/icon/play.png diff --git a/example/demo/icon/prev.png b/example/demo/icon/prev.png new file mode 100644 index 0000000000000000000000000000000000000000..0eecc2e0a49ab8e125e5408c440db87f620fcac9 GIT binary patch literal 701 zcmeAS@N?(olHy`uVBq!ia0vp^4Is?H1|$#LC7uRSY)RhkE)0?&_*lPgGf;%Hz$3Dl zfq`2Xgc%uT&5-~KvX^-Jy0YKrVdoK3h%DbblYxQB%hSa%q~g}wyMcL!3`ATm9!nCw zsl=VM`^)l)tJo$_dUWILoS!+4D{Jzf*)H<(`D|0~k=LcH&>-Nzp}-=*#KG9Y;KXq9 zMC$q3dE0OA{kHq{S1ZeFkAssp9ZmjqcJYJs1M^#XY8UK3v}jAy|LfmRRiEGX?N=7_ z9_e{zFPQ7P4?8nP-QB73f_>kyL{*2^x6}?wB$qr4=$m)*ThqpKuO7`XKDgeFRi-jf zH^6@S;`7HQ9|-tx`m5J^>mcr)LcX$wfDbGj?EVbO=Y=xvztlN!Q~aRZ?Y)8z`plUQ zTxU2S%uvC^kk8H#&&=S@@PLzHk1RtCFM|zZ16caygT<^2b|DN8n#&nJI7>792w-gB z7iIYIK$hXp1vapZGf+VVJA?fesEi;(T?R9QycWZUgUSql5*QEgivdkUgo7e1SWfI@ z{a{)o)xhG$!1;zDUP$1ekO#|rrXE(N3}z;QMg|v!hQAFAw;Dijs$y!1~ZAkhRyaVKlzn18VUqC z1nyO^aO5*HwR8tmg--r|Qrek?S74XC&UJ0ue+=dSEpity9&*)izd!Tczm4azW<9U` zdN?fpDciq)dnTVO=i_J1P-Og(-0@+ub7(`D_>?*&o20-fCYJg+e;0iFb(XE-s@;X- tSuMJ9yICfDK2pp8jeDq{Crqee(n$!Ju=$t7Bw#{g@O1TaS?83{1ON?E{p0`u literal 0 HcmV?d00001 diff --git a/example/demo/icon/stop.png b/example/demo/icon/stop.png new file mode 100644 index 0000000000000000000000000000000000000000..6742baf9aa961f5381beda98bf3fe79aa5ae9a70 GIT binary patch literal 520 zcmeAS@N?(olHy`uVBq!ia0vp^4Is?H1|$#LC7uRSY)RhkE)0?&_*lPgGf;%Hz$3Dl zfq`2Xgc%uT&5-~KvX^-Jy0YKrVdoLy+VD&4ECU1MR8JSjkcwMxZ(J;7R^)MTOt{3N z8zk`7N|EH<*pXt)$d&^v*j2Wm>d{57#I~8SQ;1t`u@%P$5ec7y5Sz_ zms??@y3sINZ;eXvxsr-R-59_lkDaQ_hGaJ+DN z{}-+Xg?k^S|7UAZxc0$YbLqEx+iP}OK0n>@PWI2Y|4cc}66Z^Avu)r_l3Ah5;IN~- uTaK0CNbU7!iy6Mu#IZ7iLlo*M)tB-K*K*4nlP`NS0D-5gpUXO@geCwHd!8!* literal 0 HcmV?d00001 diff --git a/zahnrad.c b/zahnrad.c index 990fc28..818952f 100644 --- a/zahnrad.c +++ b/zahnrad.c @@ -9103,8 +9103,33 @@ void zr_combo_close(struct zr_context *combo) * * -------------------------------------------------------------- */ -void +static int zr_menu_begin(struct zr_context *parent, struct zr_context *menu, + struct zr_rect header, float width, int *active, int is_active) +{ + struct zr_rect body; + body.x = header.x; + body.w = width; + body.y = header.y + header.h; + body.h = (parent->bounds.y + parent->bounds.h) - body.y; + if (!zr_popup_nonblocking_begin(parent, menu, ZR_WINDOW_COMBO_MENU|ZR_WINDOW_NO_SCROLLBAR, + active, is_active, body, zr_vec2(0,0))) return 0; + return 1; +} + +static void +zr_menu_failed(struct zr_context *parent, struct zr_context *menu) +{ + zr_zero(menu, sizeof(*menu)); + menu->valid = zr_false; + menu->style = parent->style; + menu->buffer = parent->buffer; + menu->input = parent->input; + menu->queue = parent->queue; +} + +void +zr_menu_text_begin(struct zr_context *parent, struct zr_context *menu, const char *title, float width, int *active) { const struct zr_input *in; @@ -9117,25 +9142,28 @@ zr_menu_begin(struct zr_context *parent, struct zr_context *menu, ZR_ASSERT(title); ZR_ASSERT(active); if (!parent || !menu || !title || !active) return; - if (!parent->valid) goto failed; + if (!parent->valid) { + zr_menu_failed(parent, menu); + return; + } is_active = *active; in = parent->input; config = parent->style; zr_zero(menu, sizeof(*menu)); { - /* exeucte menu button for open/closing the popup */ + /* exeucte menu text button for open/closing the popup */ struct zr_button_text button; enum zr_widget_states state; - zr_zero(&button, sizeof(header)); + zr_zero(&button, sizeof(button)); zr_button(&button.base, &header, parent, ZR_BUTTON_NORMAL); - button.alignment = ZR_TEXT_CENTERED; button.base.rounding = 0; - button.base.border = (*active) ? config->colors[ZR_COLOR_BORDER]: + button.base.border = (active) ? config->colors[ZR_COLOR_BORDER]: config->colors[ZR_COLOR_WINDOW]; button.base.normal = (is_active) ? config->colors[ZR_COLOR_BUTTON_HOVER]: config->colors[ZR_COLOR_WINDOW]; button.base.active = config->colors[ZR_COLOR_WINDOW]; + button.alignment = ZR_TEXT_CENTERED; button.normal = config->colors[ZR_COLOR_TEXT]; button.active = config->colors[ZR_COLOR_TEXT]; button.hover = config->colors[ZR_COLOR_TEXT]; @@ -9143,25 +9171,93 @@ zr_menu_begin(struct zr_context *parent, struct zr_context *menu, &button, in, &config->font)) is_active = !is_active; } - { - /* calculate the maximum height of the menu */ - struct zr_rect body; - body.x = header.x; - body.w = width; - body.y = header.y + header.h; - body.h = (parent->bounds.y + parent->bounds.h) - body.y; - if (!zr_popup_nonblocking_begin(parent, menu, ZR_WINDOW_COMBO_MENU|ZR_WINDOW_NO_SCROLLBAR, active, - is_active, body, zr_vec2(0,0))) goto failed; - } - return; + if (!zr_menu_begin(parent, menu, header, width, active, is_active)) + zr_menu_failed(parent, menu); +} -failed: +void +zr_menu_icon_begin(struct zr_context *parent, struct zr_context *menu, + struct zr_image img, float width, int *active) +{ + const struct zr_input *in; + const struct zr_style *config; + struct zr_rect header; + int is_active; + + ZR_ASSERT(parent); + ZR_ASSERT(menu); + ZR_ASSERT(active); + if (!parent || !menu || !active) return; + if (!parent->valid) { + zr_menu_failed(parent, menu); + return; + } + + is_active = *active; + in = parent->input; + config = parent->style; zr_zero(menu, sizeof(*menu)); - menu->valid = zr_false; - menu->style = parent->style; - menu->buffer = parent->buffer; - menu->input = parent->input; - menu->queue = parent->queue; + { + /* execute menu icon button for open/closing the popup */ + struct zr_button_icon button; + enum zr_widget_states state; + zr_zero(&button, sizeof(button)); + zr_button(&button.base, &header, parent, ZR_BUTTON_NORMAL); + button.base.rounding = 1; + button.base.border = config->colors[ZR_COLOR_BORDER]; + button.base.normal = (is_active) ? config->colors[ZR_COLOR_BUTTON_HOVER]: + config->colors[ZR_COLOR_WINDOW]; + button.base.active = config->colors[ZR_COLOR_WINDOW]; + button.padding = config->properties[ZR_PROPERTY_ITEM_PADDING]; + if (zr_do_button_image(&state, parent->buffer, header, img, ZR_BUTTON_DEFAULT, + &button, in)) is_active = !is_active; + } + if (!zr_menu_begin(parent, menu, header, width, active, is_active)) + zr_menu_failed(parent, menu); +} + +void +zr_menu_symbol_begin(struct zr_context *parent, + struct zr_context *menu, enum zr_symbol sym, float width, int *active) +{ + const struct zr_input *in; + const struct zr_style *config; + struct zr_rect header; + int is_active; + + ZR_ASSERT(parent); + ZR_ASSERT(menu); + ZR_ASSERT(active); + if (!parent || !menu || !active) return; + if (!parent->valid) { + zr_menu_failed(parent, menu); + return; + } + + is_active = *active; + in = parent->input; + config = parent->style; + zr_zero(menu, sizeof(*menu)); + { + /* execute menu symbol button for open/closing the popup */ + struct zr_button_symbol button; + enum zr_widget_states state; + zr_zero(&button, sizeof(button)); + zr_button(&button.base, &header, parent, ZR_BUTTON_NORMAL); + button.base.rounding = 1; + button.base.border = config->colors[ZR_COLOR_BORDER]; + button.base.normal = (is_active) ? config->colors[ZR_COLOR_BUTTON_HOVER]: + config->colors[ZR_COLOR_WINDOW]; + button.base.active = config->colors[ZR_COLOR_WINDOW]; + button.normal = config->colors[ZR_COLOR_TEXT]; + button.active = config->colors[ZR_COLOR_TEXT]; + button.hover = config->colors[ZR_COLOR_TEXT]; + if (zr_do_button_symbol(&state, parent->buffer, header, sym, ZR_BUTTON_DEFAULT, + &button, in, &config->font)) is_active = !is_active; + } + if (!zr_menu_begin(parent, menu, header, width, active, is_active)) + zr_menu_failed(parent, menu); + } int zr_menu_item(struct zr_context *menu, enum zr_text_align align, const char *title) diff --git a/zahnrad.h b/zahnrad.h index 8db7cc0..54c8ef5 100644 --- a/zahnrad.h +++ b/zahnrad.h @@ -3257,23 +3257,46 @@ void zr_contextual_end(struct zr_context *parent, struct zr_context *popup, int* in the future. menu widget API - zr_menu_begin -- begins the menu item build up processs + zr_menu_xxx_begin -- begins the menu item build up processs zr_menu_item -- adds a item into the menu zr_menu_item_icon -- adds a text + image item into the menu zr_menu_item_symbol -- adds a text + symbol item into the menu zr_menu_close -- closes the menu zr_menu_end -- ends the menu item build up process */ -void zr_menu_begin(struct zr_context *parent, +void zr_menu_text_begin(struct zr_context *parent, struct zr_context *menu, const char *title, float width, int *active); -/* this function begins the menu build up process +/* this function begins the menu build up process and provides a clickable text button + * to open and close the menu popup panel Input: - parent window layout the menu will be placed into - ouput menu window layout - title of the menu to - the current state of the menu with either zr_true (open) or zr_false else */ +void zr_menu_icon_begin(struct zr_context *parent, + struct zr_context *menu, struct zr_image, + float width, int *active); +/* this function begins the menu build up process and provides a clickable icon button + * to open and close the menu popup panel + Input: + - parent window layout the menu will be placed into + - ouput menu window layout + - title of the menu to + - the current state of the menu with either zr_true (open) or zr_false else +*/ +void zr_menu_symbol_begin(struct zr_context *parent, + struct zr_context *menu, enum zr_symbol, + float width, int *active); +/* this function begins the menu build up process and provides a clickable symbol button + * to open and close the menu popup panel + Input: + - parent window layout the menu will be placed into + - ouput menu window layout + - symbol to add into the button + - the current state of the menu with either zr_true (open) or zr_false else +*/ int zr_menu_item(struct zr_context *menu, enum zr_text_align align, const char*); /* this function execute a menu item Input: