浏览代码

Commandable manipulator for #11

/main
Peter Bay Bastian 8 年前
当前提交
36c08357
共有 3 个文件被更改,包括 99 次插入0 次删除
  1. 6
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/SerializableGraphView.cs
  2. 81
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/Manipulators/Commandable.cs
  3. 12
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/Manipulators/Commandable.cs.meta

6
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/SerializableGraphView.cs


{Event.KeyboardEvent("#d"), DuplicateSelection}
}));
AddManipulator(new Commandable
{
{ "Duplicate", () => true, () => Debug.Log("Duplicate!") },
{ "Copy", () => true, () => Debug.Log("Copy!") }
});
AddManipulator(new ClickGlobalSelector());
AddManipulator(new ContentZoomer());
AddManipulator(new ContentDragger());

81
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/Manipulators/Commandable.cs


using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.RMGUI;
namespace UnityEditor.Graphing.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 override EventPropagation HandleEvent(Event evt, VisualElement finalTarget)
{
var isValidation = evt.type == EventType.ValidateCommand;
var isExecution = evt.type == EventType.ExecuteCommand;
if (isValidation || isExecution)
Debug.Log(evt.commandName);
CommandHandler handler;
if ((!isValidation && !isExecution) || !m_Dictionary.TryGetValue(evt.commandName, out handler))
return EventPropagation.Continue;
if (isValidation && handler.Validate())
return EventPropagation.Stop;
if (!isExecution)
return EventPropagation.Continue;
handler.Execute();
return EventPropagation.Stop;
}
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();
}
}
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();
}
}
}

12
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/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:
正在加载...
取消
保存