guiShaderEditor.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. #ifndef _GUISHADEREDITOR_H_
  23. #define _GUISHADEREDITOR_H_
  24. #ifndef _GUICONTROL_H_
  25. #include "gui/core/guiControl.h"
  26. #endif
  27. #ifndef _UNDO_H_
  28. #include "util/undo.h"
  29. #endif
  30. #ifndef _GFX_GFXDRAWER_H_
  31. #include "gfx/gfxDrawUtil.h"
  32. #endif
  33. #ifndef _SHADERNODE_H_
  34. #include "gui/shaderEditor/guiShaderNode.h"
  35. #endif // !_SHADERNODE_H_
  36. struct NodeConnection
  37. {
  38. // keep track of the nodes hit.
  39. GuiShaderNode* nodeA = NULL;
  40. GuiShaderNode* nodeB = NULL;
  41. // keep track of the sockets.
  42. NodeInput* inSocket = NULL;
  43. NodeOutput* outSocket = NULL;
  44. };
  45. class GuiShaderEditor : public GuiControl
  46. {
  47. public:
  48. typedef GuiControl Parent;
  49. enum mouseModes { Selecting, MovingSelection, DragPanning, DragConnection, DragSelecting, DragClone };
  50. protected:
  51. // list
  52. typedef Vector<GuiShaderNode*> ShaderNodeVector;
  53. typedef Vector<NodeConnection*> ShderNodeConnections;
  54. // all nodes in this graph.
  55. ShaderNodeVector mCurrNodes;
  56. ShderNodeConnections mCurrConnections;
  57. NodeConnection* mTempConnection;
  58. // Undo
  59. SimGroup* mTrash;
  60. // view controls
  61. Point2I mViewOffset;
  62. F32 mZoomScale;
  63. // mouse interaction
  64. mouseModes mMouseDownMode;
  65. Point2I mLastMousePos;
  66. Point2I mLastDragPos;
  67. Point2I mSelectionAnchor;
  68. Point2I mDragBeginPoint;
  69. Vector<Point2I> mDragBeginPoints;
  70. bool mDragAddSelection;
  71. bool mDragMoveUndo;
  72. bool mFullBoxSelection;
  73. S32 mNodeSize;
  74. ShaderNodeVector mSelectedNodes;
  75. void renderNodes(Point2I offset, const RectI& updateRect);
  76. void renderConnections(Point2I offset, const RectI& updateRect);
  77. // functions for handling mouse events.
  78. GuiShaderNode* findHitNode(const Point2I& pt);
  79. bool findHitSocket(const Point2I& pt);
  80. U32 finishConnection(const Point2I& pt);
  81. bool hasConnection(NodeSocket* inSocket);
  82. bool hasConnection(NodeSocket* inSocket, Vector<NodeConnection*>& conn);
  83. bool hasConnection(NodeSocket* inSocket, NodeConnection*& conn);
  84. void findNodesInRect(const RectI& rect, Vector<GuiShaderNode*>& outResult);
  85. void getDragRect(RectI& box);
  86. void startDragMove(const Point2I& startPoint);
  87. void startDragRectangle(const Point2I& startPoint);
  88. void startDragClone(const Point2I& startPoint);
  89. void setMouseMode(mouseModes mode);
  90. void addNode(GuiShaderNode* newNode);
  91. public:
  92. GuiShaderEditor();
  93. DECLARE_CONOBJECT(GuiShaderEditor);
  94. DECLARE_CATEGORY("Shader Editor");
  95. DECLARE_DESCRIPTION("Implements a shader node based editor.");
  96. bool onWake() override;
  97. void onSleep() override;
  98. static void initPersistFields();
  99. bool onAdd() override;
  100. void onRemove() override;
  101. void onPreRender() override;
  102. void drawThickLine(const Point2I& pt1, const Point2I& pt2, U32 thickness = 2, ColorI col1 = ColorI(255, 255, 255), ColorI col2 = ColorI(255, 255, 255));
  103. void onRender(Point2I offset, const RectI& updateRect) override;
  104. // interaction
  105. bool onKeyDown(const GuiEvent& event) override;
  106. void onMouseDown(const GuiEvent& event) override;
  107. void onMouseUp(const GuiEvent& event) override;
  108. void onMouseDragged(const GuiEvent& event) override;
  109. void onMiddleMouseDown(const GuiEvent& event) override;
  110. void onMiddleMouseUp(const GuiEvent& event) override;
  111. void onMiddleMouseDragged(const GuiEvent& event) override;
  112. bool onMouseWheelUp(const GuiEvent& event) override;
  113. bool onMouseWheelDown(const GuiEvent& event) override;
  114. RectI getSelectionBounds();
  115. void deleteSelection();
  116. void moveSelection(const Point2I& delta, bool callback = true);
  117. void clearSelection();
  118. void cloneSelection();
  119. void addSelectionAtPoint(const Point2I& pos);
  120. void addSelection(GuiShaderNode* inNode);
  121. bool selectionContains(GuiShaderNode* inNode);
  122. void removeSelection(GuiShaderNode* inNode);
  123. void canHitSelectedNodes(bool state = true);
  124. };
  125. #endif //_GUISHADEREDITOR_H_