Skip to content

Commit bc939c2

Browse files
committed
Signal system refactored
1 parent a4e2c19 commit bc939c2

File tree

8 files changed

+51
-55
lines changed

8 files changed

+51
-55
lines changed
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
using UnityEngine;
2+
using RuntimeNodeEditor;
23

34
public class ApplicationStartup : MonoBehaviour
45
{
56
public Transform editorHolder1;
7+
public Transform editorHolder2;
68
public ExampleNodeEditor editor1;
9+
public ExampleNodeEditor editor2;
710

811
private void Start()
912
{
1013
Application.targetFrameRate = 60;
11-
var graph1 = editor1.CreateGraph<MyGraph>(editorHolder1);
14+
var graph1 = editor1.CreateGraph<NodeGraph>(editorHolder1);
1215
editor1.StartEditor(graph1);
16+
17+
var graph2 = editor2.CreateGraph<NodeGraph>(editorHolder2);
18+
editor2.StartEditor(graph2);
1319
}
1420

1521
private void Update()
1622
{
1723
editor1.UpdateEditor();
24+
editor2.UpdateEditor();
1825
}
1926
}

Assets/Example/Scripts/ExampleNodeEditor.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using RuntimeNodeEditor;
1+
using System;
2+
using RuntimeNodeEditor;
23
using UnityEngine;
34
using UnityEngine.EventSystems;
45

@@ -12,10 +13,12 @@ public override void StartEditor(NodeGraph graph)
1213

1314
_savePath = Application.dataPath + "/Example/Resources/graph.json";
1415

15-
Graph.pointerListener.GraphPointerClickEvent += OnGraphPointerClick;
16-
Graph.pointerListener.GraphPointerDragEvent += OnGraphPointerDrag;
17-
Graph.SignalSystem.NodePointerClickEvent += OnNodePointerClick;
18-
Graph.SignalSystem.NodeConnectionPointerClickEvent += OnNodeConnectionPointerClick;
16+
Graph.GraphPointerListener.GraphPointerClickEvent += OnGraphPointerClick;
17+
Graph.GraphPointerListener.GraphPointerDragEvent += OnGraphPointerDrag;
18+
Graph.EventListener.NodePointerClickEvent += OnNodePointerClick;
19+
Graph.EventListener.NodeConnectionPointerClickEvent += OnNodeConnectionPointerClick;
20+
Graph.EventListener.SocketConnect += OnSocketConnection;
21+
Graph.EventListener.SocketDisconnect += OnSocketDisconnection;
1922
}
2023

2124
private void OnGraphPointerClick(PointerEventData eventData)
@@ -75,6 +78,16 @@ private void OnNodeConnectionPointerClick(string connId, PointerEventData eventD
7578
}
7679
}
7780

81+
private void OnSocketConnection(SocketInput input, SocketOutput output)
82+
{
83+
Debug.Log("connect");
84+
}
85+
86+
private void OnSocketDisconnection(SocketInput input, SocketOutput output)
87+
{
88+
Debug.Log("disconnect");
89+
}
90+
7891

7992
// context item actions
8093
private void CreateFloatNode()

Assets/Example/Scripts/MyGraph.cs

Lines changed: 0 additions & 15 deletions
This file was deleted.

Assets/Example/Scripts/MyGraph.cs.meta

Lines changed: 0 additions & 11 deletions
This file was deleted.

