BsGUIElement.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. #include "BsGUIElement.h"
  2. #include "BsGUIWidget.h"
  3. #include "BsGUISkin.h"
  4. #include "BsGUILayout.h"
  5. #include "BsGUIManager.h"
  6. #include "BsException.h"
  7. #include "BsGUILayoutUtility.h"
  8. namespace BansheeEngine
  9. {
  10. GUIElement::GUIElement(const String& styleName, const GUILayoutOptions& layoutOptions)
  11. :mLayoutOptions(layoutOptions), mWidth(0), mHeight(0), mDepth(0), mStyle(nullptr),
  12. mIsDestroyed(false), mStyleName(styleName)
  13. {
  14. _refreshStyle();
  15. }
  16. GUIElement::~GUIElement()
  17. { }
  18. void GUIElement::_updateRenderElements()
  19. {
  20. updateRenderElementsInternal();
  21. _markAsClean();
  22. }
  23. void GUIElement::updateRenderElementsInternal()
  24. {
  25. updateClippedBounds();
  26. }
  27. void GUIElement::setLayoutOptions(const GUILayoutOptions& layoutOptions)
  28. {
  29. if(layoutOptions.maxWidth < layoutOptions.minWidth)
  30. {
  31. BS_EXCEPT(InvalidParametersException, "Maximum width is less than minimum width! Max width: " +
  32. toString(layoutOptions.maxWidth) + ". Min width: " + toString(layoutOptions.minWidth));
  33. }
  34. if(layoutOptions.maxHeight < layoutOptions.minHeight)
  35. {
  36. BS_EXCEPT(InvalidParametersException, "Maximum height is less than minimum height! Max height: " +
  37. toString(layoutOptions.maxHeight) + ". Min height: " + toString(layoutOptions.minHeight));
  38. }
  39. mLayoutOptions = layoutOptions;
  40. }
  41. void GUIElement::setStyle(const String& styleName)
  42. {
  43. mStyleName = styleName;
  44. _refreshStyle();
  45. }
  46. bool GUIElement::_mouseEvent(const GUIMouseEvent& ev)
  47. {
  48. return false;
  49. }
  50. bool GUIElement::_textInputEvent(const GUITextInputEvent& ev)
  51. {
  52. return false;
  53. }
  54. bool GUIElement::_commandEvent(const GUICommandEvent& ev)
  55. {
  56. return false;
  57. }
  58. bool GUIElement::_virtualButtonEvent(const GUIVirtualButtonEvent& ev)
  59. {
  60. return false;
  61. }
  62. void GUIElement::_setWidgetDepth(UINT8 depth)
  63. {
  64. mDepth |= depth << 24;
  65. markMeshAsDirty();
  66. }
  67. void GUIElement::_setAreaDepth(UINT16 depth)
  68. {
  69. mDepth |= depth << 8;
  70. markMeshAsDirty();
  71. }
  72. void GUIElement::_setElementDepth(UINT8 depth)
  73. {
  74. mDepth |= depth;
  75. markMeshAsDirty();
  76. }
  77. void GUIElement::setOffset(const Vector2I& offset)
  78. {
  79. if(mOffset != offset)
  80. {
  81. markMeshAsDirty();
  82. mOffset = offset;
  83. updateClippedBounds();
  84. }
  85. }
  86. void GUIElement::setWidth(UINT32 width)
  87. {
  88. if(mWidth != width)
  89. markContentAsDirty();
  90. mWidth = width;
  91. }
  92. void GUIElement::setHeight(UINT32 height)
  93. {
  94. if(mHeight != height)
  95. markContentAsDirty();
  96. mHeight = height;
  97. }
  98. void GUIElement::_setClipRect(const Rect2I& clipRect)
  99. {
  100. if(mClipRect != clipRect)
  101. {
  102. markMeshAsDirty();
  103. mClipRect = clipRect;
  104. updateClippedBounds();
  105. }
  106. }
  107. void GUIElement::_changeParentWidget(GUIWidget* widget)
  108. {
  109. bool doRefreshStyle = false;
  110. if(mParentWidget != widget)
  111. {
  112. if(mParentWidget != nullptr)
  113. mParentWidget->unregisterElement(this);
  114. if(widget != nullptr)
  115. widget->registerElement(this);
  116. doRefreshStyle = true;
  117. }
  118. GUIElementBase::_changeParentWidget(widget);
  119. if(doRefreshStyle)
  120. _refreshStyle();
  121. }
  122. const RectOffset& GUIElement::_getPadding() const
  123. {
  124. if(mStyle != nullptr)
  125. return mStyle->padding;
  126. else
  127. {
  128. static RectOffset padding;
  129. return padding;
  130. }
  131. }
  132. Vector2I GUIElement::_calculateOptimalLayoutSize() const
  133. {
  134. if (mIsDisabled)
  135. return Vector2I(0, 0);
  136. const GUILayoutOptions& layoutOptions = _getLayoutOptions();
  137. UINT32 optimalWidth = 0;
  138. UINT32 optimalHeight = 0;
  139. Vector2I optimalSize;
  140. if (!layoutOptions.fixedWidth || !layoutOptions.fixedHeight)
  141. optimalSize = _getOptimalSize();
  142. if (layoutOptions.fixedHeight)
  143. {
  144. optimalHeight = layoutOptions.height;
  145. }
  146. else
  147. {
  148. optimalHeight = optimalSize.y;
  149. if (layoutOptions.minHeight > 0)
  150. optimalHeight = std::max(layoutOptions.minHeight, optimalHeight);
  151. if (layoutOptions.maxHeight > 0)
  152. optimalHeight = std::min(layoutOptions.maxHeight, optimalHeight);
  153. }
  154. if (layoutOptions.fixedWidth)
  155. optimalWidth = layoutOptions.width;
  156. else
  157. {
  158. optimalWidth = optimalSize.x;
  159. if (layoutOptions.minWidth > 0)
  160. optimalWidth = std::max(layoutOptions.minWidth, optimalWidth);
  161. if (layoutOptions.maxWidth > 0)
  162. optimalWidth = std::min(layoutOptions.maxWidth, optimalWidth);
  163. }
  164. return Vector2I(optimalWidth, optimalHeight);
  165. }
  166. Rect2I GUIElement::_getCachedBounds() const
  167. {
  168. return Rect2I(mOffset.x, mOffset.y, mWidth, mHeight);
  169. }
  170. void GUIElement::setFocus(bool enabled)
  171. {
  172. GUIManager::instance().setFocus(this, enabled);
  173. }
  174. void GUIElement::setLayoutOptions(const GUIOptions& layoutOptions)
  175. {
  176. mLayoutOptions = GUILayoutOptions::create(layoutOptions);
  177. mLayoutOptions.updateWithStyle(mStyle);
  178. markContentAsDirty();
  179. }
  180. Rect2I GUIElement::getCachedVisibleBounds() const
  181. {
  182. Rect2I bounds = _getClippedBounds();
  183. bounds.x += mStyle->margins.left;
  184. bounds.y += mStyle->margins.top;
  185. bounds.width = (UINT32)std::max(0, (INT32)bounds.width - (INT32)(mStyle->margins.left + mStyle->margins.right));
  186. bounds.height = (UINT32)std::max(0, (INT32)bounds.height - (INT32)(mStyle->margins.top + mStyle->margins.bottom));
  187. return bounds;
  188. }
  189. Rect2I GUIElement::getCachedContentBounds() const
  190. {
  191. Rect2I bounds;
  192. bounds.x = mOffset.x + mStyle->margins.left + mStyle->contentOffset.left;
  193. bounds.y = mOffset.y + mStyle->margins.top + mStyle->contentOffset.top;
  194. bounds.width = (UINT32)std::max(0, (INT32)mWidth -
  195. (INT32)(mStyle->margins.left + mStyle->margins.right + mStyle->contentOffset.left + mStyle->contentOffset.right));
  196. bounds.height = (UINT32)std::max(0, (INT32)mHeight -
  197. (INT32)(mStyle->margins.top + mStyle->margins.bottom + mStyle->contentOffset.top + mStyle->contentOffset.bottom));
  198. return bounds;
  199. }
  200. Rect2I GUIElement::getCachedContentClipRect() const
  201. {
  202. Rect2I contentBounds = getCachedContentBounds();
  203. // Transform into element space so we can clip it using the element clip rectangle
  204. Vector2I offsetDiff = Vector2I(contentBounds.x - mOffset.x, contentBounds.y - mOffset.y);
  205. Rect2I contentClipRect(offsetDiff.x, offsetDiff.y, contentBounds.width, contentBounds.height);
  206. contentClipRect.clip(mClipRect);
  207. // Transform into content sprite space
  208. contentClipRect.x -= offsetDiff.x;
  209. contentClipRect.y -= offsetDiff.y;
  210. return contentClipRect;
  211. }
  212. bool GUIElement::_isInBounds(const Vector2I position) const
  213. {
  214. Rect2I contentBounds = getCachedVisibleBounds();
  215. return contentBounds.contains(position);
  216. }
  217. void GUIElement::_refreshStyle()
  218. {
  219. const GUIElementStyle* newStyle = nullptr;
  220. if(_getParentWidget() != nullptr)
  221. newStyle = _getParentWidget()->getSkin().getStyle(mStyleName);
  222. else
  223. newStyle = &GUISkin::DefaultStyle;
  224. if(newStyle != mStyle)
  225. {
  226. mStyle = newStyle;
  227. mLayoutOptions.updateWithStyle(mStyle);
  228. styleUpdated();
  229. markContentAsDirty();
  230. }
  231. }
  232. const String& GUIElement::getSubStyleName(const String& subStyleTypeName) const
  233. {
  234. auto iterFind = mStyle->subStyles.find(subStyleTypeName);
  235. if (iterFind != mStyle->subStyles.end())
  236. return iterFind->second;
  237. else
  238. return StringUtil::BLANK;
  239. }
  240. void GUIElement::destroy(GUIElement* element)
  241. {
  242. if(element->mIsDestroyed)
  243. return;
  244. if(element->mParentWidget != nullptr)
  245. element->mParentWidget->unregisterElement(element);
  246. if (element->mParentElement != nullptr)
  247. element->mParentElement->_unregisterChildElement(element);
  248. element->mIsDestroyed = true;
  249. GUIManager::instance().queueForDestroy(element);
  250. }
  251. Rect2I GUIElement::getBounds() const
  252. {
  253. return GUILayoutUtility::calcBounds(this);
  254. }
  255. Rect2I GUIElement::getVisibleBounds() const
  256. {
  257. Rect2I bounds = getBounds();
  258. bounds.x += mStyle->margins.left;
  259. bounds.y += mStyle->margins.top;
  260. bounds.width = (UINT32)std::max(0, (INT32)bounds.width - (INT32)(mStyle->margins.left + mStyle->margins.right));
  261. bounds.height = (UINT32)std::max(0, (INT32)bounds.height - (INT32)(mStyle->margins.top + mStyle->margins.bottom));
  262. return bounds;
  263. }
  264. }