浏览代码

Fix compile errors on trunk by introducing various compatibility extensions

/main
Peter Bay Bastian 7 年前
当前提交
fecfaaea
共有 15 个文件被更改,包括 77 次插入744 次删除
  1. 3
      com.unity.shadergraph/Editor/Drawing/Blackboard/BlackboardProvider.cs
  2. 2
      com.unity.shadergraph/Editor/Drawing/MaterialGraphEditWindow.cs
  3. 46
      com.unity.shadergraph/Editor/Drawing/Views/MaterialGraphView.cs
  4. 9
      com.unity.shadergraph/Editor/Drawing/Views/MaterialNodeView.cs
  5. 6
      com.unity.shadergraph/Editor/Drawing/Views/ShaderPort.cs
  6. 33
      com.unity.shadergraph/Editor/Util/CompatibilityExtensions.cs
  7. 3
      com.unity.shadergraph/Editor/Util/CompatibilityExtensions.cs.meta
  8. 11
      com.unity.shadergraph/Editor/Drawing/Blackboard/Blackboard.cs.meta
  9. 11
      com.unity.shadergraph/Editor/Drawing/Blackboard/BlackboardField.cs.meta
  10. 11
      com.unity.shadergraph/Editor/Drawing/Blackboard/BlackboardRow.cs.meta
  11. 11
      com.unity.shadergraph/Editor/Drawing/Blackboard/BlackboardSection.cs.meta
  12. 132
      com.unity.shadergraph/Editor/Drawing/Blackboard/Blackboard.cs
  13. 65
      com.unity.shadergraph/Editor/Drawing/Blackboard/BlackboardRow.cs
  14. 299
      com.unity.shadergraph/Editor/Drawing/Blackboard/BlackboardSection.cs
  15. 179
      com.unity.shadergraph/Editor/Drawing/Blackboard/BlackboardField.cs

3
com.unity.shadergraph/Editor/Drawing/Blackboard/BlackboardProvider.cs


using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor.Experimental.UIElements.GraphView;
using UnityEditor.Graphing;
using UnityEngine;
using UnityEngine.Experimental.UIElements;

if (create)
{
field.RenameGo();
field.OpenTextEditor();
row.expanded = true;
m_Graph.owner.RegisterCompleteObjectUndo("Create Property");
m_Graph.AddShaderProperty(property);

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


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
namespace UnityEditor.ShaderGraph.Drawing

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


if (selection.OfType<MaterialNodeView>().Count() == 1 && selection.OfType<MaterialNodeView>().First().node is SubGraphNode)
{
evt.menu.AppendSeparator();
evt.menu.AppendAction("Open Sub Graph", OpenSubGraph, ContextualMenu.MenuAction.AlwaysEnabled);
evt.menu.AppendAction("Open Sub Graph", OpenSubGraph, ContextualMenu.MenuAction.StatusFlags.Normal);
}
}
else if (evt.target is BlackboardField)