Assets/RuntimeNodeEditor/Scripts/Graph/NodeGraph.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ namespace RuntimeNodeEditor
1010
{
1111
public class NodeGraph : MonoBehaviour
1212
{
13-
public RectTransform GraphContainer => _graphContainer;
14-
public SignalSystem SignalSystem => _signalSystem;
13+
public RectTransform GraphContainer => _graphContainer;
14+
public GraphPointerListener GraphPointerListener => pointerListener;
15+
public SignalSystem EventListener => _signalSystem;
1516

1617
// scene references
1718
public RectTransform contextMenuContainer;
@@ -61,7 +62,7 @@ public void Create(string prefabPath)
6162
mousePosition = UnityEngine.InputSystem.Mouse.current.position.ReadValue();
6263
#endif
6364
var pos = Utility.GetLocalPointIn(nodeContainer, mousePosition);
64-
var node = Utility.CreateNodePrefab<Node>(prefabPath);
65+
var node = Utility.CreateNodePrefab<Node>(prefabPath, nodeContainer);
6566
node.Init(_signalSystem, pos, NewId(), prefabPath);
6667
node.Setup();
6768
nodes.Add(node);
@@ -70,7 +71,7 @@ public void Create(string prefabPath)
7071

7172
public void Create(string prefabPath, Vector2 pos)
7273
{
73-
var node = Utility.CreateNodePrefab<Node>(prefabPath);
74+
var node = Utility.CreateNodePrefab<Node>(prefabPath, nodeContainer);
7475
node.Init(_signalSystem, pos, NewId(), prefabPath);
7576
node.Setup();
7677
nodes.Add(node);
@@ -391,7 +392,7 @@ private void HandleSocketRegister(Node node)
391392

392393
private void LoadNode(NodeData data)
393394
{
394-
var node = Utility.CreateNodePrefab<Node>(data.path);
395+
var node = Utility.CreateNodePrefab<Node>(data.path, nodeContainer);
395396
node.Init(_signalSystem, new Vector2(data.posX, data.posY), data.id, data.path);
396397
node.Setup();
397398
nodes.Add(node);

Assets/RuntimeNodeEditor/Scripts/NodeEditor/NodeEditor.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,10 @@ public virtual void StartEditor(NodeGraph graph)
2323
_graph = graph;
2424
_graph.Init();
2525
_graph.pointerListener.Init(_graph.GraphContainer, minZoom, maxZoom);
26-
Utility.Initialize(_graph.nodeContainer, _graph.contextMenuContainer);
2726

2827

2928
_contextMenu = Instantiate(contextMenuPrefab, _graph.contextMenuContainer).GetComponent<ContextMenu>();
30-
_contextMenu.Init(Graph.SignalSystem);
29+
_contextMenu.Init(Graph.EventListener);
3130
CloseContextMenu();
3231
}
3332

@@ -40,7 +39,7 @@ public void UpdateEditor()
4039
public void DisplayContextMenu()
4140
{
4241
_contextMenu.Clear();
43-
_contextMenu.Show(_contextMenuData, Utility.GetCtxMenuPointerPosition());
42+
_contextMenu.Show(_contextMenuData, Utility.GetCtxMenuPointerPosition(Graph.contextMenuContainer));
4443
}
4544

4645
public void CloseContextMenu()

Assets/RuntimeNodeEditor/Scripts/SignalSystem.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public class SignalSystem
1414
public event Action<Node, PointerEventData> NodePointerDragEvent;
1515
public event Action<ContextMenuData, ContextContainer> OnMenuItemClicked;
1616
public event Action<string, PointerEventData> NodeConnectionPointerClickEvent;
17+
public event Action<SocketInput, SocketOutput> SocketConnect;
18+
public event Action<SocketInput, SocketOutput> SocketDisconnect;
1719

1820
public void InvokeSocketDragFrom(SocketOutput output)
1921
{
@@ -59,5 +61,15 @@ public void InvokeNodeConnectionPointerClick(string connId, PointerEventData eve
5961
{
6062
NodeConnectionPointerClickEvent?.Invoke(connId, eventData);
6163
}
64+
65+
public void InvokeSocketConnection(SocketInput input, SocketOutput output)
66+
{
67+
SocketConnect?.Invoke(input, output);
68+
}
69+
70+
public void InvokeSocketDisconnection(SocketInput input, SocketOutput output)
71+
{
72+
SocketDisconnect?.Invoke(input, output);
73+
}
6274
}
6375
}

Assets/RuntimeNodeEditor/Scripts/Utility.cs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,19 @@ namespace RuntimeNodeEditor
44
{
55
public class Utility
66
{
7-
private static RectTransform _nodeContainer;
8-
private static RectTransform _contextMenuContainer;
9-
10-
public static void Initialize(RectTransform nodeContainer, RectTransform contextMenuContainer)
11-
{
12-
_nodeContainer = nodeContainer;
13-
_contextMenuContainer = contextMenuContainer;
14-
}
15-
16-
177
public static Vector2 GetLocalPointIn(RectTransform container, Vector3 pos, Camera eventCamera = null)
188
{
199
var point = Vector2.zero;
2010
RectTransformUtility.ScreenPointToLocalPointInRectangle(container, pos, eventCamera, out point);
2111
return point;
2212
}
2313

24-
public static Vector2 GetCtxMenuPointerPosition()
14+
public static Vector2 GetCtxMenuPointerPosition(RectTransform rect)
2515
{
2616
Vector2 localPointerPos;
2717
var success = false;
2818
#if ENABLE_LEGACY_INPUT_MANAGER
29-
success = RectTransformUtility.ScreenPointToLocalPointInRectangle(_contextMenuContainer,
19+
success = RectTransformUtility.ScreenPointToLocalPointInRectangle(rect,
3020
Input.mousePosition,
3121
null,
3222
out localPointerPos);
@@ -42,7 +32,7 @@ public static Vector2 GetCtxMenuPointerPosition()
4232

4333

4434

45-
public static T CreatePrefab<T>(string path, Transform parent)
35+
public static T CreatePrefab<T>(string path, RectTransform parent)
4636
{
4737
var prefab = Resources.Load<GameObject>(path);
4838
var instance = GameObject.Instantiate(prefab, parent);
@@ -51,9 +41,9 @@ public static T CreatePrefab<T>(string path, Transform parent)
5141
return component;
5242
}
5343

54-
public static T CreateNodePrefab<T>(string path)
44+
public static T CreateNodePrefab<T>(string path, RectTransform parent)
5545
{
56-
return CreatePrefab<T>(path, _nodeContainer);
46+
return CreatePrefab<T>(path, parent);
5747
}
5848

5949
public static Vector3 QuadraticCurve(Vector3 a, Vector3 b, Vector3 c, float t)

0 commit comments

Comments
 (0)