浏览代码

Remove 2018.1 compatibility code from Shader Graph

/main
Peter Bay Bastian 7 年前
当前提交
f9b16024
共有 20 个文件被更改,包括 5 次插入802 次删除
  1. 2
      com.unity.shadergraph/Editor/Drawing/Blackboard/BlackboardFieldPropertyView.cs
  2. 11
      com.unity.shadergraph/Editor/Drawing/Controls/IntegerControl.cs
  3. 2
      com.unity.shadergraph/Editor/Drawing/Controls/ToggleControl.cs
  4. 3
      com.unity.shadergraph/Editor/Drawing/Inspector/MasterPreviewView.cs
  5. 3
      com.unity.shadergraph/Editor/Drawing/Manipulators/ResizeSideHandle.cs
  6. 4
      com.unity.shadergraph/Editor/Drawing/Manipulators/WindowDraggable.cs
  7. 6
      com.unity.shadergraph/Editor/Drawing/MaterialGraphEditWindow.cs
  8. 3
      com.unity.shadergraph/Editor/Drawing/Views/GraphEditorView.cs
  9. 2
      com.unity.shadergraph/Editor/Drawing/Views/MaterialGraphView.cs
  10. 3
      com.unity.shadergraph/Editor/Drawing/Views/MaterialNodeView.cs
  11. 10
      com.unity.shadergraph/Editor/Drawing/Views/ShaderPort.cs
  12. 32
      com.unity.shadergraph/Editor/Util/CompatibilityExtensions.cs
  13. 11
      com.unity.shadergraph/Editor/Drawing/Blackboard/Blackboard.cs.meta
  14. 11
      com.unity.shadergraph/Editor/Drawing/Blackboard/BlackboardField.cs.meta
  15. 11
      com.unity.shadergraph/Editor/Drawing/Blackboard/BlackboardRow.cs.meta
  16. 11
      com.unity.shadergraph/Editor/Drawing/Blackboard/BlackboardSection.cs.meta
  17. 134
      com.unity.shadergraph/Editor/Drawing/Blackboard/Blackboard.cs
  18. 67
      com.unity.shadergraph/Editor/Drawing/Blackboard/BlackboardRow.cs
  19. 181
      com.unity.shadergraph/Editor/Drawing/Blackboard/BlackboardField.cs
  20. 300
      com.unity.shadergraph/Editor/Drawing/Blackboard/BlackboardSection.cs

2
com.unity.shadergraph/Editor/Drawing/Blackboard/BlackboardFieldPropertyView.cs


DirtyNodes();
};
var field = new Toggle(onBooleanChanged);
field.SetValue(booleanProperty.value);
field.value = booleanProperty.value;
AddRow("Default", field);
}
// AddRow("Type", new TextField());

11
com.unity.shadergraph/Editor/Drawing/Controls/IntegerControl.cs


Add(intField);
}
#if UNITY_2018_1
void OnChange(ChangeEvent<long> evt)
#else
#endif
var newValue =
#if UNITY_2018_1
(int)
#endif
evt.newValue;
m_PropertyInfo.SetValue(m_Node, newValue, null);
m_PropertyInfo.SetValue(m_Node, evt.newValue, null);
this.MarkDirtyRepaint();
}
}

2
com.unity.shadergraph/Editor/Drawing/Controls/ToggleControl.cs


Action changedToggle = () => { OnChangeToggle(); };
m_Toggle = new UnityEngine.Experimental.UIElements.Toggle(changedToggle);
m_Toggle.SetEnabled(value.isEnabled);
m_Toggle.SetValue(value.isOn);
m_Toggle.value = value.isOn;
panel.Add(m_Toggle);
Add(panel);
}

3
com.unity.shadergraph/Editor/Drawing/Inspector/MasterPreviewView.cs


