From e8b7bc7c7e36cc6a124aa469249a951dd6a0432d Mon Sep 17 00:00:00 2001 From: minggo Date: Thu, 22 Mar 2012 09:40:32 +0800 Subject: [PATCH] issue #1056: LabelTest --- tests/tests/EffectsTest/EffectsTest.cpp | 33 +--- tests/tests/EffectsTest/EffectsTest.h | 1 - tests/tests/LabelTest/LabelTest.cpp | 202 +++++++++++++++++++++++- tests/tests/LabelTest/LabelTest.h | 22 +++ 4 files changed, 227 insertions(+), 31 deletions(-) diff --git a/tests/tests/EffectsTest/EffectsTest.cpp b/tests/tests/EffectsTest/EffectsTest.cpp index 947055e359..9b662d0a1b 100644 --- a/tests/tests/EffectsTest/EffectsTest.cpp +++ b/tests/tests/EffectsTest/EffectsTest.cpp @@ -41,7 +41,7 @@ class Shaky3DDemo : public CCShaky3D public: static CCActionInterval* actionWithDuration(ccTime t) { - return CCShaky3D::actionWithRange(5, true, ccg(15,10), t); + return CCShaky3D::actionWithRange(5, false, ccg(15,10), t); } }; @@ -138,7 +138,7 @@ class ShakyTiles3DDemo : public CCShakyTiles3D public: static CCActionInterval* actionWithDuration(ccTime t) { - return CCShakyTiles3D::actionWithRange(5, true, ccg(16,12), t) ; + return CCShakyTiles3D::actionWithRange(5, false, ccg(16,12), t) ; } }; @@ -148,7 +148,7 @@ class ShatteredTiles3DDemo : public CCShatteredTiles3D public: static CCActionInterval* actionWithDuration(ccTime t) { - return CCShatteredTiles3D::actionWithRange(5, true, ccg(16,12), t); + return CCShatteredTiles3D::actionWithRange(5, false, ccg(16,12), t); } }; @@ -339,7 +339,7 @@ void EffectTestScene::runThisTest() TextLayer::TextLayer(void) { - initWithColor( ccc4(32,32,32,255) ); + initWithColor( ccc4(32,128,32,255) ); float x,y; @@ -418,28 +418,6 @@ void TextLayer::onEnter() CCLayer::onEnter(); } -void TextLayer::newOrientation() -{ - ccDeviceOrientation orientation = CCDirector::sharedDirector()->getDeviceOrientation(); - switch (orientation) - { - case CCDeviceOrientationLandscapeLeft: - orientation = CCDeviceOrientationPortrait; - break; - case CCDeviceOrientationPortrait: - orientation = CCDeviceOrientationLandscapeRight; - break; - case CCDeviceOrientationLandscapeRight: - orientation = CCDeviceOrientationPortraitUpsideDown; - break; - case CCDeviceOrientationPortraitUpsideDown: - orientation = CCDeviceOrientationLandscapeLeft; - break; - } - - CCDirector::sharedDirector()->setDeviceOrientation(orientation); -} - void TextLayer::newScene() { CCScene* s = new EffectTestScene(); @@ -451,7 +429,6 @@ void TextLayer::newScene() void TextLayer::restartCallback(CCObject* pSender) { - /*newOrientation();*/ newScene(); } @@ -461,7 +438,6 @@ void TextLayer::nextCallback(CCObject* pSender) actionIdx++; actionIdx = actionIdx % MAX_LAYER; - /*newOrientation();*/ newScene(); } @@ -473,6 +449,5 @@ void TextLayer::backCallback(CCObject* pSender) if( actionIdx < 0 ) actionIdx += total; - /*newOrientation();*/ newScene(); } diff --git a/tests/tests/EffectsTest/EffectsTest.h b/tests/tests/EffectsTest/EffectsTest.h index 989eb7b192..cbe6430e9a 100644 --- a/tests/tests/EffectsTest/EffectsTest.h +++ b/tests/tests/EffectsTest/EffectsTest.h @@ -26,7 +26,6 @@ public: void nextCallback(CCObject* pSender); void backCallback(CCObject* pSender); - void newOrientation(); void newScene(); static TextLayer* node(); diff --git a/tests/tests/LabelTest/LabelTest.cpp b/tests/tests/LabelTest/LabelTest.cpp index 6582994f4a..3108578d94 100644 --- a/tests/tests/LabelTest/LabelTest.cpp +++ b/tests/tests/LabelTest/LabelTest.cpp @@ -40,7 +40,7 @@ CCLayer* restartAtlasAction(); static int sceneIdx = -1; -#define MAX_LAYER 18 +#define MAX_LAYER 19 CCLayer* createAtlasLayer(int nIndex) { @@ -66,6 +66,7 @@ CCLayer* createAtlasLayer(int nIndex) case 15: return new LabelTTFMultiline(); case 16: return new LabelTTFChinese(); case 17: return new LabelBMFontChinese(); + case 18: return new BitmapFontMultiLineAlignment(); } return NULL; @@ -978,3 +979,202 @@ string LabelBMFontChinese::title() { return "Testing CCLabelBMFont with Chinese character"; } + +/// BitmapFontMultiLineAlignment + +#define LongSentencesExample "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." +#define LineBreaksExample "Lorem ipsum dolor\nsit amet\nconsectetur adipisicing elit\nblah\nblah" +#define MixedExample "ABC\nLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt\nDEF" + +#define ArrowsMax 0.95 +#define ArrowsMin 0.7 + +#define LeftAlign 0 +#define CenterAlign 1 +#define RightAlign 2 + +#define LongSentences 0 +#define LineBreaks 1 +#define Mixed 2 + +static float alignmentItemPadding = 50; +static float menuItemPaddingCenter = 50; +BitmapFontMultiLineAlignment::BitmapFontMultiLineAlignment() +{ + this->setIsTouchEnabled(true); + + // ask director the the window size + CCSize size = CCDirector::sharedDirector()->getWinSize(); + + // create and initialize a Label + this->m_pLabelShouldRetain = CCLabelBMFont::labelWithString(LongSentencesExample, "Fonts/markerFelt.fnt", size.width/1.5, CCTextAlignmentCenter); + this->m_pLabelShouldRetain->retain(); + + this->m_pArrowsBarShouldRetain = CCSprite::spriteWithFile("Images/arrowsBar.png"); + this->m_pArrowsBarShouldRetain->retain(); + this->m_pArrowsShouldRetain = CCSprite::spriteWithFile("Images/arrows.png"); + this->m_pArrowsShouldRetain->retain(); + + CCMenuItemFont::setFontSize(20); + CCMenuItemFont *longSentences = CCMenuItemFont::itemWithString("Long Flowing Sentences", this, menu_selector(BitmapFontMultiLineAlignment::stringChanged)); + CCMenuItemFont *lineBreaks = CCMenuItemFont::itemWithString("Short Sentences With Intentional Line Breaks", this, menu_selector(BitmapFontMultiLineAlignment::stringChanged)); + CCMenuItemFont *mixed = CCMenuItemFont::itemWithString("Long Sentences Mixed With Intentional Line Breaks", this, menu_selector(BitmapFontMultiLineAlignment::stringChanged)); + CCMenu *stringMenu = CCMenu::menuWithItems(longSentences, lineBreaks, mixed, NULL); + stringMenu->alignItemsVertically(); + + longSentences->setColor(ccRED); + m_pLastSentenceItem = longSentences; + longSentences->setTag(LineBreaks); + lineBreaks->setTag(LineBreaks); + mixed->setTag(Mixed); + + CCMenuItemFont::setFontSize(30); + + CCMenuItemFont *left = CCMenuItemFont::itemWithString("Left", this, menu_selector(BitmapFontMultiLineAlignment::alignmentChanged)); + CCMenuItemFont *center = CCMenuItemFont::itemWithString("Center", this, menu_selector(BitmapFontMultiLineAlignment::alignmentChanged)); + CCMenuItemFont *right = CCMenuItemFont::itemWithString("Right", this, menu_selector(BitmapFontMultiLineAlignment::alignmentChanged)); + CCMenu *alignmentMenu = CCMenu::menuWithItems(left, center, right, NULL); + alignmentMenu->alignItemsHorizontallyWithPadding(); + + center->setColor(ccRED); + m_pLastAlignmentItem = center; + left->setTag(LeftAlign); + center->setTag(CenterAlign); + right->setTag(RightAlign); + + // position the label on the center of the screen + this->m_pLabelShouldRetain->setPosition(ccp(size.width/2, size.height/2)); + + this->m_pArrowsBarShouldRetain->setIsVisible(false); + + float arrowsWidth = (ArrowsMax - ArrowsMin) * size.width; + this->m_pArrowsBarShouldRetain->setScaleX(arrowsWidth / this->m_pArrowsBarShouldRetain->getContentSize().width); + this->m_pArrowsBarShouldRetain->setPosition(ccp(((ArrowsMax + ArrowsMin) / 2) * size.width, this->m_pLabelShouldRetain->getPosition().y)); + + this->snapArrowsToEdge(); + + stringMenu->setPosition(ccp(size.width/2, size.height - menuItemPaddingCenter)); + alignmentMenu->setPosition(ccp(size.width/2, menuItemPaddingCenter+15)); + + this->addChild(this->m_pLabelShouldRetain); + this->addChild(this->m_pArrowsBarShouldRetain); + this->addChild(this->m_pArrowsShouldRetain); + this->addChild(stringMenu); + this->addChild(alignmentMenu); +} + +BitmapFontMultiLineAlignment::~BitmapFontMultiLineAlignment() +{ + this->m_pLabelShouldRetain->release(); + this->m_pArrowsBarShouldRetain->release(); + this->m_pArrowsShouldRetain->release(); +} + +std::string BitmapFontMultiLineAlignment::title() +{ + return ""; +} + +std::string BitmapFontMultiLineAlignment::subtitle() +{ + return ""; +} + +void BitmapFontMultiLineAlignment::stringChanged(cocos2d::CCObject *sender) +{ + CCMenuItemFont *item = (CCMenuItemFont*)sender; + item->setColor(ccRED); + this->m_pLastAlignmentItem->setColor(ccWHITE); + this->m_pLastAlignmentItem = item; + + switch(item->getTag()) + { + case LongSentences: + this->m_pLabelShouldRetain->setString(LongSentencesExample); + break; + case LineBreaks: + this->m_pLabelShouldRetain->setString(LineBreaksExample); + break; + case Mixed: + this->m_pLabelShouldRetain->setString(MixedExample); + break; + + default: + break; + } + + this->snapArrowsToEdge(); +} + +void BitmapFontMultiLineAlignment::alignmentChanged(cocos2d::CCObject *sender) +{ + CCMenuItemFont *item = (CCMenuItemFont*)sender; + item->setColor(ccRED); + this->m_pLastAlignmentItem->setColor(ccWHITE); + this->m_pLastAlignmentItem = item; + + switch(item->getTag()) + { + case LeftAlign: + this->m_pLabelShouldRetain->setAlignment(CCTextAlignmentLeft); + break; + case CenterAlign: + this->m_pLabelShouldRetain->setAlignment(CCTextAlignmentCenter); + break; + case RightAlign: + this->m_pLabelShouldRetain->setAlignment(CCTextAlignmentRight); + break; + + default: + break; + } + + this->snapArrowsToEdge(); +} + +void BitmapFontMultiLineAlignment::ccTouchesBegan(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent) +{ + CCTouch *touch = pTouches->anyObject(); + CCPoint location = touch->locationInView(); + + if (CCRectContainPoint(this->m_pArrowsShouldRetain->getBoundingBox(), location) + { + m_drag = true; + this->m_pArrowsBarShouldRetain->setVisible(true); + } +} + +void BitmapFontMultiLineAlignment::ccTouchesEnded(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent) +{ + m_drag = false; + this->snapArrowsToEdge(); + + this->m_pArrowsBarShouldRetain->setVisible(false); +} + +void BitmapFontMultiLineAlignment::ccTouchesMoved(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent) +{ + if (! m_drag) + { + return; + } + + CCTouch *touch = (CCTouch *)pTouches->anyObject(); + CCPoint location = touch->locationInView(); + + CCSize winSize = CCDirector::sharedDirector()->getWinSize(); + + this->m_pArrowsShouldRetain->setPosition(ccp(MAX(MIN(location.x, ArrowsMax*winSize.width), ArrowsMin*winSize.width), + this>m_pArrowsShouldRetain->getPosition().y); + + float labelWidh = abs(this->m_pArrowsShouldRetain->getPosition().x - this->m_pLabelShouldRetain->getPosition().x) * 2; + + this->m_pLabelShouldRetain->setWidth(labelWidth); +); +} + +void BitmapFontMultiLineAlignment::snapArrowsToEdge() +{ + this->m_pArrowsShouldRetain->setPosition(ccp(this->m_pLabelShouldRetain->getPosition().x + this->m_pLabelShouldRetain->getContentSize().width/2, + this->m_pLabelShouldRetain->getPosition().y)); +} diff --git a/tests/tests/LabelTest/LabelTest.h b/tests/tests/LabelTest/LabelTest.h index 3c17c237b6..87c4991279 100644 --- a/tests/tests/LabelTest/LabelTest.h +++ b/tests/tests/LabelTest/LabelTest.h @@ -192,6 +192,28 @@ public: virtual std::string title(); }; +class BitmapFontMultiLineAlignment : public AtlasDemo +{ +public: + BitmapFontMultiLineAlignment(); + ~BitmapFontMultiLineAlignment(); + void snapArrowsToEdge(); + virtual std::string title(); + virtual std::string subtitle(); + void stringChanged(CCObject *sender); + void alignmentChanged(CCObject *sender); + virtual void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent); + virtual void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent); + virtual void ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent); + +public: + CCLabelBMFont *m_pLabelShouldRetain; + CCSprite *m_pArrowsBarShouldRetain; + CCSprite *m_pArrowsShouldRetain; + CCMenuItemFont *m_pLastSentenceItem, m_pLastAlignmentItem; + bool m_drag; +}; + // we don't support linebreak mode #endif