Skip to content

Commit cb47c64

Browse files
committed
added messageboxes + example for them
1 parent adca581 commit cb47c64

File tree

4 files changed

+201
-0
lines changed

4 files changed

+201
-0
lines changed

GeonBit.UI/GeonBit.UI.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
<Compile Include="Source\InputHelper.cs" />
7979
<Compile Include="Source\Resources.cs" />
8080
<Compile Include="Source\UserInterface.cs" />
81+
<Compile Include="Source\Utils\MessageBox.cs" />
8182
</ItemGroup>
8283
<ItemGroup>
8384
<Reference Include="MonoGame.Framework">

GeonBit.UI/GeonBitUI_Examples.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,58 @@ Note that in order to use clipping and scrollbar with Panels you need to set the
734734
}
735735
}
736736

737+
// example: messages
738+
{
739+
// create panel and add to list of panels and manager
740+
Panel panel = new Panel(new Vector2(450, 560));
741+
panels.Add(panel);
742+
UserInterface.Active.AddEntity(panel);
743+
744+
// add title and text
745+
panel.AddChild(new Header("Message Box"));
746+
panel.AddChild(new HorizontalLine());
747+
panel.AddChild(new Paragraph("GeonBit.UI comes with a utility to create simple message boxes:"));
748+
749+
// button to create simple message box
750+
{
751+
var btn = new Button("Show Simple Message", ButtonSkin.Default);
752+
btn.OnClick += (Entities.Entity entity) =>
753+
{
754+
Utils.MessageBox.ShowMsgBox("Hello World!", "This is a simple message box. It doesn't say much, really.");
755+
};
756+
panel.AddChild(btn);
757+
}
758+
759+
// button to create message box with custombuttons
760+
panel.AddChild(new Paragraph("Or you can create custom message and buttons:"));
761+
{
762+
var btn = new Button("Show Custom Message", ButtonSkin.Default);
763+
btn.OnClick += (Entities.Entity entity) =>
764+
{
765+
Utils.MessageBox.ShowMsgBox("Custom Message!", "In this message there are two custom buttons.\n\nYou can set different actions per button. For example, click on 'Surprise' and see what happens!", new Utils.MessageBox.MsgBoxOption[] {
766+
new Utils.MessageBox.MsgBoxOption("Close", () => { return true; }),
767+
new Utils.MessageBox.MsgBoxOption("Surprise", () => { Utils.MessageBox.ShowMsgBox("Files Removed Successfully", "Win32 was successfully removed from this computer. Please restart to complete OS destruction.\n\n(Just kidding!)"); return true; })
768+
});
769+
};
770+
panel.AddChild(btn);
771+
}
772+
773+
// button to create message with extras
774+
panel.AddChild(new Paragraph("And you can also add extra entities to the message box:"));
775+
{
776+
var btn = new Button("Message With Extras", ButtonSkin.Default);
777+
btn.OnClick += (Entities.Entity entity) =>
778+
{
779+
var textInput = new TextInput(false);
780+
textInput.PlaceholderText = "Enter your name";
781+
Utils.MessageBox.ShowMsgBox("Message With Extra!", "In this message box we attached an extra entity from outside (a simple text input).\n\nPretty neat, huh?", new Utils.MessageBox.MsgBoxOption[] {
782+
new Utils.MessageBox.MsgBoxOption("Close", () => { return true; }),
783+
}, new Entity[] { textInput });
784+
};
785+
panel.AddChild(btn);
786+
}
787+
}
788+
737789
// example: disabled
738790
{
739791
// create panel and add to list of panels and manager

GeonBit.UI/Source/Utils/MessageBox.cs

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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+
}

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,6 +1434,12 @@ If true, will limit input-length to fit in TextInput physical size. This propert
14341434

14351435
List of input validators / post-processors, to add special rules and limitations on this text input. For example, if you want English-only text, or numeric input only, etc.
14361436

1437+
For example, the following will attach slug validator to a text input:
1438+
1439+
```cs
1440+
textInput.Validators.Add(new GeonBit.UI.Entities.TextValidators.SlugValidator());
1441+
```
1442+
14371443
To learn more about validators, see the docs: [TextValidators]( https://ronenness.github.io/GeonBit.UI-docs/html/N_GeonBit_UI_Entities_TextValidators.htm ).
14381444

14391445
#### PlaceholderText
@@ -1775,6 +1781,7 @@ For older MonoGame versions, see [tag 2.1.0.0](https://github.com/RonenNess/Geon
17751781
- Added outline opacity property.
17761782
- Added some text validators.
17771783
- Improved existing text validators efficiency + added support in spaces / no spaces.
1784+
- Added utility to generate message boxes.
17781785

17791786
## Credits
17801787

0 commit comments

Comments
 (0)