Merge pull request #993 from minggo/gles20

issue #1310: synchronize CCLabelTTF
This commit is contained in:
minggo 2012-06-10 23:37:31 -07:00
commit 55ee220245
7 changed files with 289 additions and 139 deletions

View File

@ -26,6 +26,7 @@ THE SOFTWARE.
#include "CCDirector.h"
#include "CCGLProgram.h"
#include "CCShaderCache.h"
#include "CCApplication.h"
NS_CC_BEGIN
@ -39,34 +40,33 @@ NS_CC_BEGIN
//CCLabelTTF
//
CCLabelTTF::CCLabelTTF()
: m_eAlignment(kCCTextAlignmentCenter)
, m_pFontName(NULL)
, m_fFontSize(0.0)
, m_pString(NULL)
: m_hAlignment(kCCTextAlignmentCenter)
, m_vAlignment(kCCVerticalTextAlignmentTop)
, m_pFontName(NULL)
, m_fFontSize(0.0)
, m_string("")
{
}
CCLabelTTF::~CCLabelTTF()
{
CC_SAFE_DELETE(m_pFontName);
CC_SAFE_DELETE(m_pString);
CC_SAFE_DELETE(m_pFontName);
}
CCLabelTTF * CCLabelTTF::labelWithString(const char *label, const CCSize& dimensions, CCTextAlignment alignment, const char *fontName, float fontSize)
CCLabelTTF * CCLabelTTF::labelWithString(const char *string, const char *fontName, float fontSize)
{
CCLabelTTF *pRet = new CCLabelTTF();
if(pRet && pRet->initWithString(label, dimensions, alignment, fontName, fontSize))
{
pRet->autorelease();
return pRet;
}
CC_SAFE_DELETE(pRet);
return NULL;
return labelWithString(string, CCSizeZero, kCCTextAlignmentCenter, kCCVerticalTextAlignmentTop, fontName, fontSize);
}
CCLabelTTF * CCLabelTTF::labelWithString(const char *label, const char *fontName, float fontSize)
CCLabelTTF * CCLabelTTF::labelWithString(const char *string, const CCSize& dimensions, CCTextAlignment hAlignment, const char *fontName, float fontSize)
{
return labelWithString(string, dimensions, hAlignment, kCCVerticalTextAlignmentTop, fontName, fontSize);
}
CCLabelTTF* CCLabelTTF::labelWithString(const char *string, const cocos2d::CCSize &dimensions, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment, const char *fontName, float fontSize)
{
CCLabelTTF *pRet = new CCLabelTTF();
if(pRet && pRet->initWithString(label, fontName, fontSize))
if(pRet && pRet->initWithString(string, dimensions, hAlignment, vAlignment, fontName, fontSize))
{
pRet->autorelease();
return pRet;
@ -82,93 +82,50 @@ bool CCLabelTTF::init()
bool CCLabelTTF::initWithString(const char *label, const CCSize& dimensions, CCTextAlignment alignment, const char *fontName, float fontSize)
{
CCAssert(label != NULL, "");
if (CCSprite::init())
{
// shader program
setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(SHADER_PROGRAM));
m_tDimensions = CCSizeMake( dimensions.width * CC_CONTENT_SCALE_FACTOR(), dimensions.height * CC_CONTENT_SCALE_FACTOR() );
m_eAlignment = alignment;
CC_SAFE_DELETE(m_pFontName);
m_pFontName = new std::string(fontName);
m_fFontSize = fontSize * CC_CONTENT_SCALE_FACTOR();
this->setString(label);
return true;
}
return false;
return this->initWithString(label, dimensions, alignment, kCCVerticalTextAlignmentTop, fontName, fontSize);
}
bool CCLabelTTF::initWithString(const char *label, const char *fontName, float fontSize)
{
CCAssert(label != NULL, "");
return this->initWithString(label, CCSizeZero, kCCTextAlignmentLeft, kCCVerticalTextAlignmentTop, fontName, fontSize);
}
bool CCLabelTTF::initWithString(const char *string, const cocos2d::CCSize &dimensions, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment, const char *fontName, float fontSize)
{
if (CCSprite::init())
{
// shader program
setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(SHADER_PROGRAM));
m_tDimensions = CCSizeZero;
CC_SAFE_DELETE(m_pFontName);
this->setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(SHADER_PROGRAM));
m_tDimensions = CCSizeMake(dimensions.width, dimensions.height);
m_hAlignment = hAlignment;
m_vAlignment = vAlignment;
m_pFontName = new std::string(fontName);
m_fFontSize = fontSize * CC_CONTENT_SCALE_FACTOR();
this->setString(label);
m_fFontSize = fontSize;
this->setString(string);
return true;
}
return false;
}
void CCLabelTTF::setString(const char *label)
void CCLabelTTF::setString(const char *string)
{
if (m_pString)
{
delete m_pString;
m_pString = NULL;
}
m_pString = new std::string(label);
CCAssert(string != NULL, "Invalid string");
CCTexture2D *texture;
if( CCSize::CCSizeEqualToSize( m_tDimensions, CCSizeZero ) )
if (m_string.compare(string))
{
texture = new CCTexture2D();
texture->initWithString(label, m_pFontName->c_str(), m_fFontSize);
m_string = string;
this->updateTexture();
}
else
{
texture = new CCTexture2D();
//cjh texture->initWithString(label, m_tDimensions, m_eAlignment, kCCVerticalTextAlignmentTop, m_pFontName->c_str(), m_fFontSize);
}
// TODO
// #ifdef __CC_PLATFORM_IOS
// // iPad ?
// if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) {
// if( CC_CONTENT_SCALE_FACTOR() == 2 )
// [tex setResolutionType:kCCResolutioniPadRetinaDisplay];
// else
// [tex setResolutionType:kCCResolutioniPad];
// }
// // iPhone ?
// else
// {
// if( CC_CONTENT_SCALE_FACTOR() == 2 )
// [tex setResolutionType:kCCResolutioniPhoneRetinaDisplay];
// else
// [tex setResolutionType:kCCResolutioniPhone];
// }
// #end
this->setTexture(texture);
texture->release();
CCRect rect = CCRectZero;
rect.size = m_pobTexture->getContentSize();
this->setTextureRect(rect);
}
const char* CCLabelTTF::getString(void)
{
return m_pString->c_str();
return m_string.c_str();
}
const char* CCLabelTTF::description()
@ -176,4 +133,152 @@ const char* CCLabelTTF::description()
return CCString::stringWithFormat("<CCLabelTTF | FontName = %s, FontSize = %.1f>", m_pFontName->c_str(), m_fFontSize)->getCString();
}
CCTextAlignment CCLabelTTF::getHorizontalAlignment()
{
return m_hAlignment;
}
void CCLabelTTF::setHorizontalAlignment(CCTextAlignment alignment)
{
if (alignment != m_hAlignment)
{
m_hAlignment = alignment;
// Force update
if (m_string.size() > 0)
{
this->updateTexture();
}
}
}
CCVerticalTextAlignment CCLabelTTF::getVerticalAlignment()
{
return m_vAlignment;
}
void CCLabelTTF::setVerticalAlignment(CCVerticalTextAlignment verticalAlignment)
{
if (verticalAlignment != m_vAlignment)
{
m_vAlignment = verticalAlignment;
// Force update
if (m_string.size() > 0)
{
this->updateTexture();
}
}
}
CCSize CCLabelTTF::getDimensions()
{
return m_tDimensions;
}
void CCLabelTTF::setDimensions(CCSize &dim)
{
if (dim.width != m_tDimensions.width || dim.height != m_tDimensions.height)
{
m_tDimensions = dim;
// Force udpate
if (m_string.size() > 0)
{
this->updateTexture();
}
}
}
float CCLabelTTF::getFontSize()
{
return m_fFontSize;
}
void CCLabelTTF::setFontSize(float fontSize)
{
if (m_fFontSize != fontSize)
{
// Force update
if (m_string.size() > 0)
{
this->updateTexture();
}
}
}
const char* CCLabelTTF::getFontName()
{
return m_pFontName->c_str();
}
void CCLabelTTF::setFontName(const char *fontName)
{
if (m_pFontName->compare(fontName))
{
delete m_pFontName;
m_pFontName = new std::string(fontName);
// Force update
if (m_string.size() > 0)
{
this->updateTexture();
}
}
}
// Helper
void CCLabelTTF::updateTexture()
{
CCTexture2D *tex;
if (m_tDimensions.width == 0 || m_tDimensions.height == 0)
{
tex = new CCTexture2D();
tex->initWithString(m_string.c_str(), m_pFontName->c_str(), m_fFontSize * CC_CONTENT_SCALE_FACTOR()) ;
}
else
{
tex = new CCTexture2D();
tex->initWithString(m_string.c_str(),
CC_SIZE_POINTS_TO_PIXELS(m_tDimensions),
m_hAlignment,
m_vAlignment,
m_pFontName->c_str(),
m_fFontSize * CC_CONTENT_SCALE_FACTOR());
}
// iPad ?
//if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) {
if (CCApplication::sharedApplication().isIpad())
{
if (CC_CONTENT_SCALE_FACTOR() == 2)
{
tex->setResolutionType(kCCResolutioniPadRetinaDisplay);
}
else
{
tex->setResolutionType(kCCResolutioniPad);
}
}
// iPhone ?
else
{
if (CC_CONTENT_SCALE_FACTOR() == 2)
{
tex->setResolutionType(kCCResolutioniPhoneRetinaDisplay);
}
else
{
tex->setResolutionType(kCCResolutioniPhone);
}
}
this->setTexture(tex);
tex->release();
CCRect rect = CCRectZero;
rect.size = m_pobTexture->getContentSize();
this->setTextureRect(rect);
}
NS_CC_END

