guiShaderEditor.cpp 26 KB

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