浏览代码

UIElements-based controls for color and vector controls

/main
Peter Bay Bastian 7 年前
当前提交
99701f90
共有 4 个文件被更改,包括 107 次插入99 次删除
  1. 2
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Implementation/SerializableGraph.cs
  2. 24
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/ColorControl.cs
  3. 161
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/VectorControl.cs
  4. 19
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Resources/Styles/MaterialGraph.uss

2
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Implementation/SerializableGraph.cs


public virtual void ReplaceWith(IGraph other)
{
using (var pooledList = ListPool<IEdge>.GetDisposable())
{
var removedNodeEdges = pooledList.value;

24
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/ColorControl.cs


using System;
using System.Reflection;
using UnityEditor.Experimental.UIElements;
using UnityEngine;
using UnityEngine.Experimental.UIElements;
using UnityEngine.Experimental.UIElements.StyleSheets;

public class ColorControlView : VisualElement
{
GUIContent m_Label;
AbstractMaterialNode m_Node;
PropertyInfo m_PropertyInfo;

m_PropertyInfo = propertyInfo;
if (propertyInfo.PropertyType != typeof(Color))
throw new ArgumentException("Property must be of type Color.", "propertyInfo");
m_Label = new GUIContent(label ?? ObjectNames.NicifyVariableName(propertyInfo.Name));
Add(new IMGUIContainer(OnGUIHandler));
label = label ?? ObjectNames.NicifyVariableName(propertyInfo.Name);
if (!string.IsNullOrEmpty(label))
Add(new Label(label));
var colorField = new ColorField { value = (Color) m_PropertyInfo.GetValue(m_Node, null) };
colorField.OnValueChanged(OnChange);
Add(colorField);
void OnGUIHandler()
void OnChange(ChangeEvent<Color> evt)
var value = (Color) m_PropertyInfo.GetValue(m_Node, null);
using (var changeCheckScope = new EditorGUI.ChangeCheckScope())
{
value = EditorGUILayout.ColorField(m_Label, value);
if (changeCheckScope.changed)
m_PropertyInfo.SetValue(m_Node, value, null);
}
m_Node.owner.owner.RegisterCompleteObjectUndo("Color Change");
m_PropertyInfo.SetValue(m_Node, evt.newValue, null);
Dirty(ChangeType.Repaint);
}
}
}

161
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/VectorControl.cs


using System;
using System.Linq;
using System.Reflection;
using UnityEditor.Experimental.UIElements;
using UnityEngine.Experimental.UIElements.StyleSheets;
using UnityEngine.MaterialGraph;
namespace UnityEditor.MaterialGraph.Drawing.Controls

