guiShaderEditor.cpp 26 KB

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