| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- #include "BsGUIScrollBarHandle.h"
- #include "BsImageSprite.h"
- #include "BsGUIWidget.h"
- #include "BsGUISkin.h"
- #include "BsSpriteTexture.h"
- #include "BsTextSprite.h"
- #include "BsGUILayoutOptions.h"
- #include "BsGUIMouseEvent.h"
- #include "BsDebug.h"
- #include "BsTexture.h"
- namespace BansheeEngine
- {
- const String& GUIScrollBarHandle::getGUITypeName()
- {
- static String name = "ScrollBarHandle";
- return name;
- }
- GUIScrollBarHandle::GUIScrollBarHandle(bool horizontal, const String& styleName, const GUILayoutOptions& layoutOptions)
- :GUIElement(styleName, layoutOptions), mHorizontal(horizontal), mHandleSize(2), mMouseOverHandle(false), mHandlePos(0), mDragStartPos(0),
- mHandleDragged(false), mState(State::Normal)
- {
- mImageSprite = bs_new<ImageSprite, PoolAlloc>();
- }
- GUIScrollBarHandle::~GUIScrollBarHandle()
- {
- bs_delete<PoolAlloc>(mImageSprite);
- }
- GUIScrollBarHandle* GUIScrollBarHandle::create(bool horizontal, const String& styleName)
- {
- return new (bs_alloc<GUIScrollBarHandle, PoolAlloc>()) GUIScrollBarHandle(horizontal, getStyleName<GUIScrollBarHandle>(styleName), GUILayoutOptions::create());
- }
- GUIScrollBarHandle* GUIScrollBarHandle::create(bool horizontal, const GUIOptions& layoutOptions, const String& styleName)
- {
- return new (bs_alloc<GUIScrollBarHandle, PoolAlloc>()) GUIScrollBarHandle(horizontal, getStyleName<GUIScrollBarHandle>(styleName), GUILayoutOptions::create(layoutOptions));
- }
- void GUIScrollBarHandle::setHandleSize(UINT32 size)
- {
- mHandleSize = std::min(getMaxSize(), size);
- markContentAsDirty();
- }
- void GUIScrollBarHandle::setHandlePos(float pct)
- {
- pct = Math::clamp01(pct);
- UINT32 maxScrollAmount = getMaxSize() - mHandleSize;
- mHandlePos = pct * maxScrollAmount;
- markContentAsDirty();
- }
- float GUIScrollBarHandle::getHandlePos() const
- {
- UINT32 maxScrollAmount = getMaxSize() - mHandleSize;
- if(maxScrollAmount > 0.0f)
- return mHandlePos / maxScrollAmount;
- else
- return 0.0f;
- }
- UINT32 GUIScrollBarHandle::getScrollableSize() const
- {
- return getMaxSize() - mHandleSize;
- }
- UINT32 GUIScrollBarHandle::getNumRenderElements() const
- {
- return mImageSprite->getNumRenderElements();
- }
- const GUIMaterialInfo& GUIScrollBarHandle::getMaterial(UINT32 renderElementIdx) const
- {
- return mImageSprite->getMaterial(renderElementIdx);
- }
- UINT32 GUIScrollBarHandle::getNumQuads(UINT32 renderElementIdx) const
- {
- return mImageSprite->getNumQuads(renderElementIdx);
- }
- void GUIScrollBarHandle::updateRenderElementsInternal()
- {
- IMAGE_SPRITE_DESC desc;
- HSpriteTexture activeTex = getActiveTexture();
- if(SpriteTexture::checkIsLoaded(activeTex))
- desc.texture = activeTex.getInternalPtr();
- if(mHorizontal)
- {
- desc.width = mHandleSize;
- desc.height = mHeight;
- }
- else
- {
- desc.width = mWidth;
- desc.height = mHandleSize;
- }
- mImageSprite->update(desc);
-
- GUIElement::updateRenderElementsInternal();
- }
- void GUIScrollBarHandle::updateClippedBounds()
- {
- mClippedBounds = Rect2I(mOffset.x, mOffset.y, mWidth, mHeight);
- Rect2I localClipRect(mClipRect.x + mOffset.x, mClipRect.y + mOffset.y, mClipRect.width, mClipRect.height);
- mClippedBounds.clip(localClipRect);
- }
- Vector2I GUIScrollBarHandle::_getOptimalSize() const
- {
- HSpriteTexture activeTex = getActiveTexture();
- if(SpriteTexture::checkIsLoaded(activeTex))
- return Vector2I(activeTex->getWidth(), activeTex->getHeight());
- return Vector2I();
- }
- void GUIScrollBarHandle::fillBuffer(UINT8* vertices, UINT8* uv, UINT32* indices, UINT32 startingQuad, UINT32 maxNumQuads,
- UINT32 vertexStride, UINT32 indexStride, UINT32 renderElementIdx) const
- {
- Vector2I offset = mOffset;
- if(mHorizontal)
- offset.x += Math::floorToInt(mHandlePos);
- else
- offset.y += Math::floorToInt(mHandlePos);
- Rect2I clipRect = mClipRect;
- if(mHorizontal)
- clipRect.x -= Math::floorToInt(mHandlePos);
- else
- clipRect.y -= Math::floorToInt(mHandlePos);
- mImageSprite->fillBuffer(vertices, uv, indices, startingQuad, maxNumQuads,
- vertexStride, indexStride, renderElementIdx, offset, clipRect);
- }
- bool GUIScrollBarHandle::mouseEvent(const GUIMouseEvent& ev)
- {
- if(ev.getType() == GUIMouseEventType::MouseMove)
- {
- if(mMouseOverHandle)
- {
- if(!isOnHandle(ev.getPosition()))
- {
- mMouseOverHandle = false;
- mState = State::Normal;
- markContentAsDirty();
- return true;
- }
- }
- else
- {
- if(isOnHandle(ev.getPosition()))
- {
- mMouseOverHandle = true;
- mState = State::Hover;
- markContentAsDirty();
- return true;
- }
- }
- }
- if(ev.getType() == GUIMouseEventType::MouseDown && mMouseOverHandle)
- {
- mState = State::Active;
- markContentAsDirty();
- if(mHorizontal)
- {
- INT32 left = (INT32)mOffset.x + Math::floorToInt(mHandlePos);
- mDragStartPos = ev.getPosition().x - left;
- }
- else
- {
- INT32 top = (INT32)mOffset.y + Math::floorToInt(mHandlePos);
- mDragStartPos = ev.getPosition().y - top;
- }
- mHandleDragged = true;
- return true;
- }
- if(ev.getType() == GUIMouseEventType::MouseDrag && mHandleDragged)
- {
- if(mHorizontal)
- {
- mHandlePos = (float)(ev.getPosition().x - mDragStartPos - mOffset.x);
- }
- else
- {
- mHandlePos = float(ev.getPosition().y - mDragStartPos - mOffset.y);
- }
- float maxScrollAmount = (float)getMaxSize() - mHandleSize;
- mHandlePos = Math::clamp(mHandlePos, 0.0f, maxScrollAmount);
- if(!onHandleMoved.empty())
- {
- float pct = 0.0f;
- if(maxScrollAmount > 0.0f)
- pct = mHandlePos / maxScrollAmount;
-
- onHandleMoved(pct);
- }
- markContentAsDirty();
- return true;
- }
- if(ev.getType() == GUIMouseEventType::MouseOut && !mHandleDragged)
- {
- mState = State::Normal;
- mMouseOverHandle = false;
- markContentAsDirty();
- return true;
- }
- if(ev.getType() == GUIMouseEventType::MouseUp)
- {
- if(mMouseOverHandle)
- mState = State::Hover;
- else
- mState = State::Normal;
- // If we clicked above or below the scroll handle, scroll by one page
- INT32 handleOffset = 0;
- if(mHorizontal)
- {
- INT32 handleLeft = (INT32)mOffset.x + Math::floorToInt(mHandlePos);
- INT32 handleRight = handleLeft + mHandleSize;
- if(ev.getPosition().x < handleLeft)
- handleOffset -= mHandleSize;
- else if(ev.getPosition().x > handleRight)
- handleOffset += mHandleSize;
- }
- else
- {
- INT32 handleTop = (INT32)mOffset.y + Math::floorToInt(mHandlePos);
- INT32 handleBottom = handleTop + mHandleSize;
- if(ev.getPosition().y < handleTop)
- handleOffset -= mHandleSize;
- else if(ev.getPosition().y > handleBottom)
- handleOffset += mHandleSize;
- }
- mHandlePos += handleOffset;
- float maxScrollAmount = (float)getMaxSize() - mHandleSize;
- mHandlePos = Math::clamp(mHandlePos, 0.0f, maxScrollAmount);
- if(!onHandleMoved.empty())
- {
- float pct = 0.0f;
-
- if(maxScrollAmount > 0.0f)
- pct = (float)mHandlePos / maxScrollAmount;
- onHandleMoved(pct);
- }
- markContentAsDirty();
- return true;
- }
- if(ev.getType() == GUIMouseEventType::MouseDragEnd)
- {
- mHandleDragged = false;
- if(mMouseOverHandle)
- mState = State::Hover;
- else
- mState = State::Normal;
- markContentAsDirty();
- return true;
- }
-
- return false;
- }
- bool GUIScrollBarHandle::isOnHandle(const Vector2I& pos) const
- {
- if(mHorizontal)
- {
- INT32 left = (INT32)mOffset.x + Math::floorToInt(mHandlePos);
- INT32 right = left + mHandleSize;
- if(pos.x >= left && pos.x < right)
- return true;
- }
- else
- {
- INT32 top = (INT32)mOffset.y + Math::floorToInt(mHandlePos);
- INT32 bottom = top + mHandleSize;
- if(pos.y >= top && pos.y < bottom)
- return true;
- }
-
- return false;
- }
- UINT32 GUIScrollBarHandle::getMaxSize() const
- {
- UINT32 maxSize = mHeight;
- if(mHorizontal)
- maxSize = mWidth;
- return maxSize;
- }
- const HSpriteTexture& GUIScrollBarHandle::getActiveTexture() const
- {
- switch(mState)
- {
- case State::Active:
- return _getStyle()->active.texture;
- case State::Hover:
- return _getStyle()->hover.texture;
- case State::Normal:
- return _getStyle()->normal.texture;
- }
- return _getStyle()->normal.texture;
- }
- }
|