Merge pull request #7780 from andyque/addOptionsForHardCodedValue

add customization to UIButton and UIPageView
This commit is contained in:
minggo 2014-08-15 15:16:12 +08:00
commit 8364e179a4
11 changed files with 281 additions and 9 deletions

View File

@ -64,6 +64,7 @@ _normalTextureScaleXInSize(1.0f),
_normalTextureScaleYInSize(1.0f),
_pressedTextureScaleXInSize(1.0f),
_pressedTextureScaleYInSize(1.0f),
_zoomScale(0.1f),
_normalTextureLoaded(false),
_pressedTextureLoaded(false),
_disabledTextureLoaded(false),
@ -386,9 +387,9 @@ void Button::onPressStateChangedToPressed()
{
_buttonNormalRenderer->stopAllActions();
_buttonClickedRenderer->stopAllActions();
Action *zoomAction = ScaleTo::create(0.05f, _pressedTextureScaleXInSize + 0.1f, _pressedTextureScaleYInSize + 0.1f);
Action *zoomAction = ScaleTo::create(0.05f, _pressedTextureScaleXInSize + _zoomScale, _pressedTextureScaleYInSize + _zoomScale);
_buttonClickedRenderer->runAction(zoomAction);
_buttonNormalRenderer->setScale(_pressedTextureScaleXInSize + 0.1f, _pressedTextureScaleYInSize + 0.1f);
_buttonNormalRenderer->setScale(_pressedTextureScaleXInSize + _zoomScale, _pressedTextureScaleYInSize + _zoomScale);
}
}
else
@ -403,7 +404,7 @@ void Button::onPressStateChangedToPressed()
else
{
_buttonNormalRenderer->stopAllActions();
_buttonNormalRenderer->setScale(_normalTextureScaleXInSize + 0.1f, _normalTextureScaleYInSize + 0.1f);
_buttonNormalRenderer->setScale(_normalTextureScaleXInSize +_zoomScale, _normalTextureScaleYInSize + _zoomScale);
}
}
}
@ -643,6 +644,16 @@ float Button::getTitleFontSize() const
{
return _fontSize;
}
void Button::setZoomScale(float scale)
{
_zoomScale = scale;
}
float Button::getZoomScale()const
{
return _zoomScale;
}
void Button::setTitleFontName(const std::string& fontName)
{
@ -693,6 +704,7 @@ void Button::copySpecialProperties(Widget *widget)
setTitleFontSize(button->getTitleFontSize());
setTitleColor(button->getTitleColor());
setPressedActionEnabled(button->_pressedActionEnabled);
setZoomScale(button->_zoomScale);
}
}

View File

@ -188,6 +188,14 @@ public:
float getTitleFontSize() const;
void setTitleFontName(const std::string& fontName);
const std::string& getTitleFontName() const;
/** When user pressed the button, the button will zoom to a scale.
* The final scale of the button equals (button original scale + _zoomScale)
*/
void setZoomScale(float scale);
/**
* @brief Return a zoom scale
*/
float getZoomScale()const;
CC_CONSTRUCTOR_ACCESS:
virtual bool init() override;
@ -222,7 +230,8 @@ protected:
Scale9Sprite* _buttonClickedRenderer;
Scale9Sprite* _buttonDisableRenderer;
Label* _titleRenderer;
float _zoomScale;
std::string _normalFileName;
std::string _clickedFileName;
std::string _disabledFileName;

View File

