BsGUIElement.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. updateBounds();
  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. updateBounds();
  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. updateBounds();
  96. }
  97. }
  98. void GUIElement::_changeParentWidget(GUIWidget& widget)
  99. {
  100. if(&mParent != &widget)
  101. {
  102. mParent.unregisterElement(this);
  103. widget.registerElement(this);
  104. mParent = widget;
  105. }
  106. GUIElementBase::_changeParentWidget(widget);
  107. }
  108. Rect GUIElement::getVisibleBounds() const
  109. {
  110. Rect bounds = _getBounds();
  111. bounds.x += mStyle->margins.left;
  112. bounds.y += mStyle->margins.top;
  113. bounds.width = (UINT32)std::max(0, (INT32)bounds.width - (INT32)(mStyle->margins.left + mStyle->margins.right));
  114. bounds.height = (UINT32)std::max(0, (INT32)bounds.height - (INT32)(mStyle->margins.top + mStyle->margins.bottom));
  115. return bounds;
  116. }
  117. Rect GUIElement::getContentBounds() const
  118. {
  119. Rect bounds;
  120. bounds.x = mOffset.x + mStyle->margins.left + mStyle->contentOffset.left;
  121. bounds.y = mOffset.y + mStyle->margins.top + mStyle->contentOffset.top;
  122. bounds.width = (UINT32)std::max(0, (INT32)mWidth -
  123. (INT32)(mStyle->margins.left + mStyle->margins.right + mStyle->contentOffset.left + mStyle->contentOffset.right));
  124. bounds.height = (UINT32)std::max(0, (INT32)mHeight -
  125. (INT32)(mStyle->margins.top + mStyle->margins.bottom + mStyle->contentOffset.top + mStyle->contentOffset.bottom));
  126. return bounds;
  127. }
  128. Rect GUIElement::getContentClipRect() const
  129. {
  130. Rect contentBounds = getContentBounds();
  131. // Transform into element space so we can clip it using the element clip rectangle
  132. Int2 offsetDiff = Int2(contentBounds.x - mOffset.x, contentBounds.y - mOffset.y);
  133. Rect contentClipRect(offsetDiff.x, offsetDiff.y, contentBounds.width, contentBounds.height);
  134. contentClipRect.clip(mClipRect);
  135. // Transform into text sprite space
  136. contentClipRect.x -= offsetDiff.x;
  137. contentClipRect.y -= offsetDiff.y;
  138. return contentClipRect;
  139. }
  140. bool GUIElement::_isInBounds(const CM::Int2 position) const
  141. {
  142. Rect contentBounds = getVisibleBounds();
  143. return contentBounds.contains(position);
  144. }
  145. GUILayoutOptions GUIElement::getDefaultLayoutOptions(const GUIElementStyle* style)
  146. {
  147. GUILayoutOptions layoutOptions;
  148. layoutOptions.fixedWidth = style->fixedWidth;
  149. layoutOptions.fixedHeight = style->fixedHeight;
  150. layoutOptions.width = style->width;
  151. layoutOptions.height = style->height;
  152. layoutOptions.minWidth = style->minWidth;
  153. layoutOptions.maxWidth = style->maxWidth;
  154. layoutOptions.minHeight = style->minHeight;
  155. layoutOptions.maxHeight = style->maxHeight;
  156. return layoutOptions;
  157. }
  158. void GUIElement::destroy(GUIElement* element)
  159. {
  160. element->mParent.unregisterElement(element);
  161. cm_delete<PoolAlloc>(element);
  162. }
  163. }