/// <summary> /// Create the menu bar and return the root panel. /// The result would be a panel containing a group of dropdown entities, which implement the menu layout. /// The id of every dropdown is "menu-[menu-title]". /// Note: the returned menu bar panel comes without parent, you need to add it to your UI tree manually. /// </summary> /// <param name="layout">Layout to create menu bar with.</param> /// <param name="skin">Skin to use for panels and dropdown of this menu.</param> /// <returns>Menu root panel.</returns> static public Entities.Panel Create(MenuLayout layout, Entities.PanelSkin skin = Entities.PanelSkin.Simple) { // create the root panel var rootPanel = new Entities.Panel(new Vector2(0, Entities.DropDown.SelectedPanelHeight), skin, Entities.Anchor.TopLeft); rootPanel.PriorityBonus = 10000; rootPanel.Padding = Vector2.Zero; // create menus foreach (var menu in layout.Layout) { // create dropdown and all its items var dropdown = new Entities.DropDown(new Vector2(menu.Width, -1), Entities.Anchor.AutoInline, null, Entities.PanelSkin.None, skin, false); rootPanel.AddChild(dropdown); foreach (var item in menu.Items) { dropdown.AddItem(item); } dropdown.AutoSetListHeight = true; // set menu title and id dropdown.DefaultText = menu.Title; dropdown.Identifier = "menu-" + menu.Title; // disable dropdown selection (will only trigger event and unselect) dropdown.DontKeepSelection = true; // set callbacks for (int i = 0; i < menu.Items.Count; ++i) { var callback = menu.Actions[i]; if (callback != null) { var context = new MenuCallbackContext() { ItemIndex = i, ItemText = menu.Items[i], Entity = dropdown }; dropdown.OnSelectedSpecificItem(menu.Items[i], () => { callback(context); }); } } } // return the root panel return(rootPanel); }
/// <summary> /// Create the file menu and return the root panel. /// The result would be a panel containing a group of dropdown entities, which implement the file menu layout. /// The id of every dropdown is "menu-[menu-title]". /// Note: the returned file menu panel comes without parent, you need to add it to your UI tree manually. /// </summary> /// <param name="layout">Layout to create file menu for.</param> /// <param name="skin">Skin to use for panels and dropdown of this file menu.</param> /// <returns>Menu root panel.</returns> static public Entities.Panel Create(MenuLayout layout, Entities.PanelSkin skin = Entities.PanelSkin.Simple) { // create the root panel var rootPanel = new Entities.Panel(new Vector2(0, Entities.DropDown.SelectedPanelHeight), skin, Entities.Anchor.TopLeft); rootPanel.Padding = Vector2.Zero; // create menus foreach (var menu in layout.Layout) { // create dropdown and all its items var dropdown = new Entities.DropDown(new Vector2(menu.Width, -1), Entities.Anchor.AutoInline, null, Entities.PanelSkin.None, skin, false); rootPanel.AddChild(dropdown); foreach (var item in menu.Items) { dropdown.AddItem(item); } dropdown.AutoSetListHeight = true; // set menu title and id dropdown.DefaultText = menu.Title; dropdown.Identifier = "menu-" + menu.Title; // set events dropdown.OnValueChange += (Entities.Entity ent) => { // skip event when disable selection if (dropdown.SelectedIndex == -1) { return; } // call the item callback menu.Actions[dropdown.SelectedIndex]?.Invoke(); // unselect, so we'll show the menu title again dropdown.Unselect(); }; } // return the root panel return(rootPanel); }