@ -44,7 +44,9 @@ _autoScrollDirection(AutoScrollDirection::LEFT),
_childFocusCancelOffset(5.0f),
_pageViewEventListener(nullptr),
_pageViewEventSelector(nullptr),
_eventCallback(nullptr)
_eventCallback(nullptr),
_customScrollThreshold(0.0),
_usingCustomScrollThreshold(false)
{
this->setTouchEnabled(true);
}
@ -447,6 +449,28 @@ void PageView::handleMoveLogic(Touch *touch)
}
scrollPages(offset);
}
void PageView::setCustomScrollThreshold(float threshold)
{
CCASSERT(threshold > 0, "Invalid threshold!");
_customScrollThreshold = threshold;
this->setUsingCustomScrollThreshold(true);
}
float PageView::getCustomScrollThreshold()const
{
return _customScrollThreshold;
}
void PageView::setUsingCustomScrollThreshold(bool flag)
{
_usingCustomScrollThreshold = flag;
}
bool PageView::isUsingCustomScrollThreshold()const
{
return _usingCustomScrollThreshold;
}
void PageView::handleReleaseLogic(Touch *touch)
{
@ -461,7 +485,10 @@ void PageView::handleReleaseLogic(Touch *touch)
ssize_t pageCount = this->getPageCount();
float curPageLocation = curPagePos.x;
float pageWidth = getContentSize().width;
float boundary = pageWidth/2.0f;
if (!_usingCustomScrollThreshold) {
_customScrollThreshold = pageWidth / 2.0;
}
float boundary = _customScrollThreshold;
if (curPageLocation <= -boundary)
{
if (_curPageIdx >= pageCount-1)
@ -590,6 +617,8 @@ void PageView::copySpecialProperties(Widget *widget)
_eventCallback = pageView->_eventCallback;
_pageViewEventListener = pageView->_pageViewEventListener;
_pageViewEventSelector = pageView->_pageViewEventSelector;
_usingCustomScrollThreshold = pageView->_usingCustomScrollThreshold;
_customScrollThreshold = pageView->_customScrollThreshold;
}
}

View File

@ -168,6 +168,23 @@ public:
virtual std::string getDescription() const override;
virtual void onEnter() override;
/**
* @brief If you don't specify the value, the pageView will scroll when half pageview width reached
*/
void setCustomScrollThreshold(float threshold);
/**
*@brief Return user defined scroll page threshold
*/
float getCustomScrollThreshold()const;
/**
*@brief Set using user defined scroll page threshold or not
* If you set it to false, then the default scroll threshold is pageView.width / 2
*/
void setUsingCustomScrollThreshold(bool flag);
/**
*@brief Query whether we are using user defined scroll page threshold or not
*/
bool isUsingCustomScrollThreshold()const;
CC_CONSTRUCTOR_ACCESS:
virtual bool init() override;
@ -219,7 +236,9 @@ protected:
float _leftBoundary;
float _rightBoundary;
float _customScrollThreshold;
bool _usingCustomScrollThreshold;
float _childFocusCancelOffset;
Ref* _pageViewEventListener;

View File

@ -63,7 +63,7 @@ g_guisTests[] =
UISceneManager* sceneManager = UISceneManager::sharedUISceneManager();
sceneManager->setCurrentUISceneId(kUIButtonTest);
sceneManager->setMinUISceneId(kUIButtonTest);
sceneManager->setMaxUISceneId(kUIButtonTestSwitchScale9);
sceneManager->setMaxUISceneId(kUIButtonTestZoomScale);
Scene* scene = sceneManager->currentUIScene();
Director::getInstance()->replaceScene(scene);
}
@ -195,7 +195,7 @@ g_guisTests[] =
UISceneManager* sceneManager = UISceneManager::sharedUISceneManager();
sceneManager->setCurrentUISceneId(kUIPageViewTest);
sceneManager->setMinUISceneId(kUIPageViewTest);
sceneManager->setMaxUISceneId(kUIPageViewButtonTest);
sceneManager->setMaxUISceneId(kUIPageViewCustomScrollThreshold);
Scene* scene = sceneManager->currentUIScene();
Director::getInstance()->replaceScene(scene);
}

View File

