Prevent panels dragging outside screen

This commit is contained in:
Sinai 2021-05-15 06:21:42 +10:00
parent 54f78ac10b
commit 72c3af3dd7

View File

@ -71,10 +71,22 @@ namespace UnityExplorer.UI.Panels
if (handledInstanceThisFrame) if (handledInstanceThisFrame)
break; break;
} }
if (wasAnyDragging && state == MouseState.NotPressed)
{
foreach (var instance in Instances)
instance.WasDragging = false;
wasAnyDragging = false;
}
} }
#endregion #endregion
public static bool ResizePrompting => s_resizeCursorObj && s_resizeCursorObj.activeSelf;
public static GameObject s_resizeCursorObj;
internal static bool wasAnyDragging;
// Instance // Instance
public UIPanel UIPanel { get; private set; } public UIPanel UIPanel { get; private set; }
@ -94,8 +106,6 @@ namespace UnityExplorer.UI.Panels
// Resizing // Resizing
private const int RESIZE_THICKNESS = 10; private const int RESIZE_THICKNESS = 10;
public static GameObject s_resizeCursorObj;
//internal readonly Vector2 minResize = new Vector2(200, 50); //internal readonly Vector2 minResize = new Vector2(200, 50);
private bool WasResizing { get; set; } private bool WasResizing { get; set; }
@ -104,8 +114,6 @@ namespace UnityExplorer.UI.Panels
private bool WasHoveringResize => s_resizeCursorObj.activeInHierarchy; private bool WasHoveringResize => s_resizeCursorObj.activeInHierarchy;
public static bool ResizePrompting => s_resizeCursorObj && s_resizeCursorObj.activeSelf;
private ResizeTypes m_lastResizeHoverType; private ResizeTypes m_lastResizeHoverType;
private Rect m_totalResizeRect; private Rect m_totalResizeRect;
@ -217,17 +225,26 @@ namespace UnityExplorer.UI.Panels
public void OnBeginDrag() public void OnBeginDrag()
{ {
wasAnyDragging = true;
WasDragging = true; WasDragging = true;
m_lastDragPosition = InputManager.MousePosition; m_lastDragPosition = InputManager.MousePosition;
} }
public void OnDrag() public void OnDrag()
{ {
Vector2 diff = (Vector2)InputManager.MousePosition - m_lastDragPosition; var mousePos = InputManager.MousePosition;
m_lastDragPosition = 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; Panel.localPosition = pos;
} }