Skip to content

Commit e653942

Browse files
committed
Create nodes with drag and drop.
1 parent 213e8ea commit e653942

File tree

2 files changed

+52
-12
lines changed

2 files changed

+52
-12
lines changed

Diff for: Sources/WindowsAppSupport/WAS_NodeEditorNodeListHwndControl.cpp

+48-12
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ static LRESULT CALLBACK NodeEditorNodeListStaticWindowProc (HWND hwnd, UINT msg,
2222
}
2323

2424
switch (msg) {
25+
case WM_LBUTTONUP:
26+
{
27+
ReleaseCapture ();
28+
int x = GET_X_LPARAM (lParam);
29+
int y = GET_Y_LPARAM (lParam);
30+
control->TreeViewEndDrag (x, y);
31+
}
32+
break;
2533
case WM_NOTIFY:
2634
{
2735
LPNMHDR header = (LPNMHDR) lParam;
@@ -126,6 +134,7 @@ NodeEditorNodeListHwndControl::NodeEditorNodeListHwndControl () :
126134
nodeEditorControl (),
127135
mainHandle (NULL),
128136
selectedNode (-1),
137+
draggedNode (-1),
129138
nextNodeId (0)
130139
{
131140

@@ -199,22 +208,15 @@ void NodeEditorNodeListHwndControl::TreeViewDoubleClick (LPNMHDR lpnmhdr)
199208
return;
200209
}
201210

202-
if (selectedNode == -1) {
203-
return;
204-
}
205-
206-
auto found = nodeIdToCreator.find (selectedNode);
207-
if (DBGERROR (found == nodeIdToCreator.end ())) {
211+
if (selectedNode == (LPARAM) -1) {
208212
return;
209213
}
210214

211215
RECT clientRect;
212216
GetClientRect (nodeEditorControl.GetWindowHandle (), &clientRect);
213-
214-
NUIE::NodeEditor* nodeEditor = nodeEditorControl.GetNodeEditor ();
215217
NUIE::Rect viewRect = NUIE::Rect::FromPositionAndSize (NUIE::Point (0.0, 0.0), NUIE::Size (clientRect.right - clientRect.left, clientRect.bottom - clientRect.top));
216-
NUIE::Point modelPosition = nodeEditor->ViewToModel (viewRect.GetCenter ());
217-
nodeEditor->AddNode (found->second (modelPosition));
218+
NUIE::Point center = viewRect.GetCenter ();
219+
CreateNode (selectedNode, (int) center.GetX (), (int) center.GetY ());
218220
}
219221

220222
void NodeEditorNodeListHwndControl::TreeViewBeginDrag (LPNMTREEVIEW lpnmtv)
@@ -227,9 +229,43 @@ void NodeEditorNodeListHwndControl::TreeViewBeginDrag (LPNMTREEVIEW lpnmtv)
227229
return;
228230
}
229231

230-
HTREEITEM item = lpnmtv->itemOld.hItem;
232+
HTREEITEM item = lpnmtv->itemNew.hItem;
231233
TreeView_SelectItem (lpnmtv->hdr.hwndFrom, item);
232-
(void) item;
234+
SetCapture (mainHandle);
235+
SetCursor (LoadCursor (NULL, IDC_CROSS));
236+
237+
draggedNode = lpnmtv->itemNew.lParam;
238+
}
239+
240+
void NodeEditorNodeListHwndControl::TreeViewEndDrag (int x, int y)
241+
{
242+
if (DBGERROR (draggedNode == (LPARAM) -1)) {
243+
return;
244+
}
245+
246+
RECT editorRect;
247+
GetClientRect (nodeEditorControl.GetWindowHandle (), &editorRect);
248+
MapWindowPoints (nodeEditorControl.GetWindowHandle (), mainHandle, (LPPOINT) &editorRect, 2);
249+
250+
if (x < editorRect.left || x > editorRect.right || y < editorRect.top || y > editorRect.bottom) {
251+
draggedNode = (LPARAM) -1;
252+
return;
253+
}
254+
255+
CreateNode (draggedNode, x - editorRect.left, y - editorRect.top);
256+
draggedNode = (LPARAM) -1;
257+
}
258+
259+
void NodeEditorNodeListHwndControl::CreateNode (LPARAM nodeId, int screenX, int screenY)
260+
{
261+
auto found = nodeIdToCreator.find (nodeId);
262+
if (DBGERROR (found == nodeIdToCreator.end ())) {
263+
return;
264+
}
265+
266+
NUIE::NodeEditor* nodeEditor = nodeEditorControl.GetNodeEditor ();
267+
NUIE::Point modelPosition = nodeEditor->ViewToModel (NUIE::Point (screenX, screenY));
268+
nodeEditor->AddNode (found->second (modelPosition));
233269
}
234270

235271
}

Diff for: Sources/WindowsAppSupport/WAS_NodeEditorNodeListHwndControl.hpp

+4
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,17 @@ class NodeEditorNodeListHwndControl
4747
void TreeViewDoubleClick (LPNMHDR lpnmhdr);
4848
void TreeViewSelectionChanged (LPNMTREEVIEW lpnmtv);
4949
void TreeViewBeginDrag (LPNMTREEVIEW lpnmtv);
50+
void TreeViewEndDrag (int x, int y);
5051

5152
private:
53+
void CreateNode (LPARAM nodeId, int screenX, int screenY);
54+
5255
NodeTreeView nodeTreeView;
5356
NodeEditorHwndControl nodeEditorControl;
5457
HWND mainHandle;
5558

5659
LPARAM selectedNode;
60+
LPARAM draggedNode;
5761
LPARAM nextNodeId;
5862
std::unordered_map<LPARAM, CreatorFunction> nodeIdToCreator;
5963
};

0 commit comments

Comments
 (0)