BsGUIScrollBarHandle.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. #include "BsGUIScrollBarHandle.h"
  2. #include "BsImageSprite.h"
  3. #include "BsGUIWidget.h"
  4. #include "BsGUISkin.h"
  5. #include "BsSpriteTexture.h"
  6. #include "BsTextSprite.h"
  7. #include "BsGUILayoutOptions.h"
  8. #include "BsGUIMouseEvent.h"
  9. #include "BsDebug.h"
  10. #include "BsTexture.h"
  11. namespace BansheeEngine
  12. {
  13. const String& GUIScrollBarHandle::getGUITypeName()
  14. {
  15. static String name = "ScrollBarHandle";
  16. return name;
  17. }
  18. GUIScrollBarHandle::GUIScrollBarHandle(bool horizontal, const String& styleName, const GUILayoutOptions& layoutOptions)
  19. :GUIElement(styleName, layoutOptions), mHorizontal(horizontal), mHandleSize(2), mMouseOverHandle(false), mHandlePos(0), mDragStartPos(0),
  20. mHandleDragged(false), mState(State::Normal)
  21. {
  22. mImageSprite = bs_new<ImageSprite, PoolAlloc>();
  23. }
  24. GUIScrollBarHandle::~GUIScrollBarHandle()
  25. {
  26. bs_delete<PoolAlloc>(mImageSprite);
  27. }
  28. GUIScrollBarHandle* GUIScrollBarHandle::create(bool horizontal, const String& styleName)
  29. {
  30. return new (bs_alloc<GUIScrollBarHandle, PoolAlloc>()) GUIScrollBarHandle(horizontal, getStyleName<GUIScrollBarHandle>(styleName), GUILayoutOptions::create());
  31. }
  32. GUIScrollBarHandle* GUIScrollBarHandle::create(bool horizontal, const GUIOptions& layoutOptions, const String& styleName)
  33. {
  34. return new (bs_alloc<GUIScrollBarHandle, PoolAlloc>()) GUIScrollBarHandle(horizontal, getStyleName<GUIScrollBarHandle>(styleName), GUILayoutOptions::create(layoutOptions));
  35. }
  36. void GUIScrollBarHandle::setHandleSize(UINT32 size)
  37. {
  38. mHandleSize = std::min(getMaxSize(), size);
  39. markContentAsDirty();
  40. }
  41. void GUIScrollBarHandle::setHandlePos(float pct)
  42. {
  43. pct = Math::clamp01(pct);
  44. UINT32 maxScrollAmount = getMaxSize() - mHandleSize;
  45. mHandlePos = pct * maxScrollAmount;
  46. markContentAsDirty();
  47. }
  48. float GUIScrollBarHandle::getHandlePos() const
  49. {
  50. UINT32 maxScrollAmount = getMaxSize() - mHandleSize;
  51. if(maxScrollAmount > 0.0f)
  52. return mHandlePos / maxScrollAmount;
  53. else
  54. return 0.0f;
  55. }
  56. UINT32 GUIScrollBarHandle::getScrollableSize() const
  57. {
  58. return getMaxSize() - mHandleSize;
  59. }
  60. UINT32 GUIScrollBarHandle::getNumRenderElements() const
  61. {
  62. return mImageSprite->getNumRenderElements();
  63. }
  64. const GUIMaterialInfo& GUIScrollBarHandle::getMaterial(UINT32 renderElementIdx) const
  65. {
  66. return mImageSprite->getMaterial(renderElementIdx);
  67. }
  68. UINT32 GUIScrollBarHandle::getNumQuads(UINT32 renderElementIdx) const
  69. {
  70. return mImageSprite->getNumQuads(renderElementIdx);
  71. }
  72. void GUIScrollBarHandle::updateRenderElementsInternal()
  73. {
  74. IMAGE_SPRITE_DESC desc;
  75. HSpriteTexture activeTex = getActiveTexture();
  76. if(SpriteTexture::checkIsLoaded(activeTex))
  77. desc.texture = activeTex.getInternalPtr();
  78. if(mHorizontal)
  79. {
  80. desc.width = mHandleSize;
  81. desc.height = mHeight;
  82. }
  83. else
  84. {
  85. desc.width = mWidth;
  86. desc.height = mHandleSize;
  87. }
  88. mImageSprite->update(desc);
  89. GUIElement::updateRenderElementsInternal();
  90. }
  91. void GUIScrollBarHandle::updateClippedBounds()
  92. {
  93. mClippedBounds = Rect2I(mOffset.x, mOffset.y, mWidth, mHeight);
  94. Rect2I localClipRect(mClipRect.x + mOffset.x, mClipRect.y + mOffset.y, mClipRect.width, mClipRect.height);
  95. mClippedBounds.clip(localClipRect);
  96. }
  97. Vector2I GUIScrollBarHandle::_getOptimalSize() const
  98. {
  99. HSpriteTexture activeTex = getActiveTexture();
  100. if(SpriteTexture::checkIsLoaded(activeTex))
  101. return Vector2I(activeTex->getWidth(), activeTex->getHeight());
  102. return Vector2I();
  103. }
  104. void GUIScrollBarHandle::fillBuffer(UINT8* vertices, UINT8* uv, UINT32* indices, UINT32 startingQuad, UINT32 maxNumQuads,
  105. UINT32 vertexStride, UINT32 indexStride, UINT32 renderElementIdx) const
  106. {
  107. Vector2I offset = mOffset;
  108. if(mHorizontal)
  109. offset.x += Math::floorToInt(mHandlePos);
  110. else
  111. offset.y += Math::floorToInt(mHandlePos);
  112. Rect2I clipRect = mClipRect;
  113. if(mHorizontal)
  114. clipRect.x -= Math::floorToInt(mHandlePos);
  115. else
  116. clipRect.y -= Math::floorToInt(mHandlePos);
  117. mImageSprite->fillBuffer(vertices, uv, indices, startingQuad, maxNumQuads,
  118. vertexStride, indexStride, renderElementIdx, offset, clipRect);
  119. }
  120. bool GUIScrollBarHandle::mouseEvent(const GUIMouseEvent& ev)
  121. {
  122. if(ev.getType() == GUIMouseEventType::MouseMove)
  123. {
  124. if(mMouseOverHandle)
  125. {
  126. if(!isOnHandle(ev.getPosition()))
  127. {
  128. mMouseOverHandle = false;
  129. mState = State::Normal;
  130. markContentAsDirty();
  131. return true;
  132. }
  133. }
  134. else
  135. {
  136. if(isOnHandle(ev.getPosition()))
  137. {
  138. mMouseOverHandle = true;
  139. mState = State::Hover;
  140. markContentAsDirty();
  141. return true;
  142. }
  143. }
  144. }
  145. if(ev.getType() == GUIMouseEventType::MouseDown && mMouseOverHandle)
  146. {
  147. mState = State::Active;
  148. markContentAsDirty();
  149. if(mHorizontal)
  150. {
  151. INT32 left = (INT32)mOffset.x + Math::floorToInt(mHandlePos);
  152. mDragStartPos = ev.getPosition().x - left;
  153. }
  154. else
  155. {
  156. INT32 top = (INT32)mOffset.y + Math::floorToInt(mHandlePos);
  157. mDragStartPos = ev.getPosition().y - top;
  158. }
  159. mHandleDragged = true;
  160. return true;
  161. }
  162. if(ev.getType() == GUIMouseEventType::MouseDrag && mHandleDragged)
  163. {
  164. if(mHorizontal)
  165. {
  166. mHandlePos = (float)(ev.getPosition().x - mDragStartPos - mOffset.x);
  167. }
  168. else
  169. {
  170. mHandlePos = float(ev.getPosition().y - mDragStartPos - mOffset.y);
  171. }
  172. float maxScrollAmount = (float)getMaxSize() - mHandleSize;
  173. mHandlePos = Math::clamp(mHandlePos, 0.0f, maxScrollAmount);
  174. if(!onHandleMoved.empty())
  175. {
  176. float pct = 0.0f;
  177. if(maxScrollAmount > 0.0f)
  178. pct = mHandlePos / maxScrollAmount;
  179. onHandleMoved(pct);
  180. }
  181. markContentAsDirty();
  182. return true;
  183. }
  184. if(ev.getType() == GUIMouseEventType::MouseOut && !mHandleDragged)
  185. {
  186. mState = State::Normal;
  187. mMouseOverHandle = false;
  188. markContentAsDirty();
  189. return true;
  190. }
  191. if(ev.getType() == GUIMouseEventType::MouseUp)
  192. {
  193. if(mMouseOverHandle)
  194. mState = State::Hover;
  195. else
  196. mState = State::Normal;
  197. // If we clicked above or below the scroll handle, scroll by one page
  198. INT32 handleOffset = 0;
  199. if(mHorizontal)
  200. {
  201. INT32 handleLeft = (INT32)mOffset.x + Math::floorToInt(mHandlePos);
  202. INT32 handleRight = handleLeft + mHandleSize;
  203. if(ev.getPosition().x < handleLeft)
  204. handleOffset -= mHandleSize;
  205. else if(ev.getPosition().x > handleRight)
  206. handleOffset += mHandleSize;
  207. }
  208. else
  209. {
  210. INT32 handleTop = (INT32)mOffset.y + Math::floorToInt(mHandlePos);
  211. INT32 handleBottom = handleTop + mHandleSize;
  212. if(ev.getPosition().y < handleTop)
  213. handleOffset -= mHandleSize;
  214. else if(ev.getPosition().y > handleBottom)
  215. handleOffset += mHandleSize;
  216. }
  217. mHandlePos += handleOffset;
  218. float maxScrollAmount = (float)getMaxSize() - mHandleSize;
  219. mHandlePos = Math::clamp(mHandlePos, 0.0f, maxScrollAmount);
  220. if(!onHandleMoved.empty())
  221. {
  222. float pct = 0.0f;
  223. if(maxScrollAmount > 0.0f)
  224. pct = (float)mHandlePos / maxScrollAmount;
  225. onHandleMoved(pct);
  226. }
  227. markContentAsDirty();
  228. return true;
  229. }
  230. if(ev.getType() == GUIMouseEventType::MouseDragEnd)
  231. {
  232. mHandleDragged = false;
  233. if(mMouseOverHandle)
  234. mState = State::Hover;
  235. else
  236. mState = State::Normal;
  237. markContentAsDirty();
  238. return true;
  239. }
  240. return false;
  241. }
  242. bool GUIScrollBarHandle::isOnHandle(const Vector2I& pos) const
  243. {
  244. if(mHorizontal)
  245. {
  246. INT32 left = (INT32)mOffset.x + Math::floorToInt(mHandlePos);
  247. INT32 right = left + mHandleSize;
  248. if(pos.x >= left && pos.x < right)
  249. return true;
  250. }
  251. else
  252. {
  253. INT32 top = (INT32)mOffset.y + Math::floorToInt(mHandlePos);
  254. INT32 bottom = top + mHandleSize;
  255. if(pos.y >= top && pos.y < bottom)
  256. return true;
  257. }
  258. return false;
  259. }
  260. UINT32 GUIScrollBarHandle::getMaxSize() const
  261. {
  262. UINT32 maxSize = mHeight;
  263. if(mHorizontal)
  264. maxSize = mWidth;
  265. return maxSize;
  266. }
  267. const HSpriteTexture& GUIScrollBarHandle::getActiveTexture() const
  268. {
  269. switch(mState)
  270. {
  271. case State::Active:
  272. return _getStyle()->active.texture;
  273. case State::Hover:
  274. return _getStyle()->hover.texture;
  275. case State::Normal:
  276. return _getStyle()->normal.texture;
  277. }
  278. return _getStyle()->normal.texture;
  279. }
  280. }