guiShaderEditor.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "gui/shaderEditor/guiShaderEditor.h"
  24. #include "core/frameAllocator.h"
  25. #include "core/stream/fileStream.h"
  26. #include "core/stream/memStream.h"
  27. #include "console/consoleTypes.h"
  28. #include "gui/core/guiCanvas.h"
  29. #include "console/engineAPI.h"
  30. #include "console/script.h"
  31. #include "console/typeValidators.h"
  32. #include "gfx/primBuilder.h"
  33. IMPLEMENT_CONOBJECT(GuiShaderEditor);
  34. ConsoleDocClass(GuiShaderEditor,
  35. "@brief Implementation of a shader node editor.\n\n"
  36. "Editor use only.\n\n"
  37. "@internal"
  38. );
  39. GuiShaderEditor::GuiShaderEditor()
  40. : mDragBeginPoint(-1, -1),
  41. mViewOffset(0,0),
  42. mZoomScale(1.0f),
  43. mFullBoxSelection(false),
  44. mDragAddSelection(false),
  45. mDragMoveUndo(false)
  46. {
  47. VECTOR_SET_ASSOCIATION(mCurrNodes);
  48. VECTOR_SET_ASSOCIATION(mSelectedNodes);
  49. VECTOR_SET_ASSOCIATION(mDragBeginPoints);
  50. VECTOR_SET_ASSOCIATION(mCurrConnections);
  51. mActive = true;
  52. mMouseDownMode = GuiShaderEditor::Selecting;
  53. mTrash = NULL;
  54. mTempConnection = NULL;
  55. mNodeSize = 10;
  56. // test
  57. addNode(new GuiShaderNode());
  58. addNode(new GuiShaderNode());
  59. }
  60. bool GuiShaderEditor::onWake()
  61. {
  62. if (!Parent::onWake())
  63. return false;
  64. return true;
  65. }
  66. void GuiShaderEditor::onSleep()
  67. {
  68. Parent::onSleep();
  69. }
  70. // anything smaller than 4 is way too small....
  71. IRangeValidator nodeSizeRange(4, 30);
  72. void GuiShaderEditor::initPersistFields()
  73. {
  74. docsURL;
  75. addGroup("Node Settings");
  76. addFieldV("nodeSize", TypeS32, Offset(mNodeSize, GuiShaderEditor),&nodeSizeRange,
  77. "Size of nodes.");
  78. endGroup("Node Settings");
  79. addGroup("Selection");
  80. addField("fullBoxSelection", TypeBool, Offset(mFullBoxSelection, GuiShaderEditor),
  81. "If true, rectangle selection will only select controls fully inside the drag rectangle.");
  82. endGroup("Selection");
  83. Parent::initPersistFields();
  84. }
  85. bool GuiShaderEditor::onAdd()
  86. {
  87. if (!Parent::onAdd())
  88. return false;
  89. mTrash = new SimGroup();
  90. if (!mTrash->registerObject())
  91. return false;
  92. return true;
  93. }
  94. void GuiShaderEditor::onRemove()
  95. {
  96. Parent::onRemove();
  97. mTrash->deleteObject();
  98. mTrash = NULL;
  99. /* for (GuiShaderNode* node : mCurrNodes)
  100. {
  101. SAFE_DELETE(node);
  102. }*/
  103. for (GuiShaderNode* node : mSelectedNodes)
  104. {
  105. SAFE_DELETE(node);
  106. }
  107. }
  108. void GuiShaderEditor::onPreRender()
  109. {
  110. setUpdate();
  111. }
  112. void GuiShaderEditor::renderNodes(Point2I offset, const RectI& updateRect)
  113. {
  114. // Save the current clip rect
  115. // so we can restore it at the end of this method.
  116. RectI savedClipRect = GFX->getClipRect();
  117. // offset is the upper-left corner of this control in screen coordinates
  118. // updateRect is the intersection rectangle in screen coords of the control
  119. // hierarchy. This can be set as the clip rectangle in most cases.
  120. RectI clipRect = updateRect;
  121. clipRect.inset(2, 2);
  122. GFXDrawUtil* drawer = GFX->getDrawUtil();
  123. // render nodes in reverse order.
  124. for (ShaderNodeVector::iterator i = mCurrNodes.end(); i-- != mCurrNodes.begin(); )
  125. {
  126. GuiShaderNode* node = *i;
  127. // this is useful for sub node graphs.
  128. if (node->isVisible())
  129. {
  130. Point2I childPos = offset + node->getPosition();
  131. RectI childClip(childPos, node->getExtent() );
  132. if (selectionContains(node))
  133. {
  134. node->mSelected = true;
  135. }
  136. else
  137. {
  138. node->mSelected = false;
  139. }
  140. if (childClip.intersect(clipRect))
  141. {
  142. GFX->setClipRect(childClip);
  143. GFX->setStateBlock(mDefaultGuiSB);
  144. node->renderNode(childPos, childClip, mNodeSize);
  145. }
  146. GFX->setClipRect(clipRect);
  147. GFX->setStateBlock(mDefaultGuiSB);
  148. for (NodeInput* input : node->mInputNodes)
  149. {
  150. Point2I pos = node->localToGlobalCoord(input->pos) + offset;
  151. ColorI border = mProfile->mBorderColor;
  152. if (node->mSelected)
  153. border = mProfile->mBorderColorSEL;
  154. ColorI fill = mProfile->mFillColor;
  155. if (hasConnection(input))
  156. {
  157. fill = ColorI::WHITE;
  158. }
  159. RectI socketRect(pos, Point2I(mNodeSize, mNodeSize));
  160. drawer->drawCircleFill(socketRect, fill, mNodeSize / 2, 2.0f, border);
  161. }
  162. for (NodeOutput* output : node->mOutputNodes)
  163. {
  164. Point2I pos = node->localToGlobalCoord(output->pos) + offset;
  165. ColorI border = mProfile->mBorderColor;
  166. if (node->mSelected)
  167. border = mProfile->mBorderColorSEL;
  168. if(node->mSelected)
  169. border = mProfile->mBorderColorSEL;
  170. ColorI fill = mProfile->mFillColor;
  171. if (hasConnection(output))
  172. {
  173. fill = ColorI::WHITE;
  174. }
  175. RectI socketRect(pos, Point2I(mNodeSize, mNodeSize));
  176. drawer->drawCircleFill(socketRect, fill, mNodeSize / 2, 2.0f, border);
  177. }
  178. }
  179. }
  180. // Restore the clip rect to what it was at the start
  181. // of this method.
  182. GFX->setClipRect(savedClipRect);
  183. }
  184. void GuiShaderEditor::renderConnections(Point2I offset, const RectI& updateRect)
  185. {
  186. // Save the current clip rect
  187. // so we can restore it at the end of this method.
  188. RectI savedClipRect = GFX->getClipRect();
  189. // offset is the upper-left corner of this control in screen coordinates
  190. // updateRect is the intersection rectangle in screen coords of the control
  191. // hierarchy. This can be set as the clip rectangle in most cases.
  192. RectI clipRect = updateRect;
  193. clipRect.inset(2, 2);
  194. GFXDrawUtil* drawer = GFX->getDrawUtil();
  195. for (NodeConnection* conn : mCurrConnections)
  196. {
  197. // for temp connetion, nodeA is always the first node selected.
  198. Point2I start(Point2I::Zero);
  199. Point2I end(Point2I::Zero);
  200. start = conn->nodeA->localToGlobalCoord(conn->inSocket->pos) + offset;
  201. end = conn->nodeB->localToGlobalCoord(conn->outSocket->pos) + offset;
  202. start += Point2I(mNodeSize / 2, mNodeSize / 2);
  203. end += Point2I(mNodeSize / 2, mNodeSize / 2);
  204. drawer->drawThickLine(start, end,ColorI(255,255,255,255), (F32)mNodeSize/3);
  205. }
  206. // Restore the clip rect to what it was at the start
  207. // of this method.
  208. GFX->setClipRect(savedClipRect);
  209. }
  210. void GuiShaderEditor::onRender(Point2I offset, const RectI& updateRect)
  211. {
  212. offset += mViewOffset;
  213. GFXDrawUtil* drawer = GFX->getDrawUtil();
  214. // render our nodes.
  215. renderConnections(offset, updateRect);
  216. renderNodes(offset, updateRect);
  217. if (mActive)
  218. {
  219. if (mMouseDownMode == DragConnection)
  220. {
  221. // something went wrong.... fix it fix it fix it.
  222. if (mTempConnection == NULL)
  223. return;
  224. else
  225. {
  226. // for temp connetion, nodeA is always the first node selected.
  227. Point2I start(Point2I::Zero);
  228. if (mTempConnection->inSocket != NULL)
  229. start = mTempConnection->nodeA->localToGlobalCoord(mTempConnection->inSocket->pos) + offset;
  230. else if(mTempConnection->outSocket != NULL)
  231. start = mTempConnection->nodeA->localToGlobalCoord(mTempConnection->outSocket->pos) + offset;
  232. RectI sockActive(start, Point2I(mNodeSize, mNodeSize));
  233. start += Point2I(mNodeSize / 2, mNodeSize / 2);
  234. drawer->drawThickLine(start, mLastMousePos + offset, ColorI(255, 255, 255, 255), (F32)mNodeSize / 3);
  235. // draw socket overlay over the top of the line.
  236. sockActive.inset(1, 1);
  237. drawer->drawCircleFill(sockActive, ColorI(255, 255, 255), mNodeSize / 2);
  238. }
  239. }
  240. // Draw selection rectangle last so it is rendered on top.
  241. if (mMouseDownMode == DragSelecting)
  242. {
  243. RectI b;
  244. getDragRect(b);
  245. b.point += offset;
  246. // Draw outline.
  247. drawer->drawRect(b, ColorI(100, 100, 100, 128));
  248. // Draw fill.
  249. b.inset(1, 1);
  250. drawer->drawRectFill(b, ColorI(150, 150, 150, 128));
  251. }
  252. }
  253. }
  254. bool GuiShaderEditor::onKeyDown(const GuiEvent& event)
  255. {
  256. if (!mActive)
  257. return Parent::onKeyDown(event);
  258. if (!(event.modifier & SI_PRIMARY_CTRL))
  259. {
  260. switch (event.keyCode)
  261. {
  262. case KEY_BACKSPACE:
  263. case KEY_DELETE:
  264. deleteSelection();
  265. return true;
  266. default:
  267. break;
  268. }
  269. }
  270. return false;
  271. }
  272. void GuiShaderEditor::onMouseDown(const GuiEvent& event)
  273. {
  274. if (!mActive)
  275. {
  276. Parent::onMouseDown(event);
  277. return;
  278. }
  279. setFirstResponder();
  280. // lock mouse
  281. mouseLock();
  282. // get mouse pos with our view offset and scale.
  283. mLastMousePos = globalToLocalCoord(event.mousePoint) - mViewOffset;
  284. GuiShaderNode* hitNode = findHitNode(mLastMousePos);
  285. if(findHitSocket(mLastMousePos))
  286. {
  287. // handled in hit socket.
  288. return;
  289. }
  290. else
  291. {
  292. if (event.modifier & SI_SHIFT)
  293. {
  294. startDragRectangle(mLastMousePos);
  295. mDragAddSelection = true;
  296. }
  297. else if (selectionContains(hitNode))
  298. {
  299. if (event.modifier & SI_MULTISELECT)
  300. {
  301. removeSelection(hitNode);
  302. setMouseMode(Selecting);
  303. }
  304. else if (event.modifier & SI_PRIMARY_ALT)
  305. {
  306. startDragClone(mLastMousePos);
  307. }
  308. else
  309. {
  310. startDragMove(mLastMousePos);
  311. }
  312. }
  313. else
  314. {
  315. if (hitNode == NULL)
  316. {
  317. startDragRectangle(mLastMousePos);
  318. mDragAddSelection = false;
  319. }
  320. else if (event.modifier & SI_PRIMARY_ALT && hitNode != NULL)
  321. {
  322. // Alt is down. Start a drag clone.
  323. clearSelection();
  324. addSelection(hitNode);
  325. startDragClone(mLastMousePos);
  326. }
  327. else if (event.modifier & SI_MULTISELECT)
  328. {
  329. addSelection(hitNode);
  330. }
  331. else
  332. {
  333. // Clicked on node. Start move.
  334. clearSelection();
  335. addSelection(hitNode);
  336. startDragMove(mLastMousePos);
  337. }
  338. }
  339. }
  340. }
  341. void GuiShaderEditor::onMouseUp(const GuiEvent& event)
  342. {
  343. if (!mActive)
  344. {
  345. Parent::onMouseUp(event);
  346. return;
  347. }
  348. //unlock the mouse
  349. mouseUnlock();
  350. // Reset Drag Axis Alignment Information
  351. mDragBeginPoint.set(-1, -1);
  352. mDragBeginPoints.clear();
  353. // get mouse pos with our view offset and scale.
  354. mLastMousePos = globalToLocalCoord(event.mousePoint) - mViewOffset;
  355. if (mMouseDownMode == DragConnection)
  356. {
  357. U32 ret = finishConnection(mLastMousePos);
  358. if (ret == 1) // we set the input on mouse up, nodeB is the inputSocket, swap order.
  359. {
  360. NodeConnection* conn = new NodeConnection();
  361. conn->nodeA = mTempConnection->nodeB;
  362. conn->nodeB = mTempConnection->nodeA;
  363. conn->inSocket = mTempConnection->inSocket;
  364. conn->outSocket = mTempConnection->outSocket;
  365. mCurrConnections.push_back(conn);
  366. }
  367. if (ret == 2) // we set the output on mouse up, nodeB is the outputSocket
  368. {
  369. NodeConnection* conn = new NodeConnection();
  370. conn->nodeA = mTempConnection->nodeA;
  371. conn->nodeB = mTempConnection->nodeB;
  372. conn->inSocket = mTempConnection->inSocket;
  373. conn->outSocket = mTempConnection->outSocket;
  374. mCurrConnections.push_back(conn);
  375. }
  376. mTempConnection = NULL;
  377. }
  378. if (mMouseDownMode == DragSelecting)
  379. {
  380. if (!(event.modifier & SI_MULTISELECT) && !mDragAddSelection)
  381. clearSelection();
  382. RectI rect;
  383. getDragRect(rect);
  384. if (rect.extent.x <= 2 && rect.extent.y <= 2)
  385. {
  386. addSelectionAtPoint(rect.point);
  387. }
  388. else
  389. {
  390. Vector< GuiShaderNode* > hits;
  391. findNodesInRect(rect, hits);
  392. for (GuiShaderNode* node : hits)
  393. {
  394. addSelection(node);
  395. }
  396. }
  397. }
  398. //reset the mouse mode
  399. setFirstResponder();
  400. setMouseMode(Selecting);
  401. }
  402. void GuiShaderEditor::onMouseDragged(const GuiEvent& event)
  403. {
  404. if (!mActive)
  405. {
  406. Parent::onMouseDragged(event);
  407. return;
  408. }
  409. // get mouse pos with our view offset and scale.
  410. Point2I mousePoint = globalToLocalCoord(event.mousePoint) - mViewOffset;
  411. if (mMouseDownMode == DragClone)
  412. {
  413. // If we haven't yet crossed the mouse delta to actually start the
  414. // clone, check if we have now.
  415. S32 delta = mAbs((mousePoint - mDragBeginPoint).len());
  416. if (delta >= 4)
  417. {
  418. cloneSelection();
  419. mLastMousePos = mDragBeginPoint;
  420. mDragMoveUndo = false;
  421. setMouseMode(MovingSelection);
  422. }
  423. }
  424. if (mMouseDownMode == MovingSelection && mSelectedNodes.size())
  425. {
  426. Point2I delta = mousePoint - mLastMousePos;
  427. RectI selBounds = getSelectionBounds();
  428. if (delta.x || delta.y)
  429. moveSelection(delta, mDragMoveUndo);
  430. mLastMousePos += delta;
  431. }
  432. else
  433. mLastMousePos = mousePoint;
  434. }
  435. void GuiShaderEditor::onMiddleMouseDown(const GuiEvent& event)
  436. {
  437. if (!mActive)
  438. {
  439. Parent::onMiddleMouseDown(event);
  440. return;
  441. }
  442. setFirstResponder();
  443. // lock mouse
  444. mouseLock();
  445. // get mouse pos with our view offset and scale.
  446. mLastMousePos = globalToLocalCoord(event.mousePoint);
  447. setMouseMode(DragPanning);
  448. getRoot()->showCursor(false);
  449. }
  450. void GuiShaderEditor::onMiddleMouseUp(const GuiEvent& event)
  451. {
  452. if (!mActive)
  453. {
  454. Parent::onMiddleMouseUp(event);
  455. return;
  456. }
  457. getRoot()->showCursor(true);
  458. //unlock the mouse
  459. mouseUnlock();
  460. // Reset Drag Axis Alignment Information
  461. mDragBeginPoint.set(-1, -1);
  462. mDragBeginPoints.clear();
  463. // get mouse pos with our view offset and scale.
  464. mLastMousePos = globalToLocalCoord(event.mousePoint);
  465. setFirstResponder();
  466. setMouseMode(Selecting);
  467. }
  468. void GuiShaderEditor::onMiddleMouseDragged(const GuiEvent& event)
  469. {
  470. if (!mActive)
  471. {
  472. Parent::onMiddleMouseDragged(event);
  473. return;
  474. }
  475. // get mouse pos with our view offset and scale.
  476. Point2I mousePoint = globalToLocalCoord(event.mousePoint);
  477. if (mMouseDownMode == DragPanning)
  478. {
  479. Point2I delta = mousePoint - mLastMousePos;
  480. // invert it
  481. if (delta.x || delta.y)
  482. mViewOffset += -delta;
  483. mLastMousePos += delta;
  484. }
  485. else
  486. mLastMousePos = mousePoint;
  487. }
  488. bool GuiShaderEditor::onMouseWheelUp(const GuiEvent& event)
  489. {
  490. if (!mActive || !mAwake || !mVisible)
  491. return Parent::onMouseWheelUp(event);
  492. mZoomScale *= 1.1;
  493. return true;
  494. }
  495. bool GuiShaderEditor::onMouseWheelDown(const GuiEvent& event)
  496. {
  497. if (!mActive || !mAwake || !mVisible)
  498. return Parent::onMouseWheelDown(event);
  499. mZoomScale *= 0.9;
  500. return true;
  501. }
  502. RectI GuiShaderEditor::getSelectionBounds()
  503. {
  504. Vector<GuiShaderNode*>::const_iterator i = mSelectedNodes.begin();
  505. Point2I minPos = (*i)->localToGlobalCoord(Point2I(0, 0));
  506. Point2I maxPos = minPos;
  507. for (; i != mSelectedNodes.end(); i++)
  508. {
  509. Point2I iPos = (**i).localToGlobalCoord(Point2I(0, 0));
  510. minPos.x = getMin(iPos.x, minPos.x);
  511. minPos.y = getMin(iPos.y, minPos.y);
  512. Point2I iExt = (**i).getExtent();
  513. iPos.x += iExt.x;
  514. iPos.y += iExt.y;
  515. maxPos.x = getMax(iPos.x, maxPos.x);
  516. maxPos.y = getMax(iPos.y, maxPos.y);
  517. }
  518. minPos = globalToLocalCoord(minPos);
  519. maxPos = globalToLocalCoord(maxPos);
  520. return RectI(minPos.x, minPos.y, (maxPos.x - minPos.x), (maxPos.y - minPos.y));
  521. }
  522. void GuiShaderEditor::deleteSelection()
  523. {
  524. for (GuiShaderNode* node : mSelectedNodes)
  525. {
  526. mTrash->addObject(node);
  527. for (NodeInput* input : node->mInputNodes)
  528. {
  529. NodeConnection* conn;
  530. if (hasConnection(input, conn))
  531. {
  532. // selecting one node, push it to the front of the mcurrnodes stack so its rendered on top.
  533. Vector< NodeConnection* >::iterator i = T3D::find(mCurrConnections.begin(), mCurrConnections.end(), conn);
  534. if (i != mCurrConnections.end())
  535. {
  536. mCurrConnections.erase(i);
  537. }
  538. }
  539. }
  540. for (NodeOutput* output : node->mOutputNodes)
  541. {
  542. NodeConnection* conn;
  543. if (hasConnection(output, conn))
  544. {
  545. // selecting one node, push it to the front of the mcurrnodes stack so its rendered on top.
  546. Vector< NodeConnection* >::iterator i = T3D::find(mCurrConnections.begin(), mCurrConnections.end(), conn);
  547. if (i != mCurrConnections.end())
  548. {
  549. mCurrConnections.erase(i);
  550. }
  551. }
  552. }
  553. Vector< GuiShaderNode* >::iterator i = T3D::find(mCurrNodes.begin(), mCurrNodes.end(), node);
  554. if (i != mCurrNodes.end())
  555. mCurrNodes.erase(i);
  556. }
  557. clearSelection();
  558. }
  559. void GuiShaderEditor::moveSelection(const Point2I& delta, bool callback)
  560. {
  561. for (GuiShaderNode* node : mSelectedNodes)
  562. {
  563. node->setPosition(node->getPosition() + delta);
  564. }
  565. }
  566. void GuiShaderEditor::clearSelection()
  567. {
  568. mSelectedNodes.clear();
  569. }
  570. void GuiShaderEditor::cloneSelection()
  571. {
  572. Vector<GuiShaderNode*> newSelection;
  573. for (GuiShaderNode* node : mSelectedNodes)
  574. {
  575. GuiShaderNode* clone = dynamic_cast<GuiShaderNode*>(node->deepClone());
  576. if (clone)
  577. newSelection.push_back(clone);
  578. }
  579. clearSelection();
  580. for (GuiShaderNode* cloneNode : newSelection)
  581. {
  582. mCurrNodes.push_back(cloneNode);
  583. addSelection(cloneNode);
  584. }
  585. }
  586. void GuiShaderEditor::addSelectionAtPoint(const Point2I& pos)
  587. {
  588. // turn hit off on already selected nodes.
  589. canHitSelectedNodes(false);
  590. GuiShaderNode* node = findHitNode(pos);
  591. // reset hit status.
  592. canHitSelectedNodes();
  593. if (node)
  594. addSelection(node);
  595. }
  596. void GuiShaderEditor::addSelection(GuiShaderNode* inNode)
  597. {
  598. if (inNode != NULL && !selectionContains(inNode))
  599. {
  600. mSelectedNodes.push_back(inNode);
  601. }
  602. }
  603. bool GuiShaderEditor::selectionContains(GuiShaderNode* inNode)
  604. {
  605. for (GuiShaderNode* node : mSelectedNodes)
  606. {
  607. if (node == inNode)
  608. return true;
  609. }
  610. return false;
  611. }
  612. void GuiShaderEditor::removeSelection(GuiShaderNode* inNode)
  613. {
  614. if (selectionContains(inNode))
  615. {
  616. Vector< GuiShaderNode* >::iterator i = T3D::find(mSelectedNodes.begin(), mSelectedNodes.end(), inNode);
  617. if (i != mSelectedNodes.end())
  618. mSelectedNodes.erase(i);
  619. }
  620. }
  621. void GuiShaderEditor::canHitSelectedNodes(bool state)
  622. {
  623. for (GuiShaderNode* node : mSelectedNodes)
  624. node->setCanHit(state);
  625. }
  626. //-----------------------------------------------------------------------------
  627. // Input handling
  628. //-----------------------------------------------------------------------------
  629. GuiShaderNode* GuiShaderEditor::findHitNode(const Point2I& pt)
  630. {
  631. for (GuiShaderNode* node : mCurrNodes)
  632. {
  633. if (node->pointInControl(pt))
  634. {
  635. // selecting one node, push it to the front of the mcurrnodes stack so its rendered on top.
  636. Vector< GuiShaderNode* >::iterator i = T3D::find(mCurrNodes.begin(), mCurrNodes.end(), node);
  637. if (i != mCurrNodes.end())
  638. {
  639. mCurrNodes.erase(i);
  640. mCurrNodes.insert(mCurrNodes.begin(), node);
  641. }
  642. return node;
  643. }
  644. }
  645. return nullptr;
  646. }
  647. bool GuiShaderEditor::findHitSocket(const Point2I& pt)
  648. {
  649. Point2I parentOffset = localToGlobalCoord(getPosition()) + mViewOffset;
  650. Point2I offsetPoint = pt + localToGlobalCoord(getPosition());
  651. for (GuiShaderNode* node : mCurrNodes)
  652. {
  653. for (NodeInput* inNode : node->mInputNodes)
  654. {
  655. Point2I offSet = node->localToGlobalCoord(inNode->pos) + parentOffset;
  656. RectI box(offSet.x, offSet.y, mNodeSize, mNodeSize);
  657. S32 xt = offsetPoint.x - box.point.x;
  658. S32 yt = offsetPoint.y - box.point.y;
  659. if (xt >= 0 && yt >= 0 && xt < box.extent.x && yt < box.extent.y)
  660. {
  661. mTempConnection = new NodeConnection();
  662. mTempConnection->nodeA = node;
  663. mTempConnection->inSocket = inNode;
  664. setMouseMode(DragConnection);
  665. return true;
  666. }
  667. }
  668. for (NodeOutput* outNode : node->mOutputNodes)
  669. {
  670. Point2I offSet = node->localToGlobalCoord(outNode->pos) + parentOffset;
  671. RectI box(offSet.x, offSet.y, mNodeSize, mNodeSize);
  672. S32 xt = offsetPoint.x - box.point.x;
  673. S32 yt = offsetPoint.y - box.point.y;
  674. if (xt >= 0 && yt >= 0 && xt < box.extent.x && yt < box.extent.y)
  675. {
  676. mTempConnection = new NodeConnection();
  677. mTempConnection->nodeA = node;
  678. mTempConnection->outSocket = outNode;
  679. setMouseMode(DragConnection);
  680. return true;
  681. }
  682. }
  683. }
  684. return false;
  685. }
  686. U32 GuiShaderEditor::finishConnection(const Point2I& pt)
  687. {
  688. Point2I parentOffset = localToGlobalCoord(getPosition()) + mViewOffset;
  689. Point2I offsetPoint = pt + localToGlobalCoord(getPosition());
  690. for (GuiShaderNode* node : mCurrNodes)
  691. {
  692. for (NodeInput* inNode : node->mInputNodes)
  693. {
  694. Point2I offSet = node->localToGlobalCoord(inNode->pos) + parentOffset;
  695. RectI box(offSet.x, offSet.y, mNodeSize, mNodeSize);
  696. S32 xt = offsetPoint.x - box.point.x;
  697. S32 yt = offsetPoint.y - box.point.y;
  698. if (xt >= 0 && yt >= 0 && xt < box.extent.x && yt < box.extent.y)
  699. {
  700. if (mTempConnection->nodeA == node || mTempConnection->inSocket != NULL)
  701. return false;
  702. NodeConnection* conn;
  703. if(hasConnection(inNode, conn))
  704. {
  705. // selecting one node, push it to the front of the mcurrnodes stack so its rendered on top.
  706. Vector< NodeConnection* >::iterator i = T3D::find(mCurrConnections.begin(), mCurrConnections.end(), conn);
  707. if (i != mCurrConnections.end())
  708. {
  709. mCurrConnections.erase(i);
  710. }
  711. }
  712. mTempConnection->nodeB = node;
  713. mTempConnection->inSocket = inNode;
  714. return 1;
  715. }
  716. }
  717. for (NodeOutput* outNode : node->mOutputNodes)
  718. {
  719. Point2I offSet = node->localToGlobalCoord(outNode->pos) + parentOffset;
  720. RectI box(offSet.x, offSet.y, mNodeSize, mNodeSize);
  721. S32 xt = offsetPoint.x - box.point.x;
  722. S32 yt = offsetPoint.y - box.point.y;
  723. if (xt >= 0 && yt >= 0 && xt < box.extent.x && yt < box.extent.y)
  724. {
  725. if (mTempConnection->nodeA == node || mTempConnection->outSocket != NULL)
  726. return false;
  727. NodeConnection* conn;
  728. if (hasConnection(mTempConnection->inSocket, conn))
  729. {
  730. // selecting one node, push it to the front of the mcurrnodes stack so its rendered on top.
  731. Vector< NodeConnection* >::iterator i = T3D::find(mCurrConnections.begin(), mCurrConnections.end(), conn);
  732. if (i != mCurrConnections.end())
  733. {
  734. mCurrConnections.erase(i);
  735. }
  736. }
  737. mTempConnection->nodeB = node;
  738. mTempConnection->outSocket = outNode;
  739. return 2;
  740. }
  741. }
  742. }
  743. return 0;
  744. }
  745. bool GuiShaderEditor::hasConnection(NodeSocket* inSocket)
  746. {
  747. for (NodeConnection* con : mCurrConnections)
  748. {
  749. if (con->inSocket == dynamic_cast<NodeInput*>(inSocket) || con->outSocket == dynamic_cast<NodeOutput*>(inSocket))
  750. {
  751. return true;
  752. }
  753. }
  754. return false;
  755. }
  756. bool GuiShaderEditor::hasConnection(NodeSocket* inSocket, NodeConnection*& conn)
  757. {
  758. for (NodeConnection* con : mCurrConnections)
  759. {
  760. if (con->inSocket == dynamic_cast<NodeInput*>(inSocket) || con->outSocket == dynamic_cast<NodeOutput*>(inSocket))
  761. {
  762. if (conn != nullptr)
  763. conn = con;
  764. return true;
  765. }
  766. }
  767. return false;
  768. }
  769. void GuiShaderEditor::findNodesInRect(const RectI& rect, Vector<GuiShaderNode*>& outResult)
  770. {
  771. canHitSelectedNodes(false);
  772. for (GuiShaderNode* node : mCurrNodes)
  773. {
  774. if (node->getBounds().overlaps(rect))
  775. {
  776. outResult.push_back(node);
  777. }
  778. }
  779. canHitSelectedNodes();
  780. }
  781. void GuiShaderEditor::getDragRect(RectI& box)
  782. {
  783. box.point.x = getMin(mLastMousePos.x, mSelectionAnchor.x);
  784. box.extent.x = getMax(mLastMousePos.x, mSelectionAnchor.x) - box.point.x + 1;
  785. box.point.y = getMin(mLastMousePos.y, mSelectionAnchor.y);
  786. box.extent.y = getMax(mLastMousePos.y, mSelectionAnchor.y) - box.point.y + 1;
  787. }
  788. void GuiShaderEditor::startDragMove(const Point2I& startPoint)
  789. {
  790. mDragMoveUndo = true;
  791. mDragBeginPoint = startPoint;
  792. mDragBeginPoints.reserve(mSelectedNodes.size());
  793. for (GuiShaderNode* node : mSelectedNodes)
  794. {
  795. mDragBeginPoints.push_back(node->getPosition());
  796. }
  797. setMouseMode(MovingSelection);
  798. }
  799. void GuiShaderEditor::startDragRectangle(const Point2I& startPoint)
  800. {
  801. mSelectionAnchor = startPoint;
  802. setMouseMode(DragSelecting);
  803. }
  804. void GuiShaderEditor::startDragClone(const Point2I& startPoint)
  805. {
  806. mDragBeginPoint = startPoint;
  807. setMouseMode(DragClone);
  808. }
  809. void GuiShaderEditor::setMouseMode(mouseModes mode)
  810. {
  811. if (mMouseDownMode != mode)
  812. {
  813. mMouseDownMode = mode;
  814. }
  815. }
  816. void GuiShaderEditor::addNode(GuiShaderNode* newNode)
  817. {
  818. mCurrNodes.push_back(newNode);
  819. }