2
0

guiShaderNode.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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/guiShaderNode.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. // fixed extent for all nodes, only height should be changed
  42. setExtent(180, 35);
  43. mPrevNodeSize = -1;
  44. }
  45. bool GuiShaderNode::onWake()
  46. {
  47. if (!Parent::onWake())
  48. return false;
  49. return true;
  50. }
  51. void GuiShaderNode::onSleep()
  52. {
  53. Parent::onSleep();
  54. }
  55. void GuiShaderNode::initPersistFields()
  56. {
  57. docsURL;
  58. Parent::initPersistFields();
  59. }
  60. bool GuiShaderNode::onAdd()
  61. {
  62. if (!Parent::onAdd())
  63. return false;
  64. return true;
  65. }
  66. void GuiShaderNode::onRemove()
  67. {
  68. Parent::onRemove();
  69. }
  70. void GuiShaderNode::renderNode(Point2I offset, const RectI& updateRect, const S32 nodeSize)
  71. {
  72. if (!mProfile)
  73. return Parent::onRender(offset, updateRect);
  74. GFXDrawUtil* drawer = GFX->getDrawUtil();
  75. // draw background.
  76. // Get our rect.
  77. RectI winRect;
  78. winRect.point = offset;
  79. winRect.extent = getExtent();
  80. ColorI border = mProfile->mBorderColor;
  81. if (mSelected)
  82. border = mProfile->mBorderColorSEL;
  83. drawer->drawRoundedRect(15.0f, winRect, mProfile->mFillColor, 5.0f, border);
  84. // draw header
  85. ColorI header(50, 50, 50, 128);
  86. switch (mNodeType)
  87. {
  88. case NodeTypes::Default:
  89. header = ColorI(128, 50, 128, 128);
  90. break;
  91. case NodeTypes::Uniform:
  92. header = ColorI(50, 100, 128, 128);
  93. break;
  94. case NodeTypes::Input:
  95. header = ColorI(128, 100, 50, 128);
  96. break;
  97. case NodeTypes::Output:
  98. header = ColorI(50, 100, 50, 128);
  99. break;
  100. case NodeTypes::TextureSampler:
  101. header = ColorI(50, 50, 128, 128);
  102. break;
  103. case NodeTypes::MathOperation:
  104. header = ColorI(128, 0, 128, 128);
  105. break;
  106. case NodeTypes::Procedural:
  107. header = ColorI(128, 100, 0, 128);
  108. break;
  109. case NodeTypes::Generator:
  110. header = ColorI(0, 100, 128, 128);
  111. break;
  112. default:
  113. header = ColorI(128, 0, 0, 128);
  114. break;
  115. }
  116. RectI headRect;
  117. U32 headerSize = 30;
  118. headRect.point = offset;
  119. headRect.extent = Point2I(getExtent().x, headerSize);
  120. drawer->drawRoundedRect(15.0f, headRect, header);
  121. // draw header text.
  122. U32 strWidth = mProfile->mFont->getStrWidth(mTitle.c_str());
  123. Point2I headerPos = Point2I((getExtent().x / 2) - (strWidth / 2), (headerSize / 2) - (mProfile->mFont->getFontSize() / 2));
  124. drawer->setBitmapModulation(mProfile->mFontColor);
  125. drawer->drawText(mProfile->mFont, headerPos + offset, mTitle);
  126. drawer->clearBitmapModulation();
  127. if (mInputNodes.size() > 0 || mOutputNodes.size() > 0)
  128. {
  129. U32 textPadX = nodeSize, textPadY = mProfile->mFont->getFontSize() + (nodeSize / 2);
  130. Point2I slotPos(textPadX, headerSize + (nodeSize / 2));
  131. drawer->setBitmapModulation(mProfile->mFontColor);
  132. for (NodeInput* input : mInputNodes)
  133. {
  134. drawer->drawText(mProfile->mFont, slotPos + offset, input->name);
  135. if (input->pos == Point2I::Zero || mPrevNodeSize != nodeSize)
  136. input->pos = Point2I(-(nodeSize / 2) + 1, slotPos.y + ((mProfile->mFont->getFontSize() / 2) - (nodeSize / 2)));
  137. slotPos.y += textPadY;
  138. }
  139. U32 inputY = slotPos.y;
  140. slotPos = Point2I(getExtent().x, headerSize + (nodeSize / 2));
  141. for (NodeOutput* output : mOutputNodes)
  142. {
  143. strWidth = mProfile->mFont->getStrWidth(output->name.c_str());
  144. slotPos.x = getExtent().x - strWidth - textPadX;
  145. drawer->drawText(mProfile->mFont, slotPos + offset, output->name);
  146. if (output->pos == Point2I::Zero || mPrevNodeSize != nodeSize)
  147. output->pos = Point2I(getExtent().x - (nodeSize / 2) - 1 , slotPos.y + ((mProfile->mFont->getFontSize() / 2) - (nodeSize / 2)));
  148. slotPos.y += textPadY;
  149. }
  150. drawer->clearBitmapModulation();
  151. U32 outputY = slotPos.y;
  152. if (getExtent().y < slotPos.y || mPrevNodeSize != nodeSize)
  153. setExtent(Point2I(getExtent().x, mMax(inputY, outputY)));
  154. mPrevNodeSize = nodeSize;
  155. }
  156. }
  157. void GuiShaderNode::write(Stream& stream, U32 tabStop, U32 flags)
  158. {
  159. }
  160. void GuiShaderNode::read(Stream& stream)
  161. {
  162. }