|
| 1 | +# Frequently Asked Questions About MLEM.Ui |
| 2 | + |
| 3 | +This article compiles some frequently asked questions about MLEM.Ui and tries to answer them succinctly. If you haven't checked out the [introductory article](ui.md) to MLEM.Ui yet, it's recommended you do that first! |
| 4 | + |
| 5 | +## How Do I Structure My UI Code? |
| 6 | + |
| 7 | +Of course, you can structure your UI code any way you like, but here are two suggestions: the way that MLEM.Ui was designed for, and the way that many users have chosen to do it instead! |
| 8 | + |
| 9 | +### "Intended" Way |
| 10 | +MLEM.Ui was initially developed with the goal of not requiring users to create custom classes that extend any preexisting UI elements or even the `Element` class itself. The original idea was that users would write their UI code "on the fly", largely using [object initializers](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/object-and-collection-initializers) to configure elements, and any frequently used constructs would be put into static methods that returned a group, panel or other element with a set of children, similarly to the [ElementHelper class](xref:MLEM.Ui.Elements.ElementHelper). |
| 11 | + |
| 12 | +```cs |
| 13 | +private void InitializeMyUi() { |
| 14 | + var box = InfoBox("This is some example text!"); |
| 15 | + this.UiSystem.Add("InfoBox", box); |
| 16 | +} |
| 17 | + |
| 18 | +private static Element InfoBox(string text) { |
| 19 | + var box = new Panel(Anchor.Center, new Vector2(100, 1), Vector2.Zero, setHeightBasedOnChildren: true); |
| 20 | + box.AddChild(new Paragraph(Anchor.AutoLeft, 1, text)); |
| 21 | + box.AddChild(new Button(Anchor.AutoCenter, new Vector2(0.5F, 20), "Okay") { |
| 22 | + OnPressed = element => element.System.Remove("InfoBox"), |
| 23 | + PositionOffset = new Vector2(0, 1) |
| 24 | + }); |
| 25 | + return box; |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +### "Custom Classes" Way |
| 30 | +I've since noticed many users opting for a custom class structure instead, where they will extend `Panel` or `Group` and add all the required child elements in their constructor. This is, of course, also a great way of structuring your UI, but with the way that the MLEM.Ui API works, it [poses some issues](https://github.com/Ellpeck/MLEM/issues/43) for the virtual methods and properties frequently used for element configuration. To get around this issue, you can always make your custom classes `sealed` or call the `base` versions of the properties and methods that configure your elements instead. |
| 31 | + |
| 32 | +```cs |
| 33 | +private void InitializeMyUi() { |
| 34 | + var box = new InfoBox("This is some example text!"); |
| 35 | + this.UiSystem.Add("InfoBox", box); |
| 36 | +} |
| 37 | + |
| 38 | +private sealed class InfoBox : Panel { |
| 39 | + |
| 40 | + public InfoBox(string text) : base(Anchor.Center, new Vector2(100, 1), Vector2.Zero, setHeightBasedOnChildren: true) { |
| 41 | + this.AddChild(new Paragraph(Anchor.AutoLeft, 1, text)); |
| 42 | + this.AddChild(new Button(Anchor.AutoCenter, new Vector2(0.5F, 20), "Okay") { |
| 43 | + OnPressed = element => element.System.Remove("InfoBox"), |
| 44 | + PositionOffset = new Vector2(0, 1) |
| 45 | + }); |
| 46 | + } |
| 47 | + |
| 48 | +} |
| 49 | +``` |
0 commit comments