浏览代码

#11 #43 Copy, paste & duplicate now works properly and is handled by the window rather than GraphView

/main
Peter Bay Bastian 7 年前
当前提交
9dd4edeb
共有 5 个文件被更改,包括 49 次插入150 次删除
  1. 24
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/MaterialGraphEditWindow.cs
  2. 33
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/MaterialGraphPresenter.cs
  3. 35
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/MaterialGraphView.cs
  4. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Manipulators/Commandable.cs.meta
  5. 95
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Manipulators/Commandable.cs

24
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/MaterialGraphEditWindow.cs


Repaint();
}
void OnGUI()
{
var presenter = m_GraphEditorView.presenter;
var e = Event.current;
if (e.type == EventType.ValidateCommand && (
e.commandName == "Copy" && presenter.canCopy
|| e.commandName == "Paste" && presenter.canPaste
|| e.commandName == "Duplicate" && presenter.canDuplicate))
{
e.Use();
}
if (e.type == EventType.ExecuteCommand)
{
if (e.commandName == "Copy")
presenter.Copy();
if (e.commandName == "Paste")
presenter.Paste();
if (e.commandName == "Duplicate")
presenter.Duplicate();
}
}
public void PingAsset()
{
if (selected != null)

33
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/MaterialGraphPresenter.cs


m_Container.Repaint();
}
private void UpdateData()
void UpdateData()
{
// Find all nodes currently being drawn which are no longer in the graph (i.e. deleted)
var deletedElements = m_Elements

}
}
private CopyPasteGraph CreateCopyPasteGraph(IEnumerable<GraphElementPresenter> selection)
CopyPasteGraph CreateCopyPasteGraph(IEnumerable<GraphElementPresenter> selection)
{
var graph = new CopyPasteGraph();
foreach (var presenter in selection)

return graph;
}
private CopyPasteGraph DeserializeCopyBuffer(string copyBuffer)
CopyPasteGraph DeserializeCopyBuffer(string copyBuffer)
{
try
{

}
}
private void InsertCopyPasteGraph(CopyPasteGraph graph)
void InsertCopyPasteGraph(CopyPasteGraph graph)
{
if (graph == null || graphAsset == null || graphAsset.graph == null)
return;

graphAsset.drawingData.selection = addedNodes.Select(n => n.guid);
}
public void Copy(IEnumerable<GraphElementPresenter> selection)
public bool canCopy
var graph = CreateCopyPasteGraph(selection);
get { return elements.Any(e => e.selected); }
}
public void Copy()
{
var graph = CreateCopyPasteGraph(elements.Where(e => e.selected));
public void Duplicate(IEnumerable<GraphElementPresenter> selection)
public bool canPaste
var graph = DeserializeCopyBuffer(JsonUtility.ToJson(CreateCopyPasteGraph(selection), true));
InsertCopyPasteGraph(graph);
get { return DeserializeCopyBuffer(EditorGUIUtility.systemCopyBuffer) != null; }
}
public void Paste()

}
public bool canDuplicate
{
get { return canCopy; }
}
public void Duplicate()
{
var graph = DeserializeCopyBuffer(JsonUtility.ToJson(CreateCopyPasteGraph(elements.Where(e => e.selected)), true));
InsertCopyPasteGraph(graph);
}
public override void AddElement(EdgePresenter edge)

35
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/MaterialGraphView.cs


{Event.KeyboardEvent("o"), FrameOrigin},
{Event.KeyboardEvent("delete"), DeleteSelection},
{Event.KeyboardEvent("#tab"), FramePrev},
{Event.KeyboardEvent("tab"), FrameNext},
{Event.KeyboardEvent("#c"), CopySelection},
{Event.KeyboardEvent("#v"), Paste},
{Event.KeyboardEvent("#d"), DuplicateSelection}
{Event.KeyboardEvent("tab"), FrameNext}
this.AddManipulator(new Commandable
{
{ "Duplicate", () => true, () => Debug.Log("Duplicate!") },
{ "Copy", () => true, () => Debug.Log("Copy!") }
});
this.AddManipulator(new ContentZoomer());
this.AddManipulator(new ContentDragger());

{
base.ClearSelection();
PropagateSelection();
}
public EventPropagation CopySelection()
{
var graphDataSource = GetPresenter<MaterialGraphPresenter>();
if (selection.Any() && graphDataSource != null)
graphDataSource.Copy(selection.OfType<GraphElement>().Select(ge => ge.presenter));
return EventPropagation.Stop;
}
public EventPropagation DuplicateSelection()
{
var graphDataSource = GetPresenter<MaterialGraphPresenter>();
if (selection.Any() && graphDataSource != null)
graphDataSource.Duplicate(selection.OfType<GraphElement>().Select(ge => ge.presenter));
return EventPropagation.Stop;
}
public EventPropagation Paste()
{
var graphDataSource = GetPresenter<MaterialGraphPresenter>();
if (graphDataSource != null)
graphDataSource.Paste();
return EventPropagation.Stop;
}
}
}

12
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Manipulators/Commandable.cs.meta


fileFormatVersion: 2
guid: 370985c9b2ecbcb4e8e74b7932e51baa
timeCreated: 1483970657
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

95
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Manipulators/Commandable.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Experimental.UIElements;
namespace UnityEditor.MaterialGraph.Drawing
{
public class Commandable : Manipulator, IEnumerable<KeyValuePair<string, CommandHandler>>
{
private readonly Dictionary<string, CommandHandler> m_Dictionary;
public Commandable()
{
m_Dictionary = new Dictionary<string, CommandHandler>();
}
public void HandleEvent(EventBase evt)
{
var isValidation = evt.imguiEvent.type == EventType.ValidateCommand;
var isExecution = evt.imguiEvent.type == EventType.ExecuteCommand;
if (isValidation || isExecution)
Debug.Log(evt.imguiEvent.commandName);
CommandHandler handler;
if ((!isValidation && !isExecution) || !m_Dictionary.TryGetValue(evt.imguiEvent.commandName, out handler))
{
return;
}
if (isValidation && handler.Validate())
{
evt.StopPropagation();
return;
}
if (!isExecution)
{
return;
}
handler.Execute();
evt.StopPropagation();
}
public void Add(string commandName, CommandValidator validator, CommandExecutor executor)
{
m_Dictionary[commandName] = new CommandHandler(validator, executor);
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public IEnumerator<KeyValuePair<string, CommandHandler>> GetEnumerator()
{
return m_Dictionary.GetEnumerator();
}
protected override void RegisterCallbacksOnTarget()
{
target.RegisterCallback<IMGUIEvent>(HandleEvent);
}
protected override void UnregisterCallbacksFromTarget()
{
target.UnregisterCallback<IMGUIEvent>(HandleEvent);
}
}
public delegate bool CommandValidator();
public delegate void CommandExecutor();
public class CommandHandler
{
private readonly CommandValidator m_Validator;
private readonly CommandExecutor m_Executor;
public CommandHandler(CommandValidator validator, CommandExecutor executor)
{
m_Validator = validator;
m_Executor = executor;
}
public bool Validate()
{
if (m_Validator != null)
return m_Validator();
return false;
}
public void Execute()
{
if (m_Executor != null)
m_Executor();
}
}
}
正在加载...
取消
保存