/// <summary> /// Open the dialogue. /// </summary> /// <param name="x">World x coordinate.</param> /// <param name="y">World y coordinate.</param> /// <param name="content">Content to display.</param> /// <param name="openDirection">Direction to open the dialogue towards.</param> internal void Open(float x, float y, VisualElement content, MenuUtilities.OpenDirection openDirection = MenuUtilities.OpenDirection.DownLeft) { // Set new content Clear(); Add(content); // Set visible and give focus RemoveFromClassList(UiConstants.ussHidden); Focus(); BringToFront(); // Catch scrolling to avoid menu becoming detached. parent.RegisterCallback <WheelEvent>(OnScroll, TrickleDown.TrickleDown); // Catch clicks outside of the dialogue and close it. parent.RegisterCallback <MouseDownEvent>(OnMouseDown, TrickleDown.TrickleDown); // Catch window size changes so that the menu position can be fixed. parent.RegisterCallback <GeometryChangedEvent>(OnGeometryChange, TrickleDown.TrickleDown); content.RegisterCallback <GeometryChangedEvent>(SizeKnownCallback); void SizeKnownCallback(GeometryChangedEvent _) { // Unregister now that we know it has a size. content.UnregisterCallback <GeometryChangedEvent>(SizeKnownCallback); // Set the position in the window SetPosition(x, y, content, openDirection); } }
/// <summary> /// Set the position of the dialogue. /// </summary> /// <param name="x">World x coordinate.</param> /// <param name="y">World y coordinate.</param> /// <param name="content">Content of the window.</param> /// <param name="openDirection">Direction to open the dialogue towards.</param> void SetPosition(float x, float y, VisualElement content, MenuUtilities.OpenDirection openDirection) { // Correct for the top left corner of the element based on the direction it opens. switch (openDirection) { case MenuUtilities.OpenDirection.UpLeft: x -= content.worldBound.width; y -= content.worldBound.height; break; case MenuUtilities.OpenDirection.UpRight: y -= content.worldBound.height; break; case MenuUtilities.OpenDirection.DownLeft: x -= content.worldBound.width; break; case MenuUtilities.OpenDirection.DownRight: break; default: throw new ArgumentOutOfRangeException(nameof(openDirection), openDirection, null); } // Set the position. style.top = y - parent.worldBound.yMin; style.left = x - parent.worldBound.xMin; style.right = new StyleLength(StyleKeyword.Auto); style.bottom = new StyleLength(StyleKeyword.Auto); }