BsGUIElement.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #include "BsGUIElement.h"
  2. #include "BsGUIWidget.h"
  3. #include "BsGUISkin.h"
  4. #include "BsGUILayout.h"
  5. #include "BsGUIManager.h"
  6. #include "CmException.h"
  7. using namespace CamelotFramework;
  8. namespace BansheeEngine
  9. {
  10. GUIElement::GUIElement(GUIWidget& parent, const GUIElementStyle* style, const GUILayoutOptions& layoutOptions, bool acceptsKeyboardFocus)
  11. :mParent(&parent), mLayoutOptions(layoutOptions), mWidth(0), mHeight(0), mDepth(0), mStyle(style),
  12. mAcceptsKeyboardFocus(acceptsKeyboardFocus), mIsDestroyed(false)
  13. {
  14. mParent->registerElement(this);
  15. }
  16. GUIElement::~GUIElement()
  17. {
  18. if(mParentElement != nullptr)
  19. mParentElement->_unregisterChildElement(this);
  20. }
  21. void GUIElement::updateRenderElements()
  22. {
  23. updateRenderElementsInternal();
  24. _markAsClean();
  25. }
  26. void GUIElement::updateRenderElementsInternal()
  27. {
  28. updateClippedBounds();
  29. }
  30. void GUIElement::setLayoutOptions(const GUILayoutOptions& layoutOptions)
  31. {
  32. if(layoutOptions.maxWidth < layoutOptions.minWidth)
  33. {
  34. CM_EXCEPT(InvalidParametersException, "Maximum width is less than minimum width! Max width: " +
  35. toString(layoutOptions.maxWidth) + ". Min width: " + toString(layoutOptions.minWidth));
  36. }
  37. if(layoutOptions.maxHeight < layoutOptions.minHeight)
  38. {
  39. CM_EXCEPT(InvalidParametersException, "Maximum height is less than minimum height! Max height: " +
  40. toString(layoutOptions.maxHeight) + ". Min height: " + toString(layoutOptions.minHeight));
  41. }
  42. mLayoutOptions = layoutOptions;
  43. }
  44. bool GUIElement::mouseEvent(const GUIMouseEvent& ev)
  45. {
  46. return false;
  47. }
  48. bool GUIElement::textInputEvent(const GUITextInputEvent& ev)
  49. {
  50. return false;
  51. }
  52. bool GUIElement::commandEvent(const GUICommandEvent& ev)
  53. {
  54. return false;
  55. }
  56. void GUIElement::_setWidgetDepth(UINT8 depth)
  57. {
  58. mDepth |= depth << 24;
  59. markMeshAsDirty();
  60. }
  61. void GUIElement::_setAreaDepth(UINT16 depth)
  62. {
  63. mDepth |= depth << 8;
  64. markMeshAsDirty();
  65. }
  66. void GUIElement::_setOffset(const CM::Vector2I& offset)
  67. {
  68. if(mOffset != offset)
  69. {
  70. markMeshAsDirty();
  71. mOffset = offset;
  72. updateClippedBounds();
  73. }
  74. }
  75. void GUIElement::_setWidth(UINT32 width)
  76. {
  77. if(mWidth != width)
  78. markContentAsDirty();
  79. mWidth = width;
  80. }
  81. void GUIElement::_setHeight(UINT32 height)
  82. {
  83. if(mHeight != height)
  84. markContentAsDirty();
  85. mHeight = height;
  86. }
  87. void GUIElement::_setClipRect(const CM::RectI& clipRect)
  88. {
  89. if(mClipRect != clipRect)
  90. {
  91. markMeshAsDirty();
  92. mClipRect = clipRect;
  93. updateClippedBounds();
  94. }
  95. }
  96. void GUIElement::_changeParentWidget(GUIWidget* widget)
  97. {
  98. if(mParent != widget)
  99. {
  100. if(mParent != nullptr)
  101. mParent->unregisterElement(this);
  102. if(widget != nullptr)
  103. widget->registerElement(this);
  104. mParent = widget;
  105. }
  106. GUIElementBase::_changeParentWidget(widget);
  107. }
  108. RectI GUIElement::getBounds() const
  109. {
  110. return RectI(mOffset.x, mOffset.y, mWidth, mHeight);
  111. }
  112. RectI GUIElement::getVisibleBounds() const
  113. {
  114. RectI bounds = _getClippedBounds();
  115. bounds.x += mStyle->margins.left;
  116. bounds.y += mStyle->margins.top;
  117. bounds.width = (UINT32)std::max(0, (INT32)bounds.width - (INT32)(mStyle->margins.left + mStyle->margins.right));
  118. bounds.height = (UINT32)std::max(0, (INT32)bounds.height - (INT32)(mStyle->margins.top + mStyle->margins.bottom));
  119. return bounds;
  120. }
  121. RectI GUIElement::getContentBounds() const
  122. {
  123. RectI bounds;
  124. bounds.x = mOffset.x + mStyle->margins.left + mStyle->contentOffset.left;
  125. bounds.y = mOffset.y + mStyle->margins.top + mStyle->contentOffset.top;
  126. bounds.width = (UINT32)std::max(0, (INT32)mWidth -
  127. (INT32)(mStyle->margins.left + mStyle->margins.right + mStyle->contentOffset.left + mStyle->contentOffset.right));
  128. bounds.height = (UINT32)std::max(0, (INT32)mHeight -
  129. (INT32)(mStyle->margins.top + mStyle->margins.bottom + mStyle->contentOffset.top + mStyle->contentOffset.bottom));
  130. return bounds;
  131. }
  132. RectI GUIElement::getContentClipRect() const
  133. {
  134. RectI contentBounds = getContentBounds();
  135. // Transform into element space so we can clip it using the element clip rectangle
  136. Vector2I offsetDiff = Vector2I(contentBounds.x - mOffset.x, contentBounds.y - mOffset.y);
  137. RectI contentClipRect(offsetDiff.x, offsetDiff.y, contentBounds.width, contentBounds.height);
  138. contentClipRect.clip(mClipRect);
  139. // Transform into content sprite space
  140. contentClipRect.x -= offsetDiff.x;
  141. contentClipRect.y -= offsetDiff.y;
  142. return contentClipRect;
  143. }
  144. bool GUIElement::_isInBounds(const CM::Vector2I position) const
  145. {
  146. RectI contentBounds = getVisibleBounds();
  147. return contentBounds.contains(position);
  148. }
  149. void GUIElement::destroy(GUIElement* element)
  150. {
  151. if(element->mIsDestroyed)
  152. return;
  153. if(element->mParent != nullptr)
  154. element->mParent->unregisterElement(element);
  155. element->mIsDestroyed = true;
  156. GUIManager::instance().queueForDestroy(element);
  157. }
  158. }