2013-07-23 02:17:51 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013 Zynga Inc.
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
|
|
|
|
2013-07-23 02:17:51 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
2013-07-27 07:04:21 +08:00
|
|
|
#ifndef _COCOS2D_CCLABEL_H_
|
|
|
|
#define _COCOS2D_CCLABEL_H_
|
2013-07-23 02:17:51 +08:00
|
|
|
|
2013-10-14 14:01:00 +08:00
|
|
|
#include "CCSpriteBatchNode.h"
|
2013-08-08 07:36:04 +08:00
|
|
|
#include "ccTypes.h"
|
2014-01-04 10:56:14 +08:00
|
|
|
#include "renderer/CCCustomCommand.h"
|
2014-01-22 14:57:11 +08:00
|
|
|
#include "CCFontAtlas.h"
|
2013-07-23 02:17:51 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
enum class GlyphCollection {
|
2013-07-27 07:04:21 +08:00
|
|
|
|
2013-07-23 02:17:51 +08:00
|
|
|
DYNAMIC,
|
|
|
|
NEHE,
|
|
|
|
ASCII,
|
|
|
|
CUSTOM
|
|
|
|
};
|
|
|
|
|
2013-12-13 12:42:15 +08:00
|
|
|
enum class LabelEffect {
|
|
|
|
|
|
|
|
NORMAL,
|
|
|
|
OUTLINE,
|
|
|
|
SHADOW,
|
|
|
|
GLOW
|
|
|
|
};
|
|
|
|
|
2014-01-11 22:33:07 +08:00
|
|
|
typedef struct _ttfConfig
|
|
|
|
{
|
|
|
|
std::string fontFilePath;
|
|
|
|
int fontSize;
|
|
|
|
GlyphCollection glyphs;
|
|
|
|
const char *customGlyphs;
|
2014-01-15 17:21:08 +08:00
|
|
|
bool distanceFieldEnabled;
|
2014-03-05 15:54:40 +08:00
|
|
|
int outlineSize;
|
2014-01-11 22:33:07 +08:00
|
|
|
|
2014-03-05 15:54:40 +08:00
|
|
|
_ttfConfig(const char* filePath = "",int size = 36, const GlyphCollection& glyphCollection = GlyphCollection::NEHE,
|
|
|
|
const char *customGlyphCollection = nullptr,bool useDistanceField = false,int outline = 0)
|
2014-01-11 22:33:07 +08:00
|
|
|
:fontFilePath(filePath)
|
2014-01-16 16:37:29 +08:00
|
|
|
,fontSize(size)
|
|
|
|
,glyphs(glyphCollection)
|
|
|
|
,customGlyphs(customGlyphCollection)
|
2014-01-15 17:21:08 +08:00
|
|
|
,distanceFieldEnabled(useDistanceField)
|
2014-03-05 15:54:40 +08:00
|
|
|
,outlineSize(outline)
|
|
|
|
{
|
|
|
|
if(outline > 0)
|
|
|
|
{
|
|
|
|
distanceFieldEnabled = false;
|
|
|
|
}
|
|
|
|
}
|
2014-01-11 22:33:07 +08:00
|
|
|
}TTFConfig;
|
2013-08-08 07:36:04 +08:00
|
|
|
|
2014-01-23 13:42:27 +08:00
|
|
|
class CC_DLL Label : public SpriteBatchNode, public LabelProtocol
|
2013-07-23 02:17:51 +08:00
|
|
|
{
|
|
|
|
public:
|
2014-03-05 15:54:40 +08:00
|
|
|
static const int DefultFontSize;
|
|
|
|
|
2014-01-11 22:33:07 +08:00
|
|
|
static Label* create();
|
|
|
|
|
2014-03-07 14:58:44 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE static Label* createWithTTF(const std::string& label, const std::string& fontFilePath,
|
|
|
|
int fontSize, int lineSize = 0, TextHAlignment alignment = TextHAlignment::LEFT,
|
|
|
|
GlyphCollection glyphs = GlyphCollection::NEHE, const char *customGlyphs = 0, bool useDistanceField = false);
|
|
|
|
|
|
|
|
/** create a label with TTF configuration
|
|
|
|
* It will generate texture of character by freetype.
|
|
|
|
*/
|
2014-01-23 13:42:27 +08:00
|
|
|
static Label* createWithTTF(const TTFConfig& ttfConfig, const std::string& text, TextHAlignment alignment = TextHAlignment::LEFT, int lineWidth = 0);
|
2013-07-23 05:43:45 +08:00
|
|
|
|
2014-03-07 14:58:44 +08:00
|
|
|
static Label* createWithBMFont(const std::string& bmfontFilePath, const std::string& text,
|
|
|
|
const TextHAlignment& alignment = TextHAlignment::LEFT, int lineWidth = 0,
|
|
|
|
const Point& imageOffset = Point::ZERO);
|
2013-07-23 05:43:45 +08:00
|
|
|
|
2014-01-16 16:37:29 +08:00
|
|
|
static Label * createWithCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap);
|
|
|
|
static Label * createWithCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap);
|
|
|
|
static Label * createWithCharMap(const std::string& plistFile);
|
|
|
|
|
2014-03-07 14:58:44 +08:00
|
|
|
/** set TTF configuration for Label */
|
2014-03-05 15:54:40 +08:00
|
|
|
virtual bool setTTFConfig(const TTFConfig& ttfConfig);
|
2013-12-13 12:42:15 +08:00
|
|
|
|
2014-03-05 15:54:40 +08:00
|
|
|
virtual bool setBMFontFilePath(const std::string& bmfontFilePath, const Point& imageOffset = Point::ZERO);
|
2014-01-11 22:33:07 +08:00
|
|
|
|
2014-03-05 15:54:40 +08:00
|
|
|
virtual bool setCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap);
|
|
|
|
virtual bool setCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap);
|
|
|
|
virtual bool setCharMap(const std::string& plistFile);
|
2014-01-16 16:37:29 +08:00
|
|
|
|
2014-03-07 14:58:44 +08:00
|
|
|
/** changes the string to render
|
|
|
|
*
|
|
|
|
*/
|
2014-01-23 13:42:27 +08:00
|
|
|
virtual void setString(const std::string& text) override;
|
2014-01-11 22:33:07 +08:00
|
|
|
|
2014-03-07 14:58:44 +08:00
|
|
|
virtual const std::string& getString() const override { return _originalUTF8String; }
|
|
|
|
|
|
|
|
|
2014-03-05 15:54:40 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE void setLabelEffect(LabelEffect effect,const Color3B& effectColor);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable shadow for the label
|
|
|
|
*
|
|
|
|
* @todo support blur for shadow effect
|
|
|
|
*/
|
|
|
|
virtual void enableShadow(const Color3B& shadowColor = Color3B::BLACK,const Size &offset = Size(2,-2), float opacity = 0.75f, int blurRadius = 0);
|
|
|
|
|
|
|
|
/** only support for TTF */
|
|
|
|
virtual void enableOutline(const Color4B& outlineColor,int outlineSize = -1);
|
|
|
|
|
|
|
|
/** only support for TTF */
|
|
|
|
virtual void enableGlow(const Color3B& glowColor);
|
|
|
|
|
|
|
|
/** disable shadow/outline/glow rendering */
|
|
|
|
virtual void disableEffect();
|
2013-08-08 07:36:04 +08:00
|
|
|
|
2014-03-07 14:58:44 +08:00
|
|
|
|
2014-03-07 15:58:49 +08:00
|
|
|
virtual void setAlignment(TextHAlignment hAlignment,bool aligntext = true);
|
2014-03-07 14:58:44 +08:00
|
|
|
TextHAlignment getTextAlignment() const { return _hAlignment;}
|
|
|
|
|
2014-03-07 15:58:49 +08:00
|
|
|
virtual void setAlignment(TextHAlignment hAlignment,TextVAlignment vAlignment,bool aligntext = true);
|
2014-03-07 14:58:44 +08:00
|
|
|
|
2014-03-07 15:58:49 +08:00
|
|
|
virtual void setHorizontalAlignment(TextHAlignment alignment,bool aligntext = true);
|
2014-03-07 14:58:44 +08:00
|
|
|
TextHAlignment getHorizontalAlignment() const { return _hAlignment; }
|
2014-03-07 15:58:49 +08:00
|
|
|
|
|
|
|
virtual void setVerticalAlignment(TextVAlignment verticalAlignment,bool aligntext = true);
|
2014-03-07 14:58:44 +08:00
|
|
|
TextVAlignment getVerticalAlignment() const { return _vAlignment; }
|
|
|
|
|
2013-08-08 07:36:04 +08:00
|
|
|
virtual void setLineBreakWithoutSpace(bool breakWithoutSpace);
|
2013-08-13 07:16:34 +08:00
|
|
|
|
2014-03-07 14:58:44 +08:00
|
|
|
/** Sets the max line width of the label.
|
|
|
|
* The label's max line width be used for force line breaks if the set value not equal zero.
|
|
|
|
* The label's width and max line width has not always to be equal.
|
|
|
|
*/
|
|
|
|
virtual void setMaxLineWidth(unsigned int maxLineWidth);
|
|
|
|
unsigned int getMaxLineWidth() { return _maxLineWidth;}
|
|
|
|
|
|
|
|
/** Sets the untransformed size of the label.
|
|
|
|
* The label's width be used for text align if the set value not equal zero.
|
|
|
|
* The label's max line width will be equal to the same value.
|
|
|
|
*/
|
|
|
|
virtual void setWidth(unsigned int width);
|
|
|
|
unsigned int getWidth() const { return _labelWidth; }
|
|
|
|
|
|
|
|
/** Sets the untransformed size of the label.
|
|
|
|
* The label's height be used for text align if the set value not equal zero.
|
|
|
|
* The text will display of incomplete when the size of label not enough to support display all text.
|
|
|
|
*/
|
|
|
|
virtual void setHeight(unsigned int height);
|
|
|
|
unsigned int getHeight() const { return _labelHeight;}
|
|
|
|
|
|
|
|
/** Sets the untransformed size of the label in a more efficient way. */
|
|
|
|
virtual void setDimensions(unsigned int width,unsigned int height);
|
|
|
|
|
2013-09-13 11:46:46 +08:00
|
|
|
virtual bool isOpacityModifyRGB() const override;
|
|
|
|
virtual void setOpacityModifyRGB(bool isOpacityModifyRGB) override;
|
2013-12-06 18:07:16 +08:00
|
|
|
virtual void setColor(const Color3B& color) override;
|
2013-08-14 02:28:54 +08:00
|
|
|
|
2014-02-19 20:26:14 +08:00
|
|
|
virtual Sprite * getLetter(int lettetIndex);
|
|
|
|
|
2013-08-08 07:36:04 +08:00
|
|
|
// font related stuff
|
2014-03-07 14:58:44 +08:00
|
|
|
int getCommonLineHeight() const;
|
2013-08-08 07:36:04 +08:00
|
|
|
|
|
|
|
// string related stuff
|
2014-03-05 15:54:40 +08:00
|
|
|
int getStringNumLines() const;
|
2014-03-07 14:58:44 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE int getStringLenght() const { return getStringLength(); }
|
|
|
|
int getStringLength() const;
|
2013-07-26 08:58:13 +08:00
|
|
|
|
2014-03-01 08:10:48 +08:00
|
|
|
virtual void visit(Renderer *renderer, const kmMat4 &parentTransform, bool parentTransformUpdated) override;
|
|
|
|
virtual void draw(Renderer *renderer, const kmMat4 &transform, bool transformUpdated) override;
|
2013-12-13 06:30:22 +08:00
|
|
|
|
2014-03-07 14:58:44 +08:00
|
|
|
virtual void setScale(float scale) override;
|
|
|
|
virtual void setScaleX(float scaleX) override;
|
|
|
|
virtual void setScaleY(float scaleY) override;
|
|
|
|
virtual float getScaleX() const override;
|
|
|
|
virtual float getScaleY() const override;
|
|
|
|
|
|
|
|
virtual void addChild(Node * child, int zOrder=0, int tag=0) override;
|
|
|
|
virtual std::string getDescription() const override;
|
2014-01-22 14:57:11 +08:00
|
|
|
|
2014-03-05 15:54:40 +08:00
|
|
|
protected:
|
2014-03-06 07:49:08 +08:00
|
|
|
void onDraw(const kmMat4& transform, bool transformUpdated);
|
|
|
|
|
2014-02-19 20:26:14 +08:00
|
|
|
struct LetterInfo
|
|
|
|
{
|
|
|
|
FontLetterDefinition def;
|
|
|
|
|
|
|
|
Point position;
|
|
|
|
Size contentSize;
|
|
|
|
};
|
2013-09-13 16:46:31 +08:00
|
|
|
/**
|
2014-02-19 20:26:14 +08:00
|
|
|
* @js NA
|
|
|
|
*/
|
2014-01-23 13:42:27 +08:00
|
|
|
Label(FontAtlas *atlas = nullptr, TextHAlignment alignment = TextHAlignment::LEFT, bool useDistanceField = false,bool useA8Shader = false);
|
2013-09-13 13:52:42 +08:00
|
|
|
/**
|
2014-02-19 20:26:14 +08:00
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2014-03-05 15:54:40 +08:00
|
|
|
virtual ~Label();
|
2014-02-19 20:26:14 +08:00
|
|
|
|
2014-03-05 15:54:40 +08:00
|
|
|
virtual bool initWithFontAtlas(FontAtlas* atlas,bool distanceFieldEnabled = false, bool useA8Shader = false);
|
2014-02-19 20:26:14 +08:00
|
|
|
|
2014-03-05 15:54:40 +08:00
|
|
|
bool recordLetterInfo(const cocos2d::Point& point,const FontLetterDefinition& letterDef, int spriteIndex);
|
|
|
|
bool recordPlaceholderInfo(int spriteIndex);
|
2014-02-19 20:26:14 +08:00
|
|
|
|
|
|
|
void setFontScale(float fontScale);
|
2014-01-15 17:21:08 +08:00
|
|
|
|
2014-03-05 15:54:40 +08:00
|
|
|
virtual bool init();
|
2013-08-08 07:36:04 +08:00
|
|
|
|
2014-03-05 15:54:40 +08:00
|
|
|
virtual void alignText();
|
2013-08-08 07:36:04 +08:00
|
|
|
|
2014-01-21 17:55:49 +08:00
|
|
|
bool computeHorizontalKernings(unsigned short int *stringToRender);
|
2013-08-08 07:36:04 +08:00
|
|
|
bool setCurrentString(unsigned short *stringToSet);
|
|
|
|
bool setOriginalString(unsigned short *stringToSet);
|
|
|
|
void resetCurrentString();
|
2014-02-10 11:21:54 +08:00
|
|
|
void computeStringNumLines();
|
2013-12-06 18:07:16 +08:00
|
|
|
|
2014-02-19 20:26:14 +08:00
|
|
|
void updateSpriteWithLetterDefinition(const FontLetterDefinition &theDefinition, Texture2D *theTexture);
|
2013-12-11 16:38:47 +08:00
|
|
|
|
|
|
|
virtual void updateColor() override;
|
|
|
|
|
2014-03-05 15:54:40 +08:00
|
|
|
virtual void initProgram();
|
|
|
|
|
|
|
|
void drawShadowWithoutBlur();
|
|
|
|
|
2014-03-07 14:58:44 +08:00
|
|
|
bool _isOpacityModifyRGB;
|
|
|
|
|
|
|
|
std::vector<SpriteBatchNode*> _batchNodes;
|
|
|
|
FontAtlas * _fontAtlas;
|
|
|
|
std::vector<LetterInfo> _lettersInfo;
|
|
|
|
|
|
|
|
TTFConfig _fontConfig;
|
|
|
|
|
2013-10-29 20:25:03 +08:00
|
|
|
//! used for optimization
|
2014-01-17 13:35:58 +08:00
|
|
|
Sprite *_reusedLetter;
|
2014-02-19 20:26:14 +08:00
|
|
|
Rect _reusedRect;
|
2014-03-07 14:58:44 +08:00
|
|
|
int _limitShowCount;
|
2014-01-15 17:21:08 +08:00
|
|
|
|
2014-01-17 13:35:58 +08:00
|
|
|
float _commonLineHeight;
|
2014-02-19 20:26:14 +08:00
|
|
|
bool _lineBreakWithoutSpaces;
|
2014-03-07 14:58:44 +08:00
|
|
|
int * _horizontalKernings;
|
|
|
|
|
|
|
|
unsigned int _maxLineWidth;
|
|
|
|
unsigned int _labelWidth;
|
|
|
|
unsigned int _labelHeight;
|
|
|
|
TextHAlignment _hAlignment;
|
|
|
|
TextVAlignment _vAlignment;
|
|
|
|
|
|
|
|
int _currNumLines;
|
2014-01-17 13:35:58 +08:00
|
|
|
unsigned short int * _currentUTF16String;
|
|
|
|
unsigned short int * _originalUTF16String;
|
2014-01-23 13:42:27 +08:00
|
|
|
std::string _originalUTF8String;
|
2014-03-07 14:58:44 +08:00
|
|
|
|
|
|
|
float _fontScale;
|
2013-12-13 12:42:15 +08:00
|
|
|
|
2014-01-17 13:35:58 +08:00
|
|
|
bool _useDistanceField;
|
|
|
|
bool _useA8Shader;
|
2013-12-13 12:42:15 +08:00
|
|
|
|
2014-01-17 13:35:58 +08:00
|
|
|
LabelEffect _currLabelEffect;
|
|
|
|
Color3B _effectColor;
|
2013-12-13 12:42:15 +08:00
|
|
|
|
2014-01-17 13:35:58 +08:00
|
|
|
GLuint _uniformEffectColor;
|
2014-03-07 14:58:44 +08:00
|
|
|
CustomCommand _customCommand;
|
2014-02-19 20:26:14 +08:00
|
|
|
|
2014-03-05 15:54:40 +08:00
|
|
|
Size _shadowOffset;
|
|
|
|
float _shadowOpacity;
|
|
|
|
int _shadowBlurRadius;
|
2014-03-07 14:58:44 +08:00
|
|
|
kmMat4 _parentTransform;
|
2014-03-05 15:54:40 +08:00
|
|
|
|
|
|
|
Color4B _outlineColor;
|
|
|
|
|
|
|
|
private:
|
|
|
|
CC_DISALLOW_COPY_AND_ASSIGN(Label);
|
|
|
|
|
2014-02-19 20:26:14 +08:00
|
|
|
friend class LabelTextFormatter;
|
2013-07-23 02:17:51 +08:00
|
|
|
};
|
|
|
|
|
2013-08-08 07:36:04 +08:00
|
|
|
|
2013-07-23 02:17:51 +08:00
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
#endif /*__COCOS2D_CCLABEL_H */
|