Skip to content

Commit 3e7b9e3

Browse files
committed
Added blackboard element re-ordering
1 parent 413de98 commit 3e7b9e3

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

Assets/com.alelievr.NodeGraphProcessor/Editor/Views/ExposedParameterView.cs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public class ExposedParameterView : PinnedElementView
1818

1919
readonly string exposedParameterViewStyle = "GraphProcessorStyles/ExposedParameterView";
2020

21+
List<Rect> blackboardLayouts = new List<Rect>();
22+
2123
public ExposedParameterView()
2224
{
2325
var style = Resources.Load<StyleSheet>(exposedParameterViewStyle);
@@ -96,6 +98,11 @@ protected override void Initialize(BaseGraphView graphView)
9698

9799
graphView.onExposedParameterListChanged += UpdateParameterList;
98100
graphView.initialized += UpdateParameterList;
101+
Undo.undoRedoPerformed += UpdateParameterList;
102+
103+
RegisterCallback<DragUpdatedEvent>(OnDragUpdatedEvent);
104+
RegisterCallback<DragPerformEvent>(OnDragPerformEvent);
105+
RegisterCallback<MouseDownEvent>(OnMouseDownEvent, TrickleDown.TrickleDown);
99106

100107
UpdateParameterList();
101108

@@ -104,5 +111,85 @@ protected override void Initialize(BaseGraphView graphView)
104111
text = "+"
105112
});
106113
}
114+
115+
void OnMouseDownEvent(MouseDownEvent evt)
116+
{
117+
blackboardLayouts = content.Children().Select(c => c.layout).ToList();
118+
}
119+
120+
int GetInsertIndexFromMousePosition(Vector2 pos)
121+
{
122+
pos = content.WorldToLocal(pos);
123+
// We only need to look for y axis;
124+
float mousePos = pos.y;
125+
126+
if (mousePos < 0)
127+
return 0;
128+
129+
int index = 0;
130+
foreach (var layout in blackboardLayouts)
131+
{
132+
if (mousePos > layout.yMin && mousePos < layout.yMax)
133+
return index + 1;
134+
index++;
135+
}
136+
137+
return content.childCount;
138+
}
139+
140+
void OnDragUpdatedEvent(DragUpdatedEvent evt)
141+
{
142+
DragAndDrop.visualMode = DragAndDropVisualMode.Move;
143+
int newIndex = GetInsertIndexFromMousePosition(evt.mousePosition);
144+
145+
foreach (var obj in DragAndDrop.GetGenericData("DragSelection") as List<ISelectable>)
146+
{
147+
if (obj is ExposedParameterFieldView view)
148+
{
149+
var blackBoardRow = view.parent.parent.parent.parent.parent.parent;
150+
int oldIndex = content.Children().ToList().FindIndex(c => c == blackBoardRow);
151+
// Try to find the blackboard row
152+
content.Remove(blackBoardRow);
153+
154+
if (newIndex > oldIndex)
155+
newIndex--;
156+
157+
content.Insert(newIndex, blackBoardRow);
158+
}
159+
}
160+
}
161+
162+
void OnDragPerformEvent(DragPerformEvent evt)
163+
{
164+
bool updateList = false;
165+
166+
int newIndex = GetInsertIndexFromMousePosition(evt.mousePosition);
167+
foreach (var obj in DragAndDrop.GetGenericData("DragSelection") as List<ISelectable>)
168+
{
169+
if (obj is ExposedParameterFieldView view)
170+
{
171+
if (!updateList)
172+
graphView.RegisterCompleteObjectUndo("Moved parameters");
173+
174+
int oldIndex = graphView.graph.exposedParameters.FindIndex(e => e == view.parameter);
175+
var parameter = graphView.graph.exposedParameters[oldIndex];
176+
graphView.graph.exposedParameters.RemoveAt(oldIndex);
177+
178+
// Patch new index after the remove operation:
179+
if (newIndex > oldIndex)
180+
newIndex--;
181+
182+
graphView.graph.exposedParameters.Insert(newIndex, parameter);
183+
184+
updateList = true;
185+
}
186+
}
187+
188+
if (updateList)
189+
{
190+
evt.StopImmediatePropagation();
191+
UpdateParameterList();
192+
}
193+
}
107194
}
108195
}

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this package will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## [1.0.1]
8+
9+
### Added
10+
- You can now re-order elements inside the blackboard.
11+
712
## [1.0.0]
813

914
### Added

0 commit comments

Comments
 (0)