浏览代码

Make floating windows dock to closest corner

/main
Jens Holm 7 年前
当前提交
428479b6
共有 1 个文件被更改,包括 63 次插入11 次删除
  1. 74
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Manipulators/WindowDraggable.cs

74
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Manipulators/WindowDraggable.cs


{
bool m_Active;
bool m_DockLeft;
bool m_DockTop;
Rect m_PreviousParentRect;
m_PreviousParentRect = new Rect(0f, 0f, 0f, 0f);
}
protected override void RegisterCallbacksOnTarget()

target.RegisterCallback(new EventCallback<MouseUpEvent>(OnMouseUp), Capture.NoCapture);
target.RegisterCallback<PostLayoutEvent>(OnPostLayout);
target.RegisterCallback<PostLayoutEvent>(InitialLayoutSetup);
}
protected override void UnregisterCallbacksFromTarget()

}
evt.StopPropagation();
RefreshDocking();
}
void RefreshDocking()
{
Vector2 windowCenter = new Vector2(target.layout.x + target.layout.width * .5f, target.layout.y + target.layout.height * .5f);
windowCenter /= target.parent.layout.size;
m_DockLeft = windowCenter.x < .5f;
m_DockTop = windowCenter.y < .5f;
}
void InitialLayoutSetup(PostLayoutEvent postLayoutEvent)
{
m_PreviousParentRect = target.parent.layout;
target.UnregisterCallback<PostLayoutEvent>(InitialLayoutSetup);
target.RegisterCallback<PostLayoutEvent>(OnPostLayout);
RefreshDocking();
Rect inspectorViewRect = target.layout;
Rect windowRect = target.layout;
float minimumXPosition = target.layout.width - inspectorViewRect.width;
float maximumXPosition = target.parent.layout.width - target.layout.width;
Vector2 scaling = target.parent.layout.size / m_PreviousParentRect.size;
float minimumYPosition = target.layout.height - inspectorViewRect.height;
float maximumYPosition = target.parent.layout.height - target.layout.height;
Vector2 distanceFromEdge = Vector2.zero;
distanceFromEdge.x = m_DockLeft ? target.layout.x : (m_PreviousParentRect.width - target.layout.x - target.layout.width);
distanceFromEdge.y = m_DockTop ? target.layout.y: (m_PreviousParentRect.height - target.layout.y - target.layout.height);
inspectorViewRect.x = Mathf.Clamp(inspectorViewRect.x, minimumXPosition, maximumXPosition);
inspectorViewRect.y = Mathf.Clamp(inspectorViewRect.y, minimumYPosition, maximumYPosition);
Vector2 normalizedDistanceFromEdge = distanceFromEdge / m_PreviousParentRect.size;
windowRect.size *= scaling;
if (m_DockLeft)
{
windowRect.x = normalizedDistanceFromEdge.x * target.parent.layout.width;
}
else
{
windowRect.x = (1f - normalizedDistanceFromEdge.x) * target.parent.layout.width - windowRect.width;
}
if (m_DockTop)
{
windowRect.y = normalizedDistanceFromEdge.y * target.parent.layout.height;
}
else
{
windowRect.y = (1f - normalizedDistanceFromEdge.y) * target.parent.layout.height- windowRect.height;
}
inspectorViewRect.width = Mathf.Min(inspectorViewRect.width, target.layout.width);
inspectorViewRect.height = Mathf.Min(inspectorViewRect.height, target.layout.height);
float maximumXPosition = target.parent.layout.width - windowRect.width;
float maximumYPosition = target.parent.layout.height - windowRect.height;
target.layout = inspectorViewRect;
windowRect.x = Mathf.Clamp(windowRect.x, 0f, maximumXPosition);
windowRect.y = Mathf.Clamp(windowRect.y, 0f, maximumYPosition);
windowRect.width = Mathf.Min(windowRect.width, target.parent.layout.width);
windowRect.height = Mathf.Min(windowRect.height, target.parent.layout.height);
m_PreviousParentRect = target.parent.layout;
target.layout = windowRect;
}
}
}
正在加载...
取消
保存