axmol/core/2d/Label.h

932 lines
30 KiB
C
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2013 Zynga Inc.
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
Copyright (c) 2021 Bytedance Inc.
2019-11-23 20:27:39 +08:00
2022-10-01 16:24:52 +08:00
https://axmolengine.github.io/
2019-11-23 20:27:39 +08:00
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.
****************************************************************************/
#ifndef _COCOS2D_CCLABEL_H_
#define _COCOS2D_CCLABEL_H_
#include "2d/Node.h"
#include "renderer/CustomCommand.h"
#include "renderer/QuadCommand.h"
#include "2d/FontAtlas.h"
#include "base/Types.h"
2019-11-23 20:27:39 +08:00
NS_AX_BEGIN
2019-11-23 20:27:39 +08:00
/**
* @addtogroup _2d
* @{
*/
2022-07-16 10:43:05 +08:00
#define AX_DEFAULT_FONT_LABEL_SIZE 12
2019-11-23 20:27:39 +08:00
/**
* @struct TTFConfig
* @see `GlyphCollection`
*/
typedef struct _ttfConfig
{
std::string fontFilePath;
float fontSize;
GlyphCollection glyphs;
2021-11-26 17:53:24 +08:00
std::string customGlyphs;
2019-11-23 20:27:39 +08:00
bool distanceFieldEnabled;
int outlineSize;
bool italics;
bool bold;
bool underline;
bool strikethrough;
2021-12-25 10:04:45 +08:00
_ttfConfig(std::string_view filePath = {},
2022-07-16 10:43:05 +08:00
float size = AX_DEFAULT_FONT_LABEL_SIZE,
const GlyphCollection& glyphCollection = GlyphCollection::DYNAMIC,
const char* customGlyphCollection = nullptr, /* nullable */
bool useDistanceField = false,
int outline = 0,
bool useItalics = false,
bool useBold = false,
bool useUnderline = false,
bool useStrikethrough = false)
2019-11-23 20:27:39 +08:00
: fontFilePath(filePath)
, fontSize(size)
, glyphs(glyphCollection)
, customGlyphs(customGlyphCollection ? customGlyphCollection : "")
2019-11-23 20:27:39 +08:00
, distanceFieldEnabled(useDistanceField)
, outlineSize(outline)
, italics(useItalics)
, bold(useBold)
, underline(useUnderline)
, strikethrough(useStrikethrough)
{
2021-12-25 10:04:45 +08:00
if (outline > 0)
2019-11-23 20:27:39 +08:00
{
distanceFieldEnabled = false;
}
}
} TTFConfig;
class Sprite;
class SpriteBatchNode;
class DrawNode;
class EventListenerCustom;
class TextureAtlas;
/**
* @brief Label is a subclass of Node that knows how to render text labels.
*
* Label can be created with:
* - A true type font file.
* - A bitmap font file.
* - A char map file.
* - The built in system font.
*
* Bitmap Font supported editors:
* - http://glyphdesigner.71squared.com/ (Commercial, Mac OS X)
* - http://www.n4te.com/hiero/hiero.jnlp (Free, Java)
* - http://slick.cokeandcode.com/demos/hiero.jnlp (Free, Java)
* - http://www.angelcode.com/products/bmfont/ (Free, Windows only)
* @js NA
*/
2022-07-16 10:43:05 +08:00
class AX_DLL Label : public Node, public LabelProtocol, public BlendProtocol
2019-11-23 20:27:39 +08:00
{
public:
enum class Overflow
{
2021-12-25 10:04:45 +08:00
// In NONE mode, the dimensions is (0,0) and the content size will change dynamically to fit the label.
2019-11-23 20:27:39 +08:00
NONE,
/**
*In CLAMP mode, when label content goes out of the bounding box, it will be clipped.
*/
CLAMP,
/**
* In SHRINK mode, the font size will change dynamically to adapt the content size.
*/
SHRINK,
/**
*In RESIZE_HEIGHT mode, you can only change the width of label and the height is changed automatically.
*/
RESIZE_HEIGHT
};
2021-12-25 10:04:45 +08:00
enum class LabelType
{
2019-11-23 20:27:39 +08:00
TTF,
BMFONT,
CHARMAP,
STRING_TEXTURE
};
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/// @name Creators
/// @{
/**
2021-12-25 10:04:45 +08:00
* Allocates and initializes a Label, with default settings.
*
* @return An automatically released Label object.
*/
2019-11-23 20:27:39 +08:00
static Label* create();
/**
* Allocates and initializes a Label, base on platform-dependent API.
*
* @param text The initial text.
* @param font A font file or a font family name.
* @param fontSize The font size. This value must be > 0.
* @param dimensions
* @param hAlignment The text horizontal alignment.
* @param vAlignment The text vertical alignment.
*
* @warning It will generate texture by the platform-dependent code.
*
* @return An automatically released Label object.
*/
static Label* createWithSystemFont(std::string_view text,
std::string_view font,
2021-12-25 10:04:45 +08:00
float fontSize,
const Vec2& dimensions = Vec2::ZERO,
TextHAlignment hAlignment = TextHAlignment::LEFT,
TextVAlignment vAlignment = TextVAlignment::TOP);
/**
* Allocates and initializes a Label, base on FreeType2.
*
* @param text The initial text.
* @param fontFilePath A font file.
* @param fontSize The font size. This value must be > 0.
* @param dimensions
* @param hAlignment The text horizontal alignment.
* @param vAlignment The text vertical alignment.
*
* @return An automatically released Label object.
*/
static Label* createWithTTF(std::string_view text,
std::string_view fontFilePath,
2021-12-25 10:04:45 +08:00
float fontSize,
const Vec2& dimensions = Vec2::ZERO,
TextHAlignment hAlignment = TextHAlignment::LEFT,
TextVAlignment vAlignment = TextVAlignment::TOP);
/**
* Allocates and initializes a Label, base on FreeType2.
*
* @param ttfConfig A pointer to a TTFConfig object.
* @param text The initial text.
* @param hAlignment The text horizontal alignment.
* @param maxLineWidth The max line width.
*
* @return An automatically released Label object.
* @see TTFConfig setTTFConfig setMaxLineWidth
*/
static Label* createWithTTF(const TTFConfig& ttfConfig,
std::string_view text,
2021-12-25 10:04:45 +08:00
TextHAlignment hAlignment = TextHAlignment::LEFT,
int maxLineWidth = 0);
/**
* Allocates and initializes a Label, with a bitmap font file.
*
* @param bmfontPath A bitmap font file, it's a FNT format.
* @param text The initial text.
* @param hAlignment Text horizontal alignment.
* @param maxLineWidth The max line width.
*
* @return An automatically released Label object.
* @see setBMFontFilePath setMaxLineWidth
*/
static Label* createWithBMFont(std::string_view bmfontPath,
std::string_view text,
2021-12-25 10:04:45 +08:00
const TextHAlignment& hAlignment = TextHAlignment::LEFT,
int maxLineWidth = 0);
/**
* Allocates and initializes a Label, with a bitmap font file.
*
* @param bmfontPath A bitmap font file, it's a FNT format.
* @param text The initial text.
* @param hAlignment Text horizontal alignment.
* @param maxLineWidth The max line width.
* @param imageRect
* @param imageRotated
*
* @return An automatically released Label object.
* @see setBMFontFilePath setMaxLineWidth
*/
static Label* createWithBMFont(std::string_view bmfontPath,
std::string_view text,
2021-12-25 10:04:45 +08:00
const TextHAlignment& hAlignment,
int maxLineWidth,
const Rect& imageRect,
bool imageRotated);
/**
* Allocates and initializes a Label, with a bitmap font file.
*
* @param bmfontPath A bitmap font file, it's a FNT format.
* @param text The initial text.
* @param hAlignment Text horizontal alignment.
* @param maxLineWidth The max line width.
* @param subTextureKey Name of entry in PLIST texture atlas/sprite sheet
*
* @return An automatically released Label object.
* @see setBMFontFilePath setMaxLineWidth
*/
static Label* createWithBMFont(std::string_view bmfontPath,
std::string_view text,
2021-12-25 10:04:45 +08:00
const TextHAlignment& hAlignment,
int maxLineWidth,
std::string_view subTextureKey);
2021-12-25 10:04:45 +08:00
/**
* Allocates and initializes a Label, with a bitmap font file.
*
* @param bmfontPath A bitmap font file, it's a FNT format.
* @param text The initial text.
* @param hAlignment Text horizontal alignment.
* @param maxLineWidth The max line width.
* @param imageOffset Offset into larger texture
*
* @return An automatically released Label object.
* @see setBMFontFilePath setMaxLineWidth
*/
2022-07-16 10:43:05 +08:00
AX_DEPRECATED_ATTRIBUTE static Label* createWithBMFont(std::string_view bmfontPath,
std::string_view text,
2021-12-25 10:04:45 +08:00
const TextHAlignment& hAlignment,
int maxLineWidth,
const Vec2& imageOffset);
/**
* Allocates and initializes a Label, with char map configuration.
*
* @param charMapFile A char map file, it's a PNG format.
* @param itemWidth The width in points of each element.
* @param itemHeight The height in points of each element.
* @param startCharMap The starting char of the char map.
*
* @return An automatically released Label object.
*/
static Label* createWithCharMap(std::string_view charMapFile, int itemWidth, int itemHeight, int startCharMap);
2021-12-25 10:04:45 +08:00
/**
* Allocates and initializes a Label, with char map configuration.
*
* @param texture A pointer to an existing Texture2D object.
* @param itemWidth The width in points of each element.
* @param itemHeight The height in points of each element.
* @param startCharMap The starting char of the char map.
*
* @return An automatically released Label object.
*/
static Label* createWithCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap);
/**
* Allocates and initializes a Label, with char map configuration.
*
* @param plistFile A configuration file of char map.
*
* @return An automatically released Label object.
*/
static Label* createWithCharMap(std::string_view plistFile);
2019-11-23 20:27:39 +08:00
// end of creators group
/// @}
/// @{
/// @name Font methods
/**
* Sets a new TTF configuration to Label.
* @see `TTFConfig`
*/
virtual bool setTTFConfig(const TTFConfig& ttfConfig);
/**
* Returns the TTF configuration object used by the Label.
* @see `TTFConfig`
*/
2021-12-25 10:04:45 +08:00
virtual const TTFConfig& getTTFConfig() const { return _fontConfig; }
2019-11-23 20:27:39 +08:00
/** Sets a new bitmap font to Label */
virtual bool setBMFontFilePath(std::string_view bmfontFilePath, float fontSize = 0);
/** Sets a new bitmap font to Label */
virtual bool setBMFontFilePath(std::string_view bmfontFilePath,
2021-12-25 10:04:45 +08:00
const Rect& imageRect,
bool imageRotated,
float fontSize = 0);
/** Sets a new bitmap font to Label */
virtual bool setBMFontFilePath(std::string_view bmfontFilePath, std::string_view subTextureKey, float fontSize = 0);
/** Sets a new bitmap font to Label */
2022-07-16 10:43:05 +08:00
AX_DEPRECATED_ATTRIBUTE virtual bool setBMFontFilePath(std::string_view bmfontFilePath,
2021-12-25 10:04:45 +08:00
const Vec2& imageOffset,
float fontSize = 0);
2019-11-23 20:27:39 +08:00
/** Returns the bitmap font used by the Label.*/
std::string_view getBMFontFilePath() const { return _bmFontPath; }
2019-11-23 20:27:39 +08:00
/**
* Sets a new char map configuration to Label.
*
* @see `createWithCharMap(std::string_view,int,int,int)`
2019-11-23 20:27:39 +08:00
*/
virtual bool setCharMap(std::string_view charMapFile, int itemWidth, int itemHeight, int startCharMap);
2019-11-23 20:27:39 +08:00
/**
* Sets a new char map configuration to Label.
*
* @see `createWithCharMap(Texture2D*,int,int,int)`
*/
virtual bool setCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap);
/**
* Sets a new char map configuration to Label.
*
* @see `createWithCharMap(std::string_view)`
2019-11-23 20:27:39 +08:00
*/
virtual bool setCharMap(std::string_view plistFile);
2019-11-23 20:27:39 +08:00
/**
* Sets a new system font to Label.
*
* @param font A font file or a font family name.
* @warning
*/
virtual void setSystemFontName(std::string_view font);
2019-11-23 20:27:39 +08:00
/** Returns the system font used by the Label.*/
virtual std::string_view getSystemFontName() const { return _systemFont; }
2019-11-23 20:27:39 +08:00
/* Sets the system font size of Label.*/
virtual void setSystemFontSize(float fontSize);
/** Returns the bitmap font path used by the Label.*/
2021-12-25 10:04:45 +08:00
virtual float getSystemFontSize() const { return _systemFontSize; }
2019-11-23 20:27:39 +08:00
/**
* @warning This method is not recommended for game developers.
*/
2021-12-25 10:04:45 +08:00
virtual void requestSystemFontRefresh() { _systemFontDirty = true; }
2019-11-23 20:27:39 +08:00
// end of font methods
/// @}
/** Sets the text that this Label is to display.*/
virtual void setString(std::string_view text) override;
2019-11-23 20:27:39 +08:00
/** Return the text the Label is currently displaying.*/
virtual std::string_view getString() const override { return _utf8Text; }
2019-11-23 20:27:39 +08:00
/**
* Return the number of lines of text.
*/
int getStringNumLines();
/**
* Return length of string.
*/
int getStringLength();
/**
* Sets the text color of Label.
*
* The text color is different from the color of Node.
*
* @warning Limiting use to only when the Label created with true type font or system font.
*/
2021-12-25 10:04:45 +08:00
virtual void setTextColor(const Color4B& color);
2019-11-23 20:27:39 +08:00
/** Returns the text color of the Label.*/
2021-12-25 10:04:45 +08:00
const Color4B& getTextColor() const { return _textColor; }
2019-11-23 20:27:39 +08:00
/**
* Enable shadow effect to Label.
*
* @todo Support blur for shadow effect.
*/
2021-12-25 10:04:45 +08:00
virtual void enableShadow(const Color4B& shadowColor = Color4B::BLACK,
const Vec2& offset = Vec2(2, -2),
int blurRadius = 0);
2019-11-23 20:27:39 +08:00
/**
* Enable outline effect to Label.
* @warning Limiting use to only when the Label created with true type font or system font.
*/
2021-12-25 10:04:45 +08:00
virtual void enableOutline(const Color4B& outlineColor, int outlineSize = -1);
2019-11-23 20:27:39 +08:00
/**
2021-12-25 10:04:45 +08:00
* Enable glow effect to Label.
* @warning Limiting use to only when the Label created with true type font.
*/
2019-11-23 20:27:39 +08:00
virtual void enableGlow(const Color4B& glowColor);
/**
* Enable italics rendering
*/
void enableItalics();
/**
* Enable bold rendering
*/
void enableBold();
/**
* Enable underline
*/
void enableUnderline();
/**
* Enables strikethrough.
* Underline and Strikethrough cannot be enabled at the same time.
* Strikethrough is like an underline but at the middle of the glyph
*/
void enableStrikethrough();
/**
* Disable all effect applied to Label.
* @warning Please use disableEffect(LabelEffect::ALL) instead of this API.
*/
virtual void disableEffect();
/**
* Disable effect to Label.
*
* @see `LabelEffect`
*/
virtual void disableEffect(LabelEffect effect);
/**
2021-12-25 10:04:45 +08:00
* Return whether the shadow effect is enabled.
*/
2019-11-23 20:27:39 +08:00
bool isShadowEnabled() const { return _shadowEnabled; }
/**
2021-12-25 10:04:45 +08:00
* Return shadow effect offset value.
*/
2021-10-23 23:27:14 +08:00
Vec2 getShadowOffset() const { return _shadowOffset; }
2019-11-23 20:27:39 +08:00
/**
2021-12-25 10:04:45 +08:00
* Return the shadow effect blur radius.
*/
2019-11-23 20:27:39 +08:00
float getShadowBlurRadius() const { return _shadowBlurRadius; }
/**
2021-12-25 10:04:45 +08:00
* Return the shadow effect color value.
*/
2019-11-23 20:27:39 +08:00
Color4F getShadowColor() const { return _shadowColor4F; }
/**
2021-12-25 10:04:45 +08:00
* Return the outline effect size value.
*/
2019-11-23 20:27:39 +08:00
float getOutlineSize() const { return _outlineSize; }
/**
2021-12-25 10:04:45 +08:00
* Return current effect type.
*/
2019-11-23 20:27:39 +08:00
LabelEffect getLabelEffectType() const { return _currLabelEffect; }
/**
2021-12-25 10:04:45 +08:00
* Return current effect color value.
*/
2019-11-23 20:27:39 +08:00
Color4F getEffectColor() const { return _effectColorF; }
/** Sets the Label's text horizontal alignment.*/
2021-12-25 10:04:45 +08:00
void setAlignment(TextHAlignment hAlignment) { setAlignment(hAlignment, _vAlignment); }
2019-11-23 20:27:39 +08:00
/** Returns the Label's text horizontal alignment.*/
2021-12-25 10:04:45 +08:00
TextHAlignment getTextAlignment() const { return _hAlignment; }
2019-11-23 20:27:39 +08:00
/** Sets the Label's text alignment.*/
2021-12-25 10:04:45 +08:00
void setAlignment(TextHAlignment hAlignment, TextVAlignment vAlignment);
2019-11-23 20:27:39 +08:00
/** Sets the Label's text horizontal alignment.*/
2021-12-25 10:04:45 +08:00
void setHorizontalAlignment(TextHAlignment hAlignment) { setAlignment(hAlignment, _vAlignment); }
2019-11-23 20:27:39 +08:00
/** Returns the Label's text horizontal alignment.*/
TextHAlignment getHorizontalAlignment() const { return _hAlignment; }
/** Sets the Label's text vertical alignment.*/
2021-12-25 10:04:45 +08:00
void setVerticalAlignment(TextVAlignment vAlignment) { setAlignment(_hAlignment, vAlignment); }
2019-11-23 20:27:39 +08:00
/** Returns the Label's text vertical alignment.*/
TextVAlignment getVerticalAlignment() const { return _vAlignment; }
/**
* Specify what happens when a line is too long for Label.
*
* @param breakWithoutSpace Lines are automatically broken between words if this value is false.
*/
void setLineBreakWithoutSpace(bool breakWithoutSpace);
/**
* Makes the Label at most this line untransformed width.
* The Label's max line width be used for force line breaks if the value not equal zero.
*/
void setMaxLineWidth(float maxLineWidth);
float getMaxLineWidth() { return _maxLineWidth; }
/**
* Change font size of label type BMFONT
* Note: This function only scale the BMFONT letter to mimic the font size change effect.
*
* @param fontSize The desired font size in float.
*/
void setBMFontSize(float fontSize);
/**
* Return the user define BMFont size.
*
* @return The BMFont size in float value.
*/
2021-12-25 10:04:45 +08:00
float getBMFontSize() const;
2019-11-23 20:27:39 +08:00
/**
* Toggle wrap option of the label.
* Note: System font doesn't support manually toggle wrap.
*
* @param enable Set true to enable wrap and false to disable wrap.
*/
void enableWrap(bool enable);
/**
* Query the wrap is enabled or not.
* Note: System font will always return true.
*/
2021-12-25 10:04:45 +08:00
bool isWrapEnabled() const;
2019-11-23 20:27:39 +08:00
/**
* Change the label's Overflow type, currently only TTF and BMFont support all the valid Overflow type.
* Char Map font supports all the Overflow type except for SHRINK, because we can't measure it's font size.
* System font only support Overflow::Normal and Overflow::RESIZE_HEIGHT.
*
* @param overflow see `Overflow`
*/
void setOverflow(Overflow overflow);
/**
* Query the label's Overflow type.
*
* @return see `Overflow`
*/
2021-12-25 10:04:45 +08:00
Overflow getOverflow() const;
2019-11-23 20:27:39 +08:00
/**
* Makes the Label exactly this untransformed width.
*
* The Label's width be used for text align if the value not equal zero.
2021-12-25 10:04:45 +08:00
*/
void setWidth(float width) { setDimensions(width, _labelHeight); }
2019-11-23 20:27:39 +08:00
float getWidth() const { return _labelWidth; }
/**
* Makes the Label exactly this untransformed height.
*
* The Label's height be used for text align if the value not equal zero.
* The text will display incomplete if the size of Label is not large enough to display all text.
*/
2021-12-25 10:04:45 +08:00
void setHeight(float height) { setDimensions(_labelWidth, height); }
2019-11-23 20:27:39 +08:00
float getHeight() const { return _labelHeight; }
/** Sets the untransformed size of the Label in a more efficient way. */
void setDimensions(float width, float height);
2021-12-25 10:04:45 +08:00
const Vec2& getDimensions() const { return _labelDimensions; }
2019-11-23 20:27:39 +08:00
/** Update content immediately.*/
virtual void updateContent();
/**
* Provides a way to treat each character like a Sprite.
* @warning No support system font.
*/
2021-12-25 10:04:45 +08:00
virtual Sprite* getLetter(int lettetIndex);
2019-11-23 20:27:39 +08:00
/** Clips upper and lower margin to reduce height of Label.*/
void setClipMarginEnabled(bool clipEnabled) { _clipEnabled = clipEnabled; }
bool isClipMarginEnabled() const { return _clipEnabled; }
/** Sets the line height of the Label.
* @warning Not support system font.
* @since v3.2.0
*/
void setLineHeight(float height);
/**
* Returns the line height of this Label.
* @warning Not support system font.
* @since v3.2.0
*/
float getLineHeight() const;
void setLineSpacing(float height);
float getLineSpacing() const;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
Returns type of label
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
@warning Not support system font.
@return the type of label
@since v3.18.0
*/
LabelType getLabelType() const { return _currentLabelType; }
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
Returns font size
*/
2021-12-25 10:04:45 +08:00
float getRenderingFontSize() const;
2019-11-23 20:27:39 +08:00
/**
* Sets the additional kerning of the Label.
*
* @warning Not support system font.
* @since v3.2.0
*/
void setAdditionalKerning(float space);
/**
* Returns the additional kerning of the Label.
*
* @warning Not support system font.
* @since v3.2.0
*/
float getAdditionalKerning() const;
2023-08-25 16:57:38 +08:00
bool setProgramState(backend::ProgramState* programState, bool needsRetain = true) override;
2019-11-23 20:27:39 +08:00
FontAtlas* getFontAtlas() { return _fontAtlas; }
virtual const BlendFunc& getBlendFunc() const override { return _blendFunc; }
2021-12-25 10:04:45 +08:00
virtual void setBlendFunc(const BlendFunc& blendFunc) override;
2019-11-23 20:27:39 +08:00
virtual bool isOpacityModifyRGB() const override { return _isOpacityModifyRGB; }
virtual void setOpacityModifyRGB(bool isOpacityModifyRGB) override;
virtual void updateDisplayedColor(const Color3B& parentColor) override;
virtual void updateDisplayedOpacity(uint8_t parentOpacity) override;
virtual std::string getDescription() const override;
2021-10-23 23:27:14 +08:00
virtual const Vec2& getContentSize() const override;
2019-11-23 20:27:39 +08:00
virtual Rect getBoundingBox() const override;
2021-12-25 10:04:45 +08:00
virtual void visit(Renderer* renderer, const Mat4& parentTransform, uint32_t parentFlags) override;
virtual void draw(Renderer* renderer, const Mat4& transform, uint32_t flags) override;
2019-11-23 20:27:39 +08:00
virtual void setCameraMask(unsigned short mask, bool applyChildren = true) override;
virtual void removeAllChildrenWithCleanup(bool cleanup) override;
virtual void removeChild(Node* child, bool cleanup = true) override;
virtual void setGlobalZOrder(float globalZOrder) override;
/**
* Constructor of Label.
* @js NA
*/
Label(TextHAlignment hAlignment = TextHAlignment::LEFT, TextVAlignment vAlignment = TextVAlignment::TOP);
2019-11-23 20:27:39 +08:00
/**
* Destructor of Label.
* @js NA
* @lua NA
*/
virtual ~Label();
bool initWithTTF(std::string_view text,
std::string_view fontFilePath,
2021-12-25 10:04:45 +08:00
float fontSize,
const Vec2& dimensions = Vec2::ZERO,
TextHAlignment hAlignment = TextHAlignment::LEFT,
2019-11-23 20:27:39 +08:00
TextVAlignment vAlignment = TextVAlignment::TOP);
2021-12-25 10:04:45 +08:00
bool initWithTTF(const TTFConfig& ttfConfig,
std::string_view text,
2021-12-25 10:04:45 +08:00
TextHAlignment hAlignment = TextHAlignment::LEFT,
int maxLineWidth = 0);
2019-11-23 20:27:39 +08:00
protected:
struct LetterInfo
{
char32_t utf32Char;
bool valid;
float positionX;
float positionY;
int atlasIndex;
int lineIndex;
};
2021-12-25 10:04:45 +08:00
struct BatchCommand
{
2019-11-23 20:27:39 +08:00
BatchCommand();
2020-09-17 12:10:08 +08:00
BatchCommand(const BatchCommand& rhs) = delete;
2021-12-25 10:04:45 +08:00
BatchCommand(BatchCommand&& rhs) = default;
2019-11-23 20:27:39 +08:00
~BatchCommand();
2020-09-17 12:10:08 +08:00
BatchCommand& operator=(const BatchCommand& rhs) = delete;
BatchCommand& operator=(BatchCommand&& rhs) = default;
void setProgramState(backend::ProgramState* state);
std::array<CustomCommand*, 3> getCommandArray();
2019-11-23 20:27:39 +08:00
CustomCommand textCommand;
CustomCommand outLineCommand;
CustomCommand shadowCommand;
};
virtual void setFontAtlas(FontAtlas* atlas, bool distanceFieldEnabled = false, bool useA8Shader = false);
bool getFontLetterDef(char32_t character, FontLetterDefinition& letterDef) const;
void computeStringNumLines();
void drawSelf(bool visibleByCamera, Renderer* renderer, uint32_t flags);
bool multilineTextWrapByChar();
bool multilineTextWrapByWord();
bool multilineTextWrap(const std::function<int(const std::u32string&, int, int)>& lambda);
void shrinkLabelToContentSize(const std::function<bool(void)>& lambda);
bool isHorizontalClamp();
bool isVerticalClamp();
void rescaleWithOriginalFontSize();
void updateLabelLetters();
virtual bool alignText();
void computeAlignmentOffset();
bool computeHorizontalKernings(const std::u32string& stringToRender);
2022-08-08 18:02:17 +08:00
void recordLetterInfo(const ax::Vec2& point, char32_t utf32Char, int letterIndex, int lineIndex);
2019-11-23 20:27:39 +08:00
void recordPlaceholderInfo(int letterIndex, char32_t utf16Char);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
bool updateQuads();
void createSpriteForSystemFont(const FontDefinition& fontDef);
void createShadowSpriteForSystemFont(const FontDefinition& fontDef);
virtual void updateShaderProgram();
virtual void updateBMFontScale();
2022-01-03 13:55:52 +08:00
void scaleFontSize(float fontSize);
2019-11-23 20:27:39 +08:00
bool setTTFConfigInternal(const TTFConfig& ttfConfig);
void setBMFontSizeInternal(float fontSize);
bool isHorizontalClamped(float letterPositionX, int lineIndex);
void restoreFontSize();
void updateLetterSpriteScale(Sprite* sprite);
int getFirstCharLen(const std::u32string& utf32Text, int startIndex, int textLen) const;
int getFirstWordLen(const std::u32string& utf32Text, int startIndex, int textLen) const;
void reset();
FontDefinition _getFontDefinition() const;
virtual void updateColor() override;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
void updateUniformLocations();
void setVertexLayout();
2019-11-23 20:27:39 +08:00
void updateBlendState();
2021-12-25 10:04:45 +08:00
void updateEffectUniforms(BatchCommand& batch,
TextureAtlas* textureAtlas,
Renderer* renderer,
const Mat4& transform);
2019-11-23 20:27:39 +08:00
void updateBuffer(TextureAtlas* textureAtlas, CustomCommand& customCommand);
2021-12-25 10:04:45 +08:00
void updateBatchCommand(BatchCommand& batch);
2019-11-23 20:27:39 +08:00
LabelType _currentLabelType;
bool _contentDirty;
std::u32string _utf32Text;
std::string _utf8Text;
int _numberOfLines;
std::string _bmFontPath;
std::string _bmSubTextureKey;
Rect _bmRect;
bool _bmRotated;
2019-11-23 20:27:39 +08:00
TTFConfig _fontConfig;
float _outlineSize;
bool _systemFontDirty;
std::string _systemFont;
float _systemFontSize;
Sprite* _textSprite;
Sprite* _shadowNode;
FontAtlas* _fontAtlas;
Vector<SpriteBatchNode*> _batchNodes;
std::vector<LetterInfo> _lettersInfo;
//! used for optimization
2021-12-25 10:04:45 +08:00
Sprite* _reusedLetter;
2019-11-23 20:27:39 +08:00
Rect _reusedRect;
int _lengthOfString;
2021-12-25 10:04:45 +08:00
// layout relevant properties.
2019-11-23 20:27:39 +08:00
float _lineHeight;
float _lineSpacing;
float _additionalKerning;
int* _horizontalKernings;
bool _lineBreakWithoutSpaces;
float _maxLineWidth;
2021-10-23 23:27:14 +08:00
Vec2 _labelDimensions;
2019-11-23 20:27:39 +08:00
float _labelWidth;
float _labelHeight;
TextHAlignment _hAlignment;
TextVAlignment _vAlignment;
float _textDesiredHeight;
std::vector<float> _linesWidth;
std::vector<float> _linesOffsetX;
float _letterOffsetY;
float _tailoredTopY;
float _tailoredBottomY;
LabelEffect _currLabelEffect;
Color4F _effectColorF;
Color4B _textColor;
Color4F _textColorF;
QuadCommand _quadCommand;
std::vector<BatchCommand> _batchCommands;
2021-12-25 10:04:45 +08:00
Mat4 _shadowTransform;
int _uniformEffectColor;
int _uniformEffectType; // 0: None, 1: Outline, 2: Shadow; Only used when outline is enabled.
int _uniformTextColor;
2019-11-23 20:27:39 +08:00
bool _useDistanceField;
bool _useA8Shader;
bool _shadowDirty;
bool _shadowEnabled;
2021-10-23 23:27:14 +08:00
Vec2 _shadowOffset;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
Color4F _shadowColor4F;
Color3B _shadowColor3B;
uint8_t _shadowOpacity;
float _shadowBlurRadius;
bool _clipEnabled;
bool _blendFuncDirty;
BlendFunc _blendFunc;
/// whether or not the label was inside bounds the previous frame
bool _insideBounds;
bool _isOpacityModifyRGB;
std::unordered_map<int, Sprite*> _letters;
EventListenerCustom* _purgeTextureListener;
EventListenerCustom* _resetTextureListener;
2022-07-16 10:43:05 +08:00
#if AX_LABEL_DEBUG_DRAW
2019-11-23 20:27:39 +08:00
DrawNode* _debugDrawNode;
#endif
bool _enableWrap;
float _bmFontSize;
float _bmfontScale;
Overflow _overflow;
float _originalFontSize;
bool _boldEnabled;
DrawNode* _underlineNode;
bool _strikethroughEnabled;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
backend::UniformLocation _mvpMatrixLocation;
backend::UniformLocation _textureLocation;
backend::UniformLocation _textColorLocation;
backend::UniformLocation _effectColorLocation;
backend::UniformLocation _effectTypeLocation;
private:
2022-07-16 10:43:05 +08:00
AX_DISALLOW_COPY_AND_ASSIGN(Label);
2019-11-23 20:27:39 +08:00
};
// end group
/// @}
NS_AX_END
2019-11-23 20:27:39 +08:00
#endif /*__COCOS2D_CCLABEL_H */