| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- #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 "CmTexture.h"
- using namespace CamelotFramework;
- namespace BansheeEngine
- {
- const String& GUIScrollBarHandle::getGUITypeName()
- {
- static String name = "ScrollBarHandle";
- return name;
- }
- GUIScrollBarHandle::GUIScrollBarHandle(GUIWidget& parent, bool horizontal, const GUIElementStyle* style, const GUILayoutOptions& layoutOptions)
- :GUIElement(parent, style, layoutOptions), mHorizontal(horizontal), mHandleSize(2), mMouseOverHandle(false), mHandlePos(0), mDragStartPos(0),
- mHandleDragged(false)
- {
- mImageSprite = cm_new<ImageSprite, PoolAlloc>();
- mCurTexture = style->normal.texture;
- }
- GUIScrollBarHandle::~GUIScrollBarHandle()
- {
- cm_delete<PoolAlloc>(mImageSprite);
- }
- GUIScrollBarHandle* GUIScrollBarHandle::create(GUIWidget& parent, bool horizontal, const GUIElementStyle* style)
- {
- if(style == nullptr)
- {
- const GUISkin* skin = parent.getSkin();
- style = skin->getStyle(getGUITypeName());
- }
- return new (cm_alloc<GUIScrollBarHandle, PoolAlloc>()) GUIScrollBarHandle(parent, horizontal, style, getDefaultLayoutOptions(style));
- }
- GUIScrollBarHandle* GUIScrollBarHandle::create(GUIWidget& parent, bool horizontal, const GUILayoutOptions& layoutOptions, const GUIElementStyle* style)
- {
- if(style == nullptr)
- {
- const GUISkin* skin = parent.getSkin();
- style = skin->getStyle(getGUITypeName());
- }
- return new (cm_alloc<GUIScrollBarHandle, PoolAlloc>()) GUIScrollBarHandle(parent, horizontal, style, layoutOptions);
- }
- void GUIScrollBarHandle::setHandleSize(CM::UINT32 size)
- {
- mHandleSize = std::min(getMaxSize(), size);
- markContentAsDirty();
- }
- void GUIScrollBarHandle::setHandlePos(float pct)
- {
- UINT32 maxScrollAmount = getMaxSize() - mHandleSize;
- mHandlePos = Math::FloorToInt(pct * maxScrollAmount);
- markContentAsDirty();
- }
- UINT32 GUIScrollBarHandle::getNumRenderElements() const
- {
- return mImageSprite->getNumRenderElements();
- }
- const HMaterial& 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;
- desc.texture = mCurTexture;
- if(mHorizontal)
- {
- desc.width = mHandleSize;
- desc.height = mHeight;
- }
- else
- {
- desc.width = mWidth;
- desc.height = mHandleSize;
- }
- mImageSprite->update(desc);
- mBounds = Rect(mOffset.x, mOffset.y, mWidth, mHeight);
- Rect localClipRect(mClipRect.x + mOffset.x, mClipRect.y + mOffset.y, mClipRect.width, mClipRect.height);
- mBounds.clip(localClipRect);
- }
- UINT32 GUIScrollBarHandle::_getOptimalWidth() const
- {
- if(mCurTexture != nullptr)
- {
- return mCurTexture->getTexture()->getWidth();
- }
- return 0;
- }
- UINT32 GUIScrollBarHandle::_getOptimalHeight() const
- {
- if(mCurTexture != nullptr)
- {
- return mCurTexture->getTexture()->getHeight();
- }
- return 0;
- }
- void GUIScrollBarHandle::fillBuffer(UINT8* vertices, UINT8* uv, UINT32* indices, UINT32 startingQuad, UINT32 maxNumQuads,
- UINT32 vertexStride, UINT32 indexStride, UINT32 renderElementIdx) const
- {
- Int2 offset = mOffset;
- if(mHorizontal)
- offset.x += mHandlePos;
- else
- offset.y += mHandlePos;
- Rect clipRect = mClipRect;
- if(mHorizontal)
- clipRect.x -= mHandlePos;
- else
- clipRect.y -= 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;
- mCurTexture = mStyle->normal.texture;
- markContentAsDirty();
- return true;
- }
- }
- else
- {
- if(isOnHandle(ev.getPosition()))
- {
- mMouseOverHandle = true;
- mCurTexture = mStyle->hover.texture;
- markContentAsDirty();
- return true;
- }
- }
- }
- if(ev.getType() == GUIMouseEventType::MouseDown && mMouseOverHandle)
- {
- mCurTexture = mStyle->active.texture;
- markContentAsDirty();
- if(mHorizontal)
- {
- INT32 left = (INT32)mOffset.x + mHandlePos;
- mDragStartPos = ev.getPosition().x - left;
- }
- else
- {
- INT32 top = (INT32)mOffset.y + mHandlePos;
- mDragStartPos = ev.getPosition().y - top;
- }
- mHandleDragged = true;
- return true;
- }
- if(ev.getType() == GUIMouseEventType::MouseDrag && mHandleDragged)
- {
- if(mHorizontal)
- {
- mHandlePos = ev.getPosition().x - mDragStartPos - mOffset.x;
- }
- else
- {
- mHandlePos = ev.getPosition().y - mDragStartPos - mOffset.y;
- }
- UINT32 maxScrollAmount = getMaxSize() - mHandleSize;
- mHandlePos = Math::Clamp(mHandlePos, 0, (INT32)maxScrollAmount);
- if(!handleMoved.empty())
- {
- float pct = maxScrollAmount / (float)mHandlePos;
- handleMoved(pct);
- }
- markContentAsDirty();
- return true;
- }
- if(ev.getType() == GUIMouseEventType::MouseOut && !mHandleDragged)
- {
- mCurTexture = mStyle->normal.texture;
- markContentAsDirty();
- return true;
- }
- if(ev.getType() == GUIMouseEventType::MouseUp)
- {
- if(mMouseOverHandle)
- mCurTexture = mStyle->hover.texture;
- else
- mCurTexture = mStyle->normal.texture;
- markContentAsDirty();
- return true;
- }
- if(ev.getType() == GUIMouseEventType::MouseDragEnd)
- {
- mHandleDragged = false;
- if(mMouseOverHandle)
- mCurTexture = mStyle->hover.texture;
- else
- mCurTexture = mStyle->normal.texture;
- markContentAsDirty();
- return true;
- }
-
- return false;
- }
- bool GUIScrollBarHandle::isOnHandle(const CM::Int2& pos) const
- {
- if(mHorizontal)
- {
- INT32 left = (INT32)mOffset.x + mHandlePos;
- INT32 right = left + mHandleSize;
- if(pos.x >= left && pos.x < right)
- return true;
- }
- else
- {
- INT32 top = (INT32)mOffset.y + 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;
- }
- }
|