Skip to content

Commit 3677f40

Browse files
committed
improve error checking of ui elements with missing roots
1 parent 0a3ad73 commit 3677f40

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

MLEM.Ui/Elements/Element.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ private set {
5959
/// <summary>
6060
/// The scale that this ui element renders with
6161
/// </summary>
62-
public float Scale => this.Root.ActualScale;
62+
public float Scale => this.Root?.ActualScale ?? throw new NullReferenceException($"Cannot query scale of element {this} that does not have a root element");
6363
/// <summary>
6464
/// The <see cref="Anchor"/> that this element uses for positioning within its parent
6565
/// </summary>
@@ -320,7 +320,7 @@ public virtual bool PreventParentSpill {
320320
/// Returns whether this element is its <see cref="Root"/>'s <see cref="RootElement.SelectedElement"/>.
321321
/// Note that, unlike <see cref="IsSelectedActive"/>, this property will be <see langword="true"/> even if this element's <see cref="Root"/> is not the <see cref="UiControls.ActiveRoot"/>.
322322
/// </summary>
323-
public bool IsSelected => this.Root.SelectedElement == this;
323+
public bool IsSelected => this.Root?.SelectedElement == this;
324324
/// <summary>
325325
/// Returns whether this element is its <see cref="Controls"/>'s <see cref="UiControls.SelectedElement"/>.
326326
/// Note that <see cref="IsSelected"/> can be used to query whether this element is its <see cref="Root"/>'s <see cref="RootElement.SelectedElement"/> instead.
@@ -1061,7 +1061,8 @@ public Vector2 TransformInverse(Vector2 position) {
10611061
/// <param name="position">The position to transform</param>
10621062
/// <returns>The transformed position</returns>
10631063
public Vector2 TransformInverseAll(Vector2 position) {
1064-
position = Vector2.Transform(position, this.Root.InvTransform);
1064+
if (this.Root != null)
1065+
position = Vector2.Transform(position, this.Root.InvTransform);
10651066
foreach (var parent in this.GetParentTree().Reverse())
10661067
position = parent.TransformInverse(position);
10671068
return this.TransformInverse(position);

0 commit comments

Comments
 (0)