From b11e3d74cc683f5afdde641a8d39215ca0372a28 Mon Sep 17 00:00:00 2001 From: andyque Date: Wed, 17 Dec 2014 16:46:48 +0800 Subject: [PATCH] restrict capInsets of ui::ImageView, ui::Slider and ui::LoadinbBar --- cocos/ui/UIHelper.h | 9 +++++++-- cocos/ui/UIImageView.cpp | 5 +++-- cocos/ui/UILoadingBar.cpp | 5 +++-- cocos/ui/UISlider.cpp | 10 ++++++---- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/cocos/ui/UIHelper.h b/cocos/ui/UIHelper.h index 5f86288960..3562dec588 100644 --- a/cocos/ui/UIHelper.h +++ b/cocos/ui/UIHelper.h @@ -83,9 +83,14 @@ public: static void changeLayoutSystemActiveState(bool bActive); -private: + /** + *@brief restrict capInsetSize, when the capInsets's width is larger than the textureSize, it will restrict to 0, + * the height goes the same way as width. + *@param capInsets A user defined capInsets. + *@param textureSize The size of a scale9enabled texture + *@return a restricted capInset. + */ static Rect restrictCapInsetRect(const Rect& capInsets, const Size& textureSize); - friend class Button; }; } diff --git a/cocos/ui/UIImageView.cpp b/cocos/ui/UIImageView.cpp index 36f0d222eb..b591219d03 100644 --- a/cocos/ui/UIImageView.cpp +++ b/cocos/ui/UIImageView.cpp @@ -24,6 +24,7 @@ THE SOFTWARE. #include "ui/UIImageView.h" #include "ui/UIScale9Sprite.h" +#include "ui/UIHelper.h" #include "2d/CCSprite.h" NS_CC_BEGIN @@ -203,12 +204,12 @@ void ImageView::ignoreContentAdaptWithSize(bool ignore) void ImageView::setCapInsets(const Rect &capInsets) { - _capInsets = capInsets; + _capInsets = ui::Helper::restrictCapInsetRect(capInsets, _imageTextureSize); if (!_scale9Enabled) { return; } - _imageRenderer->setCapInsets(capInsets); + _imageRenderer->setCapInsets(_capInsets); } const Rect& ImageView::getCapInsets()const diff --git a/cocos/ui/UILoadingBar.cpp b/cocos/ui/UILoadingBar.cpp index 42a2eb7eaf..aa610196ac 100644 --- a/cocos/ui/UILoadingBar.cpp +++ b/cocos/ui/UILoadingBar.cpp @@ -23,6 +23,7 @@ THE SOFTWARE. ****************************************************************************/ #include "ui/UILoadingBar.h" +#include "ui/UIHelper.h" #include "ui/UIScale9Sprite.h" #include "2d/CCSprite.h" @@ -225,12 +226,12 @@ bool LoadingBar::isScale9Enabled()const void LoadingBar::setCapInsets(const Rect &capInsets) { - _capInsets = capInsets; + _capInsets = ui::Helper::restrictCapInsetRect(capInsets, _barRendererTextureSize); if (!_scale9Enabled) { return; } - _barRenderer->setCapInsets(capInsets); + _barRenderer->setCapInsets(_capInsets); } const Rect& LoadingBar::getCapInsets()const diff --git a/cocos/ui/UISlider.cpp b/cocos/ui/UISlider.cpp index a23bde5e62..9bb26854ef 100644 --- a/cocos/ui/UISlider.cpp +++ b/cocos/ui/UISlider.cpp @@ -24,6 +24,7 @@ THE SOFTWARE. #include "ui/UISlider.h" #include "ui/UIScale9Sprite.h" +#include "ui/UIHelper.h" #include "2d/CCSprite.h" NS_CC_BEGIN @@ -222,12 +223,12 @@ void Slider::setCapInsets(const Rect &capInsets) void Slider::setCapInsetsBarRenderer(const Rect &capInsets) { - _capInsetsBarRenderer = capInsets; + _capInsetsBarRenderer = ui::Helper::restrictCapInsetRect(capInsets, _barRenderer->getContentSize()); if (!_scale9Enabled) { return; } - _barRenderer->setCapInsets(capInsets); + _barRenderer->setCapInsets(_capInsetsBarRenderer); } const Rect& Slider::getCapInsetsBarRenderer()const @@ -237,12 +238,13 @@ const Rect& Slider::getCapInsetsBarRenderer()const void Slider::setCapInsetProgressBarRebderer(const Rect &capInsets) { - _capInsetsProgressBarRenderer = capInsets; + _capInsetsProgressBarRenderer = ui::Helper::restrictCapInsetRect(capInsets, _progressBarRenderer->getContentSize()); + if (!_scale9Enabled) { return; } - _progressBarRenderer->setCapInsets(capInsets); + _progressBarRenderer->setCapInsets(_capInsetsProgressBarRenderer); } const Rect& Slider::getCapInsetsProgressBarRebderer()const