浏览代码

Make ResizeSideHandle be aware of nearest corner

ResizeSideHandles resize function are split into three cases, and the
resize handle is recording which corner it is closest to when the mouse
is preseed. This is to figure out which direction the window should
resize in when maintaining aspect ratio.
/main
Jens Holm 7 年前
当前提交
7ba4ae46
共有 2 个文件被更改,包括 229 次插入12 次删除
  1. 222
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Manipulators/ResizeSideHandle.cs
  2. 19
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/GraphEditorView.cs

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


using System;
using System.Collections.Generic;
using System.Xml;
using UnityEngine;
using UnityEngine.Experimental.UIElements;

}
public Action OnResizeFinished;
bool m_DockingLeft;
bool m_DockingTop;
public ResizeSideHandle(VisualElement resizeTarget, ResizeHandleAnchor anchor)
{

}
}
ResizeDirection resizeDirection;
resizeDirection = ResizeDirection.Horizontal;
this.AddManipulator(new Draggable(mouseDelta => OnResizeFromHorizontal(mouseDelta, anchor)));
resizeDirection = ResizeDirection.Vertical;
this.AddManipulator(new Draggable(mouseDelta => OnResizeFromVertical(mouseDelta, anchor)));
}
else
{
this.AddManipulator(new Draggable(mouseDelta => OnResizeFromCorner(mouseDelta, anchor)));
}
RegisterCallback<MouseDownEvent>(HandleMouseDown);
RegisterCallback<MouseUpEvent>(HandleDraggableMouseUp);
}
static bool IsAnchorLeft(ResizeHandleAnchor anchor)
{
return anchor == ResizeHandleAnchor.Left || anchor == ResizeHandleAnchor.TopLeft || anchor == ResizeHandleAnchor.BottomLeft;
}
static bool IsAnchorTop(ResizeHandleAnchor anchor)
{
return anchor == ResizeHandleAnchor.Top || anchor == ResizeHandleAnchor.TopLeft || anchor == ResizeHandleAnchor.TopRight;
}
static bool IsAnchorCorner(ResizeHandleAnchor anchor)
{
return anchor == ResizeHandleAnchor.TopLeft ||
anchor == ResizeHandleAnchor.TopRight ||
anchor == ResizeHandleAnchor.BottomLeft ||
anchor == ResizeHandleAnchor.BottomRight;
}
Vector2 GetMinSize()
{
Vector2 minSize = new Vector2(60f, 60f);
if (!Mathf.Approximately(m_ResizeTarget.style.minWidth.value, 0f))
{
minSize.x = m_ResizeTarget.style.minWidth;
}
if (!Mathf.Approximately(m_ResizeTarget.style.minHeight.value, 0f))
{
minSize.y = m_ResizeTarget.style.minHeight.value;
}
return minSize;
}
float GetMaxHorizontalExpansion(bool expandingLeft)
{
float maxHorizontalExpansion;
if (expandingLeft)
{
maxHorizontalExpansion = m_ResizeTarget.layout.x;
}
else
{
maxHorizontalExpansion = m_ResizeTarget.parent.layout.width - m_ResizeTarget.layout.xMax;
}
if (maintainAspectRatio)
{
if (!m_DockingTop)
{
maxHorizontalExpansion = Mathf.Min(maxHorizontalExpansion, m_ResizeTarget.layout.y);
}
else
{
maxHorizontalExpansion = Mathf.Min(maxHorizontalExpansion, m_ResizeTarget.parent.layout.height - m_ResizeTarget.layout.yMax);
}
}
return maxHorizontalExpansion;
}
float GetMaxVerticalExpansion(bool expandingUp)
{
float maxVerticalExpansion;
if (expandingUp)
{
maxVerticalExpansion = m_ResizeTarget.layout.x;
resizeDirection = ResizeDirection.Any;
maxVerticalExpansion = m_ResizeTarget.parent.layout.width - m_ResizeTarget.layout.xMax;
bool moveWhileResizeHorizontal = anchor == ResizeHandleAnchor.TopLeft || anchor == ResizeHandleAnchor.BottomLeft || anchor == ResizeHandleAnchor.Left;
bool moveWhileResizeVertical = anchor == ResizeHandleAnchor.TopLeft || anchor == ResizeHandleAnchor.TopRight || anchor == ResizeHandleAnchor.Top;
if (maintainAspectRatio)
{
if (!m_DockingLeft)
{
maxVerticalExpansion = Mathf.Min(maxVerticalExpansion, m_ResizeTarget.layout.x);
}
else
{
maxVerticalExpansion = Mathf.Min(maxVerticalExpansion, m_ResizeTarget.parent.layout.width - m_ResizeTarget.layout.xMax);
}
}
this.AddManipulator(new Draggable(mouseDelta => OnResize(mouseDelta, resizeDirection, moveWhileResizeHorizontal, moveWhileResizeVertical, anchor)));
RegisterCallback<MouseUpEvent>(HandleDraggableMouseUp);
return maxVerticalExpansion;
void OnResize(Vector2 resizeDelta, ResizeDirection direction, bool moveWhileResizeHorizontal, bool moveWhileresizeVertical, ResizeHandleAnchor anchor)
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)
{
float resizeAmount = resizeDelta.x / 2f;
float aspectRatio = m_ResizeTarget.layout.width / m_ResizeTarget.layout.height;
Vector2 minSize = GetMinSize();
Rect newLayout = m_ResizeTarget.layout;
bool resizeLeft = anchor == ResizeHandleAnchor.Left;
if (stayWithinParentBounds)
{
resizeAmount = Mathf.Clamp(resizeAmount, GetMaxContraction(), GetMaxHorizontalExpansion(resizeLeft));
}
if (resizeLeft)
{
newLayout.xMin += resizeAmount;
}
else
{
newLayout.xMax += resizeAmount;
}
if (maintainAspectRatio)
{
if (!resizeLeft)
{
resizeAmount = -resizeAmount;
}
if (!m_DockingTop)
{
newLayout.yMin += resizeAmount;
}
else
{
newLayout.yMax -= resizeAmount;
}
}
m_ResizeTarget.layout = newLayout;
}
void OnResizeFromVertical(Vector2 resizeDelta, ResizeHandleAnchor anchor)
{
}
void OnResizeFromCorner(Vector2 resizeDelta, ResizeHandleAnchor anchor)
{
Vector2 normalizedResizeDelta = resizeDelta / 2f;
Vector2 minSize = GetMinSize();
}
void OnResize(Vector2 resizeDelta, ResizeDirection direction, ResizeHandleAnchor anchor)
{
Vector2 normalizedResizeDelta = resizeDelta / 2f;

Rect newLayout = m_ResizeTarget.layout;
if (anchor == ResizeHandleAnchor.Left || anchor == ResizeHandleAnchor.TopLeft || anchor == ResizeHandleAnchor.BottomLeft)
if (maintainAspectRatio)
{
bool dominantResizeAlongX = Mathf.Abs(normalizedResizeDelta.x) >= Mathf.Abs(normalizedResizeDelta.y);
float aspectRatio = m_ResizeTarget.layout.width / m_ResizeTarget.layout.height;
bool expanding;
if (dominantResizeAlongX)
{
expanding = (IsAnchorLeft(anchor) && normalizedResizeDelta.x < 0f) || (!IsAnchorLeft(anchor) && normalizedResizeDelta.x > 0f);
}
else
{
expanding = (IsAnchorTop(anchor) && normalizedResizeDelta.y < 0f) || (!IsAnchorTop(anchor) && normalizedResizeDelta.y > 0f);
}
if (dominantResizeAlongX)
{
normalizedResizeDelta.y = 0f;
float changeAlongY = Mathf.Abs(normalizedResizeDelta.x) / aspectRatio * .5f;
changeAlongY = expanding ? changeAlongY : -changeAlongY;
newLayout.yMin -= changeAlongY;
newLayout.yMax += changeAlongY;
}
else
{
normalizedResizeDelta.x = 0f;
float changeAlongX = Mathf.Abs(normalizedResizeDelta.y) * aspectRatio * .5f;
changeAlongX = expanding ? changeAlongX : -changeAlongX;
newLayout.xMin -= changeAlongX;
newLayout.xMax += changeAlongX;
}
}
if (IsAnchorLeft(anchor))
{
newLayout.xMin = Mathf.Min(newLayout.xMin + normalizedResizeDelta.x, newLayout.xMax - minSize.x);
}

}
if (anchor == ResizeHandleAnchor.Top || anchor == ResizeHandleAnchor.TopLeft || anchor == ResizeHandleAnchor.TopRight)
if (IsAnchorTop(anchor))
{
newLayout.yMin = Mathf.Min(newLayout.yMin + normalizedResizeDelta.y, newLayout.yMax - minSize.y);
}

}
m_ResizeTarget.layout = newLayout;
}
void HandleMouseDown(MouseDownEvent evt)
{
m_DockingLeft = m_ResizeTarget.layout.center.x / m_ResizeTarget.parent.layout.width < .5f;
m_DockingTop = m_ResizeTarget.layout.center.y / m_ResizeTarget.parent.layout.height < .5f;
Debug.Log("docking left: " + m_DockingLeft + " docking top: " + m_DockingTop);
}
void HandleDraggableMouseUp(MouseUpEvent evt)

