CmGUIWidget.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #include "CmGUIWidget.h"
  2. #include "CmGUIManager.h"
  3. #include "CmGUISkin.h"
  4. #include "CmGUILabel.h"
  5. #include "CmDeferredRenderContext.h"
  6. #include "CmMaterial.h"
  7. #include "CmPass.h"
  8. #include "CmMesh.h"
  9. #include "CmCamera.h"
  10. #include "CmViewport.h"
  11. #include "CmSceneObject.h"
  12. namespace CamelotEngine
  13. {
  14. GUISkin GUIWidget::DefaultSkin;
  15. GUIWidget::GUIWidget(const HSceneObject& parent)
  16. :Overlay(parent), mSkin(nullptr)
  17. {
  18. GUIManager::instance().registerWidget(this);
  19. }
  20. GUIWidget::~GUIWidget()
  21. {
  22. GUIManager::instance().unregisterWidget(this);
  23. for(auto& elem : mElements)
  24. {
  25. CM_DELETE(elem, GUIElement, GUIAlloc);
  26. }
  27. mElements.clear();
  28. }
  29. GUILabel* GUIWidget::addLabel(const String& text, UINT32 fixedWidth, UINT32 fixedHeight, bool wordWrap, TextHorzAlign horzAlign, TextVertAlign vertAlign)
  30. {
  31. GUILabel* label = CM_NEW(GUILabel, GUIAlloc) GUILabel(this, text, getGUISkin(), fixedWidth, fixedHeight, wordWrap, horzAlign, vertAlign);
  32. mElements.push_back(label);
  33. return label;
  34. }
  35. GUILabel* GUIWidget::addLabel(const String& text, TextHorzAlign horzAlign, TextVertAlign vertAlign)
  36. {
  37. return addLabel(text, 0, 0, false, horzAlign, vertAlign);
  38. }
  39. void GUIWidget::setSkin(const GUISkin* skin)
  40. {
  41. mSkin = skin;
  42. }
  43. const GUISkin* GUIWidget::getGUISkin() const
  44. {
  45. if(mSkin != nullptr)
  46. return mSkin;
  47. else
  48. return &DefaultSkin;
  49. }
  50. void GUIWidget::mouseEvent(const GUIMouseEvent& ev)
  51. {
  52. }
  53. void GUIWidget::updateMeshes() const
  54. {
  55. struct TempMeshData
  56. {
  57. TempMeshData()
  58. :numQuads(0), quadOffset(0), vertices(nullptr),
  59. uvs(nullptr), indices(nullptr)
  60. { }
  61. UINT32 numQuads;
  62. UINT32 quadOffset;
  63. Vector2* vertices;
  64. Vector2* uvs;
  65. UINT32* indices;
  66. HMaterial material;
  67. std::shared_ptr<MeshData> meshData;
  68. };
  69. std::unordered_map<UINT64, TempMeshData> meshDataPerRenderElement;
  70. // Group meshes based on used materials
  71. // Determine mesh sizes per group
  72. for(auto& elem : mElements)
  73. {
  74. UINT32 numRenderElems = elem->getNumRenderElements();
  75. for(UINT32 i = 0; i < numRenderElems; i++)
  76. {
  77. const HMaterial& mat = elem->getMaterial(i);
  78. UINT64 meshGroup = mat->getInternalID(); // TODO - I group based on material ID. So if two widgets used exact copies of the same material
  79. // this system won't detect it. Find a better way of determining material similarity?
  80. UINT32 numQuads = elem->getNumQuads(i);
  81. meshDataPerRenderElement[meshGroup].numQuads += numQuads;
  82. meshDataPerRenderElement[meshGroup].material = mat;
  83. }
  84. }
  85. // Allocate buffers for each group
  86. UINT32 numMeshes = 0;
  87. for(auto& renderElem : meshDataPerRenderElement)
  88. {
  89. renderElem.second.meshData = std::shared_ptr<MeshData>(CM_NEW(MeshData, PoolAlloc) MeshData(),
  90. &MemAllocDeleter<MeshData, PoolAlloc>::deleter);
  91. renderElem.second.vertices = renderElem.second.meshData->addPositionsVec2(renderElem.second.numQuads * 4);
  92. renderElem.second.uvs = renderElem.second.meshData->addUV0(renderElem.second.numQuads * 4);
  93. renderElem.second.indices = renderElem.second.meshData->addIndices32(renderElem.second.numQuads * 6);
  94. numMeshes++;
  95. }
  96. // TODO - Sorting from scratch every time is not optimal.
  97. // If more performance is needed, try re-sorting only modified elements
  98. // Sort so that farthest away elements get drawn first (needed due to transparency)
  99. std::vector<GUIElement*> sortedElements = mElements;
  100. std::sort(sortedElements.begin(), sortedElements.end(),
  101. [](GUIElement* a, GUIElement* b)
  102. {
  103. return a->getDepth() > b->getDepth();
  104. });
  105. // Fill buffers for each group
  106. for(auto& elem : sortedElements)
  107. {
  108. UINT32 numRenderElems = elem->getNumRenderElements();
  109. for(UINT32 i = 0; i < numRenderElems; i++)
  110. {
  111. const HMaterial& mat = elem->getMaterial(i);
  112. UINT64 meshGroup = mat->getInternalID();
  113. Vector2* vertices = meshDataPerRenderElement[meshGroup].vertices;
  114. Vector2* uvs = meshDataPerRenderElement[meshGroup].uvs;
  115. UINT32* indices = meshDataPerRenderElement[meshGroup].indices;
  116. UINT32 startingQuad = meshDataPerRenderElement[meshGroup].quadOffset;
  117. UINT32 maxNumQuads = meshDataPerRenderElement[meshGroup].numQuads;
  118. elem->fillBuffer(vertices, uvs, indices, startingQuad, maxNumQuads, i);
  119. UINT32 numQuads = elem->getNumQuads(i);
  120. meshDataPerRenderElement[meshGroup].quadOffset += numQuads;
  121. }
  122. }
  123. // Update meshes
  124. for(UINT32 i = (UINT32)mCachedMeshes.size(); i < numMeshes; i++)
  125. {
  126. HMesh newMesh = Mesh::create();
  127. mCachedMeshes.push_back(newMesh);
  128. newMesh.waitUntilLoaded();
  129. }
  130. while((UINT32)mCachedMeshes.size() > numMeshes && (UINT32)mCachedMeshes.size() > 0)
  131. {
  132. mCachedMeshes.erase(mCachedMeshes.end() - 1); // TODO: Destroying meshes as soon as they're not used might be a perf. penalty?
  133. // Maybe instead pool the meshes and only actually remove them when a certain number of unused ones exists.
  134. }
  135. mCachedMaterials.resize(numMeshes);
  136. UINT32 meshIdx = 0;
  137. for(auto& renderElem : meshDataPerRenderElement)
  138. {
  139. mCachedMeshes[meshIdx]->setMeshData(renderElem.second.meshData);
  140. mCachedMaterials[meshIdx] = renderElem.second.material;
  141. meshIdx++;
  142. }
  143. updateBounds();
  144. }
  145. void GUIWidget::updateBounds() const
  146. {
  147. mCachedBounds.clear();
  148. const Matrix4& worldTfrm = SO()->getWorldTfrm();
  149. for(auto& elem : mElements)
  150. {
  151. ORect elemBounds(elem->getBounds());
  152. elemBounds.applyTransform(worldTfrm);
  153. mCachedBounds.push_back(std::make_pair(elemBounds, elem));
  154. }
  155. }
  156. void GUIWidget::render(const Camera* camera, DeferredRenderContextPtr& renderContext) const
  157. {
  158. // Mesh is re-created every frame. There might be a better approach that only recreates it upon change,
  159. // but for now it seems like too much hassle for something like GUI that is pretty dynamic anyway.
  160. updateMeshes();
  161. // Render the meshes
  162. UINT32 meshIdx = 0;
  163. for(auto& mesh : mCachedMeshes)
  164. {
  165. HMaterial material = mCachedMaterials[meshIdx];
  166. // TODO - Possible optimization. I currently divide by width/height inside the shader, while it
  167. // might be more optimal to just scale the mesh as the resolution changes?
  168. material->setFloat("halfViewportWidth", camera->getViewport()->getWidth() * 0.5f);
  169. material->setFloat("halfViewportHeight", camera->getViewport()->getHeight() * 0.5f);
  170. material->setMat4("worldTransform", SO()->getWorldTfrm());
  171. if(material == nullptr || !material.isLoaded())
  172. continue;
  173. if(mesh == nullptr || !mesh.isLoaded())
  174. continue;
  175. for(UINT32 i = 0; i < material->getNumPasses(); i++)
  176. {
  177. PassPtr pass = material->getPass(i);
  178. pass->activate(renderContext);
  179. PassParametersPtr paramsPtr = material->getPassParameters(i);
  180. pass->bindParameters(renderContext, paramsPtr);
  181. renderContext->render(mesh->getRenderOperation());
  182. }
  183. meshIdx++;
  184. }
  185. }
  186. }