浏览代码

Update ChannelEnumControl to UIElements and USS

/main
Matt Dean 7 年前
当前提交
ee59804c
共有 2 个文件被更改,包括 58 次插入32 次删除
  1. 63
      com.unity.shadergraph/Editor/Drawing/Controls/ChannelEnumControl.cs
  2. 27
      com.unity.shadergraph/Editor/Resources/Styles/Controls/ChannelEnumControlView.uss

63
com.unity.shadergraph/Editor/Drawing/Controls/ChannelEnumControl.cs


using System;
using System.Collections.Generic;
using UnityEditor.Experimental.UIElements;
namespace UnityEditor.ShaderGraph.Drawing.Controls
{

public class ChannelEnumControlView : VisualElement, INodeModificationListener
{
GUIContent m_Label;
IMGUIContainer m_Container;
PopupField<string> m_PopupField;
string[] m_ValueNames;
public ChannelEnumControlView(string label, int slotId, AbstractMaterialNode node, PropertyInfo propertyInfo)
{

m_SlotId = slotId;
if (!propertyInfo.PropertyType.IsEnum)
throw new ArgumentException("Property must be an enum.", "propertyInfo");
m_Label = new GUIContent(label ?? ObjectNames.NicifyVariableName(propertyInfo.Name));
m_Container = new IMGUIContainer(OnGUIHandler);
Add(m_Container);
Add(new Label(label ?? ObjectNames.NicifyVariableName(propertyInfo.Name)));
var value = (Enum)m_PropertyInfo.GetValue(m_Node, null);
m_ValueNames = Enum.GetNames(value.GetType());
CreatePopup();
void OnGUIHandler()
void OnValueChanged(ChangeEvent<string> evt)
UpdatePopup();
var index = m_PopupField.index;
var value = (int)m_PropertyInfo.GetValue(m_Node, null);
if (!index.Equals(value))
{
m_Node.owner.owner.RegisterCompleteObjectUndo("Change " + m_Node.name);
m_PropertyInfo.SetValue(m_Node, index, null);
}
CreatePopup();
if (scope == ModificationScope.Graph)
m_Container.Dirty(ChangeType.Repaint);
if (scope == ModificationScope.Node)
{
CreatePopup();
m_PopupField.Dirty(ChangeType.Repaint);
}
private void UpdatePopup()
void CreatePopup()
var value = (int)m_PropertyInfo.GetValue(m_Node, null);
using (var changeCheckScope = new EditorGUI.ChangeCheckScope())
{
int channelCount = SlotValueHelper.GetChannelCount(m_Node.FindSlot<MaterialSlot>(m_SlotId).concreteValueType);
var enumEntryCount = (Enum)m_PropertyInfo.GetValue(m_Node, null);
string[] enumEntryNames = Enum.GetNames(enumEntryCount.GetType());
string[] popupEntries = new string[channelCount];
for (int i = 0; i < popupEntries.Length; i++)
popupEntries[i] = enumEntryNames[i];
value = EditorGUILayout.Popup(m_Label, value, popupEntries);
if(m_PopupField != null)
Remove(m_PopupField);
int channelCount = SlotValueHelper.GetChannelCount(m_Node.FindSlot<MaterialSlot>(m_SlotId).concreteValueType);
List<string> popupEntries = new List<string>();
for (int i = 0; i < channelCount; i++)
popupEntries.Add(m_ValueNames[i]);
if (changeCheckScope.changed)
{
m_Node.owner.owner.RegisterCompleteObjectUndo("Change " + m_Node.name);
m_PropertyInfo.SetValue(m_Node, value, null);
}
}
m_PopupField = new PopupField<string>(popupEntries, (int)m_PropertyInfo.GetValue(m_Node, null));
m_PopupField.OnValueChanged(OnValueChanged);
Add(m_PopupField);
}
}
}

27
com.unity.shadergraph/Editor/Resources/Styles/Controls/ChannelEnumControlView.uss


ChannelEnumControlView {
flex-direction: row;
flex: 1;
padding-left: 8;
padding-right: 8;
padding-top: 4;
padding-bottom: 4;
width: 200;
}
ChannelEnumControlView > .popupField {
width: 76;
flex: 0;
margin-left: 0;
margin-right: 8;
margin-top: 4;
margin-bottom: 4;
}
ChannelEnumControlView > Label {
width: 100;
padding-left: 0;
padding-right: 0;
padding-top: 0;
padding-bottom: 0;
margin-left: 8;
margin-right: 8;
margin-top: 4;
margin-bottom: 4;
}
正在加载...
取消
保存