guiShaderEditor.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  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. ColorI color(ColorI::WHITE);
  224. if (mTempConnection->inSocket != NULL)
  225. {
  226. start = mTempConnection->nodeA->localToGlobalCoord(mTempConnection->inSocket->pos) + offset;
  227. color = mTempConnection->inSocket->col;
  228. }
  229. else if (mTempConnection->outSocket != NULL)
  230. {
  231. start = mTempConnection->nodeA->localToGlobalCoord(mTempConnection->outSocket->pos) + offset;
  232. color = mTempConnection->outSocket->col;
  233. }
  234. RectI sockActive(start, Point2I(mNodeSize, mNodeSize));
  235. start += Point2I(mNodeSize / 2, mNodeSize / 2);
  236. drawer->drawThickLine(start, mLastMousePos + offset, color, (F32)mNodeSize / 3);
  237. // draw socket overlay over the top of the line.
  238. sockActive.inset(1, 1);
  239. drawer->drawCircleFill(sockActive, color, mNodeSize / 2);
  240. }
  241. }
  242. // Draw selection rectangle last so it is rendered on top.
  243. if (mMouseDownMode == DragSelecting)
  244. {
  245. RectI b;
  246. getDragRect(b);
  247. b.point += offset;
  248. // Draw outline.
  249. drawer->drawRect(b, ColorI(100, 100, 100, 128));
  250. // Draw fill.
  251. b.inset(1, 1);
  252. drawer->drawRectFill(b, ColorI(150, 150, 150, 128));
  253. }
  254. }
  255. }
  256. bool GuiShaderEditor::onKeyDown(const GuiEvent& event)
  257. {
  258. if (!mActive)
  259. return Parent::onKeyDown(event);
  260. if (!(event.modifier & SI_PRIMARY_CTRL))
  261. {
  262. switch (event.keyCode)
  263. {
  264. case KEY_BACKSPACE:
  265. case KEY_DELETE:
  266. deleteSelection();
  267. return true;
  268. default:
  269. break;
  270. }
  271. }
  272. return false;
  273. }
  274. void GuiShaderEditor::onMouseDown(const GuiEvent& event)
  275. {
  276. if (!mActive)
  277. {
  278. Parent::onMouseDown(event);
  279. return;
  280. }
  281. setFirstResponder();
  282. // lock mouse
  283. mouseLock();
  284. // get mouse pos with our view offset and scale.
  285. mLastMousePos = globalToLocalCoord(event.mousePoint) - mViewOffset;
  286. GuiShaderNode* hitNode = findHitNode(mLastMousePos);
  287. if(findHitSocket(mLastMousePos))
  288. {
  289. // handled in hit socket.
  290. return;
  291. }
  292. else
  293. {
  294. if (event.modifier & SI_SHIFT)
  295. {
  296. startDragRectangle(mLastMousePos);
  297. mDragAddSelection = true;
  298. }
  299. else if (selectionContains(hitNode))
  300. {
  301. if (event.modifier & SI_MULTISELECT)
  302. {
  303. removeSelection(hitNode);
  304. setMouseMode(Selecting);
  305. }
  306. else if (event.modifier & SI_PRIMARY_ALT)
  307. {
  308. startDragClone(mLastMousePos);
  309. }
  310. else
  311. {
  312. startDragMove(mLastMousePos);
  313. }
  314. }
  315. else
  316. {
  317. if (hitNode == NULL)
  318. {
  319. startDragRectangle(mLastMousePos);
  320. mDragAddSelection = false;
  321. }
  322. else if (event.modifier & SI_PRIMARY_ALT && hitNode != NULL)
  323. {
  324. // Alt is down. Start a drag clone.
  325. clearSelection();
  326. addSelection(hitNode);
  327. startDragClone(mLastMousePos);
  328. }
  329. else if (event.modifier & SI_MULTISELECT)
  330. {
  331. addSelection(hitNode);
  332. }
  333. else
  334. {
  335. // Clicked on node. Start move.
  336. clearSelection();
  337. addSelection(hitNode);
  338. startDragMove(mLastMousePos);
  339. }
  340. }
  341. }
  342. }
  343. void GuiShaderEditor::onMouseUp(const GuiEvent& event)
  344. {
  345. if (!mActive)
  346. {
  347. Parent::onMouseUp(event);
  348. return;
  349. }
  350. //unlock the mouse
  351. mouseUnlock();
  352. // Reset Drag Axis Alignment Information
  353. mDragBeginPoint.set(-1, -1);
  354. mDragBeginPoints.clear();
  355. // get mouse pos with our view offset and scale.
  356. mLastMousePos = globalToLocalCoord(event.mousePoint) - mViewOffset;
  357. if (mMouseDownMode == DragConnection)
  358. {
  359. U32 ret = finishConnection(mLastMousePos);
  360. if (ret == 1) // we set the input on mouse up, nodeB is the inputSocket, swap order.
  361. {
  362. NodeConnection* conn = new NodeConnection();
  363. conn->nodeA = mTempConnection->nodeB;
  364. conn->nodeB = mTempConnection->nodeA;
  365. conn->inSocket = mTempConnection->inSocket;
  366. conn->outSocket = mTempConnection->outSocket;
  367. mCurrConnections.push_back(conn);
  368. }
  369. if (ret == 2) // we set the output on mouse up, nodeB is the outputSocket
  370. {
  371. NodeConnection* conn = new NodeConnection();
  372. conn->nodeA = mTempConnection->nodeA;
  373. conn->nodeB = mTempConnection->nodeB;
  374. conn->inSocket = mTempConnection->inSocket;
  375. conn->outSocket = mTempConnection->outSocket;
  376. mCurrConnections.push_back(conn);
  377. }
  378. mTempConnection = NULL;
  379. }
  380. if (mMouseDownMode == DragSelecting)
  381. {
  382. if (!(event.modifier & SI_MULTISELECT) && !mDragAddSelection)
  383. clearSelection();
  384. RectI rect;
  385. getDragRect(rect);
  386. if (rect.extent.x <= 2 && rect.extent.y <= 2)
  387. {
  388. addSelectionAtPoint(rect.point);
  389. }
  390. else
  391. {
  392. Vector< GuiShaderNode* > hits;
  393. findNodesInRect(rect, hits);
  394. for (GuiShaderNode* node : hits)
  395. {
  396. addSelection(node);
  397. }
  398. }
  399. }
  400. //reset the mouse mode
  401. setFirstResponder();
  402. setMouseMode(Selecting);
  403. }
  404. void GuiShaderEditor::onMouseDragged(const GuiEvent& event)
  405. {
  406. if (!mActive)
  407. {
  408. Parent::onMouseDragged(event);
  409. return;
  410. }
  411. // get mouse pos with our view offset and scale.
  412. Point2I mousePoint = globalToLocalCoord(event.mousePoint) - mViewOffset;
  413. if (mMouseDownMode == DragClone)
  414. {
  415. // If we haven't yet crossed the mouse delta to actually start the
  416. // clone, check if we have now.
  417. S32 delta = mAbs((mousePoint - mDragBeginPoint).len());
  418. if (delta >= 4)
  419. {
  420. cloneSelection();
  421. mLastMousePos = mDragBeginPoint;
  422. mDragMoveUndo = false;
  423. setMouseMode(MovingSelection);
  424. }
  425. }
  426. if (mMouseDownMode == MovingSelection && mSelectedNodes.size())
  427. {
  428. Point2I delta = mousePoint - mLastMousePos;
  429. RectI selBounds = getSelectionBounds();
  430. if (delta.x || delta.y)
  431. moveSelection(delta, mDragMoveUndo);
  432. mLastMousePos += delta;
  433. }
  434. else
  435. mLastMousePos = mousePoint;
  436. }
  437. void GuiShaderEditor::onMiddleMouseDown(const GuiEvent& event)
  438. {
  439. if (!mActive)
  440. {
  441. Parent::onMiddleMouseDown(event);
  442. return;
  443. }
  444. setFirstResponder();
  445. // lock mouse
  446. mouseLock();
  447. // get mouse pos with our view offset and scale.
  448. mLastMousePos = globalToLocalCoord(event.mousePoint);
  449. setMouseMode(DragPanning);
  450. getRoot()->showCursor(false);
  451. }
  452. void GuiShaderEditor::onMiddleMouseUp(const GuiEvent& event)
  453. {
  454. if (!mActive)
  455. {
  456. Parent::onMiddleMouseUp(event);
  457. return;
  458. }
  459. getRoot()->showCursor(true);
  460. //unlock the mouse
  461. mouseUnlock();
  462. // Reset Drag Axis Alignment Information
  463. mDragBeginPoint.set(-1, -1);
  464. mDragBeginPoints.clear();
  465. // get mouse pos with our view offset and scale.
  466. mLastMousePos = globalToLocalCoord(event.mousePoint);
  467. setFirstResponder();
  468. setMouseMode(Selecting);
  469. }
  470. void GuiShaderEditor::onMiddleMouseDragged(const GuiEvent& event)
  471. {
  472. if (!mActive)
  473. {
  474. Parent::onMiddleMouseDragged(event);
  475. return;
  476. }
  477. // get mouse pos with our view offset and scale.
  478. Point2I mousePoint = globalToLocalCoord(event.mousePoint);
  479. if (mMouseDownMode == DragPanning)
  480. {
  481. Point2I delta = mousePoint - mLastMousePos;
  482. // invert it
  483. if (delta.x || delta.y)
  484. mViewOffset += -delta;
  485. mLastMousePos += delta;
  486. }
  487. else
  488. mLastMousePos = mousePoint;
  489. }
  490. bool GuiShaderEditor::onMouseWheelUp(const GuiEvent& event)
  491. {
  492. if (!mActive || !mAwake || !mVisible)
  493. return Parent::onMouseWheelUp(event);
  494. mZoomScale *= 1.1;
  495. return true;
  496. }
  497. bool GuiShaderEditor::onMouseWheelDown(const GuiEvent& event)
  498. {
  499. if (!mActive || !mAwake || !mVisible)
  500. return Parent::onMouseWheelDown(event);
  501. mZoomScale *= 0.9;
  502. return true;
  503. }
  504. RectI GuiShaderEditor::getSelectionBounds()
  505. {
  506. Vector<GuiShaderNode*>::const_iterator i = mSelectedNodes.begin();
  507. Point2I minPos = (*i)->localToGlobalCoord(Point2I(0, 0));
  508. Point2I maxPos = minPos;
  509. for (; i != mSelectedNodes.end(); i++)
  510. {
  511. Point2I iPos = (**i).localToGlobalCoord(Point2I(0, 0));
  512. minPos.x = getMin(iPos.x, minPos.x);
  513. minPos.y = getMin(iPos.y, minPos.y);
  514. Point2I iExt = (**i).getExtent();
  515. iPos.x += iExt.x;
  516. iPos.y += iExt.y;
  517. maxPos.x = getMax(iPos.x, maxPos.x);
  518. maxPos.y = getMax(iPos.y, maxPos.y);
  519. }
  520. minPos = globalToLocalCoord(minPos);
  521. maxPos = globalToLocalCoord(maxPos);
  522. return RectI(minPos.x, minPos.y, (maxPos.x - minPos.x), (maxPos.y - minPos.y));
  523. }
  524. void GuiShaderEditor::deleteSelection()
  525. {
  526. for (GuiShaderNode* node : mSelectedNodes)
  527. {
  528. mTrash->addObject(node);
  529. for (NodeInput* input : node->mInputNodes)
  530. {
  531. NodeConnection* conn;
  532. if (hasConnection(input, conn))
  533. {
  534. // selecting one node, push it to the front of the mcurrnodes stack so its rendered on top.
  535. Vector< NodeConnection* >::iterator i = T3D::find(mCurrConnections.begin(), mCurrConnections.end(), conn);
  536. if (i != mCurrConnections.end())
  537. {
  538. mCurrConnections.erase(i);
  539. }
  540. }
  541. }
  542. for (NodeOutput* output : node->mOutputNodes)
  543. {
  544. NodeConnection* conn;
  545. if (hasConnection(output, conn))
  546. {
  547. // selecting one node, push it to the front of the mcurrnodes stack so its rendered on top.
  548. Vector< NodeConnection* >::iterator i = T3D::find(mCurrConnections.begin(), mCurrConnections.end(), conn);
  549. if (i != mCurrConnections.end())
  550. {
  551. mCurrConnections.erase(i);
  552. }
  553. }
  554. }
  555. Vector< GuiShaderNode* >::iterator i = T3D::find(mCurrNodes.begin(), mCurrNodes.end(), node);
  556. if (i != mCurrNodes.end())
  557. mCurrNodes.erase(i);
  558. }
  559. clearSelection();
  560. }
  561. void GuiShaderEditor::moveSelection(const Point2I& delta, bool callback)
  562. {
  563. for (GuiShaderNode* node : mSelectedNodes)
  564. {
  565. node->setPosition(node->getPosition() + delta);
  566. }
  567. }
  568. void GuiShaderEditor::clearSelection()
  569. {
  570. mSelectedNodes.clear();
  571. }
  572. void GuiShaderEditor::cloneSelection()
  573. {
  574. Vector<GuiShaderNode*> newSelection;
  575. for (GuiShaderNode* node : mSelectedNodes)
  576. {
  577. GuiShaderNode* clone = dynamic_cast<GuiShaderNode*>(node->deepClone());
  578. if (clone)
  579. newSelection.push_back(clone);
  580. }
  581. clearSelection();
  582. for (GuiShaderNode* cloneNode : newSelection)
  583. {
  584. mCurrNodes.push_back(cloneNode);
  585. addSelection(cloneNode);
  586. }
  587. }
  588. void GuiShaderEditor::addSelectionAtPoint(const Point2I& pos)
  589. {
  590. // turn hit off on already selected nodes.
  591. canHitSelectedNodes(false);
  592. GuiShaderNode* node = findHitNode(pos);
  593. // reset hit status.
  594. canHitSelectedNodes();
  595. if (node)
  596. addSelection(node);
  597. }
  598. void GuiShaderEditor::addSelection(GuiShaderNode* inNode)
  599. {
  600. if (inNode != NULL && !selectionContains(inNode))
  601. {
  602. mSelectedNodes.push_back(inNode);
  603. }
  604. }
  605. bool GuiShaderEditor::selectionContains(GuiShaderNode* inNode)
  606. {
  607. for (GuiShaderNode* node : mSelectedNodes)
  608. {
  609. if (node == inNode)
  610. return true;
  611. }
  612. return false;
  613. }
  614. void GuiShaderEditor::removeSelection(GuiShaderNode* inNode)
  615. {
  616. if (selectionContains(inNode))
  617. {
  618. Vector< GuiShaderNode* >::iterator i = T3D::find(mSelectedNodes.begin(), mSelectedNodes.end(), inNode);
  619. if (i != mSelectedNodes.end())
  620. mSelectedNodes.erase(i);
  621. }
  622. }
  623. void GuiShaderEditor::canHitSelectedNodes(bool state)
  624. {
  625. for (GuiShaderNode* node : mSelectedNodes)
  626. node->setCanHit(state);
  627. }
  628. //-----------------------------------------------------------------------------
  629. // Input handling
  630. //-----------------------------------------------------------------------------
  631. GuiShaderNode* GuiShaderEditor::findHitNode(const Point2I& pt)
  632. {
  633. for (GuiShaderNode* node : mCurrNodes)
  634. {
  635. if (node->pointInControl(pt))
  636. {
  637. // selecting one node, push it to the front of the mcurrnodes stack so its rendered on top.
  638. Vector< GuiShaderNode* >::iterator i = T3D::find(mCurrNodes.begin(), mCurrNodes.end(), node);
  639. if (i != mCurrNodes.end())
  640. {
  641. mCurrNodes.erase(i);
  642. mCurrNodes.insert(mCurrNodes.begin(), node);
  643. }
  644. return node;
  645. }
  646. }
  647. return nullptr;
  648. }
  649. bool GuiShaderEditor::findHitSocket(const Point2I& pt)
  650. {
  651. Point2I parentOffset = localToGlobalCoord(getPosition()) + mViewOffset;
  652. Point2I offsetPoint = pt + localToGlobalCoord(getPosition());
  653. for (GuiShaderNode* node : mCurrNodes)
  654. {
  655. for (NodeInput* inNode : node->mInputNodes)
  656. {
  657. Point2I offSet = node->localToGlobalCoord(inNode->pos) + parentOffset;
  658. RectI box(offSet.x, offSet.y, mNodeSize, mNodeSize);
  659. S32 xt = offsetPoint.x - box.point.x;
  660. S32 yt = offsetPoint.y - box.point.y;
  661. if (xt >= 0 && yt >= 0 && xt < box.extent.x && yt < box.extent.y)
  662. {
  663. mTempConnection = new NodeConnection();
  664. mTempConnection->nodeA = node;
  665. mTempConnection->inSocket = inNode;
  666. setMouseMode(DragConnection);
  667. return true;
  668. }
  669. }
  670. for (NodeOutput* outNode : node->mOutputNodes)
  671. {
  672. Point2I offSet = node->localToGlobalCoord(outNode->pos) + parentOffset;
  673. RectI box(offSet.x, offSet.y, mNodeSize, mNodeSize);
  674. S32 xt = offsetPoint.x - box.point.x;
  675. S32 yt = offsetPoint.y - box.point.y;
  676. if (xt >= 0 && yt >= 0 && xt < box.extent.x && yt < box.extent.y)
  677. {
  678. mTempConnection = new NodeConnection();
  679. mTempConnection->nodeA = node;
  680. mTempConnection->outSocket = outNode;
  681. setMouseMode(DragConnection);
  682. return true;
  683. }
  684. }
  685. }
  686. return false;
  687. }
  688. U32 GuiShaderEditor::finishConnection(const Point2I& pt)
  689. {
  690. Point2I parentOffset = localToGlobalCoord(getPosition()) + mViewOffset;
  691. Point2I offsetPoint = pt + localToGlobalCoord(getPosition());
  692. for (GuiShaderNode* node : mCurrNodes)
  693. {
  694. for (NodeInput* inNode : node->mInputNodes)
  695. {
  696. Point2I offSet = node->localToGlobalCoord(inNode->pos) + parentOffset;
  697. RectI box(offSet.x, offSet.y, mNodeSize, mNodeSize);
  698. S32 xt = offsetPoint.x - box.point.x;
  699. S32 yt = offsetPoint.y - box.point.y;
  700. if (xt >= 0 && yt >= 0 && xt < box.extent.x && yt < box.extent.y)
  701. {
  702. if (mTempConnection->nodeA == node || mTempConnection->inSocket != NULL)
  703. return false;
  704. NodeConnection* conn;
  705. if(hasConnection(inNode, conn))
  706. {
  707. // selecting one node, push it to the front of the mcurrnodes stack so its rendered on top.
  708. Vector< NodeConnection* >::iterator i = T3D::find(mCurrConnections.begin(), mCurrConnections.end(), conn);
  709. if (i != mCurrConnections.end())
  710. {
  711. mCurrConnections.erase(i);
  712. }
  713. }
  714. mTempConnection->nodeB = node;
  715. mTempConnection->inSocket = inNode;
  716. return 1;
  717. }
  718. }
  719. for (NodeOutput* outNode : node->mOutputNodes)
  720. {
  721. Point2I offSet = node->localToGlobalCoord(outNode->pos) + parentOffset;
  722. RectI box(offSet.x, offSet.y, mNodeSize, mNodeSize);
  723. S32 xt = offsetPoint.x - box.point.x;
  724. S32 yt = offsetPoint.y - box.point.y;
  725. if (xt >= 0 && yt >= 0 && xt < box.extent.x && yt < box.extent.y)
  726. {
  727. if (mTempConnection->nodeA == node || mTempConnection->outSocket != NULL)
  728. return false;
  729. NodeConnection* conn;
  730. if (hasConnection(mTempConnection->inSocket, conn))
  731. {
  732. // selecting one node, push it to the front of the mcurrnodes stack so its rendered on top.
  733. Vector< NodeConnection* >::iterator i = T3D::find(mCurrConnections.begin(), mCurrConnections.end(), conn);
  734. if (i != mCurrConnections.end())
  735. {
  736. mCurrConnections.erase(i);
  737. }
  738. }
  739. mTempConnection->nodeB = node;
  740. mTempConnection->outSocket = outNode;
  741. return 2;
  742. }
  743. }
  744. }
  745. return 0;
  746. }
  747. bool GuiShaderEditor::hasConnection(NodeSocket* inSocket)
  748. {
  749. for (NodeConnection* con : mCurrConnections)
  750. {
  751. if (con->inSocket == dynamic_cast<NodeInput*>(inSocket) || con->outSocket == dynamic_cast<NodeOutput*>(inSocket))
  752. {
  753. return true;
  754. }
  755. }
  756. return false;
  757. }
  758. bool GuiShaderEditor::hasConnection(NodeSocket* inSocket, NodeConnection*& conn)
  759. {
  760. for (NodeConnection* con : mCurrConnections)
  761. {
  762. if (con->inSocket == dynamic_cast<NodeInput*>(inSocket) || con->outSocket == dynamic_cast<NodeOutput*>(inSocket))
  763. {
  764. if (conn != nullptr)
  765. conn = con;
  766. return true;
  767. }
  768. }
  769. return false;
  770. }
  771. void GuiShaderEditor::findNodesInRect(const RectI& rect, Vector<GuiShaderNode*>& outResult)
  772. {
  773. canHitSelectedNodes(false);
  774. for (GuiShaderNode* node : mCurrNodes)
  775. {
  776. if (node->getBounds().overlaps(rect))
  777. {
  778. outResult.push_back(node);
  779. }
  780. }
  781. canHitSelectedNodes();
  782. }
  783. void GuiShaderEditor::getDragRect(RectI& box)
  784. {
  785. box.point.x = getMin(mLastMousePos.x, mSelectionAnchor.x);
  786. box.extent.x = getMax(mLastMousePos.x, mSelectionAnchor.x) - box.point.x + 1;
  787. box.point.y = getMin(mLastMousePos.y, mSelectionAnchor.y);
  788. box.extent.y = getMax(mLastMousePos.y, mSelectionAnchor.y) - box.point.y + 1;
  789. }
  790. void GuiShaderEditor::startDragMove(const Point2I& startPoint)
  791. {
  792. mDragMoveUndo = true;
  793. mDragBeginPoint = startPoint;
  794. mDragBeginPoints.reserve(mSelectedNodes.size());
  795. for (GuiShaderNode* node : mSelectedNodes)
  796. {
  797. mDragBeginPoints.push_back(node->getPosition());
  798. }
  799. setMouseMode(MovingSelection);
  800. }
  801. void GuiShaderEditor::startDragRectangle(const Point2I& startPoint)
  802. {
  803. mSelectionAnchor = startPoint;
  804. setMouseMode(DragSelecting);
  805. }
  806. void GuiShaderEditor::startDragClone(const Point2I& startPoint)
  807. {
  808. mDragBeginPoint = startPoint;
  809. setMouseMode(DragClone);
  810. }
  811. void GuiShaderEditor::setMouseMode(mouseModes mode)
  812. {
  813. if (mMouseDownMode != mode)
  814. {
  815. mMouseDownMode = mode;
  816. }
  817. }
  818. void GuiShaderEditor::addNode(GuiShaderNode* newNode)
  819. {
  820. mCurrNodes.push_back(newNode);
  821. }