浏览代码

Split handling of resize in ResizeSideHandle into each direction

/main
Jens Holm 7 年前
当前提交
f19eae8b
共有 1 个文件被更改,包括 277 次插入153 次删除
  1. 430
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Manipulators/ResizeSideHandle.cs

430
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Manipulators/ResizeSideHandle.cs


using System.Collections.Generic;
using System.Xml;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Experimental.UIElements;
namespace UnityEditor.ShaderGraph.Drawing

bool m_DockingLeft;
bool m_DockingTop;
bool m_Dragging;
float m_InitialAspectRatio;
Rect m_ResizeBeginLayout;
Vector2 m_ResizeBeginMousePosition;
public ResizeSideHandle(VisualElement resizeTarget, ResizeHandleAnchor anchor)
{

{
AddToClassList("vertical");
AddToClassList("top");
RegisterCallback<MouseMoveEvent>(HandleResizeFromTop);
break;
}
case ResizeHandleAnchor.TopRight:

RegisterCallback<MouseMoveEvent>(HandleResizeFromTopRight);
break;
}
case ResizeHandleAnchor.Right:

RegisterCallback<MouseMoveEvent>(HandleResizeFromRight);
break;
}
case ResizeHandleAnchor.BottomRight:

RegisterCallback<MouseMoveEvent>(HandleResizeFromBottomRight);
break;
}
case ResizeHandleAnchor.Bottom:

RegisterCallback<MouseMoveEvent>(HandleResizeFromBottom);
break;
}
case ResizeHandleAnchor.BottomLeft:

RegisterCallback<MouseMoveEvent>(HandleResizeFromBottomLeft);
break;
}
case ResizeHandleAnchor.Left:

RegisterCallback<MouseMoveEvent>(HandleResizeFromLeft);
break;
}
case ResizeHandleAnchor.TopLeft:

RegisterCallback<MouseMoveEvent>(HandleResizeFromTopLeft);
if (anchor == ResizeHandleAnchor.Left || anchor == ResizeHandleAnchor.Right)
{
this.AddManipulator(new Draggable(mouseDelta => OnResizeFromHorizontal(mouseDelta, anchor)));
}
else if (anchor == ResizeHandleAnchor.Top || anchor == ResizeHandleAnchor.Bottom)
{
this.AddManipulator(new Draggable(mouseDelta => OnResizeFromVertical(mouseDelta, anchor)));
}
else
{
this.AddManipulator(new Draggable(mouseDelta => OnResizeFromCorner(mouseDelta, anchor)));
}
m_ResizeTarget.RegisterCallback<PostLayoutEvent>(InitialLayoutSetup);
}
void InitialLayoutSetup(PostLayoutEvent evt)
{
m_ResizeTarget.UnregisterCallback<PostLayoutEvent>(InitialLayoutSetup);
m_InitialAspectRatio = m_ResizeTarget.layout.width / m_ResizeTarget.layout.height;
}
Vector2 GetMinSize()

if (expandingLeft)
{
maxHorizontalExpansion = m_ResizeTarget.layout.x;
maxHorizontalExpansion = m_ResizeBeginLayout.x;
maxHorizontalExpansion = m_ResizeTarget.parent.layout.width - m_ResizeTarget.layout.xMax;
maxHorizontalExpansion = m_ResizeTarget.parent.layout.width - m_ResizeBeginLayout.xMax;
}
if (maintainAspectRatio)

maxHorizontalExpansion = Mathf.Min(maxHorizontalExpansion, m_ResizeTarget.layout.y);
maxHorizontalExpansion = Mathf.Min(maxHorizontalExpansion, m_ResizeBeginLayout.y);
maxHorizontalExpansion = Mathf.Min(maxHorizontalExpansion, m_ResizeTarget.parent.layout.height - m_ResizeTarget.layout.yMax);
maxHorizontalExpansion = Mathf.Min(maxHorizontalExpansion, m_ResizeTarget.parent.layout.height - m_ResizeBeginLayout.yMax);
}
}

if (expandingUp)
{
maxVerticalExpansion = m_ResizeTarget.layout.y;
maxVerticalExpansion = m_ResizeBeginLayout.y;
maxVerticalExpansion = m_ResizeTarget.parent.layout.height - m_ResizeTarget.layout.yMax;
maxVerticalExpansion = m_ResizeTarget.parent.layout.height - m_ResizeBeginLayout.yMax;
}
if (maintainAspectRatio)

