12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040 |
- //-----------------------------------------------------------------------------
- // Copyright (c) 2012 GarageGames, LLC
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to
- // deal in the Software without restriction, including without limitation the
- // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- // sell copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- // IN THE SOFTWARE.
- //-----------------------------------------------------------------------------
- #include "platform/platform.h"
- #include "gui/shaderEditor/guiShaderEditor.h"
- #include "core/frameAllocator.h"
- #include "core/stream/fileStream.h"
- #include "core/stream/memStream.h"
- #include "console/consoleTypes.h"
- #include "gui/core/guiCanvas.h"
- #include "console/engineAPI.h"
- #include "console/script.h"
- #include "console/typeValidators.h"
- #include "gfx/primBuilder.h"
- IMPLEMENT_CONOBJECT(GuiShaderEditor);
- ConsoleDocClass(GuiShaderEditor,
- "@brief Implementation of a shader node editor.\n\n"
- "Editor use only.\n\n"
- "@internal"
- );
- GuiShaderEditor::GuiShaderEditor()
- : mDragBeginPoint(-1, -1),
- mViewOffset(0,0),
- mZoomScale(1.0f),
- mFullBoxSelection(false),
- mDragAddSelection(false),
- mDragMoveUndo(false)
- {
- VECTOR_SET_ASSOCIATION(mCurrNodes);
- VECTOR_SET_ASSOCIATION(mSelectedNodes);
- VECTOR_SET_ASSOCIATION(mDragBeginPoints);
- VECTOR_SET_ASSOCIATION(mCurrConnections);
- mActive = true;
- mMouseDownMode = GuiShaderEditor::Selecting;
- mTrash = NULL;
- mTempConnection = NULL;
- mNodeSize = 10;
- // test
- addNode(new GuiShaderNode());
- addNode(new GuiShaderNode());
- }
- bool GuiShaderEditor::onWake()
- {
- if (!Parent::onWake())
- return false;
- return true;
- }
- void GuiShaderEditor::onSleep()
- {
- Parent::onSleep();
- }
- // anything smaller than 4 is way too small....
- IRangeValidator nodeSizeRange(4, 30);
- void GuiShaderEditor::initPersistFields()
- {
- docsURL;
- addGroup("Node Settings");
- addFieldV("nodeSize", TypeS32, Offset(mNodeSize, GuiShaderEditor),&nodeSizeRange,
- "Size of nodes.");
- endGroup("Node Settings");
- addGroup("Selection");
- addField("fullBoxSelection", TypeBool, Offset(mFullBoxSelection, GuiShaderEditor),
- "If true, rectangle selection will only select controls fully inside the drag rectangle.");
- endGroup("Selection");
- Parent::initPersistFields();
- }
- bool GuiShaderEditor::onAdd()
- {
- if (!Parent::onAdd())
- return false;
- mTrash = new SimGroup();
- if (!mTrash->registerObject())
- return false;
- return true;
- }
- void GuiShaderEditor::onRemove()
- {
- Parent::onRemove();
- mTrash->deleteObject();
- mTrash = NULL;
- /* for (GuiShaderNode* node : mCurrNodes)
- {
- SAFE_DELETE(node);
- }*/
- for (GuiShaderNode* node : mSelectedNodes)
- {
- SAFE_DELETE(node);
- }
- }
- void GuiShaderEditor::onPreRender()
- {
- setUpdate();
- }
- void GuiShaderEditor::drawThickLine(const Point2I& pt1, const Point2I& pt2, U32 thickness = 2, ColorI col1 = ColorI(255, 255, 255), ColorI col2 = ColorI(255, 255, 255))
- {
- Point2F dir = Point2F(pt2.x - pt1.x, pt2.y - pt1.y);
- if (dir == Point2F::Zero)
- return;
- Point2F unitDir = dir / mSqrt(dir.x * dir.x + dir.y * dir.y);
- Point2F unitPerp(-unitDir.y, unitDir.x);
- Point2F offset = (thickness / 2.0f) * unitPerp;
- GFX->setStateBlock(mDefaultGuiSB);
- Point2F lT = Point2F(pt1.x, pt1.y) + offset;
- Point2F lB = Point2F(pt1.x, pt1.y) - offset;
- Point2F rT = Point2F(pt2.x, pt2.y) + offset;
- Point2F rB = Point2F(pt2.x, pt2.y) - offset;
- PrimBuild::begin(GFXTriangleStrip, 4);
- // top left.
- PrimBuild::color(col1);
- PrimBuild::vertex2f(lT.x, lT.y);
- // bottom left.
- PrimBuild::color(col1);
- PrimBuild::vertex2f(lB.x, lB.y);
- // top right.
- PrimBuild::color(col2);
- PrimBuild::vertex2f(rT.x, rT.y);
- // bottom right.
- PrimBuild::color(col2);
- PrimBuild::vertex2f(rB.x, rB.y);
- PrimBuild::end();
- }
- void GuiShaderEditor::renderNodes(Point2I offset, const RectI& updateRect)
- {
- // Save the current clip rect
- // so we can restore it at the end of this method.
- RectI savedClipRect = GFX->getClipRect();
- // offset is the upper-left corner of this control in screen coordinates
- // updateRect is the intersection rectangle in screen coords of the control
- // hierarchy. This can be set as the clip rectangle in most cases.
- RectI clipRect = updateRect;
- clipRect.inset(2, 2);
- GFXDrawUtil* drawer = GFX->getDrawUtil();
- // render nodes in reverse order.
- for (ShaderNodeVector::iterator i = mCurrNodes.end(); i-- != mCurrNodes.begin(); )
- {
- GuiShaderNode* node = *i;
- // this is useful for sub node graphs.
- if (node->isVisible())
- {
- Point2I childPos = offset + node->getPosition();
- RectI childClip(childPos, node->getExtent() );
- if (selectionContains(node))
- {
- node->mSelected = true;
- }
- else
- {
- node->mSelected = false;
- }
- if (childClip.intersect(clipRect))
- {
- GFX->setClipRect(childClip);
- GFX->setStateBlock(mDefaultGuiSB);
- node->renderNode(childPos, childClip, mNodeSize);
- }
- GFX->setClipRect(clipRect);
- GFX->setStateBlock(mDefaultGuiSB);
- for (NodeInput* input : node->mInputNodes)
- {
- Point2I pos = node->localToGlobalCoord(input->pos) + offset;
- ColorI border = mProfile->mBorderColor;
- if (node->mSelected)
- border = mProfile->mBorderColorSEL;
- RectI socketRect(pos, Point2I(mNodeSize, mNodeSize));
- drawer->drawRect(socketRect, border);
- socketRect.inset(1, 1);
- ColorI fill = mProfile->mFillColor;
- if (hasConnection(input))
- {
- fill = ColorI::WHITE;
- }
- drawer->drawRectFill(socketRect, fill);
- }
- for (NodeOutput* output : node->mOutputNodes)
- {
- Point2I pos = node->localToGlobalCoord(output->pos) + offset;
- ColorI border = mProfile->mBorderColor;
- if (node->mSelected)
- border = mProfile->mBorderColorSEL;
- RectI socketRect(pos, Point2I(mNodeSize, mNodeSize));
- drawer->drawRect(socketRect, border);
- socketRect.inset(1, 1);
- ColorI fill = mProfile->mFillColor;
- if (hasConnection(output))
- {
- fill = ColorI::WHITE;
- }
- drawer->drawRectFill(socketRect, fill);
- }
- }
- }
- // Restore the clip rect to what it was at the start
- // of this method.
- GFX->setClipRect(savedClipRect);
- }
- void GuiShaderEditor::renderConnections(Point2I offset, const RectI& updateRect)
- {
- // Save the current clip rect
- // so we can restore it at the end of this method.
- RectI savedClipRect = GFX->getClipRect();
- // offset is the upper-left corner of this control in screen coordinates
- // updateRect is the intersection rectangle in screen coords of the control
- // hierarchy. This can be set as the clip rectangle in most cases.
- RectI clipRect = updateRect;
- clipRect.inset(2, 2);
- GFXDrawUtil* drawer = GFX->getDrawUtil();
- for (NodeConnection* conn : mCurrConnections)
- {
- // for temp connetion, nodeA is always the first node selected.
- Point2I start(Point2I::Zero);
- Point2I end(Point2I::Zero);
- start = conn->nodeA->localToGlobalCoord(conn->inSocket->pos) + offset;
- end = conn->nodeB->localToGlobalCoord(conn->outSocket->pos) + offset;
- start += Point2I(mNodeSize / 2, mNodeSize / 2);
- end += Point2I(mNodeSize / 2, mNodeSize / 2);
- drawThickLine(start, end);
- }
- // Restore the clip rect to what it was at the start
- // of this method.
- GFX->setClipRect(savedClipRect);
- }
- void GuiShaderEditor::onRender(Point2I offset, const RectI& updateRect)
- {
- offset += mViewOffset;
- GFXDrawUtil* drawer = GFX->getDrawUtil();
- // render our nodes.
- renderConnections(offset, updateRect);
- renderNodes(offset, updateRect);
- if (mActive)
- {
- if (mMouseDownMode == DragConnection)
- {
- // something went wrong.... fix it fix it fix it.
- if (mTempConnection == NULL)
- return;
- else
- {
- // for temp connetion, nodeA is always the first node selected.
- Point2I start(Point2I::Zero);
- if (mTempConnection->inSocket != NULL)
- start = mTempConnection->nodeA->localToGlobalCoord(mTempConnection->inSocket->pos) + offset;
- else if(mTempConnection->outSocket != NULL)
- start = mTempConnection->nodeA->localToGlobalCoord(mTempConnection->outSocket->pos) + offset;
- RectI sockActive(start, Point2I(mNodeSize, mNodeSize));
- start += Point2I(mNodeSize / 2, mNodeSize / 2);
- drawThickLine(start, mLastMousePos + offset);
- // draw socket overlay over the top of the line.
- sockActive.inset(1, 1);
- drawer->drawRectFill(sockActive, ColorI(255, 255, 255));
- }
- }
- // Draw selection rectangle last so it is rendered on top.
- if (mMouseDownMode == DragSelecting)
- {
- RectI b;
- getDragRect(b);
- b.point += offset;
- // Draw outline.
- drawer->drawRect(b, ColorI(100, 100, 100, 128));
- // Draw fill.
- b.inset(1, 1);
- drawer->drawRectFill(b, ColorI(150, 150, 150, 128));
- }
- }
- }
- bool GuiShaderEditor::onKeyDown(const GuiEvent& event)
- {
- if (!mActive)
- return Parent::onKeyDown(event);
- if (!(event.modifier & SI_PRIMARY_CTRL))
- {
- switch (event.keyCode)
- {
- case KEY_BACKSPACE:
- case KEY_DELETE:
- deleteSelection();
- return true;
- default:
- break;
- }
- }
- return false;
- }
- void GuiShaderEditor::onMouseDown(const GuiEvent& event)
- {
- if (!mActive)
- {
- Parent::onMouseDown(event);
- return;
- }
- setFirstResponder();
- // lock mouse
- mouseLock();
- // get mouse pos with our view offset and scale.
- mLastMousePos = globalToLocalCoord(event.mousePoint) - mViewOffset;
- GuiShaderNode* hitNode = findHitNode(mLastMousePos);
- if(findHitSocket(mLastMousePos))
- {
- // handled in hit socket.
- return;
- }
- else
- {
- if (event.modifier & SI_SHIFT)
- {
- startDragRectangle(mLastMousePos);
- mDragAddSelection = true;
- }
- else if (selectionContains(hitNode))
- {
- if (event.modifier & SI_MULTISELECT)
- {
- removeSelection(hitNode);
- setMouseMode(Selecting);
- }
- else if (event.modifier & SI_PRIMARY_ALT)
- {
- startDragClone(mLastMousePos);
- }
- else
- {
- startDragMove(mLastMousePos);
- }
- }
- else
- {
- if (hitNode == NULL)
- {
- startDragRectangle(mLastMousePos);
- mDragAddSelection = false;
- }
- else if (event.modifier & SI_PRIMARY_ALT && hitNode != NULL)
- {
- // Alt is down. Start a drag clone.
- clearSelection();
- addSelection(hitNode);
- startDragClone(mLastMousePos);
- }
- else if (event.modifier & SI_MULTISELECT)
- {
- addSelection(hitNode);
- }
- else
- {
- // Clicked on node. Start move.
- clearSelection();
- addSelection(hitNode);
- startDragMove(mLastMousePos);
- }
- }
- }
- }
- void GuiShaderEditor::onMouseUp(const GuiEvent& event)
- {
- if (!mActive)
- {
- Parent::onMouseUp(event);
- return;
- }
- //unlock the mouse
- mouseUnlock();
- // Reset Drag Axis Alignment Information
- mDragBeginPoint.set(-1, -1);
- mDragBeginPoints.clear();
- // get mouse pos with our view offset and scale.
- mLastMousePos = globalToLocalCoord(event.mousePoint) - mViewOffset;
- if (mMouseDownMode == DragConnection)
- {
- U32 ret = finishConnection(mLastMousePos);
- if (ret == 1) // we set the input on mouse up, nodeB is the inputSocket, swap order.
- {
- NodeConnection* conn = new NodeConnection();
- conn->nodeA = mTempConnection->nodeB;
- conn->nodeB = mTempConnection->nodeA;
- conn->inSocket = mTempConnection->inSocket;
- conn->outSocket = mTempConnection->outSocket;
- mCurrConnections.push_back(conn);
- }
- if (ret == 2) // we set the output on mouse up, nodeB is the outputSocket
- {
- NodeConnection* conn = new NodeConnection();
- conn->nodeA = mTempConnection->nodeA;
- conn->nodeB = mTempConnection->nodeB;
- conn->inSocket = mTempConnection->inSocket;
- conn->outSocket = mTempConnection->outSocket;
- mCurrConnections.push_back(conn);
- }
- mTempConnection = NULL;
- }
- if (mMouseDownMode == DragSelecting)
- {
- if (!(event.modifier & SI_MULTISELECT) && !mDragAddSelection)
- clearSelection();
- RectI rect;
- getDragRect(rect);
- if (rect.extent.x <= 2 && rect.extent.y <= 2)
- {
- addSelectionAtPoint(rect.point);
- }
- else
- {
- Vector< GuiShaderNode* > hits;
- findNodesInRect(rect, hits);
- for (GuiShaderNode* node : hits)
- {
- addSelection(node);
- }
- }
- }
- //reset the mouse mode
- setFirstResponder();
- setMouseMode(Selecting);
- }
- void GuiShaderEditor::onMouseDragged(const GuiEvent& event)
- {
- if (!mActive)
- {
- Parent::onMouseDragged(event);
- return;
- }
- // get mouse pos with our view offset and scale.
- Point2I mousePoint = globalToLocalCoord(event.mousePoint) - mViewOffset;
- if (mMouseDownMode == DragClone)
- {
- // If we haven't yet crossed the mouse delta to actually start the
- // clone, check if we have now.
- S32 delta = mAbs((mousePoint - mDragBeginPoint).len());
- if (delta >= 4)
- {
- cloneSelection();
- mLastMousePos = mDragBeginPoint;
- mDragMoveUndo = false;
- setMouseMode(MovingSelection);
- }
- }
- if (mMouseDownMode == MovingSelection && mSelectedNodes.size())
- {
- Point2I delta = mousePoint - mLastMousePos;
- RectI selBounds = getSelectionBounds();
- if (delta.x || delta.y)
- moveSelection(delta, mDragMoveUndo);
- mLastMousePos += delta;
- }
- else
- mLastMousePos = mousePoint;
- }
- void GuiShaderEditor::onMiddleMouseDown(const GuiEvent& event)
- {
- if (!mActive)
- {
- Parent::onMiddleMouseDown(event);
- return;
- }
- setFirstResponder();
- // lock mouse
- mouseLock();
- // get mouse pos with our view offset and scale.
- mLastMousePos = globalToLocalCoord(event.mousePoint);
- setMouseMode(DragPanning);
- getRoot()->showCursor(false);
- }
- void GuiShaderEditor::onMiddleMouseUp(const GuiEvent& event)
- {
- if (!mActive)
- {
- Parent::onMiddleMouseUp(event);
- return;
- }
- getRoot()->showCursor(true);
- //unlock the mouse
- mouseUnlock();
- // Reset Drag Axis Alignment Information
- mDragBeginPoint.set(-1, -1);
- mDragBeginPoints.clear();
- // get mouse pos with our view offset and scale.
- mLastMousePos = globalToLocalCoord(event.mousePoint);
- setFirstResponder();
- setMouseMode(Selecting);
- }
- void GuiShaderEditor::onMiddleMouseDragged(const GuiEvent& event)
- {
- if (!mActive)
- {
- Parent::onMiddleMouseDragged(event);
- return;
- }
- // get mouse pos with our view offset and scale.
- Point2I mousePoint = globalToLocalCoord(event.mousePoint);
- if (mMouseDownMode == DragPanning)
- {
- Point2I delta = mousePoint - mLastMousePos;
- // invert it
- if (delta.x || delta.y)
- mViewOffset += -delta;
- mLastMousePos += delta;
- }
- else
- mLastMousePos = mousePoint;
- }
- bool GuiShaderEditor::onMouseWheelUp(const GuiEvent& event)
- {
- if (!mActive || !mAwake || !mVisible)
- return Parent::onMouseWheelUp(event);
- mZoomScale *= 1.1;
- return true;
- }
- bool GuiShaderEditor::onMouseWheelDown(const GuiEvent& event)
- {
- if (!mActive || !mAwake || !mVisible)
- return Parent::onMouseWheelDown(event);
- mZoomScale *= 0.9;
- return true;
- }
- RectI GuiShaderEditor::getSelectionBounds()
- {
- Vector<GuiShaderNode*>::const_iterator i = mSelectedNodes.begin();
- Point2I minPos = (*i)->localToGlobalCoord(Point2I(0, 0));
- Point2I maxPos = minPos;
- for (; i != mSelectedNodes.end(); i++)
- {
- Point2I iPos = (**i).localToGlobalCoord(Point2I(0, 0));
- minPos.x = getMin(iPos.x, minPos.x);
- minPos.y = getMin(iPos.y, minPos.y);
- Point2I iExt = (**i).getExtent();
- iPos.x += iExt.x;
- iPos.y += iExt.y;
- maxPos.x = getMax(iPos.x, maxPos.x);
- maxPos.y = getMax(iPos.y, maxPos.y);
- }
- minPos = globalToLocalCoord(minPos);
- maxPos = globalToLocalCoord(maxPos);
- return RectI(minPos.x, minPos.y, (maxPos.x - minPos.x), (maxPos.y - minPos.y));
- }
- void GuiShaderEditor::deleteSelection()
- {
- for (GuiShaderNode* node : mSelectedNodes)
- {
- mTrash->addObject(node);
- for (NodeInput* input : node->mInputNodes)
- {
- NodeConnection* conn;
- if (hasConnection(input, conn))
- {
- // selecting one node, push it to the front of the mcurrnodes stack so its rendered on top.
- Vector< NodeConnection* >::iterator i = T3D::find(mCurrConnections.begin(), mCurrConnections.end(), conn);
- if (i != mCurrConnections.end())
- {
- mCurrConnections.erase(i);
- }
- }
- }
- for (NodeOutput* output : node->mOutputNodes)
- {
- NodeConnection* conn;
- if (hasConnection(output, conn))
- {
- // selecting one node, push it to the front of the mcurrnodes stack so its rendered on top.
- Vector< NodeConnection* >::iterator i = T3D::find(mCurrConnections.begin(), mCurrConnections.end(), conn);
- if (i != mCurrConnections.end())
- {
- mCurrConnections.erase(i);
- }
- }
- }
- Vector< GuiShaderNode* >::iterator i = T3D::find(mCurrNodes.begin(), mCurrNodes.end(), node);
- if (i != mCurrNodes.end())
- mCurrNodes.erase(i);
- }
- clearSelection();
- }
- void GuiShaderEditor::moveSelection(const Point2I& delta, bool callback)
- {
- for (GuiShaderNode* node : mSelectedNodes)
- {
- node->setPosition(node->getPosition() + delta);
- }
- }
- void GuiShaderEditor::clearSelection()
- {
- mSelectedNodes.clear();
- }
- void GuiShaderEditor::cloneSelection()
- {
- Vector<GuiShaderNode*> newSelection;
- for (GuiShaderNode* node : mSelectedNodes)
- {
- GuiShaderNode* clone = dynamic_cast<GuiShaderNode*>(node->deepClone());
- if (clone)
- newSelection.push_back(clone);
- }
- clearSelection();
- for (GuiShaderNode* cloneNode : newSelection)
- {
- mCurrNodes.push_back(cloneNode);
- addSelection(cloneNode);
- }
- }
- void GuiShaderEditor::addSelectionAtPoint(const Point2I& pos)
- {
- // turn hit off on already selected nodes.
- canHitSelectedNodes(false);
- GuiShaderNode* node = findHitNode(pos);
- // reset hit status.
- canHitSelectedNodes();
- if (node)
- addSelection(node);
- }
- void GuiShaderEditor::addSelection(GuiShaderNode* inNode)
- {
- if (inNode != NULL && !selectionContains(inNode))
- {
- mSelectedNodes.push_back(inNode);
- }
- }
- bool GuiShaderEditor::selectionContains(GuiShaderNode* inNode)
- {
- for (GuiShaderNode* node : mSelectedNodes)
- {
- if (node == inNode)
- return true;
- }
- return false;
- }
- void GuiShaderEditor::removeSelection(GuiShaderNode* inNode)
- {
- if (selectionContains(inNode))
- {
- Vector< GuiShaderNode* >::iterator i = T3D::find(mSelectedNodes.begin(), mSelectedNodes.end(), inNode);
- if (i != mSelectedNodes.end())
- mSelectedNodes.erase(i);
- }
- }
- void GuiShaderEditor::canHitSelectedNodes(bool state)
- {
- for (GuiShaderNode* node : mSelectedNodes)
- node->setCanHit(state);
- }
- //-----------------------------------------------------------------------------
- // Input handling
- //-----------------------------------------------------------------------------
- GuiShaderNode* GuiShaderEditor::findHitNode(const Point2I& pt)
- {
- for (GuiShaderNode* node : mCurrNodes)
- {
- if (node->pointInControl(pt))
- {
- // selecting one node, push it to the front of the mcurrnodes stack so its rendered on top.
- Vector< GuiShaderNode* >::iterator i = T3D::find(mCurrNodes.begin(), mCurrNodes.end(), node);
- if (i != mCurrNodes.end())
- {
- mCurrNodes.erase(i);
- mCurrNodes.insert(mCurrNodes.begin(), node);
- }
- return node;
- }
- }
- return nullptr;
- }
- bool GuiShaderEditor::findHitSocket(const Point2I& pt)
- {
- Point2I parentOffset = localToGlobalCoord(getPosition()) + mViewOffset;
- Point2I offsetPoint = pt + localToGlobalCoord(getPosition());
- for (GuiShaderNode* node : mCurrNodes)
- {
- for (NodeInput* inNode : node->mInputNodes)
- {
- Point2I offSet = node->localToGlobalCoord(inNode->pos) + parentOffset;
- RectI box(offSet.x, offSet.y, mNodeSize, mNodeSize);
- S32 xt = offsetPoint.x - box.point.x;
- S32 yt = offsetPoint.y - box.point.y;
- if (xt >= 0 && yt >= 0 && xt < box.extent.x && yt < box.extent.y)
- {
- mTempConnection = new NodeConnection();
- mTempConnection->nodeA = node;
- mTempConnection->inSocket = inNode;
- setMouseMode(DragConnection);
- return true;
- }
- }
- for (NodeOutput* outNode : node->mOutputNodes)
- {
- Point2I offSet = node->localToGlobalCoord(outNode->pos) + parentOffset;
- RectI box(offSet.x, offSet.y, mNodeSize, mNodeSize);
- S32 xt = offsetPoint.x - box.point.x;
- S32 yt = offsetPoint.y - box.point.y;
- if (xt >= 0 && yt >= 0 && xt < box.extent.x && yt < box.extent.y)
- {
- mTempConnection = new NodeConnection();
- mTempConnection->nodeA = node;
- mTempConnection->outSocket = outNode;
- setMouseMode(DragConnection);
- return true;
- }
- }
- }
- return false;
- }
- U32 GuiShaderEditor::finishConnection(const Point2I& pt)
- {
- Point2I parentOffset = localToGlobalCoord(getPosition()) + mViewOffset;
- Point2I offsetPoint = pt + localToGlobalCoord(getPosition());
- for (GuiShaderNode* node : mCurrNodes)
- {
- for (NodeInput* inNode : node->mInputNodes)
- {
- Point2I offSet = node->localToGlobalCoord(inNode->pos) + parentOffset;
- RectI box(offSet.x, offSet.y, mNodeSize, mNodeSize);
- S32 xt = offsetPoint.x - box.point.x;
- S32 yt = offsetPoint.y - box.point.y;
- if (xt >= 0 && yt >= 0 && xt < box.extent.x && yt < box.extent.y)
- {
- if (mTempConnection->nodeA == node || mTempConnection->inSocket != NULL)
- return false;
- NodeConnection* conn;
- if(hasConnection(inNode, conn))
- {
- // selecting one node, push it to the front of the mcurrnodes stack so its rendered on top.
- Vector< NodeConnection* >::iterator i = T3D::find(mCurrConnections.begin(), mCurrConnections.end(), conn);
- if (i != mCurrConnections.end())
- {
- mCurrConnections.erase(i);
- }
- }
- mTempConnection->nodeB = node;
- mTempConnection->inSocket = inNode;
- return 1;
- }
- }
- for (NodeOutput* outNode : node->mOutputNodes)
- {
- Point2I offSet = node->localToGlobalCoord(outNode->pos) + parentOffset;
- RectI box(offSet.x, offSet.y, mNodeSize, mNodeSize);
- S32 xt = offsetPoint.x - box.point.x;
- S32 yt = offsetPoint.y - box.point.y;
- if (xt >= 0 && yt >= 0 && xt < box.extent.x && yt < box.extent.y)
- {
- if (mTempConnection->nodeA == node || mTempConnection->outSocket != NULL)
- return false;
- NodeConnection* conn;
- if (hasConnection(mTempConnection->inSocket, conn))
- {
- // selecting one node, push it to the front of the mcurrnodes stack so its rendered on top.
- Vector< NodeConnection* >::iterator i = T3D::find(mCurrConnections.begin(), mCurrConnections.end(), conn);
- if (i != mCurrConnections.end())
- {
- mCurrConnections.erase(i);
- }
- }
- mTempConnection->nodeB = node;
- mTempConnection->outSocket = outNode;
- return 2;
- }
- }
- }
- return 0;
- }
- bool GuiShaderEditor::hasConnection(NodeSocket* inSocket)
- {
- for (NodeConnection* con : mCurrConnections)
- {
- if (con->inSocket == dynamic_cast<NodeInput*>(inSocket) || con->outSocket == dynamic_cast<NodeOutput*>(inSocket))
- {
- return true;
- }
- }
- return false;
- }
- bool GuiShaderEditor::hasConnection(NodeSocket* inSocket, NodeConnection*& conn)
- {
- for (NodeConnection* con : mCurrConnections)
- {
- if (con->inSocket == dynamic_cast<NodeInput*>(inSocket) || con->outSocket == dynamic_cast<NodeOutput*>(inSocket))
- {
- if (conn != nullptr)
- conn = con;
- return true;
- }
- }
- return false;
- }
- void GuiShaderEditor::findNodesInRect(const RectI& rect, Vector<GuiShaderNode*>& outResult)
- {
- canHitSelectedNodes(false);
- for (GuiShaderNode* node : mCurrNodes)
- {
- if (node->getBounds().overlaps(rect))
- {
- outResult.push_back(node);
- }
- }
- canHitSelectedNodes();
- }
- void GuiShaderEditor::getDragRect(RectI& box)
- {
- box.point.x = getMin(mLastMousePos.x, mSelectionAnchor.x);
- box.extent.x = getMax(mLastMousePos.x, mSelectionAnchor.x) - box.point.x + 1;
- box.point.y = getMin(mLastMousePos.y, mSelectionAnchor.y);
- box.extent.y = getMax(mLastMousePos.y, mSelectionAnchor.y) - box.point.y + 1;
- }
- void GuiShaderEditor::startDragMove(const Point2I& startPoint)
- {
- mDragMoveUndo = true;
- mDragBeginPoint = startPoint;
- mDragBeginPoints.reserve(mSelectedNodes.size());
- for (GuiShaderNode* node : mSelectedNodes)
- {
- mDragBeginPoints.push_back(node->getPosition());
- }
- setMouseMode(MovingSelection);
- }
- void GuiShaderEditor::startDragRectangle(const Point2I& startPoint)
- {
- mSelectionAnchor = startPoint;
- setMouseMode(DragSelecting);
- }
- void GuiShaderEditor::startDragClone(const Point2I& startPoint)
- {
- mDragBeginPoint = startPoint;
- setMouseMode(DragClone);
- }
- void GuiShaderEditor::setMouseMode(mouseModes mode)
- {
- if (mMouseDownMode != mode)
- {
- mMouseDownMode = mode;
- }
- }
- void GuiShaderEditor::addNode(GuiShaderNode* newNode)
- {
- mCurrNodes.push_back(newNode);
- }
|