Skip to content
Robert J. Lemmens edited this page Jan 5, 2022 · 1 revision

The menu

menu

Description

A menu is a simple clickable dropdown with components. A few variations exist:

  • nk_menu_begin_label(...) A menu with a text label
  • nk_menu_begin_image(...) A menu with an image
  • nk_menu_begin_image_label(...) A menu with an image and a text label
  • nk_menu_begin_symbol(...) A menu with an nk_symbol_type (See nk_symbol_type for symbols)
  • nk_menu_begin_symbol_label(...) A menu with an nk_symbol_type and a text label

Basic usage:

if (nk_menu_begin_label(...)) {
  /// Draw whatever you like!
  nk_menu_end(...);
}

We must also end all menu types with nk_menu_end(...)

Examples

Heres an example of a window with the common "file" menu:

static void scratchpad(struct nk_context *ctx, struct media *media) {
  nk_style_set_font(ctx, &media->font_20->handle);
  struct nk_vec2 size = nk_vec2(150, 200);
  nk_begin(ctx, "Nuklear Menu example", nk_rect(50,50, 255, 340), NK_WINDOW_TITLE | NK_WINDOW_MOVABLE);
  nk_layout_row_dynamic(ctx, 20, 1);
  if (nk_menu_begin_label(ctx, "File", NK_TEXT_LEFT, size)) {
    nk_layout_row_dynamic(ctx, 20, 1);
    nk_button_label(ctx, "New");
    nk_button_image_label(ctx, media->dir, "Open...", NK_TEXT_LEFT);
    nk_button_label(ctx, "Save");
    nk_menu_end(ctx);
  }
  nk_end(ctx);
}

Results in:

menu_label

menu label

nk_menu_begin_label displays as text. It takes the following parameters:

  • the nk_context
  • The display text
  • Aligment flag ( see nk_flags )
  • a vec2 that defines the size of the menu dropdown

menu image

menu_image

nk_menu_begin_image(...) displays as an arbitrary nk_image. Here we replaced the File text from the previous example with a play icon. It takes the following parameters:

  • the nk_context
  • the id (Text)
  • the nk_image to display
  • a vec2 that defines the size of the menu dropdown

There is also an nk_menu_begin_image_label(...) variant, which also takes in text to display next to the image.

menu_image_label

menu symbol

menu_symbol

nk_menu_begin_symbol(...) displays a symbol (nk_symbol_type). It works just like the image variant, but instead of an image it takes in a symbol. In the image above we've changed the play image for a plus symbol (NK_SYMBOL_PLUS). Just like the image variant there is also an nk_menu_begin_symbol_label(...) which takes in extra text to display.

Clone this wiki locally