@ -520,3 +520,71 @@ void UIButtonTestSwitchScale9::touchEvent(Ref *pSender, Widget::TouchEventType t
break;
}
}
// UIButtonTestZoomScale
UIButtonTestZoomScale::UIButtonTestZoomScale()
: _displayValueLabel(nullptr)
{
}
UIButtonTestZoomScale::~UIButtonTestZoomScale()
{
}
bool UIButtonTestZoomScale::init()
{
if (UIScene::init())
{
Size widgetSize = _widget->getContentSize();
// Add a label in which the button events will be displayed
_displayValueLabel = Text::create("Zoom Scale: 0.1", "fonts/Marker Felt.ttf",32);
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
_displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 20));
_uiLayer->addChild(_displayValueLabel);
// Add the alert
Text* alert = Text::create("Button","fonts/Marker Felt.ttf",30);
alert->setColor(Color3B(159, 168, 176));
alert->setPosition(Vec2(widgetSize.width / 2.0f,
widgetSize.height / 2.0f - alert->getContentSize().height * 1.75f));
_uiLayer->addChild(alert);
// Create the button
Button* button = Button::create("cocosui/animationbuttonnormal.png",
"cocosui/animationbuttonpressed.png");
button->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 20));
button->setPressedActionEnabled(true);
button->setName("button");
_uiLayer->addChild(button);
button->setZoomScale(-0.5);
Slider* slider = Slider::create();
slider->loadBarTexture("cocosui/sliderTrack.png");
slider->loadSlidBallTextures("cocosui/sliderThumb.png", "cocosui/sliderThumb.png", "");
slider->loadProgressBarTexture("cocosui/sliderProgress.png");
slider->setPosition(Vec2(widgetSize.width / 2.0f , widgetSize.height / 2.0f - 20));
slider->addEventListener(CC_CALLBACK_2(UIButtonTestZoomScale::sliderEvent, this));
slider->setPercent(button->getZoomScale()*100);
_uiLayer->addChild(slider);
return true;
}
return false;
}
void UIButtonTestZoomScale::sliderEvent(Ref *pSender, Slider::EventType type)
{
if (type == Slider::EventType::ON_PERCENTAGE_CHANGED)
{
Slider* slider = dynamic_cast<Slider*>(pSender);
int percent = slider->getPercent();
Button* btn = (Button*)_uiLayer->getChildByName("button");
float zoomScale = percent * 0.01;
btn->setZoomScale(zoomScale);
_displayValueLabel->setString(String::createWithFormat("Zoom Scale: %f", zoomScale)->getCString());
}
}

View File

@ -105,4 +105,17 @@ protected:
Text* _displayValueLabel;
};
class UIButtonTestZoomScale : public UIScene
{
public:
UIButtonTestZoomScale();
~UIButtonTestZoomScale();
bool init();
void sliderEvent(Ref* pSender, Slider::EventType type);
protected:
UI_SCENE_CREATE_FUNC(UIButtonTestZoomScale)
Text* _displayValueLabel;
};
#endif /* defined(__TestCpp__UIButtonTest__) */

View File

@ -208,3 +208,103 @@ void UIPageViewButtonTest::pageViewEvent(Ref *pSender, PageView::EventType type)
break;
}
}
// UIPageViewCustomScrollThreshold
UIPageViewCustomScrollThreshold::UIPageViewCustomScrollThreshold()
: _displayValueLabel(nullptr)
{
}
UIPageViewCustomScrollThreshold::~UIPageViewCustomScrollThreshold()
{
}
bool UIPageViewCustomScrollThreshold::init()
{
if (UIScene::init())
{
Size widgetSize = _widget->getContentSize();
// Add a label in which the dragpanel events will be displayed
_displayValueLabel = Text::create("Scroll Threshold", "fonts/Marker Felt.ttf", 32);
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
_displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f,
widgetSize.height / 2.0f +
_displayValueLabel->getContentSize().height * 1.5));
_uiLayer->addChild(_displayValueLabel);
// Add the black background
Text* alert = Text::create("PageView", "fonts/Marker Felt.ttf", 30);
alert->setColor(Color3B(159, 168, 176));
alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f));
_uiLayer->addChild(alert);
Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
Layout* background = dynamic_cast<Layout*>(root->getChildByName("background_Panel"));
// Create the page view
PageView* pageView = PageView::create();
pageView->setContentSize(Size(240.0f, 100.0f));
Size backgroundSize = background->getContentSize();
pageView->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f +
(backgroundSize.width - pageView->getContentSize().width) / 2.0f,
(widgetSize.height - backgroundSize.height) / 2.0f +
(backgroundSize.height - pageView->getContentSize().height) / 2.0f + 20));
int pageCount = 4;
for (int i = 0; i < pageCount; ++i)
{
Layout* layout = Layout::create();
layout->setContentSize(Size(240.0f, 130.0f));
ImageView* imageView = ImageView::create("cocosui/scrollviewbg.png");
imageView->setScale9Enabled(true);
imageView->setContentSize(Size(240, 130));
imageView->setPosition(Vec2(layout->getContentSize().width / 2.0f, layout->getContentSize().height / 2.0f));
layout->addChild(imageView);
Text* label = Text::create(StringUtils::format("page %d",(i+1)), "fonts/Marker Felt.ttf", 30);
label->setColor(Color3B(192, 192, 192));
label->setPosition(Vec2(layout->getContentSize().width / 2.0f, layout->getContentSize().height / 2.0f));
layout->addChild(label);
pageView->insertPage(layout,i);
}
_uiLayer->addChild(pageView);
pageView->setName("pageView");
Slider* slider = Slider::create();
slider->loadBarTexture("cocosui/sliderTrack.png");
slider->loadSlidBallTextures("cocosui/sliderThumb.png", "cocosui/sliderThumb.png", "");
slider->loadProgressBarTexture("cocosui/sliderProgress.png");
slider->setPosition(Vec2(widgetSize.width / 2.0f , widgetSize.height / 2.0f - 40));
slider->addEventListener(CC_CALLBACK_2(UIPageViewCustomScrollThreshold::sliderEvent, this));
slider->setPercent(50);
_uiLayer->addChild(slider);
return true;
}
return false;
}
void UIPageViewCustomScrollThreshold::sliderEvent(Ref *pSender, Slider::EventType type)
{
if (type == Slider::EventType::ON_PERCENTAGE_CHANGED)
{
Slider* slider = dynamic_cast<Slider*>(pSender);
int percent = slider->getPercent();
PageView* pageView = (PageView*)_uiLayer->getChildByName("pageView");
if (percent == 0) {
percent = 1;
}
pageView->setCustomScrollThreshold(percent * 0.01 * pageView->getContentSize().width);
_displayValueLabel->setString(String::createWithFormat("Scroll Threshold: %f", pageView->getCustomScrollThreshold())->getCString());
}
}

