BsGUISliderHandle.cpp 8.6 KB

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