Просмотр исходного кода

Fixed division by zero issues when handle size was incorrectly set to the scroll area size (which also made it impossible to fully scroll when involving small differences)
Added additional division by zero checks

Marko Pintera 12 лет назад
Родитель
Сommit
951690fa39

+ 2 - 2
BansheeEngine/Source/BsGUIScrollArea.cpp

@@ -129,7 +129,7 @@ namespace BansheeEngine
 			mVertScroll->_updateLayout(offset.x, offset.y, ScrollBarWidth, mClippedContentHeight, scrollBarLayoutClipRect, widgetDepth, areaDepth);
 
 			// Set new handle size and update position to match the new size
-			UINT32 newHandleSize = (UINT32)Math::FloorToInt(mVertScroll->getMaxHandleSize() * (height / (float)contentHeight));
+			UINT32 newHandleSize = (UINT32)Math::FloorToInt(mVertScroll->getMaxHandleSize() * (mClippedContentHeight / (float)contentHeight));
 			newHandleSize = std::max(newHandleSize, MinHandleSize);
 
 			UINT32 scrollableHeight = (UINT32)std::max(0, INT32(contentHeight) - INT32(mClippedContentHeight));
@@ -176,7 +176,7 @@ namespace BansheeEngine
 			mHorzScroll->_updateLayout(offset.x, offset.y, mClippedContentWidth, ScrollBarWidth, scrollBarLayoutClipRect, widgetDepth, areaDepth);
 
 			// Set new handle size and update position to match the new size
-			UINT32 newHandleSize = (UINT32)Math::FloorToInt(mHorzScroll->getMaxHandleSize() * (width / (float)contentWidth));
+			UINT32 newHandleSize = (UINT32)Math::FloorToInt(mHorzScroll->getMaxHandleSize() * (mClippedContentWidth / (float)contentWidth));
 			newHandleSize = std::max(newHandleSize, MinHandleSize);
 
 			UINT32 scrollableWidth = (UINT32)std::max(0, INT32(contentWidth) - INT32(mClippedContentWidth));

+ 12 - 2
BansheeEngine/Source/BsGUIScrollBar.cpp

@@ -124,7 +124,12 @@ namespace BansheeEngine
 
 	void GUIScrollBar::upButtonClicked()
 	{
-		float handleOffset = ButtonScrollAmount / (float)mHandleBtn->getScrollableSize();
+		float handleOffset = 0.0f;
+		float scrollableSize = (float)mHandleBtn->getScrollableSize();
+		
+		if(scrollableSize > 0.0f)
+			handleOffset = ButtonScrollAmount / scrollableSize;
+
 		float newHandlePos = std::max(0.0f, mHandleBtn->getHandlePos() - handleOffset);
 
 		mHandleBtn->setHandlePos(newHandlePos);
@@ -135,7 +140,12 @@ namespace BansheeEngine
 
 	void GUIScrollBar::downButtonClicked()
 	{
-		float handleOffset = ButtonScrollAmount / (float)mHandleBtn->getScrollableSize();
+		float handleOffset = 0.0f;
+		float scrollableSize = (float)mHandleBtn->getScrollableSize();
+
+		if(scrollableSize > 0.0f)
+			handleOffset = ButtonScrollAmount / scrollableSize;
+
 		float newHandlePos = std::min(1.0f, mHandleBtn->getHandlePos() + handleOffset);
 
 		mHandleBtn->setHandlePos(newHandlePos);

+ 14 - 3
BansheeEngine/Source/BsGUIScrollBarHandle.cpp

@@ -73,7 +73,11 @@ namespace BansheeEngine
 	float GUIScrollBarHandle::getHandlePos() const
 	{
 		UINT32 maxScrollAmount = getMaxSize() - mHandleSize;
-		return mHandlePos / maxScrollAmount;
+
+		if(maxScrollAmount > 0.0f)
+			return mHandlePos / maxScrollAmount;
+		else
+			return 0.0f;
 	}
 
 	UINT32 GUIScrollBarHandle::getScrollableSize() const
@@ -230,7 +234,10 @@ namespace BansheeEngine
 
 			if(!onHandleMoved.empty())
 			{
-				float pct = mHandlePos / maxScrollAmount;
+				float pct = 0.0f;
+				if(maxScrollAmount > 0.0f)
+					pct = mHandlePos / maxScrollAmount;
+				
 				onHandleMoved(pct);
 			}
 
@@ -283,7 +290,11 @@ namespace BansheeEngine
 
 			if(!onHandleMoved.empty())
 			{
-				float pct = (float)mHandlePos / maxScrollAmount;
+				float pct = 0.0f;
+				
+				if(maxScrollAmount > 0.0f)
+					pct = (float)mHandlePos / maxScrollAmount;
+
 				onHandleMoved(pct);
 			}