Skip to content

Commit 838c6c6

Browse files
committed
Graph pointer scroll handler refactored
1 parent 1cbe150 commit 838c6c6

File tree

10 files changed

+49
-77
lines changed

10 files changed

+49
-77
lines changed

Assets/RuntimeNodeEditor/Scripts/Graph/BezierCurveDrawer.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ public class BezierCurveDrawer : MonoBehaviour
1717
private bool _hasRequest;
1818
private Socket _draggingSocket;
1919
private Dictionary<string, ConnectionDrawData> _connections;
20-
private SignalSystem _signal;
20+
private IConnectionEvents _signal;
2121

2222

23-
public void Init(SignalSystem signal)
23+
public void Init(IConnectionEvents signal)
2424
{
2525
_connections = new Dictionary<string, ConnectionDrawData>();
2626
_lineContainer = lineContainer;
@@ -97,13 +97,9 @@ private void DrawConnection(SocketHandle port1, SocketHandle port2, UILineRender
9797
private void DrawDragging(SocketHandle port)
9898
{
9999
Vector2 localPointerPos;
100-
#if ENABLE_LEGACY_INPUT_MANAGER
101-
RectTransformUtility.ScreenPointToLocalPointInRectangle(_lineContainer, Input.mousePosition, null, out localPointerPos);
102-
#endif
103-
#if ENABLE_INPUT_SYSTEM
104-
var mousePosition = UnityEngine.InputSystem.Mouse.current.position.ReadValue();
100+
var mousePosition = Utility.GetMousePosition();
105101
RectTransformUtility.ScreenPointToLocalPointInRectangle(_lineContainer, mousePosition, null, out localPointerPos);
106-
#endif
102+
107103
_pointerLocator.localPosition = localPointerPos;
108104

109105
var pointList = new List<Vector2>();

Assets/RuntimeNodeEditor/Scripts/Graph/GraphPointerListener.cs

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,10 @@
33
using System.Collections.Generic;
44
using UnityEngine;
55
using UnityEngine.EventSystems;
6-
#if ENABLE_INPUT_SYSTEM
7-
using UnityEngine.InputSystem;
8-
#endif
96

107
namespace RuntimeNodeEditor
118
{
12-
public class GraphPointerListener : MonoBehaviour, IPointerClickHandler, IDragHandler
9+
public class GraphPointerListener : MonoBehaviour, IPointerClickHandler, IDragHandler, IScrollHandler
1310
{
1411

1512
private RectTransform _rectTransform;
@@ -40,27 +37,13 @@ public void OnDrag(PointerEventData eventData)
4037
_signalSystem.InvokeGraphPointerDrag(eventData);
4138
}
4239

43-
public void OnUpdate()
40+
public void OnScroll(PointerEventData eventData)
4441
{
45-
//pc input
46-
float scrollWheelInput = 0;
47-
#if ENABLE_LEGACY_INPUT_MANAGER
48-
scrollWheelInput = Input.mouseScrollDelta.y;
49-
#endif
50-
#if ENABLE_INPUT_SYSTEM
51-
scrollWheelInput = Mouse.current.scroll.y.ReadValue() * 0.001f; // Magic Number to match Input scroll values
52-
#endif
53-
54-
if (Mathf.Abs(scrollWheelInput) > float.Epsilon)
42+
if (Mathf.Abs(eventData.scrollDelta.y) > float.Epsilon)
5543
{
56-
_currentZoom *= 1 + scrollWheelInput * _mouseWheelSensitivity;
44+
_currentZoom *= 1 + eventData.scrollDelta.y * _mouseWheelSensitivity;
5745
_currentZoom = Mathf.Clamp(_currentZoom, _minZoom, _maxZoom);
58-
#if ENABLE_LEGACY_INPUT_MANAGER
59-
_zoomCenterPos = (Vector2)Input.mousePosition;
60-
#endif
61-
#if ENABLE_INPUT_SYSTEM
62-
_zoomCenterPos = Mouse.current.position.ReadValue();
63-
#endif
46+
_zoomCenterPos = Utility.GetMousePosition();
6447

6548
Vector2 beforePointInContent;
6649
RectTransformUtility.ScreenPointToLocalPointInRectangle(_rectTransform, _zoomCenterPos, null, out beforePointInContent);

Assets/RuntimeNodeEditor/Scripts/Graph/NodeGraph.cs

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.IO;
1+
using System.Collections.Generic;
42
using System.Linq;
5-
using System.Text;
63
using UnityEngine;
74
using UnityEngine.EventSystems;
85

@@ -13,11 +10,6 @@ public class NodeGraph : MonoBehaviour
1310
public RectTransform GraphContainer => _graphContainer;
1411
public GraphPointerListener GraphPointerListener => pointerListener;
1512

16-
public INodeEvents NodeEvents => _signalSystem;
17-
public ISocketEvents SocketEventListener => _signalSystem;
18-
public IConnectionEvents ConnectionEvents => _signalSystem;
19-
public IContextMenuEvents ContextMenuListener => _signalSystem;
20-
2113
// scene references
2214
public RectTransform contextMenuContainer;
2315
public RectTransform nodeContainer;
@@ -59,16 +51,10 @@ public void Init(SignalSystem signalSystem, float minZoom, float maxZoom)
5951

6052
public void Create(string prefabPath)
6153
{
62-
var mousePosition = Vector2.zero;
63-
#if ENABLE_LEGACY_INPUT_MANAGER
64-
mousePosition = Input.mousePosition;
65-
#endif
66-
#if ENABLE_INPUT_SYSTEM
67-
mousePosition = UnityEngine.InputSystem.Mouse.current.position.ReadValue();
68-
#endif
54+
var mousePosition = Utility.GetMousePosition();
6955
var pos = Utility.GetLocalPointIn(nodeContainer, mousePosition);
7056
var node = Utility.CreateNodePrefab<Node>(prefabPath, nodeContainer);
71-
node.Init(_signalSystem, pos, NewId(), prefabPath);
57+
node.Init(_signalSystem, _signalSystem, pos, NewId(), prefabPath);
7258
node.Setup();
7359
nodes.Add(node);
7460
HandleSocketRegister(node);
@@ -77,7 +63,7 @@ public void Create(string prefabPath)
7763
public void Create(string prefabPath, Vector2 pos)
7864
{
7965
var node = Utility.CreateNodePrefab<Node>(prefabPath, nodeContainer);
80-
node.Init(_signalSystem, pos, NewId(), prefabPath);
66+
node.Init(_signalSystem, _signalSystem, pos, NewId(), prefabPath);
8167
node.Setup();
8268
nodes.Add(node);
8369
HandleSocketRegister(node);
@@ -248,7 +234,6 @@ public void Clear()
248234

249235
public void OnUpdate()
250236
{
251-
pointerListener.OnUpdate();
252237
drawer.UpdateDraw();
253238
}
254239

@@ -398,7 +383,8 @@ private void HandleSocketRegister(Node node)
398383
private void LoadNode(NodeData data)
399384
{
400385
var node = Utility.CreateNodePrefab<Node>(data.path, nodeContainer);
401-
node.Init(_signalSystem, new Vector2(data.posX, data.posY), data.id, data.path);
386+
var pos = new Vector2(data.posX, data.posY);
387+
node.Init(_signalSystem, _signalSystem, pos, data.id, data.path);
402388
node.Setup();
403389
nodes.Add(node);
404390

Assets/RuntimeNodeEditor/Scripts/Line/LinePointerListener.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
using UnityEngine;
1+
using UnityEngine;
72
using UnityEngine.EventSystems;
83
namespace RuntimeNodeEditor
94
{
105
public class LinePointerListener : MonoBehaviour, IPointerClickHandler
116
{
127
public string connId;
13-
private SignalSystem _signal;
8+
private IConnectionEvents _signal;
149

15-
public void Init(SignalSystem signal, string connId)
10+
public void Init(IConnectionEvents signal, string connId)
1611
{
1712
_signal = signal;
1813
this.connId = connId;

Assets/RuntimeNodeEditor/Scripts/Node/Node.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,22 @@ public abstract class Node : MonoBehaviour
2323

2424
private NodeDraggablePanel _dragPanel;
2525
private RectTransform _panelRectTransform;
26-
private SignalSystem _signal;
26+
private INodeEvents _nodeSignal;
27+
private ISocketEvents _socketSignal;
2728

28-
public void Init(SignalSystem signal, Vector2 pos, string id, string path)
29+
public void Init(INodeEvents nodeSignal, ISocketEvents socketSignal, Vector2 pos, string id, string path)
2930
{
3031
ID = id;
3132
LoadPath = path;
3233
Outputs = new List<SocketOutput>();
3334
Inputs = new List<SocketInput>();
3435
ConnectedOutputs = new List<SocketOutput>();
3536

36-
_signal = signal;
37+
_nodeSignal = nodeSignal;
38+
_socketSignal = socketSignal;
3739
_panelRectTransform = gameObject.GetComponent<RectTransform>();
3840
_dragPanel = draggableBody.AddComponent<NodeDraggablePanel>();
39-
_dragPanel.Init(this, _signal);
41+
_dragPanel.Init(this, _nodeSignal);
4042
SetPosition(pos);
4143
}
4244

@@ -49,13 +51,13 @@ public virtual bool CanMove()
4951
}
5052
public void Register(SocketOutput output)
5153
{
52-
output.SetOwner(this, _signal);
54+
output.SetOwner(this, _socketSignal);
5355
Outputs.Add(output);
5456
}
5557

5658
public void Register(SocketInput input)
5759
{
58-
input.SetOwner(this, _signal);
60+
input.SetOwner(this, _socketSignal);
5961
Inputs.Add(input);
6062
}
6163

Assets/RuntimeNodeEditor/Scripts/Node/NodeDraggablePanel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ namespace RuntimeNodeEditor
77
public class NodeDraggablePanel : MonoBehaviour, IPointerClickHandler, IPointerDownHandler, IDragHandler
88
{
99
private Node _ownerNode;
10-
private SignalSystem _signal;
10+
private INodeEvents _signal;
1111

12-
public void Init(Node owner, SignalSystem signal)
12+
public void Init(Node owner, INodeEvents signal)
1313
{
1414
_ownerNode = owner;
1515
_signal = signal;

Assets/RuntimeNodeEditor/Scripts/NodeEditor/NodeEditor.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.IO;
41
using UnityEngine;
5-
using UnityEngine.EventSystems;
62
using UnityEngine.UI;
73

84
namespace RuntimeNodeEditor

Assets/RuntimeNodeEditor/Scripts/NodeEditor/SignalSystem.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ public interface INodeEvents
105105
event Action<Node, PointerEventData> NodePointerDragEvent;
106106

107107
void InvokeNodePointerClick(Node node, PointerEventData eventData);
108+
void InvokeNodePointerDown(Node node, PointerEventData eventData);
109+
void InvokeNodePointerDrag(Node node, PointerEventData eventData);
108110
}
109111

110112
public interface ISocketEvents

Assets/RuntimeNodeEditor/Scripts/Socket/Socket.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ namespace RuntimeNodeEditor
55
public class Socket : MonoBehaviour
66
{
77
public Node OwnerNode { get { return _ownerNode; } }
8-
public SignalSystem Signal { get { return _signal; } }
8+
public ISocketEvents Signal { get { return _signal; } }
99

1010
public string socketId;
1111
public IConnection connection;
1212
public SocketHandle handle;
1313
public ConnectionType connectionType;
1414
private Node _ownerNode;
15-
private SignalSystem _signal;
15+
private ISocketEvents _signal;
1616

17-
public void SetOwner(Node owner, SignalSystem signal)
17+
public void SetOwner(Node owner, ISocketEvents signal)
1818
{
1919
_ownerNode = owner;
2020
_signal = signal;

Assets/RuntimeNodeEditor/Scripts/Utility.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@ namespace RuntimeNodeEditor
44
{
55
public class Utility
66
{
7+
public static Vector2 GetMousePosition()
8+
{
9+
var mousePosition = Vector2.zero;
10+
#if ENABLE_LEGACY_INPUT_MANAGER
11+
mousePosition = Input.mousePosition;
12+
#endif
13+
#if ENABLE_INPUT_SYSTEM
14+
mousePosition = UnityEngine.InputSystem.Mouse.current.position.ReadValue();
15+
#endif
16+
return mousePosition;
17+
}
18+
719
public static Vector2 GetLocalPointIn(RectTransform container, Vector3 pos, Camera eventCamera = null)
820
{
921
var point = Vector2.zero;
@@ -15,18 +27,18 @@ public static Vector2 GetCtxMenuPointerPosition(RectTransform rect)
1527
{
1628
Vector2 localPointerPos;
1729
var success = false;
18-
#if ENABLE_LEGACY_INPUT_MANAGER
30+
#if ENABLE_LEGACY_INPUT_MANAGER
1931
success = RectTransformUtility.ScreenPointToLocalPointInRectangle(rect,
2032
Input.mousePosition,
2133
null,
2234
out localPointerPos);
23-
#endif
24-
#if ENABLE_INPUT_SYSTEM
35+
#endif
36+
#if ENABLE_INPUT_SYSTEM
2537
success = RectTransformUtility.ScreenPointToLocalPointInRectangle(_contextMenuContainer,
2638
UnityEngine.InputSystem.Mouse.current.position.ReadValue(),
2739
null,
2840
out localPointerPos);
29-
#endif
41+
#endif
3042
return localPointerPos;
3143
}
3244

0 commit comments

Comments
 (0)