Skip to content

Commit c9352d5

Browse files
authored
MLEM.Ui FAQ (#44)
* started work on a mlem.ui faq section * link to the faq in the ui
1 parent c5a39f5 commit c9352d5

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

Docs/articles/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
href: ui.md
1414
- name: MLEM.Ui Gallery
1515
href: ui_gallery.md
16+
- name: MLEM.Ui FAQ
17+
href: ui_faq.md
1618

1719
- name: MLEM.Extended
1820
- name: Tiled Extensions

Docs/articles/ui.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
**MLEM.Ui** is a Ui framework for MonoGame, FNA and KNI that features elements with automatic positioning and sizing. It contains various ready-made element types like buttons, paragraphs, text fields and more, along with the ability to easily create custom controls. It supports **mouse**, **keyboard**, **gamepad** and **touch** input with little to no additional setup work required.
44

5-
To see some of what MLEM.Ui can do, you can check out [the demo](https://github.com/Ellpeck/MLEM/blob/main/Demos/UiDemo.cs) as well.
5+
To see some of what MLEM.Ui can do, you can check out [the demo](https://github.com/Ellpeck/MLEM/blob/main/Demos/UiDemo.cs) as well. If you have additional questions about MLEM.Ui after reading this article, be sure to check the [MLEM.Ui FAQ](ui_faq.md) as well!
66

77
## About Ui Systems
88

Docs/articles/ui_faq.md

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

Comments
 (0)