if (evt.target is MaterialGraphView)
{
evt.menu.AppendAction("Collapse Previews", CollapsePreviews, ContextualMenu.MenuAction.AlwaysEnabled);
evt.menu.AppendAction("Expand Previews", ExpandPreviews, ContextualMenu.MenuAction.AlwaysEnabled);
evt.menu.AppendAction("Collapse Previews", CollapsePreviews, ContextualMenu.MenuAction.StatusFlags.Normal);
evt.menu.AppendAction("Expand Previews", ExpandPreviews, ContextualMenu.MenuAction.StatusFlags.Normal);
void CollapsePreviews(EventBase evt)
void CollapsePreviews()
{
graph.owner.RegisterCompleteObjectUndo("Collapse Previews");
foreach (AbstractMaterialNode node in graph.GetNodes<AbstractMaterialNode>())

}
void ExpandPreviews(EventBase evt)
void ExpandPreviews()
{
graph.owner.RegisterCompleteObjectUndo("Expand Previews");
foreach (AbstractMaterialNode node in graph.GetNodes<AbstractMaterialNode>())

}
void SeeDocumentation(EventBase evt)
void SeeDocumentation()
{
var node = selection.OfType<MaterialNodeView>().First().node;
if (node.documentationURL != null)

void OpenSubGraph(EventBase evt)
void OpenSubGraph()
{
SubGraphNode subgraphNode = selection.OfType<MaterialNodeView>().First().node as SubGraphNode;

ContextualMenu.MenuAction.StatusFlags SeeDocumentationStatus(EventBase eventBase)
ContextualMenu.MenuAction.StatusFlags SeeDocumentationStatus()
{
if (selection.OfType<MaterialNodeView>().First().node.documentationURL == null)
return ContextualMenu.MenuAction.StatusFlags.Disabled;

ContextualMenu.MenuAction.StatusFlags ConvertToPropertyStatus(EventBase eventBase)
ContextualMenu.MenuAction.StatusFlags ConvertToPropertyStatus()
{
if (selection.OfType<MaterialNodeView>().Any(v => v.node != null))
{

return ContextualMenu.MenuAction.StatusFlags.Hidden;
}
void ConvertToProperty(EventBase eventBase)
void ConvertToProperty()
{
var selectedNodeViews = selection.OfType<MaterialNodeView>().Select(x => x.node).ToList();
foreach (var node in selectedNodeViews)

}
}
ContextualMenu.MenuAction.StatusFlags ConvertToInlineNodeStatus(EventBase eventBase)
ContextualMenu.MenuAction.StatusFlags ConvertToInlineNodeStatus()
{
if (selection.OfType<MaterialNodeView>().Any(v => v.node != null))
{

return ContextualMenu.MenuAction.StatusFlags.Hidden;
}
void ConvertToInlineNode(EventBase eventBase)
void ConvertToInlineNode()
{
var selectedNodeViews = selection.OfType<MaterialNodeView>()
.Select(x => x.node)

((AbstractMaterialGraph)propNode.owner).ReplacePropertyNodeWithConcreteNode(propNode);
}
ContextualMenu.MenuAction.StatusFlags ConvertToSubgraphStatus(EventBase eventBase)
ContextualMenu.MenuAction.StatusFlags ConvertToSubgraphStatus()
void ConvertToSubgraph(EventBase eventBase)
void ConvertToSubgraph()
{
onConvertToSubgraphClick();
}

return selection.OfType<BlackboardField>().Any();
}
#if UNITY_2018_1
public EventPropagation DragUpdated(IMGUIEvent evt, IEnumerable<ISelectable> selection, IDropTarget dropTarget)
{
return EventPropagation.Continue;

{
return EventPropagation.Continue;
}
#else
public bool DragUpdated(DragUpdatedEvent evt, IEnumerable<ISelectable> selection, IDropTarget dropTarget)
{
return true;
}
public bool DragPerform(DragPerformEvent evt, IEnumerable<ISelectable> selection, IDropTarget dropTarget)
{
return true;
}
bool IDropTarget.DragExited()
{
return true;
}
#endif
}
public static class GraphViewExtensions

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


public override void BuildContextualMenu(ContextualMenuPopulateEvent evt)
{
if (evt.target is Node)
evt.menu.AppendAction("Copy shader", ConvertToShader, ConvertToShaderStatus);
evt.menu.AppendAction("Copy shader", ConvertToShader, node.hasPreview ? ContextualMenu.MenuAction.StatusFlags.Normal : ContextualMenu.MenuAction.StatusFlags.Hidden);
ContextualMenu.MenuAction.StatusFlags ConvertToShaderStatus(EventBase eventBase)
{
return node.hasPreview ? ContextualMenu.MenuAction.StatusFlags.Normal : ContextualMenu.MenuAction.StatusFlags.Hidden;
}
void ConvertToShader(EventBase eventBase)
void ConvertToShader()
{
List<PropertyCollector.TextureInfo> textureInfo;
var masterNode = node as IMasterNode;

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


{
sealed class ShaderPort : Port
{
ShaderPort(Orientation portOrientation, Direction portDirection, Type type)
: base(portOrientation, portDirection, type) { AddStyleSheetPath("Styles/ShaderPort"); }
ShaderPort(Orientation portOrientation, Direction portDirection, Capacity portCapacity, Type type)
: base(portOrientation, portDirection, portCapacity, type) { AddStyleSheetPath("Styles/ShaderPort"); }
var port = new ShaderPort(Orientation.Horizontal, slot.isInputSlot ? Direction.Input : Direction.Output, null)
var port = new ShaderPort(Orientation.Horizontal, slot.isInputSlot ? Direction.Input : Direction.Output, slot.isInputSlot ? Capacity.Single : Capacity.Multi, null)
{
m_EdgeConnector = new EdgeConnector<Edge>(connectorListener),
};

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


using System;
using UnityEngine;
using UnityEngine.Experimental.UIElements;
#if UNITY_2018_1
using UnityEditor.Experimental.UIElements.GraphView;
#endif
namespace UnityEditor.ShaderGraph.Drawing
{
public static class CompatibilityExtensions
{
#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);
Debug.Assert(actionStatusCallback != null);
contextualMenu.AppendAction(actionName, e => action(), e => actionStatusCallback());
}
public static void AppendAction(this ContextualMenu contextualMenu, string actionName, Action action, ContextualMenu.MenuAction.StatusFlags statusFlags)
{
Debug.Assert(action != null);
contextualMenu.AppendAction(actionName, e => action(), e => statusFlags);
}
}
}

3
com.unity.shadergraph/Editor/Util/CompatibilityExtensions.cs.meta


fileFormatVersion: 2
guid: 7c1717b624ad45ccbb3fa6bec28af711
timeCreated: 1519741123

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:

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


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());
}
}
}

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


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;
}
}
}

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


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;
}
}
}

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


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();
}
}
}
正在加载...
取消
保存