chore: minor work around

1.dragging a property from blackboard now create a node with an output port

2.add a rename text field to modify node's title fast
main
taoria 3 years ago
parent de0d55d07a
commit 1e16ddda5c
  1. 80
      TNode/Editor/BaseViews/DataGraphView.cs
  2. 46
      TNode/Editor/BaseViews/NodeView.cs
  3. 3
      TNode/Editor/Cache/NodeEditorExtensions.cs
  4. 3
      TNode/Editor/Manipulators.meta
  5. 4
      TNode/Editor/Search/NodeSearchWindowProvider.cs
  6. 2
      TNode/Editor/Tools/NodeCreator/NodeCreator.cs
  7. 0
      TNode/Editor/Tools/NodeCreator/NodeCreator.cs.meta
  8. 4
      TNode/JsonSerialize/JsonSerializeTool.cs
  9. 10
      TNode/Models/BlackboardDragNodeData.cs

@ -7,7 +7,7 @@ using TNode.Cache;
using TNode.Editor.GraphBlackboard;
using TNode.Editor.Inspector;
using TNode.Editor.Model;
using TNode.Editor.Tools.NodeCreator;
using TNode.Models;
using Unity.VisualScripting;
using UnityEditor;
@ -111,7 +111,7 @@ namespace TNode.Editor.BaseViews{
}
*/
public abstract class DataGraphView<T>:GraphView,IDataGraphView where T:GraphData{
#region variablesandproperties
#region variables and properties
@ -138,7 +138,10 @@ namespace TNode.Editor.BaseViews{
}
public event DataChangedEventHandler OnDataChanged;
#endregion
#region event declarations
public delegate void DataChangedEventHandler(object sender, DataChangedEventArgs<T> e);
#endregion
//A Constructor for the DataGraphView ,never to override it
@ -176,12 +179,19 @@ namespace TNode.Editor.BaseViews{
});
}
private void OnInit(){
ConstructDefaultBehaviour();
OnGraphViewCreate();
}
public void RegisterDragEvent(){
RegisterCallback<DragUpdatedEvent>(OnDragUpdated);
RegisterCallback<DragPerformEvent>(OnDragPerform);
}
#endregion
#region event callbakc
private void OnDragPerform(DragPerformEvent evt){
if (DragAndDrop.GetGenericData("DragSelection") is List<ISelectable>{Count: > 0} data){
@ -191,17 +201,12 @@ namespace TNode.Editor.BaseViews{
//Make a constructor of BlackboardDragNodeData<field.PropertyType > by reflection
var specifiedType =
typeof(BlackboardDragNodeData<>).MakeGenericType(field.BlackboardProperty.PropertyType);
//get specific constructor of specified type with two parameters, one is a string ,another is a blackboarddata
var constructor = specifiedType.GetConstructor(new[] {typeof(string),typeof(BlackboardData)});
//create a new instance of the specified type with the constructor and the two parameters
if (constructor != null){
var dragNodeData = constructor.Invoke(new object[]
{field.BlackboardProperty.PropertyName, _data.blackboardData}) as NodeData;
this.AddTNode(dragNodeData,new Rect(Owner.position.position+evt.mousePosition,new Vector2(100,100)));
}
//Create a new instance of specified type
var dragNodeData = NodeCreator.InstantiateNodeData(specifiedType);
this.AddTNode(dragNodeData,new Rect(evt.mousePosition,new Vector2(200,200)));
}
}
}
}
@ -217,10 +222,11 @@ namespace TNode.Editor.BaseViews{
}
private void OnInit(){
ConstructDefaultBehaviour();
OnGraphViewCreate();
}
#endregion
public void ResetGraphView(){
//Clear all nodes
foreach (var node in nodes){
@ -268,7 +274,7 @@ namespace TNode.Editor.BaseViews{
public virtual void CreateInspector(){
NodeInspector nodeInspector = new NodeInspector();
this.Add(nodeInspector);
@ -276,13 +282,13 @@ namespace TNode.Editor.BaseViews{
_isInspectorOn = true;
}
public void CreateMiniMap(Rect rect){
public virtual void CreateMiniMap(Rect rect){
var miniMap = new MiniMap();
this.Add(miniMap);
miniMap.SetPosition(rect);
}
public void CreateBlackboard(){
public virtual void CreateBlackboard(){
_blackboard = new Blackboard();
//Blackboard add "Add Node" button
// blackboard.Add(new BlackboardSection(){
@ -370,11 +376,10 @@ namespace TNode.Editor.BaseViews{
}
}
private void SaveEdge(){
var links = new List<NodeLink>();
foreach (var edge in edges){
var inputNode = edge.input.node as INodeView;
var outputNode = edge.output.node as INodeView;
var links = new List<NodeLink>();
Debug.Log($"Edge{inputNode},{outputNode}");
if (inputNode != null && outputNode != null){
var inputNodeData = inputNode.GetNodeData();
var outputNodeData = outputNode.GetNodeData();
@ -388,17 +393,16 @@ namespace TNode.Editor.BaseViews{
});
links.Add(newNodeLink);
}
_data.nodeLinks = links;
}
_data.nodeLinks = links;
}
private void SaveGraphData(){
_data.NodeDictionary.Clear();
_data.nodeLinks.Clear();
SaveNode();
SaveEdge();
EditorUtility.SetDirty(_data);
}
@ -436,7 +440,6 @@ namespace TNode.Editor.BaseViews{
}
}
});
if(nodeView is INodeView nodeViewInterface){
nodeViewInterface.SetNodeData(nodeData);
}
@ -448,30 +451,27 @@ namespace TNode.Editor.BaseViews{
menu.AddItem(new GUIContent("Delete"), false, () => {
RemoveElement(nodeView);
if (nodeView is INodeView tNodeView){
var nodeData1 = tNodeView.GetNodeData();
_data.NodeDictionary.Remove(nodeData1.id);
_nodeDict.Remove(nodeData1.id);
//Break all edges connected to this node
foreach (var edge in edges){
if (edge.input.node == nodeView || edge.output.node == nodeView){
RemoveElement(edge);
}
}
Owner.graphEditorData.graphElementsData.RemoveAll(x => x.guid == nodeData1.id);
RemoveTNode(tNodeView.GetNodeData());
}
});
menu.ShowAsContext();
});
}
}
public void RemoveTNode(NodeData nodeData){
throw new NotImplementedException();
_data.NodeDictionary.Remove(nodeData.id);
var nodeView = _nodeDict[nodeData.id];
_nodeDict.Remove(nodeData.id);
//Break all edges connected to this node
foreach (var edge in edges){
if (edge.input.node == nodeView || edge.output.node == nodeView){
RemoveElement(edge);
}
}
Owner.graphEditorData.graphElementsData.RemoveAll(x => x.guid == nodeData.id);
}
}

