BsGUIScrollBarHandle.cpp 8.2 KB

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