예제 #1
0
        /// <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);
        }