View File

@ -41,33 +41,70 @@ class CC_DLL CCLabelTTF : public CCSprite, public CCLabelProtocol
public:
CCLabelTTF();
virtual ~CCLabelTTF();
const char* description();
/** creates a CCLabelTTF from a fontname, alignment, dimension and font size */
static CCLabelTTF * labelWithString(const char *label, const CCSize& dimensions, CCTextAlignment alignment, const char *fontName, float fontSize);
/** creates a CCLabelTTF from a fontname and font size */
static CCLabelTTF * labelWithString(const char *label, const char *fontName, float fontSize);
const char* description();
/** creates a CCLabelTTF with a font name and font size in points*/
static CCLabelTTF * labelWithString(const char *string, const char *fontName, float fontSize);
/** creates a CCLabelTTF from a fontname, horizontal alignment, dimension in points, and font size in points.
@since v1.0
*/
static CCLabelTTF * labelWithString(const char *string, const CCSize& dimensions, CCTextAlignment hAlignment,
const char *fontName, float fontSize);
/** creates a CCLabel from a fontname, alignment, dimension in points and font size in points*/
static CCLabelTTF * labelWithString(const char *string, const CCSize& dimensions, CCTextAlignment hAlignment,
CCVerticalTextAlignment vAlignment, const char *fontName, float fontSize);
/** initializes the CCLabelTTF with a font name, alignment, dimension and font size */
bool initWithString(const char *label, const CCSize& dimensions, CCTextAlignment alignment, const char *fontName, float fontSize);
bool initWithString(const char *string, const CCSize& dimensions, CCTextAlignment hAlignment, const char *fontName, float fontSize);
/** initializes the CCLabelTTF with a font name, alignment, dimension and font size */
bool initWithString(const char *string, const CCSize& dimensions, CCTextAlignment hAlignment,
CCVerticalTextAlignment vAlignment, const char *fontName, float fontSize);
/** initializes the CCLabelTTF with a font name and font size */
bool initWithString(const char *label, const char *fontName, float fontSize);
bool initWithString(const char *string, const char *fontName, float fontSize);
/** initializes the CCLabelTTF */
bool init();
/** changes the string to render
* @warning Changing the string is as expensive as creating a new CCLabelTTF. To obtain better performance use CCLabelAtlas
*/
virtual void setString(const char *label);
virtual const char* getString(void);
CCTextAlignment getHorizontalAlignment();
void setHorizontalAlignment(CCTextAlignment alignment);
CCVerticalTextAlignment getVerticalAlignment();
void setVerticalAlignment(CCVerticalTextAlignment verticalAlignment);
CCSize getDimensions();
void setDimensions(CCSize &dim);
float getFontSize();
void setFontSize(float fontSize);
const char* getFontName();
void setFontName(const char *fontName);
virtual CCLabelProtocol* convertToLabelProtocol() { return (CCLabelProtocol*)this; }
private:
void updateTexture();
protected:
/** Dimensions of the label in Points */
CCSize m_tDimensions;
CCTextAlignment m_eAlignment;
/** The alignment of the label */
CCTextAlignment m_hAlignment;
/** The vertical alignment of the label */
CCVerticalTextAlignment m_vAlignment;
/** Font name used in the label */
std::string * m_pFontName;
/** Font size of the label */
float m_fFontSize;
std::string * m_pString;
std::string m_string;
};
NS_CC_END