using UnityEditor.Graphing.Util;
using UnityEngine.Experimental.UIElements.StyleSheets;
using Object = UnityEngine.Object;
#if UNITY_2018_1
using GeometryChangedEvent = UnityEngine.Experimental.UIElements.PostLayoutEvent;
#endif
namespace UnityEditor.ShaderGraph.Drawing.Inspector
{

3
com.unity.shadergraph/Editor/Drawing/Manipulators/ResizeSideHandle.cs


using UnityEngine;
using UnityEngine.Experimental.UIElements;
using UnityEngine.Experimental.UIElements.StyleSheets;
#if UNITY_2018_1
using GeometryChangedEvent = UnityEngine.Experimental.UIElements.PostLayoutEvent;
#endif
namespace UnityEditor.ShaderGraph.Drawing
{

4
com.unity.shadergraph/Editor/Drawing/Manipulators/WindowDraggable.cs


using UnityEngine.Experimental.UIElements;
using UnityEngine.Experimental.UIElements.StyleSheets;
#if UNITY_2018_1
using GeometryChangedEvent = UnityEngine.Experimental.UIElements.PostLayoutEvent;
#endif
namespace UnityEditor.ShaderGraph.Drawing
{
public class WindowDraggable : MouseManipulator

6
com.unity.shadergraph/Editor/Drawing/MaterialGraphEditWindow.cs


using Debug = UnityEngine.Debug;
using Object = UnityEngine.Object;
using Edge = UnityEditor.Experimental.UIElements.GraphView.Edge;
#if UNITY_2018_1
using GeometryChangedEvent = UnityEngine.Experimental.UIElements.PostLayoutEvent;
#else
using GeometryChangedEvent = UnityEngine.Experimental.UIElements.GeometryChangedEvent;
#endif
using UnityEngine.Experimental.UIElements;
namespace UnityEditor.ShaderGraph.Drawing
{

3
com.unity.shadergraph/Editor/Drawing/Views/GraphEditorView.cs


using UnityEngine.Rendering;
using Edge = UnityEditor.Experimental.UIElements.GraphView.Edge;
using Object = UnityEngine.Object;
#if UNITY_2018_1
using GeometryChangedEvent = UnityEngine.Experimental.UIElements.PostLayoutEvent;
#endif
namespace UnityEditor.ShaderGraph.Drawing
{

2
com.unity.shadergraph/Editor/Drawing/Views/MaterialGraphView.cs


using Edge = UnityEditor.Experimental.UIElements.GraphView.Edge;
using Node = UnityEditor.Experimental.UIElements.GraphView.Node;
using Object = UnityEngine.Object;
#if !UNITY_2018_1
#endif
namespace UnityEditor.ShaderGraph.Drawing
{

3
com.unity.shadergraph/Editor/Drawing/Views/MaterialNodeView.cs


using UnityEngine.Experimental.UIElements.StyleEnums;
using UnityEngine.Experimental.UIElements.StyleSheets;
using Node = UnityEditor.Experimental.UIElements.GraphView.Node;
#if UNITY_2018_1
using GeometryChangedEvent = UnityEngine.Experimental.UIElements.PostLayoutEvent;
#endif
namespace UnityEditor.ShaderGraph.Drawing
{

10
com.unity.shadergraph/Editor/Drawing/Views/ShaderPort.cs


{
sealed class ShaderPort : Port
{
#if UNITY_2018_1
ShaderPort(Orientation portOrientation, Direction portDirection, Type type)
: base(portOrientation, portDirection, type)
#else
#endif
{
AddStyleSheetPath("Styles/ShaderPort");
}

public static Port Create(MaterialSlot slot, IEdgeConnectorListener connectorListener)
{
var port = new ShaderPort(Orientation.Horizontal, slot.isInputSlot ? Direction.Input : Direction.Output,
#if !UNITY_2018_1
slot.isInputSlot ? Capacity.Single : Capacity.Multi,
#endif
null)
slot.isInputSlot ? Capacity.Single : Capacity.Multi, null)
{
m_EdgeConnector = new EdgeConnector<Edge>(connectorListener),
};

32
com.unity.shadergraph/Editor/Util/CompatibilityExtensions.cs


using UnityEngine;
using UnityEngine.Experimental.UIElements;
#if UNITY_2018_1
using UnityEditor.Experimental.UIElements.GraphView;
#endif
#if UNITY_2018_1
public static void OpenTextEditor(this BlackboardField field)
{
field.RenameGo();
}
#endif
public static void AppendAction(this ContextualMenu contextualMenu, string actionName, Action action, Func<ContextualMenu.MenuAction.StatusFlags> actionStatusCallback)
{
Debug.Assert(action != null);

contextualMenu.AppendAction(actionName, e => action(), e => statusFlags);
}
public static bool GetValue(this Toggle toggle)
{
#if UNITY_2018_1
return toggle.on;
#else
return toggle.value;
#endif
}
public static void SetValue(this Toggle toggle, bool value)
{
#if UNITY_2018_1
toggle.on = value;
#else
toggle.value = value;
#endif
}
#endif
#if !UNITY_2018_3_OR_NEWER

{
element.ReleaseMouseCapture();
}
#endif
}

11
com.unity.shadergraph/Editor/Drawing/Blackboard/Blackboard.cs.meta


fileFormatVersion: 2
guid: 4312a96008ca8754593210fc93a4575d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

11
com.unity.shadergraph/Editor/Drawing/Blackboard/BlackboardField.cs.meta


fileFormatVersion: 2
guid: 353b195797b2ff24092e20c643077f2f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

11
com.unity.shadergraph/Editor/Drawing/Blackboard/BlackboardRow.cs.meta


fileFormatVersion: 2
guid: 7532a1da2f917d54c800efef4149ae69
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

11
com.unity.shadergraph/Editor/Drawing/Blackboard/BlackboardSection.cs.meta


fileFormatVersion: 2
guid: f7dcab10210c08a488ac46fee4811efd
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

134
com.unity.shadergraph/Editor/Drawing/Blackboard/Blackboard.cs


#if UNITY_2018_1
using System;
using UnityEditor.Experimental.UIElements.GraphView;
using UnityEngine;
using UnityEngine.Experimental.UIElements;
using UnityEngine.Experimental.UIElements.StyleEnums;
namespace UnityEditor.ShaderGraph.Drawing
{
class Blackboard : GraphElement
{
private const int k_DefaultWidth = 200;
private const float k_DefaultHeight = 400;
private VisualElement m_MainContainer;
private VisualElement m_Root;
private Label m_TitleLabel;
private Label m_SubTitleLabel;
private ScrollView m_ScrollView;
private VisualElement m_ContentContainer;
private VisualElement m_HeaderItem;
private Button m_AddButton;
private bool m_Scrollable = true;
public Action<Blackboard> addItemRequested { get; set; }
public Action<Blackboard, int, VisualElement> moveItemRequested { get; set; }
public Action<Blackboard, VisualElement, string> editTextRequested { get; set; }
public string title
{
get { return m_TitleLabel.text; }
set { m_TitleLabel.text = value; }
}
public string subTitle
{
get { return m_SubTitleLabel.text; }
set { m_SubTitleLabel.text = value; }
}
public override VisualElement contentContainer { get { return m_ContentContainer; } }
public bool scrollable
{
get
{
return m_Scrollable;
}
set
{
if (m_Scrollable == value)
return;
m_Scrollable = value;
if (m_Scrollable)
{
if (m_ScrollView == null)
{
m_ScrollView = new ScrollView();
// m_ScrollView.stretchContentWidth = true;
}
// Remove the sections container from the content item and add it to the scrollview
m_ContentContainer.RemoveFromHierarchy();
m_Root.Add(m_ScrollView);
m_ScrollView.Add(m_ContentContainer);
style.positionType = PositionType.Manual; // As both the width and height can be changed by the user using a resizer
// If the current the current geometry is invalid then set a default size
if (layout.width == 0 || layout.height == 0)
{
layout = new Rect(layout.x, layout.y, layout.width == 0 ? k_DefaultWidth : layout.width, layout.height == 0 ? k_DefaultHeight : layout.height);
}
AddToClassList("scrollable");
}
else
{
if (m_ScrollView != null)
{
// Remove the sections container from the scrollview and add it to the content item
style.positionType = PositionType.Absolute; // As the height is automatically computed from the content but the width can be changed by the user using a resizer
m_ScrollView.RemoveFromHierarchy();
m_ContentContainer.RemoveFromHierarchy();
m_Root.Add(m_ContentContainer);
}
RemoveFromClassList("scrollable");
}
}
}
public Blackboard()
{
AddStyleSheetPath("Styles/Blackboard");
var tpl = Resources.Load<VisualTreeAsset>("UXML/GraphView/Blackboard");
m_MainContainer = tpl.CloneTree(null);
m_MainContainer.AddToClassList("mainContainer");
m_Root = m_MainContainer.Q("content");
m_HeaderItem = m_MainContainer.Q("header");
m_HeaderItem.AddToClassList("blackboardHeader");
m_AddButton = m_MainContainer.Q(name: "addButton") as Button;
m_AddButton.clickable.clicked += () => {
if (addItemRequested != null)
{
addItemRequested(this);
}
};
m_TitleLabel = m_MainContainer.Q<Label>(name: "titleLabel");
m_SubTitleLabel = m_MainContainer.Q<Label>(name: "subTitleLabel");
m_ContentContainer = m_MainContainer.Q<VisualElement>(name: "contentContainer");
shadow.Add(m_MainContainer);
capabilities |= Capabilities.Movable | Capabilities.Resizable;
clippingOptions = ClippingOptions.ClipAndCacheContents;
ClearClassList();
AddToClassList("sgblackboard");
this.AddManipulator(new Dragger { clampToParentEdges = true });
scrollable = false;
shadow.Add(new Resizer());
}
}
}
#endif

67
com.unity.shadergraph/Editor/Drawing/Blackboard/BlackboardRow.cs


#if UNITY_2018_1
using System;
using UnityEditor.Experimental.UIElements.GraphView;
using UnityEngine;
using UnityEngine.Experimental.UIElements;
namespace UnityEditor.ShaderGraph.Drawing
{
class BlackboardRow : GraphElement
{
private VisualElement m_Root;
private Button m_ExpandButton;
private VisualElement m_ItemContainer;
private VisualElement m_PropertyViewContainer;
private bool m_Expanded = true;
public bool expanded
{
get { return m_Expanded; }
set
{
if (m_Expanded == value)
{
return;
}
m_Expanded = value;
if (m_Expanded)
{
m_Root.Add(m_PropertyViewContainer);
AddToClassList("expanded");
}
else
{
m_Root.Remove(m_PropertyViewContainer);
RemoveFromClassList("expanded");
}
}
}
public BlackboardRow(VisualElement item, VisualElement propertyView)
{
var tpl = Resources.Load<VisualTreeAsset>("UXML/GraphView/BlackboardRow");
VisualElement mainContainer = tpl.CloneTree(null);
mainContainer.AddToClassList("mainContainer");
m_Root = mainContainer.Q<VisualElement>("root");
m_ItemContainer = mainContainer.Q<VisualElement>("itemContainer");
m_PropertyViewContainer = mainContainer.Q<VisualElement>("propertyViewContainer");
m_ExpandButton = mainContainer.Q<Button>("expandButton");
m_ExpandButton.clickable.clicked += () => expanded = !expanded;
Add(mainContainer);
ClearClassList();
AddToClassList("sgblackboardRow");
m_ItemContainer.Add(item);
m_PropertyViewContainer.Add(propertyView);
expanded = false;
}
}
}
#endif

181
com.unity.shadergraph/Editor/Drawing/Blackboard/BlackboardField.cs


#if UNITY_2018_1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEditor.Experimental.UIElements.GraphView;
using UnityEditor.Graphing.Util;
using UnityEngine;
using UnityEngine.Experimental.UIElements;
namespace UnityEditor.ShaderGraph.Drawing
{
class BlackboardField : GraphElement
{
private VisualElement m_ContentItem;
private Image m_Icon;
private Label m_TextLabel;
private TextField m_TextField;
private Label m_TypeLabel;
private bool m_EditTitleCancelled = false;
SelectionDropper m_SelectionDropper;
public string text
{
get { return m_TextLabel.text; }
set { m_TextLabel.text = value; }
}
public string typeText
{
get { return m_TypeLabel.text; }
set { m_TypeLabel.text = value; }
}
public Texture icon
{
get { return m_Icon.image; }
set
{
m_Icon.image = value;
if (value == null)
{
AddToClassList("noIcon");
m_Icon.visible = false;
}
else
{
RemoveFromClassList("noIcon");
m_Icon.visible = true;
}
}
}
public BlackboardField()
: this(null, "", "") {}
static Type s_ContextualMenuManipulator = AppDomain.CurrentDomain.GetAssemblies().SelectMany(x => x.GetTypesOrNothing()).FirstOrDefault(t => t.FullName == "UnityEngine.Experimental.UIElements.ContextualMenuManipulator");
public BlackboardField(Texture icon, string text, string typeText)
{
var tpl = Resources.Load<VisualTreeAsset>("UXML/GraphView/BlackboardField");
VisualElement mainContainer = tpl.CloneTree(null);
mainContainer.AddToClassList("mainContainer");
mainContainer.pickingMode = PickingMode.Ignore;
m_ContentItem = mainContainer.Q("contentItem");
m_TextLabel = mainContainer.Q<Label>("textLabel");
m_Icon = mainContainer.Q<Image>("iconItem");
m_TypeLabel = mainContainer.Q<Label>("typeLabel");
m_TextField = mainContainer.Q<TextField>("textField");
m_TextField.visible = false;
m_TextField.RegisterCallback<FocusOutEvent>(e => { OnEditTextFinished(); });
m_TextField.RegisterCallback<KeyDownEvent>(OnTextFieldKeyPressed);
Add(mainContainer);
RegisterCallback<MouseDownEvent>(OnMouseDownEvent);
capabilities |= Capabilities.Selectable | Capabilities.Droppable | Capabilities.Deletable;
ClearClassList();
AddToClassList("sgblackboardField");
this.text = text;
this.icon = icon;
this.typeText = typeText;
m_SelectionDropper = new SelectionDropper(Handler);
// Workaround bug causing SelectionDropper to not work (m_Active should be initialized to false rather than true)
var activeFieldInfo = typeof(SelectionDropper).GetFields(BindingFlags.NonPublic | BindingFlags.Instance).FirstOrDefault(f => f.Name == "m_Active");
Debug.Assert(activeFieldInfo != null, "activeFieldInfo != null");
activeFieldInfo.SetValue(m_SelectionDropper, false);
this.AddManipulator(m_SelectionDropper);
this.AddManipulator((IManipulator)Activator.CreateInstance(s_ContextualMenuManipulator, (Action<ContextualMenuPopulateEvent>)BuildContextualMenu));
}
void BuildContextualMenu(ContextualMenuPopulateEvent evt)
{
evt.menu.AppendAction("Rename", e => RenameGo(), ContextualMenu.MenuAction.AlwaysEnabled);
}
void Handler(IMGUIEvent evt, List<ISelectable> selection, IDropTarget dropTarget)
{
if (dropTarget == null || !dropTarget.CanAcceptDrop(selection))
return;
if (evt.imguiEvent.type == EventType.DragUpdated)
dropTarget.DragUpdated(evt, selection, dropTarget);
else if (evt.imguiEvent.type == EventType.DragPerform)
dropTarget.DragPerform(evt, selection, dropTarget);
else if (evt.imguiEvent.type == EventType.DragExited)
dropTarget.DragExited();
// if (propagation == EventPropagation.Stop)
// evt.StopPropagation();
}
private void OnTextFieldKeyPressed(KeyDownEvent e)
{
switch (e.keyCode)
{
case KeyCode.Escape:
m_EditTitleCancelled = true;
m_TextField.Blur();
break;
case KeyCode.Return:
case KeyCode.KeypadEnter:
m_TextField.Blur();
break;
default:
break;
}
}
private void OnEditTextFinished()
{
m_ContentItem.visible = true;
m_TextField.visible = false;
if (!m_EditTitleCancelled && (text != m_TextField.text))
{
Blackboard blackboard = GetFirstAncestorOfType<Blackboard>();
if (blackboard.editTextRequested != null)
{
blackboard.editTextRequested(blackboard, this, m_TextField.text);
}
else
{
text = m_TextField.text;
}
}
m_EditTitleCancelled = false;
}
private void OnMouseDownEvent(MouseDownEvent e)
{
if ((e.clickCount == 2) && e.button == (int)MouseButton.LeftMouse)
{
RenameGo();
e.PreventDefault();
}
}
internal void RenameGo()
{
m_TextField.text = text;
m_TextField.visible = true;
m_ContentItem.visible = false;
m_TextField.Focus();
m_TextField.SelectAll();
}
}
}
#endif

300
com.unity.shadergraph/Editor/Drawing/Blackboard/BlackboardSection.cs


#if UNITY_2018_1
using System;
using System.Collections.Generic;
using UnityEditor.Experimental.UIElements.GraphView;
using UnityEngine;
using UnityEngine.Experimental.UIElements;
using UnityEngine.Experimental.UIElements.StyleEnums;
namespace UnityEditor.ShaderGraph.Drawing
{
class BlackboardSection : GraphElement, IDropTarget
{
private VisualElement m_DragIndicator;
private VisualElement m_MainContainer;
private VisualElement m_Header;
private Label m_TitleLabel;
private VisualElement m_RowsContainer;
private int m_InsertIndex;
int InsertionIndex(Vector2 pos)
{
int index = -1;
VisualElement owner = contentContainer != null ? contentContainer : this;
Vector2 localPos = this.ChangeCoordinatesTo(owner, pos);
if (owner.ContainsPoint(localPos))
{
index = 0;
foreach (VisualElement child in Children())
{
Rect rect = child.layout;
if (localPos.y > (rect.y + rect.height / 2))
{
++index;
}
else
{
break;
}
}
}
return index;
}
VisualElement FindSectionDirectChild(VisualElement element)
{
VisualElement directChild = element;
while ((directChild != null) && (directChild != this))
{
if (directChild.parent == this)
{
return directChild;
}
directChild = directChild.parent;
}
return null;
}
public BlackboardSection()
{
var tpl = Resources.Load<VisualTreeAsset>("UXML/GraphView/BlackboardSection");
m_MainContainer = tpl.CloneTree(null);
m_MainContainer.AddToClassList("mainContainer");
m_Header = m_MainContainer.Q<VisualElement>("sectionHeader");
m_TitleLabel = m_MainContainer.Q<Label>("sectionTitleLabel");
m_RowsContainer = m_MainContainer.Q<VisualElement>("rowsContainer");
shadow.Add(m_MainContainer);
m_DragIndicator = new VisualElement();
m_DragIndicator.name = "dragIndicator";
m_DragIndicator.style.positionType = PositionType.Absolute;
shadow.Add(m_DragIndicator);
ClearClassList();
AddToClassList("sgblackboardSection");
m_InsertIndex = -1;
}
public override VisualElement contentContainer { get { return m_RowsContainer; } }
public string title
{
get { return m_TitleLabel.text; }
set { m_TitleLabel.text = value; }
}
public bool headerVisible
{
get { return m_Header.parent != null; }
set
{
if (value == (m_Header.parent != null))
return;
if (value)
{
m_MainContainer.Add(m_Header);
}
else
{
m_MainContainer.Remove(m_Header);
}
}
}
private void SetDragIndicatorVisible(bool visible)
{
if (visible && (m_DragIndicator.parent == null))
{
shadow.Add(m_DragIndicator);
m_DragIndicator.visible = true;
}
else if ((visible == false) && (m_DragIndicator.parent != null))
{
shadow.Remove(m_DragIndicator);
}
}
public bool CanAcceptDrop(List<ISelectable> selection)
{
// Look for at least one selected element in this section to accept drop
foreach (ISelectable selected in selection)
{
VisualElement selectedElement = selected as VisualElement;
if (selected != null && Contains(selectedElement))
{
return true;
}
}
return false;
}
// public bool DragUpdated(DragUpdatedEvent evt, IEnumerable<ISelectable> selection, IDropTarget dropTarget)
public EventPropagation DragUpdated(IMGUIEvent evt, IEnumerable<ISelectable> selection, IDropTarget dropTarget)
{
VisualElement sourceItem = null;
foreach (ISelectable selectedElement in selection)
{
sourceItem = selectedElement as VisualElement;
if (sourceItem == null)
continue;
}
if (!Contains(sourceItem))
{
SetDragIndicatorVisible(false);
return EventPropagation.Continue;
}
var target = evt.target as VisualElement;
// Vector2 localPosition = target.ChangeCoordinatesTo(this, evt.localMousePosition);
Vector2 localPosition = target.ChangeCoordinatesTo(this, evt.imguiEvent.mousePosition);
m_InsertIndex = InsertionIndex(localPosition);
if (m_InsertIndex != -1)
{
float indicatorY = 0;
if (m_InsertIndex == childCount)
{
VisualElement lastChild = this[childCount - 1];
indicatorY = lastChild.ChangeCoordinatesTo(this, new Vector2(0, lastChild.layout.height + lastChild.style.marginBottom)).y;
}
else
{
VisualElement childAtInsertIndex = this[m_InsertIndex];
indicatorY = childAtInsertIndex.ChangeCoordinatesTo(this, new Vector2(0, -childAtInsertIndex.style.marginTop)).y;
}
SetDragIndicatorVisible(true);
m_DragIndicator.layout = new Rect(0, indicatorY - m_DragIndicator.layout.height / 2, layout.width, m_DragIndicator.layout.height);
return EventPropagation.Stop;
}
else
{
SetDragIndicatorVisible(false);
m_InsertIndex = -1;
return EventPropagation.Continue;
}
}
int IndexOf(VisualElement element)
{
var index = 0;
foreach (var childElement in Children())
{
if (childElement == element)
return index;
index++;
}
return -1;
}
struct VisualElementPair
{
public VisualElement Item1;
public VisualElement Item2;
public VisualElementPair(VisualElement item1, VisualElement item2)
{
Item1 = item1;
Item2 = item2;
}
}
// public bool DragPerform(DragPerformEvent evt, IEnumerable<ISelectable> selection, IDropTarget dropTarget)
public EventPropagation DragPerform(IMGUIEvent evt, IEnumerable<ISelectable> selection, IDropTarget dropTarget)
{
if (m_InsertIndex != -1)
{
List<VisualElementPair> draggedElements = new List<VisualElementPair>();
foreach (ISelectable selectedElement in selection)
{
var draggedElement = selectedElement as VisualElement;
if (draggedElement != null && Contains(draggedElement))
{
draggedElements.Add(new VisualElementPair(FindSectionDirectChild(draggedElement), draggedElement));
}
}
if (draggedElements.Count == 0)
{
SetDragIndicatorVisible(false);
return EventPropagation.Continue;
}
// Sorts the dragged elements from their relative order in their parent
draggedElements.Sort((pair1, pair2) => { return IndexOf(pair1.Item1).CompareTo(IndexOf(pair2.Item1)); });
int insertIndex = m_InsertIndex;
foreach (var draggedElement in draggedElements)
{
VisualElement sectionDirectChild = draggedElement.Item1;
int indexOfDraggedElement = IndexOf(sectionDirectChild);
if (!((indexOfDraggedElement == insertIndex) || ((insertIndex - 1) == indexOfDraggedElement)))
{
Blackboard blackboard = GetFirstAncestorOfType<Blackboard>();
if (blackboard.moveItemRequested != null)
{
blackboard.moveItemRequested(blackboard, m_InsertIndex, draggedElement.Item2);
}
else
{
if (insertIndex == contentContainer.childCount)
{
sectionDirectChild.BringToFront();
}
else
{
sectionDirectChild.PlaceBehind(this[insertIndex]);
}
}
}
if (insertIndex > indexOfDraggedElement) // No need to increment the insert index for the next dragged element if the current dragged element is above the current insert location.
continue;
insertIndex++;
}
}
SetDragIndicatorVisible(false);
return EventPropagation.Stop;
}
EventPropagation IDropTarget.DragExited()
{
SetDragIndicatorVisible(false);
return EventPropagation.Continue;
}
}
}
#endif
正在加载...
取消
保存