This commit is contained in:
2026-11-24 15:51:02 +01:00
parent cca4d73e41
commit f4aa17114a
22092 changed files with 5042406 additions and 0 deletions
@@ -0,0 +1,75 @@
using System;
using UnityEngine;
using UnityEngine.UIElements;
namespace UnityEditor.ShaderGraph.Drawing
{
class Draggable : MouseManipulator
{
Action<Vector2> m_Handler;
bool m_Active;
bool m_OutputDeltaMovement;
public Draggable(Action<Vector2> handler, bool outputDeltaMovement = false)
{
m_Handler = handler;
m_Active = false;
m_OutputDeltaMovement = outputDeltaMovement;
activators.Add(new ManipulatorActivationFilter()
{
button = MouseButton.LeftMouse
});
}
protected override void RegisterCallbacksOnTarget()
{
target.RegisterCallback(new EventCallback<MouseDownEvent>(OnMouseDown), TrickleDownEnum.NoTrickleDown);
target.RegisterCallback(new EventCallback<MouseMoveEvent>(OnMouseMove), TrickleDownEnum.NoTrickleDown);
target.RegisterCallback(new EventCallback<MouseUpEvent>(OnMouseUp), TrickleDownEnum.NoTrickleDown);
}
protected override void UnregisterCallbacksFromTarget()
{
target.UnregisterCallback(new EventCallback<MouseDownEvent>(OnMouseDown), TrickleDownEnum.NoTrickleDown);
target.UnregisterCallback(new EventCallback<MouseMoveEvent>(OnMouseMove), TrickleDownEnum.NoTrickleDown);
target.UnregisterCallback(new EventCallback<MouseUpEvent>(OnMouseUp), TrickleDownEnum.NoTrickleDown);
}
void OnMouseDown(MouseDownEvent evt)
{
target.CaptureMouse();
m_Active = true;
evt.StopPropagation();
}
void OnMouseMove(MouseMoveEvent evt)
{
if (m_Active)
{
if (m_OutputDeltaMovement)
{
m_Handler(evt.mouseDelta);
}
else
{
m_Handler(evt.localMousePosition);
}
}
}
void OnMouseUp(MouseUpEvent evt)
{
m_Active = false;
if (target.HasMouseCapture())
{
target.ReleaseMouse();
}
evt.StopPropagation();
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ad4cd8fec883e80418e5427585b426db
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,357 @@
using System;
using UnityEditor.ShaderGraph.Drawing.Interfaces;
using UnityEngine;
using UnityEngine.UIElements;
namespace UnityEditor.ShaderGraph.Drawing
{
class ElementResizer : Manipulator
{
bool m_IsEnabled = true;
public bool isEnabled
{
get => m_IsEnabled;
set => m_IsEnabled = value;
}
public readonly ResizableElement.Resizer direction;
public readonly VisualElement resizedElement;
public ElementResizer(VisualElement resizedElement, ResizableElement.Resizer direction)
{
this.direction = direction;
this.resizedElement = resizedElement;
}
protected override void RegisterCallbacksOnTarget()
{
target.RegisterCallback<MouseDownEvent>(OnMouseDown);
target.RegisterCallback<MouseUpEvent>(OnMouseUp);
}
protected override void UnregisterCallbacksFromTarget()
{
target.UnregisterCallback<MouseDownEvent>(OnMouseDown);
target.UnregisterCallback<MouseUpEvent>(OnMouseUp);
}
Vector2 m_StartMouse;
Vector2 m_StartSize;
Vector2 m_MinSize;
Vector2 m_MaxSize;
Vector2 m_StartPosition;
bool m_DragStarted = false;
void OnMouseDown(MouseDownEvent e)
{
if (!isEnabled)
return;
if (e.button == 0 && e.clickCount == 1)
{
VisualElement resizedTarget = resizedElement.parent;
if (resizedTarget != null)
{
VisualElement resizedBase = resizedTarget.parent;
if (resizedBase != null)
{
target.RegisterCallback<MouseMoveEvent>(OnMouseMove);
e.StopPropagation();
target.CaptureMouse();
m_StartMouse = resizedBase.WorldToLocal(e.mousePosition);
m_StartSize = new Vector2(resizedTarget.resolvedStyle.width, resizedTarget.resolvedStyle.height);
m_StartPosition = new Vector2(resizedTarget.resolvedStyle.left, resizedTarget.resolvedStyle.top);
bool minWidthDefined = resizedTarget.resolvedStyle.minWidth != StyleKeyword.Auto;
bool maxWidthDefined = resizedTarget.resolvedStyle.maxWidth != StyleKeyword.None;
bool minHeightDefined = resizedTarget.resolvedStyle.minHeight != StyleKeyword.Auto;
bool maxHeightDefined = resizedTarget.resolvedStyle.maxHeight != StyleKeyword.None;
m_MinSize = new Vector2(
minWidthDefined ? resizedTarget.resolvedStyle.minWidth.value : Mathf.NegativeInfinity,
minHeightDefined ? resizedTarget.resolvedStyle.minHeight.value : Mathf.NegativeInfinity);
m_MaxSize = new Vector2(
maxWidthDefined ? resizedTarget.resolvedStyle.maxWidth.value : Mathf.Infinity,
maxHeightDefined ? resizedTarget.resolvedStyle.maxHeight.value : Mathf.Infinity);
m_DragStarted = false;
}
}
}
}
void ApplyLayoutToTargetAsStyle(Rect rect)
{
var resizedTarget = resizedElement.parent;
resizedTarget.style.left = rect.x;
resizedTarget.style.top = rect.y;
resizedTarget.style.width = rect.width;
resizedTarget.style.height = rect.height;
}
static Rect CropToParent(Rect rect, Rect parent)
{
rect.xMin = Mathf.Max(rect.xMin, parent.xMin);
rect.yMin = Mathf.Max(rect.yMin, parent.yMin);
rect.xMax = Mathf.Min(rect.xMax, parent.xMax);
rect.yMax = Mathf.Min(rect.yMax, parent.yMax);
return rect;
}
static float GetSquareSize(Rect rect, ResizableElement.Resizer resizeDirection)
{
var horizontal = (resizeDirection & (ResizableElement.Resizer.Left | ResizableElement.Resizer.Right)) != 0;
var vertical = (resizeDirection & (ResizableElement.Resizer.Top | ResizableElement.Resizer.Bottom)) != 0;
if (horizontal && vertical)
{
return Mathf.Min(rect.width, rect.height);
}
return horizontal ? rect.width : rect.height;
}
Rect ApplySquareAspect(Rect element, ResizableElement.Resizer resizeDirection, Rect? keepInParent = null)
{
var newLayout = element;
var size = GetSquareSize(element, resizeDirection);
size = Mathf.Clamp(size, m_MinSize.x, m_MaxSize.x);
size = Mathf.Clamp(size, m_MinSize.y, m_MaxSize.y);
ResizeNewLayoutAndFixPosition(resizeDirection, size);
if (keepInParent is { } parentRect)
{
newLayout = CropToParent(newLayout, parentRect);
size = Mathf.Min(newLayout.width, newLayout.height);
ResizeNewLayoutAndFixPosition(resizeDirection, size);
}
return newLayout;
void ResizeNewLayoutAndFixPosition(ResizableElement.Resizer grabbedSide, float newSize)
{
newLayout.width = newSize;
newLayout.height = newSize;
var deltaWidth = element.width - newSize;
var deltaHeight = element.height - newSize;
// Anchoring rules were written with the main preview in mind, which is in the bottom-right corner
// by default.
switch (grabbedSide)
{
// Anchor to bottom-right.
case ResizableElement.Resizer.Top:
case ResizableElement.Resizer.Top | ResizableElement.Resizer.Left:
case ResizableElement.Resizer.Left:
{
newLayout.x = element.x + deltaWidth;
newLayout.y = element.y + deltaHeight;
break;
}
// Anchor to bottom-left.
case ResizableElement.Resizer.Top | ResizableElement.Resizer.Right:
{
newLayout.y = element.y + deltaHeight;
break;
}
// Anchor to top-left.
case ResizableElement.Resizer.Right:
case ResizableElement.Resizer.Bottom | ResizableElement.Resizer.Right:
case ResizableElement.Resizer.Bottom:
{
// Element is positioned by its top-left, so no adjustment is needed.
break;
}
// Anchor to top-right.
case ResizableElement.Resizer.Bottom | ResizableElement.Resizer.Left:
{
newLayout.x = element.x + deltaWidth;
break;
}
case ResizableElement.Resizer.None:
default:
break;
}
}
}
void OnMouseMove(MouseMoveEvent e)
{
if (!isEnabled)
return;
var resizedTarget = resizedElement.parent;
var resizedBase = resizedTarget.parent;
// Top left position of the parent visual element
var parentRootPosition = resizedBase.worldBound;
// Top left of the target visual element for resizing
var targetRootPosition = resizedTarget.worldBound;
var sgResizable = resizedTarget as ISGResizable;
var canResizePastParentBounds = sgResizable?.CanResizePastParentBounds() ?? false;
var keepSquareAspect = sgResizable?.KeepSquareAspect() ?? false;
var mousePos = resizedBase.WorldToLocal(e.mousePosition);
if (!m_DragStarted)
{
sgResizable?.OnStartResize();
m_DragStarted = true;
}
var newLayout = new Rect(m_StartPosition, m_StartSize);
if ((direction & ResizableElement.Resizer.Right) != 0)
{
newLayout.width = m_StartSize.x + mousePos.x - m_StartMouse.x;
var parentRightBoundary = parentRootPosition.x + resizedBase.layout.width;
// Also ensure resizing does not happen past edge of parent views boundaries if the target does not allow it
if (!canResizePastParentBounds)
{
if ((targetRootPosition.x + newLayout.width) > parentRightBoundary)
{
var targetToRightBoundaryDelta = parentRightBoundary - targetRootPosition.x;
newLayout.width = targetToRightBoundaryDelta;
}
var newLayoutLeft = targetRootPosition.x - parentRootPosition.x;
// When resizing to right, make sure to calculate and set the target elements Style.left before resizing to ensure correct resizing behavior
// If Style.left is NaNpx it results in scaling towards the left
// This is due to how the WindowDockingLayout code affects GraphSubWindows
newLayout.x = newLayoutLeft;
}
newLayout.width = Mathf.Clamp(newLayout.width, m_MinSize.x, m_MaxSize.x);
}
else if ((direction & ResizableElement.Resizer.Left) != 0)
{
float delta = mousePos.x - m_StartMouse.x;
if (m_StartSize.x - delta < m_MinSize.x)
{
delta = -m_MinSize.x + m_StartSize.x;
}
else if (m_StartSize.x - delta > m_MaxSize.x)
{
delta = -m_MaxSize.x + m_StartSize.x;
}
newLayout.width = -delta + m_StartSize.x;
var targetToLeftBoundaryDelta = delta + m_StartPosition.x;
if (!canResizePastParentBounds)
{
// This ensures that the left side of the resizing target never can get pushed past the parent boundary even if mouse is moving really fast
targetToLeftBoundaryDelta = Mathf.Clamp(targetToLeftBoundaryDelta, 2.5f, targetToLeftBoundaryDelta);
// Clamps width to max out at left edge of parent window
if (Mathf.Approximately(targetToLeftBoundaryDelta, 2.5f))
newLayout.width = (m_StartPosition.x + m_StartSize.x);
newLayout.width = Mathf.Clamp(newLayout.width, m_MinSize.x, m_MaxSize.x);
}
newLayout.x = targetToLeftBoundaryDelta;
}
if ((direction & ResizableElement.Resizer.Bottom) != 0)
{
var delta = mousePos.y - m_StartMouse.y;
newLayout.height = m_StartSize.y + delta;
var parentBottomBoundary = parentRootPosition.y + resizedBase.layout.height;
if (!canResizePastParentBounds)
{
if ((targetRootPosition.y + newLayout.height) > parentBottomBoundary)
{
var targetToBottomBoundaryDelta = parentBottomBoundary - targetRootPosition.y;
newLayout.height = targetToBottomBoundaryDelta;
}
var targetToTopBoundaryDelta = targetRootPosition.y - parentRootPosition.y;
// When resizing to bottom, make sure to calculate and set the target elements Style.top before resizing to ensure correct resizing behavior
// If Style.top is NaNpx it results in scaling towards the bottom
// This is due to how the WindowDockingLayout code affects GraphSubWindows
newLayout.y = targetToTopBoundaryDelta;
newLayout.height = Mathf.Clamp(newLayout.height, m_MinSize.y, m_MaxSize.y);
}
}
else if ((direction & ResizableElement.Resizer.Top) != 0)
{
float delta = mousePos.y - m_StartMouse.y;
if (m_StartSize.y - delta < m_MinSize.y)
{
delta = -m_MinSize.y + m_StartSize.y;
}
else if (m_StartSize.y - delta > m_MaxSize.y)
{
delta = -m_MaxSize.y + m_StartSize.y;
}
newLayout.height = -delta + m_StartSize.y;
var targetToTopBoundaryDelta = m_StartPosition.y + delta;
if (!canResizePastParentBounds)
{
// This ensures that the top of the resizing target never can get pushed past the parent boundary even if mouse is moving really fast
targetToTopBoundaryDelta = Mathf.Clamp(targetToTopBoundaryDelta, 2.5f, targetToTopBoundaryDelta);
// Clamps height to max out at top edge of parent window
if (Mathf.Approximately(targetToTopBoundaryDelta, 2.5f))
newLayout.height = (m_StartPosition.y + m_StartSize.y);
newLayout.height = Mathf.Clamp(newLayout.height, m_MinSize.y, m_MaxSize.y);
}
newLayout.y = targetToTopBoundaryDelta;
}
if (keepSquareAspect)
{
newLayout = ApplySquareAspect(
newLayout,
direction,
canResizePastParentBounds ? null : new Rect(0, 0, parentRootPosition.width, parentRootPosition.height)
);
}
ApplyLayoutToTargetAsStyle(newLayout);
e.StopPropagation();
}
void OnMouseUp(MouseUpEvent e)
{
if (!isEnabled)
return;
if (e.button == 0)
{
VisualElement resizedTarget = resizedElement.parent;
if (resizedTarget.style.width != m_StartSize.x || resizedTarget.style.height != m_StartSize.y)
{
if (resizedTarget is ISGResizable resizable)
resizable.OnResized();
}
target.UnregisterCallback<MouseMoveEvent>(OnMouseMove);
target.ReleaseMouse();
e.StopPropagation();
}
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 115a3d6c38944c34a134edc915a90ebe
timeCreated: 1605227560
@@ -0,0 +1,41 @@
using System;
using UnityEngine;
using UnityEngine.UIElements;
namespace UnityEditor.ShaderGraph.Drawing
{
internal sealed class MasterPreviewManipulator : ContextualMenuManipulator
{
internal MasterPreviewManipulator(Action<ContextualMenuPopulateEvent> menuBuilder) : base(menuBuilder)
{
}
protected override void RegisterCallbacksOnTarget()
{
base.RegisterCallbacksOnTarget();
if (IsOSXContextualMenuPlatform())
{
target.RegisterCallback<PointerDownEvent>(MasterPointerDownEventOSX);
}
}
protected override void UnregisterCallbacksFromTarget()
{
base.UnregisterCallbacksFromTarget();
if (IsOSXContextualMenuPlatform())
{
target.UnregisterCallback<PointerDownEvent>(MasterPointerDownEventOSX);
}
}
void MasterPointerDownEventOSX(IPointerEvent evt)
{
if (CanStartManipulation(evt))
{
(evt as EventBase)?.StopImmediatePropagation();
}
}
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: f3738117378a2c645ae46e837d3cffe8
@@ -0,0 +1,32 @@
using UnityEngine;
using System;
using UnityEngine.UIElements;
namespace UnityEditor.ShaderGraph.Drawing
{
class Scrollable : MouseManipulator
{
Action<float> m_Handler;
public Scrollable(Action<float> handler)
{
m_Handler = handler;
}
protected override void RegisterCallbacksOnTarget()
{
target.RegisterCallback<WheelEvent>(HandleMouseWheelEvent);
}
protected override void UnregisterCallbacksFromTarget()
{
target.UnregisterCallback<WheelEvent>(HandleMouseWheelEvent);
}
void HandleMouseWheelEvent(WheelEvent evt)
{
m_Handler(evt.delta.y);
evt.StopPropagation();
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9ea23c5e75fdbea49824261efe5fc133
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,182 @@
using System;
using UnityEngine;
using UnityEditor.Experimental.GraphView;
using UnityEngine.UIElements;
using UnityEngine.UIElements.StyleSheets;
namespace UnityEditor.ShaderGraph.Drawing
{
class WindowDraggable : MouseManipulator
{
bool m_Active;
WindowDockingLayout m_WindowDockingLayout;
Vector2 m_LocalMosueOffset;
VisualElement m_Handle;
GraphView m_GraphView;
public Action OnDragFinished;
public WindowDraggable(VisualElement handle = null, VisualElement container = null)
{
m_Handle = handle;
m_Active = false;
m_WindowDockingLayout = new WindowDockingLayout();
if (container != null)
container.RegisterCallback<GeometryChangedEvent>(OnParentGeometryChanged);
}
protected override void RegisterCallbacksOnTarget()
{
if (m_Handle == null)
m_Handle = target;
m_Handle.RegisterCallback(new EventCallback<MouseDownEvent>(OnMouseDown), TrickleDownEnum.NoTrickleDown);
m_Handle.RegisterCallback(new EventCallback<MouseMoveEvent>(OnMouseMove), TrickleDownEnum.NoTrickleDown);
m_Handle.RegisterCallback(new EventCallback<MouseUpEvent>(OnMouseUp), TrickleDownEnum.NoTrickleDown);
target.RegisterCallback<GeometryChangedEvent>(OnGeometryChanged);
}
protected override void UnregisterCallbacksFromTarget()
{
m_Handle.UnregisterCallback(new EventCallback<MouseDownEvent>(OnMouseDown), TrickleDownEnum.NoTrickleDown);
m_Handle.UnregisterCallback(new EventCallback<MouseMoveEvent>(OnMouseMove), TrickleDownEnum.NoTrickleDown);
m_Handle.UnregisterCallback(new EventCallback<MouseUpEvent>(OnMouseUp), TrickleDownEnum.NoTrickleDown);
target.UnregisterCallback<GeometryChangedEvent>(OnGeometryChanged);
if (m_GraphView != null)
m_GraphView.UnregisterCallback<GeometryChangedEvent>(OnGeometryChanged);
}
void OnMouseDown(MouseDownEvent evt)
{
m_Active = true;
VisualElement parent = target.parent;
while (parent != null && !(parent is GraphView))
parent = parent.parent;
m_GraphView = parent as GraphView;
if (m_GraphView != null)
m_GraphView.RegisterCallback<GeometryChangedEvent>(OnGeometryChanged);
// m_LocalMouseOffset is offset from the target element's (0, 0) to the
// to the mouse position.
m_LocalMosueOffset = m_Handle.WorldToLocal(evt.mousePosition);
m_Handle.CaptureMouse();
evt.StopImmediatePropagation();
}
void OnMouseMove(MouseMoveEvent evt)
{
if (m_Active)
{
// The mouse position of is corrected according to the offset within the target
// element (m_LocalWorldOffset) to set the position relative to the mouse position
// when the dragging started.
Vector2 position = target.parent.WorldToLocal(evt.mousePosition) - m_LocalMosueOffset;
// Make sure that the object remains in the parent window
position.x = Mathf.Clamp(position.x, 0f, target.parent.layout.width - target.layout.width);
position.y = Mathf.Clamp(position.y, 0f, target.parent.layout.height - target.layout.height);
// While moving, use only the left and top position properties,
// while keeping the others NaN to not affect layout.
target.style.left = position.x;
target.style.top = position.y;
target.style.right = float.NaN;
target.style.bottom = float.NaN;
}
}
void OnMouseUp(MouseUpEvent evt)
{
bool emitDragFinishedEvent = m_Active;
m_Active = false;
if (m_Handle.HasMouseCapture())
{
m_Handle.ReleaseMouse();
}
evt.StopImmediatePropagation();
// Recalculate which corner to dock to
m_WindowDockingLayout.CalculateDockingCornerAndOffset(target.layout, target.parent.layout);
m_WindowDockingLayout.ClampToParentWindow(target.parent.layout);
// Use the docking results to figure which of left/right and top/bottom needs to be set.
m_WindowDockingLayout.ApplyPosition(target);
// Signal that the dragging has finished.
if (emitDragFinishedEvent && OnDragFinished != null)
OnDragFinished();
}
void OnGeometryChanged(GeometryChangedEvent geometryChangedEvent)
{
// Make the target clamp to the border of the window if the
// parent window becomes too small to contain it.
if (target.parent.layout.width < target.layout.width)
{
if (m_WindowDockingLayout.dockingLeft)
{
target.style.left = 0f;
target.style.right = float.NaN;
}
else
{
target.style.left = float.NaN;
target.style.right = 0f;
}
}
if (target.parent.layout.height < target.layout.height)
{
if (m_WindowDockingLayout.dockingTop)
{
target.style.top = 0f;
target.style.bottom = float.NaN;
}
else
{
target.style.top = float.NaN;
target.style.bottom = 0f;
}
}
}
void OnParentGeometryChanged(GeometryChangedEvent geometryChangedEvent)
{
// Check if the parent window can no longer contain the target window.
// If the window is out of bounds, make one edge clamp to the border of the
// parent window.
if (target.layout.xMin < 0f)
{
target.style.left = 0f;
target.style.right = float.NaN;
}
if (target.layout.xMax > geometryChangedEvent.newRect.width)
{
target.style.left = float.NaN;
target.style.right = 0f;
}
if (target.layout.yMax > geometryChangedEvent.newRect.height)
{
target.style.top = float.NaN;
target.style.bottom = 0f;
}
if (target.layout.yMin < 0f)
{
target.style.top = 0f;
target.style.bottom = float.NaN;
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 604681490aa779348a355368c95cfcf5
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: