浏览代码

Update to latest GraphView

Signed-off-by: joce <joce@unity3d.com>
/main
joce 8 年前
当前提交
134a3007
共有 4 个文件被更改,包括 145 次插入93 次删除
  1. 106
      MaterialGraphProject/Assets/NewUI/Editor/DataWatchTests/DataWatchTests.cs
  2. 50
      MaterialGraphProject/Assets/NewUI/Editor/GraphViewDataMapper.cs
  3. 70
      MaterialGraphProject/Assets/NewUI/Editor/BaseDataMapper.cs
  4. 12
      MaterialGraphProject/Assets/NewUI/Editor/BaseDataMapper.cs.meta

106
MaterialGraphProject/Assets/NewUI/Editor/DataWatchTests/DataWatchTests.cs


using System;
using UnityEngine;
using UnityEngine.RMGUI;
using UnityEngine.PlaymodeTests;
using UnityEngine.Assertions;
using UnityEngine.TestTools;
using NUnit.Framework;
[EditModeTest]
[UnityPlatform(TestPlatform.EditMode)]
public int calls;
public DataWatchService watch;
public MyWindow()
{
watch = new DataWatchService();
}
public void OnDataChanged()
{
calls++;
}
}
class MyData : ScriptableObject

int m_Calls;
MyData data;
DataWatchService watch;
public DataWatchTests()
[UnityTest]
public IEnumerator DataWatchCallsOnChanged()
data = ScriptableObject.CreateInstance<MyData>();
watch = new DataWatchService();
}
void OnDataChanged()
{
m_Calls++;
}
var data = ScriptableObject.CreateInstance<MyData>();
[EditModeTest]
public IEnumerator DataWatchCallsOnChanged()
{
IDataWatchHandle handle = watch.AddWatch(window.rootVisualContainer, data, OnDataChanged);
Assert.IsTrue(watch.IsActive(handle), "Expected watch to be active");
IDataWatchHandle handle = window.watch.AddWatch(window.rootVisualContainer, data, window.OnDataChanged);
Assert.IsTrue(window.watch.IsActive(handle), "Expected watch to be active");
watch.ProcessNotificationQueue();
Assert.AreEqual(m_Calls, 1, "Expected first call after change");
window.watch.ProcessNotificationQueue();
Assert.AreEqual(window.calls, 1, "Expected first call after change");
watch.ProcessNotificationQueue();
Assert.AreEqual(m_Calls, 1, "Expected no new call after no change");
window.watch.ProcessNotificationQueue();
Assert.AreEqual(window.calls, 1, "Expected no new call after no change");
watch.ProcessNotificationQueue();
Assert.AreEqual(m_Calls, 1, "Expected no new call after idempotent change");
window.watch.ProcessNotificationQueue();
Assert.AreEqual(window.calls, 1, "Expected no new call after idempotent change");
watch.ProcessNotificationQueue();
Assert.AreEqual(m_Calls, 2, "Expect second callback after change");
window.watch.ProcessNotificationQueue();
Assert.AreEqual(window.calls, 2, "Expect second callback after change");
handle.Dispose();
window.Close();

[EditModeTest]
[UnityTest]
public IEnumerator DataWatchCallsOnChangedForDestroyedObject() {
var window = EditorWindow.GetWindow<MyWindow>();

IDataWatchHandle handle = watch.AddWatch(window.rootVisualContainer, data, OnDataChanged);
var data = ScriptableObject.CreateInstance<MyData>();
IDataWatchHandle handle = window.watch.AddWatch(window.rootVisualContainer, data, window.OnDataChanged);
Assert.IsTrue(watch.IsActive(handle), "Expected watch to still be active after destruction");
watch.ProcessNotificationQueue();
Assert.AreEqual(m_Calls, 1, "Expect a call after destruction");
Assert.IsTrue(window.watch.IsActive(handle), "Expected watch to still be active after destruction");
window.watch.ProcessNotificationQueue();
Assert.AreEqual(window.calls, 1, "Expect a call after destruction");
[EditModeTest]
[UnityTest]
var data = ScriptableObject.CreateInstance<MyData>();
IDataWatchHandle handle = watch.AddWatch(window.rootVisualContainer, data, OnDataChanged);
IDataWatchHandle handle = window.watch.AddWatch(window.rootVisualContainer, data, window.OnDataChanged);
Assert.IsFalse(watch.IsActive(handle), "Expected watch to not be active after removal");
Assert.IsFalse(window.watch.IsActive(handle), "Expected watch to not be active after removal");
Assert.AreEqual(m_Calls, 0, "Expected no call after removing watch");
Assert.AreEqual(window.calls, 0, "Expected no call after removing watch");
[EditModeTest]
[UnityTest]
var data = ScriptableObject.CreateInstance<MyData>();
handle = watch.AddWatch(window.rootVisualContainer, data, removeOnCall);
handle = window.watch.AddWatch(window.rootVisualContainer, data, removeOnCall);
watch.ProcessNotificationQueue();
window.watch.ProcessNotificationQueue();
Assert.IsFalse(watch.IsActive(handle), "Expected watch to not be active after removal");
Assert.IsFalse(window.watch.IsActive(handle), "Expected watch to not be active after removal");
[EditModeTest]
[UnityTest]
var data = ScriptableObject.CreateInstance<MyData>();
IDataWatchHandle handle = watch.AddWatch(window.rootVisualContainer, data, OnDataChanged);
IDataWatchHandle handle = window.watch.AddWatch(window.rootVisualContainer, data, window.OnDataChanged);
watch.ProcessNotificationQueue();
Assert.AreEqual(m_Calls, 1, "Expected first call after change");
window.watch.ProcessNotificationQueue();
Assert.AreEqual(window.calls, 1, "Expected first call after change");
watch.ProcessNotificationQueue();
Assert.AreEqual(m_Calls, 2, "Expected second call after undo");
window.watch.ProcessNotificationQueue();
Assert.AreEqual(window.calls, 2, "Expected second call after undo");
watch.ProcessNotificationQueue();
Assert.AreEqual(m_Calls, 3, "Expected third call after redo");
window.watch.ProcessNotificationQueue();
Assert.AreEqual(window.calls, 3, "Expected third call after redo");
handle.Dispose();
window.Close();

50
MaterialGraphProject/Assets/NewUI/Editor/GraphViewDataMapper.cs


using System;
using System.Collections.Generic;
public class GraphViewDataMapper
public class GraphViewDataMapper : BaseDataMapper<GraphElementPresenter, GraphElement>
private readonly Dictionary<Type, Type> m_DataToViewDict = new Dictionary<Type, Type>();
public Type this[Type t]
public GraphViewDataMapper() : base(typeof(FallbackGraphElement))
get
{
return m_DataToViewDict[t];
}
set
{
if (!t.IsSubclassOf(typeof(GraphElementPresenter)))
{
throw new ArgumentException("The type passed as key does not derive from UnityEngine.Object.");
}
if (!value.IsSubclassOf(typeof(GraphElement)))
{
throw new ArgumentException("The type passed as value does not derive from DataContainer.");
}
m_DataToViewDict[t] = value;
}
public GraphElement Create(GraphElementPresenter presenter)
public override GraphElement Create(GraphElementPresenter key)
Type viewType = null;
Type dataType = presenter.GetType();
while (viewType == null && dataType != typeof(GraphElementPresenter))
GraphElement elem = base.Create(key);
if (elem != null)
if (!m_DataToViewDict.TryGetValue(dataType, out viewType))
{
dataType = dataType.BaseType;
}
elem.presenter = key;
if (viewType == null)
{
viewType = typeof(FallbackGraphElement);
}
var dataContainer = (GraphElement)Activator.CreateInstance(viewType);
dataContainer.presenter = presenter;
return dataContainer;
return elem;
}
}
}

70
MaterialGraphProject/Assets/NewUI/Editor/BaseDataMapper.cs


using System;
using System.Collections.Generic;
using System.Linq;
namespace RMGUI.GraphView
{
public abstract class BaseDataMapper<TKey, TValue>
{
private readonly Dictionary<Type, Type> m_Mappings = new Dictionary<Type, Type>();
private readonly Type m_FallbackType;
private static readonly Type k_KeyType;
private static readonly Type k_ValueType;
static BaseDataMapper()
{
k_KeyType = typeof(TKey);
k_ValueType = typeof(TValue);
}
public Type this[Type t]
{
get { return m_Mappings[t]; }
set
{
if (!t.IsSubclassOf(k_KeyType) && !t.GetInterfaces().Contains(k_KeyType))
{
throw new ArgumentException("The type passed as key (" + t.Name + ") does not implement or derive from " + k_KeyType.Name + ".");
}
if (!value.IsSubclassOf(k_ValueType))
{
throw new ArgumentException("The type passed as value ("+ value.Name + ") does not derive from " + k_ValueType.Name + ".");
}
m_Mappings[t] = value;
}
}
public virtual TValue Create(TKey key)
{
Type valueType = null;
Type keyType = key.GetType();
while (valueType == null && keyType != null && keyType != typeof(TKey))
{
if (!m_Mappings.TryGetValue(keyType, out valueType))
{
keyType = keyType.BaseType;
}
}
if (valueType == null)
{
valueType = m_FallbackType;
}
return InternalCreate(valueType);
}
protected BaseDataMapper(Type fallbackType)
{
m_FallbackType = fallbackType;
}
protected virtual TValue InternalCreate(Type valueType)
{
return (TValue) Activator.CreateInstance(valueType);
}
}
}

12
MaterialGraphProject/Assets/NewUI/Editor/BaseDataMapper.cs.meta


fileFormatVersion: 2
guid: 1fa55599f19fa274d9bc2d296fb12861
timeCreated: 1484682630
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存