added icon/symbol menus

This commit is contained in:
vurtun 2015-12-07 13:46:29 +01:00
parent 37df97bee1
commit 6db11ac7ce
9 changed files with 197 additions and 37 deletions

View File

@ -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) {

View File

@ -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);

BIN
example/demo/icon/next.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 703 B

BIN
example/demo/icon/pause.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

View File

Before

Width:  |  Height:  |  Size: 566 B

After

Width:  |  Height:  |  Size: 566 B

BIN
example/demo/icon/prev.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 701 B

BIN
example/demo/icon/stop.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 520 B

142
zahnrad.c
View File

@ -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)

View File

@ -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: