BsGUIElementBase.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include "BsGUIElementBase.h"
  2. #include "BsGUILayout.h"
  3. #include "BsGUILayoutX.h"
  4. #include "BsGUILayoutY.h"
  5. #include "CmException.h"
  6. using namespace CamelotFramework;
  7. namespace BansheeEngine
  8. {
  9. GUIElementBase::GUIElementBase()
  10. :mIsDirty(true)
  11. {
  12. }
  13. bool GUIElementBase::_isContentDirty() const
  14. {
  15. if((mIsDirty & 0x01) != 0)
  16. return true;
  17. for(auto& child : mChildren)
  18. {
  19. if(child->_isContentDirty())
  20. return true;
  21. }
  22. return false;
  23. }
  24. bool GUIElementBase::_isMeshDirty() const
  25. {
  26. return (mIsDirty & 0x02) != 0;
  27. }
  28. void GUIElementBase::markContentAsDirty()
  29. {
  30. mIsDirty |= 0x01;
  31. }
  32. void GUIElementBase::markMeshAsDirty()
  33. {
  34. mIsDirty |= 0x02;
  35. }
  36. void GUIElementBase::_updateLayout(UINT32 x, UINT32 y, UINT32 width, UINT32 height, UINT8 widgetDepth, UINT16 areaDepth)
  37. {
  38. _updateOptimalLayoutSizes(); // We calculate optimal sizes of all layouts as a pre-processing step, as they are requested often during update
  39. _updateLayoutInternal(x, y, width, height, widgetDepth, areaDepth);
  40. }
  41. void GUIElementBase::_updateOptimalLayoutSizes()
  42. {
  43. for(auto& child : mChildren)
  44. {
  45. child->_updateOptimalLayoutSizes();
  46. }
  47. }
  48. void GUIElementBase::_updateLayoutInternal(CM::UINT32 x, CM::UINT32 y, CM::UINT32 width, CM::UINT32 height, CM::UINT8 widgetDepth, CM::UINT16 areaDepth)
  49. {
  50. for(auto& child : mChildren)
  51. {
  52. child->_updateLayoutInternal(x, y, width, height, widgetDepth, areaDepth);
  53. }
  54. }
  55. GUILayout& GUIElementBase::addLayoutXInternal()
  56. {
  57. GUILayoutX* entry = cm_new<GUILayoutX, PoolAlloc>();
  58. mChildren.push_back(entry);
  59. markContentAsDirty();
  60. return *entry;
  61. }
  62. GUILayout& GUIElementBase::addLayoutYInternal()
  63. {
  64. GUILayoutY* entry = cm_new<GUILayoutY, PoolAlloc>();
  65. mChildren.push_back(entry);
  66. markContentAsDirty();
  67. return *entry;
  68. }
  69. void GUIElementBase::removeLayoutInternal(GUILayout& layout)
  70. {
  71. bool foundElem = false;
  72. for(auto iter = mChildren.begin(); iter != mChildren.end(); ++iter)
  73. {
  74. GUIElementBase* child = *iter;
  75. if(child->_getType() == GUIElementBase::Type::Layout && child == &layout)
  76. {
  77. cm_delete<PoolAlloc>(child);
  78. mChildren.erase(iter);
  79. foundElem = true;
  80. markContentAsDirty();
  81. break;
  82. }
  83. }
  84. if(!foundElem)
  85. CM_EXCEPT(InvalidParametersException, "Provided element is not a part of this layout.");
  86. }
  87. GUILayout& GUIElementBase::insertLayoutXInternal(UINT32 idx)
  88. {
  89. if(idx < 0 || idx >= (UINT32)mChildren.size())
  90. CM_EXCEPT(InvalidParametersException, "Index out of range: " + toString(idx) + ". Valid range: 0 .. " + toString((UINT32)mChildren.size()));
  91. GUILayoutX* entry = cm_new<GUILayoutX, PoolAlloc>();
  92. mChildren.insert(mChildren.begin() + idx, entry);
  93. markContentAsDirty();
  94. return *entry;
  95. }
  96. GUILayout& GUIElementBase::insertLayoutYInternal(UINT32 idx)
  97. {
  98. if(idx < 0 || idx >= (UINT32)mChildren.size())
  99. CM_EXCEPT(InvalidParametersException, "Index out of range: " + toString(idx) + ". Valid range: 0 .. " + toString((UINT32)mChildren.size()));
  100. GUILayoutY* entry = cm_new<GUILayoutY, PoolAlloc>();
  101. mChildren.insert(mChildren.begin() + idx, entry);
  102. markContentAsDirty();
  103. return *entry;
  104. }
  105. }