Skip to content

Commit 723d720

Browse files
committed
Added Element.DebugName to allow describing ui elements in ToString
1 parent c9352d5 commit 723d720

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ MLEM uses [semantic versioning](https://semver.org/). Potentially breaking chang
66
### MLEM.Ui
77
Additions
88
- Added UiSystem.OnElementAreaDirty and Element.OnAreaDirty events
9+
- Added Element.DebugName to allow describing ui elements in ToString
910

1011
Improvements
1112
- Made the exception thrown after the element recursion limit is reached more helpful

MLEM.Ui/Elements/Element.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,11 @@ public virtual bool PreventParentSpill {
336336
/// Note that, if no element is previously selected and auto-navigation is invoked, this element cannot be chosen as the first element to navigate to if its auto-nav group is non-null.
337337
/// </summary>
338338
public virtual string AutoNavGroup { get; set; }
339+
/// <summary>
340+
/// An optional string to display as this element's name in its <see cref="ToString"/> method.
341+
/// If this is unset, this element's underlying type's <see cref="Type.Name"/> is displayed instead.
342+
/// </summary>
343+
public virtual string DebugName { get; set; }
339344

340345
/// <summary>
341346
/// This Element's current <see cref="UiStyle"/>.
@@ -973,7 +978,7 @@ public virtual bool StopAnimation(UiAnimation animation) {
973978

974979
/// <inheritdoc />
975980
public override string ToString() {
976-
var ret = this.GetType().Name;
981+
var ret = this.DebugName ?? this.GetType().Name;
977982
// elements will contain their path up to the root and their index in each parent
978983
// eg Paragraph 2 @ Panel 3 @ ... @ Group RootName
979984
if (this.Parent != null) {

Tests/UiTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,21 @@ public void TestIssue40() {
266266
this.Game.UiSystem.Remove("Test");
267267
}
268268

269+
[Test]
270+
public void TestToString() {
271+
var panel = new Panel(Anchor.TopLeft, Vector2.One);
272+
var group = panel.AddChild(new Group(Anchor.TopLeft, Vector2.One, false, false));
273+
var button = group.AddChild(new Button(Anchor.TopLeft, Vector2.One));
274+
275+
Assert.AreEqual(button.ToString(), "Button 0 @ Group 0 @ Panel");
276+
this.AddAndUpdate(panel, out _, out _);
277+
Assert.AreEqual(button.ToString(), "Button 0 @ Group 0 @ Panel Test");
278+
279+
button.DebugName = "Test Button";
280+
group.DebugName = "Test Group";
281+
Assert.AreEqual(button.ToString(), "Test Button 0 @ Test Group 0 @ Panel Test");
282+
}
283+
269284
private void AddAndUpdate(Element element, out TimeSpan addTime, out TimeSpan updateTime) {
270285
foreach (var root in this.Game.UiSystem.GetRootElements())
271286
this.Game.UiSystem.Remove(root.Name);

0 commit comments

Comments
 (0)