shaderNode.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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/nodes/shaderNode.h"
  24. #include "gui/core/guiCanvas.h"
  25. IMPLEMENT_CONOBJECT(GuiShaderNode);
  26. ConsoleDocClass(GuiShaderNode,
  27. "@brief Base class for all nodes to derive from.\n\n"
  28. "Editor use only.\n\n"
  29. "@internal"
  30. );
  31. GuiShaderNode::GuiShaderNode()
  32. {
  33. VECTOR_SET_ASSOCIATION(mInputNodes);
  34. VECTOR_SET_ASSOCIATION(mOutputNodes);
  35. mTitle = "Default Node";
  36. mSelected = false;
  37. mNodeType = NodeTypes::Default;
  38. GuiControlProfile* profile = NULL;
  39. if (Sim::findObject("GuiShaderEditorProfile", profile))
  40. setControlProfile(profile);
  41. mInputNodes.push_back(new NodeInput("RGBA", DataDimensions::Dynamic));
  42. mInputNodes.push_back(new NodeInput("RGBA", DataDimensions::Dynamic));
  43. mInputNodes.push_back(new NodeInput("RGBA", DataDimensions::Dynamic));
  44. mInputNodes.push_back(new NodeInput("RGBA", DataDimensions::Dynamic));
  45. mOutputNodes.push_back(new NodeOutput("RGBA", DataDimensions::Dynamic));
  46. mOutputNodes.push_back(new NodeOutput("RGBA", DataDimensions::Dynamic));
  47. mOutputNodes.push_back(new NodeOutput("RGBA", DataDimensions::Dynamic));
  48. mOutputNodes.push_back(new NodeOutput("RGBA", DataDimensions::Dynamic));
  49. // fixed extent for all nodes, only height should be changed
  50. setExtent(210, 35);
  51. mPrevNodeSize = -1;
  52. }
  53. bool GuiShaderNode::onWake()
  54. {
  55. if (!Parent::onWake())
  56. return false;
  57. return true;
  58. }
  59. void GuiShaderNode::onSleep()
  60. {
  61. Parent::onSleep();
  62. }
  63. void GuiShaderNode::initPersistFields()
  64. {
  65. docsURL;
  66. Parent::initPersistFields();
  67. }
  68. bool GuiShaderNode::onAdd()
  69. {
  70. if (!Parent::onAdd())
  71. return false;
  72. return true;
  73. }
  74. void GuiShaderNode::onRemove()
  75. {
  76. Parent::onRemove();
  77. }
  78. void GuiShaderNode::renderNode(Point2I offset, const RectI& updateRect, const S32 nodeSize)
  79. {
  80. if (!mProfile)
  81. return Parent::onRender(offset, updateRect);
  82. GFXDrawUtil* drawer = GFX->getDrawUtil();
  83. // draw background.
  84. // Get our rect.
  85. RectI winRect;
  86. winRect.point = offset;
  87. winRect.extent = getExtent();
  88. ColorI border = mProfile->mBorderColor;
  89. if (mSelected)
  90. border = mProfile->mBorderColorSEL;
  91. drawer->drawRoundedRect(15.0f, winRect, mProfile->mFillColor, 3.0f, border);
  92. // draw header
  93. ColorI header(50, 50, 50, 128);
  94. switch (mNodeType)
  95. {
  96. case NodeTypes::Default:
  97. header = ColorI(128, 50, 128, 128);
  98. break;
  99. case NodeTypes::Uniform:
  100. header = ColorI(50, 100, 128, 128);
  101. break;
  102. case NodeTypes::Input:
  103. header = ColorI(128, 100, 50, 128);
  104. break;
  105. case NodeTypes::Output:
  106. header = ColorI(50, 100, 50, 128);
  107. break;
  108. case NodeTypes::TextureSampler:
  109. header = ColorI(50, 50, 128, 128);
  110. break;
  111. case NodeTypes::MathOperation:
  112. header = ColorI(128, 0, 128, 128);
  113. break;
  114. case NodeTypes::Procedural:
  115. header = ColorI(128, 100, 0, 128);
  116. break;
  117. case NodeTypes::Generator:
  118. header = ColorI(0, 100, 128, 128);
  119. break;
  120. default:
  121. header = ColorI(128, 0, 0, 128);
  122. break;
  123. }
  124. RectI headRect;
  125. U32 headerSize = 30;
  126. headRect.point = offset;
  127. headRect.extent = Point2I(getExtent().x, headerSize);
  128. drawer->drawRoundedRect(15.0f, headRect, header);
  129. // draw header text.
  130. U32 strWidth = mProfile->mFont->getStrWidth(mTitle.c_str());
  131. Point2I headerPos = Point2I((getExtent().x / 2) - (strWidth / 2), (headerSize / 2) - (mProfile->mFont->getFontSize() / 2));
  132. drawer->setBitmapModulation(mProfile->mFontColor);
  133. drawer->drawText(mProfile->mFont, headerPos + offset, mTitle);
  134. drawer->clearBitmapModulation();
  135. if (mInputNodes.size() > 0 || mOutputNodes.size() > 0)
  136. {
  137. U32 textPadX = nodeSize, textPadY = mProfile->mFont->getFontSize() + (nodeSize / 2);
  138. Point2I slotPos(textPadX, headerSize + (nodeSize / 2));
  139. drawer->setBitmapModulation(mProfile->mFontColor);
  140. for (NodeInput* input : mInputNodes)
  141. {
  142. drawer->drawText(mProfile->mFont, slotPos + offset, input->name);
  143. if (input->pos == Point2I::Zero || mPrevNodeSize != nodeSize)
  144. input->pos = Point2I(-(nodeSize / 2) + 1, slotPos.y + ((mProfile->mFont->getFontSize() / 2) - (nodeSize / 2)));
  145. slotPos.y += textPadY;
  146. }
  147. U32 inputY = slotPos.y;
  148. slotPos = Point2I(getExtent().x, headerSize + (nodeSize / 2));
  149. for (NodeOutput* output : mOutputNodes)
  150. {
  151. strWidth = mProfile->mFont->getStrWidth(output->name.c_str());
  152. slotPos.x = getExtent().x - strWidth - textPadX;
  153. drawer->drawText(mProfile->mFont, slotPos + offset, output->name);
  154. if (output->pos == Point2I::Zero || mPrevNodeSize != nodeSize)
  155. output->pos = Point2I(getExtent().x - (nodeSize / 2) - 1 , slotPos.y + ((mProfile->mFont->getFontSize() / 2) - (nodeSize / 2)));
  156. slotPos.y += textPadY;
  157. }
  158. drawer->clearBitmapModulation();
  159. U32 outputY = slotPos.y;
  160. if (getExtent().y < slotPos.y || mPrevNodeSize != nodeSize)
  161. setExtent(Point2I(getExtent().x, mMax(inputY, outputY)));
  162. mPrevNodeSize = nodeSize;
  163. }
  164. }
  165. void GuiShaderNode::write(Stream& stream, U32 tabStop, U32 flags)
  166. {
  167. }
  168. void GuiShaderNode::read(Stream& stream)
  169. {
  170. }