|
|
|
|
|
|
using System.Linq; |
|
|
|
using System.Reflection; |
|
|
|
using UnityEditor.Experimental.UIElements.GraphView; |
|
|
|
using UnityEditor.Graphing.Util; |
|
|
|
using UnityEngine; |
|
|
|
using UnityEngine.Experimental.UIElements; |
|
|
|
using UnityEngine; |
|
|
|
using UnityEngine.Experimental.UIElements; |
|
|
|
using Edge = UnityEditor.Experimental.UIElements.GraphView.Edge; |
|
|
|
using UnityEngine.Experimental.UIElements.StyleSheets; |
|
|
|
public class MaterialNodeView : Node |
|
|
|
public sealed class MaterialNodeView : Node |
|
|
|
public AbstractMaterialNode node { get; private set; } |
|
|
|
List<VisualElement> m_ControlViews; |
|
|
|
PreviewData m_Preview; |
|
|
|
Image m_PreviewImage; |
|
|
|
VisualElement m_PreviewToggle; |
|
|
|
VisualElement m_ControlsContainer; |
|
|
|
|
|
|
|
public MaterialNodeView(AbstractMaterialNode inNode, PreviewSystem previewSystem) |
|
|
|
{ |
|
|
|
AddToClassList("MaterialNode"); |
|
|
|
|
|
|
|
if (inNode == null) |
|
|
|
return; |
|
|
|
|
|
|
|
node = inNode; |
|
|
|
title = inNode.name; |
|
|
|
|
|
|
|
m_ControlsContainer = new VisualElement |
|
|
|
{ |
|
|
|
name = "controls" |
|
|
|
}; |
|
|
|
leftContainer.Add(m_ControlsContainer); |
|
|
|
|
|
|
|
m_PreviewToggle = new VisualElement { name = "toggle", text = "" }; |
|
|
|
m_PreviewToggle.AddManipulator(new Clickable(() => previewExpanded = !previewExpanded)); |
|
|
|
if (node.hasPreview) |
|
|
|
m_PreviewToggle.RemoveFromClassList("inactive"); |
|
|
|
else |
|
|
|
m_PreviewToggle.AddToClassList("inactive"); |
|
|
|
previewExpanded = node.previewExpanded; |
|
|
|
leftContainer.Add(m_PreviewToggle); |
|
|
|
protected List<GraphControlPresenter> m_Controls = new List<GraphControlPresenter>(); |
|
|
|
m_PreviewImage = new Image |
|
|
|
{ |
|
|
|
name = "preview", |
|
|
|
pickingMode = PickingMode.Ignore, |
|
|
|
image = Texture2D.whiteTexture |
|
|
|
}; |
|
|
|
m_Preview = previewSystem.GetPreview(inNode); |
|
|
|
m_Preview.onPreviewChanged += UpdatePreviewTexture; |
|
|
|
UpdatePreviewTexture(); |
|
|
|
leftContainer.Add(m_PreviewImage); |
|
|
|
public List<GraphControlPresenter> controls |
|
|
|
{ |
|
|
|
get { return m_Controls; } |
|
|
|
m_ControlViews = new List<VisualElement>(); |
|
|
|
foreach (var propertyInfo in node.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) |
|
|
|
foreach (IControlAttribute attribute in propertyInfo.GetCustomAttributes(typeof(IControlAttribute), false)) |
|
|
|
m_ControlViews.Add(attribute.InstantiateControl(node, propertyInfo)); |
|
|
|
expanded = node.drawState.expanded; |
|
|
|
|
|
|
|
AddSlots(node.GetSlots<ISlot>()); |
|
|
|
|
|
|
|
SetPosition(new Rect(node.drawState.position.x, node.drawState.position.y, 0, 0)); |
|
|
|
UpdateControls(); |
|
|
|
|
|
|
|
if (node is PreviewNode) |
|
|
|
{ |
|
|
|
var resizeHandle = new VisualElement { name = "resize", text = "" }; |
|
|
|
resizeHandle.AddManipulator(new Draggable(OnResize)); |
|
|
|
Add(resizeHandle); |
|
|
|
|
|
|
|
UpdateSize(); |
|
|
|
} |
|
|
|
int m_Version; |
|
|
|
PreviewData m_Preview; |
|
|
|
|
|
|
|
public Texture previewTexture { get; private set; } |
|
|
|
public AbstractMaterialNode node { get; private set; } |
|
|
|
|
|
|
|
public override bool expanded |
|
|
|
{ |
|
|
|
|
|
|
if (base.expanded != value) |
|
|
|
{ |
|
|
|
|
|
|
|
if (node.drawState.expanded != value) |
|
|
|
{ |
|
|
|
|
|
|
|
UpdateControls(); |
|
|
|
public virtual void OnModified(ModificationScope scope) |
|
|
|
bool previewExpanded |
|
|
|
m_Version++; |
|
|
|
get { return node.previewExpanded; } |
|
|
|
set |
|
|
|
{ |
|
|
|
node.previewExpanded = value; |
|
|
|
m_PreviewToggle.text = node.previewExpanded ? "▲" : "▼"; |
|
|
|
} |
|
|
|
} |
|
|
|
public void OnModified(ModificationScope scope) |
|
|
|
{ |
|
|
|
// Update slots to match node modification
|
|
|
|
if (scope == ModificationScope.Topological) |
|
|
|
{ |
|
|
|
var slots = node.GetSlots<ISlot>().ToList(); |
|
|
|
|
|
|
{ |
|
|
|
} |
|
|
|
{ |
|
|
|
} |
|
|
|
foreach (var ve in anchorsToRemove) |
|
|
|
outputContainer.Remove(ve); |
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
protected virtual IEnumerable<GraphControlPresenter> GetControlData() |
|
|
|
{ |
|
|
|
return Enumerable.Empty<GraphControlPresenter>(); |
|
|
|
} |
|
|
|
|
|
|
|
protected void AddSlots(IEnumerable<ISlot> slots) |
|
|
|
void AddSlots(IEnumerable<ISlot> slots) |
|
|
|
{ |
|
|
|
foreach (var slot in slots) |
|
|
|
{ |
|
|
|
|
|
|
data.userData = slot; |
|
|
|
|
|
|
|
if (slot.isOutputSlot) |
|
|
|
{ |
|
|
|
} |
|
|
|
{ |
|
|
|
} |
|
|
|
public virtual void Initialize(INode inNode, PreviewSystem previewSystem) |
|
|
|
{ |
|
|
|
node = inNode as AbstractMaterialNode; |
|
|
|
userData = node; |
|
|
|
|
|
|
|
if (node == null) |
|
|
|
return; |
|
|
|
|
|
|
|
title = inNode.name; |
|
|
|
expanded = node.drawState.expanded; |
|
|
|
|
|
|
|
AddSlots(node.GetSlots<ISlot>()); |
|
|
|
|
|
|
|
var controlData = GetControlData(); |
|
|
|
controls.AddRange(controlData); |
|
|
|
|
|
|
|
SetPosition(new Rect(node.drawState.position.x, node.drawState.position.y, 0, 0)); |
|
|
|
|
|
|
|
m_Version = 0; |
|
|
|
|
|
|
|
m_Preview = previewSystem.GetPreview(inNode); |
|
|
|
m_Preview.onPreviewChanged += OnPreviewChanged; |
|
|
|
|
|
|
|
// From OnDataChange()
|
|
|
|
|
|
|
|
m_PreviewToggle.text = node.previewExpanded ? "▲" : "▼"; |
|
|
|
if (node.hasPreview) |
|
|
|
m_PreviewToggle.RemoveFromClassList("inactive"); |
|
|
|
else |
|
|
|
m_PreviewToggle.AddToClassList("inactive"); |
|
|
|
|
|
|
|
UpdateControls(); |
|
|
|
|
|
|
|
UpdatePreviewTexture(node.previewExpanded ? previewTexture : null); |
|
|
|
|
|
|
|
m_NodeGuid = node.guid; |
|
|
|
if (node is PreviewNode) |
|
|
|
{ |
|
|
|
if (!m_ResizeHandleAdded) |
|
|
|
{ |
|
|
|
m_ResizeHandle = new VisualElement() { name = "resize", text = "" }; |
|
|
|
m_ResizeHandle.AddManipulator(new Draggable(OnResize)); |
|
|
|
Add(m_ResizeHandle); |
|
|
|
|
|
|
|
m_ResizeHandleAdded = true; |
|
|
|
} |
|
|
|
|
|
|
|
UpdateSize(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void OnReplaced(INode previous, INode current) |
|
|
|
{ |
|
|
|
node = current as AbstractMaterialNode; |
|
|
|
} |
|
|
|
|
|
|
|
void OnPreviewChanged() |
|
|
|
{ |
|
|
|
previewTexture = m_Preview.texture; |
|
|
|
UpdatePreviewTexture(node.previewExpanded ? previewTexture : null); |
|
|
|
m_Version++; |
|
|
|
} |
|
|
|
|
|
|
|
public void Dispose() |
|
|
|
{ |
|
|
|
if (m_Preview != null) |
|
|
|
{ |
|
|
|
m_Preview.onPreviewChanged -= OnPreviewChanged; |
|
|
|
m_Preview = null; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
VisualElement m_ControlsContainer; |
|
|
|
List<VisualElement> m_ControlViews; |
|
|
|
Guid m_NodeGuid; |
|
|
|
VisualElement m_PreviewToggle; |
|
|
|
VisualElement m_ResizeHandle; |
|
|
|
Image m_PreviewImage; |
|
|
|
bool m_IsScheduled; |
|
|
|
|
|
|
|
bool m_ResizeHandleAdded; |
|
|
|
|
|
|
|
public MaterialNodeView() |
|
|
|
{ |
|
|
|
CreateContainers(); |
|
|
|
|
|
|
|
AddToClassList("MaterialNode"); |
|
|
|
} |
|
|
|
|
|
|
|
void CreateContainers() |
|
|
|
{ |
|
|
|
m_ControlsContainer = new VisualElement |
|
|
|
{ |
|
|
|
name = "controls" |
|
|
|
}; |
|
|
|
leftContainer.Add(m_ControlsContainer); |
|
|
|
m_ControlViews = new List<VisualElement>(); |
|
|
|
|
|
|
|
m_PreviewToggle = new VisualElement { name = "toggle", text = "" }; |
|
|
|
m_PreviewToggle.AddManipulator(new Clickable(OnPreviewToggle)); |
|
|
|
leftContainer.Add(m_PreviewToggle); |
|
|
|
|
|
|
|
m_PreviewImage = new Image |
|
|
|
{ |
|
|
|
name = "preview", |
|
|
|
pickingMode = PickingMode.Ignore, |
|
|
|
image = Texture2D.whiteTexture |
|
|
|
}; |
|
|
|
|
|
|
|
leftContainer.Add(m_PreviewImage); |
|
|
|
|
|
|
|
m_ResizeHandleAdded = false; |
|
|
|
} |
|
|
|
|
|
|
|
float updatedWidth = leftContainer.layout.width + deltaSize.x; |
|
|
|
float updatedHeight = m_PreviewImage.layout.height + deltaSize.y; |
|
|
|
var updatedWidth = leftContainer.layout.width + deltaSize.x; |
|
|
|
var updatedHeight = m_PreviewImage.layout.height + deltaSize.y; |
|
|
|
PreviewNode previewNode = node as PreviewNode; |
|
|
|
|
|
|
|
var previewNode = node as PreviewNode; |
|
|
|
if (previewNode != null) |
|
|
|
{ |
|
|
|
previewNode.SetDimensions(updatedWidth, updatedHeight); |
|
|
|
|
|
|
|
|
|
|
void OnPreviewToggle() |
|
|
|
{ |
|
|
|
node.previewExpanded = !node.previewExpanded; |
|
|
|
m_PreviewToggle.text = node.previewExpanded ? "▲" : "▼"; |
|
|
|
} |
|
|
|
|
|
|
|
void UpdatePreviewTexture(Texture previewTexture) |
|
|
|
void UpdatePreviewTexture() |
|
|
|
if (previewTexture == null) |
|
|
|
if (m_Preview.texture == null || !node.previewExpanded) |
|
|
|
{ |
|
|
|
m_PreviewImage.visible = false; |
|
|
|
m_PreviewImage.RemoveFromClassList("visible"); |
|
|
|
|
|
|
m_PreviewImage.visible = true; |
|
|
|
m_PreviewImage.AddToClassList("visible"); |
|
|
|
m_PreviewImage.RemoveFromClassList("hidden"); |
|
|
|
m_PreviewImage.image = previewTexture; |
|
|
|
m_PreviewImage.image = m_Preview.texture; |
|
|
|
|
|
|
|
if (!node.guid.Equals(m_NodeGuid)) |
|
|
|
{ |
|
|
|
m_ControlViews.Clear(); |
|
|
|
foreach (var propertyInfo in node.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) |
|
|
|
{ |
|
|
|
foreach (IControlAttribute attribute in propertyInfo.GetCustomAttributes(typeof(IControlAttribute), false)) |
|
|
|
m_ControlViews.Add(attribute.InstantiateControl(node, propertyInfo)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (!expanded) |
|
|
|
{ |
|
|
|
m_ControlsContainer.Clear(); |
|
|
|
|
|
|
var previewNode = node as PreviewNode; |
|
|
|
|
|
|
|
if (previewNode == null) |
|
|
|
{ |
|
|
|
} |
|
|
|
float width = previewNode.width; |
|
|
|
float height = previewNode.height; |
|
|
|
var width = previewNode.width; |
|
|
|
var height = previewNode.height; |
|
|
|
} |
|
|
|
|
|
|
|
public void Dispose() |
|
|
|
{ |
|
|
|
if (m_Preview != null) |
|
|
|
{ |
|
|
|
m_Preview.onPreviewChanged -= UpdatePreviewTexture; |
|
|
|
m_Preview = null; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |