BsGUIElement.cpp 5.6 KB

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