AbstractMaterialNode m_Node;
PropertyInfo m_PropertyInfo;
float[] m_Values;
GUIContent[] m_Labels;
GUIContent m_Label;
float m_Height;
Action m_Read;
Action m_Write;
Vector4 m_Value;
int m_UndoGroup = -1;
SerializedPropertyType serializedPropertyType;
{
serializedPropertyType = SerializedPropertyType.Float;
m_Read = ReadFloat;
m_Write = WriteFloat;
}
{
serializedPropertyType = SerializedPropertyType.Vector2;
m_Read = ReadVector2;
m_Write = WriteVector2;
}
{
serializedPropertyType = SerializedPropertyType.Vector3;
m_Read = ReadVector3;
m_Write = WriteVector3;
}
{
serializedPropertyType = SerializedPropertyType.Vector4;
m_Read = ReadVector4;
m_Write = WriteVector4;
}
{
}
m_Label = new GUIContent(label ?? ObjectNames.NicifyVariableName(propertyInfo.Name));
m_Values = new float[components];
m_Labels = new GUIContent[components];
m_Labels[0] = new GUIContent(subLabel1);
label = label ?? ObjectNames.NicifyVariableName(propertyInfo.Name);
if (!string.IsNullOrEmpty(label))
Add(new Label(label));
m_Value = GetValue();
AddField(0, subLabel1);
m_Labels[1] = new GUIContent(subLabel2);
AddField(1, subLabel2);
m_Labels[2] = new GUIContent(subLabel3);
AddField(2, subLabel3);
m_Labels[3] = new GUIContent(subLabel4);
m_Height = EditorGUI.GetPropertyHeight(serializedPropertyType, m_Label);
Add(new IMGUIContainer(OnGUIHandler));
AddField(3, subLabel4);
void ReadFloat()
void AddField(int index, string subLabel)
var value = (float)m_PropertyInfo.GetValue(m_Node, null);
m_Values[0] = value;
}
void ReadVector2()
{
var value = (Vector2)m_PropertyInfo.GetValue(m_Node, null);
m_Values[0] = value.x;
m_Values[1] = value.y;
}
void ReadVector3()
{
var value = (Vector3)m_PropertyInfo.GetValue(m_Node, null);
m_Values[0] = value.x;
m_Values[1] = value.y;
m_Values[2] = value.z;
}
void ReadVector4()
{
var value = (Vector4)m_PropertyInfo.GetValue(m_Node, null);
m_Values[0] = value.x;
m_Values[1] = value.y;
m_Values[2] = value.z;
m_Values[3] = value.w;
}
void WriteFloat()
{
m_PropertyInfo.SetValue(m_Node, m_Values[0], null);
Add(new Label(subLabel));
var doubleField = new DoubleField { userData = index, value = m_Value[index] };
doubleField.RegisterCallback<MouseDownEvent>(Repaint);
doubleField.RegisterCallback<MouseMoveEvent>(Repaint);
doubleField.OnValueChanged(evt =>
{
var value = GetValue();
value[index] = (float)evt.newValue;
SetValue(value);
m_UndoGroup = -1;
// Dirty(ChangeType.Repaint);
});
doubleField.RegisterCallback<InputEvent>(evt =>
{
if (m_UndoGroup == -1)
{
m_UndoGroup = Undo.GetCurrentGroup();
m_Node.owner.owner.RegisterCompleteObjectUndo("Change " + m_Node.name);
}
float newValue;
if (!float.TryParse(evt.newData, out newValue))
newValue = 0f;
var value = GetValue();
value[index] = newValue;
SetValue(value);
});
doubleField.RegisterCallback<KeyDownEvent>(evt =>
{
if (evt.keyCode == KeyCode.Escape && m_UndoGroup > -1)
{
Undo.RevertAllDownToGroup(m_UndoGroup);
m_UndoGroup = -1;
m_Value = GetValue();
evt.StopPropagation();
}
Dirty(ChangeType.Repaint);
});
Add(doubleField);
void WriteVector2()
object ValueToPropertyType(Vector4 value)
m_PropertyInfo.SetValue(m_Node, new Vector2(m_Values[0], m_Values[1]), null);
if (m_PropertyInfo.PropertyType == typeof(float))
return value.x;
if (m_PropertyInfo.PropertyType == typeof(Vector2))
return (Vector2)value;
if (m_PropertyInfo.PropertyType == typeof(Vector3))
return (Vector3)value;
return value;
void WriteVector3()
Vector4 GetValue()
m_PropertyInfo.SetValue(m_Node, new Vector3(m_Values[0], m_Values[1], m_Values[2]), null);
var value = m_PropertyInfo.GetValue(m_Node, null);
if (m_PropertyInfo.PropertyType == typeof(float))
return new Vector4((float) value, 0f, 0f, 0f);
if (m_PropertyInfo.PropertyType == typeof(Vector2))
return (Vector2)value;
if (m_PropertyInfo.PropertyType == typeof(Vector3))
return (Vector3)value;
return (Vector4)value;
void WriteVector4()
void SetValue(Vector4 value)
m_PropertyInfo.SetValue(m_Node, new Vector4(m_Values[0], m_Values[1], m_Values[2], m_Values[3]), null);
m_PropertyInfo.SetValue(m_Node, ValueToPropertyType(value), null);
void OnGUIHandler()
void Repaint<T>(MouseEventBase<T> evt) where T : MouseEventBase<T>, new()
m_Read();
using (var changeCheckScope = new EditorGUI.ChangeCheckScope())
{
var position = EditorGUILayout.GetControlRect(true, m_Height, EditorStyles.numberField);
EditorGUI.MultiFloatField(position, m_Label, m_Labels, m_Values);
if (changeCheckScope.changed)
m_Write();
}
evt.StopPropagation();
Dirty(ChangeType.Repaint);
}
}
}

19
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Resources/Styles/MaterialGraph.uss


ToolbarButtonView:active {
background-color: rgba(0, 0, 0, 0.25);
}
ColorControlView {
flex-direction: row;
}
MultiFloatControlView {
flex-direction: row;
}
MultiFloatControlView > Label {
margin-left: 0;
margin-right: 0;
}
MultiFloatControlView > DoubleField {
flex: 1;
margin-left: 0;
margin-right: 0;
}
正在加载...
取消
保存