BsGUIScrollArea.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. #include "BsGUIScrollArea.h"
  2. #include "BsGUIElementStyle.h"
  3. #include "BsGUISkin.h"
  4. #include "BsGUIWidget.h"
  5. #include "BsGUILayoutOptions.h"
  6. #include "BsGUILayout.h"
  7. #include "BsGUISkin.h"
  8. #include "BsGUIScrollBarVert.h"
  9. #include "BsGUIScrollBarHorz.h"
  10. #include "BsGUIMouseEvent.h"
  11. #include "CmException.h"
  12. using namespace CamelotFramework;
  13. namespace BansheeEngine
  14. {
  15. const UINT32 GUIScrollArea::ScrollBarWidth = 8;
  16. const UINT32 GUIScrollArea::MinHandleSize = 4;
  17. const UINT32 GUIScrollArea::WheelScrollAmount = 50;
  18. GUIScrollArea::GUIScrollArea(GUIWidget& parent, ScrollBarType vertBarType, ScrollBarType horzBarType,
  19. const GUIElementStyle* scrollBarStyle, const GUIElementStyle* scrollAreaStyle, const GUILayoutOptions& layoutOptions)
  20. :GUIElement(parent, scrollAreaStyle, layoutOptions, false), mVertScroll(nullptr), mHorzScroll(nullptr), mVertOffset(0), mHorzOffset(0),
  21. mContentWidth(0), mContentHeight(0), mClippedContentWidth(0), mClippedContentHeight(0), mVertBarType(vertBarType), mHorzBarType(horzBarType),
  22. mScrollBarStyle(scrollBarStyle)
  23. {
  24. mContentLayout = &addLayoutYInternal(this);
  25. }
  26. GUIScrollArea::~GUIScrollArea()
  27. {
  28. }
  29. UINT32 GUIScrollArea::getNumRenderElements() const
  30. {
  31. return 0;
  32. }
  33. const GUIMaterialInfo& GUIScrollArea::getMaterial(UINT32 renderElementIdx) const
  34. {
  35. CM_EXCEPT(InvalidStateException, "Trying to retrieve a material from an element with no render elements.");
  36. }
  37. UINT32 GUIScrollArea::getNumQuads(UINT32 renderElementIdx) const
  38. {
  39. return 0;
  40. }
  41. void GUIScrollArea::updateClippedBounds()
  42. {
  43. mClippedBounds = RectI(0, 0, 0, 0); // We don't want any mouse input for this element. This is just a container.
  44. }
  45. Vector2I GUIScrollArea::_getOptimalSize() const
  46. {
  47. return mContentLayout->_getOptimalSize();
  48. }
  49. void GUIScrollArea::fillBuffer(UINT8* vertices, UINT8* uv, UINT32* indices, UINT32 startingQuad, UINT32 maxNumQuads,
  50. UINT32 vertexStride, UINT32 indexStride, UINT32 renderElementIdx) const
  51. { }
  52. void GUIScrollArea::_updateLayoutInternal(INT32 x, INT32 y, UINT32 width, UINT32 height,
  53. RectI clipRect, UINT8 widgetDepth, UINT16 areaDepth)
  54. {
  55. mContentLayout->_updateLayoutInternal(x, y, width, height, clipRect, widgetDepth, areaDepth);
  56. UINT32 contentWidth = mContentLayout->_getActualWidth();
  57. UINT32 contentHeight = mContentLayout->_getActualHeight();
  58. mClippedContentWidth = width;
  59. mClippedContentHeight = height;
  60. RectI layoutClipRect = clipRect;
  61. bool addHorzScrollbar = (mHorzBarType == ScrollBarType::ShowIfDoesntFit && contentWidth > mWidth) ||
  62. mHorzBarType == ScrollBarType::AlwaysShow && mHorzBarType != ScrollBarType::NeverShow;
  63. bool hasHorzScrollbar = false;
  64. bool hasVertScrollbar = false;
  65. if(addHorzScrollbar)
  66. {
  67. // Make room for scrollbar
  68. mClippedContentHeight = (UINT32)std::max(0, (INT32)height - (INT32)ScrollBarWidth);
  69. layoutClipRect.height = mClippedContentHeight;
  70. hasHorzScrollbar = true;
  71. mContentLayout->_updateLayoutInternal(x - Math::floorToInt(mHorzOffset), y,
  72. mClippedContentWidth + Math::floorToInt(mHorzOffset), mClippedContentHeight, layoutClipRect, widgetDepth, areaDepth);
  73. contentWidth = mContentLayout->_getActualWidth();
  74. contentHeight = mContentLayout->_getActualHeight();
  75. }
  76. bool addVertScrollbar = (mVertBarType == ScrollBarType::ShowIfDoesntFit && contentHeight > mClippedContentHeight) ||
  77. mVertBarType == ScrollBarType::AlwaysShow && mVertBarType != ScrollBarType::NeverShow;
  78. if(addVertScrollbar)
  79. {
  80. // Make room for scrollbar
  81. mClippedContentWidth = (UINT32)std::max(0, (INT32)width - (INT32)ScrollBarWidth);
  82. layoutClipRect.width = mClippedContentWidth;
  83. hasVertScrollbar = true;
  84. if(hasHorzScrollbar)
  85. {
  86. mContentLayout->_updateLayoutInternal(x - Math::floorToInt(mHorzOffset), y - Math::floorToInt(mVertOffset),
  87. mClippedContentWidth + Math::floorToInt(mHorzOffset), mClippedContentHeight + Math::floorToInt(mVertOffset),
  88. layoutClipRect, widgetDepth, areaDepth);
  89. }
  90. else
  91. {
  92. mContentLayout->_updateLayoutInternal(x, y - Math::floorToInt(mVertOffset),
  93. mClippedContentWidth, mClippedContentHeight + Math::floorToInt(mVertOffset),
  94. layoutClipRect, widgetDepth, areaDepth);
  95. }
  96. contentWidth = mContentLayout->_getActualWidth();
  97. contentHeight = mContentLayout->_getActualHeight();
  98. if(!hasHorzScrollbar) // Since width has been reduced, we need to check if we require the horizontal scrollbar
  99. {
  100. addHorzScrollbar = (mHorzBarType == ScrollBarType::ShowIfDoesntFit && contentWidth > mClippedContentWidth) && mHorzBarType != ScrollBarType::NeverShow;
  101. if(addHorzScrollbar)
  102. {
  103. // Make room for scrollbar
  104. mClippedContentHeight = (UINT32)std::max(0, (INT32)height - (INT32)ScrollBarWidth);
  105. layoutClipRect.height = mClippedContentHeight;
  106. mContentLayout->_updateLayoutInternal(x - Math::floorToInt(mHorzOffset), y - Math::floorToInt(mVertOffset),
  107. mClippedContentWidth + Math::floorToInt(mHorzOffset), mClippedContentHeight + Math::floorToInt(mVertOffset),
  108. layoutClipRect, widgetDepth, areaDepth);
  109. }
  110. }
  111. }
  112. // Add/remove/update vertical scrollbar as needed
  113. if((mVertBarType == ScrollBarType::ShowIfDoesntFit && contentHeight > mClippedContentHeight) || mVertBarType == ScrollBarType::AlwaysShow &&
  114. mVertBarType != ScrollBarType::NeverShow)
  115. {
  116. if(mVertScroll == nullptr)
  117. {
  118. if(mScrollBarStyle != nullptr)
  119. mVertScroll = GUIScrollBarVert::create(_getParentWidget(), mScrollBarStyle);
  120. else
  121. mVertScroll = GUIScrollBarVert::create(_getParentWidget());
  122. mVertScroll->onScrollPositionChanged.connect(boost::bind(&GUIScrollArea::vertScrollUpdate, this, _1));
  123. mVertScroll->_setAcceptsKeyboardFocus(false);
  124. }
  125. INT32 scrollBarOffset = (UINT32)std::max(0, (INT32)width - (INT32)ScrollBarWidth);
  126. UINT32 scrollBarHeight = height;
  127. if(hasHorzScrollbar)
  128. scrollBarHeight = (UINT32)std::max(0, (INT32)scrollBarHeight - (INT32)ScrollBarWidth);
  129. Vector2I offset(x + scrollBarOffset, y);
  130. mVertScroll->_setOffset(offset);
  131. mVertScroll->_setWidth(ScrollBarWidth);
  132. mVertScroll->_setHeight(scrollBarHeight);
  133. mVertScroll->_setAreaDepth(areaDepth);
  134. mVertScroll->_setWidgetDepth(widgetDepth);
  135. UINT32 clippedScrollbarWidth = std::min(width, ScrollBarWidth);
  136. RectI elemClipRect(0, 0, clippedScrollbarWidth, clipRect.height);
  137. mVertScroll->_setClipRect(elemClipRect);
  138. // This element is not a child of any layout so we treat it as a root element
  139. RectI scrollBarLayoutClipRect(clipRect.x + scrollBarOffset, clipRect.y, clippedScrollbarWidth, clipRect.height);
  140. mVertScroll->_updateLayout(offset.x, offset.y, ScrollBarWidth, scrollBarHeight, scrollBarLayoutClipRect, widgetDepth, areaDepth);
  141. // Set new handle size and update position to match the new size
  142. UINT32 newHandleSize = (UINT32)Math::floorToInt(mVertScroll->getMaxHandleSize() * (scrollBarHeight / (float)contentHeight));
  143. newHandleSize = std::max(newHandleSize, MinHandleSize);
  144. UINT32 scrollableHeight = (UINT32)std::max(0, INT32(contentHeight) - INT32(scrollBarHeight));
  145. float newScrollPct = 0.0f;
  146. if(scrollableHeight > 0)
  147. newScrollPct = mVertOffset / scrollableHeight;
  148. mVertScroll->setHandleSize(newHandleSize);
  149. mVertScroll->setScrollPos(newScrollPct);
  150. }
  151. else
  152. {
  153. if(mVertScroll != nullptr)
  154. {
  155. GUIElement::destroy(mVertScroll);
  156. mVertScroll = nullptr;
  157. }
  158. mVertOffset = 0.0f;
  159. }
  160. // Add/remove/update horizontal scrollbar as needed
  161. if((mHorzBarType == ScrollBarType::ShowIfDoesntFit && contentWidth > mClippedContentWidth) || mHorzBarType == ScrollBarType::AlwaysShow &&
  162. mHorzBarType != ScrollBarType::NeverShow)
  163. {
  164. if(mHorzScroll == nullptr)
  165. {
  166. if(mScrollBarStyle != nullptr)
  167. mHorzScroll = GUIScrollBarHorz::create(_getParentWidget(), mScrollBarStyle);
  168. else
  169. mHorzScroll = GUIScrollBarHorz::create(_getParentWidget());
  170. mHorzScroll->onScrollPositionChanged.connect(boost::bind(&GUIScrollArea::horzScrollUpdate, this, _1));
  171. mHorzScroll->_setAcceptsKeyboardFocus(false);
  172. }
  173. INT32 scrollBarOffset = (UINT32)std::max(0, (INT32)height - (INT32)ScrollBarWidth);
  174. UINT32 scrollBarWidth = width;
  175. if(hasVertScrollbar)
  176. scrollBarWidth = (UINT32)std::max(0, (INT32)scrollBarWidth - (INT32)ScrollBarWidth);
  177. Vector2I offset(x, y + scrollBarOffset);
  178. mHorzScroll->_setOffset(offset);
  179. mHorzScroll->_setWidth(scrollBarWidth);
  180. mHorzScroll->_setHeight(ScrollBarWidth);
  181. mHorzScroll->_setAreaDepth(areaDepth);
  182. mHorzScroll->_setWidgetDepth(widgetDepth);
  183. UINT32 clippedScrollbarHeight = std::min(height, ScrollBarWidth);
  184. RectI elemClipRect(0, 0, clipRect.width, clippedScrollbarHeight);
  185. mHorzScroll->_setClipRect(elemClipRect);
  186. // This element is not a child of any layout so we treat it as a root element
  187. RectI scrollBarLayoutClipRect(clipRect.x, clipRect.y + scrollBarOffset, clipRect.width, clippedScrollbarHeight);
  188. mHorzScroll->_updateLayout(offset.x, offset.y, scrollBarWidth, ScrollBarWidth, scrollBarLayoutClipRect, widgetDepth, areaDepth);
  189. // Set new handle size and update position to match the new size
  190. UINT32 newHandleSize = (UINT32)Math::floorToInt(mHorzScroll->getMaxHandleSize() * (scrollBarWidth / (float)contentWidth));
  191. newHandleSize = std::max(newHandleSize, MinHandleSize);
  192. UINT32 scrollableWidth = (UINT32)std::max(0, INT32(contentWidth) - INT32(scrollBarWidth));
  193. float newScrollPct = 0.0f;
  194. if(scrollableWidth > 0)
  195. newScrollPct = mHorzOffset / scrollableWidth;
  196. mHorzScroll->setHandleSize(newHandleSize);
  197. mHorzScroll->setScrollPos(newScrollPct);
  198. }
  199. else
  200. {
  201. if(mHorzScroll != nullptr)
  202. {
  203. GUIElement::destroy(mHorzScroll);
  204. mHorzScroll = nullptr;
  205. }
  206. mHorzOffset = 0.0f;
  207. }
  208. mContentWidth = contentWidth;
  209. mContentHeight = contentHeight;
  210. }
  211. void GUIScrollArea::vertScrollUpdate(float scrollPos)
  212. {
  213. UINT32 scrollableHeight = (UINT32)std::max(0, INT32(mContentHeight) - INT32(mClippedContentHeight));
  214. mVertOffset = scrollableHeight * scrollPos;
  215. markContentAsDirty();
  216. }
  217. void GUIScrollArea::horzScrollUpdate(float scrollPos)
  218. {
  219. UINT32 scrollableWidth = (UINT32)std::max(0, INT32(mContentWidth) - INT32(mClippedContentWidth));
  220. mHorzOffset = scrollableWidth * scrollPos;
  221. markContentAsDirty();
  222. }
  223. bool GUIScrollArea::mouseEvent(const GUIMouseEvent& ev)
  224. {
  225. if(ev.getType() == GUIMouseEventType::MouseWheelScroll)
  226. {
  227. // Mouse wheel only scrolls on the Y axis
  228. if(mVertScroll != nullptr)
  229. {
  230. UINT32 scrollableHeight = (UINT32)std::max(0, INT32(mContentWidth) - INT32(mClippedContentHeight));
  231. float additionalScroll = (float)WheelScrollAmount / scrollableHeight;
  232. mVertScroll->scroll(additionalScroll * ev.getWheelScrollAmount());
  233. return true;
  234. }
  235. }
  236. return false;
  237. }
  238. void GUIScrollArea::_changeParentWidget(GUIWidget* widget)
  239. {
  240. GUIElement::_changeParentWidget(widget);
  241. // These two elements are not part of a layout so I need to make sure to
  242. // update them manually
  243. if(mVertScroll != nullptr)
  244. mVertScroll->_changeParentWidget(widget);
  245. if(mHorzScroll != nullptr)
  246. mHorzScroll->_changeParentWidget(widget);
  247. }
  248. GUIScrollArea* GUIScrollArea::create(GUIWidget& parent, ScrollBarType vertBarType, ScrollBarType horzBarType,
  249. const GUIElementStyle* scrollBarStyle, const GUIElementStyle* scrollAreaStyle)
  250. {
  251. if(scrollAreaStyle == nullptr)
  252. {
  253. const GUISkin& skin = parent.getSkin();
  254. scrollAreaStyle = skin.getStyle(getGUITypeName());
  255. }
  256. return new (cm_alloc<GUIScrollArea, PoolAlloc>()) GUIScrollArea(parent, vertBarType, horzBarType, scrollBarStyle,
  257. scrollAreaStyle, GUILayoutOptions::create(scrollAreaStyle));
  258. }
  259. GUIScrollArea* GUIScrollArea::create(GUIWidget& parent, const GUIOptions& layoutOptions, const GUIElementStyle* scrollBarStyle,
  260. const GUIElementStyle* scrollAreaStyle)
  261. {
  262. if(scrollAreaStyle == nullptr)
  263. {
  264. const GUISkin& skin = parent.getSkin();
  265. scrollAreaStyle = skin.getStyle(getGUITypeName());
  266. }
  267. return new (cm_alloc<GUIScrollArea, PoolAlloc>()) GUIScrollArea(parent, ScrollBarType::ShowIfDoesntFit,
  268. ScrollBarType::ShowIfDoesntFit, scrollBarStyle, scrollAreaStyle, GUILayoutOptions::create(layoutOptions, scrollAreaStyle));
  269. }
  270. GUIScrollArea* GUIScrollArea::create(GUIWidget& parent, const GUIElementStyle* scrollBarStyle, const GUIElementStyle* scrollAreaStyle)
  271. {
  272. if(scrollAreaStyle == nullptr)
  273. {
  274. const GUISkin& skin = parent.getSkin();
  275. scrollAreaStyle = skin.getStyle(getGUITypeName());
  276. }
  277. return new (cm_alloc<GUIScrollArea, PoolAlloc>()) GUIScrollArea(parent, ScrollBarType::ShowIfDoesntFit, ScrollBarType::ShowIfDoesntFit, scrollBarStyle,
  278. scrollAreaStyle, GUILayoutOptions::create(scrollAreaStyle));
  279. }
  280. GUIScrollArea* GUIScrollArea::create(GUIWidget& parent, ScrollBarType vertBarType,
  281. ScrollBarType horzBarType, const GUIOptions& layoutOptions, const GUIElementStyle* scrollBarStyle,
  282. const GUIElementStyle* scrollAreaStyle)
  283. {
  284. if(scrollAreaStyle == nullptr)
  285. {
  286. const GUISkin& skin = parent.getSkin();
  287. scrollAreaStyle = skin.getStyle(getGUITypeName());
  288. }
  289. return new (cm_alloc<GUIScrollArea, PoolAlloc>()) GUIScrollArea(parent, vertBarType, horzBarType, scrollBarStyle, scrollAreaStyle, GUILayoutOptions::create(layoutOptions, scrollAreaStyle));
  290. }
  291. const String& GUIScrollArea::getGUITypeName()
  292. {
  293. static String typeName = "ScrollArea";
  294. return typeName;
  295. }
  296. }