From e0aa82da138c7a789e3335eac249143b23f78e91 Mon Sep 17 00:00:00 2001 From: CaiWenzhi Date: Wed, 6 Nov 2013 16:04:06 +0800 Subject: [PATCH 1/6] Merge master branch to develop branch --- .../project.pbxproj.REMOVED.git-id | 2 +- .../project.pbxproj.REMOVED.git-id | 2 +- .../cocostudio/CCActionNode.cpp | 2 +- .../cocostudio/CCSGUIReader.cpp | 35 +- .../cocostudio/CCSSceneReader.cpp | 4 +- cocos/gui/CocosGUI.h | 5 +- cocos/gui/Layout.cpp | 785 -------- cocos/gui/UIButton.cpp | 176 +- cocos/gui/UIButton.h | 19 +- cocos/gui/UICheckBox.cpp | 64 +- cocos/gui/UICheckBox.h | 17 +- cocos/gui/UIDragPanel.cpp | 1269 ------------ cocos/gui/UIDragPanel.h | 367 ---- cocos/gui/UIHelper.cpp | 134 +- cocos/gui/UIHelper.h | 65 +- cocos/gui/UIImageView.cpp | 49 +- cocos/gui/UIImageView.h | 16 +- cocos/gui/UIInputManager.cpp | 6 +- cocos/gui/UIInputManager.h | 4 +- cocos/gui/UILabel.cpp | 39 +- cocos/gui/UILabel.h | 13 +- cocos/gui/UILabelAtlas.cpp | 51 +- cocos/gui/UILabelAtlas.h | 19 +- cocos/gui/UILabelBMFont.cpp | 45 +- cocos/gui/UILabelBMFont.h | 9 +- cocos/gui/UILayer.cpp | 4 +- cocos/gui/UILayout.cpp | 1010 ++++++++++ cocos/gui/{Layout.h => UILayout.h} | 47 +- cocos/gui/UILayoutDefine.cpp | 2 +- cocos/gui/UILayoutDefine.h | 28 +- ...outParameter.cpp => UILayoutParameter.cpp} | 39 +- ...{LayoutParameter.h => UILayoutParameter.h} | 42 +- cocos/gui/UIListView.cpp | 1753 ++++------------- cocos/gui/UIListView.h | 294 ++- cocos/gui/UILoadingBar.cpp | 98 +- cocos/gui/UILoadingBar.h | 7 +- cocos/gui/UIPageView.cpp | 151 +- cocos/gui/UIPageView.h | 26 +- cocos/gui/UIRootWidget.cpp | 6 +- cocos/gui/UIRootWidget.h | 10 +- cocos/gui/UIScrollInterface.h | 1 - cocos/gui/UIScrollView.cpp | 1595 +++++++++++---- cocos/gui/UIScrollView.h | 216 +- cocos/gui/UISlider.cpp | 138 +- cocos/gui/UISlider.h | 9 +- cocos/gui/UITextField.cpp | 149 +- cocos/gui/UITextField.h | 46 +- cocos/gui/UIWidget.cpp | 305 +-- cocos/gui/UIWidget.h | 71 +- .../CocoStudioGUITest/CocosGUIScene.cpp | 1 - .../UIDragPanelTest/UIDragPanelTest.cpp | 160 +- .../UIDragPanelTest/UIDragPanelTest.h | 2 - .../UIListViewTest/UIListViewTest.cpp | 202 +- .../UIListViewTest/UIListViewTest.h | 2 - .../UIPageViewTest/UIPageViewTest.cpp | 4 +- .../UIPanelTest/UIPanelTest.cpp | 56 +- .../CocoStudioGUITest/UIScene.cpp | 3 +- .../CocoStudioGUITest/UIScene.h | 2 +- .../UIScrollViewTest/UIScrollViewTest.cpp | 7 +- .../UITextFieldTest/UITextFieldTest.cpp | 10 +- .../CocoStudioSceneTest/SceneEditorTest.cpp | 1 - 61 files changed, 4104 insertions(+), 5590 deletions(-) delete mode 100644 cocos/gui/Layout.cpp delete mode 100644 cocos/gui/UIDragPanel.cpp delete mode 100644 cocos/gui/UIDragPanel.h create mode 100644 cocos/gui/UILayout.cpp rename cocos/gui/{Layout.h => UILayout.h} (89%) rename cocos/gui/{LayoutParameter.cpp => UILayoutParameter.cpp} (65%) rename cocos/gui/{LayoutParameter.h => UILayoutParameter.h} (82%) diff --git a/build/cocos2d_libs.xcodeproj/project.pbxproj.REMOVED.git-id b/build/cocos2d_libs.xcodeproj/project.pbxproj.REMOVED.git-id index ac519a56c8..0d15fff578 100644 --- a/build/cocos2d_libs.xcodeproj/project.pbxproj.REMOVED.git-id +++ b/build/cocos2d_libs.xcodeproj/project.pbxproj.REMOVED.git-id @@ -1 +1 @@ -5a0361664f33af4303065c98da4b8d8e4efe48b4 \ No newline at end of file +12034db6a7c20a09b3e866ab79a536bfc0208d3f \ No newline at end of file diff --git a/build/cocos2d_samples.xcodeproj/project.pbxproj.REMOVED.git-id b/build/cocos2d_samples.xcodeproj/project.pbxproj.REMOVED.git-id index ba37de5f5d..07c8b22d15 100644 --- a/build/cocos2d_samples.xcodeproj/project.pbxproj.REMOVED.git-id +++ b/build/cocos2d_samples.xcodeproj/project.pbxproj.REMOVED.git-id @@ -1 +1 @@ -b473303312be3b69891020b5fb470dd382f31284 \ No newline at end of file +f441e32e09388d5c434035e100e591cf9a6d29fd \ No newline at end of file diff --git a/cocos/editor-support/cocostudio/CCActionNode.cpp b/cocos/editor-support/cocostudio/CCActionNode.cpp index fceca8816c..c0484a9dd8 100644 --- a/cocos/editor-support/cocostudio/CCActionNode.cpp +++ b/cocos/editor-support/cocostudio/CCActionNode.cpp @@ -159,7 +159,7 @@ void ActionNode::initActionNodeFromRoot(Object* root) UIWidget* rootWidget = dynamic_cast(root); if (rootWidget != NULL) { - UIWidget* widget = UIHelper::instance()->seekActionWidgetByActionTag(rootWidget, getActionTag()); + UIWidget* widget = UIHelper::seekActionWidgetByActionTag(rootWidget, getActionTag()); if (widget != NULL) { setObject(widget); diff --git a/cocos/editor-support/cocostudio/CCSGUIReader.cpp b/cocos/editor-support/cocostudio/CCSGUIReader.cpp index 77cb5d56de..8fd5fba3a1 100755 --- a/cocos/editor-support/cocostudio/CCSGUIReader.cpp +++ b/cocos/editor-support/cocostudio/CCSGUIReader.cpp @@ -152,7 +152,7 @@ UIWidget* CCSGUIReader::widgetFromJsonDictionary(JsonDictionary* data) } else if (classname && strcmp(classname, "Panel") == 0) { - widget = Layout::create(); + widget = UILayout::create(); setPropsForPanelFromJsonDictionary(widget, uiOptions); } else if (classname && strcmp(classname, "Slider") == 0) @@ -177,7 +177,7 @@ UIWidget* CCSGUIReader::widgetFromJsonDictionary(JsonDictionary* data) } else if (classname && strcmp(classname, "DragPanel") == 0) { - widget = UIDragPanel::create(); + widget = UIScrollView::create(); setPropsForDragPanelFromJsonDictionary(widget, uiOptions); } @@ -232,20 +232,20 @@ UIWidget* CCSGUIReader::widgetFromJsonFile(const char *fileName) const char* file = DICTOOL->getStringValueFromArray_json(jsonDict, "textures", i); std::string tp = m_strFilePath; tp.append(file); - CCUIHELPER->addSpriteFrame(tp.c_str()); +// CCUIHELPER->addSpriteFrame(tp.c_str()); } float fileDesignWidth = DICTOOL->getFloatValue_json(jsonDict, "designWidth"); float fileDesignHeight = DICTOOL->getFloatValue_json(jsonDict, "designHeight"); if (fileDesignWidth <= 0 || fileDesignHeight <= 0) { printf("Read design size error!\n"); Size winSize = Director::getInstance()->getWinSize(); - CCUIHELPER->setFileDesignWidth(winSize.width); - CCUIHELPER->setFileDesignHeight(winSize.height); +// CCUIHELPER->setFileDesignWidth(winSize.width); +// CCUIHELPER->setFileDesignHeight(winSize.height); } else { - CCUIHELPER->setFileDesignWidth(fileDesignWidth); - CCUIHELPER->setFileDesignHeight(fileDesignHeight); +// CCUIHELPER->setFileDesignWidth(fileDesignWidth); +// CCUIHELPER->setFileDesignHeight(fileDesignHeight); } JsonDictionary* widgetTree = DICTOOL->getSubDictionary_json(jsonDict, "widgetTree"); UIWidget* widget = widgetFromJsonDictionary(widgetTree); @@ -253,7 +253,7 @@ UIWidget* CCSGUIReader::widgetFromJsonFile(const char *fileName) /* *********temp********* */ if (widget->getContentSize().equals(Size::ZERO)) { - Layout* rootWidget = dynamic_cast(widget); + UILayout* rootWidget = dynamic_cast(widget); rootWidget->setSize(Size(fileDesignWidth, fileDesignHeight)); } /* ********************** */ @@ -792,7 +792,7 @@ void CCSGUIReader::setPropsForLabelFromJsonDictionary(UIWidget*widget,JsonDictio setPropsForWidgetFromJsonDictionary(widget, options); UILabel* label = (UILabel*)widget; bool touchScaleChangeAble = DICTOOL->getBooleanValue_json(options, "touchScaleEnable"); - label->setTouchScaleChangeAble(touchScaleChangeAble); + label->setTouchScaleChangeEnabled(touchScaleChangeAble); const char* text = DICTOOL->getStringValue_json(options, "text"); label->setText(text); bool fs = DICTOOL->checkObjectExist_json(options, "fontSize"); @@ -877,10 +877,9 @@ void CCSGUIReader::setPropsForLabelAtlasFromJsonDictionary(UIWidget*widget,JsonD void CCSGUIReader::setPropsForContainerWidgetFromJsonDictionary(UIWidget *widget, JsonDictionary *options) { setPropsForWidgetFromJsonDictionary(widget, options); - Layout* containerWidget = (Layout*)widget; + UILayout* containerWidget = (UILayout*)widget; if (!dynamic_cast(containerWidget) - && !dynamic_cast(containerWidget) - && !dynamic_cast(containerWidget)) + && !dynamic_cast(containerWidget)) { containerWidget->setClippingEnabled(DICTOOL->getBooleanValue_json(options, "clipAble")); } @@ -892,7 +891,7 @@ void CCSGUIReader::setPropsForPanelFromJsonDictionary(UIWidget*widget,JsonDictio if (m_bOlderVersion) { setPropsForContainerWidgetFromJsonDictionary(widget, options); - Layout* panel = (Layout*)widget; + UILayout* panel = (UILayout*)widget; bool backGroundScale9Enable = DICTOOL->getBooleanValue_json(options, "backGroundScale9Enable"); panel->setBackGroundImageScale9Enabled(backGroundScale9Enable); int cr = DICTOOL->getIntValue_json(options, "bgColorR"); @@ -959,7 +958,7 @@ void CCSGUIReader::setPropsForPanelFromJsonDictionary(UIWidget*widget,JsonDictio else { setPropsForContainerWidgetFromJsonDictionary(widget, options); - Layout* panel = (Layout*)widget; + UILayout* panel = (UILayout*)widget; bool backGroundScale9Enable = DICTOOL->getBooleanValue_json(options, "backGroundScale9Enable"); panel->setBackGroundImageScale9Enabled(backGroundScale9Enable); int cr = DICTOOL->getIntValue_json(options, "bgColorR"); @@ -1394,7 +1393,7 @@ void CCSGUIReader::setPropsForTextFieldFromJsonDictionary(UIWidget*widget,JsonDi //textField->setSize(CCSizeMake(dw, dh)); } bool maxLengthEnable = DICTOOL->getBooleanValue_json(options, "maxLengthEnable"); - textField->setMaxLengthEnable(maxLengthEnable); + textField->setMaxLengthEnabled(maxLengthEnable); if (maxLengthEnable) { @@ -1402,7 +1401,7 @@ void CCSGUIReader::setPropsForTextFieldFromJsonDictionary(UIWidget*widget,JsonDi textField->setMaxLength(maxLength); } bool passwordEnable = DICTOOL->getBooleanValue_json(options, "passwordEnable"); - textField->setPasswordEnable(passwordEnable); + textField->setPasswordEnabled(passwordEnable); if (passwordEnable) { textField->setPasswordStyleText(DICTOOL->getStringValue_json(options, "passwordStyleText")); @@ -1557,10 +1556,10 @@ void CCSGUIReader::setPropsForDragPanelFromJsonDictionary(UIWidget *widget, Json { setPropsForPanelFromJsonDictionary(widget, options); - UIDragPanel* dragPanel = (UIDragPanel*)widget; + UIScrollView* dragPanel = (UIScrollView*)widget; bool bounceEnable = DICTOOL->getBooleanValue_json(options, "bounceEnable"); - dragPanel->setBounceEnable(bounceEnable); + dragPanel->setBounceEnabled(bounceEnable); float innerWidth = DICTOOL->getFloatValue_json(options, "innerWidth"); float innerHeight = DICTOOL->getFloatValue_json(options, "innerHeight"); diff --git a/cocos/editor-support/cocostudio/CCSSceneReader.cpp b/cocos/editor-support/cocostudio/CCSSceneReader.cpp index 3d8bc7fd8c..c660b91990 100644 --- a/cocos/editor-support/cocostudio/CCSSceneReader.cpp +++ b/cocos/editor-support/cocostudio/CCSSceneReader.cpp @@ -320,8 +320,8 @@ namespace cocostudio { { gui::UILayer *pLayer = gui::UILayer::create(); pLayer->scheduleUpdate(); - UIWidget* widget= gui::UIHelper::instance()->createWidgetFromJsonFile(pPath.c_str()); - pLayer->addWidget(widget); +// UIWidget* widget= gui::UIHelper::instance()->createWidgetFromJsonFile(pPath.c_str()); +// pLayer->addWidget(widget); ComRender *pRender = ComRender::create(pLayer, "GUIComponent"); if (pComName != NULL) { diff --git a/cocos/gui/CocosGUI.h b/cocos/gui/CocosGUI.h index 497c4f89b6..663aff26e7 100644 --- a/cocos/gui/CocosGUI.h +++ b/cocos/gui/CocosGUI.h @@ -27,7 +27,7 @@ #include "gui/UIWidget.h" -#include "gui/Layout.h" +#include "gui/UILayout.h" #include "gui/UIRootWidget.h" #include "gui/UIButton.h" #include "gui/UICheckBox.h" @@ -36,10 +36,9 @@ #include "gui/UILabelAtlas.h" #include "gui/UILoadingBar.h" #include "gui/UIScrollView.h" +#include "gui/UIListView.h" #include "gui/UISlider.h" #include "gui/UITextField.h" -#include "gui/UIListView.h" -#include "gui/UIDragPanel.h" #include "gui/UILabelBMFont.h" #include "gui/UIPageView.h" #include "gui/UIHelper.h" diff --git a/cocos/gui/Layout.cpp b/cocos/gui/Layout.cpp deleted file mode 100644 index a9e4f75a30..0000000000 --- a/cocos/gui/Layout.cpp +++ /dev/null @@ -1,785 +0,0 @@ -/**************************************************************************** - Copyright (c) 2013 cocos2d-x.org - - http://www.cocos2d-x.org - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in - all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. - ****************************************************************************/ - -#include "gui/Layout.h" -#include "gui/UILayer.h" -#include "gui/UIHelper.h" -#include "extensions/GUI/CCControlExtension/CCScale9Sprite.h" - - using namespace cocos2d; - using namespace cocos2d::extension; - -namespace gui { - -#define DYNAMIC_CAST_CLIPPINGLAYER dynamic_cast(_renderer) - -Layout::Layout(): -_clippingEnabled(false), -_backGroundScale9Enabled(false), -_backGroundImage(NULL), -_backGroundImageFileName(""), -_backGroundImageCapInsets(Rect::ZERO), -_colorType(LAYOUT_COLOR_NONE), -_bgImageTexType(UI_TEX_TYPE_LOCAL), -_colorRender(NULL), -_gradientRender(NULL), -_cColor(Color3B::WHITE), -_gStartColor(Color3B::WHITE), -_gEndColor(Color3B::WHITE), -_alongVector(Point(0.0f, -1.0f)), -_cOpacity(255), -_backGroundImageTextureSize(Size::ZERO), -_layoutType(LAYOUT_ABSOLUTE) -{ - _widgetType = WidgetTypeContainer; -} - -Layout::~Layout() -{ -} - -Layout* Layout::create() -{ - Layout* layout = new Layout(); - if (layout && layout->init()) - { - layout->autorelease(); - return layout; - } - CC_SAFE_DELETE(layout); - return NULL; -} - -bool Layout::init() -{ - _children = CCArray::create(); - _children->retain(); - initRenderer(); - _renderer->retain(); - _renderer->setZOrder(_widgetZOrder); - RGBAProtocol* renderRGBA = dynamic_cast(_renderer); - if (renderRGBA) - { - renderRGBA->setCascadeColorEnabled(false); - renderRGBA->setCascadeOpacityEnabled(false); - } - ignoreContentAdaptWithSize(false); - setSize(Size::ZERO); - setBright(true); - setAnchorPoint(Point(0, 0)); - _scheduler = Director::getInstance()->getScheduler(); - CC_SAFE_RETAIN(_scheduler); - return true; -} - -void Layout::initRenderer() -{ - _renderer = RectClippingNode::create(); -} - -bool Layout::isClippingEnabled() -{ - return _clippingEnabled; -} - -bool Layout::hitTest(const Point &pt) -{ - Point nsp = _renderer->convertToNodeSpace(pt); - Rect bb = Rect(0.0f, 0.0f, _size.width, _size.height); - if (nsp.x >= bb.origin.x && nsp.x <= bb.origin.x + bb.size.width && nsp.y >= bb.origin.y && nsp.y <= bb.origin.y + bb.size.height) - { - return true; - } - return false; -} - -void Layout::setClippingEnabled(bool able) -{ - _clippingEnabled = able; - DYNAMIC_CAST_CLIPPINGLAYER->setClippingEnabled(able); -} - -void Layout::onSizeChanged() -{ - DYNAMIC_CAST_CLIPPINGLAYER->setClippingSize(_size); - doLayout(); - if (_backGroundImage) - { - _backGroundImage->setPosition(Point(_size.width/2.0f, _size.height/2.0f)); - if (_backGroundScale9Enabled) - { - dynamic_cast(_backGroundImage)->setPreferredSize(_size); - } - } - if (_colorRender) - { - _colorRender->setContentSize(_size); - } - if (_gradientRender) - { - _gradientRender->setContentSize(_size); - } -} - -void Layout::setBackGroundImageScale9Enabled(bool able) -{ - if (_backGroundScale9Enabled == able) - { - return; - } - _renderer->removeChild(_backGroundImage, true); - _backGroundImage = NULL; - _backGroundScale9Enabled = able; - if (_backGroundScale9Enabled) - { - _backGroundImage = Scale9Sprite::create(); - _renderer->addChild(_backGroundImage); - } - else - { - _backGroundImage = Sprite::create(); - _renderer->addChild(_backGroundImage); - } - _backGroundImage->setZOrder(-1); - setBackGroundImage(_backGroundImageFileName.c_str(),_bgImageTexType); - setBackGroundImageCapInsets(_backGroundImageCapInsets); -} - -void Layout::setBackGroundImage(const char* fileName,TextureResType texType) -{ - if (!fileName || strcmp(fileName, "") == 0) - { - return; - } - if (_backGroundImage == NULL) - { - addBackGroundImage(); - } - _backGroundImageFileName = fileName; - _bgImageTexType = texType; - if (_backGroundScale9Enabled) - { - switch (_bgImageTexType) - { - case UI_TEX_TYPE_LOCAL: - dynamic_cast(_backGroundImage)->initWithFile(fileName); - break; - case UI_TEX_TYPE_PLIST: - dynamic_cast(_backGroundImage)->initWithSpriteFrameName(fileName); - break; - default: - break; - } - dynamic_cast(_backGroundImage)->setPreferredSize(_size); - } - else - { - switch (_bgImageTexType) - { - case UI_TEX_TYPE_LOCAL: - dynamic_cast(_backGroundImage)->initWithFile(fileName); - break; - case UI_TEX_TYPE_PLIST: - dynamic_cast(_backGroundImage)->initWithSpriteFrameName(fileName); - break; - default: - break; - } - } - if (_backGroundScale9Enabled) - { - dynamic_cast(_backGroundImage)->setColor(getColor()); - dynamic_cast(_backGroundImage)->setOpacity(getOpacity()); - } - else - { - dynamic_cast(_backGroundImage)->setColor(getColor()); - dynamic_cast(_backGroundImage)->setOpacity(getOpacity()); - } - _backGroundImageTextureSize = _backGroundImage->getContentSize(); - _backGroundImage->setPosition(Point(_size.width/2.0f, _size.height/2.0f)); -} - -void Layout::setBackGroundImageCapInsets(const Rect &capInsets) -{ - _backGroundImageCapInsets = capInsets; - if (_backGroundScale9Enabled) - { - dynamic_cast(_backGroundImage)->setCapInsets(capInsets); - } -} - -void Layout::addBackGroundImage() -{ - if (_backGroundScale9Enabled) - { - _backGroundImage = Scale9Sprite::create(); - _backGroundImage->setZOrder(-1); - _renderer->addChild(_backGroundImage); - dynamic_cast(_backGroundImage)->setPreferredSize(_size); - } - else - { - _backGroundImage = CCSprite::create(); - _backGroundImage->setZOrder(-1); - _renderer->addChild(_backGroundImage); - } - _backGroundImage->setPosition(Point(_size.width/2.0f, _size.height/2.0f)); -} - -void Layout::removeBackGroundImage() -{ - if (!_backGroundImage) - { - return; - } - _renderer->removeChild(_backGroundImage, true); - _backGroundImage = NULL; - _backGroundImageFileName = ""; - _backGroundImageTextureSize = Size::ZERO; -} - -void Layout::setBackGroundColorType(LayoutBackGroundColorType type) -{ - if (_colorType == type) - { - return; - } - switch (_colorType) - { - case LAYOUT_COLOR_NONE: - if (_colorRender) - { - _renderer->removeChild(_colorRender, true); - _colorRender = NULL; - } - if (_gradientRender) - { - _renderer->removeChild(_gradientRender, true); - _gradientRender = NULL; - } - break; - case LAYOUT_COLOR_SOLID: - if (_colorRender) - { - _renderer->removeChild(_colorRender, true); - _colorRender = NULL; - } - break; - case LAYOUT_COLOR_GRADIENT: - if (_gradientRender) - { - _renderer->removeChild(_gradientRender, true); - _gradientRender = NULL; - } - break; - default: - break; - } - _colorType = type; - switch (_colorType) - { - case LAYOUT_COLOR_NONE: - break; - case LAYOUT_COLOR_SOLID: - _colorRender = CCLayerColor::create(); - _colorRender->setContentSize(_size); - _colorRender->setOpacity(_cOpacity); - _colorRender->setColor(_cColor); - _renderer->addChild(_colorRender,-2); - break; - case LAYOUT_COLOR_GRADIENT: - _gradientRender = CCLayerGradient::create(); - _gradientRender->setContentSize(_size); - _gradientRender->setOpacity(_cOpacity); - _gradientRender->setStartColor(_gStartColor); - _gradientRender->setEndColor(_gEndColor); - _gradientRender->setVector(_alongVector); - _renderer->addChild(_gradientRender,-2); - break; - default: - break; - } -} - -void Layout::setBackGroundColor(const Color3B &color) -{ - _cColor = color; - if (_colorRender) - { - _colorRender->setColor(color); - } -} - -void Layout::setBackGroundColor(const Color3B &startColor, const Color3B &endColor) -{ - _gStartColor = startColor; - if (_gradientRender) - { - _gradientRender->setStartColor(startColor); - } - _gEndColor = endColor; - if (_gradientRender) - { - _gradientRender->setEndColor(endColor); - } -} - -void Layout::setBackGroundColorOpacity(int opacity) -{ - _cOpacity = opacity; - switch (_colorType) - { - case LAYOUT_COLOR_NONE: - break; - case LAYOUT_COLOR_SOLID: - _colorRender->setOpacity(opacity); - break; - case LAYOUT_COLOR_GRADIENT: - _gradientRender->setOpacity(opacity); - break; - default: - break; - } -} - -void Layout::setBackGroundColorVector(const Point &vector) -{ - _alongVector = vector; - if (_gradientRender) - { - _gradientRender->setVector(vector); - } -} - -void Layout::setColor(const Color3B &color) -{ - UIWidget::setColor(color); - if (_backGroundImage) - { - RGBAProtocol* rgbap = dynamic_cast(_backGroundImage); - if (rgbap) - { - rgbap->setColor(color); - } - } -} - -void Layout::setOpacity(int opacity) -{ - UIWidget::setOpacity(opacity); - if (_backGroundImage) - { - RGBAProtocol* rgbap = dynamic_cast(_backGroundImage); - if (rgbap) - { - rgbap->setOpacity(opacity); - } - } -} - -const Size& Layout::getBackGroundImageTextureSize() const -{ - return _backGroundImageTextureSize; -} - -const Size& Layout::getContentSize() const -{ - return _renderer->getContentSize(); -} - -void Layout::setLayoutType(LayoutType type) -{ - _layoutType = type; -} - -LayoutType Layout::getLayoutType() const -{ - return _layoutType; -} - -void Layout::doLayout() -{ - switch (_layoutType) - { - case LAYOUT_ABSOLUTE: - break; - case LAYOUT_LINEAR_VERTICAL: - { - ccArray* layoutChildrenArray = getChildren()->data; - int length = layoutChildrenArray->num; - Size layoutSize = getSize(); - float topBoundary = layoutSize.height; - for (int i=0; i(layoutChildrenArray->arr[i]); - LinearLayoutParameter* layoutParameter = dynamic_cast(child->getLayoutParameter()); - - if (layoutParameter) - { - WidgetType childType = child->getWidgetType(); - UILinearGravity childGravity = layoutParameter->getGravity(); - Point ap = child->getAnchorPoint(); - Size cs = child->getSize(); - float finalPosX = childType == WidgetTypeWidget ? ap.x * cs.width : 0.0f; - float finalPosY = childType == WidgetTypeWidget ? topBoundary - ((1.0f-ap.y) * cs.height) : topBoundary - cs.height; - switch (childGravity) - { - case LINEAR_GRAVITY_NONE: - case LINEAR_GRAVITY_LEFT: - break; - case LINEAR_GRAVITY_RIGHT: - finalPosX = childType == WidgetTypeWidget ? layoutSize.width - ((1.0f - ap.x) * cs.width) : layoutSize.width - cs.width; - break; - case LINEAR_GRAVITY_CENTER_HORIZONTAL: - finalPosX = childType == WidgetTypeWidget ? layoutSize.width / 2.0f - cs.width * (0.5f-ap.x) : (layoutSize.width - cs.width) * 0.5f; - break; - default: - break; - } - UIMargin mg = layoutParameter->getMargin(); - finalPosX += mg.left; - finalPosY -= mg.top; - child->setPosition(Point(finalPosX, finalPosY)); - topBoundary = child->getBottomInParent() - mg.bottom; - } - } - break; - } - case LAYOUT_LINEAR_HORIZONTAL: - { - ccArray* layoutChildrenArray = getChildren()->data; - int length = layoutChildrenArray->num; - Size layoutSize = getSize(); - float leftBoundary = 0.0f; - for (int i=0; i(layoutChildrenArray->arr[i]); - LinearLayoutParameter* layoutParameter = dynamic_cast(child->getLayoutParameter()); - - if (layoutParameter) - { - WidgetType childType = child->getWidgetType(); - UILinearGravity childGravity = layoutParameter->getGravity(); - Point ap = child->getAnchorPoint(); - Size cs = child->getSize(); - float finalPosX = childType == WidgetTypeWidget ? leftBoundary + (ap.x * cs.width) : leftBoundary; - float finalPosY = childType == WidgetTypeWidget ? layoutSize.height - (1.0f - ap.y) * cs.height : layoutSize.height - cs.height; - switch (childGravity) - { - case LINEAR_GRAVITY_NONE: - case LINEAR_GRAVITY_TOP: - break; - case LINEAR_GRAVITY_BOTTOM: - finalPosY = childType == WidgetTypeWidget ? ap.y * cs.height : 0.0f; - break; - case LINEAR_GRAVITY_CENTER_VERTICAL: - finalPosY = childType == WidgetTypeWidget ? layoutSize.height/2.0f - cs.height * (0.5f - ap.y) : (layoutSize.height - cs.height) * 0.5f; - break; - default: - break; - } - UIMargin mg = layoutParameter->getMargin(); - finalPosX += mg.left; - finalPosY -= mg.top; - child->setPosition(Point(finalPosX, finalPosY)); - leftBoundary = child->getRightInParent() + mg.right; - } - } - break; - } - case LAYOUT_RELATIVE: - { - ccArray* layoutChildrenArray = getChildren()->data; - int length = layoutChildrenArray->num; - Size layoutSize = getSize(); - for (int i=0; i(layoutChildrenArray->arr[i]); - WidgetType childType = child->getWidgetType(); - Point ap = child->getAnchorPoint(); - Size cs = child->getSize(); - RelativeLayoutParameter* layoutParameter = dynamic_cast(child->getLayoutParameter()); - if (layoutParameter) - { - float finalPosX = childType == WidgetTypeWidget ? ap.x * cs.width : 0.0f; - float finalPosY = childType == WidgetTypeWidget ? layoutSize.height - ((1.0f - ap.y) * cs.height) : layoutSize.height - cs.height; - UIRelativeAlign align = layoutParameter->getAlign(); - const char* relativeName = layoutParameter->getRelativeToWidgetName(); - UIWidget* relativeWidget = NULL; - if (relativeName && strcmp(relativeName, "")) - { - relativeWidget = CCUIHELPER->seekWidgetByRelativeName(this, relativeName); - } - switch (align) - { - case RELATIVE_ALIGN_NONE: - break; - case RELATIVE_ALIGN_PARENT_LEFT: - break; - case RELATIVE_ALIGN_PARENT_TOP: - break; - case RELATIVE_ALIGN_PARENT_RIGHT: - finalPosX = childType == WidgetTypeWidget ? layoutSize.width - ((1.0f - ap.x) * cs.width) : layoutSize.width - cs.width; - break; - case RELATIVE_ALIGN_PARENT_BOTTOM: - finalPosY = childType == WidgetTypeWidget ? ap.y * cs.height : 0.0f; - break; - case RELATIVE_CENTER_IN_PARENT: - finalPosX = childType == WidgetTypeWidget ? layoutSize.width * 0.5f - cs.width * (0.5f - ap.x) : (layoutSize.width - cs.width) * 0.5f; - finalPosY = childType == WidgetTypeWidget ? layoutSize.height * 0.5f - cs.height * (0.5f - ap.y) : (layoutSize.height - cs.height) * 0.5f; - break; - case RELATIVE_CENTER_HORIZONTAL: - finalPosX = childType == WidgetTypeWidget ? layoutSize.width * 0.5f - cs.width * (0.5f - ap.x) : (layoutSize.width - cs.width) * 0.5f; - break; - case RELATIVE_CENTER_VERTICAL: - finalPosY = childType == WidgetTypeWidget ? layoutSize.height * 0.5f - cs.height * (0.5f - ap.y) : (layoutSize.height - cs.height) * 0.5f; - break; - case RELATIVE_LOCATION_LEFT_OF_TOPALIGN: - if (relativeWidget) - { - float locationTop = relativeWidget->getTopInParent(); - float locationRight = relativeWidget->getLeftInParent(); - finalPosY = childType == WidgetTypeWidget ? locationTop - ap.y * cs.height : locationTop - cs.height; - finalPosX = childType == WidgetTypeWidget ? locationRight - (1.0f - ap.x) * cs.width : locationRight - cs.width; - } - break; - case RELATIVE_LOCATION_LEFT_OF_CENTER: - break; - case RELATIVE_LOCATION_LEFT_OF_BOTTOMALIGN: - if (relativeWidget) - { - float locationRight = relativeWidget->getLeftInParent(); - float locationBottom = relativeWidget->getBottomInParent(); - finalPosY = childType == WidgetTypeWidget ? locationBottom + ap.y * cs.height : locationBottom; - finalPosX = childType == WidgetTypeWidget ? locationRight - (1.0f - ap.x) * cs.width : locationRight - cs.width; - } - break; - case RELATIVE_LOCATION_RIGHT_OF_TOPALIGN: - if (relativeWidget) - { - float locationTop = relativeWidget->getTopInParent(); - float locationLeft = relativeWidget->getRightInParent(); - finalPosY = childType == WidgetTypeWidget ? locationTop - ap.y * cs.height : locationTop - cs.height; - finalPosX = childType == WidgetTypeWidget ? locationLeft + ap.x * cs.width : locationLeft; - } - break; - case RELATIVE_LOCATION_RIGHT_OF_CENTER: - break; - case RELATIVE_LOCATION_RIGHT_OF_BOTTOMALIGN: - if (relativeWidget) - { - float locationLeft = relativeWidget->getRightInParent(); - float locationBottom = relativeWidget->getBottomInParent(); - finalPosY = childType == WidgetTypeWidget ? locationBottom + ap.y * cs.height : locationBottom; - finalPosX = childType == WidgetTypeWidget ? locationLeft + ap.x * cs.width : locationLeft; - } - break; - case RELATIVE_LOCATION_ABOVE_LEFTALIGN: - if (relativeWidget) - { - float locationBottom = relativeWidget->getTopInParent(); - float locationLeft = relativeWidget->getLeftInParent(); - finalPosY = childType == WidgetTypeWidget ? locationBottom + ap.y * cs.height : locationBottom; - finalPosX = childType == WidgetTypeWidget ? locationLeft + ap.x * cs.width : locationLeft; - } - break; - case RELATIVE_LOCATION_ABOVE_CENTER: - break; - case RELATIVE_LOCATION_ABOVE_RIGHTALIGN: - if (relativeWidget) - { - float locationBottom = relativeWidget->getTopInParent(); - float locationRight = relativeWidget->getRightInParent(); - finalPosY = childType == WidgetTypeWidget ? locationBottom + ap.y * cs.height : locationBottom; - finalPosX = childType == WidgetTypeWidget ? locationRight - (1.0f - ap.x) * cs.width : locationRight - cs.width; - } - break; - case RELATIVE_LOCATION_BELOW_LEFTALIGN: - if (relativeWidget) - { - float locationTop = relativeWidget->getBottomInParent(); - float locationLeft = relativeWidget->getLeftInParent(); - finalPosY = childType == WidgetTypeWidget ? locationTop - (1.0f - ap.y) * cs.height : locationTop - cs.height; - finalPosX = childType == WidgetTypeWidget ? locationLeft + ap.x * cs.width : locationLeft; - } - break; - case RELATIVE_LOCATION_BELOW_CENTER: - break; - case RELATIVE_LOCATION_BELOW_RIGHTALIGN: - if (relativeWidget) - { - float locationTop = relativeWidget->getBottomInParent(); - float locationRight = relativeWidget->getRightInParent(); - finalPosY = childType == WidgetTypeWidget ? locationTop - (1.0f - ap.y) * cs.height : locationTop - cs.height; - finalPosX = childType == WidgetTypeWidget ? locationRight - (1.0f - ap.x) * cs.width : locationRight - cs.width; - } - break; - default: - break; - } - UIMargin relativeWidgetMargin; - UIMargin mg; - if (relativeWidget) - { - relativeWidgetMargin = relativeWidget->getLayoutParameter()->getMargin(); - mg = child->getLayoutParameter()->getMargin(); - } - //handle margin - switch (align) - { - case RELATIVE_LOCATION_ABOVE_LEFTALIGN: - case RELATIVE_LOCATION_ABOVE_RIGHTALIGN: - case RELATIVE_LOCATION_ABOVE_CENTER: - finalPosY += relativeWidgetMargin.top; - finalPosY += mg.bottom; - break; - case RELATIVE_LOCATION_BELOW_LEFTALIGN: - case RELATIVE_LOCATION_BELOW_RIGHTALIGN: - case RELATIVE_LOCATION_BELOW_CENTER: - finalPosY -= relativeWidgetMargin.bottom; - finalPosY -= mg.top; - break; - case RELATIVE_LOCATION_LEFT_OF_TOPALIGN: - case RELATIVE_LOCATION_LEFT_OF_BOTTOMALIGN: - case RELATIVE_LOCATION_LEFT_OF_CENTER: - finalPosX -= relativeWidgetMargin.left; - finalPosX -= mg.right; - break; - case RELATIVE_LOCATION_RIGHT_OF_TOPALIGN: - case RELATIVE_LOCATION_RIGHT_OF_BOTTOMALIGN: - case RELATIVE_LOCATION_RIGHT_OF_CENTER: - finalPosX += relativeWidgetMargin.right; - finalPosX += mg.left; - break; - default: - break; - } - child->setPosition(Point(finalPosX, finalPosY)); - } - } - break; - } - default: - break; - } -} - -const char* Layout::getDescription() const -{ - return "Layout"; -} - -RectClippingNode::RectClippingNode(): -m_pInnerStencil(NULL), -_enabled(true), -_clippingSize(Size(50.0f, 50.0f)), -_clippingEnabled(false) -{ - -} - -RectClippingNode::~RectClippingNode() -{ - -} - -RectClippingNode* RectClippingNode::create() -{ - RectClippingNode *pRet = new RectClippingNode(); - if (pRet && pRet->init()) - { - pRet->autorelease(); - } - else - { - CC_SAFE_DELETE(pRet); - } - - return pRet; -} - -bool RectClippingNode::init() -{ - m_pInnerStencil = CCDrawNode::create(); - rect[0] = Point(0, 0); - rect[1] = Point(_clippingSize.width, 0); - rect[2] = Point(_clippingSize.width, _clippingSize.height); - rect[3] = Point(0, _clippingSize.height); - - Color4F green(0, 1, 0, 1); - m_pInnerStencil->drawPolygon(rect, 4, green, 0, green); - if (CCClippingNode::init(m_pInnerStencil)) - { - return true; - } - return false; -} - - -void RectClippingNode::setClippingSize(const Size &size) -{ - setContentSize(size); - _clippingSize = size; - rect[0] = Point(0, 0); - rect[1] = Point(_clippingSize.width, 0); - rect[2] = Point(_clippingSize.width, _clippingSize.height); - rect[3] = Point(0, _clippingSize.height); - Color4F green(0, 1, 0, 1); - m_pInnerStencil->clear(); - m_pInnerStencil->drawPolygon(rect, 4, green, 0, green); -} - -void RectClippingNode::setClippingEnabled(bool enabled) -{ - _clippingEnabled = enabled; -} - -void RectClippingNode::visit() -{ - if (!_enabled) - { - return; - } - if (_clippingEnabled) - { - CCClippingNode::visit(); - } - else - { - CCNode::visit(); - } -} - -void RectClippingNode::setEnabled(bool enabled) -{ - _enabled = enabled; -} - -bool RectClippingNode::isEnabled() const -{ - return _enabled; -} - -} diff --git a/cocos/gui/UIButton.cpp b/cocos/gui/UIButton.cpp index 12cc3cc932..b0c0c89630 100644 --- a/cocos/gui/UIButton.cpp +++ b/cocos/gui/UIButton.cpp @@ -25,9 +25,6 @@ #include "gui/UIButton.h" #include "extensions/GUI/CCControlExtension/CCScale9Sprite.h" - using namespace cocos2d; - using namespace cocos2d::extension; - namespace gui { #define NORMALRENDERERZ (0) @@ -45,9 +42,9 @@ _clickedFileName(""), _disabledFileName(""), _prevIgnoreSize(true), _scale9Enabled(false), -_capInsetsNormal(Rect::ZERO), -_capInsetsPressed(Rect::ZERO), -_capInsetsDisabled(Rect::ZERO), +_capInsetsNormal(cocos2d::Rect::ZERO), +_capInsetsPressed(cocos2d::Rect::ZERO), +_capInsetsDisabled(cocos2d::Rect::ZERO), _normalTexType(UI_TEX_TYPE_LOCAL), _pressedTexType(UI_TEX_TYPE_LOCAL), _disabledTexType(UI_TEX_TYPE_LOCAL), @@ -55,7 +52,7 @@ _normalTextureSize(_size), _pressedTextureSize(_size), _disabledTextureSize(_size), _pressedActionEnabled(false), -_titleColor(Color3B::WHITE) +_titleColor(cocos2d::Color3B::WHITE) { } @@ -88,10 +85,10 @@ bool UIButton::init() void UIButton::initRenderer() { UIWidget::initRenderer(); - _buttonNormalRenderer = Sprite::create(); - _buttonClickedRenderer = Sprite::create(); - _buttonDisableRenderer = Sprite::create(); - _titleRenderer = LabelTTF::create(); + _buttonNormalRenderer = cocos2d::Sprite::create(); + _buttonClickedRenderer = cocos2d::Sprite::create(); + _buttonDisableRenderer = cocos2d::Sprite::create(); + _titleRenderer = cocos2d::LabelTTF::create(); _renderer->addChild(_buttonNormalRenderer,NORMALRENDERERZ); _renderer->addChild(_buttonClickedRenderer,PRESSEDRENDERERZ); _renderer->addChild(_buttonDisableRenderer,DISABLEDRENDERERZ); @@ -117,15 +114,15 @@ void UIButton::setScale9Enabled(bool able) _buttonDisableRenderer = NULL; if (_scale9Enabled) { - _buttonNormalRenderer = Scale9Sprite::create(); - _buttonClickedRenderer = Scale9Sprite::create(); - _buttonDisableRenderer = Scale9Sprite::create(); + _buttonNormalRenderer = cocos2d::extension::Scale9Sprite::create(); + _buttonClickedRenderer = cocos2d::extension::Scale9Sprite::create(); + _buttonDisableRenderer = cocos2d::extension::Scale9Sprite::create(); } else { - _buttonNormalRenderer = CCSprite::create(); - _buttonClickedRenderer = CCSprite::create(); - _buttonDisableRenderer = CCSprite::create(); + _buttonNormalRenderer = cocos2d::Sprite::create(); + _buttonClickedRenderer = cocos2d::Sprite::create(); + _buttonDisableRenderer = cocos2d::Sprite::create(); } loadTextureNormal(_normalFileName.c_str(), _normalTexType); @@ -179,32 +176,33 @@ void UIButton::loadTextureNormal(const char* normal,TextureResType texType) switch (_normalTexType) { case UI_TEX_TYPE_LOCAL: - dynamic_cast(_buttonNormalRenderer)->initWithFile(normal); + dynamic_cast(_buttonNormalRenderer)->initWithFile(normal); break; case UI_TEX_TYPE_PLIST: - dynamic_cast(_buttonNormalRenderer)->initWithSpriteFrameName(normal); + dynamic_cast(_buttonNormalRenderer)->initWithSpriteFrameName(normal); break; default: break; } - dynamic_cast(_buttonNormalRenderer)->setColor(getColor()); - dynamic_cast(_buttonNormalRenderer)->setOpacity(getOpacity()); + dynamic_cast(_buttonNormalRenderer)->setColor(getColor()); + dynamic_cast(_buttonNormalRenderer)->setOpacity(getOpacity()); + dynamic_cast(_buttonNormalRenderer)->setCapInsets(_capInsetsNormal); } else { switch (_normalTexType) { case UI_TEX_TYPE_LOCAL: - dynamic_cast(_buttonNormalRenderer)->initWithFile(normal); + dynamic_cast(_buttonNormalRenderer)->initWithFile(normal); break; case UI_TEX_TYPE_PLIST: - dynamic_cast(_buttonNormalRenderer)->initWithSpriteFrameName(normal); + dynamic_cast(_buttonNormalRenderer)->initWithSpriteFrameName(normal); break; default: break; } - dynamic_cast(_buttonNormalRenderer)->setColor(getColor()); - dynamic_cast(_buttonNormalRenderer)->setOpacity(getOpacity()); + dynamic_cast(_buttonNormalRenderer)->setColor(getColor()); + dynamic_cast(_buttonNormalRenderer)->setOpacity(getOpacity()); } _normalTextureSize = _buttonNormalRenderer->getContentSize(); updateAnchorPoint(); @@ -224,32 +222,33 @@ void UIButton::loadTexturePressed(const char* selected,TextureResType texType) switch (_pressedTexType) { case UI_TEX_TYPE_LOCAL: - dynamic_cast(_buttonClickedRenderer)->initWithFile(selected); + dynamic_cast(_buttonClickedRenderer)->initWithFile(selected); break; case UI_TEX_TYPE_PLIST: - dynamic_cast(_buttonClickedRenderer)->initWithSpriteFrameName(selected); + dynamic_cast(_buttonClickedRenderer)->initWithSpriteFrameName(selected); break; default: break; } - dynamic_cast(_buttonClickedRenderer)->setColor(getColor()); - dynamic_cast(_buttonClickedRenderer)->setOpacity(getOpacity()); + dynamic_cast(_buttonClickedRenderer)->setColor(getColor()); + dynamic_cast(_buttonClickedRenderer)->setOpacity(getOpacity()); + dynamic_cast(_buttonClickedRenderer)->setCapInsets(_capInsetsPressed); } else { switch (_pressedTexType) { case UI_TEX_TYPE_LOCAL: - dynamic_cast(_buttonClickedRenderer)->initWithFile(selected); + dynamic_cast(_buttonClickedRenderer)->initWithFile(selected); break; case UI_TEX_TYPE_PLIST: - dynamic_cast(_buttonClickedRenderer)->initWithSpriteFrameName(selected); + dynamic_cast(_buttonClickedRenderer)->initWithSpriteFrameName(selected); break; default: break; } - dynamic_cast(_buttonClickedRenderer)->setColor(getColor()); - dynamic_cast(_buttonClickedRenderer)->setOpacity(getOpacity()); + dynamic_cast(_buttonClickedRenderer)->setColor(getColor()); + dynamic_cast(_buttonClickedRenderer)->setOpacity(getOpacity()); } _pressedTextureSize = _buttonClickedRenderer->getContentSize(); updateAnchorPoint(); @@ -269,73 +268,74 @@ void UIButton::loadTextureDisabled(const char* disabled,TextureResType texType) switch (_disabledTexType) { case UI_TEX_TYPE_LOCAL: - dynamic_cast(_buttonDisableRenderer)->initWithFile(disabled); + dynamic_cast(_buttonDisableRenderer)->initWithFile(disabled); break; case UI_TEX_TYPE_PLIST: - dynamic_cast(_buttonDisableRenderer)->initWithSpriteFrameName(disabled); + dynamic_cast(_buttonDisableRenderer)->initWithSpriteFrameName(disabled); break; default: break; } - dynamic_cast(_buttonDisableRenderer)->setColor(getColor()); - dynamic_cast(_buttonDisableRenderer)->setOpacity(getOpacity()); + dynamic_cast(_buttonDisableRenderer)->setColor(getColor()); + dynamic_cast(_buttonDisableRenderer)->setOpacity(getOpacity()); + dynamic_cast(_buttonDisableRenderer)->setCapInsets(_capInsetsDisabled); } else { switch (_disabledTexType) { case UI_TEX_TYPE_LOCAL: - dynamic_cast(_buttonDisableRenderer)->initWithFile(disabled); + dynamic_cast(_buttonDisableRenderer)->initWithFile(disabled); break; case UI_TEX_TYPE_PLIST: - dynamic_cast(_buttonDisableRenderer)->initWithSpriteFrameName(disabled); + dynamic_cast(_buttonDisableRenderer)->initWithSpriteFrameName(disabled); break; default: break; } - dynamic_cast(_buttonDisableRenderer)->setColor(getColor()); - dynamic_cast(_buttonDisableRenderer)->setOpacity(getOpacity()); + dynamic_cast(_buttonDisableRenderer)->setColor(getColor()); + dynamic_cast(_buttonDisableRenderer)->setOpacity(getOpacity()); } _disabledTextureSize = _buttonDisableRenderer->getContentSize(); updateAnchorPoint(); disabledTextureScaleChangedWithSize(); } -void UIButton::setCapInsets(const Rect &capInsets) +void UIButton::setCapInsets(const cocos2d::Rect &capInsets) { setCapInsetsNormalRenderer(capInsets); setCapInsetsPressedRenderer(capInsets); setCapInsetsDisabledRenderer(capInsets); } -void UIButton::setCapInsetsNormalRenderer(const Rect &capInsets) +void UIButton::setCapInsetsNormalRenderer(const cocos2d::Rect &capInsets) { _capInsetsNormal = capInsets; if (!_scale9Enabled) { return; } - dynamic_cast(_buttonNormalRenderer)->setCapInsets(capInsets); + dynamic_cast(_buttonNormalRenderer)->setCapInsets(capInsets); } -void UIButton::setCapInsetsPressedRenderer(const Rect &capInsets) +void UIButton::setCapInsetsPressedRenderer(const cocos2d::Rect &capInsets) { _capInsetsPressed = capInsets; if (!_scale9Enabled) { return; } - dynamic_cast(_buttonClickedRenderer)->setCapInsets(capInsets); + dynamic_cast(_buttonClickedRenderer)->setCapInsets(capInsets); } -void UIButton::setCapInsetsDisabledRenderer(const Rect &capInsets) +void UIButton::setCapInsetsDisabledRenderer(const cocos2d::Rect &capInsets) { _capInsetsDisabled = capInsets; if (!_scale9Enabled) { return; } - dynamic_cast(_buttonDisableRenderer)->setCapInsets(capInsets); + dynamic_cast(_buttonDisableRenderer)->setCapInsets(capInsets); } void UIButton::onPressStateChangedToNormal() @@ -348,9 +348,9 @@ void UIButton::onPressStateChangedToNormal() _buttonNormalRenderer->stopAllActions(); _buttonClickedRenderer->stopAllActions(); _buttonDisableRenderer->stopAllActions(); - Action *zoomAction = ScaleTo::create(0.05f, 1.0f); - Action *zoomAction1 = ScaleTo::create(0.05f, 1.0f); - Action *zoomAction2 = ScaleTo::create(0.05f, 1.0f); + cocos2d::Action *zoomAction = cocos2d::ScaleTo::create(0.05f, 1.0f); + cocos2d::Action *zoomAction1 = cocos2d::ScaleTo::create(0.05f, 1.0f); + cocos2d::Action *zoomAction2 = cocos2d::ScaleTo::create(0.05f, 1.0f); _buttonNormalRenderer->runAction(zoomAction); _buttonClickedRenderer->runAction(zoomAction1); _buttonDisableRenderer->runAction(zoomAction2); @@ -367,9 +367,9 @@ void UIButton::onPressStateChangedToPressed() _buttonNormalRenderer->stopAllActions(); _buttonClickedRenderer->stopAllActions(); _buttonDisableRenderer->stopAllActions(); - Action *zoomAction = ScaleTo::create(0.05f, 1.1f); - Action *zoomAction1 = ScaleTo::create(0.05f, 1.1f); - Action *zoomAction2 = ScaleTo::create(0.05f, 1.1f); + cocos2d::Action *zoomAction = cocos2d::ScaleTo::create(0.05f, 1.1f); + cocos2d::Action *zoomAction1 = cocos2d::ScaleTo::create(0.05f, 1.1f); + cocos2d::Action *zoomAction2 = cocos2d::ScaleTo::create(0.05f, 1.1f); _buttonNormalRenderer->runAction(zoomAction); _buttonClickedRenderer->runAction(zoomAction1); _buttonDisableRenderer->runAction(zoomAction2); @@ -390,9 +390,9 @@ void UIButton::setFlipX(bool flipX) { return; } - dynamic_cast(_buttonNormalRenderer)->setFlippedX(flipX); - dynamic_cast(_buttonClickedRenderer)->setFlippedX(flipX); - dynamic_cast(_buttonDisableRenderer)->setFlippedX(flipX); + dynamic_cast(_buttonNormalRenderer)->setFlippedX(flipX); + dynamic_cast(_buttonClickedRenderer)->setFlippedX(flipX); + dynamic_cast(_buttonDisableRenderer)->setFlippedX(flipX); } void UIButton::setFlipY(bool flipY) @@ -402,9 +402,9 @@ void UIButton::setFlipY(bool flipY) { return; } - dynamic_cast(_buttonNormalRenderer)->setFlippedY(flipY); - dynamic_cast(_buttonClickedRenderer)->setFlippedY(flipY); - dynamic_cast(_buttonDisableRenderer)->setFlippedY(flipY); + dynamic_cast(_buttonNormalRenderer)->setFlippedY(flipY); + dynamic_cast(_buttonClickedRenderer)->setFlippedY(flipY); + dynamic_cast(_buttonDisableRenderer)->setFlippedY(flipY); } bool UIButton::isFlipX() @@ -413,7 +413,7 @@ bool UIButton::isFlipX() { return false; } - return dynamic_cast(_buttonNormalRenderer)->isFlippedX(); + return dynamic_cast(_buttonNormalRenderer)->isFlippedX(); } bool UIButton::isFlipY() @@ -422,16 +422,16 @@ bool UIButton::isFlipY() { return false; } - return dynamic_cast(_buttonNormalRenderer)->isFlippedY(); + return dynamic_cast(_buttonNormalRenderer)->isFlippedY(); } -void UIButton::setAnchorPoint(const Point &pt) +void UIButton::setAnchorPoint(const cocos2d::Point &pt) { UIWidget::setAnchorPoint(pt); _buttonNormalRenderer->setAnchorPoint(pt); _buttonClickedRenderer->setAnchorPoint(pt); _buttonDisableRenderer->setAnchorPoint(pt); - _titleRenderer->setPosition(Point(_size.width*(0.5f-_anchorPoint.x), _size.height*(0.5f-_anchorPoint.y))); + _titleRenderer->setPosition(cocos2d::Point(_size.width*(0.5f-_anchorPoint.x), _size.height*(0.5f-_anchorPoint.y))); } void UIButton::onSizeChanged() @@ -441,12 +441,12 @@ void UIButton::onSizeChanged() disabledTextureScaleChangedWithSize(); } -const Size& UIButton::getContentSize() const +const cocos2d::Size& UIButton::getContentSize() const { return _normalTextureSize; } -Node* UIButton::getVirtualRenderer() +cocos2d::Node* UIButton::getVirtualRenderer() { if (_bright) { @@ -480,11 +480,11 @@ void UIButton::normalTextureScaleChangedWithSize() { if (_scale9Enabled) { - dynamic_cast(_buttonNormalRenderer)->setPreferredSize(_size); + dynamic_cast(_buttonNormalRenderer)->setPreferredSize(_size); } else { - Size textureSize = _normalTextureSize; + cocos2d::Size textureSize = _normalTextureSize; if (textureSize.width <= 0.0f || textureSize.height <= 0.0f) { _buttonNormalRenderer->setScale(1.0f); @@ -511,11 +511,11 @@ void UIButton::pressedTextureScaleChangedWithSize() { if (_scale9Enabled) { - dynamic_cast(_buttonClickedRenderer)->setPreferredSize(_size); + dynamic_cast(_buttonClickedRenderer)->setPreferredSize(_size); } else { - Size textureSize = _pressedTextureSize; + cocos2d::Size textureSize = _pressedTextureSize; if (textureSize.width <= 0.0f || textureSize.height <= 0.0f) { _buttonClickedRenderer->setScale(1.0f); @@ -542,11 +542,11 @@ void UIButton::disabledTextureScaleChangedWithSize() { if (_scale9Enabled) { - dynamic_cast(_buttonDisableRenderer)->setPreferredSize(_size); + dynamic_cast(_buttonDisableRenderer)->setPreferredSize(_size); } else { - Size textureSize = _disabledTextureSize; + cocos2d::Size textureSize = _disabledTextureSize; if (textureSize.width <= 0.0f || textureSize.height <= 0.0f) { _buttonDisableRenderer->setScale(1.0f); @@ -575,13 +575,13 @@ const char* UIButton::getTitleText() const return _titleRenderer->getString(); } -void UIButton::setTitleColor(const Color3B& color) +void UIButton::setTitleColor(const cocos2d::Color3B& color) { _titleColor = color; _titleRenderer->setColor(color); } -const Color3B& UIButton::getTitleColor() const +const cocos2d::Color3B& UIButton::getTitleColor() const { return _titleRenderer->getColor(); } @@ -606,7 +606,7 @@ const char* UIButton::getTitleFontName() const return _titleRenderer->getFontName(); } -void UIButton::setColor(const Color3B &color) +void UIButton::setColor(const cocos2d::Color3B &color) { UIWidget::setColor(color); setTitleColor(_titleColor); @@ -617,4 +617,30 @@ const char* UIButton::getDescription() const return "Button"; } +UIWidget* UIButton::createCloneInstance() +{ + return UIButton::create(); +} + +void UIButton::copySpecialProperties(UIWidget *widget) +{ + UIButton* button = dynamic_cast(widget); + if (button) + { + _prevIgnoreSize = button->_prevIgnoreSize; + setScale9Enabled(button->_scale9Enabled); + loadTextureNormal(button->_normalFileName.c_str(), button->_normalTexType); + loadTexturePressed(button->_clickedFileName.c_str(), button->_pressedTexType); + loadTextureDisabled(button->_disabledFileName.c_str(), button->_disabledTexType); + setCapInsetsNormalRenderer(button->_capInsetsNormal); + setCapInsetsPressedRenderer(button->_capInsetsPressed); + setCapInsetsDisabledRenderer(button->_capInsetsDisabled); + setTitleText(button->getTitleText()); + setTitleFontName(button->getTitleFontName()); + setTitleFontSize(button->getTitleFontSize()); + setTitleColor(button->getTitleColor()); + setPressedActionEnabled(button->_pressedActionEnabled); + } +} + } \ No newline at end of file diff --git a/cocos/gui/UIButton.h b/cocos/gui/UIButton.h index 94d40ec65f..bc6958c3e4 100644 --- a/cocos/gui/UIButton.h +++ b/cocos/gui/UIButton.h @@ -27,8 +27,12 @@ #include "gui/UIWidget.h" -namespace gui { +namespace gui{ +/** +* @js NA +* @lua NA +*/ class UIButton : public UIWidget { public: @@ -162,6 +166,11 @@ public: */ virtual void setColor(const cocos2d::Color3B &color); + /** + * Returns the "class name" of widget. + */ + virtual const char* getDescription() const; + void setTitleText(const char* text); const char* getTitleText() const; void setTitleColor(const cocos2d::Color3B& color); @@ -170,11 +179,7 @@ public: float getTitleFontSize() const; void setTitleFontName(const char* fontName); const char* getTitleFontName() const; - - /** - * Returns the "class name" of widget. - */ - virtual const char* getDescription() const; + protected: virtual bool init(); virtual void initRenderer(); @@ -186,6 +191,8 @@ protected: void normalTextureScaleChangedWithSize(); void pressedTextureScaleChangedWithSize(); void disabledTextureScaleChangedWithSize(); + virtual UIWidget* createCloneInstance(); + virtual void copySpecialProperties(UIWidget* model); protected: cocos2d::Node* _buttonNormalRenderer; cocos2d::Node* _buttonClickedRenderer; diff --git a/cocos/gui/UICheckBox.cpp b/cocos/gui/UICheckBox.cpp index f685d73b11..a826fa40ef 100644 --- a/cocos/gui/UICheckBox.cpp +++ b/cocos/gui/UICheckBox.cpp @@ -24,10 +24,9 @@ #include "gui/UICheckBox.h" - using namespace cocos2d; - namespace gui { + UICheckBox::UICheckBox(): _backGroundBoxRenderer(NULL), _backGroundSelectedBoxRenderer(NULL), @@ -41,7 +40,12 @@ _backGroundTexType(UI_TEX_TYPE_LOCAL), _backGroundSelectedTexType(UI_TEX_TYPE_LOCAL), _frontCrossTexType(UI_TEX_TYPE_LOCAL), _backGroundDisabledTexType(UI_TEX_TYPE_LOCAL), -_frontCrossDisabledTexType(UI_TEX_TYPE_LOCAL) +_frontCrossDisabledTexType(UI_TEX_TYPE_LOCAL), +_backGroundFileName(""), +_backGroundSelectedFileName(""), +_frontCrossFileName(""), +_backGroundDisabledFileName(""), +_frontCrossDisabledFileName("") { } @@ -75,11 +79,11 @@ bool UICheckBox::init() void UICheckBox::initRenderer() { UIWidget::initRenderer(); - _backGroundBoxRenderer = Sprite::create(); - _backGroundSelectedBoxRenderer = Sprite::create(); - _frontCrossRenderer = Sprite::create(); - _backGroundBoxDisabledRenderer = Sprite::create(); - _frontCrossDisabledRenderer = Sprite::create(); + _backGroundBoxRenderer = cocos2d::Sprite::create(); + _backGroundSelectedBoxRenderer = cocos2d::Sprite::create(); + _frontCrossRenderer = cocos2d::Sprite::create(); + _backGroundBoxDisabledRenderer = cocos2d::Sprite::create(); + _frontCrossDisabledRenderer = cocos2d::Sprite::create(); _renderer->addChild(_backGroundBoxRenderer); _renderer->addChild(_backGroundSelectedBoxRenderer); _renderer->addChild(_frontCrossRenderer); @@ -102,6 +106,7 @@ void UICheckBox::loadTextureBackGround(const char *backGround,TextureResType tex { return; } + _backGroundFileName = backGround; _backGroundTexType = texType; switch (_backGroundTexType) { @@ -125,6 +130,7 @@ void UICheckBox::loadTextureBackGroundSelected(const char *backGroundSelected,Te { return; } + _backGroundSelectedFileName = backGroundSelected; _backGroundSelectedTexType = texType; switch (_backGroundSelectedTexType) { @@ -148,6 +154,7 @@ void UICheckBox::loadTextureFrontCross(const char *cross,TextureResType texType) { return; } + _frontCrossFileName = cross; _frontCrossTexType = texType; switch (_frontCrossTexType) { @@ -171,6 +178,7 @@ void UICheckBox::loadTextureBackGroundDisabled(const char *backGroundDisabled,Te { return; } + _backGroundDisabledFileName = backGroundDisabled; _backGroundDisabledTexType = texType; switch (_backGroundDisabledTexType) { @@ -194,6 +202,7 @@ void UICheckBox::loadTextureFrontCrossDisabled(const char *frontCrossDisabled,Te { return; } + _frontCrossDisabledFileName = frontCrossDisabled; _frontCrossDisabledTexType = texType; switch (_frontCrossDisabledTexType) { @@ -211,7 +220,7 @@ void UICheckBox::loadTextureFrontCrossDisabled(const char *frontCrossDisabled,Te frontCrossDisabledTextureScaleChangedWithSize(); } -void UICheckBox::onTouchEnded(const Point &touchPoint) +void UICheckBox::onTouchEnded(const cocos2d::Point &touchPoint) { if (_focus) { @@ -289,7 +298,7 @@ void UICheckBox::unSelectedEvent() } } -void UICheckBox::addEventListener(Object *target, SEL_SelectedStateEvent selector) +void UICheckBox::addEventListener(cocos2d::Object *target, SEL_SelectedStateEvent selector) { _selectedStateEventListener = target; _selectedStateEventSelector = selector; @@ -323,7 +332,7 @@ bool UICheckBox::isFlipY() return _backGroundBoxRenderer->isFlippedY(); } -void UICheckBox::setAnchorPoint(const Point &pt) +void UICheckBox::setAnchorPoint(const cocos2d::Point &pt) { UIWidget::setAnchorPoint(pt); _backGroundBoxRenderer->setAnchorPoint(pt); @@ -342,12 +351,12 @@ void UICheckBox::onSizeChanged() frontCrossDisabledTextureScaleChangedWithSize(); } -const Size& UICheckBox::getContentSize() const +const cocos2d::Size& UICheckBox::getContentSize() const { return _backGroundBoxRenderer->getContentSize(); } -Node* UICheckBox::getVirtualRenderer() +cocos2d::Node* UICheckBox::getVirtualRenderer() { return _backGroundBoxRenderer; } @@ -361,7 +370,7 @@ void UICheckBox::backGroundTextureScaleChangedWithSize() } else { - Size textureSize = _backGroundBoxRenderer->getContentSize(); + cocos2d::Size textureSize = _backGroundBoxRenderer->getContentSize(); if (textureSize.width <= 0.0f || textureSize.height <= 0.0f) { _backGroundBoxRenderer->setScale(1.0f); @@ -382,7 +391,7 @@ void UICheckBox::backGroundSelectedTextureScaleChangedWithSize() } else { - Size textureSize = _backGroundSelectedBoxRenderer->getContentSize(); + cocos2d::Size textureSize = _backGroundSelectedBoxRenderer->getContentSize(); if (textureSize.width <= 0.0f || textureSize.height <= 0.0f) { _backGroundSelectedBoxRenderer->setScale(1.0f); @@ -403,7 +412,7 @@ void UICheckBox::frontCrossTextureScaleChangedWithSize() } else { - Size textureSize = _frontCrossRenderer->getContentSize(); + cocos2d::Size textureSize = _frontCrossRenderer->getContentSize(); if (textureSize.width <= 0.0f || textureSize.height <= 0.0f) { _frontCrossRenderer->setScale(1.0f); @@ -424,7 +433,7 @@ void UICheckBox::backGroundDisabledTextureScaleChangedWithSize() } else { - Size textureSize = _backGroundBoxDisabledRenderer->getContentSize(); + cocos2d::Size textureSize = _backGroundBoxDisabledRenderer->getContentSize(); if (textureSize.width <= 0.0f || textureSize.height <= 0.0f) { _backGroundBoxDisabledRenderer->setScale(1.0f); @@ -445,7 +454,7 @@ void UICheckBox::frontCrossDisabledTextureScaleChangedWithSize() } else { - Size textureSize = _frontCrossDisabledRenderer->getContentSize(); + cocos2d::Size textureSize = _frontCrossDisabledRenderer->getContentSize(); if (textureSize.width <= 0.0f || textureSize.height <= 0.0f) { _frontCrossDisabledRenderer->setScale(1.0f); @@ -463,4 +472,23 @@ const char* UICheckBox::getDescription() const return "CheckBox"; } +UIWidget* UICheckBox::createCloneInstance() +{ + return UICheckBox::create(); +} + +void UICheckBox::copySpecialProperties(UIWidget *widget) +{ + UICheckBox* checkBox = dynamic_cast(widget); + if (checkBox) + { + loadTextureBackGround(checkBox->_backGroundFileName.c_str(), checkBox->_backGroundTexType); + loadTextureBackGroundSelected(checkBox->_backGroundSelectedFileName.c_str(), checkBox->_backGroundSelectedTexType); + loadTextureFrontCross(checkBox->_frontCrossFileName.c_str(), checkBox->_frontCrossTexType); + loadTextureBackGroundDisabled(checkBox->_backGroundDisabledFileName.c_str(), checkBox->_backGroundDisabledTexType); + loadTextureFrontCrossDisabled(checkBox->_frontCrossDisabledFileName.c_str(), checkBox->_frontCrossDisabledTexType); + setSelectedState(checkBox->_isSelected); + } +} + } \ No newline at end of file diff --git a/cocos/gui/UICheckBox.h b/cocos/gui/UICheckBox.h index 46183f04db..5e9c836617 100644 --- a/cocos/gui/UICheckBox.h +++ b/cocos/gui/UICheckBox.h @@ -35,9 +35,13 @@ typedef enum CHECKBOX_STATE_EVENT_UNSELECTED }CheckBoxEventType; -typedef void (cocos2d::CCObject::*SEL_SelectedStateEvent)(cocos2d::Object*,CheckBoxEventType); +typedef void (cocos2d::Object::*SEL_SelectedStateEvent)(cocos2d::Object*,CheckBoxEventType); #define checkboxselectedeventselector(_SELECTOR) (SEL_SelectedStateEvent)(&_SELECTOR) +/** +* @js NA +* @lua NA +*/ class UICheckBox : public UIWidget { public: @@ -156,11 +160,12 @@ public: //override "getVirtualRenderer" method of widget. virtual cocos2d::Node* getVirtualRenderer(); - + /** * Returns the "class name" of widget. */ virtual const char* getDescription() const; + protected: virtual bool init(); virtual void initRenderer(); @@ -175,6 +180,8 @@ protected: void frontCrossTextureScaleChangedWithSize(); void backGroundDisabledTextureScaleChangedWithSize(); void frontCrossDisabledTextureScaleChangedWithSize(); + virtual UIWidget* createCloneInstance(); + virtual void copySpecialProperties(UIWidget* model); protected: cocos2d::Sprite* _backGroundBoxRenderer; cocos2d::Sprite* _backGroundSelectedBoxRenderer; @@ -191,6 +198,12 @@ protected: TextureResType _frontCrossTexType; TextureResType _backGroundDisabledTexType; TextureResType _frontCrossDisabledTexType; + + std::string _backGroundFileName; + std::string _backGroundSelectedFileName; + std::string _frontCrossFileName; + std::string _backGroundDisabledFileName; + std::string _frontCrossDisabledFileName; }; } diff --git a/cocos/gui/UIDragPanel.cpp b/cocos/gui/UIDragPanel.cpp deleted file mode 100644 index 7c717c0636..0000000000 --- a/cocos/gui/UIDragPanel.cpp +++ /dev/null @@ -1,1269 +0,0 @@ -/**************************************************************************** - Copyright (c) 2013 cocos2d-x.org - - http://www.cocos2d-x.org - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in - all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. - ****************************************************************************/ - -#include "gui/UIDragPanel.h" -#include "gui/UILayer.h" - - using namespace cocos2d; - -namespace gui { - -UIDragPanel::UIDragPanel() -: _innerContainer(NULL) -, _touchPressed(false) -, _touchMoved(false) -, _touchReleased(false) -, _touchCanceld(false) -, _touchStartNodeSpace(Point::ZERO) -, _touchStartWorldSpace(Point::ZERO) -, _touchEndWorldSpace(Point::ZERO) -, _slidTime(0.0f) -, _moveType(DRAGPANEL_MOVE_TYPE_AUTOMOVE) -, _autoMoveDuration(0.5f) -, _autoMoveEaseRate(2.0f) -, _eventLister(NULL) -, _eventSelector(NULL) -, _berthDirection(DRAGPANEL_BERTH_DIR_NONE) -, _bounceEnable(false) -, _bounceDirection(DRAGPANEL_BOUNCE_DIR_NONE) -, _bounceDuration(0.5f) -, _bounceEaseRate(2.0f) -, _runningAction(false) -, _actionType(0) -, _actionWidget(NULL) -, _duration(0.0f) -, _elapsed(0.0f) -, _firstTick(false) -, _positionDelta(Point::ZERO) -, _startPosition(Point::ZERO) -, _previousPosition(Point::ZERO) -, _endPosition(Point::ZERO) -{ - -} - -UIDragPanel::~UIDragPanel() -{ - -} - -UIDragPanel* UIDragPanel::create() -{ - UIDragPanel* widget = new UIDragPanel(); - if (widget && widget->init()) - { - widget->autorelease(); - return widget; - } - CC_SAFE_DELETE(widget); - return NULL; -} - -bool UIDragPanel::init() -{ - if (Layout::init()) - { - setUpdateEnabled(true); - setTouchEnabled(true); - setClippingEnabled(true); - return true; - } - return false; -} - -void UIDragPanel::initRenderer() -{ - Layout::initRenderer(); - - _innerContainer = Layout::create(); - Layout::addChild(_innerContainer); - -} - -void UIDragPanel::releaseResoures() -{ - setUpdateEnabled(false); - removeAllChildren(); - _renderer->removeAllChildrenWithCleanup(true); - _renderer->removeFromParentAndCleanup(true); - _renderer->release(); - - Layout::removeChild(_innerContainer); - - _children->release(); -} - -bool UIDragPanel::onTouchBegan(const Point &touchPoint) -{ - bool pass = Layout::onTouchBegan(touchPoint); - handlePressLogic(touchPoint); - return pass; -} - -void UIDragPanel::onTouchMoved(const Point &touchPoint) -{ - Layout::onTouchMoved(touchPoint); - handleMoveLogic(touchPoint); -} - -void UIDragPanel::onTouchEnded(const Point &touchPoint) -{ - Layout::onTouchEnded(touchPoint); - handleReleaseLogic(touchPoint); -} - -void UIDragPanel::onTouchCancelled(const Point &touchPoint) -{ - Layout::onTouchCancelled(touchPoint); -} - -void UIDragPanel::onTouchLongClicked(const Point &touchPoint) -{ - -} - -void UIDragPanel::update(float dt) -{ - // widget action - if (_runningAction) - { - if (actionIsDone()) - { - actionDone(); - actionStop(); - } - else - { - actionStep(dt); - } - } - - recordSlidTime(dt); -} - -bool UIDragPanel::addChild(UIWidget *widget) -{ - _innerContainer->addChild(widget); - return true; -} - -bool UIDragPanel::removeChild(UIWidget *child) -{ - bool value = false; - if (_innerContainer->removeChild(child)) - { - value = true; - } - - return value; -} - -void UIDragPanel::removeAllChildren() -{ - _innerContainer->removeAllChildren(); -} - -Array* UIDragPanel::getChildren() -{ - return _innerContainer->getChildren(); -} - -void UIDragPanel::onSizeChanged() -{ - Layout::onSizeChanged(); - Size innerSize = _innerContainer->getSize(); - float orginInnerSizeWidth = innerSize.width; - float orginInnerSizeHeight = innerSize.height; - float innerSizeWidth = MAX(orginInnerSizeWidth, _size.width); - float innerSizeHeight = MAX(orginInnerSizeHeight, _size.height); - _innerContainer->setSize(Size(innerSizeWidth, innerSizeHeight)); -} - -const Size& UIDragPanel::getInnerContainerSize() const -{ - return _innerContainer->getContentSize(); -} - -void UIDragPanel::setInnerContainerSize(const cocos2d::Size &size) -{ - float innerSizeWidth = _size.width; - float innerSizeHeight = _size.height; - if (size.width < _size.width) - { - CCLOG("Inner width <= scrollview width, it will be force sized!"); - } - else - { - innerSizeWidth = size.width; - } - if (size.height < _size.height) - { - CCLOG("Inner height <= scrollview height, it will be force sized!"); - } - else - { - innerSizeHeight = size.height; - } - _innerContainer->setSize(Size(innerSizeWidth, innerSizeHeight)); - _innerContainer->setPosition(Point(0, _size.height - _innerContainer->getSize().height)); -} - -const Point& UIDragPanel::getInnerContainerPosition() const -{ - return _innerContainer->getPosition(); -} - -void UIDragPanel::setInnerContainerPosition(const Point &point, bool animated) -{ - Point delta = point - _innerContainer->getPosition(); - -// Point delta = ccpSub(point, _innerContainer->getPosition()); - setInnerContainerOffset(delta, animated); -} - -void UIDragPanel::setInnerContainerOffset(const Point &offset, bool animated) -{ - if (animated) - { - Point delta = offset; - - if (checkToBoundaryWithDeltaPosition(delta)) - { - delta = calculateToBoundaryDeltaPosition(delta); - } - actionStartWithWidget(_innerContainer); - moveByWithDuration(_autoMoveDuration, delta); - } - else - { - setInnerContainerOffset(offset); - } -} - -void UIDragPanel::setInnerContainerOffset(const Point &offset) -{ - Point delta = offset; - - if (checkToBoundaryWithDeltaPosition(delta)) - { - delta = calculateToBoundaryDeltaPosition(delta); - } - moveWithDelta(delta); - if (checkBerth()) - { - berthEvent(); - } -} - - -void UIDragPanel::handlePressLogic(const Point &touchPoint) -{ - // check inner rect < drag panel rect - if (checkContainInnerRect()) - { - _touchPressed = false; - return; - } - - _touchPressed = true; - _touchMoved = false; - _touchReleased = false; - _touchCanceld = false; - - if (_runningAction) - { - switch (_moveType) - { - case DRAGPANEL_MOVE_TYPE_AUTOMOVE: - stopAutoMove(); - actionStop(); - break; - - case DRAGPANEL_MOVE_TYPE_BOUNCE: - _touchPressed = false; - break; - - default: - break; - } - } - - Point nsp = _renderer->convertToNodeSpace(touchPoint); - _touchStartNodeSpace = nsp; - - _touchStartWorldSpace = touchPoint; -} - -void UIDragPanel::handleMoveLogic(const Point &touchPoint) -{ - if (!_touchPressed) - { - return; - } - - // check touch out of drag panel boundary - if (_touchCanceld) - { - return; - } - - _touchMoved = true; - - Point nsp = _renderer->convertToNodeSpace(touchPoint); - Point delta = nsp - _touchStartNodeSpace; -// Point delta = ccpSub(nsp, _touchStartNodeSpace); - _touchStartNodeSpace = nsp; - - // reset berth dir to none - if (!_bounceEnable) - { - _berthDirection = DRAGPANEL_BERTH_DIR_NONE; - } - - // check will berth (bounce disable) - if (!_bounceEnable) - { - if (checkToBoundaryWithDeltaPosition(delta)) - { - delta = calculateToBoundaryDeltaPosition(delta); - } - } - // move - moveWithDelta(delta); - // check bounce or berth - if (_bounceEnable) - { - // bounce - if (!hitTest(touchPoint)) - { - _touchMoved = false; - - if (checkNeedBounce()) - { - _touchCanceld = true; - startBounce(); - } - } - } - else - { - // berth - if (checkBerth()) - { - berthEvent(); - } - } -} - -void UIDragPanel::handleReleaseLogic(const Point &touchPoint) -{ - if (!_touchPressed) - { - return; - } - - _touchPressed = false; - _touchMoved = false; - _touchReleased = true; - _touchCanceld = false; - - // check touch out of drag panel boundary - if (_touchCanceld) - { - return; - } - - if (hitTest(touchPoint)) - { - _touchEndWorldSpace = touchPoint; - startAutoMove(); - } -} - -void UIDragPanel::checkChildInfo(int handleState, UIWidget *sender, const Point &touchPoint) -{ - interceptTouchEvent(handleState, sender, touchPoint); -} - -void UIDragPanel::interceptTouchEvent(int handleState, UIWidget *sender, const Point &touchPoint) -{ - switch (handleState) - { - case 0: - handlePressLogic(touchPoint); - break; - - case 1: - { -// float offset = ccpDistance(sender->getTouchStartPos(), touchPoint); - float offset = sender->getTouchStartPos().getDistance(touchPoint); - if (offset > 5.0) - { - sender->setFocused(false); - handleMoveLogic(touchPoint); - } - } - break; - - case 2: - handleReleaseLogic(touchPoint); - break; - - case 3: - break; - } -} - -void UIDragPanel::recordSlidTime(float dt) -{ - if (_touchPressed) - { - _slidTime += dt; - } -} - -// check if dragpanel rect contain inner rect -bool UIDragPanel::checkContainInnerRect() -{ - float width = _size.width; - float height = _size.height; - float innerWidth = _innerContainer->getSize().width; - float innerHeight = _innerContainer->getSize().height; - - if (innerWidth <= width && innerHeight <= height) - { - return true; - } - - return false; -} - -// move -void UIDragPanel::moveWithDelta(const Point &delta) -{ - Point newPos = _innerContainer->getPosition() + delta; -// Point newPos = ccpAdd(_innerContainer->getPosition(), delta); - _innerContainer->setPosition(newPos); -} - -// auto move -void UIDragPanel::autoMove() -{ - if (_bounceEnable) - { - if (checkNeedBounce()) - { - stopAutoMove(); - startBounce(); - } - } -} - -void UIDragPanel::autoMoveOver() -{ - stopAutoMove(); - - if (checkBerth()) - { - berthEvent(); - _berthDirection = DRAGPANEL_BERTH_DIR_NONE; - } -} - -void UIDragPanel::startAutoMove() -{ - _moveType = DRAGPANEL_MOVE_TYPE_AUTOMOVE; - - actionStop(); - - Point delta = _touchEndWorldSpace - _touchStartWorldSpace; -// Point delta = ccpSub(m_touchEndWorldSpace, _touchStartWorldSpace); - delta.x /= _slidTime * 60; - delta.y /= _slidTime * 60; - _slidTime = 0.0; - - // bounceEnable is disable - if (!_bounceEnable) - { - if (checkToBoundaryWithDeltaPosition(delta)) - { - delta = calculateToBoundaryDeltaPosition(delta); - } - } - actionStartWithWidget(_innerContainer); - moveByWithDuration(_autoMoveDuration, delta); -} - -void UIDragPanel::stopAutoMove() -{ - _moveType = DRAGPANEL_MOVE_TYPE_NONE; -} - -void UIDragPanel::setAutoMoveDuration(float duration) -{ - _autoMoveDuration = duration; -} - -void UIDragPanel::setAutoMoveEaseRate(float rate) -{ - _autoMoveEaseRate = rate; -} - -// berth - -// check if move to boundary - -bool UIDragPanel::checkToBoundaryWithDeltaPosition(const Point& delta) -{ - float innerLeft = _innerContainer->getLeftInParent(); - float innerTop = _innerContainer->getTopInParent(); - float innerRight = _innerContainer->getRightInParent(); - float innerBottom = _innerContainer->getBottomInParent(); - - float left = 0; - float top = _size.height; - float right = _size.width; - float bottom = 0; - - bool toLeftBottom = false; - bool toLeftTop = false; - bool toRightBottom = false; - bool toRightTop = false; - bool toLeft = false; - bool toRight = false; - bool toTop = false; - bool toBottom = false; - - if (innerLeft + delta.x > left && innerBottom + delta.y > bottom) // left bottom - { - toLeftBottom = true; - } - else if (innerLeft + delta.x > left && innerTop + delta.y < top) // left top - { - toLeftTop = true; - } - else if (innerRight + delta.x < right && innerBottom + delta.y > bottom) // right bottom - { - toRightBottom = true; - } - else if (innerRight + delta.x < right && innerTop + delta.y < top) // right top - { - toRightTop = true; - } - else if (innerLeft + delta.x > left) // left - { - toLeft = true; - } - else if (innerRight + delta.x < right) // right - { - toRight = true; - } - else if (innerTop + delta.y < top) // top - { - toTop = true; - } - else if (innerBottom + delta.y > bottom) // bottom - { - toBottom = true; - } - - if (toLeft || toTop || toRight || toBottom - || toLeftBottom || toLeftTop || toRightBottom || toRightTop) - { - return true; - } - - return false; -} - -Point UIDragPanel::calculateToBoundaryDeltaPosition(const Point& paramDelta) -{ - float innerLeft = _innerContainer->getLeftInParent(); - float innerTop = _innerContainer->getTopInParent(); - float innerRight = _innerContainer->getRightInParent(); - float innerBottom = _innerContainer->getBottomInParent(); - - float left = 0; - float top = _size.height; - float right = _size.width; - float bottom = 0; - - Point delta = paramDelta; - - if (innerLeft + delta.x > left && innerBottom + delta.y > bottom) // left bottom - { - delta.x = left - innerLeft; - delta.y = bottom - innerBottom; - } - else if (innerLeft + delta.x > left && innerTop + delta.y < top) // left top - { - delta.x = left - innerLeft; - delta.y = top - innerTop; - } - else if (innerRight + delta.x < right && innerBottom + delta.y > bottom) // right bottom - { - delta.x = right - innerRight; - delta.y = bottom - innerBottom; - } - else if (innerRight + delta.x < right && innerTop + delta.y < top) // right bottom - { - delta.x = right - innerRight; - delta.y = top - innerTop; - } - else if (innerLeft + delta.x > left) // left - { - delta.x = left - innerLeft; - } - else if (innerRight + delta.x < right) // right - { - delta.x = right - innerRight; - } - else if (innerTop + delta.y < top) // top - { - delta.y = top - innerTop; - } - else if (innerBottom + delta.y > bottom) // bottom - { - delta.y = bottom - innerBottom; - } - - return delta; -} - -bool UIDragPanel::isBerth() -{ - return _berthDirection != DRAGPANEL_BERTH_DIR_NONE; -} - -// check berth -bool UIDragPanel::checkBerth() -{ - float innerLeft = _innerContainer->getLeftInParent(); - float innerTop = _innerContainer->getTopInParent(); - float innerRight = _innerContainer->getRightInParent(); - float innerBottom = _innerContainer->getBottomInParent(); - - float left = 0; - float top = _size.height; - float right = _size.width; - float bottom = 0; - - if (innerLeft == left && innerBottom == bottom) // left bottom - { - _berthDirection = DRAGPANEL_BERTH_DIR_LEFTBOTTOM; - } - else if (innerLeft == left && innerTop == top) // left top - { - _berthDirection = DRAGPANEL_BERTH_DIR_LFETTOP; - } - else if (innerRight == right && innerBottom == bottom) // right bottom - { - _berthDirection = DRAGPANEL_BERTH_DIR_RIGHTBOTTOM; - } - else if (innerRight == right && innerTop == top) // right top - { - _berthDirection = DRAGPANEL_BERTH_DIR_RIGHTTOP; - } - else if (innerLeft == left) // left - { - _berthDirection = DRAGPANEL_BERTH_DIR_LEFT; - } - else if (innerRight == right) // right - { - _berthDirection = DRAGPANEL_BERTH_DIR_RIGHT; - } - else if (innerTop == top) // top - { - _berthDirection = DRAGPANEL_BERTH_DIR_TOP; - } - else if (innerBottom == bottom) // bottom - { - _berthDirection = DRAGPANEL_BERTH_DIR_BOTTOM; - } - - if (_berthDirection != DRAGPANEL_BERTH_DIR_NONE) - { - return true; - } - - return false; -} - -void UIDragPanel::berthEvent() -{ - switch (_berthDirection) - { - case DRAGPANEL_BERTH_DIR_LEFTBOTTOM: - berthToLeftBottomEvent(); - break; - - case DRAGPANEL_BERTH_DIR_LFETTOP: - berthToLeftTopEvent(); - break; - - case DRAGPANEL_BERTH_DIR_RIGHTBOTTOM: - berthToRightBottomEvent(); - break; - - case DRAGPANEL_BERTH_DIR_RIGHTTOP: - berthToRightTopEvent(); - break; - - case DRAGPANEL_BERTH_DIR_LEFT: - berthToLeftEvent(); - break; - - case DRAGPANEL_BERTH_DIR_TOP: - berthToTopEvent(); - break; - - case DRAGPANEL_BERTH_DIR_RIGHT: - berthToRightEvent(); - break; - - case DRAGPANEL_BERTH_DIR_BOTTOM: - berthToBottomEvent(); - break; - - default: - break; - } -} - -void UIDragPanel::berthToLeftBottomEvent() -{ - if (_eventLister && _eventSelector) - { - (_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BERTH_LEFTBOTTOM); - } -} - -void UIDragPanel::berthToLeftTopEvent() -{ - if (_eventLister && _eventSelector) - { - (_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BERTH_LFETTOP); - } -} - -void UIDragPanel::berthToRightBottomEvent() -{ - if (_eventLister && _eventSelector) - { - (_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BERTH_RIGHTBOTTOM); - } -} - -void UIDragPanel::berthToRightTopEvent() -{ - if (_eventLister && _eventSelector) - { - (_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BERTH_RIGHTTOP); - } -} - -void UIDragPanel::berthToLeftEvent() -{ - if (_eventLister && _eventSelector) - { - (_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BERTH_LEFT); - } -} - -void UIDragPanel::berthToTopEvent() -{ - if (_eventLister && _eventSelector) - { - (_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BERTH_TOP); - } -} - -void UIDragPanel::berthToRightEvent() -{ - if (_eventLister && _eventSelector) - { - (_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BERTH_RIGHT); - } -} - -void UIDragPanel::berthToBottomEvent() -{ - if (_eventLister && _eventSelector) - { - (_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BERTH_BOTTOM); - } -} - -void UIDragPanel::addEventListener(Object *target, SEL_DragPanelEvent selector) -{ - _eventLister = target; - _eventSelector = selector; -} - -// bounce -bool UIDragPanel::isBounceEnable() -{ - return _bounceEnable; -} - -void UIDragPanel::setBounceEnable(bool bounce) -{ - _bounceEnable = bounce; -} - -bool UIDragPanel::checkNeedBounce() -{ - float innerLeft = _innerContainer->getLeftInParent(); - float innerTop = _innerContainer->getTopInParent(); - float innerRight = _innerContainer->getRightInParent(); - float innerBottom = _innerContainer->getBottomInParent(); - - float left = 0; - float top = _size.height; - float right = _size.width; - float bottom = 0; - - bool need = ((innerLeft > left && innerBottom > bottom) - || (innerLeft > left && innerTop < top) - || (innerRight < right && innerBottom > bottom) - || (innerRight < right && innerTop < top) - || (innerLeft > left) - || (innerTop < top) - || (innerRight < right) - || (innerBottom > bottom)); - return need; -} - -void UIDragPanel::startBounce() -{ - if (_moveType == DRAGPANEL_MOVE_TYPE_BOUNCE) - { - return; - } - - actionStop(); - _moveType = DRAGPANEL_MOVE_TYPE_BOUNCE; - bounceToCorner(); -} - -void UIDragPanel::stopBounce() -{ - _moveType = DRAGPANEL_MOVE_TYPE_NONE; -} - -void UIDragPanel::bounceToCorner() -{ - float innerLeft = _innerContainer->getLeftInParent(); - float innerTop = _innerContainer->getTopInParent(); - float innerRight = _innerContainer->getRightInParent(); - float innerBottom = _innerContainer->getBottomInParent(); - - float width = _size.width; - float height = _size.height; - float left = 0; - float top = height; - float right = width; - float bottom = 0; - - float from_x = 0; - float from_y = 0; - float to_x = 0; - float to_y = 0; - Point delta = Point::ZERO; - - if (innerLeft > left && innerBottom > bottom) // left bottom - { - from_x = innerLeft; - from_y = innerBottom; - to_x = left; - to_y = bottom; - - _bounceDirection = DRAGPANEL_BOUNCE_DIR_LEFTBOTTOM; - } - else if (innerLeft > left && innerTop < top) // left top - { - from_x = innerLeft; - from_y = innerTop; - to_x = left; - to_y = top; - - _bounceDirection = DRAGPANEL_BOUNCE_DIR_LEFTTOP; - } - else if (innerRight < right && innerBottom > bottom) // right bottom - { - from_x = innerRight; - from_y = innerBottom; - to_x = right; - to_y = bottom; - - _bounceDirection = DRAGPANEL_BOUNCE_DIR_RIGHTBOTTOM; - } - else if (innerRight < right && innerTop < top) // right top - { - from_x = innerRight; - from_y = innerTop; - to_x = right; - to_y = top; - - _bounceDirection = DRAGPANEL_BOUNCE_DIR_RIGHTTOP; - } - else if (innerLeft > left) // left - { - from_x = innerLeft; - from_y = innerBottom; - to_x = left; - to_y = from_y; - - _bounceDirection = DRAGPANEL_BOUNCE_DIR_LEFT; - } - else if (innerTop < top) // top - { - from_x = innerLeft; - from_y = innerTop; - to_x = from_x; - to_y = top; - - _bounceDirection = DRAGPANEL_BOUNCE_DIR_TOP; - } - else if (innerRight < right) // right - { - from_x = innerRight; - from_y = innerBottom; - to_x = right; - to_y = from_y; - - _bounceDirection = DRAGPANEL_BOUNCE_DIR_RIGHT; - } - else if (innerBottom > bottom) // bottom - { - from_x = innerLeft; - from_y = innerBottom; - to_x = from_x; - to_y = bottom; - - _bounceDirection = DRAGPANEL_BOUNCE_DIR_BOTTOM; - } - delta = Point(to_x, to_y) - Point(from_x, from_y); -// delta = ccpSub(ccp(to_x, to_y), ccp(from_x, from_y)); - - actionStartWithWidget(_innerContainer); - moveByWithDuration(_bounceDuration, delta); -} - -void UIDragPanel::bounceOver() -{ - stopBounce(); - - switch (_bounceDirection) - { - case DRAGPANEL_BOUNCE_DIR_LEFTBOTTOM: - bounceToLeftBottomEvent(); - break; - - case DRAGPANEL_BOUNCE_DIR_LEFTTOP: - bounceToLeftTopEvent(); - break; - - case DRAGPANEL_BOUNCE_DIR_RIGHTBOTTOM: - bounceToRightBottomEvent(); - break; - - case DRAGPANEL_BOUNCE_DIR_RIGHTTOP: - bounceToRightTopEvent(); - break; - - case DRAGPANEL_BOUNCE_DIR_LEFT: - bounceToLeftEvent(); - break; - - case DRAGPANEL_BOUNCE_DIR_TOP: - bounceToTopEvent(); - break; - - case DRAGPANEL_BOUNCE_DIR_RIGHT: - bounceToRightEvent(); - break; - - case DRAGPANEL_BOUNCE_DIR_BOTTOM: - bounceToBottomEvent(); - break; - - default: - break; - } - - _bounceDirection = DRAGPANEL_BOUNCE_DIR_NONE; -} - -void UIDragPanel::bounceToLeftBottomEvent() -{ - if (_eventLister && _eventSelector) - { - (_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BOUNCE_LEFTBOTTOM); - } -} - -void UIDragPanel::bounceToLeftTopEvent() -{ - if (_eventLister && _eventSelector) - { - (_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BOUNCE_LEFTTOP); - } -} - -void UIDragPanel::bounceToRightBottomEvent() -{ - if (_eventLister && _eventSelector) - { - (_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BOUNCE_RIGHTBOTTOM); - } -} - -void UIDragPanel::bounceToRightTopEvent() -{ - if (_eventLister && _eventSelector) - { - (_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BOUNCE_RIGHTTOP); - } -} - -void UIDragPanel::bounceToLeftEvent() -{ - if (_eventLister && _eventSelector) - { - (_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BOUNCE_LEFT); - } -} - -void UIDragPanel::bounceToTopEvent() -{ - if (_eventLister && _eventSelector) - { - (_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BOUNCE_TOP); - } -} - -void UIDragPanel::bounceToRightEvent() -{ - if (_eventLister && _eventSelector) - { - (_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BOUNCE_RIGHT); - } -} - -void UIDragPanel::bounceToBottomEvent() -{ - if (_eventLister && _eventSelector) - { - (_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BOUNCE_BOTTOM); - } -} - -// widget action -void UIDragPanel::actionWithDuration(float duration) -{ - _duration = duration; - - if (_duration == 0) - { - _duration = FLT_EPSILON; - } - - _elapsed = 0; - _firstTick = true; -} - -bool UIDragPanel::actionIsDone() -{ - bool value = (_elapsed >= _duration); - return value; -} - -void UIDragPanel::actionStartWithWidget(UIWidget *widget) -{ - _runningAction = true; - _actionWidget = widget; -} - -void UIDragPanel::actionStep(float dt) -{ - if (_firstTick) - { - _firstTick = false; - _elapsed = 0; - } - else - { - _elapsed += dt; - } - - actionUpdate(MAX (0, - MIN(1, _elapsed / - MAX(_duration, FLT_EPSILON) - ) - ) - ); -} - -void UIDragPanel::actionUpdate(float dt) -{ - switch (_actionType) - { - case 1: // move by - moveByUpdate(dt); - break; - - case 2: // move to - moveToUpdate(dt); - break; - - default: - break; - } -} - -void UIDragPanel::actionStop() -{ - _runningAction = false; -} - -void UIDragPanel::actionDone() -{ - switch (_moveType) - { - case DRAGPANEL_MOVE_TYPE_AUTOMOVE: - autoMoveOver(); - break; - - case DRAGPANEL_MOVE_TYPE_BOUNCE: - bounceOver(); - break; - - default: - break; - } -} - -// move by -void UIDragPanel::moveByWithDuration(float duration, const Point& deltaPosition) -{ - actionWithDuration(duration); - _positionDelta = deltaPosition; - moveByInit(); - _actionType = 1; -} - -void UIDragPanel::moveByInit() -{ - _previousPosition = _startPosition = _actionWidget->getPosition(); -} - -void UIDragPanel::moveByUpdate(float t) -{ - float easeRate = 0.0f; - switch (_moveType) - { - case DRAGPANEL_MOVE_TYPE_AUTOMOVE: - easeRate = _autoMoveEaseRate; - break; - - case DRAGPANEL_MOVE_TYPE_BOUNCE: - easeRate = _bounceEaseRate; - break; - - default: - break; - } - t = powf(t, 1 / easeRate); - - Point currentPos = _actionWidget->getPosition(); - Point diff = currentPos - _previousPosition; - _startPosition = _startPosition + diff; -// Point diff = ccpSub(currentPos, _previousPosition); -// _startPosition = ccpAdd( _startPosition, diff); - -// Point newPos = ccpAdd( _startPosition, ccpMult(_positionDelta, t) ); - Point newPos = _startPosition + (_positionDelta * t); - - _actionWidget->setPosition(newPos); - _previousPosition = newPos; - - switch (_moveType) - { - case DRAGPANEL_MOVE_TYPE_AUTOMOVE: - autoMove(); - break; - - default: - break; - } -} - -// move to -void UIDragPanel::moveToWithDuration(float duration, const Point& position) -{ - actionWithDuration(duration); - _endPosition = position; - moveToInit(); - _actionType = 2; -} - -void UIDragPanel::moveToInit() -{ - moveByInit(); - _positionDelta = _endPosition - _actionWidget->getPosition(); -// _positionDelta = ccpSub( _endPosition, _actionWidget->getPosition() ); -} - -void UIDragPanel::moveToUpdate(float t) -{ - moveByUpdate(t); -} - -Layout* UIDragPanel::getInnerContainer() -{ - return _innerContainer; -} - -void UIDragPanel::setLayoutType(LayoutType type) -{ - _innerContainer->setLayoutType(type); -} - -LayoutType UIDragPanel::getLayoutType() const -{ - return _innerContainer->getLayoutType(); -} - -void UIDragPanel::doLayout() -{ - _innerContainer->doLayout(); -} - -const char* UIDragPanel::getDescription() const -{ - return "DragPanel"; -} - -} \ No newline at end of file diff --git a/cocos/gui/UIDragPanel.h b/cocos/gui/UIDragPanel.h deleted file mode 100644 index d6b4f0b62c..0000000000 --- a/cocos/gui/UIDragPanel.h +++ /dev/null @@ -1,367 +0,0 @@ -/**************************************************************************** - Copyright (c) 2013 cocos2d-x.org - - http://www.cocos2d-x.org - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in - all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. - ****************************************************************************/ - -#ifndef __UIDRAGPANEL_H__ -#define __UIDRAGPANEL_H__ - -#include "gui/Layout.h" -#include "gui/UIScrollInterface.h" - -namespace gui { - -/** - * drag panel move type - */ -enum DRAGPANEL_MOVE_TYPE -{ - DRAGPANEL_MOVE_TYPE_NONE, - DRAGPANEL_MOVE_TYPE_AUTOMOVE, - DRAGPANEL_MOVE_TYPE_BOUNCE, -}; - -/** - * dragpanel berth direction - */ -enum DRAGPANEL_BERTH_DIR -{ - DRAGPANEL_BERTH_DIR_NONE, - DRAGPANEL_BERTH_DIR_LEFTBOTTOM, - DRAGPANEL_BERTH_DIR_LFETTOP, - DRAGPANEL_BERTH_DIR_RIGHTBOTTOM, - DRAGPANEL_BERTH_DIR_RIGHTTOP, - DRAGPANEL_BERTH_DIR_LEFT, - DRAGPANEL_BERTH_DIR_TOP, - DRAGPANEL_BERTH_DIR_RIGHT, - DRAGPANEL_BERTH_DIR_BOTTOM, -}; - -/** - * dragpanel bounce direction - */ -enum DRAGPANEL_BOUNCE_DIR -{ - DRAGPANEL_BOUNCE_DIR_NONE, - DRAGPANEL_BOUNCE_DIR_LEFTBOTTOM, - DRAGPANEL_BOUNCE_DIR_LEFTTOP, - DRAGPANEL_BOUNCE_DIR_RIGHTBOTTOM, - DRAGPANEL_BOUNCE_DIR_RIGHTTOP, - DRAGPANEL_BOUNCE_DIR_LEFT, - DRAGPANEL_BOUNCE_DIR_TOP, - DRAGPANEL_BOUNCE_DIR_RIGHT, - DRAGPANEL_BOUNCE_DIR_BOTTOM, -}; - -typedef enum -{ - DRAGPANEL_EVENT_BERTH_LEFTBOTTOM, - DRAGPANEL_EVENT_BERTH_LFETTOP, - DRAGPANEL_EVENT_BERTH_RIGHTBOTTOM, - DRAGPANEL_EVENT_BERTH_RIGHTTOP, - DRAGPANEL_EVENT_BERTH_LEFT, - DRAGPANEL_EVENT_BERTH_TOP, - DRAGPANEL_EVENT_BERTH_RIGHT, - DRAGPANEL_EVENT_BERTH_BOTTOM, - DRAGPANEL_EVENT_BOUNCE_LEFTBOTTOM, - DRAGPANEL_EVENT_BOUNCE_LEFTTOP, - DRAGPANEL_EVENT_BOUNCE_RIGHTBOTTOM, - DRAGPANEL_EVENT_BOUNCE_RIGHTTOP, - DRAGPANEL_EVENT_BOUNCE_LEFT, - DRAGPANEL_EVENT_BOUNCE_TOP, - DRAGPANEL_EVENT_BOUNCE_RIGHT, - DRAGPANEL_EVENT_BOUNCE_BOTTOM, -}DragPanelEventType; - -/** - * dragpanel event - */ -typedef void (cocos2d::Object::*SEL_DragPanelEvent)(cocos2d::Object*, DragPanelEventType); -#define dragpaneleventselector(_SELECTOR)(SEL_DragPanelEvent)(&_SELECTOR) - -class UIDragPanel : public Layout, public UIScrollInterface -{ -public: - UIDragPanel(); - virtual ~UIDragPanel(); - - static UIDragPanel* create(); - - virtual bool onTouchBegan(const cocos2d::Point &touchPoint); - virtual void onTouchMoved(const cocos2d::Point &touchPoint); - virtual void onTouchEnded(const cocos2d::Point &touchPoint); - virtual void onTouchCancelled(const cocos2d::Point &touchPoint); - virtual void onTouchLongClicked(const cocos2d::Point &touchPoint); - - virtual void update(float dt); - - /** - * add widget child override - */ - virtual bool addChild(UIWidget* widget); - /** - * remove widget child override - */ - virtual bool removeChild(UIWidget* child); - /** - * remove all widget children override - */ - virtual void removeAllChildren(); - /** - * get widget children of inner container - */ - virtual cocos2d::Array* getChildren(); - /* gui mark */ - /** - * get and set inner container size - */ - const cocos2d::Size& getInnerContainerSize() const; - void setInnerContainerSize(const cocos2d::Size &size); - /** - * get and set inner container position - */ - const cocos2d::Point& getInnerContainerPosition() const; - void setInnerContainerPosition(const cocos2d::Point& point, bool animated); - /** - * set inner container offset - */ - void setInnerContainerOffset(const cocos2d::Point& offset, bool animated); - /**/ - - // auto move - /** - * set auto move duration - */ - void setAutoMoveDuration(float duration); - /** - * set auto move ease rate - */ - void setAutoMoveEaseRate(float rate); - - // berth - /** - * get berth or not - */ - bool isBerth(); - - /** - * event - */ - void addEventListener(cocos2d::Object* target, SEL_DragPanelEvent selector); - - /** - * get and set bounce enable - */ - bool isBounceEnable(); - void setBounceEnable(bool bounce); - /** - * set bounce duration - */ - void setBounceDuratoin(float duration); - /** - * set bounce ease rate - */ - void setBounceEaseRate(float rate); - - /** - * Gets inner container of dragpanel. - * - * Inner container is the container of dragpanel's children. - * - * @return inner container. - */ - Layout* getInnerContainer(); - - /** - * Sets LayoutType. - * - * @see LayoutType - * - * @param LayoutType - */ - virtual void setLayoutType(LayoutType type); - - /** - * Gets LayoutType. - * - * @see LayoutType - * - * @return LayoutType - */ - virtual LayoutType getLayoutType() const; - - virtual void doLayout(); - - /** - * Returns the "class name" of widget. - */ - virtual const char* getDescription() const; - -protected: - virtual bool init(); - virtual void initRenderer(); - virtual void releaseResoures(); - - virtual void handlePressLogic(const cocos2d::Point &touchPoint); - virtual void handleMoveLogic(const cocos2d::Point &touchPoint); - virtual void handleReleaseLogic(const cocos2d::Point &touchPoint); - virtual void interceptTouchEvent(int handleState,UIWidget* sender, const cocos2d::Point &touchPoint); - /* gui mark */ -// virtual bool isInScrollDegreeRange(UIWidget* widget); - /**/ - virtual void checkChildInfo(int handleState, UIWidget *sender, const cocos2d::Point &touchPoint); -// void updateWidthAndHeight(); - void recordSlidTime(float dt); - - /* gui mark */ - void setInnerContainerOffset(const cocos2d::Point& offset); - /**/ - - // check if dragpanel rect contain inner rect - bool checkContainInnerRect(); - - // move - void moveWithDelta(const cocos2d::Point& delta); - - // auto move - void autoMove(); - void autoMoveOver(); - void startAutoMove(); - void stopAutoMove(); - - // berth - // check if move to boundary with update - bool checkToBoundaryWithDeltaPosition(const cocos2d::Point& delta); - - // calculate to boundary delta - cocos2d::Point calculateToBoundaryDeltaPosition(const cocos2d::Point& paramDelta); - - // check berth - bool checkBerth(); - - // berth event - void berthEvent(); - void berthToLeftEvent(); - void berthToRightEvent(); - void berthToTopEvent(); - void berthToBottomEvent(); - void berthToLeftBottomEvent(); - void berthToLeftTopEvent(); - void berthToRightBottomEvent(); - void berthToRightTopEvent(); - - // bounce - bool checkNeedBounce(); - void startBounce(); - void stopBounce(); - void bounceToCorner(); - void bounceOver(); - // bounce event - void bounceToLeftBottomEvent(); - void bounceToRightBottomEvent(); - void bounceToLeftTopEvent(); - void bounceToRightTopEvent(); - void bounceToLeftEvent(); - void bounceToTopEvent(); - void bounceToRightEvent(); - void bounceToBottomEvent(); - - void actionWithDuration(float duration); - bool actionIsDone(); - void actionStartWithWidget(UIWidget* widget); - void actionStep(float dt); - void actionUpdate(float dt); - void actionStop(); - void actionDone(); - void moveByWithDuration(float duration, const cocos2d::Point& deltaPosition); - void moveByInit(); - void moveByUpdate(float t); - void moveToWithDuration(float duration, const cocos2d::Point& position); - void moveToInit(); - void moveToUpdate(float t); - virtual void onSizeChanged(); - /*compatible*/ - /** - * These methods will be removed - */ - virtual void setClippingEnable(bool is){setClippingEnabled(is);}; - /************/ - virtual void setClippingEnabled(bool able){Layout::setClippingEnabled(able);}; -protected: - Layout* _innerContainer; - - /* - DRAGPANEL_DIR m_eDirection; - DRAGPANEL_MOVE_DIR m_eMoveDirection; - */ - - bool _touchPressed; - bool _touchMoved; - bool _touchReleased; - bool _touchCanceld; // check touch out of drag panel boundary - - cocos2d::Point _touchStartNodeSpace; - cocos2d::Point _touchStartWorldSpace; - cocos2d::Point _touchEndWorldSpace; - - float _slidTime; - - // move type - DRAGPANEL_MOVE_TYPE _moveType; - - // auto move - float _autoMoveDuration; - float _autoMoveEaseRate; - - // event - cocos2d::Object* _eventLister; - SEL_DragPanelEvent _eventSelector; - - // berth - DRAGPANEL_BERTH_DIR _berthDirection; - - // bounce - bool _bounceEnable; - DRAGPANEL_BOUNCE_DIR _bounceDirection; - float _bounceDuration; - float _bounceEaseRate; - - - float _runningAction; - int _actionType; - - UIWidget* _actionWidget; - - float _duration; - float _elapsed; - bool _firstTick; - - cocos2d::Point _positionDelta; - cocos2d::Point _startPosition; - cocos2d::Point _previousPosition; - - cocos2d::Point _endPosition; -}; - -} - -#endif /* defined(__TestCpp__UIDragPanel__) */ diff --git a/cocos/gui/UIHelper.cpp b/cocos/gui/UIHelper.cpp index 409d0f4461..d896a6372c 100644 --- a/cocos/gui/UIHelper.cpp +++ b/cocos/gui/UIHelper.cpp @@ -22,111 +22,9 @@ THE SOFTWARE. ****************************************************************************/ -#include "gui/UIHelper.h" -#include "cocos2d.h" -#include "cocostudio/DictionaryHelper.h" -#include "cocostudio/CCSGUIReader.h" - - -using namespace cocos2d; -using namespace cocostudio; +#include "CocosGUI.h" namespace gui { - -static UIHelper* helperInstance = NULL; - -UIHelper* UIHelper::instance() -{ - if (!helperInstance) - { - helperInstance = new UIHelper(); - } - return helperInstance; -} - -void UIHelper::purgeUIHelper() -{ - CC_SAFE_DELETE(helperInstance); -} - -UIHelper::UIHelper(): -_textureFiles(NULL), -_fileDesignHeight(0.0f), -_fileDesignWidth(0.0f) -{ - Size winSize = Director::getInstance()->getWinSize(); - _fileDesignWidth = winSize.width; - _fileDesignHeight = winSize.height; - init(); -} - -UIHelper::~UIHelper() -{ - cocostudio::CCSGUIReader::purgeCCSGUIReader(); -} - -void UIHelper::init() -{ - _textureFiles = CCArray::create(); - _textureFiles->retain(); -} - -UIWidget* UIHelper::createWidgetFromJsonFile(const char *fileName) -{ - return CCSGUIReader::shareReader()->widgetFromJsonFile(fileName); -} - -void UIHelper::addSpriteFrame(const char *fileName) -{ - if (!fileName || strcmp(fileName, "") == 0) - { - return; - } - ccArray* arrayTextures = _textureFiles->data; - int length = arrayTextures->num; - for (int i=0;iarr[i]); - if (strcmp(file->_string.c_str(), fileName) == 0) - { - return; - } - } - _textureFiles->addObject(CCString::create(fileName)); - SpriteFrameCache::getInstance()->addSpriteFramesWithFile(fileName); -} - -void UIHelper::removeSpriteFrame(const char *fileName) -{ - if (!fileName || strcmp(fileName, "") == 0) - { - return; - } - ccArray* arrayTextures = _textureFiles->data; - int length = arrayTextures->num; - for (int i=0;iarr[i]); - if (strcmp(file->_string.c_str(), fileName) == 0) - { - SpriteFrameCache::getInstance()->removeSpriteFrameByName(fileName); - _textureFiles->removeObject(file); - return; - } - } -} - -void UIHelper::removeAllSpriteFrame() -{ - ccArray* arrayTextures = _textureFiles->data; - int length = arrayTextures->num; - for (int i=0;iarr[i]); - SpriteFrameCache::getInstance()->removeSpriteFrameByName(file->_string.c_str()); - } - _textureFiles->removeAllObjects(); -} UIWidget* UIHelper::seekWidgetByTag(UIWidget* root, int tag) { @@ -138,7 +36,7 @@ UIWidget* UIHelper::seekWidgetByTag(UIWidget* root, int tag) { return root; } - ccArray* arrayRootChildren = root->getChildren()->data; + cocos2d::ccArray* arrayRootChildren = root->getChildren()->data; int length = arrayRootChildren->num; for (int i=0;igetChildren()->data; + cocos2d::ccArray* arrayRootChildren = root->getChildren()->data; int length = arrayRootChildren->num; for (int i=0;igetChildren()->data; + cocos2d::ccArray* arrayRootChildren = root->getChildren()->data; int length = arrayRootChildren->num; for (int i=0;iarr[i]); - RelativeLayoutParameter* layoutParameter = dynamic_cast(child->getLayoutParameter()); + UIRelativeLayoutParameter* layoutParameter = dynamic_cast(child->getLayoutParameter(LAYOUT_PARAMETER_RELATIVE)); if (layoutParameter && strcmp(layoutParameter->getRelativeName(), name) == 0) { return child; @@ -196,26 +94,6 @@ UIWidget* UIHelper::seekWidgetByRelativeName(UIWidget *root, const char *name) return NULL; } -void UIHelper::setFileDesignWidth(float width) -{ - _fileDesignWidth = width; -} - -float UIHelper::getFileDesignWidth() -{ - return _fileDesignWidth; -} - -void UIHelper::setFileDesignHeight(float height) -{ - _fileDesignHeight = height; -} - -float UIHelper::getFileDesignHeight() -{ - return _fileDesignHeight; -} - /*temp action*/ UIWidget* UIHelper::seekActionWidgetByActionTag(UIWidget* root, int tag) { @@ -227,7 +105,7 @@ UIWidget* UIHelper::seekActionWidgetByActionTag(UIWidget* root, int tag) { return root; } - ccArray* arrayRootChildren = root->getChildren()->data; + cocos2d::ccArray* arrayRootChildren = root->getChildren()->data; int length = arrayRootChildren->num; for (int i=0;i(_imageRenderer) #define DYNAMIC_CAST_SCALE9SPRITE dynamic_cast(_imageRenderer) @@ -41,7 +39,7 @@ _touchRelease(false), _doubleClickEnabled(false), _scale9Enabled(false), _prevIgnoreSize(true), -_capInsets(Rect::ZERO), +_capInsets(cocos2d::Rect::ZERO), _imageRenderer(NULL), _textureFile(""), _imageTexType(UI_TEX_TYPE_LOCAL), @@ -70,7 +68,7 @@ UIImageView* UIImageView::create() void UIImageView::initRenderer() { UIWidget::initRenderer(); - _imageRenderer = Sprite::create(); + _imageRenderer = cocos2d::Sprite::create(); _renderer->addChild(_imageRenderer); } @@ -90,6 +88,7 @@ void UIImageView::loadTexture(const char *fileName, TextureResType texType) DYNAMIC_CAST_SCALE9SPRITE->initWithFile(fileName); DYNAMIC_CAST_SCALE9SPRITE->setColor(getColor()); DYNAMIC_CAST_SCALE9SPRITE->setOpacity(getOpacity()); + DYNAMIC_CAST_SCALE9SPRITE->setCapInsets(_capInsets); } else { @@ -104,6 +103,7 @@ void UIImageView::loadTexture(const char *fileName, TextureResType texType) DYNAMIC_CAST_SCALE9SPRITE->initWithSpriteFrameName(fileName); DYNAMIC_CAST_SCALE9SPRITE->setColor(getColor()); DYNAMIC_CAST_SCALE9SPRITE->setOpacity(getOpacity()); + DYNAMIC_CAST_SCALE9SPRITE->setCapInsets(_capInsets); } else { @@ -120,7 +120,7 @@ void UIImageView::loadTexture(const char *fileName, TextureResType texType) imageTextureScaleChangedWithSize(); } -void UIImageView::setTextureRect(const Rect &rect) +void UIImageView::setTextureRect(const cocos2d::Rect &rect) { if (_scale9Enabled) { @@ -131,7 +131,7 @@ void UIImageView::setTextureRect(const Rect &rect) } } -bool UIImageView::onTouchBegan(const Point &touchPoint) +bool UIImageView::onTouchBegan(const cocos2d::Point &touchPoint) { setFocused(true); _touchStartPos.x = touchPoint.x; @@ -149,7 +149,7 @@ bool UIImageView::onTouchBegan(const Point &touchPoint) return _touchPassedEnabled; } -void UIImageView::onTouchEnded(const Point &touchPoint) +void UIImageView::onTouchEnded(const cocos2d::Point &touchPoint) { if (_doubleClickEnabled) { @@ -278,11 +278,11 @@ void UIImageView::setScale9Enabled(bool able) _imageRenderer = NULL; if (_scale9Enabled) { - _imageRenderer = extension::Scale9Sprite::create(); + _imageRenderer = cocos2d::extension::Scale9Sprite::create(); } else { - _imageRenderer = CCSprite::create(); + _imageRenderer = cocos2d::Sprite::create(); } loadTexture(_textureFile.c_str(),_imageTexType); _renderer->addChild(_imageRenderer); @@ -308,7 +308,7 @@ void UIImageView::ignoreContentAdaptWithSize(bool ignore) } } -void UIImageView::setCapInsets(const Rect &capInsets) +void UIImageView::setCapInsets(const cocos2d::Rect &capInsets) { _capInsets = capInsets; if (!_scale9Enabled) @@ -318,7 +318,7 @@ void UIImageView::setCapInsets(const Rect &capInsets) DYNAMIC_CAST_SCALE9SPRITE->setCapInsets(capInsets); } -void UIImageView::setAnchorPoint(const Point &pt) +void UIImageView::setAnchorPoint(const cocos2d::Point &pt) { UIWidget::setAnchorPoint(pt); _imageRenderer->setAnchorPoint(pt); @@ -329,12 +329,12 @@ void UIImageView::onSizeChanged() imageTextureScaleChangedWithSize(); } -const Size& UIImageView::getContentSize() const +const cocos2d::Size& UIImageView::getContentSize() const { return _imageTextureSize; } -Node* UIImageView::getVirtualRenderer() +cocos2d::Node* UIImageView::getVirtualRenderer() { return _imageRenderer; } @@ -353,11 +353,11 @@ void UIImageView::imageTextureScaleChangedWithSize() { if (_scale9Enabled) { - dynamic_cast(_imageRenderer)->setPreferredSize(_size); + dynamic_cast(_imageRenderer)->setPreferredSize(_size); } else { - Size textureSize = _imageRenderer->getContentSize(); + cocos2d::Size textureSize = _imageRenderer->getContentSize(); if (textureSize.width <= 0.0f || textureSize.height <= 0.0f) { _imageRenderer->setScale(1.0f); @@ -376,4 +376,21 @@ const char* UIImageView::getDescription() const return "ImageView"; } +UIWidget* UIImageView::createCloneInstance() +{ + return UIImageView::create(); +} + +void UIImageView::copySpecialProperties(UIWidget *widget) +{ + UIImageView* imageView = dynamic_cast(widget); + if (imageView) + { + _prevIgnoreSize = imageView->_prevIgnoreSize; + setScale9Enabled(imageView->_scale9Enabled); + loadTexture(imageView->_textureFile.c_str(), imageView->_imageTexType); + setCapInsets(imageView->_capInsets); + } +} + } \ No newline at end of file diff --git a/cocos/gui/UIImageView.h b/cocos/gui/UIImageView.h index 91bd2573ec..16103aa256 100644 --- a/cocos/gui/UIImageView.h +++ b/cocos/gui/UIImageView.h @@ -29,6 +29,10 @@ namespace gui { +/** +* @js NA +* @lua NA +*/ class UIImageView : public UIWidget { public: @@ -100,22 +104,22 @@ public: //override "ignoreContentAdaptWithSize" method of widget. virtual void ignoreContentAdaptWithSize(bool ignore); - + /** + * Returns the "class name" of widget. + */ + virtual const char* getDescription() const; void setDoubleClickEnabled(bool able); void doubleClickEvent(); void checkDoubleClick(float dt); virtual const cocos2d::Size& getContentSize() const; virtual cocos2d::Node* getVirtualRenderer(); - - /** - * Returns the "class name" of widget. - */ - virtual const char* getDescription() const; protected: virtual void initRenderer(); virtual void onSizeChanged(); void imageTextureScaleChangedWithSize(); + virtual UIWidget* createCloneInstance(); + virtual void copySpecialProperties(UIWidget* model); protected: int _clickCount; float _clickTimeInterval; diff --git a/cocos/gui/UIInputManager.cpp b/cocos/gui/UIInputManager.cpp index 0fe2eb153e..bb36043855 100644 --- a/cocos/gui/UIInputManager.cpp +++ b/cocos/gui/UIInputManager.cpp @@ -37,11 +37,11 @@ _longClickRecordTime(0.0), _checkedDoubleClickWidget(NULL), _rootWidget(NULL) { - _manageredWidget = CCArray::create(); + _manageredWidget = Array::create(); _manageredWidget->retain(); - _checkedDoubleClickWidget = CCArray::create(); + _checkedDoubleClickWidget = Array::create(); _checkedDoubleClickWidget->retain(); - _selectedWidgets = CCArray::create(); + _selectedWidgets = Array::create(); _selectedWidgets->retain(); } diff --git a/cocos/gui/UIInputManager.h b/cocos/gui/UIInputManager.h index 9547b091a1..ed5a931f89 100644 --- a/cocos/gui/UIInputManager.h +++ b/cocos/gui/UIInputManager.h @@ -26,7 +26,7 @@ #define __UIINPUTMANAGER_H__ #include "cocos2d.h" -#include "gui/Layout.h" +#include "gui/UILayout.h" namespace gui { @@ -41,7 +41,7 @@ public: /** * Default destructor */ - ~UIInputManager(); + virtual ~UIInputManager(); /** * Regist a widget to input manager. diff --git a/cocos/gui/UILabel.cpp b/cocos/gui/UILabel.cpp index 6ac5d68fdf..4c8779c9eb 100644 --- a/cocos/gui/UILabel.cpp +++ b/cocos/gui/UILabel.cpp @@ -24,10 +24,9 @@ #include "gui/UILabel.h" - using namespace cocos2d; - namespace gui { + UILabel::UILabel(): _touchScaleChangeEnabled(false), _normalScaleValue(1.0f), @@ -67,7 +66,7 @@ bool UILabel::init() void UILabel::initRenderer() { UIWidget::initRenderer(); - _labelRenderer = CCLabelTTF::create(); + _labelRenderer = cocos2d::LabelTTF::create(); _renderer->addChild(_labelRenderer); } @@ -95,29 +94,31 @@ int UILabel::getStringLength() void UILabel::setFontSize(int size) { + _fontSize = size; _labelRenderer->setFontSize(size); labelScaleChangedWithSize(); } void UILabel::setFontName(const char* name) { + _fontName = name; _labelRenderer->setFontName(name); labelScaleChangedWithSize(); } -void UILabel::setTextAreaSize(const Size &size) +void UILabel::setTextAreaSize(const cocos2d::Size &size) { _labelRenderer->setDimensions(size); labelScaleChangedWithSize(); } -void UILabel::setTextHorizontalAlignment(TextHAlignment alignment) +void UILabel::setTextHorizontalAlignment(cocos2d::TextHAlignment alignment) { _labelRenderer->setHorizontalAlignment(alignment); labelScaleChangedWithSize(); } -void UILabel::setTextVerticalAlignment(TextVAlignment alignment) +void UILabel::setTextVerticalAlignment(cocos2d::TextVAlignment alignment) { _labelRenderer->setVerticalAlignment(alignment); labelScaleChangedWithSize(); @@ -182,7 +183,7 @@ bool UILabel::isFlipY() return _labelRenderer->isFlippedY(); } -void UILabel::setAnchorPoint(const Point &pt) +void UILabel::setAnchorPoint(const cocos2d::Point &pt) { UIWidget::setAnchorPoint(pt); _labelRenderer->setAnchorPoint(pt); @@ -193,12 +194,12 @@ void UILabel::onSizeChanged() labelScaleChangedWithSize(); } -const Size& UILabel::getContentSize() const +const cocos2d::Size& UILabel::getContentSize() const { return _labelRenderer->getContentSize(); } -Node* UILabel::getVirtualRenderer() +cocos2d::Node* UILabel::getVirtualRenderer() { return _labelRenderer; } @@ -212,7 +213,7 @@ void UILabel::labelScaleChangedWithSize() } else { - Size textureSize = _labelRenderer->getContentSize(); + cocos2d::Size textureSize = _labelRenderer->getContentSize(); if (textureSize.width <= 0.0f || textureSize.height <= 0.0f) { _labelRenderer->setScale(1.0f); @@ -223,6 +224,7 @@ void UILabel::labelScaleChangedWithSize() _labelRenderer->setScaleX(scaleX); _labelRenderer->setScaleY(scaleY); } + } const char* UILabel::getDescription() const @@ -230,4 +232,21 @@ const char* UILabel::getDescription() const return "Label"; } +UIWidget* UILabel::createCloneInstance() +{ + return UILabel::create(); +} + +void UILabel::copySpecialProperties(UIWidget *widget) +{ + UILabel* label = dynamic_cast(widget); + if (label) + { + setFontName(label->_fontName.c_str()); + setFontSize(label->_labelRenderer->getFontSize()); + setText(label->getStringValue()); + setTouchScaleChangeEnabled(label->_touchScaleChangeEnabled); + } +} + } \ No newline at end of file diff --git a/cocos/gui/UILabel.h b/cocos/gui/UILabel.h index 0d9150f17d..5c6a4a105b 100644 --- a/cocos/gui/UILabel.h +++ b/cocos/gui/UILabel.h @@ -27,8 +27,13 @@ #include "gui/UIWidget.h" + namespace gui { +/** +* @js NA +* @lua NA +*/ class UILabel : public UIWidget { public: @@ -124,11 +129,7 @@ public: void setTextAreaSize(const cocos2d::Size &size); void setTextHorizontalAlignment(cocos2d::TextHAlignment alignment); - void setTextVerticalAlignment(cocos2d::TextVAlignment alignment); - - - void setTouchScaleChangeAble(bool able){setTouchScaleChangeEnabled(able);}; - bool getTouchScaleChangeAble(){return isTouchScaleChangeEnabled();}; + void setTextVerticalAlignment(cocos2d::TextVAlignment alignment); protected: virtual bool init(); virtual void initRenderer(); @@ -138,6 +139,8 @@ protected: virtual void onSizeChanged(); void clickScale(float scale); void labelScaleChangedWithSize(); + virtual UIWidget* createCloneInstance(); + virtual void copySpecialProperties(UIWidget* model); protected: bool _touchScaleChangeEnabled; float _normalScaleValue; diff --git a/cocos/gui/UILabelAtlas.cpp b/cocos/gui/UILabelAtlas.cpp index 72b816e4a6..8e8db6c422 100644 --- a/cocos/gui/UILabelAtlas.cpp +++ b/cocos/gui/UILabelAtlas.cpp @@ -24,10 +24,9 @@ #include "gui/UILabelAtlas.h" - using namespace cocos2d; - namespace gui { + UICCLabelAtlas::UICCLabelAtlas() { @@ -56,7 +55,7 @@ void UICCLabelAtlas::setProperty(const char *string, const char *charMapFile, un initWithString(string, charMapFile, itemWidth, itemHeight, startCharMap); } -void UICCLabelAtlas::setProperty(const char *string, Texture2D *texture, unsigned int itemWidth, unsigned int itemHeight, unsigned int startCharMap) +void UICCLabelAtlas::setProperty(const char *string, cocos2d::Texture2D *texture, unsigned int itemWidth, unsigned int itemHeight, unsigned int startCharMap) { initWithString(string, texture, itemWidth, itemHeight, startCharMap); } @@ -68,20 +67,26 @@ void UICCLabelAtlas::draw() return; } - CCAtlasNode::draw(); + cocos2d::AtlasNode::draw(); } void UICCLabelAtlas::updateDisplayedOpacity(GLubyte opacity) { - CCAtlasNode::setOpacity(opacity); + cocos2d::AtlasNode::setOpacity(opacity); } UILabelAtlas::UILabelAtlas(): -_laberAtlasRenderer(NULL) +_laberAtlasRenderer(NULL), +_stringValue(""), +_charMapFileName(""), +_itemWidth(0), +_itemHeight(0), +_startCharMap("") { + } UILabelAtlas::~UILabelAtlas() @@ -108,8 +113,13 @@ void UILabelAtlas::initRenderer() _renderer->addChild(_laberAtlasRenderer); } -void UILabelAtlas::setProperty(const char *stringValue, const char *charMapFile, int itemWidth, int itemHeight, const char *startCharMap,bool useSpriteFrame) +void UILabelAtlas::setProperty(const char *stringValue, const char *charMapFile, int itemWidth, int itemHeight, const char *startCharMap) { + _stringValue = stringValue; + _charMapFileName = charMapFile; + _itemWidth = itemWidth; + _itemHeight = itemHeight; + _startCharMap = startCharMap; _laberAtlasRenderer->setProperty(stringValue, charMapFile, itemWidth, itemHeight, (int)(startCharMap[0])); updateAnchorPoint(); labelAtlasScaleChangedWithSize(); @@ -117,6 +127,7 @@ void UILabelAtlas::setProperty(const char *stringValue, const char *charMapFile, void UILabelAtlas::setStringValue(const char *value) { + _stringValue = value; _laberAtlasRenderer->setString(value); labelAtlasScaleChangedWithSize(); } @@ -126,10 +137,10 @@ const char* UILabelAtlas::getStringValue() return _laberAtlasRenderer->getString(); } -void UILabelAtlas::setAnchorPoint(const Point &pt) +void UILabelAtlas::setAnchorPoint(const cocos2d::Point &pt) { UIWidget::setAnchorPoint(pt); - _laberAtlasRenderer->setAnchorPoint(Point(pt.x, pt.y)); + _laberAtlasRenderer->setAnchorPoint(cocos2d::Point(pt.x, pt.y)); } void UILabelAtlas::onSizeChanged() @@ -137,12 +148,12 @@ void UILabelAtlas::onSizeChanged() labelAtlasScaleChangedWithSize(); } -const Size& UILabelAtlas::getContentSize() const +const cocos2d::Size& UILabelAtlas::getContentSize() const { return _laberAtlasRenderer->getContentSize(); } -Node* UILabelAtlas::getVirtualRenderer() +cocos2d::Node* UILabelAtlas::getVirtualRenderer() { return _laberAtlasRenderer; } @@ -156,7 +167,7 @@ void UILabelAtlas::labelAtlasScaleChangedWithSize() } else { - Size textureSize = _laberAtlasRenderer->getContentSize(); + cocos2d::Size textureSize = _laberAtlasRenderer->getContentSize(); if (textureSize.width <= 0.0f || textureSize.height <= 0.0f) { _laberAtlasRenderer->setScale(1.0f); @@ -171,7 +182,21 @@ void UILabelAtlas::labelAtlasScaleChangedWithSize() const char* UILabelAtlas::getDescription() const { - return "LabelAtlase"; + return "LabelAtlas"; } +UIWidget* UILabelAtlas::createCloneInstance() +{ + return UILabelAtlas::create(); +} + +void UILabelAtlas::copySpecialProperties(UIWidget *widget) +{ + UILabelAtlas* labelAtlas = dynamic_cast(widget); + if (labelAtlas) + { + setProperty(labelAtlas->_stringValue.c_str(), labelAtlas->_charMapFileName.c_str(), labelAtlas->_itemWidth, labelAtlas->_itemHeight, labelAtlas->_startCharMap.c_str()); + } +} + } \ No newline at end of file diff --git a/cocos/gui/UILabelAtlas.h b/cocos/gui/UILabelAtlas.h index b74ea87644..73b902633f 100644 --- a/cocos/gui/UILabelAtlas.h +++ b/cocos/gui/UILabelAtlas.h @@ -29,6 +29,10 @@ namespace gui { +/** + * @js NA + * @lua NA + */ class UICCLabelAtlas : public cocos2d::LabelAtlas { public: @@ -51,7 +55,10 @@ public: virtual void updateDisplayedOpacity(GLubyte opacity); virtual void draw(void); }; - +/** + * @js NA + * @lua NA + */ class UILabelAtlas : public UIWidget { public: @@ -71,7 +78,7 @@ public: static UILabelAtlas* create(); /** initializes the UILabelAtlas with a string, a char map file(the atlas), the width and height of each element and the starting char of the atlas */ - void setProperty(const char* stringValue,const char* charMapFile, int itemWidth, int itemHeight, const char* startCharMap,bool useSpriteFrame = false); + void setProperty(const char* stringValue,const char* charMapFile, int itemWidth, int itemHeight, const char* startCharMap); //set string value for labelatlas. void setStringValue(const char* value); @@ -92,12 +99,20 @@ public: * Returns the "class name" of widget. */ virtual const char* getDescription() const; + protected: virtual void initRenderer(); virtual void onSizeChanged(); void labelAtlasScaleChangedWithSize(); + virtual UIWidget* createCloneInstance(); + virtual void copySpecialProperties(UIWidget* model); protected: UICCLabelAtlas* _laberAtlasRenderer; + std::string _stringValue; + std::string _charMapFileName; + int _itemWidth; + int _itemHeight; + std::string _startCharMap; }; } diff --git a/cocos/gui/UILabelBMFont.cpp b/cocos/gui/UILabelBMFont.cpp index b112c06bf4..d8560fd4a8 100644 --- a/cocos/gui/UILabelBMFont.cpp +++ b/cocos/gui/UILabelBMFont.cpp @@ -24,13 +24,13 @@ #include "gui/UILabelBMFont.h" - using namespace cocos2d; - namespace gui { UILabelBMFont::UILabelBMFont(): _labelBMFontRenderer(NULL), -_fntFileHasInit(false) +_fntFileHasInit(false), +_fntFileName(""), +_stringValue("") { } @@ -54,38 +54,45 @@ UILabelBMFont* UILabelBMFont::create() void UILabelBMFont::initRenderer() { UIWidget::initRenderer(); - _labelBMFontRenderer = CCLabelBMFont::create(); + _labelBMFontRenderer = cocos2d::LabelBMFont::create(); _renderer->addChild(_labelBMFontRenderer); } void UILabelBMFont::setFntFile(const char *fileName) { - if (!fileName || strcmp(fileName, "") == 0) + if (!fileName || std::strcmp(fileName, "") == 0) { return; } + _fntFileName = fileName; _labelBMFontRenderer->initWithString("", fileName); updateAnchorPoint(); labelBMFontScaleChangedWithSize(); _fntFileHasInit = true; + setText(_stringValue.c_str()); } void UILabelBMFont::setText(const char* value) { - if (!value || !_fntFileHasInit) + if (!value) { return; } - _labelBMFontRenderer->setString(value); + _stringValue = value; + if (!_fntFileHasInit) + { + return; + } + _labelBMFontRenderer->setString(value); labelBMFontScaleChangedWithSize(); } const char* UILabelBMFont::getStringValue() { - return _labelBMFontRenderer->getString(); + return _stringValue.c_str(); } -void UILabelBMFont::setAnchorPoint(const Point &pt) +void UILabelBMFont::setAnchorPoint(const cocos2d::Point &pt) { UIWidget::setAnchorPoint(pt); _labelBMFontRenderer->setAnchorPoint(pt); @@ -96,12 +103,12 @@ void UILabelBMFont::onSizeChanged() labelBMFontScaleChangedWithSize(); } -const Size& UILabelBMFont::getContentSize() const +const cocos2d::Size& UILabelBMFont::getContentSize() const { return _labelBMFontRenderer->getContentSize(); } -Node* UILabelBMFont::getVirtualRenderer() +cocos2d::Node* UILabelBMFont::getVirtualRenderer() { return _labelBMFontRenderer; } @@ -115,7 +122,7 @@ void UILabelBMFont::labelBMFontScaleChangedWithSize() } else { - Size textureSize = _labelBMFontRenderer->getContentSize(); + cocos2d::Size textureSize = _labelBMFontRenderer->getContentSize(); if (textureSize.width <= 0.0f || textureSize.height <= 0.0f) { _labelBMFontRenderer->setScale(1.0f); @@ -133,5 +140,19 @@ const char* UILabelBMFont::getDescription() const return "LabelBMFont"; } +UIWidget* UILabelBMFont::createCloneInstance() +{ + return UILabelBMFont::create(); +} + +void UILabelBMFont::copySpecialProperties(UIWidget *widget) +{ + UILabelBMFont* labelBMFont = dynamic_cast(widget); + if (labelBMFont) + { + setFntFile(labelBMFont->_fntFileName.c_str()); + setText(labelBMFont->_stringValue.c_str()); + } +} } \ No newline at end of file diff --git a/cocos/gui/UILabelBMFont.h b/cocos/gui/UILabelBMFont.h index ddf4e31728..5c03c1ae75 100644 --- a/cocos/gui/UILabelBMFont.h +++ b/cocos/gui/UILabelBMFont.h @@ -29,6 +29,10 @@ namespace gui { +/** +* @js NA +* @lua NA +*/ class UILabelBMFont : public UIWidget { public: @@ -58,7 +62,6 @@ public: virtual void setAnchorPoint(const cocos2d::Point &pt); virtual const cocos2d::Size& getContentSize() const; virtual cocos2d::Node* getVirtualRenderer(); - /** * Returns the "class name" of widget. */ @@ -67,9 +70,13 @@ protected: virtual void initRenderer(); virtual void onSizeChanged(); void labelBMFontScaleChangedWithSize(); + virtual UIWidget* createCloneInstance(); + virtual void copySpecialProperties(UIWidget* model); protected: cocos2d::LabelBMFont* _labelBMFontRenderer; bool _fntFileHasInit; + std::string _fntFileName; + std::string _stringValue; }; } diff --git a/cocos/gui/UILayer.cpp b/cocos/gui/UILayer.cpp index bfdea4094f..a935f9efc5 100644 --- a/cocos/gui/UILayer.cpp +++ b/cocos/gui/UILayer.cpp @@ -119,7 +119,7 @@ UIWidget* UILayer::getWidgetByTag(int tag) { return NULL; } - return CCUIHELPER->seekWidgetByTag(_rootWidget, tag); + return UIHelper::seekWidgetByTag(_rootWidget, tag); } UIWidget* UILayer::getWidgetByName(const char* name) @@ -128,7 +128,7 @@ UIWidget* UILayer::getWidgetByName(const char* name) { return NULL; } - return CCUIHELPER->seekWidgetByName(_rootWidget, name); + return UIHelper::seekWidgetByName(_rootWidget, name); } UIRootWidget* UILayer::getRootWidget() diff --git a/cocos/gui/UILayout.cpp b/cocos/gui/UILayout.cpp new file mode 100644 index 0000000000..70b66a0b56 --- /dev/null +++ b/cocos/gui/UILayout.cpp @@ -0,0 +1,1010 @@ +/**************************************************************************** + Copyright (c) 2013 cocos2d-x.org + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +#include "gui/UILayout.h" +#include "gui/UILayer.h" +#include "gui/UIHelper.h" +#include "extensions/GUI/CCControlExtension/CCScale9Sprite.h" + +namespace gui { + + +#define DYNAMIC_CAST_CLIPPINGLAYER dynamic_cast(_renderer) + +UILayout::UILayout(): +_clippingEnabled(false), +_backGroundScale9Enabled(false), +_backGroundImage(NULL), +_backGroundImageFileName(""), +_backGroundImageCapInsets(cocos2d::Rect::ZERO), +_colorType(LAYOUT_COLOR_NONE), +_bgImageTexType(UI_TEX_TYPE_LOCAL), +_colorRender(NULL), +_gradientRender(NULL), +_cColor(cocos2d::Color3B::WHITE), +_gStartColor(cocos2d::Color3B::WHITE), +_gEndColor(cocos2d::Color3B::WHITE), +_alongVector(cocos2d::Point(0.0f, -1.0f)), +_cOpacity(255), +_backGroundImageTextureSize(cocos2d::Size::ZERO), +_layoutType(LAYOUT_ABSOLUTE) +{ + _widgetType = WidgetTypeContainer; +} + +UILayout::~UILayout() +{ +} + +UILayout* UILayout::create() +{ + UILayout* layout = new UILayout(); + if (layout && layout->init()) + { + layout->autorelease(); + return layout; + } + CC_SAFE_DELETE(layout); + return NULL; +} + +bool UILayout::init() +{ + _children = cocos2d::Array::create(); + _children->retain(); + _layoutParameterDictionary = cocos2d::Dictionary::create(); + CC_SAFE_RETAIN(_layoutParameterDictionary); + initRenderer(); + _renderer->retain(); + _renderer->setZOrder(_widgetZOrder); + cocos2d::RGBAProtocol* renderRGBA = dynamic_cast(_renderer); + if (renderRGBA) + { + renderRGBA->setCascadeColorEnabled(false); + renderRGBA->setCascadeOpacityEnabled(false); + } + ignoreContentAdaptWithSize(false); + setSize(cocos2d::Size::ZERO); + setBright(true); + setAnchorPoint(cocos2d::Point(0, 0)); + _scheduler = cocos2d::Director::getInstance()->getScheduler(); + CC_SAFE_RETAIN(_scheduler); + return true; +} + +void UILayout::initRenderer() +{ + _renderer = UIRectClippingNode::create(); +} + +bool UILayout::addChild(UIWidget *child) +{ + supplyTheLayoutParameterLackToChild(child); + return UIWidget::addChild(child); +} + +bool UILayout::isClippingEnabled() +{ + return _clippingEnabled; +} + +bool UILayout::hitTest(const cocos2d::Point &pt) +{ + cocos2d::Point nsp = _renderer->convertToNodeSpace(pt); + cocos2d::Rect bb = cocos2d::Rect(0.0f, 0.0f, _size.width, _size.height); + if (nsp.x >= bb.origin.x && nsp.x <= bb.origin.x + bb.size.width && nsp.y >= bb.origin.y && nsp.y <= bb.origin.y + bb.size.height) + { + return true; + } + return false; +} + +void UILayout::setClippingEnabled(bool able) +{ + _clippingEnabled = able; + DYNAMIC_CAST_CLIPPINGLAYER->setClippingEnabled(able); +} + +void UILayout::onSizeChanged() +{ + DYNAMIC_CAST_CLIPPINGLAYER->setClippingSize(_size); + doLayout(); + if (_backGroundImage) + { + _backGroundImage->setPosition(cocos2d::Point(_size.width/2.0f, _size.height/2.0f)); + if (_backGroundScale9Enabled) + { + dynamic_cast(_backGroundImage)->setPreferredSize(_size); + } + } + if (_colorRender) + { + _colorRender->setContentSize(_size); + } + if (_gradientRender) + { + _gradientRender->setContentSize(_size); + } +} + +void UILayout::setBackGroundImageScale9Enabled(bool able) +{ + if (_backGroundScale9Enabled == able) + { + return; + } + _renderer->removeChild(_backGroundImage, true); + _backGroundImage = NULL; + _backGroundScale9Enabled = able; + if (_backGroundScale9Enabled) + { + _backGroundImage = cocos2d::extension::Scale9Sprite::create(); + _renderer->addChild(_backGroundImage); + } + else + { + _backGroundImage = cocos2d::Sprite::create(); + _renderer->addChild(_backGroundImage); + } + _backGroundImage->setZOrder(-1); + setBackGroundImage(_backGroundImageFileName.c_str(),_bgImageTexType); + setBackGroundImageCapInsets(_backGroundImageCapInsets); +} + +void UILayout::setBackGroundImage(const char* fileName,TextureResType texType) +{ + if (!fileName || strcmp(fileName, "") == 0) + { + return; + } + if (_backGroundImage == NULL) + { + addBackGroundImage(); + } + _backGroundImageFileName = fileName; + _bgImageTexType = texType; + if (_backGroundScale9Enabled) + { + switch (_bgImageTexType) + { + case UI_TEX_TYPE_LOCAL: + dynamic_cast(_backGroundImage)->initWithFile(fileName); + break; + case UI_TEX_TYPE_PLIST: + dynamic_cast(_backGroundImage)->initWithSpriteFrameName(fileName); + break; + default: + break; + } + dynamic_cast(_backGroundImage)->setPreferredSize(_size); + } + else + { + switch (_bgImageTexType) + { + case UI_TEX_TYPE_LOCAL: + dynamic_cast(_backGroundImage)->initWithFile(fileName); + break; + case UI_TEX_TYPE_PLIST: + dynamic_cast(_backGroundImage)->initWithSpriteFrameName(fileName); + break; + default: + break; + } + } + if (_backGroundScale9Enabled) + { + dynamic_cast(_backGroundImage)->setColor(getColor()); + dynamic_cast(_backGroundImage)->setOpacity(getOpacity()); + } + else + { + dynamic_cast(_backGroundImage)->setColor(getColor()); + dynamic_cast(_backGroundImage)->setOpacity(getOpacity()); + } + _backGroundImageTextureSize = _backGroundImage->getContentSize(); + _backGroundImage->setPosition(cocos2d::Point(_size.width/2.0f, _size.height/2.0f)); +} + +void UILayout::setBackGroundImageCapInsets(const cocos2d::Rect &capInsets) +{ + _backGroundImageCapInsets = capInsets; + if (_backGroundScale9Enabled) + { + dynamic_cast(_backGroundImage)->setCapInsets(capInsets); + } +} + +void UILayout::supplyTheLayoutParameterLackToChild(UIWidget *child) +{ + if (!child) + { + return; + } + switch (_layoutType) + { + case LAYOUT_ABSOLUTE: + break; + case LAYOUT_LINEAR_HORIZONTAL: + case LAYOUT_LINEAR_VERTICAL: + { + UILinearLayoutParameter* layoutParameter = dynamic_cast(child->getLayoutParameter(LAYOUT_PARAMETER_LINEAR)); + if (!layoutParameter) + { + child->setLayoutParameter(UILinearLayoutParameter::create()); + } + break; + } + case LAYOUT_RELATIVE: + { + UIRelativeLayoutParameter* layoutParameter = dynamic_cast(child->getLayoutParameter(LAYOUT_PARAMETER_RELATIVE)); + if (!layoutParameter) + { + child->setLayoutParameter(UIRelativeLayoutParameter::create()); + } + break; + } + default: + break; + } +} + +void UILayout::addBackGroundImage() +{ + if (_backGroundScale9Enabled) + { + _backGroundImage = cocos2d::extension::Scale9Sprite::create(); + _backGroundImage->setZOrder(-1); + _renderer->addChild(_backGroundImage); + dynamic_cast(_backGroundImage)->setPreferredSize(_size); + } + else + { + _backGroundImage = cocos2d::Sprite::create(); + _backGroundImage->setZOrder(-1); + _renderer->addChild(_backGroundImage); + } + _backGroundImage->setPosition(cocos2d::Point(_size.width/2.0f, _size.height/2.0f)); +} + +void UILayout::removeBackGroundImage() +{ + if (!_backGroundImage) + { + return; + } + _renderer->removeChild(_backGroundImage, true); + _backGroundImage = NULL; + _backGroundImageFileName = ""; + _backGroundImageTextureSize = cocos2d::Size::ZERO; +} + +void UILayout::setBackGroundColorType(LayoutBackGroundColorType type) +{ + if (_colorType == type) + { + return; + } + switch (_colorType) + { + case LAYOUT_COLOR_NONE: + if (_colorRender) + { + _renderer->removeChild(_colorRender, true); + _colorRender = NULL; + } + if (_gradientRender) + { + _renderer->removeChild(_gradientRender, true); + _gradientRender = NULL; + } + break; + case LAYOUT_COLOR_SOLID: + if (_colorRender) + { + _renderer->removeChild(_colorRender, true); + _colorRender = NULL; + } + break; + case LAYOUT_COLOR_GRADIENT: + if (_gradientRender) + { + _renderer->removeChild(_gradientRender, true); + _gradientRender = NULL; + } + break; + default: + break; + } + _colorType = type; + switch (_colorType) + { + case LAYOUT_COLOR_NONE: + break; + case LAYOUT_COLOR_SOLID: + _colorRender = cocos2d::LayerColor::create(); + _colorRender->setContentSize(_size); + _colorRender->setOpacity(_cOpacity); + _colorRender->setColor(_cColor); + _renderer->addChild(_colorRender,-2); + break; + case LAYOUT_COLOR_GRADIENT: + _gradientRender = cocos2d::LayerGradient::create(); + _gradientRender->setContentSize(_size); + _gradientRender->setOpacity(_cOpacity); + _gradientRender->setStartColor(_gStartColor); + _gradientRender->setEndColor(_gEndColor); + _gradientRender->setVector(_alongVector); + _renderer->addChild(_gradientRender,-2); + break; + default: + break; + } +} + +void UILayout::setBackGroundColor(const cocos2d::Color3B &color) +{ + _cColor = color; + if (_colorRender) + { + _colorRender->setColor(color); + } +} + +void UILayout::setBackGroundColor(const cocos2d::Color3B &startColor, const cocos2d::Color3B &endColor) +{ + _gStartColor = startColor; + if (_gradientRender) + { + _gradientRender->setStartColor(startColor); + } + _gEndColor = endColor; + if (_gradientRender) + { + _gradientRender->setEndColor(endColor); + } +} + +void UILayout::setBackGroundColorOpacity(int opacity) +{ + _cOpacity = opacity; + switch (_colorType) + { + case LAYOUT_COLOR_NONE: + break; + case LAYOUT_COLOR_SOLID: + _colorRender->setOpacity(opacity); + break; + case LAYOUT_COLOR_GRADIENT: + _gradientRender->setOpacity(opacity); + break; + default: + break; + } +} + +void UILayout::setBackGroundColorVector(const cocos2d::Point &vector) +{ + _alongVector = vector; + if (_gradientRender) + { + _gradientRender->setVector(vector); + } +} + +void UILayout::setColor(const cocos2d::Color3B &color) +{ + UIWidget::setColor(color); + if (_backGroundImage) + { + cocos2d::RGBAProtocol* rgbap = dynamic_cast(_backGroundImage); + if (rgbap) + { + rgbap->setColor(color); + } + } +} + +void UILayout::setOpacity(int opacity) +{ + UIWidget::setOpacity(opacity); + if (_backGroundImage) + { + cocos2d::RGBAProtocol* rgbap = dynamic_cast(_backGroundImage); + if (rgbap) + { + rgbap->setOpacity(opacity); + } + } +} + +const cocos2d::Size& UILayout::getBackGroundImageTextureSize() const +{ + return _backGroundImageTextureSize; +} + +const cocos2d::Size& UILayout::getContentSize() const +{ + return _renderer->getContentSize(); +} + +void UILayout::setLayoutType(LayoutType type) +{ + _layoutType = type; + + cocos2d::ccArray* layoutChildrenArray = getChildren()->data; + int length = layoutChildrenArray->num; + for (int i=0; i(layoutChildrenArray->arr[i]); + supplyTheLayoutParameterLackToChild(child); + } +} + +LayoutType UILayout::getLayoutType() const +{ + return _layoutType; +} + +void UILayout::doLayout() +{ + switch (_layoutType) + { + case LAYOUT_ABSOLUTE: + break; + case LAYOUT_LINEAR_VERTICAL: + { + cocos2d::ccArray* layoutChildrenArray = getChildren()->data; + int length = layoutChildrenArray->num; + cocos2d::Size layoutSize = getSize(); + float topBoundary = layoutSize.height; + for (int i=0; i(layoutChildrenArray->arr[i]); + UILinearLayoutParameter* layoutParameter = dynamic_cast(child->getLayoutParameter(LAYOUT_PARAMETER_LINEAR)); + + if (layoutParameter) + { + UILinearGravity childGravity = layoutParameter->getGravity(); + cocos2d::Point ap = child->getAnchorPoint(); + cocos2d::Size cs = child->getSize(); + float finalPosX = ap.x * cs.width; + float finalPosY = topBoundary - ((1.0f-ap.y) * cs.height); + switch (childGravity) + { + case LINEAR_GRAVITY_NONE: + case LINEAR_GRAVITY_LEFT: + break; + case LINEAR_GRAVITY_RIGHT: + finalPosX = layoutSize.width - ((1.0f - ap.x) * cs.width); + break; + case LINEAR_GRAVITY_CENTER_HORIZONTAL: + finalPosX = layoutSize.width / 2.0f - cs.width * (0.5f-ap.x); + break; + default: + break; + } + UIMargin mg = layoutParameter->getMargin(); + finalPosX += mg.left; + finalPosY -= mg.top; + child->setPosition(cocos2d::Point(finalPosX, finalPosY)); + topBoundary = child->getBottomInParent() - mg.bottom; + } + } + break; + } + case LAYOUT_LINEAR_HORIZONTAL: + { + cocos2d::ccArray* layoutChildrenArray = getChildren()->data; + int length = layoutChildrenArray->num; + cocos2d::Size layoutSize = getSize(); + float leftBoundary = 0.0f; + for (int i=0; i(layoutChildrenArray->arr[i]); + UILinearLayoutParameter* layoutParameter = dynamic_cast(child->getLayoutParameter(LAYOUT_PARAMETER_LINEAR)); + + if (layoutParameter) + { + UILinearGravity childGravity = layoutParameter->getGravity(); + cocos2d::Point ap = child->getAnchorPoint(); + cocos2d::Size cs = child->getSize(); + float finalPosX = leftBoundary + (ap.x * cs.width); + float finalPosY = layoutSize.height - (1.0f - ap.y) * cs.height; + switch (childGravity) + { + case LINEAR_GRAVITY_NONE: + case LINEAR_GRAVITY_TOP: + break; + case LINEAR_GRAVITY_BOTTOM: + finalPosY = ap.y * cs.height; + break; + case LINEAR_GRAVITY_CENTER_VERTICAL: + finalPosY = layoutSize.height / 2.0f - cs.height * (0.5f - ap.y); + break; + default: + break; + } + UIMargin mg = layoutParameter->getMargin(); + finalPosX += mg.left; + finalPosY -= mg.top; + child->setPosition(cocos2d::Point(finalPosX, finalPosY)); + leftBoundary = child->getRightInParent() + mg.right; + } + } + break; + } + case LAYOUT_RELATIVE: + { + cocos2d::ccArray* layoutChildrenArray = getChildren()->data; + int length = layoutChildrenArray->num; + int unlayoutChildCount = length; + cocos2d::Size layoutSize = getSize(); + + for (int i=0; i(layoutChildrenArray->arr[i]); + UIRelativeLayoutParameter* layoutParameter = dynamic_cast(child->getLayoutParameter(LAYOUT_PARAMETER_RELATIVE)); + layoutParameter->_put = false; + } + + while (unlayoutChildCount > 0) + { + for (int i=0; i(layoutChildrenArray->arr[i]); + UIRelativeLayoutParameter* layoutParameter = dynamic_cast(child->getLayoutParameter(LAYOUT_PARAMETER_RELATIVE)); + + if (layoutParameter) + { + if (layoutParameter->_put) + { + continue; + } + cocos2d::Point ap = child->getAnchorPoint(); + cocos2d::Size cs = child->getSize(); + UIRelativeAlign align = layoutParameter->getAlign(); + const char* relativeName = layoutParameter->getRelativeToWidgetName(); + UIWidget* relativeWidget = NULL; + UIRelativeLayoutParameter* relativeWidgetLP = NULL; + float finalPosX = 0.0f; + float finalPosY = 0.0f; + if (relativeName && strcmp(relativeName, "")) + { + relativeWidget = UIHelper::seekWidgetByRelativeName(this, relativeName); + if (relativeWidget) + { + relativeWidgetLP = dynamic_cast(relativeWidget->getLayoutParameter(LAYOUT_PARAMETER_RELATIVE)); + } + } + switch (align) + { + case RELATIVE_ALIGN_NONE: + case RELATIVE_ALIGN_PARENT_TOP_LEFT: + finalPosX = ap.x * cs.width; + finalPosY = layoutSize.height - ((1.0f - ap.y) * cs.height); + break; + case RELATIVE_ALIGN_PARENT_TOP_CENTER_HORIZONTAL: + finalPosX = layoutSize.width * 0.5f - cs.width * (0.5f - ap.x); + finalPosY = layoutSize.height - ((1.0f - ap.y) * cs.height); + break; + case RELATIVE_ALIGN_PARENT_TOP_RIGHT: + finalPosX = layoutSize.width - ((1.0f - ap.x) * cs.width); + finalPosY = layoutSize.height - ((1.0f - ap.y) * cs.height); + break; + case RELATIVE_ALIGN_PARENT_LEFT_CENTER_VERTICAL: + finalPosX = ap.x * cs.width; + finalPosY = layoutSize.height * 0.5f - cs.height * (0.5f - ap.y); + break; + case RELATIVE_CENTER_IN_PARENT: + finalPosX = layoutSize.width * 0.5f - cs.width * (0.5f - ap.x); + finalPosY = layoutSize.height * 0.5f - cs.height * (0.5f - ap.y); + break; + case RELATIVE_ALIGN_PARENT_RIGHT_CENTER_VERTICAL: + finalPosX = layoutSize.width - ((1.0f - ap.x) * cs.width); + finalPosY = layoutSize.height * 0.5f - cs.height * (0.5f - ap.y); + break; + case RELATIVE_ALIGN_PARENT_LEFT_BOTTOM: + finalPosX = ap.x * cs.width; + finalPosY = ap.y * cs.height; + break; + case RELATIVE_ALIGN_PARENT_BOTTOM_CENTER_HORIZONTAL: + finalPosX = layoutSize.width * 0.5f - cs.width * (0.5f - ap.x); + finalPosY = ap.y * cs.height; + break; + case RELATIVE_ALIGN_PARENT_RIGHT_BOTTOM: + finalPosX = layoutSize.width - ((1.0f - ap.x) * cs.width); + finalPosY = ap.y * cs.height; + break; + + case RELATIVE_LOCATION_ABOVE_LEFTALIGN: + if (relativeWidget) + { + if (relativeWidgetLP && !relativeWidgetLP->_put) + { + continue; + } + float locationBottom = relativeWidget->getTopInParent(); + float locationLeft = relativeWidget->getLeftInParent(); + finalPosY = locationBottom + ap.y * cs.height; + finalPosX = locationLeft + ap.x * cs.width; + } + break; + case RELATIVE_LOCATION_ABOVE_CENTER: + if (relativeWidget) + { + if (relativeWidgetLP && !relativeWidgetLP->_put) + { + continue; + } + cocos2d::Size rbs = relativeWidget->getSize(); + float locationBottom = relativeWidget->getTopInParent(); + + finalPosY = locationBottom + ap.y * cs.height; + finalPosX = relativeWidget->getLeftInParent() + rbs.width * 0.5f + ap.x * cs.width - cs.width * 0.5f; + } + break; + case RELATIVE_LOCATION_ABOVE_RIGHTALIGN: + if (relativeWidget) + { + if (relativeWidgetLP && !relativeWidgetLP->_put) + { + continue; + } + float locationBottom = relativeWidget->getTopInParent(); + float locationRight = relativeWidget->getRightInParent(); + finalPosY = locationBottom + ap.y * cs.height; + finalPosX = locationRight - (1.0f - ap.x) * cs.width; + } + break; + case RELATIVE_LOCATION_LEFT_OF_TOPALIGN: + if (relativeWidget) + { + if (relativeWidgetLP && !relativeWidgetLP->_put) + { + continue; + } + float locationTop = relativeWidget->getTopInParent(); + float locationRight = relativeWidget->getLeftInParent(); + finalPosY = locationTop - (1.0f - ap.y) * cs.height; + finalPosX = locationRight - (1.0f - ap.x) * cs.width; + } + break; + case RELATIVE_LOCATION_LEFT_OF_CENTER: + if (relativeWidget) + { + if (relativeWidgetLP && !relativeWidgetLP->_put) + { + continue; + } + cocos2d::Size rbs = relativeWidget->getSize(); + float locationRight = relativeWidget->getLeftInParent(); + finalPosX = locationRight - (1.0f - ap.x) * cs.width; + + finalPosY = relativeWidget->getBottomInParent() + rbs.height * 0.5f + ap.y * cs.height - cs.height * 0.5f; + } + break; + case RELATIVE_LOCATION_LEFT_OF_BOTTOMALIGN: + if (relativeWidget) + { + if (relativeWidgetLP && !relativeWidgetLP->_put) + { + continue; + } + float locationBottom = relativeWidget->getBottomInParent(); + float locationRight = relativeWidget->getLeftInParent(); + finalPosY = locationBottom + ap.y * cs.height; + finalPosX = locationRight - (1.0f - ap.x) * cs.width; + } + break; + case RELATIVE_LOCATION_RIGHT_OF_TOPALIGN: + if (relativeWidget) + { + if (relativeWidgetLP && !relativeWidgetLP->_put) + { + continue; + } + float locationTop = relativeWidget->getTopInParent(); + float locationLeft = relativeWidget->getRightInParent(); + finalPosY = locationTop - (1.0f - ap.y) * cs.height; + finalPosX = locationLeft + ap.x * cs.width; + } + break; + case RELATIVE_LOCATION_RIGHT_OF_CENTER: + if (relativeWidget) + { + if (relativeWidgetLP && !relativeWidgetLP->_put) + { + continue; + } + cocos2d::Size rbs = relativeWidget->getSize(); + float locationLeft = relativeWidget->getRightInParent(); + finalPosX = locationLeft + ap.x * cs.width; + + finalPosY = relativeWidget->getBottomInParent() + rbs.height * 0.5f + ap.y * cs.height - cs.height * 0.5f; + } + break; + case RELATIVE_LOCATION_RIGHT_OF_BOTTOMALIGN: + if (relativeWidget) + { + if (relativeWidgetLP && !relativeWidgetLP->_put) + { + continue; + } + float locationBottom = relativeWidget->getBottomInParent(); + float locationLeft = relativeWidget->getRightInParent(); + finalPosY = locationBottom + ap.y * cs.height; + finalPosX = locationLeft + ap.x * cs.width; + } + break; + case RELATIVE_LOCATION_BELOW_LEFTALIGN: + if (relativeWidget) + { + if (relativeWidgetLP && !relativeWidgetLP->_put) + { + continue; + } + float locationTop = relativeWidget->getBottomInParent(); + float locationLeft = relativeWidget->getLeftInParent(); + finalPosY = locationTop - (1.0f - ap.y) * cs.height; + finalPosX = locationLeft + ap.x * cs.width; + } + break; + case RELATIVE_LOCATION_BELOW_CENTER: + if (relativeWidget) + { + if (relativeWidgetLP && !relativeWidgetLP->_put) + { + continue; + } + cocos2d::Size rbs = relativeWidget->getSize(); + float locationTop = relativeWidget->getBottomInParent(); + + finalPosY = locationTop - (1.0f - ap.y) * cs.height; + finalPosX = relativeWidget->getLeftInParent() + rbs.width * 0.5f + ap.x * cs.width - cs.width * 0.5f; + } + break; + case RELATIVE_LOCATION_BELOW_RIGHTALIGN: + if (relativeWidget) + { + if (relativeWidgetLP && !relativeWidgetLP->_put) + { + continue; + } + float locationTop = relativeWidget->getBottomInParent(); + float locationRight = relativeWidget->getRightInParent(); + finalPosY = locationTop - (1.0f - ap.y) * cs.height; + finalPosX = locationRight - (1.0f - ap.x) * cs.width; + } + break; + default: + break; + } + UIMargin relativeWidgetMargin; + UIMargin mg = layoutParameter->getMargin(); + if (relativeWidget) + { + relativeWidgetMargin = relativeWidget->getLayoutParameter(LAYOUT_PARAMETER_RELATIVE)->getMargin(); + } + //handle margin + switch (align) + { + case RELATIVE_ALIGN_NONE: + case RELATIVE_ALIGN_PARENT_TOP_LEFT: + finalPosX += mg.left; + finalPosY -= mg.top; + break; + case RELATIVE_ALIGN_PARENT_TOP_CENTER_HORIZONTAL: + finalPosY -= mg.top; + break; + case RELATIVE_ALIGN_PARENT_TOP_RIGHT: + finalPosX -= mg.right; + finalPosY -= mg.top; + break; + case RELATIVE_ALIGN_PARENT_LEFT_CENTER_VERTICAL: + finalPosX += mg.left; + break; + case RELATIVE_CENTER_IN_PARENT: + break; + case RELATIVE_ALIGN_PARENT_RIGHT_CENTER_VERTICAL: + finalPosX -= mg.right; + break; + case RELATIVE_ALIGN_PARENT_LEFT_BOTTOM: + finalPosX += mg.left; + finalPosY += mg.bottom; + break; + case RELATIVE_ALIGN_PARENT_BOTTOM_CENTER_HORIZONTAL: + finalPosY += mg.bottom; + break; + case RELATIVE_ALIGN_PARENT_RIGHT_BOTTOM: + finalPosX -= mg.right; + finalPosY += mg.bottom; + break; + + case RELATIVE_LOCATION_ABOVE_LEFTALIGN: + case RELATIVE_LOCATION_ABOVE_CENTER: + case RELATIVE_LOCATION_ABOVE_RIGHTALIGN: + finalPosY += mg.bottom; + finalPosY += relativeWidgetMargin.top; + break; + case RELATIVE_LOCATION_LEFT_OF_TOPALIGN: + case RELATIVE_LOCATION_LEFT_OF_CENTER: + case RELATIVE_LOCATION_LEFT_OF_BOTTOMALIGN: + finalPosX -= mg.right; + finalPosX -= relativeWidgetMargin.left; + break; + case RELATIVE_LOCATION_RIGHT_OF_TOPALIGN: + case RELATIVE_LOCATION_RIGHT_OF_CENTER: + case RELATIVE_LOCATION_RIGHT_OF_BOTTOMALIGN: + finalPosX += mg.left; + finalPosX += relativeWidgetMargin.right; + break; + case RELATIVE_LOCATION_BELOW_LEFTALIGN: + case RELATIVE_LOCATION_BELOW_CENTER: + case RELATIVE_LOCATION_BELOW_RIGHTALIGN: + finalPosY -= mg.top; + finalPosY -= relativeWidgetMargin.bottom; + break; + default: + break; + } + child->setPosition(cocos2d::Point(finalPosX, finalPosY)); + layoutParameter->_put = true; + unlayoutChildCount--; + } + } + } + break; + } + default: + break; + } +} + +const char* UILayout::getDescription() const +{ + return "Layout"; +} + +UIWidget* UILayout::createCloneInstance() +{ + return UILayout::create(); +} + +void UILayout::copyClonedWidgetChildren(UIWidget* model) +{ + UIWidget::copyClonedWidgetChildren(model); + doLayout(); +} + +void UILayout::copySpecialProperties(UIWidget *widget) +{ + UILayout* layout = dynamic_cast(widget); + if (layout) + { + setBackGroundImageScale9Enabled(layout->_backGroundScale9Enabled); + setBackGroundImage(layout->_backGroundImageFileName.c_str(),layout->_bgImageTexType); + setBackGroundImageCapInsets(layout->_backGroundImageCapInsets); + setBackGroundColorType(layout->_colorType); + setBackGroundColor(layout->_cColor); + setBackGroundColor(layout->_gStartColor, layout->_gEndColor); + setBackGroundColorOpacity(layout->_cOpacity); + setBackGroundColorVector(layout->_alongVector); + setLayoutType(layout->_layoutType); + setClippingEnabled(layout->_clippingEnabled); + } +} + +UIRectClippingNode::UIRectClippingNode(): +_innerStencil(NULL), +_enabled(true), +_clippingSize(cocos2d::Size(50.0f, 50.0f)), +_clippingEnabled(false) +{ + +} + +UIRectClippingNode::~UIRectClippingNode() +{ + +} + +UIRectClippingNode* UIRectClippingNode::create() +{ + UIRectClippingNode *pRet = new UIRectClippingNode(); + if (pRet && pRet->init()) + { + pRet->autorelease(); + } + else + { + CC_SAFE_DELETE(pRet); + } + + return pRet; +} + +bool UIRectClippingNode::init() +{ + _innerStencil = cocos2d::DrawNode::create(); + rect[0] = cocos2d::Point(0, 0); + rect[1] = cocos2d::Point(_clippingSize.width, 0); + rect[2] = cocos2d::Point(_clippingSize.width, _clippingSize.height); + rect[3] = cocos2d::Point(0, _clippingSize.height); + + cocos2d::Color4F green = {0, 1, 0, 1}; + _innerStencil->drawPolygon(rect, 4, green, 0, green); + if (cocos2d::ClippingNode::init(_innerStencil)) + { + return true; + } + return false; +} + + +void UIRectClippingNode::setClippingSize(const cocos2d::Size &size) +{ + setContentSize(size); + _clippingSize = size; + rect[0] = cocos2d::Point(0, 0); + rect[1] = cocos2d::Point(_clippingSize.width, 0); + rect[2] = cocos2d::Point(_clippingSize.width, _clippingSize.height); + rect[3] = cocos2d::Point(0, _clippingSize.height); + cocos2d::Color4F green = {0, 1, 0, 1}; + _innerStencil->clear(); + _innerStencil->drawPolygon(rect, 4, green, 0, green); +} + +void UIRectClippingNode::setClippingEnabled(bool enabled) +{ + _clippingEnabled = enabled; +} + +void UIRectClippingNode::visit() +{ + if (!_enabled) + { + return; + } + if (_clippingEnabled) + { + cocos2d::ClippingNode::visit(); + } + else + { + cocos2d::Node::visit(); + } +} + +void UIRectClippingNode::setEnabled(bool enabled) +{ + _enabled = enabled; +} + +bool UIRectClippingNode::isEnabled() const +{ + return _enabled; +} + +} \ No newline at end of file diff --git a/cocos/gui/Layout.h b/cocos/gui/UILayout.h similarity index 89% rename from cocos/gui/Layout.h rename to cocos/gui/UILayout.h index b58b0fbd2b..918bd868af 100644 --- a/cocos/gui/Layout.h +++ b/cocos/gui/UILayout.h @@ -44,23 +44,28 @@ typedef enum LAYOUT_RELATIVE }LayoutType; -class Layout : public UIWidget + +/** + * @js NA + * @lua NA + */ +class UILayout : public UIWidget { public: /** * Default constructor */ - Layout(); + UILayout(); /** * Default destructor */ - virtual ~Layout(); + virtual ~UILayout(); /** * Allocates and initializes a layout. */ - static Layout* create(); + static UILayout* create(); //override "hitTest" method of widget. virtual bool hitTest(const cocos2d::Point &pt); @@ -168,6 +173,11 @@ public: */ virtual const cocos2d::Size& getContentSize() const; + /** + * Returns the "class name" of widget. + */ + virtual const char* getDescription() const; + /** * Sets LayoutType. * @@ -187,11 +197,14 @@ public: virtual LayoutType getLayoutType() const; virtual void doLayout(); - + /** - * Returns the "class name" of widget. + * Adds a child to the container. + * + * @param child A child widget */ - virtual const char* getDescription() const; + virtual bool addChild(UIWidget* child); + protected: //override "init" method of widget. virtual bool init(); @@ -204,6 +217,11 @@ protected: //init background image renderer. void addBackGroundImage(); + + void supplyTheLayoutParameterLackToChild(UIWidget* child); + virtual UIWidget* createCloneInstance(); + virtual void copySpecialProperties(UIWidget* model); + virtual void copyClonedWidgetChildren(UIWidget* model); protected: bool _clippingEnabled; @@ -224,23 +242,26 @@ protected: cocos2d::Size _backGroundImageTextureSize; LayoutType _layoutType; }; - -class RectClippingNode : public cocos2d::ClippingNode +/** + * @js NA + * @lua NA + */ +class UIRectClippingNode : public cocos2d::ClippingNode { public: - virtual ~RectClippingNode(); + virtual ~UIRectClippingNode(); virtual bool init(); - static RectClippingNode* create(); + static UIRectClippingNode* create(); void setClippingSize(const cocos2d::Size& size); void setClippingEnabled(bool enabled); virtual void visit(); void setEnabled(bool enabled); bool isEnabled() const; protected: - cocos2d::DrawNode* m_pInnerStencil; + cocos2d::DrawNode* _innerStencil; bool _enabled; private: - RectClippingNode(); + UIRectClippingNode(); cocos2d::Point rect[4]; cocos2d::Size _clippingSize; bool _clippingEnabled; diff --git a/cocos/gui/UILayoutDefine.cpp b/cocos/gui/UILayoutDefine.cpp index d79444fc75..b6b785a47d 100644 --- a/cocos/gui/UILayoutDefine.cpp +++ b/cocos/gui/UILayoutDefine.cpp @@ -21,7 +21,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ -#include "gui/UILayoutDefine.h" +#include "UILayoutDefine.h" namespace gui { diff --git a/cocos/gui/UILayoutDefine.h b/cocos/gui/UILayoutDefine.h index 1d0bd716bf..0b772837ca 100644 --- a/cocos/gui/UILayoutDefine.h +++ b/cocos/gui/UILayoutDefine.h @@ -26,9 +26,13 @@ #define __UILAYOUTDEFINE_H__ #include "cocos2d.h" +#include "ExtensionMacros.h" namespace gui { - +/** +* @js NA +* @lua NA +*/ class UIMargin { public: @@ -46,6 +50,7 @@ public: bool equals(const UIMargin& target) const; }; +const UIMargin UIMarginZero = UIMargin(); typedef enum { @@ -61,22 +66,25 @@ typedef enum typedef enum { RELATIVE_ALIGN_NONE, - RELATIVE_ALIGN_PARENT_LEFT, - RELATIVE_ALIGN_PARENT_TOP, - RELATIVE_ALIGN_PARENT_RIGHT, - RELATIVE_ALIGN_PARENT_BOTTOM, + RELATIVE_ALIGN_PARENT_TOP_LEFT, + RELATIVE_ALIGN_PARENT_TOP_CENTER_HORIZONTAL, + RELATIVE_ALIGN_PARENT_TOP_RIGHT, + RELATIVE_ALIGN_PARENT_LEFT_CENTER_VERTICAL, RELATIVE_CENTER_IN_PARENT, - RELATIVE_CENTER_HORIZONTAL, - RELATIVE_CENTER_VERTICAL, + RELATIVE_ALIGN_PARENT_RIGHT_CENTER_VERTICAL, + RELATIVE_ALIGN_PARENT_LEFT_BOTTOM, + RELATIVE_ALIGN_PARENT_BOTTOM_CENTER_HORIZONTAL, + RELATIVE_ALIGN_PARENT_RIGHT_BOTTOM, + + RELATIVE_LOCATION_ABOVE_LEFTALIGN, + RELATIVE_LOCATION_ABOVE_CENTER, + RELATIVE_LOCATION_ABOVE_RIGHTALIGN, RELATIVE_LOCATION_LEFT_OF_TOPALIGN, RELATIVE_LOCATION_LEFT_OF_CENTER, RELATIVE_LOCATION_LEFT_OF_BOTTOMALIGN, RELATIVE_LOCATION_RIGHT_OF_TOPALIGN, RELATIVE_LOCATION_RIGHT_OF_CENTER, RELATIVE_LOCATION_RIGHT_OF_BOTTOMALIGN, - RELATIVE_LOCATION_ABOVE_LEFTALIGN, - RELATIVE_LOCATION_ABOVE_CENTER, - RELATIVE_LOCATION_ABOVE_RIGHTALIGN, RELATIVE_LOCATION_BELOW_LEFTALIGN, RELATIVE_LOCATION_BELOW_CENTER, RELATIVE_LOCATION_BELOW_RIGHTALIGN diff --git a/cocos/gui/LayoutParameter.cpp b/cocos/gui/UILayoutParameter.cpp similarity index 65% rename from cocos/gui/LayoutParameter.cpp rename to cocos/gui/UILayoutParameter.cpp index 01caa33158..c468f69a24 100644 --- a/cocos/gui/LayoutParameter.cpp +++ b/cocos/gui/UILayoutParameter.cpp @@ -22,14 +22,15 @@ THE SOFTWARE. ****************************************************************************/ -#include "gui/LayoutParameter.h" -#include "gui/Layout.h" +#include "gui/UILayoutParameter.h" +#include "gui/UILayout.h" namespace gui { -LayoutParameter* LayoutParameter::create() + +UILayoutParameter* UILayoutParameter::create() { - LayoutParameter* parameter = new LayoutParameter(); + UILayoutParameter* parameter = new UILayoutParameter(); if (parameter) { parameter->autorelease(); @@ -39,24 +40,24 @@ LayoutParameter* LayoutParameter::create() return NULL; } -void LayoutParameter::setMargin(const UIMargin &margin) +void UILayoutParameter::setMargin(const UIMargin &margin) { _margin = margin; } -const UIMargin& LayoutParameter::getMargin() const +const UIMargin& UILayoutParameter::getMargin() const { return _margin; } -LayoutParameterType LayoutParameter::getLayoutType() const +LayoutParameterType UILayoutParameter::getLayoutType() const { return _layoutParameterType; } -LinearLayoutParameter* LinearLayoutParameter::create() +UILinearLayoutParameter* UILinearLayoutParameter::create() { - LinearLayoutParameter* parameter = new LinearLayoutParameter(); + UILinearLayoutParameter* parameter = new UILinearLayoutParameter(); if (parameter) { parameter->autorelease(); @@ -66,19 +67,19 @@ LinearLayoutParameter* LinearLayoutParameter::create() return NULL; } -void LinearLayoutParameter::setGravity(UILinearGravity gravity) +void UILinearLayoutParameter::setGravity(UILinearGravity gravity) { _linearGravity = gravity; } -UILinearGravity LinearLayoutParameter::getGravity() const +UILinearGravity UILinearLayoutParameter::getGravity() const { return _linearGravity; } -RelativeLayoutParameter* RelativeLayoutParameter::create() +UIRelativeLayoutParameter* UIRelativeLayoutParameter::create() { - RelativeLayoutParameter* parameter = new RelativeLayoutParameter(); + UIRelativeLayoutParameter* parameter = new UIRelativeLayoutParameter(); if (parameter) { parameter->autorelease(); @@ -88,32 +89,32 @@ RelativeLayoutParameter* RelativeLayoutParameter::create() return NULL; } -void RelativeLayoutParameter::setAlign(UIRelativeAlign align) +void UIRelativeLayoutParameter::setAlign(UIRelativeAlign align) { _relativeAlign = align; } -UIRelativeAlign RelativeLayoutParameter::getAlign() const +UIRelativeAlign UIRelativeLayoutParameter::getAlign() const { return _relativeAlign; } -void RelativeLayoutParameter::setRelativeToWidgetName(const char *name) +void UIRelativeLayoutParameter::setRelativeToWidgetName(const char *name) { _relativeWidgetName = name; } -const char* RelativeLayoutParameter::getRelativeToWidgetName() const +const char* UIRelativeLayoutParameter::getRelativeToWidgetName() const { return _relativeWidgetName.c_str(); } -void RelativeLayoutParameter::setRelativeName(const char* name) +void UIRelativeLayoutParameter::setRelativeName(const char* name) { _relativeLayoutName = name; } -const char* RelativeLayoutParameter::getRelativeName() const +const char* UIRelativeLayoutParameter::getRelativeName() const { return _relativeLayoutName.c_str(); } diff --git a/cocos/gui/LayoutParameter.h b/cocos/gui/UILayoutParameter.h similarity index 82% rename from cocos/gui/LayoutParameter.h rename to cocos/gui/UILayoutParameter.h index 3c457635d1..60a7e387ad 100644 --- a/cocos/gui/LayoutParameter.h +++ b/cocos/gui/UILayoutParameter.h @@ -35,25 +35,28 @@ typedef enum LAYOUT_PARAMETER_LINEAR, LAYOUT_PARAMETER_RELATIVE }LayoutParameterType; - -class LayoutParameter : public cocos2d::Object +/** +* @js NA +* @lua NA +*/ +class UILayoutParameter : public cocos2d::Object { public: /** * Default constructor */ - LayoutParameter() : _margin(UIMargin()){_layoutParameterType = LAYOUT_PARAMETER_NONE;}; + UILayoutParameter() : _margin(UIMargin()){_layoutParameterType = LAYOUT_PARAMETER_NONE;}; /** * Default destructor */ - virtual ~LayoutParameter(){}; + virtual ~UILayoutParameter(){}; /** * Allocates and initializes. * @return A initialized LayoutParameter which is marked as "autorelease". */ - static LayoutParameter* create(); + static UILayoutParameter* create(); /** * Sets Margin parameter for LayoutParameter. @@ -85,25 +88,28 @@ protected: UIMargin _margin; LayoutParameterType _layoutParameterType; }; - -class LinearLayoutParameter : public LayoutParameter +/** +* @js NA +* @lua NA +*/ +class UILinearLayoutParameter : public UILayoutParameter { public: /** * Default constructor */ - LinearLayoutParameter() : _linearGravity(LINEAR_GRAVITY_NONE){_layoutParameterType = LAYOUT_PARAMETER_LINEAR;}; + UILinearLayoutParameter() : _linearGravity(LINEAR_GRAVITY_NONE){_layoutParameterType = LAYOUT_PARAMETER_LINEAR;}; /** * Default destructor */ - virtual ~LinearLayoutParameter(){}; + virtual ~UILinearLayoutParameter(){}; /** * Allocates and initializes. * @return A initialized LayoutParameter which is marked as "autorelease". */ - static LinearLayoutParameter* create(); + static UILinearLayoutParameter* create(); /** * Sets UILinearGravity parameter for LayoutParameter. @@ -125,25 +131,31 @@ public: protected: UILinearGravity _linearGravity; }; +/** +* @js NA +* @lua NA +*/ -class RelativeLayoutParameter : public LayoutParameter +class UILayout; + +class UIRelativeLayoutParameter : public UILayoutParameter { public: /** * Default constructor */ - RelativeLayoutParameter() : _relativeAlign(RELATIVE_ALIGN_NONE),_relativeWidgetName(""),_relativeLayoutName(""){_layoutParameterType = LAYOUT_PARAMETER_RELATIVE;}; + UIRelativeLayoutParameter() : _relativeAlign(RELATIVE_ALIGN_NONE),_relativeWidgetName(""),_relativeLayoutName(""),_put(false){_layoutParameterType = LAYOUT_PARAMETER_RELATIVE;}; /** * Default destructor */ - virtual ~RelativeLayoutParameter(){}; + virtual ~UIRelativeLayoutParameter(){}; /** * Allocates and initializes. * @return A initialized LayoutParameter which is marked as "autorelease". */ - static RelativeLayoutParameter* create(); + static UIRelativeLayoutParameter* create(); /** * Sets UIRelativeAlign parameter for LayoutParameter. @@ -194,6 +206,8 @@ protected: UIRelativeAlign _relativeAlign; std::string _relativeWidgetName; std::string _relativeLayoutName; + bool _put; + friend class UILayout; }; } diff --git a/cocos/gui/UIListView.cpp b/cocos/gui/UIListView.cpp index 5ec6e71540..81baaad41d 100644 --- a/cocos/gui/UIListView.cpp +++ b/cocos/gui/UIListView.cpp @@ -23,55 +23,24 @@ ****************************************************************************/ #include "gui/UIListView.h" - - using namespace cocos2d; +#include "gui/UIHelper.h" +#include "extensions/GUI/CCControlExtension/CCScale9Sprite.h" namespace gui { -UIListView::UIListView() -: _direction(LISTVIEW_DIR_VERTICAL) -, _moveDirection(LISTVIEW_MOVE_DIR_NONE) -, _touchStartLocation(0.0f) -, _touchEndLocation(0.0f) -, _touchMoveStartLocation(0.0f) -, _topBoundary(0.0f) -, _bottomBoundary(0.0f) -, _leftBoundary(0.0f) -, _rightBoundary(0.0f) -, _autoScroll(false) -, _autoScrollOriginalSpeed(0.0f) -, _autoScrollAcceleration(600.0f) -, _bePressed(false) -, _slidTime(0.0f) -, _childFocusCancelOffset(5.0f) -, _eventListener(NULL) -, _eventSelector(NULL) -, _childPool(NULL) -, _updatePool(NULL) -, _dataLength(0) -, _begin(0) -, _end(0) -, _updateChild(NULL) -, _updateDataIndex(-1) -, _updateSuccess(false) -, _overTopArray(NULL) -, _overBottomArray(NULL) -, _overLeftArray(NULL) -, _overRightArray(NULL) -, _disBoundaryToChild_0(0.0f) -, _disBetweenChild(0.0f) -, _scrollDegreeRange(45.0f) +UIListView::UIListView(): +_model(NULL), +_items(NULL), +_gravity(LISTVIEW_GRAVITY_CENTER_HORIZONTAL), +_itemsMargin(0.0f) { + } UIListView::~UIListView() { - CC_SAFE_RELEASE_NULL(_childPool); - CC_SAFE_RELEASE_NULL(_updatePool); - CC_SAFE_RELEASE_NULL(_overTopArray); - CC_SAFE_RELEASE_NULL(_overBottomArray); - CC_SAFE_RELEASE_NULL(_overLeftArray); - CC_SAFE_RELEASE_NULL(_overRightArray); + _items->removeAllObjects(); + CC_SAFE_RELEASE(_items); } UIListView* UIListView::create() @@ -88,1373 +57,365 @@ UIListView* UIListView::create() bool UIListView::init() { - if (Layout::init()) + if (UIScrollView::init()) { - setUpdateEnabled(true); - setTouchEnabled(true); - setClippingEnabled(true); - - _childPool = CCArray::create(); - _updatePool = CCArray::create(); - CC_SAFE_RETAIN(_childPool); - CC_SAFE_RETAIN(_updatePool); - _overTopArray = cocos2d::CCArray::create(); - _overBottomArray = cocos2d::CCArray::create(); - _overLeftArray = cocos2d::CCArray::create(); - _overRightArray = cocos2d::CCArray::create(); - CC_SAFE_RETAIN(_overTopArray); - CC_SAFE_RETAIN(_overBottomArray); - CC_SAFE_RETAIN(_overLeftArray); - CC_SAFE_RETAIN(_overRightArray); - + _items = cocos2d::Array::create(); + CC_SAFE_RETAIN(_items); + setLayoutType(LAYOUT_LINEAR_VERTICAL); return true; } return false; } +void UIListView::setItemModel(UIWidget *model) +{ + if (!model) + { + return; + } + CC_SAFE_RELEASE_NULL(_model); + _model = model; + CC_SAFE_RETAIN(_model); +} + +void UIListView::updateInnerContainerSize() +{ + if (!_model) + { + return; + } + switch (_direction) { + case SCROLLVIEW_DIR_VERTICAL: + { + int childrenCount = _items->count(); + float totalHeight = _model->getSize().height * childrenCount + (childrenCount - 1) * _itemsMargin; + float finalWidth = _size.width; + float finalHeight = totalHeight; + setInnerContainerSize(cocos2d::Size(finalWidth, finalHeight)); + break; + } + case SCROLLVIEW_DIR_HORIZONTAL: + { + int childrenCount = _items->count(); + float totalWidth = _model->getSize().width * childrenCount + (childrenCount - 1) * _itemsMargin; + float finalWidth = totalWidth; + float finalHeight = _size.height; + setInnerContainerSize(cocos2d::Size(finalWidth, finalHeight)); + break; + } + default: + break; + } +} + +void UIListView::remedyLayoutParameter(UIWidget *item) +{ + if (!item) + { + return; + } + switch (_direction) { + case SCROLLVIEW_DIR_VERTICAL: + { + UILinearLayoutParameter* llp = (UILinearLayoutParameter*)(item->getLayoutParameter(LAYOUT_PARAMETER_LINEAR)); + if (!llp) + { + UILinearLayoutParameter* defaultLp = UILinearLayoutParameter::create(); + switch (_gravity) { + case LISTVIEW_GRAVITY_LEFT: + defaultLp->setGravity(LINEAR_GRAVITY_LEFT); + break; + case LISTVIEW_GRAVITY_RIGHT: + defaultLp->setGravity(LINEAR_GRAVITY_RIGHT); + break; + case LISTVIEW_GRAVITY_CENTER_HORIZONTAL: + defaultLp->setGravity(LINEAR_GRAVITY_CENTER_HORIZONTAL); + break; + default: + break; + } + if (getIndex(item) == 0) + { + defaultLp->setMargin(UIMarginZero); + } + else + { + defaultLp->setMargin(UIMargin(0.0f, _itemsMargin, 0.0f, 0.0f)); + } + item->setLayoutParameter(defaultLp); + } + else + { + if (getIndex(item) == 0) + { + llp->setMargin(UIMarginZero); + } + else + { + llp->setMargin(UIMargin(0.0f, _itemsMargin, 0.0f, 0.0f)); + } + switch (_gravity) { + case LISTVIEW_GRAVITY_LEFT: + llp->setGravity(LINEAR_GRAVITY_LEFT); + break; + case LISTVIEW_GRAVITY_RIGHT: + llp->setGravity(LINEAR_GRAVITY_RIGHT); + break; + case LISTVIEW_GRAVITY_CENTER_HORIZONTAL: + llp->setGravity(LINEAR_GRAVITY_CENTER_HORIZONTAL); + break; + default: + break; + } + } + break; + } + case SCROLLVIEW_DIR_HORIZONTAL: + { + UILinearLayoutParameter* llp = (UILinearLayoutParameter*)(item->getLayoutParameter(LAYOUT_PARAMETER_LINEAR)); + if (!llp) + { + UILinearLayoutParameter* defaultLp = UILinearLayoutParameter::create(); + switch (_gravity) { + case LISTVIEW_GRAVITY_TOP: + defaultLp->setGravity(LINEAR_GRAVITY_TOP); + break; + case LISTVIEW_GRAVITY_BOTTOM: + defaultLp->setGravity(LINEAR_GRAVITY_BOTTOM); + break; + case LISTVIEW_GRAVITY_CENTER_VERTICAL: + defaultLp->setGravity(LINEAR_GRAVITY_CENTER_VERTICAL); + break; + default: + break; + } + if (getIndex(item) == 0) + { + defaultLp->setMargin(UIMarginZero); + } + else + { + defaultLp->setMargin(UIMargin(_itemsMargin, 0.0f, 0.0f, 0.0f)); + } + item->setLayoutParameter(defaultLp); + } + else + { + if (getIndex(item) == 0) + { + llp->setMargin(UIMarginZero); + } + else + { + llp->setMargin(UIMargin(_itemsMargin, 0.0f, 0.0f, 0.0f)); + } + switch (_gravity) { + case LISTVIEW_GRAVITY_TOP: + llp->setGravity(LINEAR_GRAVITY_TOP); + break; + case LISTVIEW_GRAVITY_BOTTOM: + llp->setGravity(LINEAR_GRAVITY_BOTTOM); + break; + case LISTVIEW_GRAVITY_CENTER_VERTICAL: + llp->setGravity(LINEAR_GRAVITY_CENTER_VERTICAL); + break; + default: + break; + } + } + break; + } + default: + break; + } + +} + +void UIListView::pushBackDefaultItem() +{ + if (!_model) + { + return; + } + UIWidget* newItem = _model->clone(); + _items->addObject(newItem); + remedyLayoutParameter(newItem); + addChild(newItem); +} + +void UIListView::insertDefaultItem(int index) +{ + if (!_items) + { + return; + } + if (!_model) + { + return; + } + UIWidget* newItem = _model->clone(); + _items->insertObject(newItem, index); + remedyLayoutParameter(newItem); + addChild(newItem); +} + +void UIListView::pushBackCustomItem(UIWidget* item) +{ + _items->addObject(item); + remedyLayoutParameter(item); + addChild(item); +} + +void UIListView::insertCustomItem(UIWidget* item, int index) +{ + _items->insertObject(item, index); + remedyLayoutParameter(item); + addChild(item); +} + +void UIListView::removeItem(int index) +{ + if (!_items) + { + return; + } + UIWidget* item = getItem(index); + if (!item) + { + return; + + } + _items->removeObject(item); + removeChild(item); +} + +void UIListView::removeLastItem() +{ + removeItem(_items->count() -1); +} + +UIWidget* UIListView::getItem(unsigned int index) +{ + if ((int)index < 0 || index >= _items->count()) + { + return NULL; + } + return (UIWidget*)(_items->data->arr[index]); +} + +cocos2d::Array* UIListView::getItems() +{ + return _items; +} + +unsigned int UIListView::getIndex(UIWidget *item) const +{ + if (!_items) + { + return -1; + } + if (!item) + { + return -1; + } + return _items->getIndexOfObject(item); +} + +void UIListView::setGravity(ListViewGravity gravity) +{ + if (_gravity == gravity) + { + return; + } + _gravity = gravity; + refreshView(); +} + +void UIListView::setItemsMargin(float margin) +{ + if (_itemsMargin == margin) + { + return; + } + _itemsMargin = margin; + refreshView(); +} + +void UIListView::setDirection(SCROLLVIEW_DIR dir) +{ + switch (dir) + { + case SCROLLVIEW_DIR_VERTICAL: + setLayoutType(LAYOUT_LINEAR_VERTICAL); + break; + case SCROLLVIEW_DIR_HORIZONTAL: + setLayoutType(LAYOUT_LINEAR_HORIZONTAL); + break; + case SCROLLVIEW_DIR_BOTH: + return; + default: + return; + break; + } + UIScrollView::setDirection(dir); + +} + +void UIListView::refreshView() +{ + if (!_items) + { + return; + } + cocos2d::ccArray* arrayItems = _items->data; + int length = arrayItems->num; + for (int i=0; iarr[i]); + item->setZOrder(i); + remedyLayoutParameter(item); + } + updateInnerContainerSize(); + doLayout(); +} + void UIListView::onSizeChanged() { - Layout::onSizeChanged(); - _topBoundary = _size.height; - _rightBoundary = _size.width; -} - -bool UIListView::addChild(UIWidget* widget) -{ - Layout::addChild(widget); - resetProperty(); - return true; -} - -void UIListView::removeAllChildren() -{ - _updatePool->removeAllObjects(); - _childPool->removeAllObjects(); - Layout::removeAllChildren(); -} - -bool UIListView::removeChild(UIWidget* child) -{ - bool value = false; - - if (Layout::removeChild(child)) - { - value = true; - resetProperty(); - } - - return value; -} - -bool UIListView::onTouchBegan(const Point &touchPoint) -{ - bool pass = Layout::onTouchBegan(touchPoint); - handlePressLogic(touchPoint); - return pass; -} - -void UIListView::onTouchMoved(const Point &touchPoint) -{ - Layout::onTouchMoved(touchPoint); - handleMoveLogic(touchPoint); -} - -void UIListView::onTouchEnded(const Point &touchPoint) -{ - Layout::onTouchEnded(touchPoint); - handleReleaseLogic(touchPoint); -} - -void UIListView::onTouchCancelled(const Point &touchPoint) -{ - Layout::onTouchCancelled(touchPoint); -} - -void UIListView::onTouchLongClicked(const Point &touchPoint) -{ - -} - -void UIListView::update(float dt) -{ - if (_autoScroll) - { - autoScrollChildren(dt); - } - recordSlidTime(dt); -} - -void UIListView::setDirection(ListViewDirection dir) -{ - _direction = dir; -} - -ListViewDirection UIListView::getDirection() -{ - return _direction; -} - -void UIListView::resetProperty() -{ - ccArray* arrayChildren = _children->data; - - if (arrayChildren->num <= 0) - { - return; - } - - switch (_direction) - { - case LISTVIEW_DIR_VERTICAL: // vertical - if (_topBoundary == 0) - { - return; - } - break; - - case LISTVIEW_DIR_HORIZONTAL: // horizontal - if (_rightBoundary == 0) - { - return; - } - break; - - default: - break; - } - - float scroll_top = _topBoundary; - float scroll_left = _leftBoundary; - - switch (_children->count()) - { - case 1: - { - UIWidget* child_0 = dynamic_cast(arrayChildren->arr[0]); - - switch (_direction) - { - case LISTVIEW_DIR_VERTICAL: // vertical - { - float child_0_top = child_0->getTopInParent(); - _disBoundaryToChild_0 = scroll_top - child_0_top; - } - break; - - case LISTVIEW_DIR_HORIZONTAL: // horizontal - { - float child_0_left = child_0->getLeftInParent(); - _disBoundaryToChild_0 = child_0_left - scroll_left; - } - break; - - default: - break; - } - } - break; - - default: - { - UIWidget* child_0 = dynamic_cast(arrayChildren->arr[0]); - UIWidget* child_1 = dynamic_cast(arrayChildren->arr[1]); - - switch (_direction) - { - case LISTVIEW_DIR_VERTICAL: // vertical - { - float child_0_top = child_0->getTopInParent(); - _disBoundaryToChild_0 = scroll_top - child_0_top; - _disBetweenChild = child_0->getPosition().y - child_1->getPosition().y; - } - break; - - case LISTVIEW_DIR_HORIZONTAL: // horizontal - { - float child_0_left = child_0->getLeftInParent(); - _disBoundaryToChild_0 = child_0_left - scroll_left; - _disBetweenChild = child_1->getPosition().x - child_0->getPosition().x; - } - break; - - default: - break; - } - } - break; - } -} - -void UIListView::handlePressLogic(const Point &touchPoint) -{ - Point nsp = _renderer->convertToNodeSpace(touchPoint); - - switch (_direction) - { - case LISTVIEW_DIR_VERTICAL: // vertical - _touchMoveStartLocation = nsp.y; - _touchStartLocation = nsp.y; - break; - - case LISTVIEW_DIR_HORIZONTAL: // horizontal - _touchMoveStartLocation = nsp.x; - _touchStartLocation = nsp.x; - break; - - default: - break; - } - startRecordSlidAction(); - clearCollectOverArray(); -} - -void UIListView::handleMoveLogic(const Point &touchPoint) -{ - Point nsp = _renderer->convertToNodeSpace(touchPoint); - float offset = 0.0f; - - switch (_direction) - { - case LISTVIEW_DIR_VERTICAL: // vertical - { - float moveY = nsp.y; - offset = moveY - _touchMoveStartLocation; - _touchMoveStartLocation = moveY; - - if (offset < 0.0f) - { - _moveDirection = LISTVIEW_MOVE_DIR_DOWN; // down - } - else if (offset > 0.0f) - { - _moveDirection = LISTVIEW_MOVE_DIR_UP; // up - } - } - break; - - case LISTVIEW_DIR_HORIZONTAL: // horizontal - { - float moveX = nsp.x; - offset = moveX - _touchMoveStartLocation; - _touchMoveStartLocation = moveX; - - if (offset < 0) - { - _moveDirection = LISTVIEW_MOVE_DIR_LEFT; // left - } - else if (offset > 0) - { - _moveDirection = LISTVIEW_MOVE_DIR_RIGHT; // right - } - } - break; - - default: - break; - } - scrollChildren(offset); -} - -void UIListView::handleReleaseLogic(const Point &touchPoint) -{ - Point nsp = _renderer->convertToNodeSpace(touchPoint); - - switch (_direction) - { - case LISTVIEW_DIR_VERTICAL: // vertical - _touchEndLocation = nsp.y; - break; - - case LISTVIEW_DIR_HORIZONTAL: // horizontal - _touchEndLocation = nsp.x; - break; - - default: - break; - } - endRecordSlidAction(); -} - -void UIListView::interceptTouchEvent(int handleState, UIWidget *sender, const Point &touchPoint) -{ - switch (handleState) - { - case 0: - handlePressLogic(touchPoint); - break; - - case 1: - { - float offset = 0; - switch (_direction) - { - case LISTVIEW_DIR_VERTICAL: // vertical - offset = fabs(sender->getTouchStartPos().y - touchPoint.y); - break; - - case LISTVIEW_DIR_HORIZONTAL: // horizontal - offset = fabs(sender->getTouchStartPos().x - touchPoint.x); - break; - - default: - break; - } - if (offset > _childFocusCancelOffset) - { - sender->setFocused(false); - handleMoveLogic(touchPoint); - } - } - break; - - case 2: - handleReleaseLogic(touchPoint); - break; - - case 3: - break; - } -} - -void UIListView::checkChildInfo(int handleState,UIWidget* sender,const Point &touchPoint) -{ - interceptTouchEvent(handleState, sender, touchPoint); -} - -void UIListView::moveChildren(float offset) -{ - switch (_direction) - { - case LISTVIEW_DIR_VERTICAL: // vertical - { - ccArray* arrayChildren = _children->data; - int childrenCount = arrayChildren->num; - for (int i = 0; i < childrenCount; i++) - { - UIWidget* child = (UIWidget*)(arrayChildren->arr[i]); - _moveChildPoint.x = child->getPosition().x; - _moveChildPoint.y = child->getPosition().y + offset; - child->setPosition(_moveChildPoint); - } - break; - } - - case LISTVIEW_DIR_HORIZONTAL: // horizontal - { - ccArray* arrayChildren = _children->data; - int childrenCount = arrayChildren->num; - for (int i=0;iarr[i]); - _moveChildPoint.x = child->getPosition().x + offset; - _moveChildPoint.y = child->getPosition().y; - child->setPosition(_moveChildPoint); - } - break; - } - - default: - break; - } -} - -bool UIListView::scrollChildren(float touchOffset) -{ - float realOffset = touchOffset; - - switch (_direction) - { - case LISTVIEW_DIR_VERTICAL: // vertical - switch (_moveDirection) - { - case LISTVIEW_MOVE_DIR_UP: // up - { - realOffset = MIN(realOffset, _disBetweenChild); - - UIWidget* child_last = dynamic_cast(_childPool->getLastObject()); - float child_last_bottom = child_last->getBottomInParent(); - float scroll_bottom = _bottomBoundary; - - if (_end == _dataLength - 1) - { - if (realOffset > scroll_bottom + _disBoundaryToChild_0 - child_last_bottom) - { - realOffset = scroll_bottom + _disBoundaryToChild_0 - child_last_bottom; - } - moveChildren(realOffset); - return false; - } - moveChildren(realOffset); - - if (_end < _dataLength - 1) - { - collectOverTopChild(); - unsigned int count = _overTopArray->count(); - if (count > 0) - { - updateChild(); - setLoopPosition(); - _overTopArray->removeAllObjects(); - } - } - } - break; - - case LISTVIEW_MOVE_DIR_DOWN: // down - { - realOffset = MAX(realOffset, -_disBetweenChild); - - UIWidget* child_0 = dynamic_cast(_childPool->getObjectAtIndex(0)); - float child_0_top = child_0->getTopInParent(); - float scroll_top = _topBoundary; - - if (_begin == 0) - { - if (realOffset < scroll_top - _disBoundaryToChild_0 - child_0_top) - { - realOffset = scroll_top - _disBoundaryToChild_0 - child_0_top; - } - moveChildren(realOffset); - return false; - } - moveChildren(realOffset); - - if (_begin > 0) - { - collectOverBottomChild(); - int count = _overBottomArray->count(); - if (count > 0) - { - updateChild(); - setLoopPosition(); - _overBottomArray->removeAllObjects(); - } - } - } - break; - - default: - break; - } - return true; - break; - - case LISTVIEW_DIR_HORIZONTAL: // horizontal - switch (_moveDirection) - { - case LISTVIEW_MOVE_DIR_LEFT: // left - { - realOffset = MAX(realOffset, -_disBetweenChild); - - UIWidget* child_last = dynamic_cast(_childPool->getLastObject()); - float child_last_right = child_last->getRightInParent(); - float scroll_right = _rightBoundary; - - if (_end == _dataLength - 1) - { - if (realOffset < scroll_right - _disBoundaryToChild_0 - child_last_right) - { - realOffset = scroll_right - _disBoundaryToChild_0 - child_last_right; - } - moveChildren(realOffset); - return false; - } - moveChildren(realOffset); - - if (_end < _dataLength - 1) - { - collectOverLeftChild(); - int count = _overLeftArray->count(); - if (count > 0) - { - updateChild(); - setLoopPosition(); - _overLeftArray->removeAllObjects(); - } - } - } - break; - - case LISTVIEW_MOVE_DIR_RIGHT: // right - { - realOffset = MIN(realOffset, _disBetweenChild); - - UIWidget* child_0 = dynamic_cast(_childPool->getObjectAtIndex(0)); - float child_0_left = child_0->getLeftInParent(); - float scroll_left = _leftBoundary; - - if (_begin == 0) - { - if (realOffset > scroll_left + _disBoundaryToChild_0 - child_0_left) - { - realOffset = scroll_left + _disBoundaryToChild_0 - child_0_left; - } - moveChildren(realOffset); - return false; - } - moveChildren(realOffset); - - collectOverRightChild(); - int count = _overRightArray->count(); - if (count > 0) - { - updateChild(); - setLoopPosition(); - _overRightArray->removeAllObjects(); - } - } - break; - - default: - break; - } - return true; - break; - - default: - break; - } - - return false; -} - -void UIListView::autoScrollChildren(float dt) -{ - switch (_direction) - { - case LISTVIEW_DIR_VERTICAL: // vertical - switch (_moveDirection) - { - case LISTVIEW_MOVE_DIR_UP: // up - { - float curDis = getCurAutoScrollDistance(dt); - if (curDis <= 0) - { - curDis = 0; - stopAutoScrollChildren(); - } - if (!scrollChildren(curDis)) - { - stopAutoScrollChildren(); - } - } - break; - - case LISTVIEW_MOVE_DIR_DOWN: // down - { - float curDis = getCurAutoScrollDistance(dt); - if (curDis <= 0) - { - curDis = 0; - stopAutoScrollChildren(); - } - if (!scrollChildren(-curDis)) - { - stopAutoScrollChildren(); - } - } - break; - - default: - break; - } - break; - - case LISTVIEW_DIR_HORIZONTAL: // horizontal - switch (_moveDirection) - { - case LISTVIEW_MOVE_DIR_LEFT: // left - { - float curDis = getCurAutoScrollDistance(dt); - if (curDis <= 0) - { - curDis = 0; - stopAutoScrollChildren(); - } - if (!scrollChildren(-curDis)) - { - stopAutoScrollChildren(); - } - } - break; - - case LISTVIEW_MOVE_DIR_RIGHT: // right - { - float curDis = getCurAutoScrollDistance(dt); - if (curDis <= 0) - { - curDis = 0; - stopAutoScrollChildren(); - } - if (!scrollChildren(curDis)) - { - stopAutoScrollChildren(); - } - } - break; - - default: - break; - } - break; - - default: - break; - } -} - -float UIListView::getCurAutoScrollDistance(float time) -{ - float dt = time; - _autoScrollOriginalSpeed -= _autoScrollAcceleration*dt; - return _autoScrollOriginalSpeed*dt; -} - -void UIListView::startAutoScrollChildren(float v) -{ - _autoScrollOriginalSpeed = v; - _autoScroll = true; -} - -void UIListView::stopAutoScrollChildren() -{ - _autoScroll = false; - _autoScrollOriginalSpeed = 0.0f; -} - -void UIListView::recordSlidTime(float dt) -{ - if (_bePressed) - { - _slidTime += dt; - } -} - -void UIListView::startRecordSlidAction() -{ - if (_children->count() <= 0) - { - return; - } - if (_autoScroll) - { - stopAutoScrollChildren(); - } - _bePressed = true; - _slidTime = 0.0; -} - -void UIListView::endRecordSlidAction() -{ - if (_children->count() <= 0) - { - return; - } - if (_slidTime <= 0.016f) - { - return; - } - float totalDis = 0; - totalDis = _touchEndLocation-_touchStartLocation; - float orSpeed = fabs(totalDis)/(_slidTime); - startAutoScrollChildren(orSpeed / 4); - - _bePressed = false; - _slidTime = 0.0; -} - -UIWidget* UIListView::getCheckPositionChild() -{ - UIWidget* child = NULL; - - switch (_direction) - { - case LISTVIEW_DIR_VERTICAL: // vertical - switch (_moveDirection) - { - case LISTVIEW_MOVE_DIR_UP: // up - child = dynamic_cast(_childPool->getLastObject()); - break; - - case LISTVIEW_MOVE_DIR_DOWN: // down - child = dynamic_cast(_childPool->getObjectAtIndex(0)); - break; - - default: - break; - } - break; - - case LISTVIEW_DIR_HORIZONTAL: // horizontal - switch (_moveDirection) - { - case LISTVIEW_MOVE_DIR_LEFT: // left - child = dynamic_cast(_childPool->getLastObject()); - break; - - case LISTVIEW_MOVE_DIR_RIGHT: // right - child = dynamic_cast(_childPool->getObjectAtIndex(0)); - break; - - default: - break; - } - break; - - default: - break; - } - - return child; -} - -void UIListView::initChildWithDataLength(int length) -{ - _dataLength = length; - _begin = 0; - _end = 0; - - // init child pool - ccArray* arrayChildren = _children->data; - int times = arrayChildren->num; - for (int i = 0; i < times; ++i) - { - UIWidget* child = dynamic_cast(arrayChildren->arr[i]); - setUpdateChild(child); - setUpdateDataIndex(i); - initChildEvent(); - _childPool->addObject(child); - _end = i; - } -} - -UIWidget* UIListView::getChildFromUpdatePool() -{ - UIWidget* child = dynamic_cast(_updatePool->getLastObject()); - _updatePool->removeLastObject(); - return child; -} - -void UIListView::pushChildToPool() -{ - switch (_direction) - { - case LISTVIEW_DIR_VERTICAL: // vertical - switch (_moveDirection) - { - case LISTVIEW_MOVE_DIR_UP: // up - { - UIWidget* child = dynamic_cast(_childPool->getObjectAtIndex(0)); - _updatePool->insertObject(child, 0); - _childPool->removeObjectAtIndex(0); - } - break; - - case LISTVIEW_MOVE_DIR_DOWN: // down - { - UIWidget* child = dynamic_cast(_childPool->getLastObject()); - _updatePool->insertObject(child, 0); - _childPool->removeLastObject(); - - } - break; - - default: - break; - } - break; - - case LISTVIEW_DIR_HORIZONTAL: // horizontal - switch (_moveDirection) - { - case LISTVIEW_MOVE_DIR_LEFT: // left - { - UIWidget* child = dynamic_cast(_childPool->getObjectAtIndex(0)); - _updatePool->insertObject(child, 0); - _childPool->removeObjectAtIndex(0); - } - break; - - case LISTVIEW_MOVE_DIR_RIGHT: // right - { - UIWidget* child = dynamic_cast(_childPool->getLastObject()); - _updatePool->insertObject(child, 0); - _childPool->removeLastObject(); - } - break; - - default: - break; - } - break; - - default: - break; - } -} - -void UIListView::getAndCallback() -{ - UIWidget* child = getChildFromUpdatePool(); - - if (child == NULL) - { - return; - } - - switch (_direction) - { - case LISTVIEW_DIR_VERTICAL: // vertical - switch (_moveDirection) - { - case LISTVIEW_MOVE_DIR_UP: // up - ++_end; - setUpdateChild(child); - setUpdateDataIndex(_end); - updateChildEvent(); - - if (_updateSuccess == false) - { - --_end; - _childPool->insertObject(child, 0); - return; - } - ++_begin; - break; - - case LISTVIEW_MOVE_DIR_DOWN: // down - --_begin; - setUpdateChild(child); - setUpdateDataIndex(_begin); - updateChildEvent(); - - if (_updateSuccess == false) - { - ++_begin; - _childPool->addObject(child); - return; - } - --_end; - break; - - default: - break; - } - break; - - case LISTVIEW_DIR_HORIZONTAL: // horizontal - switch (_moveDirection) - { - case LISTVIEW_MOVE_DIR_LEFT: // left - ++_end; - setUpdateChild(child); - setUpdateDataIndex(_end); - updateChildEvent(); - - if (_updateSuccess == false) - { - --_end; - _childPool->insertObject(child, 0); - return; - } - ++_begin; - break; - - case LISTVIEW_MOVE_DIR_RIGHT: // right - --_begin; - setUpdateChild(child); - setUpdateDataIndex(_begin); - updateChildEvent(); - - if (_updateSuccess == false) - { - ++_begin; - _childPool->addObject(child); - return; - } - --_end; - break; - - default: - break; - } - break; - - default: - break; - } - - switch (_direction) - { - case LISTVIEW_DIR_VERTICAL: // vertical - switch (_moveDirection) - { - case LISTVIEW_MOVE_DIR_UP: // up - _childPool->addObject(child); - break; - - case LISTVIEW_MOVE_DIR_DOWN: // down - _childPool->insertObject(child, 0); - break; - - default: - break; - } - break; - - case LISTVIEW_DIR_HORIZONTAL: // horizontal - switch (_moveDirection) - { - case LISTVIEW_MOVE_DIR_LEFT: // left - _childPool->addObject(child); - break; - case LISTVIEW_MOVE_DIR_RIGHT: // right - _childPool->insertObject(child, 0); - break; - - default: - break; - } - break; - - default: - break; - } -} - -int UIListView::getDataLength() -{ - return _dataLength; -} - -UIWidget* UIListView::getUpdateChild() -{ - return _updateChild; -} - -void UIListView::setUpdateChild(UIWidget* child) -{ - _updateChild = child; -} - -int UIListView::getUpdateDataIndex() -{ - return _updateDataIndex; -} - -void UIListView::setUpdateDataIndex(int index) -{ - _updateDataIndex = index; -} - -bool UIListView::getUpdateSuccess() -{ - return _updateSuccess; -} - -void UIListView::setUpdateSuccess(bool sucess) -{ - _updateSuccess = sucess; -} - -void UIListView::clearCollectOverArray() -{ - switch (_direction) - { - case LISTVIEW_DIR_VERTICAL: - _overTopArray->removeAllObjects(); - _overBottomArray->removeAllObjects(); - break; - - case LISTVIEW_DIR_HORIZONTAL: - _overLeftArray->removeAllObjects(); - _overRightArray->removeAllObjects(); - break; - - default: - break; - } -} - -void UIListView::collectOverTopChild() -{ - float scroll_top = _topBoundary; - - ccArray* arrayChildren = _children->data; - int times = arrayChildren->num; - for (int i = 0; i < times; ++i) - { - UIWidget* child = dynamic_cast(arrayChildren->arr[i]); - float child_bottom = child->getBottomInParent(); - - if (child_bottom >= scroll_top) - { - _overTopArray->addObject(child); - } - } -} - -void UIListView::collectOverBottomChild() -{ - float scroll_bottom = _bottomBoundary; - - ccArray* arrayChildren = _children->data; - int times = arrayChildren->num; - for (int i = 0; i < times; ++i) - { - UIWidget* child = dynamic_cast(arrayChildren->arr[i]); - float child_top = child->getTopInParent(); - - if (child_top <= scroll_bottom) - { - _overBottomArray->addObject(child); - } - } -} - -void UIListView::collectOverLeftChild() -{ - float scroll_left = _leftBoundary; - - ccArray* arrayChildren = _children->data; - int times = arrayChildren->num; - for (int i = 0; i < times; ++i) - { - UIWidget* child = dynamic_cast(arrayChildren->arr[i]); - float child_right = child->getRightInParent(); - - if (child_right <= scroll_left) - { - _overLeftArray->addObject(child); - } - } -} - -void UIListView::collectOverRightChild() -{ - float scroll_right = _rightBoundary; - - ccArray* arrayChildren = _children->data; - int times = arrayChildren->num; - for (int i = 0; i < times; ++i) - { - UIWidget* child = dynamic_cast(arrayChildren->arr[i]); - float child_left = child->getLeftInParent(); - if (child_left >= scroll_right) - { - _overRightArray->addObject(child); - } - } -} - -void UIListView::setLoopPosition() -{ - switch (_direction) - { - case LISTVIEW_DIR_VERTICAL: // vertical - switch (_moveDirection) - { - case LISTVIEW_MOVE_DIR_UP: // up - { - ccArray* arrayChildren = _children->data; - unsigned int childrenCount = arrayChildren->num; - - if (_overTopArray->count() == childrenCount) - { - unsigned int count = childrenCount; - for (unsigned int i = 0; i < count; ++i) - { - UIWidget* child = dynamic_cast(_overTopArray->getObjectAtIndex(i)); - - if (i == 0) - { - float height = child->getSize().height; - float offset = (child->getWidgetType() == WidgetTypeWidget) ? height / 2 : height; - float y = _topBoundary - _disBoundaryToChild_0 - offset; - child->setPosition(Point(child->getPosition().x, y)); - } - else - { - UIWidget* prev_child = dynamic_cast(_overTopArray->getObjectAtIndex(i - 1)); - child->setPosition(Point(child->getPosition().x, prev_child->getPosition().y - _disBetweenChild)); - } - } - } - else - { - float scroll_top = _topBoundary; - - ccArray* arrayChildren = _children->data; - int count = arrayChildren->num; - for (int i = 0; i < count; ++i) - { - UIWidget* child = dynamic_cast(arrayChildren->arr[i]); - float child_bottom = child->getBottomInParent(); - - if (child_bottom >= scroll_top) - { - int index = (i == 0) ? (count - 1) : (i - 1); - UIWidget* prev_child = dynamic_cast(arrayChildren->arr[index]); - child->setPosition(Point(child->getPosition().x, prev_child->getPosition().y - _disBetweenChild)); - } - } - } - } - break; - - case LISTVIEW_MOVE_DIR_DOWN: // down - { - ccArray* arrayChildren = _children->data; - unsigned int childrenCount = arrayChildren->num; - - if (_overBottomArray->count() == childrenCount) - { - unsigned int count = childrenCount; - for (unsigned int i = 0; i < count; ++i) - { - UIWidget* child = dynamic_cast(_overBottomArray->getObjectAtIndex(i)); - - if (i == 0) - { - float y = _bottomBoundary + _disBoundaryToChild_0 - _disBetweenChild; - child->setPosition(Point(child->getPosition().x, y)); - } - else - { - UIWidget* prev_child = dynamic_cast(_overBottomArray->getObjectAtIndex(i - 1)); - child->setPosition(Point(child->getPosition().x, prev_child->getPosition().y + _disBetweenChild)); - } - } - } - else - { - float scroll_bottom = _bottomBoundary; - - ccArray* arrayChildren = _children->data; - int count = arrayChildren->num; - for (int i = count - 1; i >= 0; --i) - { - UIWidget* child = dynamic_cast(arrayChildren->arr[i]); - float child_top = child->getTopInParent(); - - if (child_top <= scroll_bottom) - { - int index = (i == count - 1) ? 0 : (i + 1); - UIWidget* next_child = dynamic_cast(arrayChildren->arr[index]); - child->setPosition(Point(child->getPosition().x, next_child->getPosition().y + _disBetweenChild)); - } - } - } - } - break; - - default: - break; - } - break; - - case LISTVIEW_DIR_HORIZONTAL: // horizontal - switch (_moveDirection) - { - case LISTVIEW_MOVE_DIR_LEFT: // left - { - ccArray* arrayChildren = _children->data; - unsigned int childrenCount = arrayChildren->num; - - if (_overLeftArray->count() == childrenCount) - { - unsigned int count = childrenCount; - for (unsigned int i = 0; i < count; ++i) - { - UIWidget* child = dynamic_cast(_overLeftArray->getObjectAtIndex(i)); - - if (i == 0) - { - float width = child->getSize().width; - float offset = (child->getWidgetType() == WidgetTypeWidget) ? (width / 2) : 0; - float x = _leftBoundary + _disBoundaryToChild_0 + width + offset; - child->setPosition(Point(x, child->getPosition().y)); - } - else - { - UIWidget* prev_child = dynamic_cast(_overLeftArray->getObjectAtIndex(i - 1)); - child->setPosition(Point(prev_child->getPosition().x + _disBetweenChild, child->getPosition().y)); - } - } - } - else - { - float scroll_left = _leftBoundary; - - ccArray* arrayChildren = _children->data; - int count = arrayChildren->num; - for (int i = 0; i < count; ++i) - { - UIWidget* child = dynamic_cast(arrayChildren->arr[i]); - float child_right = child->getRightInParent(); - - if (child_right <= scroll_left) - { - int index = (i == 0) ? (count - 1) : (i - 1); - UIWidget* prev_child = dynamic_cast(arrayChildren->arr[index]); - child->setPosition(Point(prev_child->getPosition().x + _disBetweenChild, child->getPosition().y)); - } - } - } - } - break; - - case LISTVIEW_MOVE_DIR_RIGHT: // right - { - ccArray* arrayChildren = _children->data; - unsigned int childrenCount = arrayChildren->num; - - if (_overRightArray->count() == childrenCount) - { - unsigned int count = childrenCount; - for (unsigned int i = 0; i < count; ++i) - { - UIWidget* child = dynamic_cast(_overRightArray->getObjectAtIndex(i)); - - if (i == 0) - { - float x = _rightBoundary - _disBoundaryToChild_0 + _disBetweenChild; - child->setPosition(Point(x, child->getPosition().y)); - } - else - { - UIWidget* prev_child = dynamic_cast(_overRightArray->getObjectAtIndex(i - 1)); - child->setPosition(Point(prev_child->getPosition().x - _disBetweenChild, child->getPosition().y)); - } - } - } - else - { - float scroll_right = _rightBoundary; - - ccArray* arrayChildren = _children->data; - int count = arrayChildren->num; - for (int i = count - 1; i >= 0; --i) - { - UIWidget* child = dynamic_cast(arrayChildren->arr[i]); - float child_left = child->getLeftInParent(); - - if (child_left >= scroll_right) - { - int index = (i == count - 1) ? 0 : (i + 1); - UIWidget* next_child = dynamic_cast(arrayChildren->arr[index]); - child->setPosition(Point(next_child->getPosition().x - _disBetweenChild, child->getPosition().y)); - } - } - } - } - break; - - default: - break; - } - break; - - default: - break; - } -} - -void UIListView::updateChild() -{ - switch (_direction) - { - case LISTVIEW_DIR_VERTICAL: // vertical - switch (_moveDirection) - { - case LISTVIEW_MOVE_DIR_UP: // up - { - int count = _overTopArray->count(); - for (int i = 0; i < count; ++i) - { - pushChildToPool(); - getAndCallback(); - } - } - break; - - case LISTVIEW_MOVE_DIR_DOWN: // down - { - int count = _overBottomArray->count(); - for (int i = 0; i < count; ++i) - { - pushChildToPool(); - getAndCallback(); - } - } - break; - - default: - break; - } - break; - - case LISTVIEW_DIR_HORIZONTAL: // horizontal - switch (_moveDirection) - { - case LISTVIEW_MOVE_DIR_LEFT: // left - { - int count = _overLeftArray->count(); - for (int i = 0; i < count; ++i) - { - pushChildToPool(); - getAndCallback(); - } - } - break; - - case LISTVIEW_MOVE_DIR_RIGHT: // right - { - int count = _overRightArray->count(); - for (int i = 0; i < count; ++i) - { - pushChildToPool(); - getAndCallback(); - } - } - break; - - default: - break; - } - break; - - default: - break; - } -} - -void UIListView::initChildEvent() -{ - if (_eventListener && _eventSelector) - { - (_eventListener->*_eventSelector)(this, LISTVIEW_EVENT_INIT_CHILD); - } -} - -void UIListView::updateChildEvent() -{ - if (_eventListener && _eventSelector) - { - (_eventListener->*_eventSelector)(this, LISTVIEW_EVENT_UPDATE_CHILD); - } -} - -void UIListView::addEventListenter(Object *target, SEL_ListViewEvent selector) -{ - _eventListener = target; - _eventSelector = selector; + UIScrollView::onSizeChanged(); + refreshView(); } const char* UIListView::getDescription() const { - return "ListView"; + return "ListViewEx"; } +UIWidget* UIListView::createCloneInstance() +{ + return UIListView::create(); +} -} \ No newline at end of file +void UIListView::copyClonedWidgetChildren(UIWidget* model) +{ + cocos2d::ccArray* arrayItems = dynamic_cast(model)->getItems()->data; + int length = arrayItems->num; + for (int i=0; iarr[i]); + pushBackCustomItem(item->clone()); + } +} + +void UIListView::copySpecialProperties(UIWidget *widget) +{ + UIListView* listViewEx = dynamic_cast(widget); + if (listViewEx) + { + UIScrollView::copySpecialProperties(widget); + setItemModel(listViewEx->_model); + setItemsMargin(listViewEx->_itemsMargin); + setGravity(listViewEx->_gravity); + } +} + +} diff --git a/cocos/gui/UIListView.h b/cocos/gui/UIListView.h index 8830db6209..c78d06ace2 100644 --- a/cocos/gui/UIListView.h +++ b/cocos/gui/UIListView.h @@ -22,226 +22,156 @@ THE SOFTWARE. ****************************************************************************/ + #ifndef __UILISTVIEW_H__ #define __UILISTVIEW_H__ -/* gui mark */ -#include "gui/Layout.h" -/**/ +#include "gui/UIScrollView.h" -namespace gui { - -/** - * list view direction - */ -typedef enum LISTVIEW_DIR -{ - LISTVIEW_DIR_NONE, - LISTVIEW_DIR_VERTICAL, - LISTVIEW_DIR_HORIZONTAL -}ListViewDirection; - -/** - * list view scroll direction - */ -typedef enum LISTVIEW_MOVE_DIR -{ - LISTVIEW_MOVE_DIR_NONE, - LISTVIEW_MOVE_DIR_UP, - LISTVIEW_MOVE_DIR_DOWN, - LISTVIEW_MOVE_DIR_LEFT, - LISTVIEW_MOVE_DIR_RIGHT, -}ListViewMoveDirection; +namespace gui{ typedef enum { - LISTVIEW_EVENT_INIT_CHILD, - LISTVIEW_EVENT_UPDATE_CHILD, -}ListViewEventType; + LISTVIEW_GRAVITY_LEFT, + LISTVIEW_GRAVITY_RIGHT, + LISTVIEW_GRAVITY_CENTER_HORIZONTAL, + + LISTVIEW_GRAVITY_TOP, + LISTVIEW_GRAVITY_BOTTOM, + LISTVIEW_GRAVITY_CENTER_VERTICAL, +}ListViewGravity; -/** - * list view event - */ -typedef void (cocos2d::Object::*SEL_ListViewEvent)(cocos2d::Object*, ListViewEventType); -#define listvieweventselector(_SELECTOR)(SEL_ListViewEvent)(&_SELECTOR) - -class UIListView : public Layout +class UIListView : public UIScrollView { + public: + + /** + * Default constructor + */ UIListView(); + + /** + * Default destructor + */ virtual ~UIListView(); + + /** + * Allocates and initializes. + */ static UIListView* create(); /** - * add widget child override + * Sets a item model for listview + * + * A model will be cloned for adding default item. + * + * @param model item model for listview */ - virtual bool addChild(UIWidget* widget); - /** - * remove all widget children override - */ - virtual void removeAllChildren(); - /** - * remove widget child override - */ - virtual bool removeChild(UIWidget* child); - - virtual bool onTouchBegan(const cocos2d::Point &touchPoint); - virtual void onTouchMoved(const cocos2d::Point &touchPoint); - virtual void onTouchEnded(const cocos2d::Point &touchPoint); - virtual void onTouchCancelled(const cocos2d::Point &touchPoint); - virtual void onTouchLongClicked(const cocos2d::Point &touchPoint); + void setItemModel(UIWidget* model); /** - * set and get direction + * Push back a default item(create by a cloned model) into listview. */ - void setDirection(ListViewDirection dir); - ListViewDirection getDirection(); + void pushBackDefaultItem(); /** - * initialze data length - * and create children with parameter length + * Insert a default item(create by a cloned model) into listview. */ - void initChildWithDataLength(int length); - /** - * get data length - */ - int getDataLength(); + void insertDefaultItem(int index); /** - * update child function whetn trigger update child event + * Push back custom item into listview. */ - /** - * get update widget child - */ - UIWidget* getUpdateChild(); - /** - * get update data index - */ - int getUpdateDataIndex(); - /** - * get and set update success or not - */ - bool getUpdateSuccess(); - void setUpdateSuccess(bool sucess); + void pushBackCustomItem(UIWidget* item); /** - * add event call-back function + * Insert custom item into listview. */ - /** - * add event - */ - void addEventListenter(cocos2d::Object* target, SEL_ListViewEvent selector); - - /* gui mark */ - /** - * get and set degree range for checking move or not with scrolling - */ - /**/ - virtual void update(float dt); - - virtual void doLayout(){}; + void insertCustomItem(UIWidget* item, int index); /** - * Returns the "class name" of widget. + * Removes the last item of listview. */ + void removeLastItem(); + + /** + * Removes a item whose index is same as the parameter. + * + * @param index of item. + */ + void removeItem(int index); + + /** + * Returns a item whose index is same as the parameter. + * + * @param index of item. + * + * @return the item widget. + */ + UIWidget* getItem(unsigned int index); + + /** + * Returns the item container. + */ + cocos2d::Array* getItems(); + + /** + * Returns the index of item. + * + * @param item the item which need to be checked. + * + * @return the index of item. + */ + unsigned int getIndex(UIWidget* item) const; + + /** + * Changes the gravity of listview. + * @see ListViewGravity + */ + void setGravity(ListViewGravity gravity); + + /** + * Changes the margin between each item. + * + * @param margin + */ + void setItemsMargin(float margin); + + /** + * Refresh the view of list. + * + * If you change the data, you need to call this mathod. + */ + void refreshView(); + + /** + * Changes scroll direction of scrollview. + * + * @see SCROLLVIEW_DIR SCROLLVIEW_DIR_VERTICAL means vertical scroll, SCROLLVIEW_DIR_HORIZONTAL means horizontal scroll + * + * @param SCROLLVIEW_DIR + */ + virtual void setDirection(SCROLLVIEW_DIR dir); + virtual const char* getDescription() const; + protected: virtual bool init(); - + void updateInnerContainerSize(); + void remedyLayoutParameter(UIWidget* item); virtual void onSizeChanged(); - - void setMoveDirection(ListViewMoveDirection dir); - ListViewMoveDirection getMoveDirection(); - - virtual void resetProperty(); - - virtual void handlePressLogic(const cocos2d::Point &touchPoint); - virtual void handleMoveLogic(const cocos2d::Point &touchPoint); - virtual void handleReleaseLogic(const cocos2d::Point &touchPoint); - virtual void interceptTouchEvent(int handleState,UIWidget* sender,const cocos2d::Point &touchPoint); - /* gui mark */ -// virtual bool isInScrollDegreeRange(UIWidget* widget); - /**/ - virtual void checkChildInfo(int handleState,UIWidget* sender,const cocos2d::Point &touchPoint); - - void moveChildren(float offset); - virtual bool scrollChildren(float touchOffset); - void autoScrollChildren(float dt); - float getCurAutoScrollDistance(float time); - void startAutoScrollChildren(float v); - void stopAutoScrollChildren(); - void recordSlidTime(float dt); - void startRecordSlidAction(); - virtual void endRecordSlidAction(); - - UIWidget* getCheckPositionChild(); - UIWidget* getChildFromUpdatePool(); - void pushChildToPool(); - void getAndCallback(); - - void setUpdateChild(UIWidget* child); - void setUpdateDataIndex(int index); - void clearCollectOverArray(); - void collectOverTopChild(); - void collectOverBottomChild(); - void collectOverLeftChild(); - void collectOverRightChild(); - void setLoopPosition(); - void updateChild(); - - void initChildEvent(); - void updateChildEvent(); - - virtual void setClippingEnabled(bool able){Layout::setClippingEnabled(able);}; + virtual UIWidget* createCloneInstance(); + virtual void copySpecialProperties(UIWidget* model); + virtual void copyClonedWidgetChildren(UIWidget* model); protected: - ListViewDirection _direction; - ListViewMoveDirection _moveDirection; - float _touchStartLocation; - float _touchEndLocation; - float _touchMoveStartLocation; - float _topBoundary;//test - float _bottomBoundary;//test - float _leftBoundary; - float _rightBoundary; - - bool _autoScroll; - - float _autoScrollOriginalSpeed; - float _autoScrollAcceleration; - - bool _bePressed; - float _slidTime; - cocos2d::Point _moveChildPoint; - float _childFocusCancelOffset; - - cocos2d::Object* _eventListener; - SEL_ListViewEvent _eventSelector; - - cocos2d::Array* _childPool; - cocos2d::Array* _updatePool; - - int _dataLength; - int _begin; - int _end; - UIWidget* _updateChild; - int _updateDataIndex; - bool _updateSuccess; - - cocos2d::Array* _overTopArray; - cocos2d::Array* _overBottomArray; - cocos2d::Array* _overLeftArray; - cocos2d::Array* _overRightArray; - - float _disBoundaryToChild_0; - float _disBetweenChild; - - /* gui mark */ - float _scrollDegreeRange; - /**/ + UIWidget* _model; + cocos2d::Array* _items; + ListViewGravity _gravity; + float _itemsMargin; }; } - -#endif /* defined(__Test__UIListView__) */ +#endif /* defined(__UIListView__) */ diff --git a/cocos/gui/UILoadingBar.cpp b/cocos/gui/UILoadingBar.cpp index 6d74be86c4..09b78c7b25 100644 --- a/cocos/gui/UILoadingBar.cpp +++ b/cocos/gui/UILoadingBar.cpp @@ -25,11 +25,9 @@ #include "gui/UILoadingBar.h" #include "extensions/GUI/CCControlExtension/CCScale9Sprite.h" - using namespace cocos2d; - using namespace cocos2d::extension; - namespace gui { + #define DYNAMIC_CAST_CCSPRITE dynamic_cast(_barRenderer) UILoadingBar::UILoadingBar(): @@ -38,10 +36,10 @@ _percent(100), _totalLength(0), _barRenderer(NULL), _renderBarTexType(UI_TEX_TYPE_LOCAL), -_barRendererTextureSize(Size::ZERO), +_barRendererTextureSize(cocos2d::Size::ZERO), _scale9Enabled(false), _prevIgnoreSize(true), -_capInsets(Rect::ZERO), +_capInsets(cocos2d::Rect::ZERO), _textureFile("") { } @@ -66,9 +64,9 @@ UILoadingBar* UILoadingBar::create() void UILoadingBar::initRenderer() { UIWidget::initRenderer(); - _barRenderer = CCSprite::create(); + _barRenderer = cocos2d::Sprite::create(); _renderer->addChild(_barRenderer); - _barRenderer->setAnchorPoint(Point(0.0f,0.5f)); + _barRenderer->setAnchorPoint(cocos2d::Point(0.0,0.5)); } void UILoadingBar::setDirection(LoadingBarType dir) @@ -82,19 +80,19 @@ void UILoadingBar::setDirection(LoadingBarType dir) switch (_barType) { case LoadingBarTypeLeft: - _barRenderer->setAnchorPoint(Point(0.0f,0.5f)); - _barRenderer->setPosition(Point(-_totalLength*0.5f,0.0f)); + _barRenderer->setAnchorPoint(cocos2d::Point(0.0f,0.5f)); + _barRenderer->setPosition(cocos2d::Point(-_totalLength*0.5f,0.0f)); if (!_scale9Enabled) { - dynamic_cast(_barRenderer)->setFlippedX(false); + dynamic_cast(_barRenderer)->setFlippedX(false); } break; case LoadingBarTypeRight: - _barRenderer->setAnchorPoint(Point(1.0f,0.5f)); - _barRenderer->setPosition(Point(_totalLength*0.5f,0.0f)); + _barRenderer->setAnchorPoint(cocos2d::Point(1.0f,0.5f)); + _barRenderer->setPosition(cocos2d::Point(_totalLength*0.5f,0.0f)); if (!_scale9Enabled) { - dynamic_cast(_barRenderer)->setFlippedX(true); + dynamic_cast(_barRenderer)->setFlippedX(true); } break; } @@ -118,21 +116,23 @@ void UILoadingBar::loadTexture(const char* texture,TextureResType texType) case UI_TEX_TYPE_LOCAL: if (_scale9Enabled) { - dynamic_cast(_barRenderer)->initWithFile(texture); + dynamic_cast(_barRenderer)->initWithFile(texture); + dynamic_cast(_barRenderer)->setCapInsets(_capInsets); } else { - dynamic_cast(_barRenderer)->initWithFile(texture); + dynamic_cast(_barRenderer)->initWithFile(texture); } break; case UI_TEX_TYPE_PLIST: if (_scale9Enabled) { - dynamic_cast(_barRenderer)->initWithSpriteFrameName(texture); + dynamic_cast(_barRenderer)->initWithSpriteFrameName(texture); + dynamic_cast(_barRenderer)->setCapInsets(_capInsets); } else { - dynamic_cast(_barRenderer)->initWithSpriteFrameName(texture); + dynamic_cast(_barRenderer)->initWithSpriteFrameName(texture); } break; default: @@ -140,31 +140,31 @@ void UILoadingBar::loadTexture(const char* texture,TextureResType texType) } if (_scale9Enabled) { - dynamic_cast(_barRenderer)->setColor(getColor()); - dynamic_cast(_barRenderer)->setOpacity(getOpacity()); + dynamic_cast(_barRenderer)->setColor(getColor()); + dynamic_cast(_barRenderer)->setOpacity(getOpacity()); + } else { - dynamic_cast(_barRenderer)->setColor(getColor()); - dynamic_cast(_barRenderer)->setOpacity(getOpacity()); + dynamic_cast(_barRenderer)->setColor(getColor()); + dynamic_cast(_barRenderer)->setOpacity(getOpacity()); } - _barRendererTextureSize.width = _barRenderer->getContentSize().width; - _barRendererTextureSize.height = _barRenderer->getContentSize().height; + _barRendererTextureSize = _barRenderer->getContentSize(); switch (_barType) { case LoadingBarTypeLeft: - _barRenderer->setAnchorPoint(Point(0.0f,0.5f)); + _barRenderer->setAnchorPoint(cocos2d::Point(0.0f,0.5f)); if (!_scale9Enabled) { - dynamic_cast(_barRenderer)->setFlippedX(false); + dynamic_cast(_barRenderer)->setFlippedX(false); } break; case LoadingBarTypeRight: - _barRenderer->setAnchorPoint(Point(1.0f,0.5f)); + _barRenderer->setAnchorPoint(cocos2d::Point(1.0f,0.5f)); if (!_scale9Enabled) { - dynamic_cast(_barRenderer)->setFlippedX(true); + dynamic_cast(_barRenderer)->setFlippedX(true); } break; } @@ -182,11 +182,11 @@ void UILoadingBar::setScale9Enabled(bool enabled) _barRenderer = NULL; if (_scale9Enabled) { - _barRenderer = Scale9Sprite::create(); + _barRenderer = cocos2d::extension::Scale9Sprite::create(); } else { - _barRenderer = CCSprite::create(); + _barRenderer = cocos2d::Sprite::create(); } loadTexture(_textureFile.c_str(),_renderBarTexType); _renderer->addChild(_barRenderer); @@ -203,14 +203,14 @@ void UILoadingBar::setScale9Enabled(bool enabled) setCapInsets(_capInsets); } -void UILoadingBar::setCapInsets(const Rect &capInsets) +void UILoadingBar::setCapInsets(const cocos2d::Rect &capInsets) { _capInsets = capInsets; if (!_scale9Enabled) { return; } - dynamic_cast(_barRenderer)->setCapInsets(capInsets); + dynamic_cast(_barRenderer)->setCapInsets(capInsets); } void UILoadingBar::setPercent(int percent) @@ -231,10 +231,10 @@ void UILoadingBar::setPercent(int percent) { case UI_TEX_TYPE_PLIST: { - Sprite* barNode = DYNAMIC_CAST_CCSPRITE; + cocos2d::Sprite* barNode = DYNAMIC_CAST_CCSPRITE; if (barNode) { - Point to = barNode->getTextureRect().origin; + cocos2d::Point to = barNode->getTextureRect().origin; x = to.x; y = to.y; } @@ -249,7 +249,7 @@ void UILoadingBar::setPercent(int percent) } else { - dynamic_cast(_barRenderer)->setTextureRect(Rect(x, y, _barRendererTextureSize.width * res, _barRendererTextureSize.height)); + dynamic_cast(_barRenderer)->setTextureRect(cocos2d::Rect(x, y, _barRendererTextureSize.width * res, _barRendererTextureSize.height)); } } @@ -272,12 +272,12 @@ void UILoadingBar::ignoreContentAdaptWithSize(bool ignore) } } -const Size& UILoadingBar::getContentSize() const +const cocos2d::Size& UILoadingBar::getContentSize() const { return _barRendererTextureSize; } -Node* UILoadingBar::getVirtualRenderer() +cocos2d::Node* UILoadingBar::getVirtualRenderer() { return _barRenderer; } @@ -303,7 +303,7 @@ void UILoadingBar::barRendererScaleChangedWithSize() else { - Size textureSize = _barRenderer->getContentSize(); + cocos2d::Size textureSize = _barRendererTextureSize; if (textureSize.width <= 0.0f || textureSize.height <= 0.0f) { _barRenderer->setScale(1.0f); @@ -318,10 +318,10 @@ void UILoadingBar::barRendererScaleChangedWithSize() switch (_barType) { case LoadingBarTypeLeft: - _barRenderer->setPosition(Point(-_totalLength * 0.5f, 0.0f)); + _barRenderer->setPosition(cocos2d::Point(-_totalLength * 0.5f, 0.0f)); break; case LoadingBarTypeRight: - _barRenderer->setPosition(Point(_totalLength * 0.5f, 0.0f)); + _barRenderer->setPosition(cocos2d::Point(_totalLength * 0.5f, 0.0f)); break; default: break; @@ -331,7 +331,7 @@ void UILoadingBar::barRendererScaleChangedWithSize() void UILoadingBar::setScale9Scale() { float width = (float)(_percent) / 100 * _totalLength; - dynamic_cast(_barRenderer)->setPreferredSize(Size(width, _barRendererTextureSize.height)); + dynamic_cast(_barRenderer)->setPreferredSize(cocos2d::Size(width, _size.height)); } const char* UILoadingBar::getDescription() const @@ -339,4 +339,22 @@ const char* UILoadingBar::getDescription() const return "LoadingBar"; } +UIWidget* UILoadingBar::createCloneInstance() +{ + return UILoadingBar::create(); +} + +void UILoadingBar::copySpecialProperties(UIWidget *widget) +{ + UILoadingBar* loadingBar = dynamic_cast(widget); + if (loadingBar) + { + _prevIgnoreSize = loadingBar->_prevIgnoreSize; + setScale9Enabled(loadingBar->_scale9Enabled); + loadTexture(loadingBar->_textureFile.c_str(), loadingBar->_renderBarTexType); + setCapInsets(loadingBar->_capInsets); + setPercent(loadingBar->_percent); + } +} + } \ No newline at end of file diff --git a/cocos/gui/UILoadingBar.h b/cocos/gui/UILoadingBar.h index b67e7314f5..81c5722b4b 100644 --- a/cocos/gui/UILoadingBar.h +++ b/cocos/gui/UILoadingBar.h @@ -34,7 +34,10 @@ typedef enum LoadingBarTypeLeft, LoadingBarTypeRight }LoadingBarType; - +/** +* @js NA +* @lua NA +*/ class UILoadingBar : public UIWidget { public: @@ -126,6 +129,8 @@ protected: virtual void onSizeChanged(); void setScale9Scale(); void barRendererScaleChangedWithSize(); + virtual UIWidget* createCloneInstance(); + virtual void copySpecialProperties(UIWidget* model); protected: LoadingBarType _barType; int _percent; diff --git a/cocos/gui/UIPageView.cpp b/cocos/gui/UIPageView.cpp index 0fc6558975..8498e79d88 100644 --- a/cocos/gui/UIPageView.cpp +++ b/cocos/gui/UIPageView.cpp @@ -24,8 +24,6 @@ #include "gui/UIPageView.h" - using namespace cocos2d; - namespace gui { UIPageView::UIPageView(): @@ -33,9 +31,8 @@ _curPageIdx(0), _pages(NULL), _touchMoveDir(PAGEVIEW_TOUCHLEFT), _touchStartLocation(0.0f), -_touchEndLocation(0.0f), _touchMoveStartLocation(0.0f), -_movePagePoint(Point::ZERO), +_movePagePoint(cocos2d::Point::ZERO), _leftChild(NULL), _rightChild(NULL), _leftBoundary(0.0f), @@ -52,7 +49,8 @@ _eventSelector(NULL) UIPageView::~UIPageView() { - _pages->release(); + _pages->removeAllObjects(); + CC_SAFE_RELEASE(_pages); } UIPageView* UIPageView::create() @@ -69,12 +67,13 @@ UIPageView* UIPageView::create() bool UIPageView::init() { - if (Layout::init()) + if (UILayout::init()) { - _pages = CCArray::create(); + _pages = cocos2d::Array::create(); _pages->retain(); setClippingEnabled(true); setUpdateEnabled(true); + setTouchEnabled(true); return true; } return false; @@ -86,6 +85,10 @@ void UIPageView::addWidgetToPage(UIWidget *widget, int pageIdx, bool forceCreate { return; } + if (pageIdx < 0) + { + return; + } int pageCount = _pages->count(); if (pageIdx < 0 || pageIdx >= pageCount) { @@ -95,30 +98,29 @@ void UIPageView::addWidgetToPage(UIWidget *widget, int pageIdx, bool forceCreate { CCLOG("pageIdx is %d, it will be added as page id [%d]",pageIdx,pageCount); } - Layout* newPage = createPage(); + UILayout* newPage = createPage(); newPage->addChild(widget); addPage(newPage); } } else { - Layout * page = dynamic_cast(_pages->getObjectAtIndex(pageIdx)); + UILayout * page = dynamic_cast(_pages->getObjectAtIndex(pageIdx)); if (page) { page->addChild(widget); } } - } -Layout* UIPageView::createPage() +UILayout* UIPageView::createPage() { - Layout* newPage = Layout::create(); + UILayout* newPage = UILayout::create(); newPage->setSize(getSize()); return newPage; } -void UIPageView::addPage(Layout* page) +void UIPageView::addPage(UILayout* page) { if (!page) { @@ -132,20 +134,20 @@ void UIPageView::addPage(Layout* page) { return; } - Size pSize = page->getSize(); - Size pvSize = getSize(); + cocos2d::Size pSize = page->getSize(); + cocos2d::Size pvSize = getSize(); if (!pSize.equals(pvSize)) { CCLOG("page size does not match pageview size, it will be force sized!"); page->setSize(pvSize); } - page->setPosition(Point(getPositionXByIndex(_pages->count()), 0)); + page->setPosition(cocos2d::Point(getPositionXByIndex(_pages->count()), 0)); _pages->addObject(page); addChild(page); updateBoundaryPages(); } -void UIPageView::insertPage(Layout* page, int idx) +void UIPageView::insertPage(UILayout* page, int idx) { if (idx < 0) { @@ -172,27 +174,27 @@ void UIPageView::insertPage(Layout* page, int idx) else { _pages->insertObject(page, idx); - page->setPosition(Point(getPositionXByIndex(idx), 0)); + page->setPosition(cocos2d::Point(getPositionXByIndex(idx), 0)); addChild(page); - Size pSize = page->getSize(); - Size pvSize = getSize(); + cocos2d::Size pSize = page->getSize(); + cocos2d::Size pvSize = getSize(); if (!pSize.equals(pvSize)) { CCLOG("page size does not match pageview size, it will be force sized!"); page->setSize(pvSize); } - ccArray* arrayPages = _pages->data; + cocos2d::ccArray* arrayPages = _pages->data; int length = arrayPages->num; for (int i=(idx+1); i(arrayPages->arr[i]); - Point formerPos = behindPage->getPosition(); - behindPage->setPosition(Point(formerPos.x+getSize().width, 0)); + cocos2d::Point formerPos = behindPage->getPosition(); + behindPage->setPosition(cocos2d::Point(formerPos.x+getSize().width, 0)); } updateBoundaryPages(); } } -void UIPageView::removePage(Layout* page) +void UIPageView::removePage(UILayout* page) { if (!page) { @@ -209,7 +211,7 @@ void UIPageView::removePageAtIndex(int index) { return; } - Layout* page = dynamic_cast(_pages->getObjectAtIndex(index)); + UILayout* page = dynamic_cast(_pages->getObjectAtIndex(index)); if (page) { removePage(page); @@ -222,6 +224,7 @@ void UIPageView::updateBoundaryPages() { _leftChild = NULL; _rightChild = NULL; + return; } _leftChild = dynamic_cast(_pages->getObjectAtIndex(0)); _rightChild = dynamic_cast(_pages->getLastObject()); @@ -234,7 +237,7 @@ float UIPageView::getPositionXByIndex(int idx) bool UIPageView::addChild(UIWidget* widget) { - return Layout::addChild(widget); + return UILayout::addChild(widget); } bool UIPageView::removeChild(UIWidget* widget) @@ -242,14 +245,14 @@ bool UIPageView::removeChild(UIWidget* widget) if (_pages->containsObject(widget)) { _pages->removeObject(widget); - return Layout::removeChild(widget); + return UILayout::removeChild(widget); } return false; } void UIPageView::onSizeChanged() { - Layout::onSizeChanged(); + UILayout::onSizeChanged(); _rightBoundary = getSize().width; updateChildrenSize(); updateChildrenPosition(); @@ -262,10 +265,10 @@ void UIPageView::updateChildrenSize() return; } - Size selfSize = getSize(); - for (int i = 0; i < _pages->count(); i++) + cocos2d::Size selfSize = getSize(); + for (unsigned int i=0; i<_pages->count(); i++) { - Layout* page = dynamic_cast(_pages->getObjectAtIndex(i)); + UILayout* page = dynamic_cast(_pages->getObjectAtIndex(i)); page->setSize(selfSize); } } @@ -288,18 +291,18 @@ void UIPageView::updateChildrenPosition() _curPageIdx = pageCount-1; } float pageWidth = getSize().width; - ccArray* arrayPages = _pages->data; + cocos2d::ccArray* arrayPages = _pages->data; for (int i=0; i(arrayPages->arr[i]); - page->setPosition(Point((i-_curPageIdx)*pageWidth, 0)); + UILayout* page = dynamic_cast(arrayPages->arr[i]); + page->setPosition(cocos2d::Point((i-_curPageIdx)*pageWidth, 0)); } } void UIPageView::removeAllChildren() { _pages->removeAllObjects(); - Layout::removeAllChildren(); + UILayout::removeAllChildren(); } void UIPageView::scrollToPage(int idx) @@ -330,13 +333,16 @@ void UIPageView::update(float dt) step = -_autoScrollDistance; _autoScrollDistance = 0.0f; _isAutoScrolling = false; - pageTurningEvent(); } else { _autoScrollDistance += step; } scrollPages(-step); + if (!_isAutoScrolling) + { + pageTurningEvent(); + } break; } break; @@ -348,13 +354,16 @@ void UIPageView::update(float dt) step = _autoScrollDistance; _autoScrollDistance = 0.0f; _isAutoScrolling = false; - pageTurningEvent(); } else { _autoScrollDistance -= step; } scrollPages(step); + if (!_isAutoScrolling) + { + pageTurningEvent(); + } break; } default: @@ -363,14 +372,14 @@ void UIPageView::update(float dt) } } -bool UIPageView::onTouchBegan(const Point &touchPoint) +bool UIPageView::onTouchBegan(const cocos2d::Point &touchPoint) { - bool pass = Layout::onTouchBegan(touchPoint); + bool pass = UILayout::onTouchBegan(touchPoint); handlePressLogic(touchPoint); return pass; } -void UIPageView::onTouchMoved(const Point &touchPoint) +void UIPageView::onTouchMoved(const cocos2d::Point &touchPoint) { _touchMovePos.x = touchPoint.x; _touchMovePos.y = touchPoint.y; @@ -387,15 +396,15 @@ void UIPageView::onTouchMoved(const Point &touchPoint) } } -void UIPageView::onTouchEnded(const Point &touchPoint) +void UIPageView::onTouchEnded(const cocos2d::Point &touchPoint) { - Layout::onTouchEnded(touchPoint); + UILayout::onTouchEnded(touchPoint); handleReleaseLogic(touchPoint); } void UIPageView::movePages(float offset) { - ccArray* arrayPages = _pages->data; + cocos2d::ccArray* arrayPages = _pages->data; int length = arrayPages->num; for (int i = 0; i < length; i++) { @@ -447,21 +456,21 @@ bool UIPageView::scrollPages(float touchOffset) return true; } -void UIPageView::onTouchCancelled(const Point &touchPoint) +void UIPageView::onTouchCancelled(const cocos2d::Point &touchPoint) { - Layout::onTouchCancelled(touchPoint); + UILayout::onTouchCancelled(touchPoint); } -void UIPageView::handlePressLogic(const Point &touchPoint) +void UIPageView::handlePressLogic(const cocos2d::Point &touchPoint) { - Point nsp = _renderer->convertToNodeSpace(touchPoint); + cocos2d::Point nsp = _renderer->convertToNodeSpace(touchPoint); _touchMoveStartLocation = nsp.x; _touchStartLocation = nsp.x; } -void UIPageView::handleMoveLogic(const Point &touchPoint) +void UIPageView::handleMoveLogic(const cocos2d::Point &touchPoint) { - Point nsp = _renderer->convertToNodeSpace(touchPoint); + cocos2d::Point nsp = _renderer->convertToNodeSpace(touchPoint); float offset = 0.0; float moveX = nsp.x; offset = moveX - _touchMoveStartLocation; @@ -477,12 +486,16 @@ void UIPageView::handleMoveLogic(const Point &touchPoint) scrollPages(offset); } -void UIPageView::handleReleaseLogic(const Point &touchPoint) +void UIPageView::handleReleaseLogic(const cocos2d::Point &touchPoint) { + if (_pages->count() <= 0) + { + return; + } UIWidget* curPage = dynamic_cast(_pages->getObjectAtIndex(_curPageIdx)); if (curPage) { - Point curPagePos = curPage->getPosition(); + cocos2d::Point curPagePos = curPage->getPosition(); int pageCount = _pages->count(); float curPageLocation = curPagePos.x; float pageWidth = getSize().width; @@ -516,12 +529,12 @@ void UIPageView::handleReleaseLogic(const Point &touchPoint) } } -void UIPageView::checkChildInfo(int handleState,UIWidget* sender, const Point &touchPoint) +void UIPageView::checkChildInfo(int handleState,UIWidget* sender, const cocos2d::Point &touchPoint) { interceptTouchEvent(handleState, sender, touchPoint); } -void UIPageView::interceptTouchEvent(int handleState, UIWidget *sender, const Point &touchPoint) +void UIPageView::interceptTouchEvent(int handleState, UIWidget *sender, const cocos2d::Point &touchPoint) { switch (handleState) { @@ -556,7 +569,7 @@ void UIPageView::pageTurningEvent() } } -void UIPageView::addEventListener(Object *target, SEL_PageViewEvent selector) +void UIPageView::addEventListener(cocos2d::Object *target, SEL_PageViewEvent selector) { _eventListener = target; _eventSelector = selector; @@ -567,9 +580,39 @@ int UIPageView::getCurPageIndex() const return _curPageIdx; } +cocos2d::Array* UIPageView::getPages() +{ + return _pages; +} + const char* UIPageView::getDescription() const { return "PageView"; } +UIWidget* UIPageView::createCloneInstance() +{ + return UIPageView::create(); +} + +void UIPageView::copyClonedWidgetChildren(UIWidget* model) +{ + cocos2d::ccArray* arrayPages = dynamic_cast(model)->getPages()->data; + int length = arrayPages->num; + for (int i=0; iarr[i]); + addPage(dynamic_cast(page->clone())); + } +} + +void UIPageView::copySpecialProperties(UIWidget *widget) +{ + UIPageView* pageView = dynamic_cast(widget); + if (pageView) + { + UILayout::copySpecialProperties(widget); + } +} + } \ No newline at end of file diff --git a/cocos/gui/UIPageView.h b/cocos/gui/UIPageView.h index e8d3d5192d..ec78df8bbd 100644 --- a/cocos/gui/UIPageView.h +++ b/cocos/gui/UIPageView.h @@ -25,7 +25,7 @@ #ifndef __UIPAGEVIEW_H__ #define __UIPAGEVIEW_H__ -#include "gui/Layout.h" +#include "gui/UILayout.h" #include "gui/UIScrollInterface.h" namespace gui { @@ -43,7 +43,7 @@ typedef enum { PAGEVIEW_TOUCHRIGHT }PVTouchDir; -class UIPageView : public Layout , public UIScrollInterface +class UIPageView : public UILayout , public UIScrollInterface { public: @@ -78,21 +78,21 @@ public: * * @param page page to be added to pageview. */ - void addPage(Layout* page); + void addPage(UILayout* page); /** * Inert a page to pageview. * * @param page page to be added to pageview. */ - void insertPage(Layout* page, int idx); + void insertPage(UILayout* page, int idx); /** * Remove a page of pageview. * * @param page page which will be removed. */ - void removePage(Layout* page); + void removePage(UILayout* page); /** * Remove a page at index of pageview. @@ -115,8 +115,11 @@ public: */ int getCurPageIndex() const; + cocos2d::Array* getPages(); + // event void addEventListener(cocos2d::Object *target, SEL_PageViewEvent selector); + //override "removeChild" method of widget. virtual bool removeChild(UIWidget* widget); @@ -140,15 +143,16 @@ public: virtual void update(float dt); virtual void doLayout(){}; - + /** * Returns the "class name" of widget. */ virtual const char* getDescription() const; + protected: virtual bool addChild(UIWidget* widget); virtual bool init(); - Layout* createPage(); + UILayout* createPage(); float getPositionXByIndex(int idx); void updateBoundaryPages(); virtual void handlePressLogic(const cocos2d::Point &touchPoint); @@ -162,14 +166,15 @@ protected: void updateChildrenSize(); void updateChildrenPosition(); virtual void onSizeChanged(); - - virtual void setClippingEnabled(bool able){Layout::setClippingEnabled(able);}; + virtual UIWidget* createCloneInstance(); + virtual void copySpecialProperties(UIWidget* model); + virtual void copyClonedWidgetChildren(UIWidget* model); + virtual void setClippingEnabled(bool able){UILayout::setClippingEnabled(able);}; protected: int _curPageIdx; cocos2d::Array* _pages; PVTouchDir _touchMoveDir; float _touchStartLocation; - float _touchEndLocation; float _touchMoveStartLocation; cocos2d::Point _movePagePoint; UIWidget* _leftChild; @@ -183,6 +188,7 @@ protected: float _childFocusCancelOffset; cocos2d::Object* _eventListener; SEL_PageViewEvent _eventSelector; + }; } diff --git a/cocos/gui/UIRootWidget.cpp b/cocos/gui/UIRootWidget.cpp index f6bf7359f0..0b7581287f 100644 --- a/cocos/gui/UIRootWidget.cpp +++ b/cocos/gui/UIRootWidget.cpp @@ -24,8 +24,6 @@ #include "gui/UIRootWidget.h" - using namespace cocos2d; - namespace gui { UIRootWidget::UIRootWidget() @@ -50,9 +48,9 @@ UIRootWidget* UIRootWidget::create() bool UIRootWidget::init() { - if (Layout::init()) + if (UILayout::init()) { - setSize(Director::getInstance()->getWinSize()); + setSize(cocos2d::Director::getInstance()->getWinSize()); return true; } return false; diff --git a/cocos/gui/UIRootWidget.h b/cocos/gui/UIRootWidget.h index f4bff4955f..dc93ac4645 100644 --- a/cocos/gui/UIRootWidget.h +++ b/cocos/gui/UIRootWidget.h @@ -25,11 +25,15 @@ #ifndef __UIROOTWIDGET_H__ #define __UIROOTWIDGET_H__ -#include "gui/Layout.h" +#include "gui/UILayout.h" namespace gui { - -class UIRootWidget : public Layout + +/** +* @js NA +* @lua NA +*/ +class UIRootWidget : public UILayout { public: /** diff --git a/cocos/gui/UIScrollInterface.h b/cocos/gui/UIScrollInterface.h index 2e835982fd..dfff4cd66c 100644 --- a/cocos/gui/UIScrollInterface.h +++ b/cocos/gui/UIScrollInterface.h @@ -36,7 +36,6 @@ protected: virtual void handleMoveLogic(const cocos2d::Point &touchPoint) = 0; virtual void handleReleaseLogic(const cocos2d::Point &touchPoint) = 0; virtual void interceptTouchEvent(int handleState, UIWidget* sender, const cocos2d::Point &touchPoint) = 0; -// virtual bool isInScrollDegreeRange(UIWidget* widget) = 0; }; } diff --git a/cocos/gui/UIScrollView.cpp b/cocos/gui/UIScrollView.cpp index bdb3d45a21..1270c78682 100644 --- a/cocos/gui/UIScrollView.cpp +++ b/cocos/gui/UIScrollView.cpp @@ -23,34 +23,52 @@ ****************************************************************************/ #include "gui/UIScrollView.h" -#include "gui/UILayer.h" - - using namespace cocos2d; namespace gui { +#define AUTOSCROLLMAXSPEED 1000.0f + +const cocos2d::Point SCROLLDIR_UP = cocos2d::Point(0.0f, 1.0f); +const cocos2d::Point SCROLLDIR_DOWN = cocos2d::Point(0.0f, -1.0f); +const cocos2d::Point SCROLLDIR_LEFT = cocos2d::Point(-1.0f, 0.0f); +const cocos2d::Point SCROLLDIR_RIGHT = cocos2d::Point(1.0f, 0.0f); + UIScrollView::UIScrollView(): _innerContainer(NULL), _direction(SCROLLVIEW_DIR_VERTICAL), -_moveDirection(SCROLLVIEW_MOVE_DIR_NONE), -_touchStartLocation(0.0f), -_touchEndLocation(0.0f), -_touchMoveStartLocation(0.0f), +_touchBeganPoint(cocos2d::Point::ZERO), +_touchMovedPoint(cocos2d::Point::ZERO), +_touchEndedPoint(cocos2d::Point::ZERO), +_touchMovingPoint(cocos2d::Point::ZERO), +_autoScrollDir(cocos2d::Point::ZERO), _topBoundary(0.0f), _bottomBoundary(0.0f), _leftBoundary(0.0f), _rightBoundary(0.0f), -_topEnd(false), -_bottomEnd(false), -_leftEnd(false), -_rightEnd(false), +_bounceTopBoundary(0.0f), +_bounceBottomBoundary(0.0f), +_bounceLeftBoundary(0.0f), +_bounceRightBoundary(0.0f), _autoScroll(false), +_autoScrollAddUpTime(0.0f), _autoScrollOriginalSpeed(0.0f), -_autoScrollAcceleration(600.0f), +_autoScrollAcceleration(-1000.0f), +_isAutoScrollSpeedAttenuated(false), +_needCheckAutoScrollDestination(false), +_autoScrollDestination(cocos2d::Point::ZERO), _bePressed(false), _slidTime(0.0f), -_moveChildPoint(Point::ZERO), +_moveChildPoint(cocos2d::Point::ZERO), _childFocusCancelOffset(5.0f), +_leftBounceNeeded(false), +_topBounceNeeded(false), +_rightBounceNeeded(false), +_bottomBounceNeeded(false), +_bounceEnabled(false), +_bouncing(false), +_bounceDir(cocos2d::Point::ZERO), +_bounceOriginalSpeed(0.0f), +_inertiaScrollEnabled(true), _eventListener(NULL), _eventSelector(NULL) { @@ -73,22 +91,9 @@ UIScrollView* UIScrollView::create() return NULL; } -void UIScrollView::releaseResoures() -{ - setUpdateEnabled(false); - removeAllChildren(); - _renderer->removeAllChildrenWithCleanup(true); - _renderer->removeFromParentAndCleanup(true); - _renderer->release(); - - Layout::removeChild(_innerContainer); - - _children->release(); -} - bool UIScrollView::init() { - if (Layout::init()) + if (UILayout::init()) { setUpdateEnabled(true); setTouchEnabled(true); @@ -101,29 +106,36 @@ bool UIScrollView::init() void UIScrollView::initRenderer() { - Layout::initRenderer(); - _innerContainer = Layout::create(); - Layout::addChild(_innerContainer); + UILayout::initRenderer(); + _innerContainer = UILayout::create(); + UILayout::addChild(_innerContainer); } void UIScrollView::onSizeChanged() { - Layout::onSizeChanged(); + UILayout::onSizeChanged(); _topBoundary = _size.height; _rightBoundary = _size.width; - Size innerSize = _innerContainer->getSize(); + float bounceBoundaryParameterX = _size.width / 3.0f; + float bounceBoundaryParameterY = _size.height / 3.0f; + _bounceTopBoundary = _size.height - bounceBoundaryParameterY; + _bounceBottomBoundary = bounceBoundaryParameterY; + _bounceLeftBoundary = bounceBoundaryParameterX; + _bounceRightBoundary = _size.width - bounceBoundaryParameterX; + cocos2d::Size innerSize = _innerContainer->getSize(); float orginInnerSizeWidth = innerSize.width; float orginInnerSizeHeight = innerSize.height; float innerSizeWidth = MAX(orginInnerSizeWidth, _size.width); float innerSizeHeight = MAX(orginInnerSizeHeight, _size.height); - _innerContainer->setSize(Size(innerSizeWidth, innerSizeHeight)); - _innerContainer->setPosition(Point(0, _size.height - _innerContainer->getSize().height)); + _innerContainer->setSize(cocos2d::Size(innerSizeWidth, innerSizeHeight)); + _innerContainer->setPosition(cocos2d::Point(0, _size.height - _innerContainer->getSize().height)); } -void UIScrollView::setInnerContainerSize(const Size &size) +void UIScrollView::setInnerContainerSize(const cocos2d::Size &size) { float innerSizeWidth = _size.width; float innerSizeHeight = _size.height; + cocos2d::Size originalInnerSize = _innerContainer->getSize(); if (size.width < _size.width) { CCLOG("Inner width <= scrollview width, it will be force sized!"); @@ -140,11 +152,45 @@ void UIScrollView::setInnerContainerSize(const Size &size) { innerSizeHeight = size.height; } - _innerContainer->setSize(Size(innerSizeWidth, innerSizeHeight)); - _innerContainer->setPosition(Point(0, _size.height - _innerContainer->getSize().height)); + _innerContainer->setSize(cocos2d::Size(innerSizeWidth, innerSizeHeight)); + + switch (_direction) + { + case SCROLLVIEW_DIR_VERTICAL: + { + cocos2d::Size newInnerSize = _innerContainer->getSize(); + float offset = originalInnerSize.height - newInnerSize.height; + scrollChildren(0.0f, offset); + break; + } + case SCROLLVIEW_DIR_HORIZONTAL: + { + if (_innerContainer->getRightInParent() <= _size.width) + { + cocos2d::Size newInnerSize = _innerContainer->getSize(); + float offset = originalInnerSize.width - newInnerSize.width; + scrollChildren(offset, 0.0f); + } + break; + } + case SCROLLVIEW_DIR_BOTH: + { + cocos2d::Size newInnerSize = _innerContainer->getSize(); + float offsetY = originalInnerSize.height - newInnerSize.height; + float offsetX = 0.0f; + if (_innerContainer->getRightInParent() <= _size.width) + { + offsetX = originalInnerSize.width - newInnerSize.width; + } + scrollChildren(offsetX, offsetY); + break; + } + default: + break; + } } -const Size& UIScrollView::getInnerContainerSize() const +const cocos2d::Size& UIScrollView::getInnerContainerSize() const { return _innerContainer->getSize(); } @@ -164,378 +210,1192 @@ bool UIScrollView::removeChild(UIWidget* child) return _innerContainer->removeChild(child); } -Array* UIScrollView::getChildren() +cocos2d::Array* UIScrollView::getChildren() { return _innerContainer->getChildren(); } -void UIScrollView::moveChildren(float offset) +void UIScrollView::moveChildren(float offsetX, float offsetY) { - switch (_direction) - { - case SCROLLVIEW_DIR_VERTICAL: // vertical - { - _moveChildPoint.x = _innerContainer->getPosition().x; - _moveChildPoint.y = _innerContainer->getPosition().y + offset; - _innerContainer->setPosition(_moveChildPoint); - break; - } - case SCROLLVIEW_DIR_HORIZONTAL: // horizontal - { - _moveChildPoint.x = _innerContainer->getPosition().x + offset; - _moveChildPoint.y = _innerContainer->getPosition().y; - _innerContainer->setPosition(_moveChildPoint); - break; - } - default: - break; - } + _moveChildPoint = _innerContainer->getPosition() + cocos2d::Point(offsetX, offsetY); + _innerContainer->setPosition(_moveChildPoint); } void UIScrollView::autoScrollChildren(float dt) { - switch (_direction) + float lastTime = _autoScrollAddUpTime; + _autoScrollAddUpTime += dt; + if (_isAutoScrollSpeedAttenuated) { - case SCROLLVIEW_DIR_VERTICAL: // vertical - switch (_moveDirection) - { - case SCROLLVIEW_MOVE_DIR_UP: // up - { - float curDis = getCurAutoScrollDistance(dt); - if (curDis <= 0) - { - curDis = 0; - stopAutoScrollChildren(); - } - if (!scrollChildren(curDis)) - { - stopAutoScrollChildren(); - } - } - break; - case SCROLLVIEW_MOVE_DIR_DOWN: // down - { - float curDis = getCurAutoScrollDistance(dt); - if (curDis <= 0) - { - curDis = 0; - stopAutoScrollChildren(); - } - if (!scrollChildren(-curDis)) - { - stopAutoScrollChildren(); - } - } - break; - default: - break; - } - break; - - case SCROLLVIEW_DIR_HORIZONTAL: // horizontal - switch (_moveDirection) + float nowSpeed = _autoScrollOriginalSpeed + _autoScrollAcceleration * _autoScrollAddUpTime; + if (nowSpeed <= 0.0f) + { + stopAutoScrollChildren(); + checkNeedBounce(); + } + else + { + float timeParam = lastTime * 2 + dt; + float offset = (_autoScrollOriginalSpeed + _autoScrollAcceleration * timeParam * 0.5f) * dt; + float offsetX = offset * _autoScrollDir.x; + float offsetY = offset * _autoScrollDir.y; + if (!scrollChildren(offsetX, offsetY)) { - case SCROLLVIEW_MOVE_DIR_LEFT: // left - { - float curDis = getCurAutoScrollDistance(dt); - if (curDis <= 0) - { - curDis = 0; - stopAutoScrollChildren(); - } - if (!scrollChildren(-curDis)) - { - stopAutoScrollChildren(); - } - } - break; - - case SCROLLVIEW_MOVE_DIR_RIGHT: // right - { - float curDis = getCurAutoScrollDistance(dt); - if (curDis <= 0) - { - curDis = 0; - stopAutoScrollChildren(); - } - if (!scrollChildren(curDis)) - { - stopAutoScrollChildren(); - } - } - break; - - default: - break; + stopAutoScrollChildren(); + checkNeedBounce(); } - break; - - default: - break; + } + } + else + { + if (_needCheckAutoScrollDestination) + { + float xOffset = _autoScrollDir.x * dt * _autoScrollOriginalSpeed; + float yOffset = _autoScrollDir.y * dt * _autoScrollOriginalSpeed; + bool notDone = checkCustomScrollDestination(&xOffset, &yOffset); + bool scrollCheck = scrollChildren(xOffset, yOffset); + if (!notDone || !scrollCheck) + { + stopAutoScrollChildren(); + checkNeedBounce(); + } + } + else + { + if (!scrollChildren(_autoScrollDir.x * dt * _autoScrollOriginalSpeed, _autoScrollDir.y * dt * _autoScrollOriginalSpeed)) + { + stopAutoScrollChildren(); + checkNeedBounce(); + } + } } } -void UIScrollView::startAutoScrollChildren(float v) +void UIScrollView::bounceChildren(float dt) { + if (_bounceOriginalSpeed <= 0.0f) + { + stopBounceChildren(); + } + if (!bounceScrollChildren(_bounceDir.x * dt * _bounceOriginalSpeed, _bounceDir.y * dt * _bounceOriginalSpeed)) + { + stopBounceChildren(); + } +} + +bool UIScrollView::checkNeedBounce() +{ + if (!_bounceEnabled) + { + return false; + } + checkBounceBoundary(); + if (_topBounceNeeded || _bottomBounceNeeded || _leftBounceNeeded || _rightBounceNeeded) + { + if (_topBounceNeeded && _leftBounceNeeded) + { + cocos2d::Point scrollVector = cocos2d::Point(0.0f, _size.height) - cocos2d::Point(_innerContainer->getLeftInParent(), _innerContainer->getTopInParent()); + float orSpeed = scrollVector.getLength()/(0.2f); + _bounceDir = scrollVector.normalize(); + startBounceChildren(orSpeed); + } + else if (_topBounceNeeded && _rightBounceNeeded) + { + cocos2d::Point scrollVector = cocos2d::Point(_size.width, _size.height) - cocos2d::Point(_innerContainer->getRightInParent(), _innerContainer->getTopInParent()); + float orSpeed = scrollVector.getLength()/(0.2f); + _bounceDir = scrollVector.normalize(); + startBounceChildren(orSpeed); + } + else if (_bottomBounceNeeded && _leftBounceNeeded) + { + cocos2d::Point scrollVector = cocos2d::Point::ZERO - cocos2d::Point(_innerContainer->getLeftInParent(), _innerContainer->getBottomInParent()); + float orSpeed = scrollVector.getLength()/(0.2f); + _bounceDir = scrollVector.normalize(); + startBounceChildren(orSpeed); + } + else if (_bottomBounceNeeded && _rightBounceNeeded) + { + cocos2d::Point scrollVector = cocos2d::Point(_size.width, 0.0f) - cocos2d::Point(_innerContainer->getRightInParent(), _innerContainer->getBottomInParent()); + float orSpeed = scrollVector.getLength()/(0.2f); + _bounceDir = scrollVector.normalize(); + startBounceChildren(orSpeed); + } + else if (_topBounceNeeded) + { + cocos2d::Point scrollVector = cocos2d::Point(0.0f, _size.height) - cocos2d::Point(0.0f, _innerContainer->getTopInParent()); + float orSpeed = scrollVector.getLength()/(0.2f); + _bounceDir = scrollVector.normalize(); + startBounceChildren(orSpeed); + } + else if (_bottomBounceNeeded) + { + cocos2d::Point scrollVector = cocos2d::Point::ZERO - cocos2d::Point(0.0f, _innerContainer->getBottomInParent()); + float orSpeed = scrollVector.getLength()/(0.2f); + _bounceDir = scrollVector.normalize(); + startBounceChildren(orSpeed); + } + else if (_leftBounceNeeded) + { + cocos2d::Point scrollVector = cocos2d::Point::ZERO - cocos2d::Point(_innerContainer->getLeftInParent(), 0.0f); + float orSpeed = scrollVector.getLength()/(0.2f); + _bounceDir = scrollVector.normalize(); + startBounceChildren(orSpeed); + } + else if (_rightBounceNeeded) + { + cocos2d::Point scrollVector = cocos2d::Point(_size.width, 0.0f) - cocos2d::Point(_innerContainer->getRightInParent(), 0.0f); + float orSpeed = scrollVector.getLength()/(0.2f); + _bounceDir = scrollVector.normalize(); + startBounceChildren(orSpeed); + } + return true; + } + return false; +} + +void UIScrollView::checkBounceBoundary() +{ + float icBottomPos = _innerContainer->getBottomInParent(); + if (icBottomPos > _bottomBoundary) + { + scrollToBottomEvent(); + _bottomBounceNeeded = true; + } + else + { + _bottomBounceNeeded = false; + } + float icTopPos = _innerContainer->getTopInParent(); + if (icTopPos < _topBoundary) + { + scrollToTopEvent(); + _topBounceNeeded = true; + } + else + { + _topBounceNeeded = false; + } + float icRightPos = _innerContainer->getRightInParent(); + if (icRightPos < _rightBoundary) + { + scrollToRightEvent(); + _rightBounceNeeded = true; + } + else + { + _rightBounceNeeded = false; + } + float icLeftPos = _innerContainer->getLeftInParent(); + if (icLeftPos > _leftBoundary) + { + scrollToLeftEvent(); + _leftBounceNeeded = true; + } + else + { + _leftBounceNeeded = false; + } +} + +void UIScrollView::startBounceChildren(float v) +{ + _bounceOriginalSpeed = v; + _bouncing = true; +} + +void UIScrollView::stopBounceChildren() +{ + _bouncing = false; + _bounceOriginalSpeed = 0.0f; + _leftBounceNeeded = false; + _rightBounceNeeded = false; + _topBounceNeeded = false; + _bottomBounceNeeded = false; +} + +void UIScrollView::startAutoScrollChildrenWithOriginalSpeed(const cocos2d::Point& dir, float v, bool attenuated, float acceleration) +{ + stopAutoScrollChildren(); + _autoScrollDir = dir; + _isAutoScrollSpeedAttenuated = attenuated; _autoScrollOriginalSpeed = v; _autoScroll = true; + _autoScrollAcceleration = acceleration; +} + +void UIScrollView::startAutoScrollChildrenWithDestination(const cocos2d::Point& des, float time, bool attenuated) +{ + _needCheckAutoScrollDestination = false; + _autoScrollDestination = des; + cocos2d::Point dis = des - _innerContainer->getPosition(); + cocos2d::Point dir = dis.normalize(); + float orSpeed = 0.0f; + float acceleration = -1000.0f; + if (attenuated) + { + acceleration = (-(2 * dis.getLength())) / (time * time); + orSpeed = 2 * dis.getLength() / time; + } + else + { + _needCheckAutoScrollDestination = true; + orSpeed = dis.getLength() / time; + } + startAutoScrollChildrenWithOriginalSpeed(dir, orSpeed, attenuated, acceleration); +} + +void UIScrollView::jumpToDestination(const cocos2d::Point &des) +{ + float finalOffsetX = des.x; + float finalOffsetY = des.y; + switch (_direction) + { + case SCROLLVIEW_DIR_VERTICAL: + if (des.y <= 0) + { + finalOffsetY = MAX(des.y, _size.height - _innerContainer->getSize().height); + } + break; + case SCROLLVIEW_DIR_HORIZONTAL: + if (des.x <= 0) + { + finalOffsetX = MAX(des.x, _size.width - _innerContainer->getSize().width); + } + break; + case SCROLLVIEW_DIR_BOTH: + if (des.y <= 0) + { + finalOffsetY = MAX(des.y, _size.height - _innerContainer->getSize().height); + } + if (des.x <= 0) + { + finalOffsetX = MAX(des.x, _size.width - _innerContainer->getSize().width); + } + break; + default: + break; + } + _innerContainer->setPosition(cocos2d::Point(finalOffsetX, finalOffsetY)); } void UIScrollView::stopAutoScrollChildren() { _autoScroll = false; _autoScrollOriginalSpeed = 0.0f; + _autoScrollAddUpTime = 0.0f; } -float UIScrollView::getCurAutoScrollDistance(float time) +bool UIScrollView::bounceScrollChildren(float touchOffsetX, float touchOffsetY) { - float dt = time; - _autoScrollOriginalSpeed -= _autoScrollAcceleration*dt; - return _autoScrollOriginalSpeed*dt; + bool scrollenabled = true; + if (touchOffsetX > 0.0f && touchOffsetY > 0.0f) //first quadrant //bounce to top-right + { + float realOffsetX = touchOffsetX; + float realOffsetY = touchOffsetY; + float icRightPos = _innerContainer->getRightInParent(); + if (icRightPos + realOffsetX >= _rightBoundary) + { + realOffsetX = _rightBoundary - icRightPos; + bounceRightEvent(); + scrollenabled = false; + } + float icTopPos = _innerContainer->getTopInParent(); + if (icTopPos + touchOffsetY >= _topBoundary) + { + realOffsetY = _topBoundary - icTopPos; + bounceTopEvent(); + scrollenabled = false; + } + moveChildren(realOffsetX, realOffsetY); + } + else if(touchOffsetX < 0.0f && touchOffsetY > 0.0f) //second quadrant //bounce to top-left + { + float realOffsetX = touchOffsetX; + float realOffsetY = touchOffsetY; + float icLefrPos = _innerContainer->getLeftInParent(); + if (icLefrPos + realOffsetX <= _leftBoundary) + { + realOffsetX = _leftBoundary - icLefrPos; + bounceLeftEvent(); + scrollenabled = false; + } + float icTopPos = _innerContainer->getTopInParent(); + if (icTopPos + touchOffsetY >= _topBoundary) + { + realOffsetY = _topBoundary - icTopPos; + bounceTopEvent(); + scrollenabled = false; + } + moveChildren(realOffsetX, realOffsetY); + } + else if (touchOffsetX < 0.0f && touchOffsetY < 0.0f) //third quadrant //bounce to bottom-left + { + float realOffsetX = touchOffsetX; + float realOffsetY = touchOffsetY; + float icLefrPos = _innerContainer->getLeftInParent(); + if (icLefrPos + realOffsetX <= _leftBoundary) + { + realOffsetX = _leftBoundary - icLefrPos; + bounceLeftEvent(); + scrollenabled = false; + } + float icBottomPos = _innerContainer->getBottomInParent(); + if (icBottomPos + touchOffsetY <= _bottomBoundary) + { + realOffsetY = _bottomBoundary - icBottomPos; + bounceBottomEvent(); + scrollenabled = false; + } + moveChildren(realOffsetX, realOffsetY); + } + else if (touchOffsetX > 0.0f && touchOffsetY < 0.0f) //forth quadrant //bounce to bottom-right + { + float realOffsetX = touchOffsetX; + float realOffsetY = touchOffsetY; + float icRightPos = _innerContainer->getRightInParent(); + if (icRightPos + realOffsetX >= _rightBoundary) + { + realOffsetX = _rightBoundary - icRightPos; + bounceRightEvent(); + scrollenabled = false; + } + float icBottomPos = _innerContainer->getBottomInParent(); + if (icBottomPos + touchOffsetY <= _bottomBoundary) + { + realOffsetY = _bottomBoundary - icBottomPos; + bounceBottomEvent(); + scrollenabled = false; + } + moveChildren(realOffsetX, realOffsetY); + } + else if (touchOffsetX == 0.0f && touchOffsetY > 0.0f) // bounce to top + { + float realOffsetY = touchOffsetY; + float icTopPos = _innerContainer->getTopInParent(); + if (icTopPos + touchOffsetY >= _topBoundary) + { + realOffsetY = _topBoundary - icTopPos; + bounceTopEvent(); + scrollenabled = false; + } + moveChildren(0.0f, realOffsetY); + } + else if (touchOffsetX == 0.0f && touchOffsetY < 0.0f) //bounce to bottom + { + float realOffsetY = touchOffsetY; + float icBottomPos = _innerContainer->getBottomInParent(); + if (icBottomPos + touchOffsetY <= _bottomBoundary) + { + realOffsetY = _bottomBoundary - icBottomPos; + bounceBottomEvent(); + scrollenabled = false; + } + moveChildren(0.0f, realOffsetY); + } + else if (touchOffsetX > 0.0f && touchOffsetY == 0.0f) //bounce to right + { + float realOffsetX = touchOffsetX; + float icRightPos = _innerContainer->getRightInParent(); + if (icRightPos + realOffsetX >= _rightBoundary) + { + realOffsetX = _rightBoundary - icRightPos; + bounceRightEvent(); + scrollenabled = false; + } + moveChildren(realOffsetX, 0.0f); + } + else if (touchOffsetX < 0.0f && touchOffsetY == 0.0f) //bounce to left + { + float realOffsetX = touchOffsetX; + float icLeftPos = _innerContainer->getLeftInParent(); + if (icLeftPos + realOffsetX <= _leftBoundary) + { + realOffsetX = _leftBoundary - icLeftPos; + bounceLeftEvent(); + scrollenabled = false; + } + moveChildren(realOffsetX, 0.0f); + } + return scrollenabled; } -bool UIScrollView::scrollChildren(float touchOffset) -{ - float realOffset = touchOffset; - +bool UIScrollView::checkCustomScrollDestination(float* touchOffsetX, float* touchOffsetY) +{ + bool scrollenabled = true; switch (_direction) { case SCROLLVIEW_DIR_VERTICAL: // vertical - switch (_moveDirection) - { - case SCROLLVIEW_MOVE_DIR_UP: // up - { - float icBottomPos = _innerContainer->getBottomInParent(); - if (icBottomPos + touchOffset >= _bottomBoundary) - { - realOffset = _bottomBoundary - icBottomPos; - moveChildren(realOffset); - _bottomEnd = true; - scrollToBottomEvent(); - return false; - } - break; - } - case SCROLLVIEW_MOVE_DIR_DOWN: // down - { - float icTopPos = _innerContainer->getTopInParent(); - if (icTopPos + touchOffset <= _topBoundary) - { - realOffset = _topBoundary - icTopPos; - moveChildren(realOffset); - _topEnd = true; - scrollToTopEvent(); - return false; - } - break; - } - default: - break; - } - moveChildren(realOffset); - _topEnd = false; - _bottomEnd = false; - return true; - break; - case SCROLLVIEW_DIR_HORIZONTAL: // horizontal - switch (_moveDirection) + { + if (_autoScrollDir.y > 0) { - case SCROLLVIEW_MOVE_DIR_LEFT: // left + float icBottomPos = _innerContainer->getBottomInParent(); + if (icBottomPos + *touchOffsetY >= _autoScrollDestination.y) { - float icRightPos = _innerContainer->getRightInParent(); - if (icRightPos + touchOffset <= _rightBoundary) - { - realOffset = _rightBoundary - icRightPos; - moveChildren(realOffset); - _rightEnd = true; - scrollToRightEvent(); - return false; - } - break; + *touchOffsetY = _autoScrollDestination.y - icBottomPos; + scrollenabled = false; + } + } + else + { + float icBottomPos = _innerContainer->getBottomInParent(); + if (icBottomPos + *touchOffsetY <= _autoScrollDestination.y) + { + *touchOffsetY = _autoScrollDestination.y - icBottomPos; + scrollenabled = false; } - case SCROLLVIEW_MOVE_DIR_RIGHT: // right - { - float icLeftPos = _innerContainer->getLeftInParent(); - if (icLeftPos + touchOffset >= _leftBoundary) - { - realOffset = _leftBoundary - icLeftPos; - moveChildren(realOffset); - _leftEnd = true; - scrollToLeftEvent(); - return false; - } - break; - } - default: - break; } - moveChildren(realOffset); - _leftEnd = false; - _rightEnd = false; - return true; break; - + } + case SCROLLVIEW_DIR_HORIZONTAL: // horizontal + { + if (_autoScrollDir.x > 0) + { + float icLeftPos = _innerContainer->getLeftInParent(); + if (icLeftPos + *touchOffsetX >= _autoScrollDestination.x) + { + *touchOffsetX = _autoScrollDestination.x - icLeftPos; + scrollenabled = false; + } + } + else + { + float icLeftPos = _innerContainer->getLeftInParent(); + if (icLeftPos + *touchOffsetX <= _autoScrollDestination.x) + { + *touchOffsetX = _autoScrollDestination.x - icLeftPos; + scrollenabled = false; + } + } + break; + } + case SCROLLVIEW_DIR_BOTH: + { + if (*touchOffsetX > 0.0f && *touchOffsetY > 0.0f) // up right + { + float icLeftPos = _innerContainer->getLeftInParent(); + if (icLeftPos + *touchOffsetX >= _autoScrollDestination.x) + { + *touchOffsetX = _autoScrollDestination.x - icLeftPos; + scrollenabled = false; + } + float icBottomPos = _innerContainer->getBottomInParent(); + if (icBottomPos + *touchOffsetY >= _autoScrollDestination.y) + { + *touchOffsetY = _autoScrollDestination.y - icBottomPos; + scrollenabled = false; + } + } + else if (*touchOffsetX < 0.0f && *touchOffsetY > 0.0f) // up left + { + float icRightPos = _innerContainer->getRightInParent(); + if (icRightPos + *touchOffsetX <= _autoScrollDestination.x) + { + *touchOffsetX = _autoScrollDestination.x - icRightPos; + scrollenabled = false; + } + float icBottomPos = _innerContainer->getBottomInParent(); + if (icBottomPos + *touchOffsetY >= _autoScrollDestination.y) + { + *touchOffsetY = _autoScrollDestination.y - icBottomPos; + scrollenabled = false; + } + } + else if (*touchOffsetX < 0.0f && *touchOffsetY < 0.0f) // down left + { + float icRightPos = _innerContainer->getRightInParent(); + if (icRightPos + *touchOffsetX <= _autoScrollDestination.x) + { + *touchOffsetX = _autoScrollDestination.x - icRightPos; + scrollenabled = false; + } + float icTopPos = _innerContainer->getTopInParent(); + if (icTopPos + *touchOffsetY <= _autoScrollDestination.y) + { + *touchOffsetY = _autoScrollDestination.y - icTopPos; + scrollenabled = false; + } + } + else if (*touchOffsetX > 0.0f && *touchOffsetY < 0.0f) // down right + { + float icLeftPos = _innerContainer->getLeftInParent(); + if (icLeftPos + *touchOffsetX >= _autoScrollDestination.x) + { + *touchOffsetX = _autoScrollDestination.x - icLeftPos; + scrollenabled = false; + } + float icTopPos = _innerContainer->getTopInParent(); + if (icTopPos + *touchOffsetY <= _autoScrollDestination.y) + { + *touchOffsetY = _autoScrollDestination.y - icTopPos; + scrollenabled = false; + } + } + else if (*touchOffsetX == 0.0f && *touchOffsetY > 0.0f) // up + { + float icBottomPos = _innerContainer->getBottomInParent(); + if (icBottomPos + *touchOffsetY >= _autoScrollDestination.y) + { + *touchOffsetY = _autoScrollDestination.y - icBottomPos; + scrollenabled = false; + } + } + else if (*touchOffsetX < 0.0f && *touchOffsetY == 0.0f) // left + { + float icRightPos = _innerContainer->getRightInParent(); + if (icRightPos + *touchOffsetX <= _autoScrollDestination.x) + { + *touchOffsetX = _autoScrollDestination.x - icRightPos; + scrollenabled = false; + } + } + else if (*touchOffsetX == 0.0f && *touchOffsetY < 0.0f) // down + { + float icTopPos = _innerContainer->getTopInParent(); + if (icTopPos + *touchOffsetY <= _autoScrollDestination.y) + { + *touchOffsetY = _autoScrollDestination.y - icTopPos; + scrollenabled = false; + } + } + else if (*touchOffsetX > 0.0f && *touchOffsetY == 0.0f) // right + { + float icLeftPos = _innerContainer->getLeftInParent(); + if (icLeftPos + *touchOffsetX >= _autoScrollDestination.x) + { + *touchOffsetX = _autoScrollDestination.x - icLeftPos; + scrollenabled = false; + } + } + break; + } default: break; } - - return false; + return scrollenabled; } -void UIScrollView::scrollToBottom() +bool UIScrollView::scrollChildren(float touchOffsetX, float touchOffsetY) { - _moveDirection = SCROLLVIEW_MOVE_DIR_UP; // up - scrollChildren(_innerContainer->getSize().height); + bool scrollenabled = true; + scrollingEvent(); + switch (_direction) + { + case SCROLLVIEW_DIR_VERTICAL: // vertical + { + float realOffset = touchOffsetY; + if (_bounceEnabled) + { + float icBottomPos = _innerContainer->getBottomInParent(); + if (icBottomPos + touchOffsetY >= _bounceBottomBoundary) + { + realOffset = _bounceBottomBoundary - icBottomPos; + scrollToBottomEvent(); + scrollenabled = false; + } + float icTopPos = _innerContainer->getTopInParent(); + if (icTopPos + touchOffsetY <= _bounceTopBoundary) + { + realOffset = _bounceTopBoundary - icTopPos; + scrollToTopEvent(); + scrollenabled = false; + } + } + else + { + float icBottomPos = _innerContainer->getBottomInParent(); + if (icBottomPos + touchOffsetY >= _bottomBoundary) + { + realOffset = _bottomBoundary - icBottomPos; + scrollToBottomEvent(); + scrollenabled = false; + } + float icTopPos = _innerContainer->getTopInParent(); + if (icTopPos + touchOffsetY <= _topBoundary) + { + realOffset = _topBoundary - icTopPos; + scrollToTopEvent(); + scrollenabled = false; + } + } + moveChildren(0.0f, realOffset); + break; + } + case SCROLLVIEW_DIR_HORIZONTAL: // horizontal + { + float realOffset = touchOffsetX; + if (_bounceEnabled) + { + float icRightPos = _innerContainer->getRightInParent(); + if (icRightPos + touchOffsetX <= _bounceRightBoundary) + { + realOffset = _bounceRightBoundary - icRightPos; + scrollToRightEvent(); + scrollenabled = false; + } + float icLeftPos = _innerContainer->getLeftInParent(); + if (icLeftPos + touchOffsetX >= _bounceLeftBoundary) + { + realOffset = _bounceLeftBoundary - icLeftPos; + scrollToLeftEvent(); + scrollenabled = false; + } + } + else + { + float icRightPos = _innerContainer->getRightInParent(); + if (icRightPos + touchOffsetX <= _rightBoundary) + { + realOffset = _rightBoundary - icRightPos; + scrollToRightEvent(); + scrollenabled = false; + } + float icLeftPos = _innerContainer->getLeftInParent(); + if (icLeftPos + touchOffsetX >= _leftBoundary) + { + realOffset = _leftBoundary - icLeftPos; + scrollToLeftEvent(); + scrollenabled = false; + } + } + moveChildren(realOffset, 0.0f); + break; + } + case SCROLLVIEW_DIR_BOTH: + { + float realOffsetX = touchOffsetX; + float realOffsetY = touchOffsetY; + if (_bounceEnabled) + { + if (touchOffsetX > 0.0f && touchOffsetY > 0.0f) // up right + { + float icLeftPos = _innerContainer->getLeftInParent(); + if (icLeftPos + touchOffsetX >= _bounceLeftBoundary) + { + realOffsetX = _bounceLeftBoundary - icLeftPos; + scrollToLeftEvent(); + scrollenabled = false; + } + float icBottomPos = _innerContainer->getBottomInParent(); + if (icBottomPos + touchOffsetY >= _bounceBottomBoundary) + { + realOffsetY = _bounceBottomBoundary - icBottomPos; + scrollToBottomEvent(); + scrollenabled = false; + } + } + else if (touchOffsetX < 0.0f && touchOffsetY > 0.0f) // up left + { + float icRightPos = _innerContainer->getRightInParent(); + if (icRightPos + touchOffsetX <= _bounceRightBoundary) + { + realOffsetX = _bounceRightBoundary - icRightPos; + scrollToRightEvent(); + scrollenabled = false; + } + float icBottomPos = _innerContainer->getBottomInParent(); + if (icBottomPos + touchOffsetY >= _bounceBottomBoundary) + { + realOffsetY = _bounceBottomBoundary - icBottomPos; + scrollToBottomEvent(); + scrollenabled = false; + } + } + else if (touchOffsetX < 0.0f && touchOffsetY < 0.0f) // down left + { + float icRightPos = _innerContainer->getRightInParent(); + if (icRightPos + touchOffsetX <= _bounceRightBoundary) + { + realOffsetX = _bounceRightBoundary - icRightPos; + scrollToRightEvent(); + scrollenabled = false; + } + float icTopPos = _innerContainer->getTopInParent(); + if (icTopPos + touchOffsetY <= _bounceTopBoundary) + { + realOffsetY = _bounceTopBoundary - icTopPos; + scrollToTopEvent(); + scrollenabled = false; + } + } + else if (touchOffsetX > 0.0f && touchOffsetY < 0.0f) // down right + { + float icLeftPos = _innerContainer->getLeftInParent(); + if (icLeftPos + touchOffsetX >= _bounceLeftBoundary) + { + realOffsetX = _bounceLeftBoundary - icLeftPos; + scrollToLeftEvent(); + scrollenabled = false; + } + float icTopPos = _innerContainer->getTopInParent(); + if (icTopPos + touchOffsetY <= _bounceTopBoundary) + { + realOffsetY = _bounceTopBoundary - icTopPos; + scrollToTopEvent(); + scrollenabled = false; + } + } + else if (touchOffsetX == 0.0f && touchOffsetY > 0.0f) // up + { + float icBottomPos = _innerContainer->getBottomInParent(); + if (icBottomPos + touchOffsetY >= _bounceBottomBoundary) + { + realOffsetY = _bounceBottomBoundary - icBottomPos; + scrollToBottomEvent(); + scrollenabled = false; + } + } + else if (touchOffsetX < 0.0f && touchOffsetY == 0.0f) // left + { + float icRightPos = _innerContainer->getRightInParent(); + if (icRightPos + touchOffsetX <= _bounceRightBoundary) + { + realOffsetX = _bounceRightBoundary - icRightPos; + scrollToRightEvent(); + scrollenabled = false; + } + } + else if (touchOffsetX == 0.0f && touchOffsetY < 0.0f) // down + { + float icTopPos = _innerContainer->getTopInParent(); + if (icTopPos + touchOffsetY <= _bounceTopBoundary) + { + realOffsetY = _bounceTopBoundary - icTopPos; + scrollToTopEvent(); + scrollenabled = false; + } + } + else if (touchOffsetX > 0.0f && touchOffsetY == 0.0f) // right + { + float icLeftPos = _innerContainer->getLeftInParent(); + if (icLeftPos + touchOffsetX >= _bounceLeftBoundary) + { + realOffsetX = _bounceLeftBoundary - icLeftPos; + scrollToLeftEvent(); + scrollenabled = false; + } + } + } + else + { + if (touchOffsetX > 0.0f && touchOffsetY > 0.0f) // up right + { + float icLeftPos = _innerContainer->getLeftInParent(); + if (icLeftPos + touchOffsetX >= _leftBoundary) + { + realOffsetX = _leftBoundary - icLeftPos; + scrollToLeftEvent(); + scrollenabled = false; + } + float icBottomPos = _innerContainer->getBottomInParent(); + if (icBottomPos + touchOffsetY >= _bottomBoundary) + { + realOffsetY = _bottomBoundary - icBottomPos; + scrollToBottomEvent(); + scrollenabled = false; + } + } + else if (touchOffsetX < 0.0f && touchOffsetY > 0.0f) // up left + { + float icRightPos = _innerContainer->getRightInParent(); + if (icRightPos + touchOffsetX <= _rightBoundary) + { + realOffsetX = _rightBoundary - icRightPos; + scrollToRightEvent(); + scrollenabled = false; + } + float icBottomPos = _innerContainer->getBottomInParent(); + if (icBottomPos + touchOffsetY >= _bottomBoundary) + { + realOffsetY = _bottomBoundary - icBottomPos; + scrollToBottomEvent(); + scrollenabled = false; + } + } + else if (touchOffsetX < 0.0f && touchOffsetY < 0.0f) // down left + { + float icRightPos = _innerContainer->getRightInParent(); + if (icRightPos + touchOffsetX <= _rightBoundary) + { + realOffsetX = _rightBoundary - icRightPos; + scrollToRightEvent(); + scrollenabled = false; + } + float icTopPos = _innerContainer->getTopInParent(); + if (icTopPos + touchOffsetY <= _topBoundary) + { + realOffsetY = _topBoundary - icTopPos; + scrollToTopEvent(); + scrollenabled = false; + } + } + else if (touchOffsetX > 0.0f && touchOffsetY < 0.0f) // down right + { + float icLeftPos = _innerContainer->getLeftInParent(); + if (icLeftPos + touchOffsetX >= _leftBoundary) + { + realOffsetX = _leftBoundary - icLeftPos; + scrollToLeftEvent(); + scrollenabled = false; + } + float icTopPos = _innerContainer->getTopInParent(); + if (icTopPos + touchOffsetY <= _topBoundary) + { + realOffsetY = _topBoundary - icTopPos; + scrollToTopEvent(); + scrollenabled = false; + } + } + else if (touchOffsetX == 0.0f && touchOffsetY > 0.0f) // up + { + float icBottomPos = _innerContainer->getBottomInParent(); + if (icBottomPos + touchOffsetY >= _bottomBoundary) + { + realOffsetY = _bottomBoundary - icBottomPos; + scrollToBottomEvent(); + scrollenabled = false; + } + } + else if (touchOffsetX < 0.0f && touchOffsetY == 0.0f) // left + { + float icRightPos = _innerContainer->getRightInParent(); + if (icRightPos + touchOffsetX <= _rightBoundary) + { + realOffsetX = _rightBoundary - icRightPos; + scrollToRightEvent(); + scrollenabled = false; + } + } + else if (touchOffsetX == 0.0f && touchOffsetY < 0.0f) // down + { + float icTopPos = _innerContainer->getTopInParent(); + if (icTopPos + touchOffsetY <= _topBoundary) + { + realOffsetY = _topBoundary - icTopPos; + scrollToTopEvent(); + scrollenabled = false; + } + } + else if (touchOffsetX > 0.0f && touchOffsetY == 0.0f) // right + { + float icLeftPos = _innerContainer->getLeftInParent(); + if (icLeftPos + touchOffsetX >= _leftBoundary) + { + realOffsetX = _leftBoundary - icLeftPos; + scrollToLeftEvent(); + scrollenabled = false; + } + } + } + moveChildren(realOffsetX, realOffsetY); + break; + } + default: + break; + } + return scrollenabled; } -void UIScrollView::scrollToTop() +void UIScrollView::scrollToBottom(float time, bool attenuated) { - _moveDirection = SCROLLVIEW_MOVE_DIR_DOWN; // down - scrollChildren(-_innerContainer->getSize().height); + startAutoScrollChildrenWithDestination(cocos2d::Point(_innerContainer->getPosition().x, 0.0f), time, attenuated); +} + +void UIScrollView::scrollToTop(float time, bool attenuated) +{ + startAutoScrollChildrenWithDestination(cocos2d::Point(_innerContainer->getPosition().x, _size.height - _innerContainer->getSize().height), time, attenuated); +} + +void UIScrollView::scrollToLeft(float time, bool attenuated) +{ + startAutoScrollChildrenWithDestination(cocos2d::Point(0.0f, _innerContainer->getPosition().y), time, attenuated); +} + +void UIScrollView::scrollToRight(float time, bool attenuated) +{ + startAutoScrollChildrenWithDestination(cocos2d::Point(_size.width - _innerContainer->getSize().width, _innerContainer->getPosition().y), time, attenuated); +} + +void UIScrollView::scrollToTopLeft(float time, bool attenuated) +{ + if (_direction != SCROLLVIEW_DIR_BOTH) + { + CCLOG("Scroll diretion is not both!"); + return; + } + startAutoScrollChildrenWithDestination(cocos2d::Point(0.0f, _size.height - _innerContainer->getSize().height), time, attenuated); +} + +void UIScrollView::scrollToTopRight(float time, bool attenuated) +{ + if (_direction != SCROLLVIEW_DIR_BOTH) + { + CCLOG("Scroll diretion is not both!"); + return; + } + startAutoScrollChildrenWithDestination(cocos2d::Point(_size.width - _innerContainer->getSize().width, _size.height - _innerContainer->getSize().height), time, attenuated); +} + +void UIScrollView::scrollToBottomLeft(float time, bool attenuated) +{ + if (_direction != SCROLLVIEW_DIR_BOTH) + { + CCLOG("Scroll diretion is not both!"); + return; + } + startAutoScrollChildrenWithDestination(cocos2d::Point::ZERO, time, attenuated); +} + +void UIScrollView::scrollToBottomRight(float time, bool attenuated) +{ + if (_direction != SCROLLVIEW_DIR_BOTH) + { + CCLOG("Scroll diretion is not both!"); + return; + } + startAutoScrollChildrenWithDestination(cocos2d::Point(_size.width - _innerContainer->getSize().width, 0.0f), time, attenuated); +} + +void UIScrollView::scrollToPercentVertical(float percent, float time, bool attenuated) +{ + float minY = _size.height - _innerContainer->getSize().height; + float h = - minY; + startAutoScrollChildrenWithDestination(cocos2d::Point(_innerContainer->getPosition().x, minY + percent * h / 100.0f), time, attenuated); +} + +void UIScrollView::scrollToPercentHorizontal(float percent, float time, bool attenuated) +{ + float w = _innerContainer->getSize().width - _size.width; + startAutoScrollChildrenWithDestination(cocos2d::Point(-(percent * w / 100.0f), _innerContainer->getPosition().y), time, attenuated); +} + +void UIScrollView::scrollToPercentBothDirection(const cocos2d::Point& percent, float time, bool attenuated) +{ + if (_direction != SCROLLVIEW_DIR_BOTH) + { + return; + } + float minY = _size.height - _innerContainer->getSize().height; + float h = - minY; + float w = _innerContainer->getSize().width - _size.width; + startAutoScrollChildrenWithDestination(cocos2d::Point(-(percent.x * w / 100.0f), minY + percent.y * h / 100.0f), time, attenuated); +} + +void UIScrollView::jumpToBottom() +{ + jumpToDestination(cocos2d::Point(_innerContainer->getPosition().x, 0.0f)); +} + +void UIScrollView::jumpToTop() +{ + jumpToDestination(cocos2d::Point(_innerContainer->getPosition().x, _size.height - _innerContainer->getSize().height)); +} + +void UIScrollView::jumpToLeft() +{ + jumpToDestination(cocos2d::Point(0.0f, _innerContainer->getPosition().y)); +} + +void UIScrollView::jumpToRight() +{ + jumpToDestination(cocos2d::Point(_size.width - _innerContainer->getSize().width, _innerContainer->getPosition().y)); +} + +void UIScrollView::jumpToTopLeft() +{ + if (_direction != SCROLLVIEW_DIR_BOTH) + { + CCLOG("Scroll diretion is not both!"); + return; + } + jumpToDestination(cocos2d::Point(0.0f, _size.height - _innerContainer->getSize().height)); +} + +void UIScrollView::jumpToTopRight() +{ + if (_direction != SCROLLVIEW_DIR_BOTH) + { + CCLOG("Scroll diretion is not both!"); + return; + } + jumpToDestination(cocos2d::Point(_size.width - _innerContainer->getSize().width, _size.height - _innerContainer->getSize().height)); +} + +void UIScrollView::jumpToBottomLeft() +{ + if (_direction != SCROLLVIEW_DIR_BOTH) + { + CCLOG("Scroll diretion is not both!"); + return; + } + jumpToDestination(cocos2d::Point::ZERO); +} + +void UIScrollView::jumpToBottomRight() +{ + if (_direction != SCROLLVIEW_DIR_BOTH) + { + CCLOG("Scroll diretion is not both!"); + return; + } + jumpToDestination(cocos2d::Point(_size.width - _innerContainer->getSize().width, 0.0f)); +} + +void UIScrollView::jumpToPercentVertical(float percent) +{ + float minY = _size.height - _innerContainer->getSize().height; + float h = - minY; + jumpToDestination(cocos2d::Point(_innerContainer->getPosition().x, minY + percent * h / 100.0f)); +} + +void UIScrollView::jumpToPercentHorizontal(float percent) +{ + float w = _innerContainer->getSize().width - _size.width; + jumpToDestination(cocos2d::Point(-(percent * w / 100.0f), _innerContainer->getPosition().y)); +} + +void UIScrollView::jumpToPercentBothDirection(const cocos2d::Point& percent) +{ + if (_direction != SCROLLVIEW_DIR_BOTH) + { + return; + } + float minY = _size.height - _innerContainer->getSize().height; + float h = - minY; + float w = _innerContainer->getSize().width - _size.width; + jumpToDestination(cocos2d::Point(-(percent.x * w / 100.0f), minY + percent.y * h / 100.0f)); } void UIScrollView::startRecordSlidAction() { - if (_children->count() <= 0) + if (_autoScroll) { - return; - } - if (_autoScroll){ stopAutoScrollChildren(); } - _bePressed = true; - _slidTime = 0.0; + if (_bouncing) + { + stopBounceChildren(); + } + _slidTime = 0.0f; } void UIScrollView::endRecordSlidAction() { - if (_children->count() <= 0) + if (!checkNeedBounce() && _inertiaScrollEnabled) { - return; + if (_slidTime <= 0.016f) + { + return; + } + float totalDis = 0.0f; + cocos2d::Point dir; + switch (_direction) + { + case SCROLLVIEW_DIR_VERTICAL: + totalDis = _touchEndedPoint.y - _touchBeganPoint.y; + if (totalDis < 0.0f) + { + dir = SCROLLDIR_DOWN; + } + else + { + dir = SCROLLDIR_UP; + } + break; + case SCROLLVIEW_DIR_HORIZONTAL: + totalDis = _touchEndedPoint.x - _touchBeganPoint.x; + if (totalDis < 0.0f) + { + dir = SCROLLDIR_LEFT; + } + else + { + dir = SCROLLDIR_RIGHT; + } + break; + case SCROLLVIEW_DIR_BOTH: + { + cocos2d::Point subVector = _touchEndedPoint - _touchBeganPoint; + totalDis = subVector.getLength(); + dir = subVector.normalize(); + break; + } + default: + break; + } + float orSpeed = MIN(fabs(totalDis)/(_slidTime), AUTOSCROLLMAXSPEED); + startAutoScrollChildrenWithOriginalSpeed(dir, orSpeed, true, -1000); + _slidTime = 0.0f; } - if (_slidTime <= 0.016f) - { - return; - } - float totalDis = 0; - totalDis = _touchEndLocation-_touchStartLocation; - float orSpeed = fabs(totalDis)/(_slidTime); - startAutoScrollChildren(orSpeed); - - _bePressed = false; - _slidTime = 0.0; } -void UIScrollView::handlePressLogic(const Point &touchPoint) +void UIScrollView::handlePressLogic(const cocos2d::Point &touchPoint) { - Point nsp = _renderer->convertToNodeSpace(touchPoint); - switch (_direction) - { - case SCROLLVIEW_DIR_VERTICAL: // vertical - _touchMoveStartLocation = nsp.y; - _touchStartLocation = nsp.y; - break; - case SCROLLVIEW_DIR_HORIZONTAL: // horizontal - _touchMoveStartLocation = nsp.x; - _touchStartLocation = nsp.x; - break; - default: - break; - } + _touchBeganPoint = _renderer->convertToNodeSpace(touchPoint); + _touchMovingPoint = _touchBeganPoint; startRecordSlidAction(); + _bePressed = true; } -void UIScrollView::handleMoveLogic(const Point &touchPoint) +void UIScrollView::handleMoveLogic(const cocos2d::Point &touchPoint) { - Point nsp = _renderer->convertToNodeSpace(touchPoint); - float offset = 0.0f; - + _touchMovedPoint = _renderer->convertToNodeSpace(touchPoint); + cocos2d::Point delta = _touchMovedPoint - _touchMovingPoint; + _touchMovingPoint = _touchMovedPoint; switch (_direction) { case SCROLLVIEW_DIR_VERTICAL: // vertical - { - float moveY = nsp.y; - offset = moveY - _touchMoveStartLocation; - _touchMoveStartLocation = moveY; - - if (offset < 0.0f) - { - _moveDirection = SCROLLVIEW_MOVE_DIR_DOWN; // down - } - else if (offset > 0.0f) - { - _moveDirection = SCROLLVIEW_MOVE_DIR_UP; // up - } - } + { + scrollChildren(0.0f, delta.y); break; - + } case SCROLLVIEW_DIR_HORIZONTAL: // horizontal - { - float moveX = nsp.x; - offset = moveX - _touchMoveStartLocation; - _touchMoveStartLocation = moveX; - - if (offset < 0) - { - _moveDirection = SCROLLVIEW_MOVE_DIR_LEFT; // left - } - else if (offset > 0) - { - _moveDirection = SCROLLVIEW_MOVE_DIR_RIGHT; // right - } - } + { + scrollChildren(delta.x, 0.0f); break; - + } + case SCROLLVIEW_DIR_BOTH: // both + { + scrollChildren(delta.x, delta.y); + break; + } default: break; } - scrollChildren(offset); } -void UIScrollView::handleReleaseLogic(const Point &touchPoint) +void UIScrollView::handleReleaseLogic(const cocos2d::Point &touchPoint) { - Point nsp = _renderer->convertToNodeSpace(touchPoint); - switch (_direction) - { - case SCROLLVIEW_DIR_VERTICAL: // vertical - _touchEndLocation = nsp.y; - break; - - case SCROLLVIEW_DIR_HORIZONTAL: // horizontal - _touchEndLocation = nsp.x; - break; - - default: - break; - } + _touchEndedPoint = _renderer->convertToNodeSpace(touchPoint); endRecordSlidAction(); + _bePressed = false; } -bool UIScrollView::onTouchBegan(const Point &touchPoint) +bool UIScrollView::onTouchBegan(const cocos2d::Point &touchPoint) { - bool pass = Layout::onTouchBegan(touchPoint); + bool pass = UILayout::onTouchBegan(touchPoint); handlePressLogic(touchPoint); return pass; } -void UIScrollView::onTouchMoved(const Point &touchPoint) +void UIScrollView::onTouchMoved(const cocos2d::Point &touchPoint) { - Layout::onTouchMoved(touchPoint); + UILayout::onTouchMoved(touchPoint); handleMoveLogic(touchPoint); } -void UIScrollView::onTouchEnded(const Point &touchPoint) +void UIScrollView::onTouchEnded(const cocos2d::Point &touchPoint) { - Layout::onTouchEnded(touchPoint); + UILayout::onTouchEnded(touchPoint); handleReleaseLogic(touchPoint); } -void UIScrollView::onTouchCancelled(const Point &touchPoint) +void UIScrollView::onTouchCancelled(const cocos2d::Point &touchPoint) { - Layout::onTouchCancelled(touchPoint); + UILayout::onTouchCancelled(touchPoint); + handleReleaseLogic(touchPoint); } -void UIScrollView::onTouchLongClicked(const Point &touchPoint) +void UIScrollView::onTouchLongClicked(const cocos2d::Point &touchPoint) { } @@ -546,6 +1406,10 @@ void UIScrollView::update(float dt) { autoScrollChildren(dt); } + if (_bouncing) + { + bounceChildren(dt); + } recordSlidTime(dt); } @@ -557,7 +1421,7 @@ void UIScrollView::recordSlidTime(float dt) } } -void UIScrollView::interceptTouchEvent(int handleState, UIWidget *sender, const Point &touchPoint) +void UIScrollView::interceptTouchEvent(int handleState, UIWidget *sender, const cocos2d::Point &touchPoint) { switch (handleState) { @@ -567,20 +1431,7 @@ void UIScrollView::interceptTouchEvent(int handleState, UIWidget *sender, const case 1: { - float offset = 0; - switch (_direction) - { - case SCROLLVIEW_DIR_VERTICAL: // vertical - offset = fabs(sender->getTouchStartPos().y - touchPoint.y); - break; - - case SCROLLVIEW_DIR_HORIZONTAL: // horizontal - offset = fabs(sender->getTouchStartPos().x - touchPoint.x); - break; - - default: - break; - } + float offset = (sender->getTouchStartPos() - touchPoint).getLength(); if (offset > _childFocusCancelOffset) { sender->setFocused(false); @@ -594,11 +1445,12 @@ void UIScrollView::interceptTouchEvent(int handleState, UIWidget *sender, const break; case 3: + handleReleaseLogic(touchPoint); break; } } -void UIScrollView::checkChildInfo(int handleState,UIWidget* sender,const Point &touchPoint) +void UIScrollView::checkChildInfo(int handleState,UIWidget* sender,const cocos2d::Point &touchPoint) { interceptTouchEvent(handleState, sender, touchPoint); } @@ -635,7 +1487,47 @@ void UIScrollView::scrollToRightEvent() } } -void UIScrollView::addEventListener(Object *target, SEL_ScrollViewEvent selector) +void UIScrollView::scrollingEvent() +{ + if (_eventListener && _eventSelector) + { + (_eventListener->*_eventSelector)(this, SCROLLVIEW_EVENT_SCROLLING); + } +} + +void UIScrollView::bounceTopEvent() +{ + if (_eventListener && _eventSelector) + { + (_eventListener->*_eventSelector)(this, SCROLLVIEW_EVENT_BOUNCE_TOP); + } +} + +void UIScrollView::bounceBottomEvent() +{ + if (_eventListener && _eventSelector) + { + (_eventListener->*_eventSelector)(this, SCROLLVIEW_EVENT_BOUNCE_BOTTOM); + } +} + +void UIScrollView::bounceLeftEvent() +{ + if (_eventListener && _eventSelector) + { + (_eventListener->*_eventSelector)(this, SCROLLVIEW_EVENT_BOUNCE_LEFT); + } +} + +void UIScrollView::bounceRightEvent() +{ + if (_eventListener && _eventSelector) + { + (_eventListener->*_eventSelector)(this, SCROLLVIEW_EVENT_BOUNCE_RIGHT); + } +} + +void UIScrollView::addEventListener(cocos2d::Object *target, SEL_ScrollViewEvent selector) { _eventListener = target; _eventSelector = selector; @@ -651,17 +1543,27 @@ SCROLLVIEW_DIR UIScrollView::getDirection() return _direction; } -void UIScrollView::setMoveDirection(SCROLLVIEW_MOVE_DIR dir) +void UIScrollView::setBounceEnabled(bool enabled) { - _moveDirection = dir; + _bounceEnabled = enabled; } -SCROLLVIEW_MOVE_DIR UIScrollView::getMoveDirection() +bool UIScrollView::isBounceEnabled() const { - return _moveDirection; + return _bounceEnabled; } -Layout* UIScrollView::getInnerContainer() +void UIScrollView::setInertiaScrollEnabled(bool enabled) +{ + _inertiaScrollEnabled = enabled; +} + +bool UIScrollView::isInertiaScrollEnabled() const +{ + return _inertiaScrollEnabled; +} + +UILayout* UIScrollView::getInnerContainer() { return _innerContainer; } @@ -686,4 +1588,27 @@ const char* UIScrollView::getDescription() const return "ScrollView"; } +UIWidget* UIScrollView::createCloneInstance() +{ + return UIScrollView::create(); +} + +void UIScrollView::copyClonedWidgetChildren(UIWidget* model) +{ + UILayout::copyClonedWidgetChildren(model); +} + +void UIScrollView::copySpecialProperties(UIWidget *widget) +{ + UIScrollView* scrollView = dynamic_cast(widget); + if (scrollView) + { + UILayout::copySpecialProperties(widget); + setInnerContainerSize(scrollView->getInnerContainerSize()); + setDirection(scrollView->_direction); + setBounceEnabled(scrollView->_bounceEnabled); + setInertiaScrollEnabled(scrollView->_inertiaScrollEnabled); + } +} + } \ No newline at end of file diff --git a/cocos/gui/UIScrollView.h b/cocos/gui/UIScrollView.h index 1a6cf31f99..4d762930f8 100644 --- a/cocos/gui/UIScrollView.h +++ b/cocos/gui/UIScrollView.h @@ -25,7 +25,7 @@ #ifndef __UISCROLLVIEW_H__ #define __UISCROLLVIEW_H__ -#include "gui//Layout.h" +#include "gui/UILayout.h" #include "gui/UIScrollInterface.h" namespace gui { @@ -34,16 +34,8 @@ enum SCROLLVIEW_DIR { SCROLLVIEW_DIR_NONE, SCROLLVIEW_DIR_VERTICAL, - SCROLLVIEW_DIR_HORIZONTAL -}; - -enum SCROLLVIEW_MOVE_DIR -{ - SCROLLVIEW_MOVE_DIR_NONE, - SCROLLVIEW_MOVE_DIR_UP, - SCROLLVIEW_MOVE_DIR_DOWN, - SCROLLVIEW_MOVE_DIR_LEFT, - SCROLLVIEW_MOVE_DIR_RIGHT, + SCROLLVIEW_DIR_HORIZONTAL, + SCROLLVIEW_DIR_BOTH }; typedef enum @@ -52,13 +44,18 @@ typedef enum SCROLLVIEW_EVENT_SCROLL_TO_BOTTOM, SCROLLVIEW_EVENT_SCROLL_TO_LEFT, SCROLLVIEW_EVENT_SCROLL_TO_RIGHT, + SCROLLVIEW_EVENT_SCROLLING, + SCROLLVIEW_EVENT_BOUNCE_TOP, + SCROLLVIEW_EVENT_BOUNCE_BOTTOM, + SCROLLVIEW_EVENT_BOUNCE_LEFT, + SCROLLVIEW_EVENT_BOUNCE_RIGHT }ScrollviewEventType; -typedef void (cocos2d::Object::*SEL_ScrollViewEvent)(cocos2d::Object*, ScrollviewEventType); +typedef void (cocos2d::CCObject::*SEL_ScrollViewEvent)(cocos2d::Object*, ScrollviewEventType); #define scrollvieweventselector(_SELECTOR) (SEL_ScrollViewEvent)(&_SELECTOR) -class UIScrollView : public Layout , public UIScrollInterface +class UIScrollView : public UILayout , public UIScrollInterface { public: /** @@ -83,7 +80,7 @@ public: * * @param SCROLLVIEW_DIR */ - void setDirection(SCROLLVIEW_DIR dir); + virtual void setDirection(SCROLLVIEW_DIR dir); /** * Gets scroll direction of scrollview. @@ -101,17 +98,117 @@ public: * * @return inner container. */ - Layout* getInnerContainer(); + UILayout* getInnerContainer(); /** * Scroll inner container to bottom boundary of scrollview. */ - void scrollToBottom(); + void scrollToBottom(float time, bool attenuated); /** * Scroll inner container to top boundary of scrollview. */ - void scrollToTop(); + void scrollToTop(float time, bool attenuated); + + /** + * Scroll inner container to left boundary of scrollview. + */ + void scrollToLeft(float time, bool attenuated); + + /** + * Scroll inner container to right boundary of scrollview. + */ + void scrollToRight(float time, bool attenuated); + + /** + * Scroll inner container to top and left boundary of scrollview. + */ + void scrollToTopLeft(float time, bool attenuated); + + /** + * Scroll inner container to top and right boundary of scrollview. + */ + void scrollToTopRight(float time, bool attenuated); + + /** + * Scroll inner container to bottom and left boundary of scrollview. + */ + void scrollToBottomLeft(float time, bool attenuated); + + /** + * Scroll inner container to bottom and right boundary of scrollview. + */ + void scrollToBottomRight(float time, bool attenuated); + + /** + * Scroll inner container to vertical percent position of scrollview. + */ + void scrollToPercentVertical(float percent, float time, bool attenuated); + + /** + * Scroll inner container to horizontal percent position of scrollview. + */ + void scrollToPercentHorizontal(float percent, float time, bool attenuated); + + /** + * Scroll inner container to both direction percent position of scrollview. + */ + void scrollToPercentBothDirection(const cocos2d::Point& percent, float time, bool attenuated); + + /** + * Move inner container to bottom boundary of scrollview. + */ + void jumpToBottom(); + + /** + * Move inner container to top boundary of scrollview. + */ + void jumpToTop(); + + /** + * Move inner container to left boundary of scrollview. + */ + void jumpToLeft(); + + /** + * Move inner container to right boundary of scrollview. + */ + void jumpToRight(); + + /** + * Move inner container to top and left boundary of scrollview. + */ + void jumpToTopLeft(); + + /** + * Move inner container to top and right boundary of scrollview. + */ + void jumpToTopRight(); + + /** + * Move inner container to bottom and left boundary of scrollview. + */ + void jumpToBottomLeft(); + + /** + * Move inner container to bottom and right boundary of scrollview. + */ + void jumpToBottomRight(); + + /** + * Move inner container to vertical percent position of scrollview. + */ + void jumpToPercentVertical(float percent); + + /** + * Move inner container to horizontal percent position of scrollview. + */ + void jumpToPercentHorizontal(float percent); + + /** + * Move inner container to both direction percent position of scrollview. + */ + void jumpToPercentBothDirection(const cocos2d::Point& percent); /** * Changes inner container size of scrollview. @@ -134,7 +231,7 @@ public: /** * Add call back function called scrollview event triggered */ - void addEventListener(cocos2d::Object* target, SEL_ScrollViewEvent selector); + void addEventListener(cocos2d::Object* target, SEL_ScrollViewEvent selector); //override "addChild" method of widget. virtual bool addChild(UIWidget* widget); @@ -165,6 +262,14 @@ public: virtual void update(float dt); + void setBounceEnabled(bool enabled); + + bool isBounceEnabled() const; + + void setInertiaScrollEnabled(bool enabled); + + bool isInertiaScrollEnabled() const; + /** * Sets LayoutType. * @@ -192,12 +297,20 @@ public: protected: virtual bool init(); virtual void initRenderer(); - void moveChildren(float offset); + void moveChildren(float offsetX, float offsetY); void autoScrollChildren(float dt); - void startAutoScrollChildren(float v); + void bounceChildren(float dt); + void checkBounceBoundary(); + bool checkNeedBounce(); + void startAutoScrollChildrenWithOriginalSpeed(const cocos2d::Point& dir, float v, bool attenuated, float acceleration); + void startAutoScrollChildrenWithDestination(const cocos2d::Point& des, float time, bool attenuated); + void jumpToDestination(const cocos2d::Point& des); void stopAutoScrollChildren(); - float getCurAutoScrollDistance(float time); - virtual bool scrollChildren(float touchOffset); + void startBounceChildren(float v); + void stopBounceChildren(); + bool checkCustomScrollDestination(float* touchOffsetX, float* touchOffsetY); + virtual bool scrollChildren(float touchOffsetX, float touchOffsetY); + bool bounceScrollChildren(float touchOffsetX, float touchOffsetY); void startRecordSlidAction(); virtual void endRecordSlidAction(); virtual void handlePressLogic(const cocos2d::Point &touchPoint); @@ -206,48 +319,71 @@ protected: virtual void interceptTouchEvent(int handleState,UIWidget* sender,const cocos2d::Point &touchPoint); virtual void checkChildInfo(int handleState,UIWidget* sender,const cocos2d::Point &touchPoint); void recordSlidTime(float dt); - //override "releaseResoures" method of widget. - virtual void releaseResoures(); - void scrollToTopEvent(); void scrollToBottomEvent(); void scrollToLeftEvent(); void scrollToRightEvent(); - void setMoveDirection(SCROLLVIEW_MOVE_DIR dir); - SCROLLVIEW_MOVE_DIR getMoveDirection(); + void scrollingEvent(); + void bounceTopEvent(); + void bounceBottomEvent(); + void bounceLeftEvent(); + void bounceRightEvent(); virtual void onSizeChanged(); - virtual void setClippingEnabled(bool able){Layout::setClippingEnabled(able);}; + virtual UIWidget* createCloneInstance(); + virtual void copySpecialProperties(UIWidget* model); + virtual void copyClonedWidgetChildren(UIWidget* model); + virtual void setClippingEnabled(bool able){UILayout::setClippingEnabled(able);}; protected: - Layout* _innerContainer; + UILayout* _innerContainer; SCROLLVIEW_DIR _direction; - SCROLLVIEW_MOVE_DIR _moveDirection; - float _touchStartLocation; - float _touchEndLocation; - float _touchMoveStartLocation; - float _topBoundary;//test - float _bottomBoundary;//test + + cocos2d::Point _touchBeganPoint; + cocos2d::Point _touchMovedPoint; + cocos2d::Point _touchEndedPoint; + cocos2d::Point _touchMovingPoint; + cocos2d::Point _autoScrollDir; + + float _topBoundary; + float _bottomBoundary; float _leftBoundary; float _rightBoundary; - bool _topEnd; - bool _bottomEnd; - bool _leftEnd; - bool _rightEnd; + float _bounceTopBoundary; + float _bounceBottomBoundary; + float _bounceLeftBoundary; + float _bounceRightBoundary; + bool _autoScroll; + float _autoScrollAddUpTime; float _autoScrollOriginalSpeed; float _autoScrollAcceleration; + bool _isAutoScrollSpeedAttenuated; + bool _needCheckAutoScrollDestination; + cocos2d::Point _autoScrollDestination; bool _bePressed; float _slidTime; cocos2d::Point _moveChildPoint; float _childFocusCancelOffset; + bool _leftBounceNeeded; + bool _topBounceNeeded; + bool _rightBounceNeeded; + bool _bottomBounceNeeded; + + bool _bounceEnabled; + bool _bouncing; + cocos2d::Point _bounceDir; + float _bounceOriginalSpeed; + bool _inertiaScrollEnabled; + + + cocos2d::Object* _eventListener; SEL_ScrollViewEvent _eventSelector; - }; } diff --git a/cocos/gui/UISlider.cpp b/cocos/gui/UISlider.cpp index 425fc581e0..f3c0a90b97 100644 --- a/cocos/gui/UISlider.cpp +++ b/cocos/gui/UISlider.cpp @@ -25,15 +25,12 @@ #include "gui/UISlider.h" #include "extensions/GUI/CCControlExtension/CCScale9Sprite.h" -using namespace cocos2d; -using namespace cocos2d::extension; - namespace gui { UISlider::UISlider(): _barRenderer(NULL), _progressBarRenderer(NULL), -_progressBarTextureSize(Size::ZERO), +_progressBarTextureSize(cocos2d::Size::ZERO), _slidBallNormalRenderer(NULL), _slidBallPressedRenderer(NULL), _slidBallDisabledRenderer(NULL), @@ -47,8 +44,8 @@ _progressBarTextureFile(""), _slidBallNormalTextureFile(""), _slidBallPressedTextureFile(""), _slidBallDisabledTextureFile(""), -_capInsetsBarRenderer(Rect::ZERO), -_capInsetsProgressBarRenderer(Rect::ZERO), +_capInsetsBarRenderer(cocos2d::Rect::ZERO), +_capInsetsProgressBarRenderer(cocos2d::Rect::ZERO), _slidPercentListener(NULL), _slidPercentSelector(NULL), _barTexType(UI_TEX_TYPE_LOCAL), @@ -79,17 +76,17 @@ UISlider* UISlider::create() void UISlider::initRenderer() { UIWidget::initRenderer(); - _barRenderer = CCSprite::create(); - _progressBarRenderer = CCSprite::create(); - _progressBarRenderer->setAnchorPoint(Point(0.0f, 0.5f)); + _barRenderer = cocos2d::Sprite::create(); + _progressBarRenderer = cocos2d::Sprite::create(); + _progressBarRenderer->setAnchorPoint(cocos2d::Point(0.0f, 0.5f)); _renderer->addChild(_barRenderer, -1); _renderer->addChild(_progressBarRenderer, -1); - _slidBallNormalRenderer = CCSprite::create(); - _slidBallPressedRenderer = CCSprite::create(); + _slidBallNormalRenderer = cocos2d::Sprite::create(); + _slidBallPressedRenderer = cocos2d::Sprite::create(); _slidBallPressedRenderer->setVisible(false); - _slidBallDisabledRenderer = CCSprite::create(); + _slidBallDisabledRenderer = cocos2d::Sprite::create(); _slidBallDisabledRenderer->setVisible(false); - _slidBallRenderer = CCNode::create(); + _slidBallRenderer = cocos2d::Node::create(); _slidBallRenderer->addChild(_slidBallNormalRenderer); _slidBallRenderer->addChild(_slidBallPressedRenderer); _slidBallRenderer->addChild(_slidBallDisabledRenderer); @@ -109,21 +106,21 @@ void UISlider::loadBarTexture(const char* fileName, TextureResType texType) case UI_TEX_TYPE_LOCAL: if (_scale9Enabled) { - dynamic_cast(_barRenderer)->initWithFile(fileName); + dynamic_cast(_barRenderer)->initWithFile(fileName); } else { - dynamic_cast(_barRenderer)->initWithFile(fileName); + dynamic_cast(_barRenderer)->initWithFile(fileName); } break; case UI_TEX_TYPE_PLIST: if (_scale9Enabled) { - dynamic_cast(_barRenderer)->initWithSpriteFrameName(fileName); + dynamic_cast(_barRenderer)->initWithSpriteFrameName(fileName); } else { - dynamic_cast(_barRenderer)->initWithSpriteFrameName(fileName); + dynamic_cast(_barRenderer)->initWithSpriteFrameName(fileName); } break; default: @@ -131,13 +128,13 @@ void UISlider::loadBarTexture(const char* fileName, TextureResType texType) } if (_scale9Enabled) { - dynamic_cast(_barRenderer)->setColor(getColor()); - dynamic_cast(_barRenderer)->setOpacity(getOpacity()); + dynamic_cast(_barRenderer)->setColor(getColor()); + dynamic_cast(_barRenderer)->setOpacity(getOpacity()); } else { - dynamic_cast(_barRenderer)->setColor(getColor()); - dynamic_cast(_barRenderer)->setOpacity(getOpacity()); + dynamic_cast(_barRenderer)->setColor(getColor()); + dynamic_cast(_barRenderer)->setOpacity(getOpacity()); } barRendererScaleChangedWithSize(); } @@ -155,21 +152,21 @@ void UISlider::loadProgressBarTexture(const char *fileName, TextureResType texTy case UI_TEX_TYPE_LOCAL: if (_scale9Enabled) { - dynamic_cast(_progressBarRenderer)->initWithFile(fileName); + dynamic_cast(_progressBarRenderer)->initWithFile(fileName); } else { - dynamic_cast(_progressBarRenderer)->initWithFile(fileName); + dynamic_cast(_progressBarRenderer)->initWithFile(fileName); } break; case UI_TEX_TYPE_PLIST: if (_scale9Enabled) { - dynamic_cast(_progressBarRenderer)->initWithSpriteFrameName(fileName); + dynamic_cast(_progressBarRenderer)->initWithSpriteFrameName(fileName); } else { - dynamic_cast(_progressBarRenderer)->initWithSpriteFrameName(fileName); + dynamic_cast(_progressBarRenderer)->initWithSpriteFrameName(fileName); } break; default: @@ -177,15 +174,15 @@ void UISlider::loadProgressBarTexture(const char *fileName, TextureResType texTy } if (_scale9Enabled) { - dynamic_cast(_progressBarRenderer)->setColor(getColor()); - dynamic_cast(_progressBarRenderer)->setOpacity(getOpacity()); + dynamic_cast(_progressBarRenderer)->setColor(getColor()); + dynamic_cast(_progressBarRenderer)->setOpacity(getOpacity()); } else { - dynamic_cast(_progressBarRenderer)->setColor(getColor()); - dynamic_cast(_progressBarRenderer)->setOpacity(getOpacity()); + dynamic_cast(_progressBarRenderer)->setColor(getColor()); + dynamic_cast(_progressBarRenderer)->setOpacity(getOpacity()); } - _progressBarRenderer->setAnchorPoint(Point(0.0f, 0.5f)); + _progressBarRenderer->setAnchorPoint(cocos2d::Point(0.0f, 0.5f)); _progressBarTextureSize = _progressBarRenderer->getContentSize(); progressBarRendererScaleChangedWithSize(); } @@ -204,13 +201,13 @@ void UISlider::setScale9Enabled(bool able) _progressBarRenderer = NULL; if (_scale9Enabled) { - _barRenderer = Scale9Sprite::create(); - _progressBarRenderer = Scale9Sprite::create(); + _barRenderer = cocos2d::extension::Scale9Sprite::create(); + _progressBarRenderer = cocos2d::extension::Scale9Sprite::create(); } else { - _barRenderer = CCSprite::create(); - _progressBarRenderer = CCSprite::create(); + _barRenderer = cocos2d::Sprite::create(); + _progressBarRenderer = cocos2d::Sprite::create(); } loadBarTexture(_textureFile.c_str(), _barTexType); loadProgressBarTexture(_progressBarTextureFile.c_str(), _progressBarTexType); @@ -239,30 +236,30 @@ void UISlider::ignoreContentAdaptWithSize(bool ignore) } } -void UISlider::setCapInsets(const Rect &capInsets) +void UISlider::setCapInsets(const cocos2d::Rect &capInsets) { setCapInsetsBarRenderer(capInsets); setCapInsetProgressBarRebderer(capInsets); } -void UISlider::setCapInsetsBarRenderer(const Rect &capInsets) +void UISlider::setCapInsetsBarRenderer(const cocos2d::Rect &capInsets) { _capInsetsBarRenderer = capInsets; if (!_scale9Enabled) { return; } - dynamic_cast(_barRenderer)->setCapInsets(capInsets); + dynamic_cast(_barRenderer)->setCapInsets(capInsets); } -void UISlider::setCapInsetProgressBarRebderer(const Rect &capInsets) +void UISlider::setCapInsetProgressBarRebderer(const cocos2d::Rect &capInsets) { _capInsetsProgressBarRenderer = capInsets; if (!_scale9Enabled) { return; } - dynamic_cast(_progressBarRenderer)->setCapInsets(capInsets); + dynamic_cast(_progressBarRenderer)->setCapInsets(capInsets); } void UISlider::loadSlidBallTextures(const char* normal,const char* pressed,const char* disabled,TextureResType texType) @@ -353,10 +350,10 @@ void UISlider::setPercent(int percent) } _percent = percent; float dis = _barLength*(percent/100.0f); - _slidBallRenderer->setPosition(Point(-_barLength/2.0f + dis, 0.0f)); + _slidBallRenderer->setPosition(cocos2d::Point(-_barLength/2.0f + dis, 0.0f)); if (_scale9Enabled) { - dynamic_cast(_progressBarRenderer)->setPreferredSize(Size(dis,_progressBarTextureSize.height)); + dynamic_cast(_progressBarRenderer)->setPreferredSize(cocos2d::Size(dis,_progressBarTextureSize.height)); } else { @@ -365,10 +362,10 @@ void UISlider::setPercent(int percent) { case UI_TEX_TYPE_PLIST: { - Sprite* barNode = dynamic_cast(_progressBarRenderer); + cocos2d::Sprite* barNode = dynamic_cast(_progressBarRenderer); if (barNode) { - Point to = barNode->getTextureRect().origin; + cocos2d::Point to = barNode->getTextureRect().origin; x = to.x; y = to.y; } @@ -377,33 +374,33 @@ void UISlider::setPercent(int percent) default: break; } - dynamic_cast(_progressBarRenderer)->setTextureRect(Rect(x, y, _progressBarTextureSize.width * (percent/100.0f), _progressBarTextureSize.height)); + dynamic_cast(_progressBarRenderer)->setTextureRect(cocos2d::Rect(x, y, _progressBarTextureSize.width * (percent/100.0f), _progressBarTextureSize.height)); } } -bool UISlider::onTouchBegan(const Point &touchPoint) +bool UISlider::onTouchBegan(const cocos2d::Point &touchPoint) { bool pass = UIWidget::onTouchBegan(touchPoint); - Point nsp = _renderer->convertToNodeSpace(touchPoint); + cocos2d::Point nsp = _renderer->convertToNodeSpace(touchPoint); setPercent(getPercentWithBallPos(nsp.x)); percentChangedEvent(); return pass; } -void UISlider::onTouchMoved(const Point &touchPoint) +void UISlider::onTouchMoved(const cocos2d::Point &touchPoint) { - Point nsp = _renderer->convertToNodeSpace(touchPoint); - _slidBallRenderer->setPosition(Point(nsp.x,0)); + cocos2d::Point nsp = _renderer->convertToNodeSpace(touchPoint); + _slidBallRenderer->setPosition(cocos2d::Point(nsp.x,0)); setPercent(getPercentWithBallPos(nsp.x)); percentChangedEvent(); } -void UISlider::onTouchEnded(const Point &touchPoint) +void UISlider::onTouchEnded(const cocos2d::Point &touchPoint) { UIWidget::onTouchEnded(touchPoint); } -void UISlider::onTouchCancelled(const Point &touchPoint) +void UISlider::onTouchCancelled(const cocos2d::Point &touchPoint) { UIWidget::onTouchCancelled(touchPoint); } @@ -413,7 +410,7 @@ float UISlider::getPercentWithBallPos(float px) return (((px-(-_barLength/2.0f))/_barLength)*100.0f); } -void UISlider::addEventListener(Object *target, SEL_SlidPercentChangedEvent selector) +void UISlider::addEventListener(cocos2d::Object *target, SEL_SlidPercentChangedEvent selector) { _slidPercentListener = target; _slidPercentSelector = selector; @@ -438,12 +435,12 @@ void UISlider::onSizeChanged() progressBarRendererScaleChangedWithSize(); } -const Size& UISlider::getContentSize() const +const cocos2d::Size& UISlider::getContentSize() const { return _barRenderer->getContentSize(); } -Node* UISlider::getVirtualRenderer() +cocos2d::Node* UISlider::getVirtualRenderer() { return _barRenderer; } @@ -462,11 +459,11 @@ void UISlider::barRendererScaleChangedWithSize() _barLength = _size.width; if (_scale9Enabled) { - dynamic_cast(_barRenderer)->setPreferredSize(_size); + dynamic_cast(_barRenderer)->setPreferredSize(_size); } else { - Size btextureSize = _barRenderer->getContentSize(); + cocos2d::Size btextureSize = _barRenderer->getContentSize(); if (btextureSize.width <= 0.0f || btextureSize.height <= 0.0f) { _barRenderer->setScale(1.0f); @@ -487,7 +484,7 @@ void UISlider::progressBarRendererScaleChangedWithSize() { if (!_scale9Enabled) { - Size ptextureSize = _progressBarTextureSize; + cocos2d::Size ptextureSize = _progressBarTextureSize; float pscaleX = _size.width / ptextureSize.width; float pscaleY = _size.height / ptextureSize.height; _progressBarRenderer->setScaleX(pscaleX); @@ -498,11 +495,11 @@ void UISlider::progressBarRendererScaleChangedWithSize() { if (_scale9Enabled) { - dynamic_cast(_progressBarRenderer)->setPreferredSize(_size); + dynamic_cast(_progressBarRenderer)->setPreferredSize(_size); } else { - Size ptextureSize = _progressBarTextureSize; + cocos2d::Size ptextureSize = _progressBarTextureSize; if (ptextureSize.width <= 0.0f || ptextureSize.height <= 0.0f) { _progressBarRenderer->setScale(1.0f); @@ -514,7 +511,7 @@ void UISlider::progressBarRendererScaleChangedWithSize() _progressBarRenderer->setScaleY(pscaleY); } } - _progressBarRenderer->setPosition(Point(-_barLength * 0.5f, 0.0f)); + _progressBarRenderer->setPosition(cocos2d::Point(-_barLength * 0.5f, 0.0f)); setPercent(_percent); } @@ -544,4 +541,25 @@ const char* UISlider::getDescription() const return "Slider"; } +UIWidget* UISlider::createCloneInstance() +{ + return UISlider::create(); +} + +void UISlider::copySpecialProperties(UIWidget *widget) +{ + UISlider* slider = dynamic_cast(widget); + if (slider) + { + _prevIgnoreSize = slider->_prevIgnoreSize; + setScale9Enabled(slider->_scale9Enabled); + loadBarTexture(slider->_textureFile.c_str(), slider->_barTexType); + loadProgressBarTexture(slider->_progressBarTextureFile.c_str(), slider->_progressBarTexType); + loadSlidBallTextureNormal(slider->_slidBallNormalTextureFile.c_str(), slider->_ballNTexType); + loadSlidBallTexturePressed(slider->_slidBallPressedTextureFile.c_str(), slider->_ballPTexType); + loadSlidBallTextureDisabled(slider->_slidBallDisabledTextureFile.c_str(), slider->_ballDTexType); + setPercent(slider->getPercent()); + } +} + } \ No newline at end of file diff --git a/cocos/gui/UISlider.h b/cocos/gui/UISlider.h index 0654336600..186af44d45 100644 --- a/cocos/gui/UISlider.h +++ b/cocos/gui/UISlider.h @@ -34,9 +34,13 @@ typedef enum SLIDER_PERCENTCHANGED }SliderEventType; -typedef void (cocos2d::CCObject::*SEL_SlidPercentChangedEvent)(cocos2d::Object*,SliderEventType); +typedef void (cocos2d::Object::*SEL_SlidPercentChangedEvent)(cocos2d::Object*,SliderEventType); #define sliderpercentchangedselector(_SELECTOR) (SEL_SlidPercentChangedEvent)(&_SELECTOR) +/** +* @js NA +* @lua NA +*/ class UISlider : public UIWidget { public: @@ -185,6 +189,7 @@ public: * Returns the "class name" of widget. */ virtual const char* getDescription() const; + protected: virtual void initRenderer(); float getPercentWithBallPos(float location); @@ -195,6 +200,8 @@ protected: virtual void onSizeChanged(); void barRendererScaleChangedWithSize(); void progressBarRendererScaleChangedWithSize(); + virtual UIWidget* createCloneInstance(); + virtual void copySpecialProperties(UIWidget* model); protected: cocos2d::Node* _barRenderer; cocos2d::Node* _progressBarRenderer; diff --git a/cocos/gui/UITextField.cpp b/cocos/gui/UITextField.cpp index 4e1365592d..2eabd208b2 100644 --- a/cocos/gui/UITextField.cpp +++ b/cocos/gui/UITextField.cpp @@ -24,19 +24,17 @@ #include "gui/UITextField.h" - using namespace cocos2d; - namespace gui { UICCTextField::UICCTextField() -: m_bMaxLengthEnabled(false) -, m_nMaxLength(0) -, m_bPasswordEnabled(false) -, m_strPasswordStyleText("*") -, m_bAttachWithIME(false) -, m_bDetachWithIME(false) -, m_bInsertText(false) -, m_bDeleteBackward(false) +: _maxLengthEnabled(false) +, _maxLength(0) +, _passwordEnabled(false) +, _passwordStyleText("*") +, _attachWithIME(false) +, _detachWithIME(false) +, _insertText(false) +, _deleteBackward(false) { } @@ -64,26 +62,26 @@ UICCTextField * UICCTextField::create(const char *placeholder, const char *fontN void UICCTextField::onEnter() { - CCTextFieldTTF::setDelegate(this); + cocos2d::TextFieldTTF::setDelegate(this); } -bool UICCTextField::onTextFieldAttachWithIME(TextFieldTTF *pSender) +bool UICCTextField::onTextFieldAttachWithIME(cocos2d::TextFieldTTF *pSender) { setAttachWithIME(true); return false; } -bool UICCTextField::onTextFieldInsertText(TextFieldTTF *pSender, const char *text, int nLen) +bool UICCTextField::onTextFieldInsertText(cocos2d::TextFieldTTF *pSender, const char *text, int nLen) { if (nLen == 1 && strcmp(text, "\n") == 0) { return false; } setInsertText(true); - if (m_bMaxLengthEnabled) + if (_maxLengthEnabled) { - if (CCTextFieldTTF::getCharCount() >= m_nMaxLength) + if (cocos2d::TextFieldTTF::getCharCount() >= _maxLength) { return true; } @@ -92,13 +90,13 @@ bool UICCTextField::onTextFieldInsertText(TextFieldTTF *pSender, const char *tex return false; } -bool UICCTextField::onTextFieldDeleteBackward(TextFieldTTF *pSender, const char *delText, int nLen) +bool UICCTextField::onTextFieldDeleteBackward(cocos2d::TextFieldTTF *pSender, const char *delText, int nLen) { setDeleteBackward(true); return false; } -bool UICCTextField::onTextFieldDetachWithIME(TextFieldTTF *pSender) +bool UICCTextField::onTextFieldDetachWithIME(cocos2d::TextFieldTTF *pSender) { setDetachWithIME(true); return false; @@ -107,11 +105,11 @@ bool UICCTextField::onTextFieldDetachWithIME(TextFieldTTF *pSender) void UICCTextField::insertText(const char * text, int len) { std::string str_text = text; - int str_len = strlen(CCTextFieldTTF::getString()); + int str_len = strlen(cocos2d::TextFieldTTF::getString()); if (strcmp(text, "\n") != 0) { - if (m_bMaxLengthEnabled) + if (_maxLengthEnabled) { int multiple = 1; char value = text[0]; @@ -120,26 +118,26 @@ void UICCTextField::insertText(const char * text, int len) multiple = 3; } - if (str_len + len > m_nMaxLength * multiple) + if (str_len + len > _maxLength * multiple) { - str_text = str_text.substr(0, m_nMaxLength * multiple); - len = m_nMaxLength * multiple; + str_text = str_text.substr(0, _maxLength * multiple); + len = _maxLength * multiple; /* int mod = str_len % 3; int offset = (mod == 0) ? 0 : (3 - mod); int amount = str_len + offset; - str_text = str_text.substr(0, m_nMaxLength - amount); + str_text = str_text.substr(0, _maxLength - amount); // CCLOG("str_test = %s", str_text.c_str()); */ } } } - CCTextFieldTTF::insertText(str_text.c_str(), len); + cocos2d::TextFieldTTF::insertText(str_text.c_str(), len); // password - if (m_bPasswordEnabled) + if (_passwordEnabled) { - if (CCTextFieldTTF::getCharCount() > 0) + if (cocos2d::TextFieldTTF::getCharCount() > 0) { setPasswordText(_inputText->c_str()); } @@ -148,12 +146,12 @@ void UICCTextField::insertText(const char * text, int len) void UICCTextField::deleteBackward() { - CCTextFieldTTF::deleteBackward(); + cocos2d::TextFieldTTF::deleteBackward(); - if (CCTextFieldTTF::getCharCount() > 0) + if (cocos2d::TextFieldTTF::getCharCount() > 0) { // password - if (m_bPasswordEnabled) + if (_passwordEnabled) { setPasswordText(_inputText->c_str()); } @@ -162,47 +160,47 @@ void UICCTextField::deleteBackward() void UICCTextField::openIME() { - CCTextFieldTTF::attachWithIME(); + cocos2d::TextFieldTTF::attachWithIME(); } void UICCTextField::closeIME() { - CCTextFieldTTF::detachWithIME(); + cocos2d::TextFieldTTF::detachWithIME(); } void UICCTextField::setMaxLengthEnabled(bool enable) { - m_bMaxLengthEnabled = enable; + _maxLengthEnabled = enable; } bool UICCTextField::isMaxLengthEnabled() { - return m_bMaxLengthEnabled; + return _maxLengthEnabled; } void UICCTextField::setMaxLength(int length) { - m_nMaxLength = length; + _maxLength = length; } int UICCTextField::getMaxLength() { - return m_nMaxLength; + return _maxLength; } int UICCTextField::getCharCount() { - return CCTextFieldTTF::getCharCount(); + return cocos2d::TextFieldTTF::getCharCount(); } void UICCTextField::setPasswordEnabled(bool enable) { - m_bPasswordEnabled = enable; + _passwordEnabled = enable; } bool UICCTextField::isPasswordEnabled() { - return m_bPasswordEnabled; + return _passwordEnabled; } void UICCTextField::setPasswordStyleText(const char* styleText) @@ -216,7 +214,7 @@ void UICCTextField::setPasswordStyleText(const char* styleText) { return; } - m_strPasswordStyleText = styleText; + _passwordStyleText = styleText; } void UICCTextField::setPasswordText(const char *text) @@ -224,49 +222,49 @@ void UICCTextField::setPasswordText(const char *text) std::string tempStr; for (size_t i = 0; i < strlen(text); ++i) { - tempStr.append(m_strPasswordStyleText); + tempStr.append(_passwordStyleText); } - CCLabelTTF::setString(tempStr.c_str()); + cocos2d::LabelTTF::setString(tempStr.c_str()); } void UICCTextField::setAttachWithIME(bool attach) { - m_bAttachWithIME = attach; + _attachWithIME = attach; } bool UICCTextField::getAttachWithIME() { - return m_bAttachWithIME; + return _attachWithIME; } void UICCTextField::setDetachWithIME(bool detach) { - m_bDetachWithIME = detach; + _detachWithIME = detach; } bool UICCTextField::getDetachWithIME() { - return m_bDetachWithIME; + return _detachWithIME; } void UICCTextField::setInsertText(bool insert) { - m_bInsertText = insert; + _insertText = insert; } bool UICCTextField::getInsertText() { - return m_bInsertText; + return _insertText; } void UICCTextField::setDeleteBackward(bool deleteBackward) { - m_bDeleteBackward = deleteBackward; + _deleteBackward = deleteBackward; } bool UICCTextField::getDeleteBackward() { - return m_bDeleteBackward; + return _deleteBackward; } @@ -278,7 +276,8 @@ _touchWidth(0.0f), _touchHeight(0.0f), _useTouchArea(false), _eventListener(NULL), -_eventSelector(NULL) +_eventSelector(NULL), +_passwordStyleText("") { } @@ -315,7 +314,7 @@ void UITextField::initRenderer() _renderer->addChild(_textFieldRenderer); } -void UITextField::setTouchSize(const Size &size) +void UITextField::setTouchSize(const cocos2d::Size &size) { _useTouchArea = true; _touchWidth = size.width; @@ -361,7 +360,7 @@ const char* UITextField::getStringValue() return _textFieldRenderer->getString(); } -bool UITextField::onTouchBegan(const Point &touchPoint) +bool UITextField::onTouchBegan(const cocos2d::Point &touchPoint) { bool pass = UIWidget::onTouchBegan(touchPoint); _textFieldRenderer->attachWithIME(); @@ -401,6 +400,8 @@ bool UITextField::isPasswordEnabled() void UITextField::setPasswordStyleText(const char *styleText) { _textFieldRenderer->setPasswordStyleText(styleText); + + _passwordStyleText = styleText; } void UITextField::update(float dt) @@ -489,7 +490,7 @@ void UITextField::insertTextEvent() { if (_eventListener && _eventSelector) { - (_eventListener->*_eventSelector)(this, TEXTFIELD_EVENT_INDERT_TEXT); + (_eventListener->*_eventSelector)(this, TEXTFIELD_EVENT_INSERT_TEXT); } } @@ -501,19 +502,19 @@ void UITextField::deleteBackwardEvent() } } -void UITextField::addEventListener(Object *target, SEL_TextFieldEvent selecor) +void UITextField::addEventListener(cocos2d::Object *target, SEL_TextFieldEvent selecor) { _eventListener = target; _eventSelector = selecor; } -void UITextField::setAnchorPoint(const Point &pt) +void UITextField::setAnchorPoint(const cocos2d::Point &pt) { UIWidget::setAnchorPoint(pt); _textFieldRenderer->setAnchorPoint(pt); } -void UITextField::setColor(const Color3B &color) +void UITextField::setColor(const cocos2d::Color3B &color) { UIWidget::setColor(color); _textFieldRenderer->setColor(color); @@ -539,7 +540,7 @@ void UITextField::textfieldRendererScaleChangedWithSize() } else { - Size textureSize = getContentSize(); + cocos2d::Size textureSize = getContentSize(); if (textureSize.width <= 0.0f || textureSize.height <= 0.0f) { _textFieldRenderer->setScale(1.0f); @@ -552,12 +553,12 @@ void UITextField::textfieldRendererScaleChangedWithSize() } } -const Size& UITextField::getContentSize() const +const cocos2d::Size& UITextField::getContentSize() const { return _textFieldRenderer->getContentSize(); } -Node* UITextField::getVirtualRenderer() +cocos2d::Node* UITextField::getVirtualRenderer() { return _textFieldRenderer; } @@ -567,4 +568,34 @@ const char* UITextField::getDescription() const return "TextField"; } +void UITextField::attachWithIME() +{ + _textFieldRenderer->attachWithIME(); +} + +UIWidget* UITextField::createCloneInstance() +{ + return UITextField::create(); +} + +void UITextField::copySpecialProperties(UIWidget *widget) +{ + UITextField* textField = dynamic_cast(widget); + if (textField) + { + setText(textField->_textFieldRenderer->getString()); + setPlaceHolder(textField->getStringValue()); + setFontSize(textField->_textFieldRenderer->getFontSize()); + setFontName(textField->_textFieldRenderer->getFontName()); + setMaxLengthEnabled(textField->isMaxLengthEnabled()); + setMaxLength(textField->getMaxLength()); + setPasswordEnabled(textField->isPasswordEnabled()); + setPasswordStyleText(textField->_passwordStyleText.c_str()); + setAttachWithIME(textField->getAttachWithIME()); + setDetachWithIME(textField->getDetachWithIME()); + setInsertText(textField->getInsertText()); + setDeleteBackward(textField->getDeleteBackward()); + } +} + } \ No newline at end of file diff --git a/cocos/gui/UITextField.h b/cocos/gui/UITextField.h index 31b7052443..ac5641114f 100644 --- a/cocos/gui/UITextField.h +++ b/cocos/gui/UITextField.h @@ -30,6 +30,10 @@ namespace gui { +/** + * @js NA + * @lua NA + */ class UICCTextField: public cocos2d::TextFieldTTF, public cocos2d::TextFieldDelegate { public: @@ -70,32 +74,32 @@ public: bool getInsertText(); void setDeleteBackward(bool deleteBackward); bool getDeleteBackward(); - - protected: - bool m_bMaxLengthEnabled; - int m_nMaxLength; - bool m_bPasswordEnabled; - std::string m_strPasswordStyleText; - bool m_bAttachWithIME; - bool m_bDetachWithIME; - bool m_bInsertText; - bool m_bDeleteBackward; + bool _maxLengthEnabled; + int _maxLength; + bool _passwordEnabled; + std::string _passwordStyleText; + bool _attachWithIME; + bool _detachWithIME; + bool _insertText; + bool _deleteBackward; }; - typedef enum { TEXTFIELD_EVENT_ATTACH_WITH_IME, TEXTFIELD_EVENT_DETACH_WITH_IME, - TEXTFIELD_EVENT_INDERT_TEXT, + TEXTFIELD_EVENT_INSERT_TEXT, TEXTFIELD_EVENT_DELETE_BACKWARD, }TextFiledEventType; typedef void (cocos2d::Object::*SEL_TextFieldEvent)(cocos2d::Object*, TextFiledEventType); #define textfieldeventselector(_SELECTOR) (SEL_TextFieldEvent)(&_SELECTOR) -//class UITextField : public UIWidget +/** class UITextField : public UIWidget +* @js NA +* @lua NA +*/ class UITextField : public UIWidget { public: @@ -129,22 +133,19 @@ public: bool getDeleteBackward(); void setDeleteBackward(bool deleteBackward); void addEventListener(cocos2d::Object* target, SEL_TextFieldEvent selecor); + virtual void setAnchorPoint(const cocos2d::Point &pt); virtual void setColor(const cocos2d::Color3B &color); virtual void setOpacity(int opacity); + /** * Returns the "class name" of widget. */ virtual const char* getDescription() const; - /*compatibel*/ - /** - * These methods will be removed - */ - void setMaxLengthEnable(bool is){setMaxLengthEnabled(is);}; - void setPasswordEnable(bool is){setPasswordEnabled(is);}; - /************/ + virtual const cocos2d::Size& getContentSize() const; virtual cocos2d::Node* getVirtualRenderer(); + void attachWithIME(); protected: // event void attachWithIMEEvent(); @@ -153,9 +154,11 @@ protected: void deleteBackwardEvent(); virtual void onSizeChanged(); void textfieldRendererScaleChangedWithSize(); + virtual UIWidget* createCloneInstance(); + virtual void copySpecialProperties(UIWidget* model); protected: UICCTextField* _textFieldRenderer; - + float _touchWidth; float _touchHeight; bool _useTouchArea; @@ -163,6 +166,7 @@ protected: cocos2d::Object* _eventListener; SEL_TextFieldEvent _eventSelector; + std::string _passwordStyleText; }; } diff --git a/cocos/gui/UIWidget.cpp b/cocos/gui/UIWidget.cpp index ea9651582b..00ee50680c 100644 --- a/cocos/gui/UIWidget.cpp +++ b/cocos/gui/UIWidget.cpp @@ -24,11 +24,9 @@ #include "gui/UIWidget.h" #include "gui/UILayer.h" -#include "gui/Layout.h" +#include "gui/UILayout.h" #include "gui/UIHelper.h" - using namespace cocos2d; - namespace gui { #define DYNAMIC_CAST_CCBLENDPROTOCOL dynamic_cast(_renderer) @@ -45,31 +43,31 @@ _touchEnabled(false), _touchPassedEnabled(false), _focus(false), _widgetZOrder(0), -_anchorPoint(Point(0.5f, 0.5f)), +_anchorPoint(cocos2d::Point(0.5f, 0.5f)), _widgetParent(NULL), _brightStyle(BRIGHT_NONE), _updateEnabled(false), _renderer(NULL), -_touchStartPos(Point::ZERO), -_touchMovePos(Point::ZERO), -_touchEndPos(Point::ZERO), +_touchStartPos(cocos2d::Point::ZERO), +_touchMovePos(cocos2d::Point::ZERO), +_touchEndPos(cocos2d::Point::ZERO), _touchEventListener(NULL), _touchEventSelector(NULL), _widgetTag(-1), _name("default"), _widgetType(WidgetTypeWidget), _actionTag(0), -_size(Size::ZERO), -_customSize(Size::ZERO), -_layoutParameter(NULL), +_size(cocos2d::Size::ZERO), +_customSize(cocos2d::Size::ZERO), +_layoutParameterDictionary(NULL), _ignoreSize(false), _children(NULL), _affectByClipping(false), _scheduler(NULL), _sizeType(SIZE_ABSOLUTE), -_sizePercent(Point::ZERO), +_sizePercent(cocos2d::Point::ZERO), _positionType(POSITION_ABSOLUTE), -_positionPercent(Point::ZERO), +_positionPercent(cocos2d::Point::ZERO), _isRunning(false) { @@ -79,7 +77,8 @@ UIWidget::~UIWidget() { releaseResoures(); setParent(NULL); - CC_SAFE_RELEASE_NULL(_layoutParameter); + _layoutParameterDictionary->removeAllObjects(); + CC_SAFE_RELEASE(_layoutParameterDictionary); CC_SAFE_RELEASE(_scheduler); } @@ -97,12 +96,14 @@ UIWidget* UIWidget::create() bool UIWidget::init() { - _children = Array::create(); + _children = cocos2d::Array::create(); _children->retain(); + _layoutParameterDictionary = cocos2d::Dictionary::create(); + CC_SAFE_RETAIN(_layoutParameterDictionary); initRenderer(); _renderer->retain(); _renderer->setZOrder(_widgetZOrder); - RGBAProtocol* renderRGBA = DYNAMIC_CAST_CCRGBAPROTOCOL; + cocos2d::RGBAProtocol* renderRGBA = DYNAMIC_CAST_CCRGBAPROTOCOL; if (renderRGBA) { renderRGBA->setCascadeColorEnabled(true); @@ -110,14 +111,13 @@ bool UIWidget::init() } setBright(true); ignoreContentAdaptWithSize(true); - _scheduler = Director::getInstance()->getScheduler(); + _scheduler = cocos2d::Director::getInstance()->getScheduler(); CC_SAFE_RETAIN(_scheduler); return true; } void UIWidget::releaseResoures() { - setUpdateEnabled(false); removeAllChildren(); _children->release(); _renderer->removeAllChildrenWithCleanup(true); @@ -157,7 +157,7 @@ bool UIWidget::addChild(UIWidget *child) else { bool seekSucceed = false; - ccArray* arrayChildren = _children->data; + cocos2d::ccArray* arrayChildren = _children->data; for (int i=childrenCount-1; i>=0; --i) { UIWidget* widget = (UIWidget*)(arrayChildren->arr[i]); @@ -203,7 +203,7 @@ bool UIWidget::removeChild(UIWidget *child) { child->onExit(); } - child->disableUpdate(); + child->setUpdateEnabled(false); child->setParent(NULL); _renderer->removeChild(child->getRenderer()); _children->removeObject(child); @@ -246,7 +246,7 @@ void UIWidget::reorderChild(UIWidget* child) else { bool seekSucceed = false; - ccArray* arrayChildren = _children->data; + cocos2d::ccArray* arrayChildren = _children->data; for (int i=childrenCount-1; i>=0; --i) { UIWidget* widget = (UIWidget*)(arrayChildren->arr[i]); @@ -274,21 +274,6 @@ void UIWidget::reorderChild(UIWidget* child) CC_SAFE_RELEASE(child); } -void UIWidget::disableUpdate() -{ - if (_scheduler) - { - _scheduler->unscheduleUpdateForTarget(this); - } - int childrenCount = _children->data->num; - ccArray* arrayChildren = _children->data; - for (int i=0; iarr[i]); - child->disableUpdate(); - } -} - void UIWidget::setEnabled(bool enabled) { _enabled = enabled; @@ -299,9 +284,9 @@ void UIWidget::setEnabled(bool enabled) } else { - dynamic_cast(_renderer)->setEnabled(enabled); + dynamic_cast(_renderer)->setEnabled(enabled); } - ccArray* arrayChildren = _children->data; + cocos2d::ccArray* arrayChildren = _children->data; int childrenCount = arrayChildren->num; for (int i = 0; i < childrenCount; i++) { @@ -312,15 +297,15 @@ void UIWidget::setEnabled(bool enabled) UIWidget* UIWidget::getChildByName(const char *name) { - return CCUIHELPER->seekWidgetByName(this, name); + return UIHelper::seekWidgetByName(this, name); } UIWidget* UIWidget::getChildByTag(int tag) { - return CCUIHELPER->seekWidgetByTag(this, tag); + return UIHelper::seekWidgetByTag(this, tag); } -Array* UIWidget::getChildren() +cocos2d::Array* UIWidget::getChildren() { return _children; } @@ -330,7 +315,7 @@ void UIWidget::initRenderer() _renderer = GUIRenderer::create(); } -void UIWidget::setSize(const Size &size) +void UIWidget::setSize(const cocos2d::Size &size) { _customSize = size; if (_ignoreSize) @@ -343,19 +328,19 @@ void UIWidget::setSize(const Size &size) } if (_isRunning) { - _sizePercent = (_widgetParent == NULL) ? Point::ZERO : Point(_customSize.width / _widgetParent->getSize().width, _customSize.height / _widgetParent->getSize().height); + _sizePercent = (_widgetParent == NULL) ? cocos2d::Point::ZERO : cocos2d::Point(_customSize.width / _widgetParent->getSize().width, _customSize.height / _widgetParent->getSize().height); } onSizeChanged(); } -void UIWidget::setSizePercent(const Point &percent) +void UIWidget::setSizePercent(const cocos2d::Point &percent) { _sizePercent = percent; if (!_isRunning) { return; } - Size cSize = (_widgetParent == NULL) ? Size::ZERO : Size(_widgetParent->getSize().width * percent.x , _widgetParent->getSize().height * percent.y); + cocos2d::Size cSize = (_widgetParent == NULL) ? cocos2d::Size::ZERO : cocos2d::Size(_widgetParent->getSize().width * percent.x , _widgetParent->getSize().height * percent.y); if (_ignoreSize) { _size = getContentSize(); @@ -381,11 +366,11 @@ void UIWidget::updateSizeAndPosition() { _size = _customSize; } - _sizePercent = (_widgetParent == NULL) ? Point::ZERO : Point(_customSize.width / _widgetParent->getSize().width, _customSize.height / _widgetParent->getSize().height); + _sizePercent = (_widgetParent == NULL) ? cocos2d::Point::ZERO : cocos2d::Point(_customSize.width / _widgetParent->getSize().width, _customSize.height / _widgetParent->getSize().height); break; case SIZE_PERCENT: { - Size cSize = (_widgetParent == NULL) ? Size::ZERO : Size(_widgetParent->getSize().width * _sizePercent.x , _widgetParent->getSize().height * _sizePercent.y); + cocos2d::Size cSize = (_widgetParent == NULL) ? cocos2d::Size::ZERO : cocos2d::Size(_widgetParent->getSize().width * _sizePercent.x , _widgetParent->getSize().height * _sizePercent.y); if (_ignoreSize) { _size = getContentSize(); @@ -401,16 +386,16 @@ void UIWidget::updateSizeAndPosition() break; } onSizeChanged(); - Point absPos = getPosition(); + cocos2d::Point absPos = getPosition(); switch (_positionType) { case POSITION_ABSOLUTE: - _positionPercent = (_widgetParent == NULL) ? Point::ZERO : Point(absPos.x / _widgetParent->getSize().width, absPos.y / _widgetParent->getSize().height); + _positionPercent = (_widgetParent == NULL) ? cocos2d::Point::ZERO : cocos2d::Point(absPos.x / _widgetParent->getSize().width, absPos.y / _widgetParent->getSize().height); break; case POSITION_PERCENT: { - Size parentSize = _widgetParent->getSize(); - absPos = Point(parentSize.width * _positionPercent.x, parentSize.height * _positionPercent.y); + cocos2d::Size parentSize = _widgetParent->getSize(); + absPos = cocos2d::Point(parentSize.width * _positionPercent.x, parentSize.height * _positionPercent.y); } break; default: @@ -434,7 +419,7 @@ void UIWidget::ignoreContentAdaptWithSize(bool ignore) _ignoreSize = ignore; if (_ignoreSize) { - Size s = getContentSize(); + cocos2d::Size s = getContentSize(); _size = s; } else @@ -449,27 +434,27 @@ bool UIWidget::isIgnoreContentAdaptWithSize() const return _ignoreSize; } -const Size& UIWidget::getSize() const +const cocos2d::Size& UIWidget::getSize() const { return _size; } -const Point& UIWidget::getSizePercent() const +const cocos2d::Point& UIWidget::getSizePercent() const { return _sizePercent; } -Point UIWidget::getWorldPosition() +cocos2d::Point UIWidget::getWorldPosition() { - return _renderer->convertToWorldSpace(Point::ZERO); + return _renderer->convertToWorldSpace(cocos2d::Point::ZERO); } -Point UIWidget::convertToWorldSpace(const Point& pt) +cocos2d::Point UIWidget::convertToWorldSpace(const cocos2d::Point& pt) { return _renderer->convertToWorldSpace(pt); } -Node* UIWidget::getVirtualRenderer() +cocos2d::Node* UIWidget::getVirtualRenderer() { return _renderer; } @@ -479,7 +464,7 @@ void UIWidget::onSizeChanged() } -const Size& UIWidget::getContentSize() const +const cocos2d::Size& UIWidget::getContentSize() const { return _size; } @@ -511,6 +496,10 @@ bool UIWidget::isTouchEnabled() const void UIWidget::setUpdateEnabled(bool enable) { + if (enable == _updateEnabled) + { + return; + } _updateEnabled = enable; if (enable) { @@ -616,7 +605,7 @@ void UIWidget::didNotSelectSelf() } -bool UIWidget::onTouchBegan(const Point &touchPoint) +bool UIWidget::onTouchBegan(const cocos2d::Point &touchPoint) { setFocused(true); _touchStartPos.x = touchPoint.x; @@ -629,7 +618,7 @@ bool UIWidget::onTouchBegan(const Point &touchPoint) return _touchPassedEnabled; } -void UIWidget::onTouchMoved(const Point &touchPoint) +void UIWidget::onTouchMoved(const cocos2d::Point &touchPoint) { _touchMovePos.x = touchPoint.x; _touchMovePos.y = touchPoint.y; @@ -641,7 +630,7 @@ void UIWidget::onTouchMoved(const Point &touchPoint) moveEvent(); } -void UIWidget::onTouchEnded(const Point &touchPoint) +void UIWidget::onTouchEnded(const cocos2d::Point &touchPoint) { _touchEndPos.x = touchPoint.x; _touchEndPos.y = touchPoint.y; @@ -661,13 +650,13 @@ void UIWidget::onTouchEnded(const Point &touchPoint) } } -void UIWidget::onTouchCancelled(const Point &touchPoint) +void UIWidget::onTouchCancelled(const cocos2d::Point &touchPoint) { setFocused(false); cancelUpEvent(); } -void UIWidget::onTouchLongClicked(const Point &touchPoint) +void UIWidget::onTouchLongClicked(const cocos2d::Point &touchPoint) { longClickEvent(); } @@ -709,31 +698,31 @@ void UIWidget::longClickEvent() } -void UIWidget::addTouchEventListener(Object *target, SEL_TouchEvent selector) +void UIWidget::addTouchEventListener(cocos2d::Object *target, SEL_TouchEvent selector) { _touchEventListener = target; _touchEventSelector = selector; } -Node* UIWidget::getRenderer() +cocos2d::Node* UIWidget::getRenderer() { return _renderer; } -void UIWidget::addRenderer(Node* renderer, int zOrder) +void UIWidget::addRenderer(cocos2d::Node* renderer, int zOrder) { _renderer->addChild(renderer, zOrder); } -void UIWidget::removeRenderer(Node* renderer, bool cleanup) +void UIWidget::removeRenderer(cocos2d::Node* renderer, bool cleanup) { _renderer->removeChild(renderer,cleanup); } -bool UIWidget::hitTest(const Point &pt) +bool UIWidget::hitTest(const cocos2d::Point &pt) { - Point nsp = _renderer->convertToNodeSpace(pt); - Rect bb = Rect(-_size.width * _anchorPoint.x, -_size.height * _anchorPoint.y, _size.width, _size.height); + cocos2d::Point nsp = _renderer->convertToNodeSpace(pt); + cocos2d::Rect bb = cocos2d::Rect(-_size.width * _anchorPoint.x, -_size.height * _anchorPoint.y, _size.width, _size.height); if (nsp.x >= bb.origin.x && nsp.x <= bb.origin.x + bb.size.width && nsp.y >= bb.origin.y && nsp.y <= bb.origin.y + bb.size.height) { return true; @@ -741,14 +730,14 @@ bool UIWidget::hitTest(const Point &pt) return false; } -bool UIWidget::clippingParentAreaContainPoint(const Point &pt) +bool UIWidget::clippingParentAreaContainPoint(const cocos2d::Point &pt) { _affectByClipping = false; UIWidget* parent = getParent(); UIWidget* clippingParent = NULL; while (parent) { - Layout* layoutParent = dynamic_cast(parent); + UILayout* layoutParent = dynamic_cast(parent); if (layoutParent) { if (layoutParent->isClippingEnabled()) @@ -783,7 +772,7 @@ bool UIWidget::clippingParentAreaContainPoint(const Point &pt) return true; } -void UIWidget::checkChildInfo(int handleState, UIWidget *sender, const Point &touchPoint) +void UIWidget::checkChildInfo(int handleState, UIWidget *sender, const cocos2d::Point &touchPoint) { if (_widgetParent) { @@ -791,27 +780,27 @@ void UIWidget::checkChildInfo(int handleState, UIWidget *sender, const Point &to } } -void UIWidget::setPosition(const Point &pos) +void UIWidget::setPosition(const cocos2d::Point &pos) { if (_isRunning) { - _positionPercent = (_widgetParent == NULL) ? Point::ZERO : Point(pos.x / _widgetParent->getSize().width, pos.y / _widgetParent->getSize().height); + _positionPercent = (_widgetParent == NULL) ? cocos2d::Point::ZERO : cocos2d::Point(pos.x / _widgetParent->getSize().width, pos.y / _widgetParent->getSize().height); } _renderer->setPosition(pos); } -void UIWidget::setPositionPercent(const Point &percent) +void UIWidget::setPositionPercent(const cocos2d::Point &percent) { _positionPercent = percent; if (_isRunning) { - Size parentSize = _widgetParent->getSize(); - Point absPos = Point(parentSize.width * _positionPercent.x, parentSize.height * _positionPercent.y); + cocos2d::Size parentSize = _widgetParent->getSize(); + cocos2d::Point absPos = cocos2d::Point(parentSize.width * _positionPercent.x, parentSize.height * _positionPercent.y); _renderer->setPosition(absPos); } } -void UIWidget::setAnchorPoint(const Point &pt) +void UIWidget::setAnchorPoint(const cocos2d::Point &pt) { _anchorPoint = pt; _renderer->setAnchorPoint(pt); @@ -822,12 +811,12 @@ void UIWidget::updateAnchorPoint() setAnchorPoint(_anchorPoint); } -const Point& UIWidget::getPosition() +const cocos2d::Point& UIWidget::getPosition() { return _renderer->getPosition(); } -const Point& UIWidget::getPositionPercent() +const cocos2d::Point& UIWidget::getPositionPercent() { return _positionPercent; } @@ -842,7 +831,7 @@ PositionType UIWidget::getPositionType() const return _positionType; } -const Point& UIWidget::getAnchorPoint() +const cocos2d::Point& UIWidget::getAnchorPoint() { return _anchorPoint; } @@ -930,36 +919,12 @@ bool UIWidget::isEnabled() const float UIWidget::getLeftInParent() { - float leftPos = 0.0f; - switch (_widgetType) - { - case WidgetTypeWidget: - leftPos = getPosition().x - getAnchorPoint().x * _size.width; - break; - case WidgetTypeContainer: - leftPos = getPosition().x; - break; - default: - break; - } - return leftPos; + return getPosition().x - getAnchorPoint().x * _size.width;; } float UIWidget::getBottomInParent() { - float bottomPos = 0.0f; - switch (_widgetType) - { - case WidgetTypeWidget: - bottomPos = getPosition().y - getAnchorPoint().y * _size.height; - break; - case WidgetTypeContainer: - bottomPos = getPosition().y; - break; - default: - break; - } - return bottomPos; + return getPosition().y - getAnchorPoint().y * _size.height;; } float UIWidget::getRightInParent() @@ -982,17 +947,17 @@ void UIWidget::setParent(UIWidget* parent) _widgetParent = parent; } -Action* UIWidget::runAction(Action *action) +cocos2d::Action* UIWidget::runAction(cocos2d::Action *action) { return _renderer->runAction(action); } -void UIWidget::setActionManager(ActionManager *actionManager) +void UIWidget::setActionManager(cocos2d::ActionManager *actionManager) { _renderer->setActionManager(actionManager); } -ActionManager* UIWidget::getActionManager() +cocos2d::ActionManager* UIWidget::getActionManager() { return _renderer->getActionManager(); } @@ -1002,7 +967,7 @@ void UIWidget::stopAllActions() _renderer->stopAllActions(); } -void UIWidget::stopAction(Action *action) +void UIWidget::stopAction(cocos2d::Action *action) { _renderer->stopAction(action); } @@ -1012,33 +977,33 @@ void UIWidget::stopActionByTag(int tag) _renderer->stopActionByTag(tag); } -Action* UIWidget::getActionByTag(int tag) +cocos2d::Action* UIWidget::getActionByTag(int tag) { return _renderer->getActionByTag(tag); } -void UIWidget::setColor(const Color3B &color) +void UIWidget::setColor(const cocos2d::Color3B &color) { - RGBAProtocol* rgbap = DYNAMIC_CAST_CCRGBAPROTOCOL; + cocos2d::RGBAProtocol* rgbap = DYNAMIC_CAST_CCRGBAPROTOCOL; if (rgbap) { rgbap->setColor(color); } } -const Color3B& UIWidget::getColor() +const cocos2d::Color3B& UIWidget::getColor() { - RGBAProtocol* rgbap = DYNAMIC_CAST_CCRGBAPROTOCOL; + cocos2d::RGBAProtocol* rgbap = DYNAMIC_CAST_CCRGBAPROTOCOL; if (rgbap) { return rgbap->getColor(); } - return Color3B::WHITE; + return cocos2d::Color3B::WHITE; } void UIWidget::setOpacity(int opacity) { - RGBAProtocol* rgbap = DYNAMIC_CAST_CCRGBAPROTOCOL; + cocos2d::RGBAProtocol* rgbap = DYNAMIC_CAST_CCRGBAPROTOCOL; if (rgbap) { rgbap->setOpacity(opacity); @@ -1047,7 +1012,7 @@ void UIWidget::setOpacity(int opacity) int UIWidget::getOpacity() { - RGBAProtocol* rgbap = DYNAMIC_CAST_CCRGBAPROTOCOL; + cocos2d::RGBAProtocol* rgbap = DYNAMIC_CAST_CCRGBAPROTOCOL; if (rgbap) { return rgbap->getOpacity(); @@ -1057,7 +1022,7 @@ int UIWidget::getOpacity() bool UIWidget::isCascadeOpacityEnabled() { - RGBAProtocol* rgbap = DYNAMIC_CAST_CCRGBAPROTOCOL; + cocos2d::RGBAProtocol* rgbap = DYNAMIC_CAST_CCRGBAPROTOCOL; if (rgbap) { return rgbap->isCascadeOpacityEnabled(); @@ -1067,7 +1032,7 @@ bool UIWidget::isCascadeOpacityEnabled() void UIWidget::setCascadeOpacityEnabled(bool cascadeOpacityEnabled) { - RGBAProtocol* rgbap = DYNAMIC_CAST_CCRGBAPROTOCOL; + cocos2d::RGBAProtocol* rgbap = DYNAMIC_CAST_CCRGBAPROTOCOL; if (rgbap) { rgbap->setCascadeOpacityEnabled(cascadeOpacityEnabled); @@ -1076,7 +1041,7 @@ void UIWidget::setCascadeOpacityEnabled(bool cascadeOpacityEnabled) bool UIWidget::isCascadeColorEnabled() { - RGBAProtocol* rgbap = DYNAMIC_CAST_CCRGBAPROTOCOL; + cocos2d::RGBAProtocol* rgbap = DYNAMIC_CAST_CCRGBAPROTOCOL; if (rgbap) { return rgbap->isCascadeColorEnabled(); @@ -1086,33 +1051,33 @@ bool UIWidget::isCascadeColorEnabled() void UIWidget::setCascadeColorEnabled(bool cascadeColorEnabled) { - RGBAProtocol* rgbap = DYNAMIC_CAST_CCRGBAPROTOCOL; + cocos2d::RGBAProtocol* rgbap = DYNAMIC_CAST_CCRGBAPROTOCOL; if (rgbap) { rgbap->setCascadeColorEnabled(cascadeColorEnabled); } } -void UIWidget::setBlendFunc(BlendFunc blendFunc) +void UIWidget::setBlendFunc(cocos2d::BlendFunc blendFunc) { - BlendProtocol * blendNode = DYNAMIC_CAST_CCBLENDPROTOCOL; + cocos2d::BlendProtocol * blendNode = DYNAMIC_CAST_CCBLENDPROTOCOL; if (blendNode) { blendNode->setBlendFunc(blendFunc); } } -const Point& UIWidget::getTouchStartPos() +const cocos2d::Point& UIWidget::getTouchStartPos() { return _touchStartPos; } -const Point& UIWidget::getTouchMovePos() +const cocos2d::Point& UIWidget::getTouchMovePos() { return _touchMovePos; } -const Point& UIWidget::getTouchEndPos() +const cocos2d::Point& UIWidget::getTouchEndPos() { return _touchEndPos; } @@ -1142,19 +1107,14 @@ WidgetType UIWidget::getWidgetType() const return _widgetType; } -void UIWidget::setLayoutParameter(LayoutParameter *parameter) +void UIWidget::setLayoutParameter(UILayoutParameter *parameter) { - if (_layoutParameter) - { - CC_SAFE_RELEASE_NULL(_layoutParameter); - } - _layoutParameter = parameter; - CC_SAFE_RETAIN(_layoutParameter); + _layoutParameterDictionary->setObject(parameter, parameter->getLayoutType()); } -LayoutParameter* UIWidget::getLayoutParameter() +UILayoutParameter* UIWidget::getLayoutParameter(LayoutParameterType type) { - return _layoutParameter; + return dynamic_cast(_layoutParameterDictionary->objectForKey(type)); } const char* UIWidget::getDescription() const @@ -1162,6 +1122,71 @@ const char* UIWidget::getDescription() const return "Widget"; } +UIWidget* UIWidget::clone() +{ + UIWidget* clonedWidget = createCloneInstance(); + clonedWidget->copyProperties(this); + clonedWidget->copyClonedWidgetChildren(this); + return clonedWidget; +} + +UIWidget* UIWidget::createCloneInstance() +{ + return UIWidget::create(); +} + +void UIWidget::copyClonedWidgetChildren(UIWidget* model) +{ + cocos2d::ccArray* arrayWidgetChildren = model->getChildren()->data; + int length = arrayWidgetChildren->num; + for (int i=0; iarr[i]); + addChild(child->clone()); + } +} + +void UIWidget::copySpecialProperties(UIWidget* model) +{ + +} + +void UIWidget::copyProperties(UIWidget *widget) +{ + setEnabled(widget->isEnabled()); + setVisible(widget->isVisible()); + setBright(widget->isBright()); + setTouchEnabled(widget->isTouchEnabled()); + _touchPassedEnabled = false; + setZOrder(widget->getZOrder()); + setUpdateEnabled(widget->isUpdateEnabled()); + setTag(widget->getTag()); + setName(widget->getName()); + setActionTag(widget->getActionTag()); + _ignoreSize = widget->_ignoreSize; + _size = widget->_size; + _customSize = widget->_customSize; + copySpecialProperties(widget); + _sizeType = widget->getSizeType(); + _sizePercent = widget->_sizePercent; + _positionType = widget->_positionType; + _positionPercent = widget->_positionPercent; + setPosition(widget->getPosition()); + setAnchorPoint(widget->getAnchorPoint()); + setScaleX(widget->getScaleX()); + setScaleY(widget->getScaleY()); + setRotation(widget->getRotation()); + setRotationX(widget->getRotationX()); + setRotationY(widget->getRotationY()); + setFlipX(widget->isFlipX()); + setFlipY(widget->isFlipY()); + setColor(widget->getColor()); + setOpacity(widget->getOpacity()); + setCascadeOpacityEnabled(widget->isCascadeOpacityEnabled()); + setCascadeColorEnabled(widget->isCascadeColorEnabled()); + onSizeChanged(); +} + /*temp action*/ void UIWidget::setActionTag(int tag) { @@ -1214,7 +1239,7 @@ void GUIRenderer::visit() { return; } - NodeRGBA::visit(); + cocos2d::NodeRGBA::visit(); } - + } diff --git a/cocos/gui/UIWidget.h b/cocos/gui/UIWidget.h index ebc7e8a665..d8f35578f4 100644 --- a/cocos/gui/UIWidget.h +++ b/cocos/gui/UIWidget.h @@ -27,9 +27,9 @@ #include "cocos2d.h" #include "gui/UILayoutDefine.h" -#include "gui/LayoutParameter.h" -namespace gui { +#include "gui/UILayoutParameter.h" +namespace gui { typedef enum { @@ -72,11 +72,10 @@ typedef enum typedef void (cocos2d::Object::*SEL_TouchEvent)(cocos2d::Object*,TouchEventType); #define toucheventselector(_SELECTOR) (SEL_TouchEvent)(&_SELECTOR) - -//class UILayer; -/*temp action*/ -class UIActionNode; - +/** +* @js NA +* @lua NA +*/ class UIWidget : public cocos2d::Object { public: @@ -260,12 +259,7 @@ public: * Removes all children from the container, and do a cleanup to all running actions depending on the cleanup parameter. */ virtual void removeAllChildren(); - - /** - * Unschedules the "update" method. - */ - void disableUpdate(); - + /** * Reorders a child according to a new z value. * @@ -304,27 +298,27 @@ public: /** * Gets the renderer of widget * - * renderer is a CCNode, it's for drawing + * renderer is a Node, it's for drawing * - * @return a CCNode object + * @return a Node object */ cocos2d::Node* getRenderer(); /** - * Add a CCNode for rendering. + * Add a Node for rendering. * - * renderer is a CCNode, it's for drawing + * renderer is a Node, it's for drawing * * @param renderer A render node * - * @param zOrder Z order for drawing priority. Please refer to CCNode::setZOrder(int) + * @param zOrder Z order for drawing priority. Please refer to Node::setZOrder(int) */ void addRenderer(cocos2d::Node* renderer, int zOrder); /** - * Remove a CCNode from widget. + * Remove a Node from widget. * - * renderer is a CCNode, it's for drawing + * renderer is a Node, it's for drawing * * @param renderer A render node which needs to be removed * @@ -359,7 +353,7 @@ public: /** * Changes the position (x,y) of the widget in OpenGL coordinates * - * Usually we use ccp(x,y) to compose Point object. + * Usually we use p(x,y) to compose Point object. * The original point (0,0) is at the left-bottom corner of screen. * * @param position The position (x,y) of the widget in OpenGL coordinates @@ -369,7 +363,7 @@ public: /** * Changes the position (x,y) of the widget in OpenGL coordinates * - * Usually we use ccp(x,y) to compose Point object. + * Usually we use p(x,y) to compose Point object. * The original point (0,0) is at the left-bottom corner of screen. * * @param percent The percent (x,y) of the widget in OpenGL coordinates @@ -809,15 +803,21 @@ public: * @see LayoutParameter * * @param LayoutParameter pointer + * + * @param type Relative or Linear */ - void setLayoutParameter(LayoutParameter* parameter); + void setLayoutParameter(UILayoutParameter* parameter); /** * Gets LayoutParameter of widget. * * @see LayoutParameter + * + * @param type Relative or Linear + * + * @return LayoutParameter */ - LayoutParameter* getLayoutParameter(); + UILayoutParameter* getLayoutParameter(LayoutParameterType type); /** * Ignore the widget size @@ -850,7 +850,7 @@ public: * * For example, a button's Virtual Renderer is it's texture renderer. * - * @return CCNode pointer. + * @return Node pointer. */ virtual cocos2d::Node* getVirtualRenderer(); @@ -871,14 +871,15 @@ public: */ virtual const cocos2d::Size& getContentSize() const; - virtual void onEnter(); - virtual void onExit(); - /** * Returns the "class name" of widget. */ virtual const char* getDescription() const; + UIWidget* clone(); + + virtual void onEnter(); + virtual void onExit(); /*temp action*/ void setActionTag(int tag); int getActionTag(); @@ -913,6 +914,10 @@ protected: */ virtual void releaseResoures(); void updateSizeAndPosition(); + void copyProperties(UIWidget* model); + virtual UIWidget* createCloneInstance(); + virtual void copySpecialProperties(UIWidget* model); + virtual void copyClonedWidgetChildren(UIWidget* model); protected: bool _enabled; ///< Highest control of widget bool _visible; ///< is this widget visible @@ -930,7 +935,7 @@ protected: cocos2d::Point _touchMovePos; ///< touch moved point cocos2d::Point _touchEndPos; ///< touch ended point - cocos2d::Object* _touchEventListener; + Object* _touchEventListener; SEL_TouchEvent _touchEventSelector; @@ -941,7 +946,7 @@ protected: int _actionTag; cocos2d::Size _size; cocos2d::Size _customSize; - LayoutParameter* _layoutParameter; + cocos2d::Dictionary* _layoutParameterDictionary; bool _ignoreSize; cocos2d::Array* _children; bool _affectByClipping; @@ -954,7 +959,10 @@ protected: cocos2d::Point _positionPercent; bool _isRunning; }; - +/** +* @js NA +* @lua NA +*/ class GUIRenderer : public cocos2d::NodeRGBA { public: @@ -969,5 +977,4 @@ protected: }; } - #endif /* defined(__UIWidget__) */ diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/CocosGUIScene.cpp b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/CocosGUIScene.cpp index 1ba28914df..750b562a67 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/CocosGUIScene.cpp +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/CocosGUIScene.cpp @@ -19,7 +19,6 @@ CocosGUITestScene::~CocosGUITestScene() { cocostudio::SceneReader::getInstance()->purgeSceneReader(); cocostudio::ActionManagerEx::purgeActionManager(); - gui::UIHelper::purgeUIHelper(); } void CocosGUITestScene::runThisTest() diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIDragPanelTest/UIDragPanelTest.cpp b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIDragPanelTest/UIDragPanelTest.cpp index e80ff2fd01..dacec9ba84 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIDragPanelTest/UIDragPanelTest.cpp +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIDragPanelTest/UIDragPanelTest.cpp @@ -44,78 +44,25 @@ bool UIDragPanelTest::init() alert->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getSize().height * 2.925)); m_pUiLayer->addWidget(alert); - Layout *background = dynamic_cast(m_pUiLayer->getWidgetByName("background_Panel")); - - // Create the dragpanel - UIDragPanel* dragPanel = UIDragPanel::create(); - dragPanel->setTouchEnabled(true); - dragPanel->setBackGroundImageScale9Enabled(true); - dragPanel->setBackGroundImage("cocosgui/scrollviewbg.png"); - dragPanel->setSize(Size(210, 122.5)); - Size backgroundSize = background->getContentSize(); - dragPanel->setPosition(Point((widgetSize.width - backgroundSize.width) / 2 + - (backgroundSize.width - dragPanel->getSize().width) / 2, - (widgetSize.height - backgroundSize.height) / 2 + - (backgroundSize.height - dragPanel->getSize().height) / 2)); - dragPanel->addEventListener(this, dragpaneleventselector(UIDragPanelTest::dragPanelEvent)); - - UIImageView* imageView = UIImageView::create(); - imageView->setTouchEnabled(true); - imageView->loadTexture("cocosgui/b11.png"); - dragPanel->addChild(imageView); - - dragPanel->setInnerContainerSize(imageView->getContentSize()); - Size innerSize = dragPanel->getInnerContainerSize(); - imageView->setPosition(Point(innerSize.width / 2, innerSize.height / 2)); - - m_pUiLayer->addWidget(dragPanel); + UIScrollView* sc = UIScrollView::create(); + sc->setBackGroundColor(Color3B::GREEN); + sc->setBackGroundColorType(LAYOUT_COLOR_SOLID); + sc->setDirection(SCROLLVIEW_DIR_BOTH); + sc->setInnerContainerSize(Size(480, 320)); + sc->setSize(Size(100,100)); + sc->setPosition(Point(100,100)); + sc->scrollToPercentBothDirection(Point(50, 50), 1, true); + UIImageView* iv = UIImageView::create(); + iv->loadTexture("cocosgui/Hello.png"); + iv->setPosition(Point(240, 160)); + sc->addChild(iv); + m_pUiLayer->addWidget(sc); return true; } return false; } -void UIDragPanelTest::dragPanelEvent(Object *pSender, DragPanelEventType type) -{ - switch (type) - { - case DRAGPANEL_EVENT_BERTH_LEFTBOTTOM: - m_pDisplayValueLabel->setText(CCString::createWithFormat("Berth To Left Bottom")->getCString()); - break; - - case DRAGPANEL_EVENT_BERTH_LFETTOP: - m_pDisplayValueLabel->setText(CCString::createWithFormat("Berth To Left Top")->getCString()); - break; - - case DRAGPANEL_EVENT_BERTH_RIGHTBOTTOM: - m_pDisplayValueLabel->setText(CCString::createWithFormat("Berth To Right Bottom")->getCString()); - break; - - case DRAGPANEL_EVENT_BERTH_RIGHTTOP: - m_pDisplayValueLabel->setText(CCString::createWithFormat("Berth To Right Top")->getCString()); - break; - - case DRAGPANEL_EVENT_BERTH_LEFT: - m_pDisplayValueLabel->setText(CCString::createWithFormat("Berth To Left")->getCString()); - break; - - case DRAGPANEL_EVENT_BERTH_TOP: - m_pDisplayValueLabel->setText(CCString::createWithFormat("Berth To Top")->getCString()); - break; - - case DRAGPANEL_EVENT_BERTH_RIGHT: - m_pDisplayValueLabel->setText(CCString::createWithFormat("Berth To Right")->getCString()); - break; - - case DRAGPANEL_EVENT_BERTH_BOTTOM: - m_pDisplayValueLabel->setText(CCString::createWithFormat("Berth To Bottom")->getCString()); - break; - - default: - break; - } -} - // UIDragPanelTest_Bounce UIDragPanelTest_Bounce::UIDragPanelTest_Bounce() : m_pDisplayValueLabel(NULL) @@ -151,76 +98,23 @@ bool UIDragPanelTest_Bounce::init() alert->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getSize().height * 2.925)); m_pUiLayer->addWidget(alert); - Layout *background = dynamic_cast(m_pUiLayer->getWidgetByName("background_Panel")); - - // Create the dragpanel - UIDragPanel* dragPanel = UIDragPanel::create(); - dragPanel->setTouchEnabled(true); - dragPanel->setBounceEnable(true); - dragPanel->setBackGroundImageScale9Enabled(true); - dragPanel->setBackGroundImage("cocosgui/green_edit.png"); - dragPanel->setSize(Size(210, 122.5)); - Size backgroundSize = background->getContentSize(); - dragPanel->setPosition(Point((widgetSize.width - backgroundSize.width) / 2 + - (backgroundSize.width - dragPanel->getSize().width) / 2, - (widgetSize.height - backgroundSize.height) / 2 + - (backgroundSize.height - dragPanel->getSize().height) / 2)); - dragPanel->addEventListener(this, dragpaneleventselector(UIDragPanelTest_Bounce::dragPanelEvent)); - - UIImageView* imageView = UIImageView::create(); - imageView->setTouchEnabled(true); - imageView->loadTexture("cocosgui/b11.png"); - dragPanel->addChild(imageView); - - dragPanel->setInnerContainerSize(imageView->getContentSize()); - Size innerSize = dragPanel->getInnerContainerSize(); - imageView->setPosition(Point(innerSize.width / 2, innerSize.height / 2)); - - m_pUiLayer->addWidget(dragPanel); - + UIScrollView* sc = UIScrollView::create(); + sc->setBackGroundColor(Color3B::GREEN); + sc->setBackGroundColorType(LAYOUT_COLOR_SOLID); + sc->setBounceEnabled(true); + sc->setDirection(SCROLLVIEW_DIR_BOTH); + sc->setInnerContainerSize(Size(480, 320)); + sc->setSize(Size(100,100)); + sc->setPosition(Point(100,100)); + sc->scrollToPercentBothDirection(Point(50, 50), 1, true); + UIImageView* iv = UIImageView::create(); + iv->loadTexture("cocosgui/Hello.png"); + iv->setPosition(Point(240, 160)); + sc->addChild(iv); + m_pUiLayer->addWidget(sc); return true; } return false; } -void UIDragPanelTest_Bounce::dragPanelEvent(Object *pSender, DragPanelEventType type) -{ - switch (type) - { - case DRAGPANEL_EVENT_BOUNCE_LEFTBOTTOM: - m_pDisplayValueLabel->setText(CCString::createWithFormat("Bounce To Left Bottom")->getCString()); - break; - - case DRAGPANEL_EVENT_BOUNCE_LEFTTOP: - m_pDisplayValueLabel->setText(CCString::createWithFormat("Bounce To Left Top")->getCString()); - break; - - case DRAGPANEL_EVENT_BOUNCE_RIGHTBOTTOM: - m_pDisplayValueLabel->setText(CCString::createWithFormat("Bounce To Right Bottom")->getCString()); - break; - - case DRAGPANEL_EVENT_BOUNCE_RIGHTTOP: - m_pDisplayValueLabel->setText(CCString::createWithFormat("Bounce To Right Top")->getCString()); - break; - - case DRAGPANEL_EVENT_BOUNCE_LEFT: - m_pDisplayValueLabel->setText(CCString::createWithFormat("Bounce To Left")->getCString()); - break; - - case DRAGPANEL_EVENT_BOUNCE_TOP: - m_pDisplayValueLabel->setText(CCString::createWithFormat("Bounce To Top")->getCString()); - break; - - case DRAGPANEL_EVENT_BOUNCE_RIGHT: - m_pDisplayValueLabel->setText(CCString::createWithFormat("Bounce To Right")->getCString()); - break; - - case DRAGPANEL_EVENT_BOUNCE_BOTTOM: - m_pDisplayValueLabel->setText(CCString::createWithFormat("Bounce To Bottom")->getCString()); - break; - - default: - break; - } -} diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIDragPanelTest/UIDragPanelTest.h b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIDragPanelTest/UIDragPanelTest.h index 76f8469154..b8967247f3 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIDragPanelTest/UIDragPanelTest.h +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIDragPanelTest/UIDragPanelTest.h @@ -33,7 +33,6 @@ public: UIDragPanelTest(); ~UIDragPanelTest(); bool init(); - void dragPanelEvent(Object* pSender, DragPanelEventType type); protected: UI_SCENE_CREATE_FUNC(UIDragPanelTest) @@ -46,7 +45,6 @@ public: UIDragPanelTest_Bounce(); ~UIDragPanelTest_Bounce(); bool init(); - void dragPanelEvent(Object* pSender, DragPanelEventType type); protected: UI_SCENE_CREATE_FUNC(UIDragPanelTest_Bounce) diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIListViewTest/UIListViewTest.cpp b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIListViewTest/UIListViewTest.cpp index 6681c744ee..c2163e8059 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIListViewTest/UIListViewTest.cpp +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIListViewTest/UIListViewTest.cpp @@ -45,54 +45,23 @@ bool UIListViewTest_Vertical::init() alert->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getSize().height * 2.925)); m_pUiLayer->addWidget(alert); - Layout *background = dynamic_cast(m_pUiLayer->getWidgetByName("background_Panel")); - // Create the list view - m_nCount = 0; - m_array = CCArray::create(); - m_array->retain(); - for (int i = 0; i < 20; ++i) + UIListView* lv = UIListView::create(); + UIButton* model = UIButton::create(); + model->loadTextures("cocosgui/animationbuttonnormal.png", "cocosgui/animationbuttonpressed.png", ""); + lv->setItemModel(model); + + for (int i=0; i<20; i++) { - String* ccstr = String::createWithFormat("object_%d", i); - m_array->addObject(ccstr); + lv->pushBackDefaultItem(); } - - UIListView *listView = UIListView::create(); - listView->setTouchEnabled(true); - listView->setBackGroundImageScale9Enabled(true); - listView->setBackGroundImage("cocosgui/green_edit.png"); - listView->setSize(Size(240, 130)); - Size backgroundSize = background->getContentSize(); - listView->setPosition(Point((widgetSize.width - backgroundSize.width) / 2 + - (backgroundSize.width - listView->getSize().width) / 2, - (widgetSize.height - backgroundSize.height) / 2 + - (backgroundSize.height - listView->getSize().height) / 2)); - - float listWidth = listView->getSize().width; - float listHeight = listView->getSize().height; - - for (int i = 0; i < 5; ++i) - { - UIButton* textButton = UIButton::create(); - textButton->setName("TextButton"); - textButton->setTouchEnabled(true); - textButton->loadTextures("cocosgui/backtotoppressed.png", "cocosgui/backtotopnormal.png", ""); - - Layout *layout = Layout::create(); - layout->setName(String::createWithFormat("panel_%i", i)->getCString()); - layout->setSize(Size(textButton->getSize().width, textButton->getSize().height)); - textButton->setPosition(Point(layout->getSize().width / 2, layout->getSize().height / 2)); - layout->addChild(textButton); - - Size panel_size = layout->getSize(); - layout->setPosition(Point((listWidth - panel_size.width) / 2, - (listHeight - (panel_size.height + panel_size.height * 0.25)) - i * (panel_size.height + panel_size.height * 0.25))); - - listView->addChild(layout); - } - listView->addEventListenter(this, listvieweventselector(UIListViewTest_Vertical::listViewEvent)); - listView->initChildWithDataLength(m_array->count()); - m_pUiLayer->addWidget(listView); + lv->setItemsMargin(10); + lv->setGravity(LISTVIEW_GRAVITY_CENTER_HORIZONTAL); + lv->setSize(Size(100, 100)); + lv->setBackGroundColorType(LAYOUT_COLOR_SOLID); + lv->setBackGroundColor(Color3B::GREEN); + lv->setPosition(Point(100, 100)); + m_pUiLayer->addWidget(lv); return true; } @@ -100,46 +69,6 @@ bool UIListViewTest_Vertical::init() return false; } -void UIListViewTest_Vertical::listViewEvent(Object *pSender, ListViewEventType type) -{ - switch (type) - { - case LISTVIEW_EVENT_INIT_CHILD: - { - String* ccstr = static_cast(m_array->getObjectAtIndex(m_nCount)); - UIListView* list = dynamic_cast(pSender); - - Layout* layout = dynamic_cast(list->getUpdateChild()); - UIButton* textButton = dynamic_cast(layout->getChildByName("TextButton")); - textButton->setTitleText(ccstr->getCString()); - - m_nCount++; - } - break; - - case LISTVIEW_EVENT_UPDATE_CHILD: - { - UIListView* list = dynamic_cast(pSender); - int index = list->getUpdateDataIndex(); - - if (index < 0 || index >= list->getDataLength()) - { - list->setUpdateSuccess(false); - } - - String* ccstr = static_cast(m_array->getObjectAtIndex(index)); - Layout* layout = dynamic_cast(list->getUpdateChild()); - UIButton* textButton = dynamic_cast(layout->getChildByName("TextButton")); - textButton->setTitleText(ccstr->getCString()); - list->setUpdateSuccess(true); - } - break; - - default: - break; - } -} - // UIListViewTest_Horizontal UIListViewTest_Horizontal::UIListViewTest_Horizontal() @@ -176,97 +105,26 @@ bool UIListViewTest_Horizontal::init() alert->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getSize().height * 2.925)); m_pUiLayer->addWidget(alert); - Layout *background = dynamic_cast(m_pUiLayer->getWidgetByName("background_Panel")); + + UIListView* lv = UIListView::create(); + lv->setDirection(SCROLLVIEW_DIR_HORIZONTAL); + UIButton* model = UIButton::create(); + model->loadTextures("cocosgui/animationbuttonnormal.png", "cocosgui/animationbuttonpressed.png", ""); + lv->setItemModel(model); - // Create the list view - m_nCount = 0; - m_array = CCArray::create(); - m_array->retain(); - for (int i = 0; i < 20; ++i) + for (int i=0; i<20; i++) { - String* ccstr = String::createWithFormat("object_%d", i); - m_array->addObject(ccstr); + lv->pushBackDefaultItem(); } - - UIListView *listView = UIListView::create(); - listView->setDirection(LISTVIEW_DIR_HORIZONTAL); - listView->setTouchEnabled(true); - listView->setBackGroundImageScale9Enabled(true); - listView->setBackGroundImage("cocosgui/green_edit.png"); - listView->setSize(Size(240, 130)); - Size backgroundSize = background->getContentSize(); - listView->setPosition(Point((widgetSize.width - backgroundSize.width) / 2 + - (backgroundSize.width - listView->getSize().width) / 2, - (widgetSize.height - backgroundSize.height) / 2 + - (backgroundSize.height - listView->getSize().height) / 2)); - - float listHeight = listView->getSize().height; - - for (int i = 0; i < 3; ++i) - { - UIButton* textButton = UIButton::create(); - textButton->setName("TextButton"); - textButton->setTouchEnabled(true); - textButton->loadTextures("cocosgui/backtotoppressed.png", "cocosgui/backtotopnormal.png", ""); - - Layout *layout = Layout::create(); - layout->setName(String::createWithFormat("panel_%i", i)->getCString()); - layout->setSize(Size(textButton->getSize().width, textButton->getSize().height)); - textButton->setPosition(Point(layout->getSize().width / 2, layout->getSize().height / 2)); - layout->addChild(textButton); - - Size layout_size = layout->getSize(); - layout->setPosition(Point(0 + (layout_size.width * 0.2) + i * (layout_size.width + layout_size.width * 0.2), - (listHeight - layout_size.height) / 2)); - - listView->addChild(layout); - } - listView->addEventListenter(this, listvieweventselector(UIListViewTest_Horizontal::listViewEvent)); - listView->initChildWithDataLength(m_array->count()); - m_pUiLayer->addWidget(listView); - + lv->setItemsMargin(10); + lv->setGravity(LISTVIEW_GRAVITY_CENTER_VERTICAL); + lv->setSize(Size(100, 100)); + lv->setBackGroundColorType(LAYOUT_COLOR_SOLID); + lv->setBackGroundColor(Color3B::GREEN); + lv->setPosition(Point(100, 100)); + m_pUiLayer->addWidget(lv); return true; } return false; -} - -void UIListViewTest_Horizontal::listViewEvent(Object *pSender, ListViewEventType type) -{ - switch (type) - { - case LISTVIEW_EVENT_INIT_CHILD: - { - String* ccstr = static_cast(m_array->getObjectAtIndex(m_nCount)); - UIListView* list = dynamic_cast(pSender); - - Layout* layout = dynamic_cast(list->getUpdateChild()); - UIButton* textButton = dynamic_cast(layout->getChildByName("TextButton")); - textButton->setTitleText(ccstr->getCString()); - - m_nCount++; - } - break; - - case LISTVIEW_EVENT_UPDATE_CHILD: - { - UIListView* list = dynamic_cast(pSender); - int index = list->getUpdateDataIndex(); - - if (index < 0 || index >= list->getDataLength()) - { - list->setUpdateSuccess(false); - } - - String* ccstr = static_cast(m_array->getObjectAtIndex(index)); - Layout* layout = dynamic_cast(list->getUpdateChild()); - UIButton* textButton = dynamic_cast(layout->getChildByName("TextButton")); - textButton->setTitleText(ccstr->getCString()); - list->setUpdateSuccess(true); - } - break; - - default: - break; - } -} +} \ No newline at end of file diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIListViewTest/UIListViewTest.h b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIListViewTest/UIListViewTest.h index b2725e33ee..4dee33553c 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIListViewTest/UIListViewTest.h +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIListViewTest/UIListViewTest.h @@ -33,7 +33,6 @@ public: UIListViewTest_Vertical(); ~UIListViewTest_Vertical(); bool init(); - void listViewEvent(Object* pSender, ListViewEventType type); protected: UI_SCENE_CREATE_FUNC(UIListViewTest_Vertical) @@ -49,7 +48,6 @@ public: UIListViewTest_Horizontal(); ~UIListViewTest_Horizontal(); bool init(); - void listViewEvent(Object* pSender, ListViewEventType type); protected: UI_SCENE_CREATE_FUNC(UIListViewTest_Horizontal) diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIPageViewTest/UIPageViewTest.cpp b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIPageViewTest/UIPageViewTest.cpp index 22b97ef9e6..ce5028ae35 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIPageViewTest/UIPageViewTest.cpp +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIPageViewTest/UIPageViewTest.cpp @@ -44,7 +44,7 @@ bool UIPageViewTest::init() alert->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getSize().height * 2.925)); m_pUiLayer->addWidget(alert); - Layout *background = dynamic_cast(m_pUiLayer->getWidgetByName("background_Panel")); + UILayout *background = dynamic_cast(m_pUiLayer->getWidgetByName("background_Panel")); // Create the page view UIPageView* pageView = UIPageView::create(); @@ -58,7 +58,7 @@ bool UIPageViewTest::init() for (int i = 0; i < 3; ++i) { - Layout* layout = Layout::create(); + UILayout* layout = UILayout::create(); layout->setSize(Size(240, 130)); UIImageView* imageView = UIImageView::create(); diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIPanelTest/UIPanelTest.cpp b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIPanelTest/UIPanelTest.cpp index 49674298bc..94dbb02819 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIPanelTest/UIPanelTest.cpp +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIPanelTest/UIPanelTest.cpp @@ -34,10 +34,10 @@ bool UIPanelTest::init() alert->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getSize().height * 2.925)); m_pUiLayer->addWidget(alert); - Layout *background = dynamic_cast(m_pUiLayer->getWidgetByName("background_Panel")); + UILayout *background = dynamic_cast(m_pUiLayer->getWidgetByName("background_Panel")); // Create the layout - Layout* layout = Layout::create(); + UILayout* layout = UILayout::create(); layout->setSize(Size(280, 150)); Size backgroundSize = background->getSize(); layout->setPosition(Point((widgetSize.width - backgroundSize.width) / 2 + @@ -98,10 +98,10 @@ bool UIPanelTest_Color::init() alert->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getSize().height * 2.925)); m_pUiLayer->addWidget(alert); - Layout *background = dynamic_cast(m_pUiLayer->getWidgetByName("background_Panel")); + UILayout *background = dynamic_cast(m_pUiLayer->getWidgetByName("background_Panel")); // Create the layout with color render - Layout* layout = Layout::create(); + UILayout* layout = UILayout::create(); layout->setBackGroundColorType(LAYOUT_COLOR_SOLID); layout->setBackGroundColor(Color3B(128, 128, 128)); layout->setSize(Size(280, 150)); @@ -163,10 +163,10 @@ bool UIPanelTest_Gradient::init() alert->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getSize().height * 2.925)); m_pUiLayer->addWidget(alert); - Layout *background = dynamic_cast(m_pUiLayer->getWidgetByName("background_Panel")); + UILayout *background = dynamic_cast(m_pUiLayer->getWidgetByName("background_Panel")); // Create the layout with gradient render - Layout* layout = Layout::create(); + UILayout* layout = UILayout::create(); layout->setBackGroundColorType(LAYOUT_COLOR_GRADIENT); layout->setBackGroundColor(Color3B(64, 64, 64), Color3B(192, 192, 192)); layout->setSize(Size(280, 150)); @@ -228,10 +228,10 @@ bool UIPanelTest_BackGroundImage::init() alert->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getSize().height * 2.925)); m_pUiLayer->addWidget(alert); - Layout *background = dynamic_cast(m_pUiLayer->getWidgetByName("background_Panel")); + UILayout *background = dynamic_cast(m_pUiLayer->getWidgetByName("background_Panel")); // Create the layout with background image - Layout* layout = Layout::create(); + UILayout* layout = UILayout::create(); layout->setClippingEnabled(true); layout->setBackGroundImage("cocosgui/Hello.png"); layout->setSize(Size(280, 150)); @@ -293,10 +293,10 @@ bool UIPanelTest_BackGroundImage_Scale9::init() alert->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getSize().height * 2.925)); m_pUiLayer->addWidget(alert); - Layout *background = dynamic_cast(m_pUiLayer->getWidgetByName("background_Panel")); + UILayout *background = dynamic_cast(m_pUiLayer->getWidgetByName("background_Panel")); // Create the layout with background image - Layout* layout = Layout::create(); + UILayout* layout = UILayout::create(); layout->setBackGroundImageScale9Enabled(true); layout->setBackGroundImage("cocosgui/green_edit.png"); layout->setSize(Size(280, 150)); @@ -358,10 +358,10 @@ bool UIPanelTest_Layout_Linear_Vertical::init() alert->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getSize().height * 2.925)); m_pUiLayer->addWidget(alert); - Layout *background = dynamic_cast(m_pUiLayer->getWidgetByName("background_Panel")); + UILayout *background = dynamic_cast(m_pUiLayer->getWidgetByName("background_Panel")); // Create the layout - Layout* layout = Layout::create(); + UILayout* layout = UILayout::create(); layout->setLayoutType(LAYOUT_LINEAR_VERTICAL); layout->setSize(Size(280, 150)); Size backgroundSize = background->getSize(); @@ -377,7 +377,7 @@ bool UIPanelTest_Layout_Linear_Vertical::init() button->loadTextures("cocosgui/animationbuttonnormal.png", "cocosgui/animationbuttonpressed.png", ""); layout->addChild(button); - LinearLayoutParameter* lp1 = LinearLayoutParameter::create(); + UILinearLayoutParameter* lp1 = UILinearLayoutParameter::create(); button->setLayoutParameter(lp1); lp1->setGravity(LINEAR_GRAVITY_CENTER_HORIZONTAL); lp1->setMargin(UIMargin(0, 5, 0, 10)); @@ -389,7 +389,7 @@ bool UIPanelTest_Layout_Linear_Vertical::init() textButton->setTitleText("Text Button"); layout->addChild(textButton); - LinearLayoutParameter* lp2 = LinearLayoutParameter::create(); + UILinearLayoutParameter* lp2 = UILinearLayoutParameter::create(); textButton->setLayoutParameter(lp2); lp2->setGravity(LINEAR_GRAVITY_CENTER_HORIZONTAL); lp2->setMargin(UIMargin(0, 10, 0, 10)); @@ -402,7 +402,7 @@ bool UIPanelTest_Layout_Linear_Vertical::init() button_scale9->setSize(Size(100, button_scale9->getContentSize().height)); layout->addChild(button_scale9); - LinearLayoutParameter* lp3 = LinearLayoutParameter::create(); + UILinearLayoutParameter* lp3 = UILinearLayoutParameter::create(); button_scale9->setLayoutParameter(lp3); lp3->setGravity(LINEAR_GRAVITY_CENTER_HORIZONTAL); lp3->setMargin(UIMargin(0, 10, 0, 10)); @@ -441,10 +441,10 @@ bool UIPanelTest_Layout_Linear_Horizontal::init() alert->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getSize().height * 2.925)); m_pUiLayer->addWidget(alert); - Layout *background = dynamic_cast(m_pUiLayer->getWidgetByName("background_Panel")); + UILayout *background = dynamic_cast(m_pUiLayer->getWidgetByName("background_Panel")); // Create the layout - Layout* layout = Layout::create(); + UILayout* layout = UILayout::create(); layout->setLayoutType(LAYOUT_LINEAR_HORIZONTAL); layout->setClippingEnabled(true); layout->setSize(Size(280, 150)); @@ -460,7 +460,7 @@ bool UIPanelTest_Layout_Linear_Horizontal::init() button->loadTextures("cocosgui/animationbuttonnormal.png", "cocosgui/animationbuttonpressed.png", ""); layout->addChild(button); - LinearLayoutParameter* lp1 = LinearLayoutParameter::create(); + UILinearLayoutParameter* lp1 = UILinearLayoutParameter::create(); button->setLayoutParameter(lp1); lp1->setGravity(LINEAR_GRAVITY_CENTER_VERTICAL); lp1->setMargin(UIMargin(0, 10, 0, 10)); @@ -472,7 +472,7 @@ bool UIPanelTest_Layout_Linear_Horizontal::init() textButton->setTitleText("Text Button"); layout->addChild(textButton); - LinearLayoutParameter* lp2 = LinearLayoutParameter::create(); + UILinearLayoutParameter* lp2 = UILinearLayoutParameter::create(); textButton->setLayoutParameter(lp2); lp2->setGravity(LINEAR_GRAVITY_CENTER_VERTICAL); lp2->setMargin(UIMargin(0, 10, 0, 10)); @@ -485,7 +485,7 @@ bool UIPanelTest_Layout_Linear_Horizontal::init() button_scale9->setSize(Size(100, button_scale9->getContentSize().height)); layout->addChild(button_scale9); - LinearLayoutParameter* lp3 = LinearLayoutParameter::create(); + UILinearLayoutParameter* lp3 = UILinearLayoutParameter::create(); button_scale9->setLayoutParameter(lp3); lp3->setGravity(LINEAR_GRAVITY_CENTER_VERTICAL); lp3->setMargin(UIMargin(0, 10, 0, 10)); @@ -524,10 +524,10 @@ bool UIPanelTest_Layout_Relative::init() alert->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getSize().height * 2.925)); m_pUiLayer->addWidget(alert); - Layout *background = dynamic_cast(m_pUiLayer->getWidgetByName("background_Panel")); + UILayout *background = dynamic_cast(m_pUiLayer->getWidgetByName("background_Panel")); // Create the layout - Layout* layout = Layout::create(); + UILayout* layout = UILayout::create(); layout->setLayoutType(LAYOUT_RELATIVE); layout->setSize(Size(280, 150)); layout->setBackGroundColorType(LAYOUT_COLOR_SOLID); @@ -544,9 +544,9 @@ bool UIPanelTest_Layout_Relative::init() button->loadTextures("cocosgui/animationbuttonnormal.png", "cocosgui/animationbuttonpressed.png", ""); layout->addChild(button); - RelativeLayoutParameter* rp1 = RelativeLayoutParameter::create(); + UIRelativeLayoutParameter* rp1 = UIRelativeLayoutParameter::create(); button->setLayoutParameter(rp1); - rp1->setAlign(RELATIVE_ALIGN_PARENT_BOTTOM); + rp1->setAlign(RELATIVE_ALIGN_PARENT_BOTTOM_CENTER_HORIZONTAL); // rp1->setMargin(UIMargin(0, 10, 0, 10)); @@ -556,9 +556,9 @@ bool UIPanelTest_Layout_Relative::init() textButton->setTitleText("Text Button"); layout->addChild(textButton); - RelativeLayoutParameter* rp2 = RelativeLayoutParameter::create(); + UIRelativeLayoutParameter* rp2 = UIRelativeLayoutParameter::create(); textButton->setLayoutParameter(rp2); - rp2->setAlign(RELATIVE_ALIGN_PARENT_LEFT); + rp2->setAlign(RELATIVE_ALIGN_PARENT_LEFT_BOTTOM); UIButton* button_scale9 = UIButton::create(); @@ -568,9 +568,9 @@ bool UIPanelTest_Layout_Relative::init() button_scale9->setSize(Size(100, button_scale9->getContentSize().height)); layout->addChild(button_scale9); - RelativeLayoutParameter* rp3 = RelativeLayoutParameter::create(); + UIRelativeLayoutParameter* rp3 = UIRelativeLayoutParameter::create(); textButton->setLayoutParameter(rp3); - rp3->setAlign(RELATIVE_ALIGN_PARENT_RIGHT); + rp3->setAlign(RELATIVE_ALIGN_PARENT_RIGHT_BOTTOM); layout->doLayout(); diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIScene.cpp b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIScene.cpp index a57787dfb1..38344c9490 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIScene.cpp +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIScene.cpp @@ -3,6 +3,7 @@ #include "UIScene.h" #include "UISceneManager.h" #include "../ExtensionsTest.h" +#include "editor-support/cocostudio/CCSGUIReader.h" using namespace gui; @@ -25,7 +26,7 @@ bool UIScene::init() m_pUiLayer = UILayer::create(); addChild(m_pUiLayer); - m_pWidget = dynamic_cast(CCUIHELPER->createWidgetFromJsonFile("cocosgui/UITest/UITest.json")); + m_pWidget = dynamic_cast(cocostudio::CCSGUIReader::shareReader()->widgetFromJsonFile("cocosgui/UITest/UITest.json")); m_pUiLayer->addWidget(m_pWidget); m_pSceneTitle = dynamic_cast(m_pUiLayer->getWidgetByName("UItest")); diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIScene.h b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIScene.h index bf10ffa76b..720fdd4224 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIScene.h +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIScene.h @@ -95,7 +95,7 @@ protected: protected: UILayer *m_pUiLayer; - Layout *m_pWidget; + UILayout *m_pWidget; }; #endif /* defined(__TestCpp__UIScene__) */ diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIScrollViewTest/UIScrollViewTest.cpp b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIScrollViewTest/UIScrollViewTest.cpp index c7a13fd225..01b3b83278 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIScrollViewTest/UIScrollViewTest.cpp +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIScrollViewTest/UIScrollViewTest.cpp @@ -44,7 +44,7 @@ bool UIScrollViewTest_Vertical::init() alert->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getSize().height * 2.925)); m_pUiLayer->addWidget(alert); - Layout *background = dynamic_cast(m_pUiLayer->getWidgetByName("background_Panel")); + UILayout *background = dynamic_cast(m_pUiLayer->getWidgetByName("background_Panel")); // Create the scrollview by vertical UIScrollView* scrollView = UIScrollView::create(); @@ -129,10 +129,11 @@ bool UIScrollViewTest_Horizontal::init() alert->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getSize().height * 2.925)); m_pUiLayer->addWidget(alert); - Layout *background = dynamic_cast(m_pUiLayer->getWidgetByName("background_Panel")); + UILayout *background = dynamic_cast(m_pUiLayer->getWidgetByName("background_Panel")); // Create the scrollview by horizontal - UIScrollView* scrollView = UIScrollView::create(); + UIScrollView* scrollView = UIScrollView::create(); + scrollView->setBounceEnabled(true); scrollView->setDirection(SCROLLVIEW_DIR_HORIZONTAL); scrollView->setTouchEnabled(true); scrollView->setSize(Size(280, 150)); diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp index bba53b0444..c486c5be3a 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp @@ -82,7 +82,7 @@ void UITextFieldTest::textFieldEvent(Object *pSender, TextFiledEventType type) } break; - case TEXTFIELD_EVENT_INDERT_TEXT: + case TEXTFIELD_EVENT_INSERT_TEXT: m_pDisplayValueLabel->setText(CCString::createWithFormat("insert words")->getCString()); break; @@ -132,7 +132,7 @@ bool UITextFieldTest_MaxLength::init() // Create the textfield UITextField* textField = UITextField::create(); - textField->setMaxLengthEnable(true); + textField->setMaxLengthEnabled(true); textField->setMaxLength(3); textField->setTouchEnabled(true); textField->setFontName(font_UITextFieldTest); @@ -170,7 +170,7 @@ void UITextFieldTest_MaxLength::textFieldEvent(Object *pSender, TextFiledEventTy } break; - case TEXTFIELD_EVENT_INDERT_TEXT: + case TEXTFIELD_EVENT_INSERT_TEXT: { UITextField* textField = dynamic_cast(pSender); m_pDisplayValueLabel->setText(CCString::createWithFormat("insert words max length %d", textField->getMaxLength())->getCString()); @@ -226,7 +226,7 @@ bool UITextFieldTest_Password::init() // Create the textfield UITextField* textField = UITextField::create(); - textField->setPasswordEnable(true); + textField->setPasswordEnabled(true); textField->setPasswordStyleText("*"); textField->setTouchEnabled(true); textField->setFontName(font_UITextFieldTest); @@ -264,7 +264,7 @@ void UITextFieldTest_Password::textFieldEvent(Object *pSender, TextFiledEventTyp } break; - case TEXTFIELD_EVENT_INDERT_TEXT: + case TEXTFIELD_EVENT_INSERT_TEXT: m_pDisplayValueLabel->setText(CCString::createWithFormat("insert words password")->getCString()); break; diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioSceneTest/SceneEditorTest.cpp b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioSceneTest/SceneEditorTest.cpp index 0ce946c5a6..f7ddf0c777 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioSceneTest/SceneEditorTest.cpp +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioSceneTest/SceneEditorTest.cpp @@ -14,7 +14,6 @@ SceneEditorTestLayer::~SceneEditorTestLayer() ArmatureDataManager::getInstance()->destoryInstance(); SceneReader::getInstance()->purgeSceneReader(); ActionManagerEx::shareManager()->purgeActionManager(); - UIHelper::instance()->purgeUIHelper(); } SceneEditorTestLayer::SceneEditorTestLayer() From 12570b5e8fe3a103bd0f6a801dff17538369eb29 Mon Sep 17 00:00:00 2001 From: CaiWenzhi Date: Wed, 6 Nov 2013 16:20:01 +0800 Subject: [PATCH 2/6] temp commit --- cocos/editor-support/cocostudio/CCSGUIReader.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/cocos/editor-support/cocostudio/CCSGUIReader.cpp b/cocos/editor-support/cocostudio/CCSGUIReader.cpp index 0f5c597306..d5b10e62b3 100755 --- a/cocos/editor-support/cocostudio/CCSGUIReader.cpp +++ b/cocos/editor-support/cocostudio/CCSGUIReader.cpp @@ -162,8 +162,8 @@ UIWidget* CCSGUIReader::widgetFromJsonDictionary(JsonDictionary* data) } else if (classname && strcmp(classname, "ListView") == 0) { -// widget = UIListView::create(); -// setPropsForListViewFromJsonDictionary(widget, uiOptions); + widget = UIListView::create(); + setPropsForListViewFromJsonDictionary(widget, uiOptions); } else if (classname && strcmp(classname, "PageView") == 0) { @@ -232,13 +232,14 @@ UIWidget* CCSGUIReader::widgetFromJsonFile(const char *fileName) const char* file = DICTOOL->getStringValueFromArray_json(jsonDict, "textures", i); std::string tp = m_strFilePath; tp.append(file); -// CCUIHELPER->addSpriteFrame(tp.c_str()); + SpriteFrameCache::getInstance()->addSpriteFramesWithFile(tp.c_str()); } float fileDesignWidth = DICTOOL->getFloatValue_json(jsonDict, "designWidth"); float fileDesignHeight = DICTOOL->getFloatValue_json(jsonDict, "designHeight"); if (fileDesignWidth <= 0 || fileDesignHeight <= 0) { printf("Read design size error!\n"); Size winSize = Director::getInstance()->getWinSize(); +// UIHelper:: // CCUIHELPER->setFileDesignWidth(winSize.width); // CCUIHELPER->setFileDesignHeight(winSize.height); } From 94896b5b74bfa9e923468a0b3f8f542d1265c5b3 Mon Sep 17 00:00:00 2001 From: CaiWenzhi Date: Wed, 6 Nov 2013 17:20:14 +0800 Subject: [PATCH 3/6] Modify mk files --- cocos/gui/Android.mk | 5 ++--- cocos/gui/CMakeLists.txt | 5 ++--- cocos/gui/Makefile | 5 ++--- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/cocos/gui/Android.mk b/cocos/gui/Android.mk index 33171c7c1d..17f15efe44 100644 --- a/cocos/gui/Android.mk +++ b/cocos/gui/Android.mk @@ -7,14 +7,13 @@ LOCAL_MODULE_FILENAME := libgui LOCAL_SRC_FILES := UIRootWidget.cpp \ UIWidget.cpp \ -Layout.cpp \ -LayoutParameter.cpp \ +UILayout.cpp \ +UILayoutParameter.cpp \ UILayoutDefine.cpp \ CocosGUI.cpp \ UIHelper.cpp \ UIInputManager.cpp \ UILayer.cpp \ -UIDragPanel.cpp \ UIListView.cpp \ UIPageView.cpp \ UIScrollView.cpp \ diff --git a/cocos/gui/CMakeLists.txt b/cocos/gui/CMakeLists.txt index 916e5cf0f2..d4b428f890 100644 --- a/cocos/gui/CMakeLists.txt +++ b/cocos/gui/CMakeLists.txt @@ -1,14 +1,13 @@ set(GUI_SRC UIRootWidget.cpp UIWidget.cpp - Layout.cpp - LayoutParameter.cpp + UILayout.cpp + UILayoutParameter.cpp UILayoutDefine.cpp CocosGUI.cpp UIHelper.cpp UIInputManager.cpp UILayer.cpp - UIDragPanel.cpp UIListView.cpp UIPageView.cpp UIScrollView.cpp diff --git a/cocos/gui/Makefile b/cocos/gui/Makefile index 0e8501cc63..8dcb0cb404 100644 --- a/cocos/gui/Makefile +++ b/cocos/gui/Makefile @@ -6,14 +6,13 @@ INCLUDES = -I../ \ SOURCES = UIRootWidget.cpp \ UIWidget.cpp \ -Layout.cpp \ -LayoutParameter.cpp \ +UILayout.cpp \ +UILayoutParameter.cpp \ UILayoutDefine.cpp \ CocosGUI.cpp \ UIHelper.cpp \ UIInputManager.cpp \ UILayer.cpp \ -UIDragPanel.cpp \ UIListView.cpp \ UIPageView.cpp \ UIScrollView.cpp \ From e765eb1e7a5a9f5cc6b20b12aa3904849ee9c767 Mon Sep 17 00:00:00 2001 From: CaiWenzhi Date: Wed, 6 Nov 2013 18:30:09 +0800 Subject: [PATCH 4/6] Test commit --- cocos/editor-support/cocostudio/CCSGUIReader.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/cocos/editor-support/cocostudio/CCSGUIReader.cpp b/cocos/editor-support/cocostudio/CCSGUIReader.cpp index d5b10e62b3..de644e6ec6 100755 --- a/cocos/editor-support/cocostudio/CCSGUIReader.cpp +++ b/cocos/editor-support/cocostudio/CCSGUIReader.cpp @@ -239,7 +239,6 @@ UIWidget* CCSGUIReader::widgetFromJsonFile(const char *fileName) if (fileDesignWidth <= 0 || fileDesignHeight <= 0) { printf("Read design size error!\n"); Size winSize = Director::getInstance()->getWinSize(); -// UIHelper:: // CCUIHELPER->setFileDesignWidth(winSize.width); // CCUIHELPER->setFileDesignHeight(winSize.height); } From fa0011346e21345a03e688e7a85181cc67aa03d0 Mon Sep 17 00:00:00 2001 From: CaiWenzhi Date: Thu, 7 Nov 2013 09:58:24 +0800 Subject: [PATCH 5/6] fixed linux build error --- cocos/gui/UILabelBMFont.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos/gui/UILabelBMFont.cpp b/cocos/gui/UILabelBMFont.cpp index d8560fd4a8..ee3ad66ddf 100644 --- a/cocos/gui/UILabelBMFont.cpp +++ b/cocos/gui/UILabelBMFont.cpp @@ -60,7 +60,7 @@ void UILabelBMFont::initRenderer() void UILabelBMFont::setFntFile(const char *fileName) { - if (!fileName || std::strcmp(fileName, "") == 0) + if (!fileName || strcmp(fileName, "") == 0) { return; } From f05b79d14082f1fb09aeca7e8247960f7d60a79f Mon Sep 17 00:00:00 2001 From: minggo Date: Thu, 7 Nov 2013 11:29:23 +0800 Subject: [PATCH 6/6] remove unneeded files --- samples/samples.xcodeproj/project.pbxproj.REMOVED.git-id | 1 - 1 file changed, 1 deletion(-) delete mode 100644 samples/samples.xcodeproj/project.pbxproj.REMOVED.git-id diff --git a/samples/samples.xcodeproj/project.pbxproj.REMOVED.git-id b/samples/samples.xcodeproj/project.pbxproj.REMOVED.git-id deleted file mode 100644 index 06123eec12..0000000000 --- a/samples/samples.xcodeproj/project.pbxproj.REMOVED.git-id +++ /dev/null @@ -1 +0,0 @@ -3275ac270645139eb273b2a47c215f39bcb0e0d5 \ No newline at end of file