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 "CCLabelTextFormatProtocol.h"
|
|
|
|
#include "ccTypes.h"
|
2014-01-04 10:56:14 +08:00
|
|
|
#include "renderer/CCCustomCommand.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
|
|
|
|
};
|
|
|
|
|
2013-08-08 07:36:04 +08:00
|
|
|
//fwd
|
2013-08-15 21:38:27 +08:00
|
|
|
struct FontLetterDefinition;
|
2013-08-08 07:36:04 +08:00
|
|
|
class FontAtlas;
|
|
|
|
|
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-01-11 22:33:07 +08:00
|
|
|
|
|
|
|
_ttfConfig(const char* filePath,int fontSize = 36, const GlyphCollection& glyphs = GlyphCollection::NEHE,
|
|
|
|
const char *customGlyphs = nullptr,bool useDistanceField = false)
|
|
|
|
:fontFilePath(filePath)
|
|
|
|
,fontSize(fontSize)
|
|
|
|
,glyphs(glyphs)
|
|
|
|
,customGlyphs(customGlyphs)
|
2014-01-15 17:21:08 +08:00
|
|
|
,distanceFieldEnabled(useDistanceField)
|
2014-01-11 22:33:07 +08:00
|
|
|
{}
|
|
|
|
}TTFConfig;
|
2013-08-08 07:36:04 +08:00
|
|
|
|
2014-01-15 17:21:08 +08:00
|
|
|
class CC_DLL Label : public SpriteBatchNode, public LabelTextFormatProtocol
|
2013-07-23 02:17:51 +08:00
|
|
|
{
|
|
|
|
public:
|
2014-01-11 22:33:07 +08:00
|
|
|
static Label* create();
|
|
|
|
|
2014-01-13 16:32:35 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE static Label* createWithTTF(const std::string& label, const std::string& fontFilePath, int fontSize, int lineSize = 0, TextHAlignment alignment = TextHAlignment::CENTER, GlyphCollection glyphs = GlyphCollection::NEHE, const char *customGlyphs = 0, bool useDistanceField = false);
|
2014-01-11 22:33:07 +08:00
|
|
|
static Label* createWithTTF(const TTFConfig& ttfConfig, const std::string& text, TextHAlignment alignment = TextHAlignment::CENTER, int lineWidth = 0);
|
2013-07-23 05:43:45 +08:00
|
|
|
|
2014-01-11 22:33:07 +08:00
|
|
|
static Label* createWithBMFont(const std::string& bmfontFilePath, const std::string& text,const TextHAlignment& alignment = TextHAlignment::CENTER, int lineWidth = 0);
|
2013-07-23 05:43:45 +08:00
|
|
|
|
2014-01-11 22:33:07 +08:00
|
|
|
bool setTTFConfig(const TTFConfig& ttfConfig);
|
2013-12-13 12:42:15 +08:00
|
|
|
|
2014-01-11 22:33:07 +08:00
|
|
|
bool setBMFontFilePath(const std::string& bmfontFilePath);
|
|
|
|
|
2014-01-15 17:21:08 +08:00
|
|
|
bool setString(const std::string& text, const TextHAlignment& alignment = TextHAlignment::CENTER, float lineWidth = -1, bool lineBreakWithoutSpaces = false);
|
2014-01-11 22:33:07 +08:00
|
|
|
|
|
|
|
//only support for TTF
|
2013-12-13 12:42:15 +08:00
|
|
|
void setLabelEffect(LabelEffect effect,const Color3B& effectColor);
|
2013-08-08 07:36:04 +08:00
|
|
|
|
|
|
|
virtual void setAlignment(TextHAlignment alignment);
|
|
|
|
virtual void setWidth(float width);
|
|
|
|
virtual void setLineBreakWithoutSpace(bool breakWithoutSpace);
|
2013-09-13 11:46:46 +08:00
|
|
|
virtual void setScale(float scale) override;
|
|
|
|
virtual void setScaleX(float scaleX) override;
|
|
|
|
virtual void setScaleY(float scaleY) override;
|
2013-12-13 12:42:15 +08:00
|
|
|
virtual float getScaleX() const;
|
|
|
|
virtual float getScaleY() const;
|
2013-08-13 07:16:34 +08:00
|
|
|
|
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-08 07:36:04 +08:00
|
|
|
|
|
|
|
// CCLabelTextFormat protocol implementation
|
2013-10-31 17:52:22 +08:00
|
|
|
virtual std::vector<LetterInfo> *getLettersInfo() override { return &_lettersInfo; };
|
2013-11-08 04:42:16 +08:00
|
|
|
virtual bool recordLetterInfo(const cocos2d::Point& point,unsigned short int theChar, int spriteIndex) override;
|
|
|
|
virtual bool recordPlaceholderInfo(int spriteIndex) override;
|
|
|
|
virtual float getLetterPosXLeft( int index ) const override;
|
|
|
|
virtual float getLetterPosXRight( int index ) const override;
|
2013-08-14 02:28:54 +08:00
|
|
|
|
2013-11-08 04:42:16 +08:00
|
|
|
virtual Sprite * getLetter(int ID) override;
|
2013-08-08 07:36:04 +08:00
|
|
|
|
|
|
|
// font related stuff
|
2013-11-08 04:42:16 +08:00
|
|
|
virtual int getCommonLineHeight() const override;
|
|
|
|
virtual int getKerningForCharsPair(unsigned short first, unsigned short second) const override;
|
|
|
|
virtual int getXOffsetForChar(unsigned short c) const override;
|
|
|
|
virtual int getYOffsetForChar(unsigned short c) const override;
|
|
|
|
virtual int getAdvanceForChar(unsigned short c, int hintPositionInString) const override;
|
|
|
|
virtual Rect getRectForChar(unsigned short c) const override;
|
2013-08-08 07:36:04 +08:00
|
|
|
|
|
|
|
// string related stuff
|
2013-11-08 04:42:16 +08:00
|
|
|
virtual int getStringNumLines() const override;
|
|
|
|
virtual int getStringLenght() const override;
|
|
|
|
virtual unsigned short getCharAtStringPosition(int position) const override;
|
|
|
|
virtual unsigned short * getUTF8String() const override;
|
|
|
|
virtual void assignNewUTF8String(unsigned short *newString) override;
|
|
|
|
virtual TextHAlignment getTextAlignment() const override;
|
2013-08-08 07:36:04 +08:00
|
|
|
|
|
|
|
// label related stuff
|
2013-11-08 04:42:16 +08:00
|
|
|
virtual float getMaxLineWidth() const override;
|
|
|
|
virtual bool breakLineWithoutSpace() const override;
|
|
|
|
virtual Size getLabelContentSize() const override;
|
|
|
|
virtual void setLabelContentSize(const Size &newSize) override;
|
2013-07-26 08:58:13 +08:00
|
|
|
|
2013-08-13 07:16:34 +08:00
|
|
|
// carloX
|
2014-01-15 17:21:08 +08:00
|
|
|
virtual const std::string& getString() const { static std::string _ret("not implemented"); return _ret; }
|
2013-11-08 04:42:16 +08:00
|
|
|
void addChild(Node * child, int zOrder=0, int tag=0) override;
|
2013-10-29 20:25:03 +08:00
|
|
|
|
2013-12-13 06:30:22 +08:00
|
|
|
virtual std::string getDescription() const override;
|
2013-12-13 12:42:15 +08:00
|
|
|
virtual void draw(void) override;
|
2014-01-04 10:56:14 +08:00
|
|
|
virtual void onDraw();
|
2013-12-13 06:30:22 +08:00
|
|
|
|
2013-07-26 08:58:13 +08:00
|
|
|
private:
|
2013-09-13 16:46:31 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
*/
|
2014-01-11 22:33:07 +08:00
|
|
|
Label(FontAtlas *atlas = nullptr, TextHAlignment alignment = TextHAlignment::CENTER, bool useDistanceField = false,bool useA8Shader = false);
|
2013-09-13 13:52:42 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-08-13 07:16:34 +08:00
|
|
|
~Label();
|
|
|
|
|
2014-01-15 17:21:08 +08:00
|
|
|
bool initWithFontAtlas(FontAtlas* atlas,bool distanceFieldEnabled = false, bool useA8Shader = false);
|
|
|
|
|
2013-12-13 12:42:15 +08:00
|
|
|
void setFontSize(int fontSize);
|
2013-08-08 07:36:04 +08:00
|
|
|
|
|
|
|
bool init();
|
|
|
|
|
2013-10-29 20:25:03 +08:00
|
|
|
void alignText();
|
2013-08-08 07:36:04 +08:00
|
|
|
|
|
|
|
bool computeAdvancesForString(unsigned short int *stringToRender);
|
|
|
|
bool setCurrentString(unsigned short *stringToSet);
|
|
|
|
bool setOriginalString(unsigned short *stringToSet);
|
|
|
|
void resetCurrentString();
|
2013-12-06 18:07:16 +08:00
|
|
|
|
|
|
|
Sprite * updateSpriteWithLetterDefinition(Sprite *spriteToUpdate, const FontLetterDefinition &theDefinition, Texture2D *theTexture);
|
2013-12-11 16:38:47 +08:00
|
|
|
|
|
|
|
virtual void updateColor() override;
|
|
|
|
|
2013-10-29 20:25:03 +08:00
|
|
|
//! used for optimization
|
|
|
|
Sprite *_reusedLetter;
|
2014-01-15 17:21:08 +08:00
|
|
|
std::vector<LetterInfo> _lettersInfo;
|
|
|
|
|
2013-08-08 07:36:04 +08:00
|
|
|
float _commonLineHeight;
|
|
|
|
bool _lineBreakWithoutSpaces;
|
|
|
|
float _width;
|
|
|
|
TextHAlignment _alignment;
|
2013-10-29 20:25:03 +08:00
|
|
|
unsigned short int * _currentUTF16String;
|
|
|
|
unsigned short int * _originalUTF16String;
|
2013-08-08 07:36:04 +08:00
|
|
|
Size * _advances;
|
|
|
|
FontAtlas * _fontAtlas;
|
2013-08-13 04:29:54 +08:00
|
|
|
bool _isOpacityModifyRGB;
|
2013-12-13 12:42:15 +08:00
|
|
|
|
|
|
|
bool _useDistanceField;
|
|
|
|
bool _useA8Shader;
|
|
|
|
int _fontSize;
|
|
|
|
|
|
|
|
LabelEffect _currLabelEffect;
|
|
|
|
Color3B _effectColor;
|
|
|
|
|
|
|
|
GLuint _uniformEffectColor;
|
2014-01-04 10:56:14 +08:00
|
|
|
|
|
|
|
CustomCommand _customCommand;
|
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 */
|