|
| 1 | +#region File Description |
| 2 | +//----------------------------------------------------------------------------- |
| 3 | +// Generate message boxes and other prompts. |
| 4 | +// |
| 5 | +// Author: Ronen Ness. |
| 6 | +// Since: 2017. |
| 7 | +//----------------------------------------------------------------------------- |
| 8 | +#endregion |
| 9 | +using Microsoft.Xna.Framework; |
| 10 | + |
| 11 | + |
| 12 | +namespace GeonBit.UI.Utils |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// Helper class to generate message boxes and prompts. |
| 16 | + /// </summary> |
| 17 | + public static class MessageBox |
| 18 | + { |
| 19 | + /// <summary> |
| 20 | + /// Default size to use for message boxes. |
| 21 | + /// </summary> |
| 22 | + public static Vector2 DefaultMsgBoxSize = new Vector2(480, 350); |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Default text for OK button. |
| 26 | + /// </summary> |
| 27 | + public static string DefaultOkButtonText = "OK"; |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Will block and fade background with this color while messages are opened. |
| 31 | + /// </summary> |
| 32 | + public static Color BackgroundFaderColor = new Color(0, 0, 0, 100); |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// A button / option for a message box. |
| 36 | + /// </summary> |
| 37 | + public class MsgBoxOption |
| 38 | + { |
| 39 | + /// <summary> |
| 40 | + /// Option title (for the button). |
| 41 | + /// </summary> |
| 42 | + public string Title; |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Callback to run when clicked. Return false to leave message box opened (true will close it). |
| 46 | + /// </summary> |
| 47 | + public System.Func<bool> Callback; |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Create the message box option. |
| 51 | + /// </summary> |
| 52 | + /// <param name="title">Text to write on the button.</param> |
| 53 | + /// <param name="callback">Action when clicked. Return false if you want to abort and leave the message opened, return true to close it.</param> |
| 54 | + public MsgBoxOption(string title, System.Func<bool> callback) |
| 55 | + { |
| 56 | + Title = title; |
| 57 | + Callback = callback; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Show a message box with custom buttons and callbacks. |
| 63 | + /// </summary> |
| 64 | + /// <param name="header">Messagebox header.</param> |
| 65 | + /// <param name="text">Main text.</param> |
| 66 | + /// <param name="options">Msgbox response options.</param> |
| 67 | + /// <param name="append">Optional array of entities to add to msg box under the text and above the buttons.</param> |
| 68 | + /// <param name="size">Alternative size to use.</param> |
| 69 | + /// <param name="onDone">Optional callback to call when this msgbox closes.</param> |
| 70 | + /// <returns>Message box panel.</returns> |
| 71 | + public static Entities.Panel ShowMsgBox(string header, string text, MsgBoxOption[] options, Entities.Entity[] append = null, Vector2? size = null, System.Action onDone = null) |
| 72 | + { |
| 73 | + // create panel for messagebox |
| 74 | + size = size ?? new Vector2(500, 500); |
| 75 | + var panel = new Entities.Panel(size.Value); |
| 76 | + panel.AddChild(new Entities.Header(header)); |
| 77 | + panel.AddChild(new Entities.HorizontalLine()); |
| 78 | + panel.AddChild(new Entities.Paragraph(text)); |
| 79 | + |
| 80 | + // add rectangle to hide and lock background |
| 81 | + Entities.ColoredRectangle fader = null; |
| 82 | + if (BackgroundFaderColor.A != 0) |
| 83 | + { |
| 84 | + fader = new Entities.ColoredRectangle(Vector2.Zero, Entities.Anchor.Center); |
| 85 | + fader.FillColor = new Color(0, 0, 0, 100); |
| 86 | + fader.OutlineWidth = 0; |
| 87 | + fader.ClickThrough = false; |
| 88 | + UserInterface.Active.AddEntity(fader); |
| 89 | + } |
| 90 | + |
| 91 | + // add custom appended entities |
| 92 | + if (append != null) |
| 93 | + { |
| 94 | + foreach (var entity in append) |
| 95 | + { |
| 96 | + panel.AddChild(entity); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + // add bottom buttons |
| 101 | + var buttonsPanel = new Entities.Panel(new Vector2(0, 70), Entities.PanelSkin.None, Entities.Anchor.BottomCenter); |
| 102 | + buttonsPanel.Padding = Vector2.Zero; |
| 103 | + panel.AddChild(buttonsPanel); |
| 104 | + var btnSize = new Vector2(options.Length == 1 ? 0f : (1f / options.Length), 60); |
| 105 | + foreach (var option in options) |
| 106 | + { |
| 107 | + var button = new Entities.Button(option.Title, anchor: Entities.Anchor.AutoInline, size: btnSize); |
| 108 | + button.OnClick += (Entities.Entity ent) => |
| 109 | + { |
| 110 | + if (option.Callback == null || option.Callback()) |
| 111 | + { |
| 112 | + if (fader != null) { fader.RemoveFromParent(); } |
| 113 | + panel.RemoveFromParent(); |
| 114 | + onDone?.Invoke(); |
| 115 | + } |
| 116 | + }; |
| 117 | + buttonsPanel.AddChild(button); |
| 118 | + } |
| 119 | + |
| 120 | + // add panel to active ui |
| 121 | + UserInterface.Active.AddEntity(panel); |
| 122 | + return panel; |
| 123 | + } |
| 124 | + |
| 125 | + /// <summary> |
| 126 | + /// Show a message box with just "OK". |
| 127 | + /// </summary> |
| 128 | + /// <param name="header">Message box title.</param> |
| 129 | + /// <param name="text">Main text to write on the message box.</param> |
| 130 | + /// <param name="closeButtonTxt">Text for the closing button (if not provided will use default).</param> |
| 131 | + /// <param name="size">Message box size (if not provided will use default).</param> |
| 132 | + /// <returns>Message box panel.</returns> |
| 133 | + public static Entities.Panel ShowMsgBox(string header, string text, string closeButtonTxt = null, Vector2? size = null) |
| 134 | + { |
| 135 | + return ShowMsgBox(header, text, new MsgBoxOption[] |
| 136 | + { |
| 137 | + new MsgBoxOption(closeButtonTxt ?? DefaultOkButtonText, null) |
| 138 | + }, size: size ?? DefaultMsgBoxSize); |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments