|
| 1 | +#include "WAS_NodeEditorNodeListHwndControl.hpp" |
| 2 | +#include "WAS_WindowsAppUtils.hpp" |
| 3 | +#include "NE_Debug.hpp" |
| 4 | + |
| 5 | +#include "BI_InputUINodes.hpp" // TODO: remove |
| 6 | + |
| 7 | +namespace WAS |
| 8 | +{ |
| 9 | + |
| 10 | +static const short NodeListWidth = 200; |
| 11 | + |
| 12 | +static LRESULT CALLBACK NodeEditorNodeListStaticWindowProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) |
| 13 | +{ |
| 14 | + if (msg == WM_CREATE) { |
| 15 | + LPCREATESTRUCT createStruct = LPCREATESTRUCT (lParam); |
| 16 | + SetWindowLongPtr (hwnd, GWLP_USERDATA, (LONG_PTR) createStruct->lpCreateParams); |
| 17 | + } |
| 18 | + |
| 19 | + NodeEditorNodeListHwndControl* control = (NodeEditorNodeListHwndControl*) GetWindowLongPtr (hwnd, GWLP_USERDATA); |
| 20 | + if (control == nullptr) { |
| 21 | + return DefWindowProc (hwnd, msg, wParam, lParam); |
| 22 | + } |
| 23 | + |
| 24 | + switch (msg) { |
| 25 | + case WM_NOTIFY: |
| 26 | + { |
| 27 | + LPNMHDR header = (LPNMHDR) lParam; |
| 28 | + switch (header->code) { |
| 29 | + case TVN_BEGINDRAG: |
| 30 | + control->TreeViewBeginDrag ((LPNMTREEVIEW) lParam); |
| 31 | + break; |
| 32 | + case TVN_SELCHANGED: |
| 33 | + control->TreeViewSelectionChanged ((LPNMTREEVIEW) lParam); |
| 34 | + break; |
| 35 | + case NM_DBLCLK: |
| 36 | + control->TreeViewDoubleClick (header); |
| 37 | + break; |
| 38 | + } |
| 39 | + } |
| 40 | + break; |
| 41 | + } |
| 42 | + |
| 43 | + return DefWindowProc (hwnd, msg, wParam, lParam); |
| 44 | +} |
| 45 | + |
| 46 | +NodeTreeView::NodeTreeView () : |
| 47 | + hwnd (NULL) |
| 48 | +{ |
| 49 | + |
| 50 | +} |
| 51 | + |
| 52 | +NodeTreeView::~NodeTreeView () |
| 53 | +{ |
| 54 | + |
| 55 | +} |
| 56 | + |
| 57 | +bool NodeTreeView::Init (HWND parentHandle, int x, int y, int width, int height) |
| 58 | +{ |
| 59 | + hwnd = CreateWindowEx ( |
| 60 | + 0, WC_TREEVIEW, NULL, WS_VISIBLE | WS_CHILD | TVS_HASBUTTONS | TVS_HASLINES | TVS_LINESATROOT | TVS_SHOWSELALWAYS, |
| 61 | + x, y, width, height, parentHandle, NULL, NULL, NULL |
| 62 | + ); |
| 63 | + if (DBGERROR (hwnd == NULL)) { |
| 64 | + return false; |
| 65 | + } |
| 66 | + return true; |
| 67 | +} |
| 68 | + |
| 69 | +bool NodeTreeView::HasGroup (const std::wstring& group) const |
| 70 | +{ |
| 71 | + return groups.find (group) != groups.end (); |
| 72 | +} |
| 73 | + |
| 74 | +void NodeTreeView::AddGroup (const std::wstring& group) |
| 75 | +{ |
| 76 | + auto found = groups.find (group); |
| 77 | + if (DBGERROR (found != groups.end ())) { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + TVINSERTSTRUCT tvInsertStruct; |
| 82 | + memset (&tvInsertStruct, 0, sizeof (TVINSERTSTRUCT)); |
| 83 | + tvInsertStruct.hParent = NULL; |
| 84 | + tvInsertStruct.hInsertAfter = NULL; |
| 85 | + tvInsertStruct.item.mask = TVIF_TEXT | TVIF_PARAM; |
| 86 | + tvInsertStruct.item.pszText = (LPWSTR) group.c_str (); |
| 87 | + tvInsertStruct.item.cchTextMax = sizeof (tvInsertStruct.item.pszText) / sizeof (wchar_t); |
| 88 | + tvInsertStruct.item.lParam = (LPARAM) -1; |
| 89 | + |
| 90 | + HTREEITEM groupItem = (HTREEITEM) SendMessage (hwnd, TVM_INSERTITEM, 0, (LPARAM) &tvInsertStruct); |
| 91 | + groups.insert ({ group, groupItem }); |
| 92 | +} |
| 93 | + |
| 94 | +void NodeTreeView::AddItem (const std::wstring& group, const std::wstring& text, LPARAM lParam) |
| 95 | +{ |
| 96 | + auto found = groups.find (group); |
| 97 | + if (DBGERROR (found == groups.end ())) { |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + TVINSERTSTRUCT tvInsertStruct; |
| 102 | + memset (&tvInsertStruct, 0, sizeof (TVINSERTSTRUCT)); |
| 103 | + tvInsertStruct.hParent = found->second; |
| 104 | + tvInsertStruct.hInsertAfter = NULL; |
| 105 | + tvInsertStruct.item.mask = TVIF_TEXT | TVIF_PARAM; |
| 106 | + tvInsertStruct.item.pszText = (LPWSTR) text.c_str (); |
| 107 | + tvInsertStruct.item.cchTextMax = sizeof (tvInsertStruct.item.pszText) / sizeof (wchar_t); |
| 108 | + tvInsertStruct.item.lParam = lParam; |
| 109 | + SendMessage (hwnd, TVM_INSERTITEM, 0, (LPARAM) &tvInsertStruct); |
| 110 | +} |
| 111 | + |
| 112 | +void NodeTreeView::ExpandAll () |
| 113 | +{ |
| 114 | + for (const auto& it : groups) { |
| 115 | + TreeView_Expand (hwnd, it.second, TVM_EXPAND); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +HWND NodeTreeView::GetWindowHandle () |
| 120 | +{ |
| 121 | + return hwnd; |
| 122 | +} |
| 123 | + |
| 124 | +NodeEditorNodeListHwndControl::NodeEditorNodeListHwndControl () : |
| 125 | + nodeTreeView (), |
| 126 | + nodeEditorControl (), |
| 127 | + mainHandle (NULL), |
| 128 | + selectedNode (-1), |
| 129 | + nextNodeId (0) |
| 130 | +{ |
| 131 | + |
| 132 | +} |
| 133 | + |
| 134 | +NodeEditorNodeListHwndControl::~NodeEditorNodeListHwndControl () |
| 135 | +{ |
| 136 | + |
| 137 | +} |
| 138 | + |
| 139 | +bool NodeEditorNodeListHwndControl::Init (NUIE::NodeEditor* nodeEditorPtr, HWND parentHandle, int x, int y, int width, int height) |
| 140 | +{ |
| 141 | + mainHandle = CreateCustomControl (parentHandle, NodeEditorNodeListStaticWindowProc, L"NodeEditorNodeListHwndControl", this); |
| 142 | + if (DBGERROR (mainHandle == NULL)) { |
| 143 | + return false; |
| 144 | + } |
| 145 | + |
| 146 | + MoveWindow (mainHandle, x, y, width, height, TRUE); |
| 147 | + nodeTreeView.Init (mainHandle, 0, 0, NodeListWidth, height); |
| 148 | + nodeEditorControl.Init (nodeEditorPtr, mainHandle, NodeListWidth, 0, width - NodeListWidth, height); |
| 149 | + |
| 150 | + return true; |
| 151 | +} |
| 152 | + |
| 153 | +void NodeEditorNodeListHwndControl::Resize (int x, int y, int width, int height) |
| 154 | +{ |
| 155 | + MoveWindow (mainHandle, x, y, width, height, TRUE); |
| 156 | + nodeEditorControl.Resize (x + NodeListWidth, y, width - NodeListWidth, height); |
| 157 | +} |
| 158 | + |
| 159 | +HWND NodeEditorNodeListHwndControl::GetEditorHandle () |
| 160 | +{ |
| 161 | + return nodeEditorControl.GetWindowHandle (); |
| 162 | +} |
| 163 | + |
| 164 | +NUIE::DrawingContext& NodeEditorNodeListHwndControl::GetEditorContext () |
| 165 | +{ |
| 166 | + return nodeEditorControl.GetDrawingContext (); |
| 167 | +} |
| 168 | + |
| 169 | +void NodeEditorNodeListHwndControl::InvalidateEditor () |
| 170 | +{ |
| 171 | + nodeEditorControl.Invalidate (); |
| 172 | +} |
| 173 | + |
| 174 | +void NodeEditorNodeListHwndControl::RegisterNode (const std::wstring& group, const std::wstring& text, const CreatorFunction& creator) |
| 175 | +{ |
| 176 | + if (!nodeTreeView.HasGroup (group)) { |
| 177 | + nodeTreeView.AddGroup (group); |
| 178 | + } |
| 179 | + |
| 180 | + nodeTreeView.AddItem (group, text, nextNodeId); |
| 181 | + nodeIdToCreator.insert ({ nextNodeId, creator }); |
| 182 | + nextNodeId++; |
| 183 | + |
| 184 | + nodeTreeView.ExpandAll (); |
| 185 | +} |
| 186 | + |
| 187 | +void NodeEditorNodeListHwndControl::TreeViewSelectionChanged (LPNMTREEVIEW lpnmtv) |
| 188 | +{ |
| 189 | + if (lpnmtv->hdr.hwndFrom != nodeTreeView.GetWindowHandle ()) { |
| 190 | + return; |
| 191 | + } |
| 192 | + |
| 193 | + selectedNode = lpnmtv->itemNew.lParam; |
| 194 | +} |
| 195 | + |
| 196 | +void NodeEditorNodeListHwndControl::TreeViewDoubleClick (LPNMHDR lpnmhdr) |
| 197 | +{ |
| 198 | + if (lpnmhdr->hwndFrom != nodeTreeView.GetWindowHandle ()) { |
| 199 | + return; |
| 200 | + } |
| 201 | + |
| 202 | + if (selectedNode == -1) { |
| 203 | + return; |
| 204 | + } |
| 205 | + |
| 206 | + auto found = nodeIdToCreator.find (selectedNode); |
| 207 | + if (DBGERROR (found == nodeIdToCreator.end ())) { |
| 208 | + return; |
| 209 | + } |
| 210 | + |
| 211 | + RECT clientRect; |
| 212 | + GetClientRect (nodeEditorControl.GetWindowHandle (), &clientRect); |
| 213 | + |
| 214 | + NUIE::NodeEditor* nodeEditor = nodeEditorControl.GetNodeEditor (); |
| 215 | + 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 | +} |
| 219 | + |
| 220 | +void NodeEditorNodeListHwndControl::TreeViewBeginDrag (LPNMTREEVIEW lpnmtv) |
| 221 | +{ |
| 222 | + if (lpnmtv->hdr.hwndFrom != nodeTreeView.GetWindowHandle ()) { |
| 223 | + return; |
| 224 | + } |
| 225 | + |
| 226 | + if (lpnmtv->itemNew.lParam == (LPARAM) -1) { |
| 227 | + return; |
| 228 | + } |
| 229 | + |
| 230 | + HTREEITEM item = lpnmtv->itemOld.hItem; |
| 231 | + TreeView_SelectItem (lpnmtv->hdr.hwndFrom, item); |
| 232 | + (void) item; |
| 233 | +} |
| 234 | + |
| 235 | +} |
0 commit comments