guiShaderEditor.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "gui/shaderEditor/guiShaderEditor.h"
  24. #include "core/frameAllocator.h"
  25. #include "core/stream/fileStream.h"
  26. #include "core/stream/memStream.h"
  27. #include "console/consoleTypes.h"
  28. #include "gui/core/guiCanvas.h"
  29. #include "console/engineAPI.h"
  30. #include "console/script.h"
  31. #include "console/typeValidators.h"
  32. IMPLEMENT_CONOBJECT(GuiShaderEditor);
  33. ConsoleDocClass(GuiShaderEditor,
  34. "@brief Implementation of a shader node editor.\n\n"
  35. "Editor use only.\n\n"
  36. "@internal"
  37. );
  38. GuiShaderEditor::GuiShaderEditor()
  39. : mDragBeginPoint(-1, -1),
  40. mViewOffset(0,0),
  41. mZoomScale(1.0f),
  42. mFullBoxSelection(false),
  43. mDragAddSelection(false),
  44. mDragMoveUndo(false)
  45. {
  46. VECTOR_SET_ASSOCIATION(mCurrNodes);
  47. VECTOR_SET_ASSOCIATION(mSelectedNodes);
  48. VECTOR_SET_ASSOCIATION(mDragBeginPoints);
  49. mActive = true;
  50. mMouseDownMode = GuiShaderEditor::Selecting;
  51. mTrash = NULL;
  52. mNodeSize = 10;
  53. // test
  54. mCurrNodes.push_back(new GuiShaderNode());
  55. mCurrNodes.push_back(new GuiShaderNode());
  56. }
  57. bool GuiShaderEditor::onWake()
  58. {
  59. if (!Parent::onWake())
  60. return false;
  61. return true;
  62. }
  63. void GuiShaderEditor::onSleep()
  64. {
  65. Parent::onSleep();
  66. }
  67. // anything smaller than 4 is way too small....
  68. IRangeValidator nodeSizeRange(4, 30);
  69. void GuiShaderEditor::initPersistFields()
  70. {
  71. docsURL;
  72. addGroup("Node Settings");
  73. addFieldV("nodeSize", TypeS32, Offset(mNodeSize, GuiShaderEditor),&nodeSizeRange,
  74. "Size of nodes.");
  75. endGroup("Node Settings");
  76. addGroup("Selection");
  77. addField("fullBoxSelection", TypeBool, Offset(mFullBoxSelection, GuiShaderEditor),
  78. "If true, rectangle selection will only select controls fully inside the drag rectangle.");
  79. endGroup("Selection");
  80. Parent::initPersistFields();
  81. }
  82. bool GuiShaderEditor::onAdd()
  83. {
  84. if (!Parent::onAdd())
  85. return false;
  86. mTrash = new SimGroup();
  87. if (!mTrash->registerObject())
  88. return false;
  89. return true;
  90. }
  91. void GuiShaderEditor::onRemove()
  92. {
  93. Parent::onRemove();
  94. mTrash->deleteObject();
  95. mTrash = NULL;
  96. for (GuiShaderNode* node : mCurrNodes)
  97. {
  98. SAFE_DELETE(node);
  99. }
  100. for (GuiShaderNode* node : mSelectedNodes)
  101. {
  102. SAFE_DELETE(node);
  103. }
  104. }
  105. void GuiShaderEditor::onPreRender()
  106. {
  107. setUpdate();
  108. }
  109. void GuiShaderEditor::renderNodes(Point2I offset, const RectI& updateRect)
  110. {
  111. // Save the current clip rect
  112. // so we can restore it at the end of this method.
  113. RectI savedClipRect = GFX->getClipRect();
  114. // offset is the upper-left corner of this control in screen coordinates
  115. // updateRect is the intersection rectangle in screen coords of the control
  116. // hierarchy. This can be set as the clip rectangle in most cases.
  117. RectI clipRect = updateRect;
  118. clipRect.inset(2, 2);
  119. GFXDrawUtil* drawer = GFX->getDrawUtil();
  120. // render nodes in reverse order.
  121. for (ShaderNodeVector::iterator i = mCurrNodes.end(); i-- != mCurrNodes.begin(); )
  122. {
  123. GuiShaderNode* node = *i;
  124. // this is useful for sub node graphs.
  125. if (node->isVisible())
  126. {
  127. Point2I childPos = offset + node->getPosition();
  128. RectI childClip(childPos, node->getExtent() );
  129. if (selectionContains(node))
  130. {
  131. node->mSelected = true;
  132. }
  133. else
  134. {
  135. node->mSelected = false;
  136. }
  137. if (childClip.intersect(clipRect))
  138. {
  139. GFX->setClipRect(childClip);
  140. GFX->setStateBlock(mDefaultGuiSB);
  141. node->onRender(childPos, childClip, mNodeSize);
  142. }
  143. GFX->setClipRect(clipRect);
  144. GFX->setStateBlock(mDefaultGuiSB);
  145. for (NodeInput* input : node->mInputNodes)
  146. {
  147. Point2I pos = node->localToGlobalCoord(input->pos) + offset;
  148. ColorI border = mProfile->mBorderColor;
  149. if (node->mSelected)
  150. border = mProfile->mBorderColorSEL;
  151. RectI socketRect(pos, Point2I(mNodeSize, mNodeSize));
  152. drawer->drawRect(socketRect, border);
  153. socketRect.inset(1, 1);
  154. drawer->drawRectFill(socketRect, mProfile->mFillColor);
  155. }
  156. for (NodeOutput* output : node->mOutputNodes)
  157. {
  158. Point2I pos = node->localToGlobalCoord(output->pos) + offset;
  159. ColorI border = mProfile->mBorderColor;
  160. if (node->mSelected)
  161. border = mProfile->mBorderColorSEL;
  162. RectI socketRect(pos, Point2I(mNodeSize, mNodeSize));
  163. drawer->drawRect(socketRect, border);
  164. socketRect.inset(1, 1);
  165. drawer->drawRectFill(socketRect, mProfile->mFillColor);
  166. }
  167. }
  168. }
  169. // Restore the clip rect to what it was at the start
  170. // of this method.
  171. GFX->setClipRect(savedClipRect);
  172. }
  173. void GuiShaderEditor::onRender(Point2I offset, const RectI& updateRect)
  174. {
  175. offset += mViewOffset;
  176. GFXDrawUtil* drawer = GFX->getDrawUtil();
  177. // render our nodes.
  178. renderNodes(offset, updateRect);
  179. // Draw selection rectangle last so it is rendered on top.
  180. if (mActive && mMouseDownMode == DragSelecting)
  181. {
  182. RectI b;
  183. getDragRect(b);
  184. b.point += offset;
  185. // Draw outline.
  186. drawer->drawRect(b, ColorI(100, 100, 100, 128));
  187. // Draw fill.
  188. b.inset(1, 1);
  189. drawer->drawRectFill(b, ColorI(150, 150, 150, 128));
  190. }
  191. }
  192. bool GuiShaderEditor::onKeyDown(const GuiEvent& event)
  193. {
  194. if (!mActive)
  195. return Parent::onKeyDown(event);
  196. if (!(event.modifier & SI_PRIMARY_CTRL))
  197. {
  198. switch (event.keyCode)
  199. {
  200. case KEY_BACKSPACE:
  201. case KEY_DELETE:
  202. deleteSelection();
  203. return true;
  204. default:
  205. break;
  206. }
  207. }
  208. return false;
  209. }
  210. void GuiShaderEditor::onMouseDown(const GuiEvent& event)
  211. {
  212. if (!mActive)
  213. {
  214. Parent::onMouseDown(event);
  215. return;
  216. }
  217. setFirstResponder();
  218. // lock mouse
  219. mouseLock();
  220. // get mouse pos with our view offset and scale.
  221. mLastMousePos = globalToLocalCoord(event.mousePoint) - mViewOffset;
  222. GuiShaderNode* hitNode = findHitNode(mLastMousePos);
  223. if (event.modifier & SI_SHIFT)
  224. {
  225. startDragRectangle(mLastMousePos);
  226. mDragAddSelection = true;
  227. }
  228. else if (selectionContains(hitNode))
  229. {
  230. if (event.modifier & SI_MULTISELECT)
  231. {
  232. removeSelection(hitNode);
  233. setMouseMode(Selecting);
  234. }
  235. else if (event.modifier & SI_PRIMARY_ALT)
  236. {
  237. startDragClone(mLastMousePos);
  238. }
  239. else
  240. {
  241. startDragMove(mLastMousePos);
  242. }
  243. }
  244. else
  245. {
  246. if (hitNode == NULL)
  247. {
  248. startDragRectangle(mLastMousePos);
  249. mDragAddSelection = false;
  250. }
  251. else if (event.modifier & SI_PRIMARY_ALT && hitNode != NULL)
  252. {
  253. // Alt is down. Start a drag clone.
  254. clearSelection();
  255. addSelection(hitNode);
  256. startDragClone(mLastMousePos);
  257. }
  258. else if (event.modifier & SI_MULTISELECT)
  259. {
  260. addSelection(hitNode);
  261. }
  262. else
  263. {
  264. // Clicked on node. Start move.
  265. clearSelection();
  266. addSelection(hitNode);
  267. startDragMove(mLastMousePos);
  268. }
  269. }
  270. }
  271. void GuiShaderEditor::onMouseUp(const GuiEvent& event)
  272. {
  273. if (!mActive)
  274. {
  275. Parent::onMouseUp(event);
  276. return;
  277. }
  278. //unlock the mouse
  279. mouseUnlock();
  280. // Reset Drag Axis Alignment Information
  281. mDragBeginPoint.set(-1, -1);
  282. mDragBeginPoints.clear();
  283. // get mouse pos with our view offset and scale.
  284. mLastMousePos = globalToLocalCoord(event.mousePoint) - mViewOffset;
  285. if (mMouseDownMode == DragSelecting)
  286. {
  287. if (!(event.modifier & SI_MULTISELECT) && !mDragAddSelection)
  288. clearSelection();
  289. RectI rect;
  290. getDragRect(rect);
  291. if (rect.extent.x <= 2 && rect.extent.y <= 2)
  292. {
  293. addSelectionAtPoint(rect.point);
  294. }
  295. else
  296. {
  297. Vector< GuiShaderNode* > hits;
  298. findNodesInRect(rect, hits);
  299. for (GuiShaderNode* node : hits)
  300. {
  301. addSelection(node);
  302. }
  303. }
  304. }
  305. //reset the mouse mode
  306. setFirstResponder();
  307. setMouseMode(Selecting);
  308. }
  309. void GuiShaderEditor::onMouseDragged(const GuiEvent& event)
  310. {
  311. if (!mActive)
  312. {
  313. Parent::onMouseDragged(event);
  314. return;
  315. }
  316. // get mouse pos with our view offset and scale.
  317. Point2I mousePoint = globalToLocalCoord(event.mousePoint) - mViewOffset;
  318. if (mMouseDownMode == DragClone)
  319. {
  320. // If we haven't yet crossed the mouse delta to actually start the
  321. // clone, check if we have now.
  322. S32 delta = mAbs((mousePoint - mDragBeginPoint).len());
  323. if (delta >= 4)
  324. {
  325. cloneSelection();
  326. mLastMousePos = mDragBeginPoint;
  327. mDragMoveUndo = false;
  328. setMouseMode(MovingSelection);
  329. }
  330. }
  331. if (mMouseDownMode == MovingSelection && mSelectedNodes.size())
  332. {
  333. Point2I delta = mousePoint - mLastMousePos;
  334. RectI selBounds = getSelectionBounds();
  335. if (delta.x || delta.y)
  336. moveSelection(delta, mDragMoveUndo);
  337. mLastMousePos += delta;
  338. }
  339. else
  340. mLastMousePos = mousePoint;
  341. }
  342. void GuiShaderEditor::onMiddleMouseDown(const GuiEvent& event)
  343. {
  344. if (!mActive)
  345. {
  346. Parent::onMiddleMouseDown(event);
  347. return;
  348. }
  349. setFirstResponder();
  350. // lock mouse
  351. mouseLock();
  352. // get mouse pos with our view offset and scale.
  353. mLastMousePos = globalToLocalCoord(event.mousePoint);
  354. setMouseMode(DragPanning);
  355. getRoot()->showCursor(false);
  356. }
  357. void GuiShaderEditor::onMiddleMouseUp(const GuiEvent& event)
  358. {
  359. if (!mActive)
  360. {
  361. Parent::onMiddleMouseUp(event);
  362. return;
  363. }
  364. getRoot()->showCursor(true);
  365. //unlock the mouse
  366. mouseUnlock();
  367. // Reset Drag Axis Alignment Information
  368. mDragBeginPoint.set(-1, -1);
  369. mDragBeginPoints.clear();
  370. // get mouse pos with our view offset and scale.
  371. mLastMousePos = globalToLocalCoord(event.mousePoint);
  372. setFirstResponder();
  373. setMouseMode(Selecting);
  374. }
  375. void GuiShaderEditor::onMiddleMouseDragged(const GuiEvent& event)
  376. {
  377. if (!mActive)
  378. {
  379. Parent::onMiddleMouseDragged(event);
  380. return;
  381. }
  382. // get mouse pos with our view offset and scale.
  383. Point2I mousePoint = globalToLocalCoord(event.mousePoint);
  384. if (mMouseDownMode == DragPanning)
  385. {
  386. Point2I delta = mousePoint - mLastMousePos;
  387. // invert it
  388. if (delta.x || delta.y)
  389. mViewOffset += -delta;
  390. mLastMousePos += delta;
  391. }
  392. else
  393. mLastMousePos = mousePoint;
  394. }
  395. bool GuiShaderEditor::onMouseWheelUp(const GuiEvent& event)
  396. {
  397. if (!mActive || !mAwake || !mVisible)
  398. return Parent::onMouseWheelUp(event);
  399. mZoomScale *= 1.1;
  400. return true;
  401. }
  402. bool GuiShaderEditor::onMouseWheelDown(const GuiEvent& event)
  403. {
  404. if (!mActive || !mAwake || !mVisible)
  405. return Parent::onMouseWheelDown(event);
  406. mZoomScale *= 0.9;
  407. return true;
  408. }
  409. RectI GuiShaderEditor::getSelectionBounds()
  410. {
  411. Vector<GuiShaderNode*>::const_iterator i = mSelectedNodes.begin();
  412. Point2I minPos = (*i)->localToGlobalCoord(Point2I(0, 0));
  413. Point2I maxPos = minPos;
  414. for (; i != mSelectedNodes.end(); i++)
  415. {
  416. Point2I iPos = (**i).localToGlobalCoord(Point2I(0, 0));
  417. minPos.x = getMin(iPos.x, minPos.x);
  418. minPos.y = getMin(iPos.y, minPos.y);
  419. Point2I iExt = (**i).getExtent();
  420. iPos.x += iExt.x;
  421. iPos.y += iExt.y;
  422. maxPos.x = getMax(iPos.x, maxPos.x);
  423. maxPos.y = getMax(iPos.y, maxPos.y);
  424. }
  425. minPos = globalToLocalCoord(minPos);
  426. maxPos = globalToLocalCoord(maxPos);
  427. return RectI(minPos.x, minPos.y, (maxPos.x - minPos.x), (maxPos.y - minPos.y));
  428. }
  429. void GuiShaderEditor::deleteSelection()
  430. {
  431. for (GuiShaderNode* node : mSelectedNodes)
  432. {
  433. mTrash->addObject(node);
  434. Vector< GuiShaderNode* >::iterator i = T3D::find(mCurrNodes.begin(), mCurrNodes.end(), node);
  435. if (i != mCurrNodes.end())
  436. mCurrNodes.erase(i);
  437. }
  438. clearSelection();
  439. }
  440. void GuiShaderEditor::moveSelection(const Point2I& delta, bool callback)
  441. {
  442. for (GuiShaderNode* node : mSelectedNodes)
  443. {
  444. node->setPosition(node->getPosition() + delta);
  445. }
  446. }
  447. void GuiShaderEditor::clearSelection()
  448. {
  449. mSelectedNodes.clear();
  450. }
  451. void GuiShaderEditor::cloneSelection()
  452. {
  453. Vector<GuiShaderNode*> newSelection;
  454. for (GuiShaderNode* node : mSelectedNodes)
  455. {
  456. GuiShaderNode* clone = dynamic_cast<GuiShaderNode*>(node->deepClone());
  457. if (clone)
  458. newSelection.push_back(clone);
  459. }
  460. clearSelection();
  461. for (GuiShaderNode* cloneNode : newSelection)
  462. {
  463. mCurrNodes.push_back(cloneNode);
  464. addSelection(cloneNode);
  465. }
  466. }
  467. void GuiShaderEditor::addSelectionAtPoint(const Point2I& pos)
  468. {
  469. // turn hit off on already selected nodes.
  470. canHitSelectedNodes(false);
  471. GuiShaderNode* node = findHitNode(pos);
  472. // reset hit status.
  473. canHitSelectedNodes();
  474. if (node)
  475. addSelection(node);
  476. }
  477. void GuiShaderEditor::addSelection(GuiShaderNode* inNode)
  478. {
  479. if (inNode != NULL && !selectionContains(inNode))
  480. {
  481. mSelectedNodes.push_back(inNode);
  482. }
  483. }
  484. bool GuiShaderEditor::selectionContains(GuiShaderNode* inNode)
  485. {
  486. for (GuiShaderNode* node : mSelectedNodes)
  487. {
  488. if (node == inNode)
  489. return true;
  490. }
  491. return false;
  492. }
  493. void GuiShaderEditor::removeSelection(GuiShaderNode* inNode)
  494. {
  495. if (selectionContains(inNode))
  496. {
  497. Vector< GuiShaderNode* >::iterator i = T3D::find(mSelectedNodes.begin(), mSelectedNodes.end(), inNode);
  498. if (i != mSelectedNodes.end())
  499. mSelectedNodes.erase(i);
  500. }
  501. }
  502. void GuiShaderEditor::canHitSelectedNodes(bool state)
  503. {
  504. for (GuiShaderNode* node : mSelectedNodes)
  505. node->setCanHit(state);
  506. }
  507. //-----------------------------------------------------------------------------
  508. // Input handling
  509. //-----------------------------------------------------------------------------
  510. GuiShaderNode* GuiShaderEditor::findHitNode(const Point2I& pt)
  511. {
  512. for (GuiShaderNode* node : mCurrNodes)
  513. {
  514. if (node->pointInControl(pt))
  515. {
  516. // selecting one node, push it to the front of the mcurrnodes stack so its rendered on top.
  517. Vector< GuiShaderNode* >::iterator i = T3D::find(mCurrNodes.begin(), mCurrNodes.end(), node);
  518. if (i != mCurrNodes.end())
  519. {
  520. mCurrNodes.erase(i);
  521. mCurrNodes.insert(mCurrNodes.begin(), node);
  522. }
  523. return node;
  524. }
  525. }
  526. return nullptr;
  527. }
  528. void GuiShaderEditor::findNodesInRect(const RectI& rect, Vector<GuiShaderNode*>& outResult)
  529. {
  530. canHitSelectedNodes(false);
  531. for (GuiShaderNode* node : mCurrNodes)
  532. {
  533. if (node->getBounds().overlaps(rect))
  534. {
  535. outResult.push_back(node);
  536. }
  537. }
  538. canHitSelectedNodes();
  539. }
  540. void GuiShaderEditor::getDragRect(RectI& box)
  541. {
  542. box.point.x = getMin(mLastMousePos.x, mSelectionAnchor.x);
  543. box.extent.x = getMax(mLastMousePos.x, mSelectionAnchor.x) - box.point.x + 1;
  544. box.point.y = getMin(mLastMousePos.y, mSelectionAnchor.y);
  545. box.extent.y = getMax(mLastMousePos.y, mSelectionAnchor.y) - box.point.y + 1;
  546. }
  547. void GuiShaderEditor::startDragMove(const Point2I& startPoint)
  548. {
  549. mDragMoveUndo = true;
  550. mDragBeginPoint = startPoint;
  551. mDragBeginPoints.reserve(mSelectedNodes.size());
  552. for (GuiShaderNode* node : mSelectedNodes)
  553. {
  554. mDragBeginPoints.push_back(node->getPosition());
  555. }
  556. setMouseMode(MovingSelection);
  557. }
  558. void GuiShaderEditor::startDragRectangle(const Point2I& startPoint)
  559. {
  560. mSelectionAnchor = startPoint;
  561. setMouseMode(DragSelecting);
  562. }
  563. void GuiShaderEditor::startDragClone(const Point2I& startPoint)
  564. {
  565. mDragBeginPoint = startPoint;
  566. setMouseMode(DragClone);
  567. }
  568. void GuiShaderEditor::setMouseMode(mouseModes mode)
  569. {
  570. if (mMouseDownMode != mode)
  571. {
  572. mMouseDownMode = mode;
  573. }
  574. }