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)
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;
}