From 72c3af3dd71bb36c57199641d32a5785e70c3180 Mon Sep 17 00:00:00 2001 From: Sinai Date: Sat, 15 May 2021 06:21:42 +1000 Subject: [PATCH] Prevent panels dragging outside screen --- src/UI/Panels/PanelDragger.cs | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/src/UI/Panels/PanelDragger.cs b/src/UI/Panels/PanelDragger.cs index 4b2d09c..1809b02 100644 --- a/src/UI/Panels/PanelDragger.cs +++ b/src/UI/Panels/PanelDragger.cs @@ -71,10 +71,22 @@ namespace UnityExplorer.UI.Panels if (handledInstanceThisFrame) break; } + + if (wasAnyDragging && state == MouseState.NotPressed) + { + foreach (var instance in Instances) + instance.WasDragging = false; + wasAnyDragging = false; + } } #endregion + public static bool ResizePrompting => s_resizeCursorObj && s_resizeCursorObj.activeSelf; + public static GameObject s_resizeCursorObj; + + internal static bool wasAnyDragging; + // Instance public UIPanel UIPanel { get; private set; } @@ -94,8 +106,6 @@ namespace UnityExplorer.UI.Panels // Resizing private const int RESIZE_THICKNESS = 10; - public static GameObject s_resizeCursorObj; - //internal readonly Vector2 minResize = new Vector2(200, 50); private bool WasResizing { get; set; } @@ -104,8 +114,6 @@ namespace UnityExplorer.UI.Panels private bool WasHoveringResize => s_resizeCursorObj.activeInHierarchy; - public static bool ResizePrompting => s_resizeCursorObj && s_resizeCursorObj.activeSelf; - private ResizeTypes m_lastResizeHoverType; private Rect m_totalResizeRect; @@ -217,17 +225,26 @@ namespace UnityExplorer.UI.Panels public void OnBeginDrag() { + wasAnyDragging = true; WasDragging = true; m_lastDragPosition = InputManager.MousePosition; } public void OnDrag() { - Vector2 diff = (Vector2)InputManager.MousePosition - m_lastDragPosition; - m_lastDragPosition = InputManager.MousePosition; + var mousePos = InputManager.MousePosition; + + Vector2 diff = (Vector2)mousePos - m_lastDragPosition; + m_lastDragPosition = mousePos; + + var pos = Panel.localPosition + (Vector3)diff; + + // Prevent panel going oustide screen bounds + var halfW = Screen.width * 0.5f; + var halfH = Screen.height * 0.5f; + pos.x = Math.Max(-halfW, Math.Min(pos.x, halfW - Panel.rect.width)); + pos.y = Math.Max(-halfH + Panel.rect.height, Math.Min(pos.y, halfH)); - Vector3 pos = Panel.localPosition; - pos += (Vector3)diff; Panel.localPosition = pos; }