@ -7,6 +7,7 @@ using UnityEditor;
using UnityEditor.Experimental.GraphView;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;
namespace TNode.Editor.BaseViews{
@ -41,10 +42,13 @@ namespace TNode.Editor.BaseViews{
protected NodeView(){
OnDataChanged+=OnDataChangedHandler;
_nodeInspectorInNode = new NodeInspectorInNode(){
name = "nodeInspectorInNode"
};
this.extensionContainer.Add(_nodeInspectorInNode);
BuildDoubleClickRename();
}
private void OnDataChangedHandler(T obj){
this.title = _data.nodeName;
@ -83,6 +87,48 @@ namespace TNode.Editor.BaseViews{
}
}
public void StartARenameTitleTextField(){
var textField = new TextField{
value = title,
style ={
//Make the text filed overlap the title container
position = Position.Absolute,
left = 0,
top = 0,
width = titleContainer.layout.width,
height = titleContainer.layout.height
}
};
textField.StretchToParentSize();
textField.RegisterValueChangedCallback(evt2 => {
title = evt2.newValue;
});
textField.RegisterCallback<FocusOutEvent>(evt2 => {
title = textField.text;
((NodeDataWrapper)_data).SetValue("nodeName",textField.text);
textField.RemoveFromHierarchy();
});
//if enter is pressed ,set the title and remove the text field
textField.RegisterCallback<KeyDownEvent>(evt2 => {
if (evt2.keyCode == KeyCode.Return){
title = textField.text;
((NodeDataWrapper)_data).SetValue("nodeName",textField.text);
textField.RemoveFromHierarchy();
}
});
titleContainer.Add(textField);
textField.Focus();
}
private void BuildDoubleClickRename(){
//when double click titleContainer ,create a textfield to rename the node
titleContainer.RegisterCallback<MouseDownEvent>(evt => {
if (evt.clickCount == 2){
StartARenameTitleTextField();
}
});
}
public void SetNodeData(NodeData nodeData){
Data = (T)nodeData;

@ -145,6 +145,9 @@ namespace TNode.Cache{
}
public static object CreateNodeViewFromNodeType(Type t){
//Check the generic type of NodeView by t
Debug.Log(t);
Debug.Log(t.ToString());
Debug.Log(typeof(NodeData).IsAssignableFrom(t));
var type = typeof(NodeView<>).MakeGenericType(t);
if (NodeEditorSingleton.Instance.FromGenericToSpecific.ContainsKey(type)){
var implementedType = NodeEditorSingleton.Instance.FromGenericToSpecific[type];

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 400542f3cec140e2b55e2bcf637b2d9b
timeCreated: 1657009018

@ -48,8 +48,8 @@ namespace TNode.Editor{
//Check if type is derived from NodeData
if (typeof(NodeData).IsAssignableFrom(type)){
//Make an instance of the type
if (NodeHelper.InstantiateNodeData(type) is { } nodeData){
nodeData.nodeName = "New Node";
if (NodeCreator.InstantiateNodeData(type) is { } nodeData){
nodeData.nodeName = $"New {type.Name}";
((IDataGraphView) _graphView).AddTNode(nodeData, new Rect(localPos.x, localPos.y, 100, 100));
}
}

@ -2,7 +2,7 @@
using TNode.Models;
namespace TNode.Editor.Tools.NodeCreator{
public static class NodeHelper{
public static class NodeCreator{
/// <summary>
/// always use this to create a new node.

@ -21,7 +21,9 @@ namespace TNode.JsonSerialize{
DateFormatString = "yyyy-MM-dd HH:mm:ss",
Converters = new List<JsonConverter> { new Vector3Converter() },
TypeNameHandling = TypeNameHandling.Auto,
ContractResolver = new WritablePropertiesOnlyResolver()
ContractResolver = new WritablePropertiesOnlyResolver(),
Formatting = Formatting.Indented
};

@ -4,17 +4,17 @@ using TNode.Attribute.Ports;
using TNode.RuntimeCache;
namespace TNode.Models{
public class BlackboardDragNodeData<T>:NodeData where T:BlackboardData{
public class BlackboardDragNodeData<T>:NodeData{
[JsonIgnore]
private string _blackDragData;
[JsonIgnore]
private T _blackboardData;
private BlackboardData _blackboardData;
[Output]
public T Value => _blackboardData.GetValue<T>(_blackDragData);
public BlackboardDragNodeData(string blackDragData,T blackboardData){
_blackDragData = blackDragData;
_blackboardData = blackboardData;
public BlackboardDragNodeData(){
}
}

Loading…
Cancel
Save