View File

@ -43,6 +43,8 @@ public:
@return Current language config
*/
virtual ccLanguageType getCurrentLanguage() = 0;
virtual bool isIpad() { return false; }
};

View File

@ -55,11 +55,6 @@ CCEGLViewProtocol::~CCEGLViewProtocol()
}
bool CCEGLViewProtocol::isIpad()
{
return false;
}
void CCEGLViewProtocol::setFrameSize(float width, float height)
{
m_sSizeInPixel.setSize(width, height);

View File

@ -21,7 +21,6 @@ public:
virtual void swapBuffers() = 0;
virtual void setIMEKeyboardState(bool bOpen) = 0;
virtual bool isIpad();
virtual CCRect getViewPort();
virtual CCSize getSize();
virtual void setFrameSize(float width, float height);

View File

@ -35,53 +35,55 @@ class CCRect;
class CC_DLL CCApplication : public CCApplicationProtocol
{
public:
CCApplication();
virtual ~CCApplication();
CCApplication();
virtual ~CCApplication();
/**
@brief Callback by CCDirector for limit FPS.
@interval The time, which expressed in second in second, between current frame and next.
*/
void setAnimationInterval(double interval);
/**
@brief Callback by CCDirector for limit FPS.
@interval The time, which expressed in second in second, between current frame and next.
*/
void setAnimationInterval(double interval);
typedef enum
{
/// Device oriented vertically, home button on the bottom
kOrientationPortrait = 0,
typedef enum
{
/// Device oriented vertically, home button on the bottom
kOrientationPortrait = 0,
/// Device oriented vertically, home button on the top
kOrientationPortraitUpsideDown = 1,
/// Device oriented horizontally, home button on the right
kOrientationLandscapeLeft = 2,
/// Device oriented horizontally, home button on the left
kOrientationLandscapeRight = 3,
} Orientation;
kOrientationPortraitUpsideDown = 1,
/// Device oriented horizontally, home button on the right
kOrientationLandscapeLeft = 2,
/// Device oriented horizontally, home button on the left
kOrientationLandscapeRight = 3,
} Orientation;
/**
@brief Callback by CCDirector for change device orientation.
@orientation The defination of orientation which CCDirector want change to.
@return The actual orientation of the application.
*/
Orientation setOrientation(Orientation orientation);
/**
@brief Callback by CCDirector for change device orientation.
@orientation The defination of orientation which CCDirector want change to.
@return The actual orientation of the application.
*/
Orientation setOrientation(Orientation orientation);
/**
@brief Run the message loop.
*/
int run();
/**
@brief Run the message loop.
*/
int run();
/**
@brief Get current applicaiton instance.
@return Current application instance pointer.
*/
static CCApplication& sharedApplication();
/**
@brief Get current applicaiton instance.
@return Current application instance pointer.
*/
static CCApplication& sharedApplication();
/**
@brief Get current language config
@return Current language config
*/
virtual ccLanguageType getCurrentLanguage();
/**
@brief Get current language config
@return Current language config
*/
virtual ccLanguageType getCurrentLanguage();
virtual bool isIpad();
protected:
static CCApplication * sm_pSharedApplication;
static CCApplication * sm_pSharedApplication;
};
NS_CC_END

View File

@ -145,4 +145,14 @@ ccLanguageType CCApplication::getCurrentLanguage()
return ret;
}
bool CCApplication::isIpad()
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
return true;
}
return false;
}
NS_CC_END