View File

@ -55,4 +55,19 @@ protected:
Text* _displayValueLabel;
};
class UIPageViewCustomScrollThreshold : public UIScene
{
public:
UIPageViewCustomScrollThreshold();
~UIPageViewCustomScrollThreshold();
bool init();
void sliderEvent(Ref* pSender, Slider::EventType type);
void pageViewEvent(Ref* pSender, PageView::EventType type);
protected:
UI_SCENE_CREATE_FUNC(UIPageViewCustomScrollThreshold)
Text* _displayValueLabel;
};
#endif /* defined(__TestCpp__UIPageViewTest__) */

View File

@ -34,6 +34,7 @@ static const char* s_testArray[] =
"UIButtonTest_Title",
"UIButtonTest_RemoveSelf",
"UIButtonTestSwitchScale9",
"UIButtonTestZoomScale",
"UICheckBoxTest",
"UISliderTest",
"UISliderTest_Scale9",
@ -181,6 +182,8 @@ Scene *UISceneManager::currentUIScene()
return UIButtonTestRemoveSelf::sceneWithTitle(s_testArray[_currentUISceneId]);
case kUIButtonTestSwitchScale9:
return UIButtonTestSwitchScale9::sceneWithTitle(s_testArray[_currentUISceneId]);
case kUIButtonTestZoomScale:
return UIButtonTestZoomScale::sceneWithTitle(s_testArray[_currentUISceneId]);
case kUICheckBoxTest:
return UICheckBoxTest::sceneWithTitle(s_testArray[_currentUISceneId]);
@ -290,6 +293,8 @@ Scene *UISceneManager::currentUIScene()
return UIPageViewTest::sceneWithTitle(s_testArray[_currentUISceneId]);
case kUIPageViewButtonTest:
return UIPageViewButtonTest::sceneWithTitle(s_testArray[_currentUISceneId]);
case kUIPageViewCustomScrollThreshold:
return UIPageViewCustomScrollThreshold::sceneWithTitle(s_testArray[_currentUISceneId]);
case kUIListViewTest_Vertical:
return UIListViewTest_Vertical::sceneWithTitle(s_testArray[_currentUISceneId]);

View File

@ -37,6 +37,7 @@ enum
kUIButtonTest_Title,
kUIButtonTest_RemoveSelf,
kUIButtonTestSwitchScale9,
kUIButtonTestZoomScale,
kUICheckBoxTest,
kUISliderTest,
kUISliderTest_Scale9,
@ -76,6 +77,7 @@ enum
kUIScrollViewTest_ScrollToPercentBothDirection_Bounce,
kUIPageViewTest,
kUIPageViewButtonTest,
kUIPageViewCustomScrollThreshold,
kUIListViewTest_Vertical,
kUIListViewTest_Horizontal,
kUIWidgetAddNodeTest,