/// <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); }
/// <summary> /// Show a message box with custom buttons and callbacks. /// </summary> /// <param name="header">Messagebox header.</param> /// <param name="text">Main text.</param> /// <param name="options">Msgbox response options.</param> /// <param name="extraEntities">Optional array of entities to add to msg box under the text and above the buttons.</param> /// <param name="size">Alternative size to use.</param> /// <param name="onDone">Optional callback to call when this msgbox closes.</param> /// <param name="parent">Parent to add message box to (if not defined will use root)</param> /// <returns>Message box handle.</returns> public static MessageBoxHandle ShowMsgBox(string header, string text, MsgBoxOption[] options, Entities.Entity[] extraEntities = null, Vector2?size = null, System.Action onDone = null, Entities.Entity parent = null) { // object to return MessageBoxHandle ret = new MessageBoxHandle(); // create panel for messagebox size = size ?? new Vector2(500, -1); var panel = new Entities.Panel(size.Value); ret.Panel = panel; panel.AddChild(new Entities.Header(header)); panel.AddChild(new Entities.HorizontalLine()); panel.AddChild(new Entities.RichParagraph(text)); // add to opened boxes counter OpenedMsgBoxesCount++; // add rectangle to hide and lock background Entities.ColoredRectangle fader = null; if (BackgroundFaderColor.A != 0) { fader = new Entities.ColoredRectangle(Vector2.Zero, Entities.Anchor.Center); fader.FillColor = new Color(0, 0, 0, 100); fader.OutlineWidth = 0; fader.ClickThrough = false; UserInterface.Active.AddEntity(fader); ret.BackgroundFader = fader; } // add custom appended entities if (extraEntities != null) { foreach (var entity in extraEntities) { panel.AddChild(entity); } } // add bottom buttons panel var buttonsPanel = new Entities.Panel(new Vector2(0, 70), Entities.PanelSkin.None, size.Value.Y == -1 ? Entities.Anchor.Auto : Entities.Anchor.BottomCenter); buttonsPanel.Padding = Vector2.Zero; panel.AddChild(buttonsPanel); buttonsPanel.PriorityBonus = -10; // add all option buttons var btnSize = new Vector2(options.Length == 1 ? 0f : (1f / options.Length), 60); foreach (var option in options) { // add button entity var button = new Entities.Button(option.Title, anchor: Entities.Anchor.AutoInline, size: btnSize); // set click event button.OnClick += (Entities.Entity ent) => { // if need to close message box after clicking this button, close it: if (option.Callback == null || option.Callback()) { // remove fader and msg box panel if (fader != null) { fader.RemoveFromParent(); } panel.RemoveFromParent(); // decrease msg boxes count OpenedMsgBoxesCount--; // call on-done callback onDone?.Invoke(); } }; // add button to buttons panel buttonsPanel.AddChild(button); } // add panel to given parent if (parent != null) { parent.AddChild(panel); } // add panel to active ui root else { UserInterface.Active.AddEntity(panel); } return(ret); }