2010-12-17 23:44:19 +08:00
|
|
|
/****************************************************************************
|
2012-09-24 21:22:20 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2011-03-19 14:45:51 +08:00
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
2010-12-17 23:44:19 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
2010-12-24 14:00:49 +08:00
|
|
|
#include "CCLabelTTF.h"
|
2011-11-28 17:28:43 +08:00
|
|
|
#include "CCDirector.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "shaders/CCGLProgram.h"
|
|
|
|
#include "shaders/CCShaderCache.h"
|
2012-06-11 14:36:25 +08:00
|
|
|
#include "CCApplication.h"
|
2010-12-17 23:44:19 +08:00
|
|
|
|
2012-03-16 13:42:53 +08:00
|
|
|
NS_CC_BEGIN
|
2011-03-15 11:59:45 +08:00
|
|
|
|
2012-03-16 13:42:53 +08:00
|
|
|
#if CC_USE_LA88_LABELS
|
2013-07-25 17:48:22 +08:00
|
|
|
#define SHADER_PROGRAM GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR
|
2012-03-16 13:42:53 +08:00
|
|
|
#else
|
2013-07-25 17:48:22 +08:00
|
|
|
#define SHADER_PROGRAM GLProgram::SHADER_NAME_POSITION_TEXTUREA8Color
|
2012-03-16 13:42:53 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
//
|
|
|
|
//CCLabelTTF
|
|
|
|
//
|
2013-06-20 14:13:12 +08:00
|
|
|
LabelTTF::LabelTTF()
|
2013-07-27 07:04:21 +08:00
|
|
|
: _alignment(TextHAlignment::CENTER)
|
|
|
|
, _vAlignment(TextVAlignment::TOP)
|
2013-06-15 14:03:30 +08:00
|
|
|
, _fontName(NULL)
|
|
|
|
, _fontSize(0.0)
|
|
|
|
, _string("")
|
|
|
|
, _shadowEnabled(false)
|
|
|
|
, _strokeEnabled(false)
|
2013-07-08 18:11:32 +08:00
|
|
|
, _textFillColor(Color3B::WHITE)
|
2012-03-16 13:42:53 +08:00
|
|
|
{
|
|
|
|
}
|
2011-03-15 11:59:45 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
LabelTTF::~LabelTTF()
|
2012-03-16 13:42:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_DELETE(_fontName);
|
2012-03-16 13:42:53 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
LabelTTF * LabelTTF::create()
|
2012-05-31 06:34:50 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
LabelTTF * pRet = new LabelTTF();
|
2012-05-31 06:34:50 +08:00
|
|
|
if (pRet && pRet->init())
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
}
|
|
|
|
return pRet;
|
2012-03-16 13:42:53 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
LabelTTF * LabelTTF::create(const char *string, const char *fontName, float fontSize)
|
2012-03-16 13:42:53 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
return LabelTTF::create(string, fontName, fontSize,
|
2013-07-27 07:04:21 +08:00
|
|
|
Size::ZERO, TextHAlignment::CENTER, TextVAlignment::TOP);
|
2012-06-11 14:36:25 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
LabelTTF * LabelTTF::create(const char *string, const char *fontName, float fontSize,
|
2013-07-27 07:04:21 +08:00
|
|
|
const Size& dimensions, TextHAlignment hAlignment)
|
2012-06-11 14:36:25 +08:00
|
|
|
{
|
2013-07-27 07:04:21 +08:00
|
|
|
return LabelTTF::create(string, fontName, fontSize, dimensions, hAlignment, TextVAlignment::TOP);
|
2012-03-16 13:42:53 +08:00
|
|
|
}
|
2012-06-11 14:36:25 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
LabelTTF* LabelTTF::create(const char *string, const char *fontName, float fontSize,
|
2013-07-27 07:04:21 +08:00
|
|
|
const Size &dimensions, TextHAlignment hAlignment,
|
|
|
|
TextVAlignment vAlignment)
|
2012-03-16 13:42:53 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
LabelTTF *pRet = new LabelTTF();
|
2012-08-01 13:24:23 +08:00
|
|
|
if(pRet && pRet->initWithString(string, fontName, fontSize, dimensions, hAlignment, vAlignment))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
return NULL;
|
2012-03-16 13:42:53 +08:00
|
|
|
}
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
LabelTTF * LabelTTF::createWithFontDefinition(const char *string, FontDefinition &textDefinition)
|
2013-06-08 07:33:01 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
LabelTTF *pRet = new LabelTTF();
|
2013-06-08 07:33:01 +08:00
|
|
|
if(pRet && pRet->initWithStringAndTextDefinition(string, textDefinition))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool LabelTTF::init()
|
2012-03-16 13:42:53 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return this->initWithString("", "Helvetica", 12);
|
2012-03-16 13:42:53 +08:00
|
|
|
}
|
2010-08-11 18:09:10 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool LabelTTF::initWithString(const char *label, const char *fontName, float fontSize,
|
2013-07-27 07:04:21 +08:00
|
|
|
const Size& dimensions, TextHAlignment alignment)
|
2012-03-16 13:42:53 +08:00
|
|
|
{
|
2013-07-27 07:04:21 +08:00
|
|
|
return this->initWithString(label, fontName, fontSize, dimensions, alignment, TextVAlignment::TOP);
|
2012-06-11 14:36:25 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool LabelTTF::initWithString(const char *label, const char *fontName, float fontSize)
|
2012-06-11 14:36:25 +08:00
|
|
|
{
|
2012-08-01 13:24:23 +08:00
|
|
|
return this->initWithString(label, fontName, fontSize,
|
2013-07-27 07:04:21 +08:00
|
|
|
Size::ZERO, TextHAlignment::LEFT, TextVAlignment::TOP);
|
2012-06-11 14:36:25 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool LabelTTF::initWithString(const char *string, const char *fontName, float fontSize,
|
2013-07-27 07:04:21 +08:00
|
|
|
const cocos2d::Size &dimensions, TextHAlignment hAlignment,
|
|
|
|
TextVAlignment vAlignment)
|
2012-06-11 14:36:25 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
if (Sprite::init())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// shader program
|
2013-09-16 20:38:03 +08:00
|
|
|
this->setShaderProgram(ShaderCache::getInstance()->getProgram(SHADER_PROGRAM));
|
2012-06-11 14:36:25 +08:00
|
|
|
|
2013-07-12 14:30:26 +08:00
|
|
|
_dimensions = Size(dimensions.width, dimensions.height);
|
2013-06-15 14:03:30 +08:00
|
|
|
_alignment = hAlignment;
|
|
|
|
_vAlignment = vAlignment;
|
|
|
|
_fontName = new std::string(fontName);
|
|
|
|
_fontSize = fontSize;
|
2012-06-11 14:36:25 +08:00
|
|
|
|
|
|
|
this->setString(string);
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
2012-06-11 14:36:25 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return false;
|
2012-03-16 13:42:53 +08:00
|
|
|
}
|
2012-06-11 14:36:25 +08:00
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
bool LabelTTF::initWithStringAndTextDefinition(const char *string, FontDefinition &textDefinition)
|
2013-05-04 01:06:08 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
if (Sprite::init())
|
2013-05-04 01:06:08 +08:00
|
|
|
{
|
|
|
|
// shader program
|
2013-09-16 20:38:03 +08:00
|
|
|
this->setShaderProgram(ShaderCache::getInstance()->getProgram(SHADER_PROGRAM));
|
2013-05-04 01:06:08 +08:00
|
|
|
|
|
|
|
// prepare everythin needed to render the label
|
2013-05-08 07:59:45 +08:00
|
|
|
_updateWithTextDefinition(textDefinition, false);
|
2013-05-04 01:06:08 +08:00
|
|
|
|
|
|
|
// set the string
|
|
|
|
this->setString(string);
|
|
|
|
|
|
|
|
//
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void LabelTTF::setString(const char *string)
|
2012-03-16 13:42:53 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(string != NULL, "Invalid string");
|
2012-06-11 14:36:25 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_string.compare(string))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_string = string;
|
2012-06-11 14:36:25 +08:00
|
|
|
|
|
|
|
this->updateTexture();
|
|
|
|
}
|
|
|
|
}
|
2012-03-16 13:42:53 +08:00
|
|
|
|
2013-07-04 08:22:15 +08:00
|
|
|
const char* LabelTTF::getString(void) const
|
2012-06-11 14:36:25 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _string.c_str();
|
2012-06-11 14:36:25 +08:00
|
|
|
}
|
2012-03-16 13:42:53 +08:00
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
const char* LabelTTF::description() const
|
2012-06-11 14:36:25 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
return String::createWithFormat("<LabelTTF | FontName = %s, FontSize = %.1f>", _fontName->c_str(), _fontSize)->getCString();
|
2012-06-11 14:36:25 +08:00
|
|
|
}
|
2012-03-16 13:42:53 +08:00
|
|
|
|
2013-07-27 07:04:21 +08:00
|
|
|
TextHAlignment LabelTTF::getHorizontalAlignment() const
|
2012-06-11 14:36:25 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _alignment;
|
2012-03-16 13:42:53 +08:00
|
|
|
}
|
2012-06-11 14:36:25 +08:00
|
|
|
|
2013-07-27 07:04:21 +08:00
|
|
|
void LabelTTF::setHorizontalAlignment(TextHAlignment alignment)
|
2012-03-16 13:42:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (alignment != _alignment)
|
2012-03-16 13:42:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_alignment = alignment;
|
2012-06-11 14:36:25 +08:00
|
|
|
|
|
|
|
// Force update
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_string.size() > 0)
|
2012-06-11 14:36:25 +08:00
|
|
|
{
|
|
|
|
this->updateTexture();
|
|
|
|
}
|
2012-03-16 13:42:53 +08:00
|
|
|
}
|
2012-06-11 14:36:25 +08:00
|
|
|
}
|
|
|
|
|
2013-07-27 07:04:21 +08:00
|
|
|
TextVAlignment LabelTTF::getVerticalAlignment() const
|
2012-06-11 14:36:25 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _vAlignment;
|
2012-06-11 14:36:25 +08:00
|
|
|
}
|
|
|
|
|
2013-07-27 07:04:21 +08:00
|
|
|
void LabelTTF::setVerticalAlignment(TextVAlignment verticalAlignment)
|
2012-06-11 14:36:25 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (verticalAlignment != _vAlignment)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_vAlignment = verticalAlignment;
|
2012-06-11 14:36:25 +08:00
|
|
|
|
|
|
|
// Force update
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_string.size() > 0)
|
2012-06-11 14:36:25 +08:00
|
|
|
{
|
|
|
|
this->updateTexture();
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-06-11 14:36:25 +08:00
|
|
|
}
|
|
|
|
|
2013-07-04 08:44:41 +08:00
|
|
|
const Size& LabelTTF::getDimensions() const
|
2012-06-11 14:36:25 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _dimensions;
|
2012-06-11 14:36:25 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void LabelTTF::setDimensions(const Size &dim)
|
2012-06-11 14:36:25 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (dim.width != _dimensions.width || dim.height != _dimensions.height)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_dimensions = dim;
|
2012-06-11 14:36:25 +08:00
|
|
|
|
2012-09-17 15:02:24 +08:00
|
|
|
// Force update
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_string.size() > 0)
|
2012-06-11 14:36:25 +08:00
|
|
|
{
|
|
|
|
this->updateTexture();
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-06-11 14:36:25 +08:00
|
|
|
}
|
2010-12-28 15:05:55 +08:00
|
|
|
|
2013-07-04 08:44:41 +08:00
|
|
|
float LabelTTF::getFontSize() const
|
2012-06-11 14:36:25 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _fontSize;
|
2012-03-16 13:42:53 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void LabelTTF::setFontSize(float fontSize)
|
2012-03-16 13:42:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_fontSize != fontSize)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_fontSize = fontSize;
|
2012-06-12 09:43:27 +08:00
|
|
|
|
2012-06-11 14:36:25 +08:00
|
|
|
// Force update
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_string.size() > 0)
|
2012-06-11 14:36:25 +08:00
|
|
|
{
|
|
|
|
this->updateTexture();
|
|
|
|
}
|
|
|
|
}
|
2012-03-16 13:42:53 +08:00
|
|
|
}
|
|
|
|
|
2013-07-04 08:44:41 +08:00
|
|
|
const char* LabelTTF::getFontName() const
|
2012-03-16 13:42:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _fontName->c_str();
|
2012-06-11 14:36:25 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void LabelTTF::setFontName(const char *fontName)
|
2012-06-11 14:36:25 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_fontName->compare(fontName))
|
2012-06-11 14:36:25 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
delete _fontName;
|
|
|
|
_fontName = new std::string(fontName);
|
2012-06-11 14:36:25 +08:00
|
|
|
|
|
|
|
// Force update
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_string.size() > 0)
|
2012-06-11 14:36:25 +08:00
|
|
|
{
|
|
|
|
this->updateTexture();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper
|
2013-06-20 14:13:12 +08:00
|
|
|
bool LabelTTF::updateTexture()
|
2012-06-11 14:36:25 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Texture2D *tex;
|
|
|
|
tex = new Texture2D();
|
2013-05-04 01:06:08 +08:00
|
|
|
|
|
|
|
if (!tex)
|
|
|
|
return false;
|
|
|
|
|
2013-05-02 08:11:53 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
|
2012-08-16 11:43:44 +08:00
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
FontDefinition texDef = _prepareTextDefinition(true);
|
2013-07-08 15:17:37 +08:00
|
|
|
tex->initWithString( _string.c_str(), texDef );
|
2013-04-27 07:34:10 +08:00
|
|
|
|
2013-05-02 08:11:53 +08:00
|
|
|
#else
|
2013-04-27 07:34:10 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
tex->initWithString( _string.c_str(),
|
|
|
|
_fontName->c_str(),
|
|
|
|
_fontSize * CC_CONTENT_SCALE_FACTOR(),
|
|
|
|
CC_SIZE_POINTS_TO_PIXELS(_dimensions),
|
|
|
|
_alignment,
|
|
|
|
_vAlignment);
|
2013-05-02 08:11:53 +08:00
|
|
|
|
|
|
|
#endif
|
2013-05-04 01:06:08 +08:00
|
|
|
|
|
|
|
// set the texture
|
|
|
|
this->setTexture(tex);
|
|
|
|
// release it
|
|
|
|
tex->release();
|
|
|
|
|
|
|
|
// set the size in the sprite
|
2013-07-12 14:47:36 +08:00
|
|
|
Rect rect =Rect::ZERO;
|
2013-06-15 14:03:30 +08:00
|
|
|
rect.size = _texture->getContentSize();
|
2013-05-04 01:06:08 +08:00
|
|
|
this->setTextureRect(rect);
|
|
|
|
|
|
|
|
//ok
|
|
|
|
return true;
|
2012-03-16 13:42:53 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void LabelTTF::enableShadow(const Size &shadowOffset, float shadowOpacity, float shadowBlur, bool updateTexture)
|
2013-04-26 09:22:26 +08:00
|
|
|
{
|
2013-05-02 08:11:53 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
|
2013-04-26 09:22:26 +08:00
|
|
|
|
2013-05-02 08:11:53 +08:00
|
|
|
bool valueChanged = false;
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (false == _shadowEnabled)
|
2013-05-02 08:11:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_shadowEnabled = true;
|
2013-05-02 08:11:53 +08:00
|
|
|
valueChanged = true;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if ( (_shadowOffset.width != shadowOffset.width) || (_shadowOffset.height!=shadowOffset.height) )
|
2013-05-02 08:11:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_shadowOffset.width = shadowOffset.width;
|
|
|
|
_shadowOffset.height = shadowOffset.height;
|
2013-05-11 05:15:02 +08:00
|
|
|
|
2013-05-02 08:11:53 +08:00
|
|
|
valueChanged = true;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_shadowOpacity != shadowOpacity )
|
2013-05-02 08:11:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_shadowOpacity = shadowOpacity;
|
2013-05-02 08:11:53 +08:00
|
|
|
valueChanged = true;
|
|
|
|
}
|
2013-04-26 09:22:26 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_shadowBlur != shadowBlur)
|
2013-05-02 08:11:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_shadowBlur = shadowBlur;
|
2013-05-02 08:11:53 +08:00
|
|
|
valueChanged = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-04 01:06:08 +08:00
|
|
|
if ( valueChanged && updateTexture )
|
2013-05-02 08:11:53 +08:00
|
|
|
{
|
|
|
|
this->updateTexture();
|
|
|
|
}
|
2013-04-26 09:22:26 +08:00
|
|
|
|
2013-05-02 08:11:53 +08:00
|
|
|
#else
|
2013-06-06 14:19:16 +08:00
|
|
|
CCLOGERROR("Currently only supported on iOS and Android!");
|
2013-05-02 08:11:53 +08:00
|
|
|
#endif
|
2013-04-26 09:22:26 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void LabelTTF::disableShadow(bool updateTexture)
|
2013-04-26 09:22:26 +08:00
|
|
|
{
|
2013-05-02 08:11:53 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_shadowEnabled)
|
2013-05-02 08:11:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_shadowEnabled = false;
|
2013-05-04 01:06:08 +08:00
|
|
|
|
|
|
|
if (updateTexture)
|
|
|
|
this->updateTexture();
|
|
|
|
|
2013-05-02 08:11:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
2013-06-06 14:19:16 +08:00
|
|
|
CCLOGERROR("Currently only supported on iOS and Android!");
|
2013-05-02 08:11:53 +08:00
|
|
|
#endif
|
2013-04-26 09:22:26 +08:00
|
|
|
}
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
void LabelTTF::enableStroke(const Color3B &strokeColor, float strokeSize, bool updateTexture)
|
2013-04-26 09:22:26 +08:00
|
|
|
{
|
2013-05-02 08:11:53 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
|
2013-04-26 09:22:26 +08:00
|
|
|
|
2013-05-02 08:11:53 +08:00
|
|
|
bool valueChanged = false;
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if(_strokeEnabled == false)
|
2013-05-02 08:11:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_strokeEnabled = true;
|
2013-05-02 08:11:53 +08:00
|
|
|
valueChanged = true;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if ( (_strokeColor.r != strokeColor.r) || (_strokeColor.g != strokeColor.g) || (_strokeColor.b != strokeColor.b) )
|
2013-05-02 08:11:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_strokeColor = strokeColor;
|
2013-05-02 08:11:53 +08:00
|
|
|
valueChanged = true;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_strokeSize!=strokeSize)
|
2013-05-02 08:11:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_strokeSize = strokeSize;
|
2013-05-02 08:11:53 +08:00
|
|
|
valueChanged = true;
|
|
|
|
}
|
|
|
|
|
2013-05-04 01:06:08 +08:00
|
|
|
if ( valueChanged && updateTexture )
|
2013-05-02 08:11:53 +08:00
|
|
|
{
|
|
|
|
this->updateTexture();
|
|
|
|
}
|
2013-04-26 09:22:26 +08:00
|
|
|
|
2013-05-02 08:11:53 +08:00
|
|
|
#else
|
2013-06-06 14:19:16 +08:00
|
|
|
CCLOGERROR("Currently only supported on iOS and Android!");
|
2013-05-02 08:11:53 +08:00
|
|
|
#endif
|
2013-04-26 09:22:26 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void LabelTTF::disableStroke(bool updateTexture)
|
2013-04-26 09:22:26 +08:00
|
|
|
{
|
2013-05-02 08:11:53 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_strokeEnabled)
|
2013-05-02 08:11:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_strokeEnabled = false;
|
2013-05-04 01:06:08 +08:00
|
|
|
|
|
|
|
if (updateTexture)
|
|
|
|
this->updateTexture();
|
2013-05-02 08:11:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
2013-06-06 14:19:16 +08:00
|
|
|
CCLOGERROR("Currently only supported on iOS and Android!");
|
2013-05-02 08:11:53 +08:00
|
|
|
#endif
|
|
|
|
|
2013-04-26 09:22:26 +08:00
|
|
|
}
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
void LabelTTF::setFontFillColor(const Color3B &tintColor, bool updateTexture)
|
2013-05-01 07:36:14 +08:00
|
|
|
{
|
2013-05-02 08:11:53 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_textFillColor.r != tintColor.r || _textFillColor.g != tintColor.g || _textFillColor.b != tintColor.b)
|
2013-05-02 08:11:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_textFillColor = tintColor;
|
2013-05-04 01:06:08 +08:00
|
|
|
|
|
|
|
if (updateTexture)
|
|
|
|
this->updateTexture();
|
2013-05-02 08:11:53 +08:00
|
|
|
}
|
|
|
|
#else
|
2013-06-06 14:19:16 +08:00
|
|
|
CCLOGERROR("Currently only supported on iOS and Android!");
|
2013-05-02 08:11:53 +08:00
|
|
|
#endif
|
2013-05-01 07:36:14 +08:00
|
|
|
}
|
|
|
|
|
2013-07-08 15:17:37 +08:00
|
|
|
void LabelTTF::setTextDefinition(const FontDefinition& theDefinition)
|
2013-05-04 01:06:08 +08:00
|
|
|
{
|
2013-07-08 15:17:37 +08:00
|
|
|
_updateWithTextDefinition(theDefinition, true);
|
2013-05-04 01:06:08 +08:00
|
|
|
}
|
|
|
|
|
2013-07-08 15:17:37 +08:00
|
|
|
FontDefinition LabelTTF::getTextDefinition()
|
2013-05-04 01:06:08 +08:00
|
|
|
{
|
2013-07-08 15:17:37 +08:00
|
|
|
return _prepareTextDefinition(false);
|
2013-05-04 01:06:08 +08:00
|
|
|
}
|
|
|
|
|
2013-07-08 15:17:37 +08:00
|
|
|
void LabelTTF::_updateWithTextDefinition(const FontDefinition& textDefinition, bool mustUpdateTexture)
|
2013-05-04 01:06:08 +08:00
|
|
|
{
|
2013-07-12 14:30:26 +08:00
|
|
|
_dimensions = Size(textDefinition._dimensions.width, textDefinition._dimensions.height);
|
2013-06-15 14:03:30 +08:00
|
|
|
_alignment = textDefinition._alignment;
|
|
|
|
_vAlignment = textDefinition._vertAlignment;
|
2013-05-04 01:06:08 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_fontName = new std::string(textDefinition._fontName);
|
|
|
|
_fontSize = textDefinition._fontSize;
|
2013-05-04 01:06:08 +08:00
|
|
|
|
|
|
|
|
|
|
|
// shadow
|
2013-06-15 14:03:30 +08:00
|
|
|
if ( textDefinition._shadow._shadowEnabled )
|
2013-05-04 01:06:08 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
enableShadow(textDefinition._shadow._shadowOffset, textDefinition._shadow._shadowOpacity, textDefinition._shadow._shadowBlur, false);
|
2013-05-04 01:06:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// stroke
|
2013-06-15 14:03:30 +08:00
|
|
|
if ( textDefinition._stroke._strokeEnabled )
|
2013-05-04 01:06:08 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
enableStroke(textDefinition._stroke._strokeColor, textDefinition._stroke._strokeSize, false);
|
2013-05-04 01:06:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// fill color
|
2013-06-15 14:03:30 +08:00
|
|
|
setFontFillColor(textDefinition._fontFillColor, false);
|
2013-05-04 01:06:08 +08:00
|
|
|
|
|
|
|
if (mustUpdateTexture)
|
|
|
|
updateTexture();
|
|
|
|
}
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
FontDefinition LabelTTF::_prepareTextDefinition(bool adjustForResolution)
|
2013-05-04 01:06:08 +08:00
|
|
|
{
|
2013-07-05 16:49:22 +08:00
|
|
|
FontDefinition texDef;
|
2013-05-04 01:06:08 +08:00
|
|
|
|
2013-05-11 05:15:02 +08:00
|
|
|
if (adjustForResolution)
|
2013-06-15 14:03:30 +08:00
|
|
|
texDef._fontSize = _fontSize * CC_CONTENT_SCALE_FACTOR();
|
2013-05-11 05:15:02 +08:00
|
|
|
else
|
2013-06-15 14:03:30 +08:00
|
|
|
texDef._fontSize = _fontSize;
|
2013-05-11 05:15:02 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
texDef._fontName = *_fontName;
|
|
|
|
texDef._alignment = _alignment;
|
|
|
|
texDef._vertAlignment = _vAlignment;
|
2013-05-04 01:06:08 +08:00
|
|
|
|
2013-05-11 05:15:02 +08:00
|
|
|
|
|
|
|
if (adjustForResolution)
|
2013-06-15 14:03:30 +08:00
|
|
|
texDef._dimensions = CC_SIZE_POINTS_TO_PIXELS(_dimensions);
|
2013-05-11 05:15:02 +08:00
|
|
|
else
|
2013-06-15 14:03:30 +08:00
|
|
|
texDef._dimensions = _dimensions;
|
2013-05-11 05:15:02 +08:00
|
|
|
|
|
|
|
|
|
|
|
// stroke
|
2013-06-15 14:03:30 +08:00
|
|
|
if ( _strokeEnabled )
|
2013-05-04 01:06:08 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
texDef._stroke._strokeEnabled = true;
|
|
|
|
texDef._stroke._strokeColor = _strokeColor;
|
2013-05-11 05:15:02 +08:00
|
|
|
|
|
|
|
if (adjustForResolution)
|
2013-06-15 14:03:30 +08:00
|
|
|
texDef._stroke._strokeSize = _strokeSize * CC_CONTENT_SCALE_FACTOR();
|
2013-05-11 05:15:02 +08:00
|
|
|
else
|
2013-06-15 14:03:30 +08:00
|
|
|
texDef._stroke._strokeSize = _strokeSize;
|
2013-05-11 05:15:02 +08:00
|
|
|
|
|
|
|
|
2013-05-04 01:06:08 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
texDef._stroke._strokeEnabled = false;
|
2013-05-04 01:06:08 +08:00
|
|
|
}
|
|
|
|
|
2013-05-11 05:15:02 +08:00
|
|
|
|
|
|
|
// shadow
|
2013-06-15 14:03:30 +08:00
|
|
|
if ( _shadowEnabled )
|
2013-05-04 01:06:08 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
texDef._shadow._shadowEnabled = true;
|
|
|
|
texDef._shadow._shadowBlur = _shadowBlur;
|
|
|
|
texDef._shadow._shadowOpacity = _shadowOpacity;
|
2013-05-11 05:15:02 +08:00
|
|
|
|
|
|
|
if (adjustForResolution)
|
2013-06-15 14:03:30 +08:00
|
|
|
texDef._shadow._shadowOffset = CC_SIZE_POINTS_TO_PIXELS(_shadowOffset);
|
2013-05-11 05:15:02 +08:00
|
|
|
else
|
2013-06-15 14:03:30 +08:00
|
|
|
texDef._shadow._shadowOffset = _shadowOffset;
|
2013-05-04 01:06:08 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
texDef._shadow._shadowEnabled = false;
|
2013-05-04 01:06:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// text tint
|
2013-06-15 14:03:30 +08:00
|
|
|
texDef._fontFillColor = _textFillColor;
|
2013-05-04 01:06:08 +08:00
|
|
|
|
|
|
|
return texDef;
|
|
|
|
}
|
2013-05-01 07:36:14 +08:00
|
|
|
|
2012-03-16 13:42:53 +08:00
|
|
|
NS_CC_END
|