maxVerticalExpansion = Mathf.Min(maxVerticalExpansion, m_ResizeTarget.layout.x);
maxVerticalExpansion = Mathf.Min(maxVerticalExpansion, m_ResizeBeginLayout.x);
maxVerticalExpansion = Mathf.Min(maxVerticalExpansion, m_ResizeTarget.parent.layout.width - m_ResizeTarget.layout.xMax);
maxVerticalExpansion = Mathf.Min(maxVerticalExpansion, m_ResizeTarget.parent.layout.width - m_ResizeBeginLayout.xMax);
}
}

float GetMaxContraction()
{
Vector2 minSize = GetMinSize();
return Mathf.Min(m_ResizeTarget.layout.width - minSize.x, m_ResizeTarget.layout.height - minSize.y);
}
void OnResizeFromHorizontal(Vector2 resizeDelta, ResizeHandleAnchor anchor)
void HandleResizeFromTop(MouseMoveEvent mouseMoveEvent)
float resizeAmount = resizeDelta.x / 2f;
if (!m_Dragging)
{
return;
}
Rect newLayout = m_ResizeTarget.layout;
Vector2 restrictedMousePosition = m_ResizeTarget.parent.WorldToLocal(mouseMoveEvent.mousePosition);
bool resizeLeft = anchor == ResizeHandleAnchor.Left;
restrictedMousePosition.y = Mathf.Min(restrictedMousePosition.y, m_ResizeBeginLayout.yMax - GetMinSize().y);
if (resizeLeft)
restrictedMousePosition.y = Mathf.Max(restrictedMousePosition.y, m_ResizeBeginMousePosition.y - GetMaxVerticalExpansion(true));
}
Vector2 delta = restrictedMousePosition - m_ResizeBeginMousePosition;
Rect newLayout = m_ResizeBeginLayout;
newLayout.yMin = m_ResizeBeginLayout.yMin + delta.y;
if (maintainAspectRatio)
{
if (m_DockingLeft)
resizeAmount = Mathf.Clamp(resizeAmount, -GetMaxHorizontalExpansion(resizeLeft), GetMaxContraction());
newLayout.width = newLayout.height * m_InitialAspectRatio;
resizeAmount = Mathf.Clamp(resizeAmount, -GetMaxContraction(), GetMaxHorizontalExpansion(resizeLeft));
newLayout.xMin = newLayout.xMax - (newLayout.height * m_InitialAspectRatio);
if (resizeLeft)
m_ResizeTarget.layout = newLayout;
mouseMoveEvent.StopPropagation();
}
void HandleResizeFromTopRight(MouseMoveEvent mouseMoveEvent)
{
if (!m_Dragging)
newLayout.xMin += resizeAmount;
return;
else
Vector2 restrictedMousePosition = m_ResizeTarget.parent.WorldToLocal(mouseMoveEvent.mousePosition);
restrictedMousePosition.x = Mathf.Max(restrictedMousePosition.x, m_ResizeBeginLayout.xMin + GetMinSize().x);
restrictedMousePosition.y = Mathf.Min(restrictedMousePosition.y, m_ResizeBeginLayout.yMax - GetMinSize().y);
if (stayWithinParentBounds)
newLayout.xMax += resizeAmount;
restrictedMousePosition.x = Mathf.Min(restrictedMousePosition.x, m_ResizeBeginMousePosition.x + GetMaxHorizontalExpansion(false));
restrictedMousePosition.y = Mathf.Max(restrictedMousePosition.y, 0f);
Vector2 delta = restrictedMousePosition - m_ResizeBeginMousePosition;
Rect newLayout = m_ResizeBeginLayout;
newLayout.width += delta.x;
newLayout.yMin += delta.y;
if (!resizeLeft)
if (newLayout.width < newLayout.height * m_InitialAspectRatio)
resizeAmount = -resizeAmount;
}
if (!m_DockingTop)
{
newLayout.yMin += resizeAmount;
newLayout.yMin = Mathf.Min(newLayout.yMax - newLayout.width / m_InitialAspectRatio, newLayout.yMax - GetMinSize().y);
newLayout.yMax -= resizeAmount;
newLayout.width = newLayout.height * m_InitialAspectRatio;
mouseMoveEvent.StopPropagation();
void OnResizeFromVertical(Vector2 resizeDelta, ResizeHandleAnchor anchor)
void HandleResizeFromRight(MouseMoveEvent mouseMoveEvent)
float resizeAmount = resizeDelta.y / 2f;
if (!m_Dragging)
{
return;
}
Rect newLayout = m_ResizeTarget.layout;
Vector2 restrictedMousePosition = m_ResizeTarget.parent.WorldToLocal(mouseMoveEvent.mousePosition);
bool resizeUp = anchor == ResizeHandleAnchor.Top;
restrictedMousePosition.x = Mathf.Max(restrictedMousePosition.x, m_ResizeBeginLayout.xMin + GetMinSize().x);
if (resizeUp)
restrictedMousePosition.x = Mathf.Min(restrictedMousePosition.x, m_ResizeBeginMousePosition.x + GetMaxHorizontalExpansion(false));
}
Vector2 delta = restrictedMousePosition - m_ResizeBeginMousePosition;
Rect newLayout = m_ResizeBeginLayout;
newLayout.xMax = m_ResizeBeginLayout.xMax + delta.x;
if (maintainAspectRatio)
{
if (m_DockingTop)
resizeAmount = Mathf.Clamp(resizeAmount, -GetMaxVerticalExpansion(resizeUp), GetMaxContraction());
newLayout.height = newLayout.width / m_InitialAspectRatio;
resizeAmount = Mathf.Clamp(resizeAmount, -GetMaxContraction(), GetMaxVerticalExpansion(resizeUp));
newLayout.yMin = newLayout.yMax - (newLayout.width / m_InitialAspectRatio);
if (resizeUp)
m_ResizeTarget.layout = newLayout;
mouseMoveEvent.StopPropagation();
}
void HandleResizeFromBottomRight(MouseMoveEvent mouseMoveEvent)
{
if (!m_Dragging)
newLayout.yMin += resizeAmount;
return;
else
Vector2 restrictedMousePosition = m_ResizeTarget.parent.WorldToLocal(mouseMoveEvent.mousePosition);
restrictedMousePosition.x = Mathf.Max(restrictedMousePosition.x, m_ResizeBeginLayout.xMin + GetMinSize().x);
restrictedMousePosition.y = Mathf.Max(restrictedMousePosition.y, m_ResizeBeginLayout.yMin + GetMinSize().y);
if (stayWithinParentBounds)
newLayout.yMax += resizeAmount;
restrictedMousePosition.x = Mathf.Min(restrictedMousePosition.x, m_ResizeBeginMousePosition.x + GetMaxHorizontalExpansion(false));
restrictedMousePosition.y = Mathf.Min(restrictedMousePosition.y, m_ResizeBeginMousePosition.y + GetMaxVerticalExpansion(false));
Vector2 delta = restrictedMousePosition - m_ResizeBeginMousePosition;
Rect newLayout = m_ResizeBeginLayout;
newLayout.size += delta;
if (!resizeUp)
if (newLayout.width < newLayout.height * m_InitialAspectRatio)
resizeAmount = -resizeAmount;
}
if (!m_DockingLeft)
{
newLayout.xMin += resizeAmount;
newLayout.height = newLayout.width / m_InitialAspectRatio;
newLayout.xMax -= resizeAmount;
newLayout.width = newLayout.height * m_InitialAspectRatio;
mouseMoveEvent.StopPropagation();
void OnResizeFromCorner(Vector2 resizeDelta, ResizeHandleAnchor anchor)
void HandleResizeFromBottom(MouseMoveEvent mouseMoveEvent)
Rect newLayout = m_ResizeTarget.layout;
float resizeAmount;
if (!m_Dragging)
{
return;
}
Vector2 restrictedMousePosition = m_ResizeTarget.parent.WorldToLocal(mouseMoveEvent.mousePosition);
restrictedMousePosition.y = Mathf.Max(restrictedMousePosition.y, m_ResizeBeginLayout.yMin + GetMinSize().y);
if (maintainAspectRatio)
if (stayWithinParentBounds)
bool horizontalDominant;
restrictedMousePosition.y = Mathf.Min(restrictedMousePosition.y, m_ResizeBeginMousePosition.y + GetMaxVerticalExpansion(false));
}
if (Mathf.Abs(resizeDelta.x) > Mathf.Abs(resizeDelta.y))
Vector2 delta = restrictedMousePosition - m_ResizeBeginMousePosition;
Rect newLayout = m_ResizeBeginLayout;
newLayout.yMax = m_ResizeBeginLayout.yMax + delta.y;
if (maintainAspectRatio)
{
if (m_DockingLeft)
resizeAmount = resizeDelta.x * .5f;
horizontalDominant = true;
newLayout.width = newLayout.height * m_InitialAspectRatio;
resizeAmount = resizeDelta.y * .5f;
horizontalDominant = false;
newLayout.xMin = newLayout.xMax - (newLayout.height * m_InitialAspectRatio);
}
switch (anchor)
{
case ResizeHandleAnchor.TopLeft:
{
float maxResizeAmount = Mathf.Min(GetMaxHorizontalExpansion(true), GetMaxVerticalExpansion(true));
float minResizeAmount = GetMaxContraction();
resizeAmount = Mathf.Clamp(resizeAmount, -maxResizeAmount, minResizeAmount);
newLayout.xMin += resizeAmount;
newLayout.yMin += resizeAmount;
break;
}
case ResizeHandleAnchor.TopRight:
{
float maxResizeAmount = Mathf.Min(GetMaxHorizontalExpansion(false), GetMaxVerticalExpansion(true));
float minResizeAmount = GetMaxContraction();
m_ResizeTarget.layout = newLayout;
if (horizontalDominant)
{
resizeAmount = Mathf.Clamp(resizeAmount, -minResizeAmount, maxResizeAmount);
newLayout.xMax += resizeAmount;
newLayout.yMin -= resizeAmount;
}
else
{
resizeAmount = Mathf.Clamp(resizeAmount, -maxResizeAmount, minResizeAmount);
newLayout.xMax -= resizeAmount;
newLayout.yMin += resizeAmount;
}
mouseMoveEvent.StopPropagation();
}
break;
}
case ResizeHandleAnchor.BottomLeft:
{
float maxResizeAmount = Mathf.Min(GetMaxHorizontalExpansion(true), GetMaxVerticalExpansion(false));
float minResizeAmount = GetMaxContraction();
void HandleResizeFromBottomLeft(MouseMoveEvent mouseMoveEvent)
{
if (!m_Dragging)
{
return;
}
if (horizontalDominant)
{
resizeAmount = Mathf.Clamp(resizeAmount, -maxResizeAmount, minResizeAmount);
newLayout.xMin += resizeAmount;
newLayout.yMax -= resizeAmount;
}
else
{
resizeAmount = Mathf.Clamp(resizeAmount, -minResizeAmount, maxResizeAmount);
newLayout.xMin -= resizeAmount;
newLayout.yMax += resizeAmount;
}
Vector2 restrictedMousePosition = m_ResizeTarget.parent.WorldToLocal(mouseMoveEvent.mousePosition);
break;
}
case ResizeHandleAnchor.BottomRight:
{
float maxResizeAmount = Mathf.Min(GetMaxHorizontalExpansion(false), GetMaxVerticalExpansion(false));
float minResizeAmount = GetMaxContraction();
resizeAmount = Mathf.Clamp(resizeAmount, -minResizeAmount, maxResizeAmount);
newLayout.xMax += resizeAmount;
newLayout.yMax += resizeAmount;
break;
}
default:
{
break;
}
}
restrictedMousePosition.x = Mathf.Min(restrictedMousePosition.x, m_ResizeBeginLayout.xMax - GetMinSize().x);
restrictedMousePosition.y = Mathf.Max(restrictedMousePosition.y, m_ResizeBeginLayout.yMin + GetMinSize().y);
if (stayWithinParentBounds)
{
restrictedMousePosition.x = Mathf.Max(restrictedMousePosition.x, 0f);
restrictedMousePosition.y = Mathf.Min(restrictedMousePosition.y, m_ResizeBeginMousePosition.y + GetMaxVerticalExpansion(false));
else
Vector2 delta = restrictedMousePosition - m_ResizeBeginMousePosition;
Rect newLayout = m_ResizeBeginLayout;
newLayout.xMin += delta.x;
newLayout.height += delta.y;
if (maintainAspectRatio)
Vector2 normalizedResizeDelta = resizeDelta * .5f;
Vector2 minSize = GetMinSize();
if (anchor == ResizeHandleAnchor.Left || anchor == ResizeHandleAnchor.TopLeft || anchor == ResizeHandleAnchor.BottomLeft)
if (newLayout.width < newLayout.height * m_InitialAspectRatio)
newLayout.xMin = Mathf.Min(newLayout.xMin + normalizedResizeDelta.x, newLayout.xMax - minSize.x);
newLayout.height = newLayout.width / m_InitialAspectRatio;
newLayout.xMax = Mathf.Max(newLayout.xMax + normalizedResizeDelta.x, newLayout.xMin + minSize.x);
newLayout.xMin = Mathf.Min(newLayout.xMax - newLayout.height * m_InitialAspectRatio, newLayout.xMax - GetMinSize().x);
}
if (anchor == ResizeHandleAnchor.Top || anchor == ResizeHandleAnchor.TopLeft || anchor == ResizeHandleAnchor.TopRight)
m_ResizeTarget.layout = newLayout;
mouseMoveEvent.StopPropagation();
}
void HandleResizeFromLeft(MouseMoveEvent mouseMoveEvent)
{
if (!m_Dragging)
{
return;
}
Vector2 restrictedMousePosition = m_ResizeTarget.parent.WorldToLocal(mouseMoveEvent.mousePosition);
restrictedMousePosition.x = Mathf.Min(restrictedMousePosition.x, m_ResizeBeginLayout.xMax - GetMinSize().x);
if (stayWithinParentBounds)
{
restrictedMousePosition.x = Mathf.Max(restrictedMousePosition.x, m_ResizeBeginMousePosition.x - GetMaxHorizontalExpansion(true));
}
Vector2 delta = restrictedMousePosition - m_ResizeBeginMousePosition;
Rect newLayout = m_ResizeBeginLayout;
newLayout.xMin = m_ResizeBeginLayout.xMin + delta.x;
if (maintainAspectRatio)
{
if (m_DockingTop)
newLayout.yMin = Mathf.Min(newLayout.yMin + normalizedResizeDelta.y, newLayout.yMax - minSize.y);
newLayout.height = newLayout.width / m_InitialAspectRatio;
newLayout.yMax = Mathf.Max(newLayout.yMax + normalizedResizeDelta.y, newLayout.yMin + minSize.y);
newLayout.yMin = newLayout.yMax - (newLayout.width / m_InitialAspectRatio);
}
m_ResizeTarget.layout = newLayout;
mouseMoveEvent.StopPropagation();
}
void HandleResizeFromTopLeft(MouseMoveEvent mouseMoveEvent)
{
if (!m_Dragging)
{
return;
}
if (stayWithinParentBounds)
Vector2 restrictedMousePosition = m_ResizeTarget.parent.WorldToLocal(mouseMoveEvent.mousePosition);
restrictedMousePosition.x = Mathf.Min(restrictedMousePosition.x, m_ResizeBeginLayout.xMax - GetMinSize().x);
restrictedMousePosition.y = Mathf.Min(restrictedMousePosition.y, m_ResizeBeginLayout.yMax - GetMinSize().y);
if (stayWithinParentBounds)
{
restrictedMousePosition.x = Mathf.Max(restrictedMousePosition.x, 0f);
restrictedMousePosition.y = Mathf.Max(restrictedMousePosition.y, 0f);
}
Vector2 delta = restrictedMousePosition - m_ResizeBeginMousePosition;
Rect newLayout = m_ResizeBeginLayout;
newLayout.xMin += delta.x;
newLayout.yMin += delta.y;
if (maintainAspectRatio)
{
if (newLayout.width < newLayout.height * m_InitialAspectRatio)
newLayout.xMin = Mathf.Max(newLayout.xMin, 0f);
newLayout.yMin = Mathf.Max(newLayout.yMin, 0f);
newLayout.xMax = Mathf.Min(newLayout.xMax, m_ResizeTarget.parent.layout.width);
newLayout.yMax = Mathf.Min(newLayout.yMax, m_ResizeTarget.parent.layout.height);
newLayout.yMin = Mathf.Min(newLayout.yMax - newLayout.width / m_InitialAspectRatio, newLayout.yMax - GetMinSize().y);
}
else
{
newLayout.xMin = Mathf.Min(newLayout.xMax - newLayout.height * m_InitialAspectRatio, newLayout.xMax - GetMinSize().x);
mouseMoveEvent.StopPropagation();
void HandleMouseDown(MouseDownEvent evt)
void HandleMouseDown(MouseDownEvent mouseDownEvent)
m_Dragging = true;
m_ResizeBeginLayout = m_ResizeTarget.layout;
m_ResizeBeginMousePosition = m_ResizeTarget.parent.WorldToLocal(mouseDownEvent.mousePosition);
m_Dragging = true;
this.TakeMouseCapture();
mouseDownEvent.StopPropagation();
void HandleDraggableMouseUp(MouseUpEvent evt)
void HandleDraggableMouseUp(MouseUpEvent mouseUpEvent)
m_Dragging = false;
if (this.HasMouseCapture())
{
this.ReleaseMouseCapture();
}
if (OnResizeFinished != null)
{
OnResizeFinished();

正在加载...
取消
保存