BsGUIScrollBarHandle.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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::updateBounds()
  102. {
  103. mBounds = Rect(mOffset.x, mOffset.y, mWidth, mHeight);
  104. Rect localClipRect(mClipRect.x + mOffset.x, mClipRect.y + mOffset.y, mClipRect.width, mClipRect.height);
  105. mBounds.clip(localClipRect);
  106. }
  107. UINT32 GUIScrollBarHandle::_getOptimalWidth() const
  108. {
  109. if(mCurTexture != nullptr)
  110. {
  111. return mCurTexture->getTexture()->getWidth();
  112. }
  113. return 0;
  114. }
  115. UINT32 GUIScrollBarHandle::_getOptimalHeight() const
  116. {
  117. if(mCurTexture != nullptr)
  118. {
  119. return mCurTexture->getTexture()->getHeight();
  120. }
  121. return 0;
  122. }
  123. void GUIScrollBarHandle::fillBuffer(UINT8* vertices, UINT8* uv, UINT32* indices, UINT32 startingQuad, UINT32 maxNumQuads,
  124. UINT32 vertexStride, UINT32 indexStride, UINT32 renderElementIdx) const
  125. {
  126. Int2 offset = mOffset;
  127. if(mHorizontal)
  128. offset.x += Math::FloorToInt(mHandlePos);
  129. else
  130. offset.y += Math::FloorToInt(mHandlePos);
  131. Rect clipRect = mClipRect;
  132. if(mHorizontal)
  133. clipRect.x -= Math::FloorToInt(mHandlePos);
  134. else
  135. clipRect.y -= Math::FloorToInt(mHandlePos);
  136. mImageSprite->fillBuffer(vertices, uv, indices, startingQuad, maxNumQuads,
  137. vertexStride, indexStride, renderElementIdx, offset, clipRect);
  138. }
  139. bool GUIScrollBarHandle::mouseEvent(const GUIMouseEvent& ev)
  140. {
  141. if(ev.getType() == GUIMouseEventType::MouseMove)
  142. {
  143. if(mMouseOverHandle)
  144. {
  145. if(!isOnHandle(ev.getPosition()))
  146. {
  147. mMouseOverHandle = false;
  148. mCurTexture = mStyle->normal.texture;
  149. markContentAsDirty();
  150. return true;
  151. }
  152. }
  153. else
  154. {
  155. if(isOnHandle(ev.getPosition()))
  156. {
  157. mMouseOverHandle = true;
  158. mCurTexture = mStyle->hover.texture;
  159. markContentAsDirty();
  160. return true;
  161. }
  162. }
  163. }
  164. if(ev.getType() == GUIMouseEventType::MouseDown && mMouseOverHandle)
  165. {
  166. mCurTexture = mStyle->active.texture;
  167. markContentAsDirty();
  168. if(mHorizontal)
  169. {
  170. INT32 left = (INT32)mOffset.x + Math::FloorToInt(mHandlePos);
  171. mDragStartPos = ev.getPosition().x - left;
  172. }
  173. else
  174. {
  175. INT32 top = (INT32)mOffset.y + Math::FloorToInt(mHandlePos);
  176. mDragStartPos = ev.getPosition().y - top;
  177. }
  178. mHandleDragged = true;
  179. return true;
  180. }
  181. if(ev.getType() == GUIMouseEventType::MouseDrag && mHandleDragged)
  182. {
  183. if(mHorizontal)
  184. {
  185. mHandlePos = (float)(ev.getPosition().x - mDragStartPos - mOffset.x);
  186. }
  187. else
  188. {
  189. mHandlePos = float(ev.getPosition().y - mDragStartPos - mOffset.y);
  190. }
  191. float maxScrollAmount = (float)getMaxSize() - mHandleSize;
  192. mHandlePos = Math::Clamp(mHandlePos, 0.0f, maxScrollAmount);
  193. if(!onHandleMoved.empty())
  194. {
  195. float pct = 0.0f;
  196. if(maxScrollAmount > 0.0f)
  197. pct = mHandlePos / maxScrollAmount;
  198. onHandleMoved(pct);
  199. }
  200. markContentAsDirty();
  201. return true;
  202. }
  203. if(ev.getType() == GUIMouseEventType::MouseOut && !mHandleDragged)
  204. {
  205. mCurTexture = mStyle->normal.texture;
  206. mMouseOverHandle = false;
  207. markContentAsDirty();
  208. return true;
  209. }
  210. if(ev.getType() == GUIMouseEventType::MouseUp)
  211. {
  212. if(mMouseOverHandle)
  213. mCurTexture = mStyle->hover.texture;
  214. else
  215. mCurTexture = mStyle->normal.texture;
  216. // If we clicked above or below the scroll handle, scroll by one page
  217. INT32 handleOffset = 0;
  218. if(mHorizontal)
  219. {
  220. INT32 handleLeft = (INT32)mOffset.x + Math::FloorToInt(mHandlePos);
  221. INT32 handleRight = handleLeft + mHandleSize;
  222. if(ev.getPosition().x < handleLeft)
  223. handleOffset -= mHandleSize;
  224. else if(ev.getPosition().x > handleRight)
  225. handleOffset += mHandleSize;
  226. }
  227. else
  228. {
  229. INT32 handleTop = (INT32)mOffset.y + Math::FloorToInt(mHandlePos);
  230. INT32 handleBottom = handleTop + mHandleSize;
  231. if(ev.getPosition().y < handleTop)
  232. handleOffset -= mHandleSize;
  233. else if(ev.getPosition().y > handleBottom)
  234. handleOffset += mHandleSize;
  235. }
  236. mHandlePos += handleOffset;
  237. float maxScrollAmount = (float)getMaxSize() - mHandleSize;
  238. mHandlePos = Math::Clamp(mHandlePos, 0.0f, maxScrollAmount);
  239. if(!onHandleMoved.empty())
  240. {
  241. float pct = 0.0f;
  242. if(maxScrollAmount > 0.0f)
  243. pct = (float)mHandlePos / maxScrollAmount;
  244. onHandleMoved(pct);
  245. }
  246. markContentAsDirty();
  247. return true;
  248. }
  249. if(ev.getType() == GUIMouseEventType::MouseDragEnd)
  250. {
  251. mHandleDragged = false;
  252. if(mMouseOverHandle)
  253. mCurTexture = mStyle->hover.texture;
  254. else
  255. mCurTexture = mStyle->normal.texture;
  256. markContentAsDirty();
  257. return true;
  258. }
  259. return false;
  260. }
  261. bool GUIScrollBarHandle::isOnHandle(const CM::Int2& pos) const
  262. {
  263. if(mHorizontal)
  264. {
  265. INT32 left = (INT32)mOffset.x + Math::FloorToInt(mHandlePos);
  266. INT32 right = left + mHandleSize;
  267. if(pos.x >= left && pos.x < right)
  268. return true;
  269. }
  270. else
  271. {
  272. INT32 top = (INT32)mOffset.y + Math::FloorToInt(mHandlePos);
  273. INT32 bottom = top + mHandleSize;
  274. if(pos.y >= top && pos.y < bottom)
  275. return true;
  276. }
  277. return false;
  278. }
  279. UINT32 GUIScrollBarHandle::getMaxSize() const
  280. {
  281. UINT32 maxSize = mHeight;
  282. if(mHorizontal)
  283. maxSize = mWidth;
  284. return maxSize;
  285. }
  286. }