From 8b7a5c3141c309a2a51aa064a6a46ba36494dc62 Mon Sep 17 00:00:00 2001 From: CaiWenzhi Date: Thu, 6 Mar 2014 20:52:29 +0800 Subject: [PATCH] Add RichText sample --- .../project.pbxproj.REMOVED.git-id | 2 +- cocos/gui/Android.mk | 3 +- cocos/gui/CMakeLists.txt | 1 + cocos/gui/CocosGUI.h | 1 + tests/Android.mk | 1 + tests/CMakeLists.txt | 1 + .../CocoStudioGUITest/CocosGUIScene.cpp | 12 ++ .../UIRichTextTest/UIRichTextTest.cpp | 103 ++++++++++++++++++ .../UIRichTextTest/UIRichTextTest.h | 23 ++++ .../CocoStudioGUITest/UISceneManager.cpp | 5 + .../CocoStudioGUITest/UISceneManager.h | 1 + 11 files changed, 151 insertions(+), 2 deletions(-) create mode 100644 tests/Classes/ExtensionsTest/CocoStudioGUITest/UIRichTextTest/UIRichTextTest.cpp create mode 100644 tests/Classes/ExtensionsTest/CocoStudioGUITest/UIRichTextTest/UIRichTextTest.h diff --git a/build/cocos2d_tests.xcodeproj/project.pbxproj.REMOVED.git-id b/build/cocos2d_tests.xcodeproj/project.pbxproj.REMOVED.git-id index e9a18f650a..a8e1f5ae28 100644 --- a/build/cocos2d_tests.xcodeproj/project.pbxproj.REMOVED.git-id +++ b/build/cocos2d_tests.xcodeproj/project.pbxproj.REMOVED.git-id @@ -1 +1 @@ -d5bc7043083749a9bb33b41b6f902c251d4de747 \ No newline at end of file +d5aad52713bc25f053afa48235e48aae9155f328 \ No newline at end of file diff --git a/cocos/gui/Android.mk b/cocos/gui/Android.mk index b74d7316bf..ba8a3dcf17 100644 --- a/cocos/gui/Android.mk +++ b/cocos/gui/Android.mk @@ -23,7 +23,8 @@ UITextAtlas.cpp \ UITextBMFont.cpp \ UILoadingBar.cpp \ UISlider.cpp \ -UITextField.cpp +UITextField.cpp \ +UIRichText.cpp LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/.. \ diff --git a/cocos/gui/CMakeLists.txt b/cocos/gui/CMakeLists.txt index c6a9905072..4b78830596 100644 --- a/cocos/gui/CMakeLists.txt +++ b/cocos/gui/CMakeLists.txt @@ -17,6 +17,7 @@ set(GUI_SRC UILoadingBar.cpp UISlider.cpp UITextField.cpp + UIRichText.cpp ) add_library(gui STATIC diff --git a/cocos/gui/CocosGUI.h b/cocos/gui/CocosGUI.h index 72300d6c60..e390c0c4fd 100644 --- a/cocos/gui/CocosGUI.h +++ b/cocos/gui/CocosGUI.h @@ -41,6 +41,7 @@ THE SOFTWARE. #include "gui/UITextBMFont.h" #include "gui/UIPageView.h" #include "gui/UIHelper.h" +#include "gui/UIRichText.h" NS_CC_BEGIN namespace ui { diff --git a/tests/Android.mk b/tests/Android.mk index 2fb5d83a75..8bd6505386 100644 --- a/tests/Android.mk +++ b/tests/Android.mk @@ -80,6 +80,7 @@ Classes/ExtensionsTest/CocoStudioGUITest/UITextAtlasTest/UITextAtlasTest.cpp \ Classes/ExtensionsTest/CocoStudioGUITest/UITextBMFontTest/UITextBMFontTest.cpp \ Classes/ExtensionsTest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp \ Classes/ExtensionsTest/CocoStudioGUITest/UIWidgetAddNodeTest/UIWidgetAddNodeTest.cpp \ +Classes/ExtensionsTest/CocoStudioGUITest/UIRichTextTest/UIRichTextTest.cpp \ Classes/ExtensionsTest/CocoStudioGUITest/UIScene_Editor.cpp \ Classes/ExtensionsTest/CocoStudioGUITest/UISceneManager_Editor.cpp \ Classes/ExtensionsTest/CocoStudioGUITest/UIButtonTest/UIButtonTest_Editor.cpp \ diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index e899e602ce..b6dc3d4f41 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -94,6 +94,7 @@ set(SAMPLE_SRC Classes/ExtensionsTest/CocoStudioGUITest/UITextBMFontTest/UITextBMFontTest.cpp Classes/ExtensionsTest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp Classes/ExtensionsTest/CocoStudioGUITest/UIWidgetAddNodeTest/UIWidgetAddNodeTest.cpp + Classes/ExtensionsTest/CocoStudioGUITest/UIRichTextTest/UIRichTextTest.cpp Classes/ExtensionsTest/CocoStudioGUITest/UIScene_Editor.cpp Classes/ExtensionsTest/CocoStudioGUITest/UISceneManager_Editor.cpp Classes/ExtensionsTest/CocoStudioGUITest/UIButtonTest/UIButtonTest_Editor.cpp diff --git a/tests/Classes/ExtensionsTest/CocoStudioGUITest/CocosGUIScene.cpp b/tests/Classes/ExtensionsTest/CocoStudioGUITest/CocosGUIScene.cpp index 21818d50ee..2447255365 100644 --- a/tests/Classes/ExtensionsTest/CocoStudioGUITest/CocosGUIScene.cpp +++ b/tests/Classes/ExtensionsTest/CocoStudioGUITest/CocosGUIScene.cpp @@ -255,6 +255,18 @@ g_guisTests[] = Director::getInstance()->replaceScene(pScene); } }, + { + "gui RichTextTest", + [](Ref* sender) + { + UISceneManager* pManager = UISceneManager::sharedUISceneManager(); + pManager->setCurrentUISceneId(kUIRichTextTest); + pManager->setMinUISceneId(kUIRichTextTest); + pManager->setMaxUISceneId(kUIRichTextTest); + Scene* pScene = pManager->currentUIScene(); + Director::getInstance()->replaceScene(pScene); + } + }, }; static const int g_maxTests = sizeof(g_guisTests) / sizeof(g_guisTests[0]); diff --git a/tests/Classes/ExtensionsTest/CocoStudioGUITest/UIRichTextTest/UIRichTextTest.cpp b/tests/Classes/ExtensionsTest/CocoStudioGUITest/UIRichTextTest/UIRichTextTest.cpp new file mode 100644 index 0000000000..27dbadffb7 --- /dev/null +++ b/tests/Classes/ExtensionsTest/CocoStudioGUITest/UIRichTextTest/UIRichTextTest.cpp @@ -0,0 +1,103 @@ + + +#include "UIRichTextTest.h" +#include "cocostudio/CCArmatureDataManager.h" +#include "cocostudio/CCArmature.h" + +UIRichTextTest::UIRichTextTest() +{ + +} + +UIRichTextTest::~UIRichTextTest() +{ + +} + +bool UIRichTextTest::init() +{ + if (UIScene::init()) + { + Size widgetSize = _widget->getSize(); + + // Add the alert + Text *alert = Text::create(); + alert->setText("RichText"); + alert->setFontName("Marker Felt"); + alert->setFontSize(30); + alert->setColor(Color3B(159, 168, 176)); + alert->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getSize().height * 3.125)); + _widget->addChild(alert); + + + Button* button = Button::create(); + button->setTouchEnabled(true); + button->loadTextures("cocosgui/animationbuttonnormal.png", "cocosgui/animationbuttonpressed.png", ""); + button->setTitleText("switch"); + button->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f + button->getSize().height * 2.5)); + button->addTouchEventListener(this, toucheventselector(UIRichTextTest::touchEvent)); + button->setZOrder(10); + _widget->addChild(button); + + + // RichText + _richText = RichText::create(); + _richText->ignoreContentAdaptWithSize(false); + _richText->setSize(Size(100, 100)); + + RichElementText* re1 = RichElementText::create(1, Color3B::WHITE, 255, "This color is white. ", "Helvetica", 10); + RichElementText* re2 = RichElementText::create(2, Color3B::YELLOW, 255, "And this is yellow. ", "Helvetica", 10); + RichElementText* re3 = RichElementText::create(3, Color3B::BLUE, 255, "This one is blue. ", "Helvetica", 10); + RichElementText* re4 = RichElementText::create(4, Color3B::GREEN, 255, "And green. ", "Helvetica", 10); + RichElementText* re5 = RichElementText::create(5, Color3B::RED, 255, "Last one is red ", "Helvetica", 10); + + RichElementImage* reimg = RichElementImage::create(6, Color3B::WHITE, 255, "cocosgui/sliderballnormal.png"); + + cocostudio::ArmatureDataManager::getInstance()->addArmatureFileInfo("cocosgui/100/100.ExportJson"); + cocostudio::Armature *pAr = cocostudio::Armature::create("100"); + pAr->getAnimation()->play("Animation1"); + + RichElementCustomNode* recustom = RichElementCustomNode::create(1, Color3B::WHITE, 255, pAr); + RichElementText* re6 = RichElementText::create(7, Color3B::ORANGE, 255, "Have fun!! ", "Helvetica", 10); + _richText->pushBackElement(re1); + _richText->insertElement(re2, 1); + _richText->pushBackElement(re3); + _richText->pushBackElement(re4); + _richText->pushBackElement(re5); + _richText->insertElement(reimg, 2); + _richText->pushBackElement(recustom); + _richText->pushBackElement(re6); + + _richText->setPosition(Point(widgetSize.width / 2, widgetSize.height / 2)); + _richText->setZOrder(10); + + + _widget->addChild(_richText); + + return true; + } + return false; +} + +void UIRichTextTest::touchEvent(Ref *pSender, TouchEventType type) +{ + switch (type) + { + case TOUCH_EVENT_ENDED: + { + if (_richText->isIgnoreContentAdaptWithSize()) + { + _richText->ignoreContentAdaptWithSize(false); + _richText->setSize(Size(100, 100)); + } + else + { + _richText->ignoreContentAdaptWithSize(true); + } + } + break; + + default: + break; + } +} \ No newline at end of file diff --git a/tests/Classes/ExtensionsTest/CocoStudioGUITest/UIRichTextTest/UIRichTextTest.h b/tests/Classes/ExtensionsTest/CocoStudioGUITest/UIRichTextTest/UIRichTextTest.h new file mode 100644 index 0000000000..0fd2db97f4 --- /dev/null +++ b/tests/Classes/ExtensionsTest/CocoStudioGUITest/UIRichTextTest/UIRichTextTest.h @@ -0,0 +1,23 @@ + + +#ifndef __TestCpp__UIRichTextTest__ +#define __TestCpp__UIRichTextTest__ + +#include "../UIScene.h" + +class UIRichTextTest : public UIScene +{ +public: + UIRichTextTest(); + ~UIRichTextTest(); + bool init(); + void touchEvent(Ref *pSender, TouchEventType type); + +protected: + UI_SCENE_CREATE_FUNC(UIRichTextTest) + +protected: + RichText* _richText; +}; + +#endif /* defined(__TestCpp__UIRichTextTest__) */ diff --git a/tests/Classes/ExtensionsTest/CocoStudioGUITest/UISceneManager.cpp b/tests/Classes/ExtensionsTest/CocoStudioGUITest/UISceneManager.cpp index 1f869a181d..b85beee93f 100644 --- a/tests/Classes/ExtensionsTest/CocoStudioGUITest/UISceneManager.cpp +++ b/tests/Classes/ExtensionsTest/CocoStudioGUITest/UISceneManager.cpp @@ -16,6 +16,7 @@ #include "UIPageViewTest/UIPageViewTest.h" #include "UIListViewTest/UIListViewTest.h" #include "UIWidgetAddNodeTest/UIWidgetAddNodeTest.h" +#include "UIRichTextTest/UIRichTextTest.h" /* #include "UISwitchTest/UISwitchTest.h" */ @@ -107,6 +108,7 @@ static const char* s_testArray[] = "UIPickerViewTest_Horizontal", */ "UIWidgetAddNodeTest", + "UIRichTextTest", }; static UISceneManager *sharedInstance = NULL; @@ -354,6 +356,9 @@ Scene *UISceneManager::currentUIScene() case kUIWidgetAddNodeTest: return UIWidgetAddNodeTest::sceneWithTitle(s_testArray[_currentUISceneId]); + + case kUIRichTextTest: + return UIRichTextTest::sceneWithTitle(s_testArray[_currentUISceneId]); } return NULL; } \ No newline at end of file diff --git a/tests/Classes/ExtensionsTest/CocoStudioGUITest/UISceneManager.h b/tests/Classes/ExtensionsTest/CocoStudioGUITest/UISceneManager.h index a337386f3d..feb293678e 100644 --- a/tests/Classes/ExtensionsTest/CocoStudioGUITest/UISceneManager.h +++ b/tests/Classes/ExtensionsTest/CocoStudioGUITest/UISceneManager.h @@ -100,6 +100,7 @@ enum kUIPickerViewTest_Horizontal, */ kUIWidgetAddNodeTest, + kUIRichTextTest, kUITestMax };