19
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/GraphEditorView.cs


{
public WindowDockingLayout previewLayout = new WindowDockingLayout();
public WindowDockingLayout blackboardLayout = new WindowDockingLayout();
[NonSerialized]
public WindowDockingLayout defaultPreviewLayout = new WindowDockingLayout();
[NonSerialized]
public WindowDockingLayout defaultBlackboardLayout = new WindowDockingLayout();
}
public class GraphEditorView : VisualElement, IDisposable

{
if (showInProjectRequested != null)
showInProjectRequested();
}
GUILayout.Space(6);
if (GUILayout.Button("Reset Layout", EditorStyles.toolbarButton))
{
m_MasterPreviewView.layout = m_FloatingWindowsLayout.defaultPreviewLayout.GetLayout(layout);
m_BlackboardProvider.blackboard.layout = m_FloatingWindowsLayout.defaultBlackboardLayout.GetLayout(layout);
m_MasterPreviewView.UpdateRenderTextureOnNextLayoutChange();
UpdateSerializedWindowLayout();
}
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();

{
m_FloatingWindowsLayout = JsonUtility.FromJson<FloatingWindowsLayout>(serializedWindowLayout);
m_FloatingWindowsLayout.defaultPreviewLayout.CalculateDockingCornerAndOffset(m_MasterPreviewView.layout, layout);
m_FloatingWindowsLayout.defaultBlackboardLayout.CalculateDockingCornerAndOffset(m_BlackboardProvider.blackboard.layout, layout);
m_MasterPreviewView.layout = m_FloatingWindowsLayout.previewLayout.GetLayout(layout);
m_BlackboardProvider.blackboard.layout = m_FloatingWindowsLayout.blackboardLayout.GetLayout(layout);

{
m_FloatingWindowsLayout = new FloatingWindowsLayout();
m_FloatingWindowsLayout.defaultPreviewLayout.CalculateDockingCornerAndOffset(m_MasterPreviewView.layout, layout);
m_FloatingWindowsLayout.defaultBlackboardLayout.CalculateDockingCornerAndOffset(m_BlackboardProvider.blackboard.layout, layout);
}
}

正在加载...
取消
保存