BsGUIScrollBarHandle.cpp 6.6 KB

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