From 16235c21c0cb1e6a2924dccadb288c14b2109747 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Tue, 22 Oct 2024 21:34:28 +0200 Subject: [PATCH] Add optional direction option to context menu --- Source/Editor/GUI/ContextMenu/ContextMenu.cs | 4 +-- .../Editor/GUI/ContextMenu/ContextMenuBase.cs | 25 +++++++++++++++++-- .../Editor/Surface/ContextMenu/VisjectCM.cs | 2 +- Source/Editor/Windows/Search/ContentFinder.cs | 4 +-- 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/Source/Editor/GUI/ContextMenu/ContextMenu.cs b/Source/Editor/GUI/ContextMenu/ContextMenu.cs index 80a2d7494..cb0eb09c1 100644 --- a/Source/Editor/GUI/ContextMenu/ContextMenu.cs +++ b/Source/Editor/GUI/ContextMenu/ContextMenu.cs @@ -355,14 +355,14 @@ namespace FlaxEditor.GUI.ContextMenu } /// - public override void Show(Control parent, Float2 location) + public override void Show(Control parent, Float2 location, ContextMenuDirection? direction = null) { // Remove last separator to make context menu look better int lastIndex = _panel.Children.Count - 1; if (lastIndex >= 0 && _panel.Children[lastIndex] is ContextMenuSeparator separator) separator.Dispose(); - base.Show(parent, location); + base.Show(parent, location, direction); } /// diff --git a/Source/Editor/GUI/ContextMenu/ContextMenuBase.cs b/Source/Editor/GUI/ContextMenu/ContextMenuBase.cs index 511cda2e5..121b3bf31 100644 --- a/Source/Editor/GUI/ContextMenu/ContextMenuBase.cs +++ b/Source/Editor/GUI/ContextMenu/ContextMenuBase.cs @@ -134,7 +134,8 @@ namespace FlaxEditor.GUI.ContextMenu /// /// Parent control to attach to it. /// Popup menu origin location in parent control coordinates. - public virtual void Show(Control parent, Float2 location) + /// The custom popup direction. Null to use automatic direction. + public virtual void Show(Control parent, Float2 location, ContextMenuDirection? direction = null) { Assert.IsNotNull(parent); @@ -166,7 +167,7 @@ namespace FlaxEditor.GUI.ContextMenu var monitorBounds = Platform.GetMonitorBounds(locationSS); var rightBottomLocationSS = locationSS + dpiSize; bool isUp = false, isLeft = false; - if (UseAutomaticDirectionFix) + if (UseAutomaticDirectionFix && direction == null) { var parentMenu = parent as ContextMenu; if (monitorBounds.Bottom < rightBottomLocationSS.Y) @@ -193,6 +194,26 @@ namespace FlaxEditor.GUI.ContextMenu locationSS.X -= dpiSize.X; } } + else if (direction.HasValue) + { + switch (direction.Value) + { + case ContextMenuDirection.RightUp: + isUp = true; + break; + case ContextMenuDirection.LeftDown: + isLeft = true; + break; + case ContextMenuDirection.LeftUp: + isLeft = true; + isUp = true; + break; + } + if (isLeft) + locationSS.X -= dpiSize.X; + if (isUp) + locationSS.Y -= dpiSize.Y; + } // Update direction flag if (isUp) diff --git a/Source/Editor/Surface/ContextMenu/VisjectCM.cs b/Source/Editor/Surface/ContextMenu/VisjectCM.cs index f31f1245a..21aa9ca7f 100644 --- a/Source/Editor/Surface/ContextMenu/VisjectCM.cs +++ b/Source/Editor/Surface/ContextMenu/VisjectCM.cs @@ -759,7 +759,7 @@ namespace FlaxEditor.Surface.ContextMenu } /// - public override void Show(Control parent, Float2 location) + public override void Show(Control parent, Float2 location, ContextMenuDirection? direction = null) { Show(parent, location, null); } diff --git a/Source/Editor/Windows/Search/ContentFinder.cs b/Source/Editor/Windows/Search/ContentFinder.cs index 06b5852dd..c6522255b 100644 --- a/Source/Editor/Windows/Search/ContentFinder.cs +++ b/Source/Editor/Windows/Search/ContentFinder.cs @@ -155,9 +155,9 @@ namespace FlaxEditor.Windows.Search } /// - public override void Show(Control parent, Float2 location) + public override void Show(Control parent, Float2 location, ContextMenuDirection? direction = null) { - base.Show(parent, location); + base.Show(parent, location, direction); // Setup _resultPanel.ScrollViewTo(Float2.Zero);