2014-03-11 17:13:54 +08:00
|
|
|
/****************************************************************************
|
2016-08-05 09:42:15 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
2014-03-11 17:13:54 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __UILABEL_H__
|
|
|
|
#define __UILABEL_H__
|
|
|
|
|
|
|
|
#include "ui/UIWidget.h"
|
2014-07-21 17:45:56 +08:00
|
|
|
#include "ui/GUIExport.h"
|
2015-04-10 14:18:16 +08:00
|
|
|
#include "base/ccTypes.h"
|
2014-03-11 17:13:54 +08:00
|
|
|
|
2015-03-25 10:13:17 +08:00
|
|
|
/**
|
|
|
|
* @addtogroup ui
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2014-06-04 14:26:21 +08:00
|
|
|
class Label;
|
2017-01-13 18:06:01 +08:00
|
|
|
class Sprite;
|
2014-06-04 14:26:21 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
namespace ui {
|
|
|
|
|
|
|
|
/**
|
2014-05-14 16:14:28 +08:00
|
|
|
* For creating a system font or a TTF font Text
|
|
|
|
*/
|
2014-07-21 17:45:56 +08:00
|
|
|
class CC_GUI_DLL Text : public Widget
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2015-04-10 14:18:16 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
DECLARE_CLASS_GUI_INFO
|
2015-04-10 14:18:16 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
public:
|
2015-03-25 10:13:17 +08:00
|
|
|
/** Type Text type.
|
|
|
|
*/
|
2014-05-13 17:45:15 +08:00
|
|
|
enum class Type
|
|
|
|
{
|
|
|
|
SYSTEM,
|
|
|
|
TTF
|
|
|
|
};
|
2014-03-11 17:13:54 +08:00
|
|
|
/**
|
2015-03-25 10:13:17 +08:00
|
|
|
* Default constructor.
|
2015-03-30 21:38:32 +08:00
|
|
|
* @js ctor
|
2015-03-30 15:47:47 +08:00
|
|
|
* @lua new
|
2014-03-11 17:13:54 +08:00
|
|
|
*/
|
|
|
|
Text();
|
|
|
|
|
|
|
|
/**
|
2015-03-25 10:13:17 +08:00
|
|
|
* Default destructor.
|
2015-03-30 21:38:32 +08:00
|
|
|
* @js NA
|
2015-03-30 15:47:47 +08:00
|
|
|
* @lua NA
|
2014-03-11 17:13:54 +08:00
|
|
|
*/
|
|
|
|
virtual ~Text();
|
|
|
|
|
|
|
|
/**
|
2015-03-25 10:13:17 +08:00
|
|
|
* Create a Text object.
|
|
|
|
*
|
|
|
|
* @return An autoreleased Text object.
|
2014-03-11 17:13:54 +08:00
|
|
|
*/
|
|
|
|
static Text* create();
|
2015-04-10 14:18:16 +08:00
|
|
|
|
2014-04-02 17:40:09 +08:00
|
|
|
/**
|
2015-03-25 10:13:17 +08:00
|
|
|
* Create a Text object with textContent, fontName and fontSize.
|
|
|
|
* The fontName could be a system font name or a TTF file path.
|
2015-04-10 15:30:18 +08:00
|
|
|
* Usage:
|
|
|
|
* @code
|
|
|
|
* //create a system font UIText.
|
|
|
|
* Text *text = Text::create("Hello", "Arial", 20);
|
|
|
|
* //create a TTF font UIText.
|
|
|
|
* Text *text = Text::create("Hello", "xxx\xxx.ttf", 20);
|
|
|
|
* @endcode
|
2015-03-25 10:13:17 +08:00
|
|
|
*
|
|
|
|
* @param textContent Text content string.
|
|
|
|
* @param fontName A given font name.
|
|
|
|
* @param fontSize A given font size.
|
|
|
|
* @return An autoreleased Text object.
|
2014-04-02 17:40:09 +08:00
|
|
|
*/
|
|
|
|
static Text* create(const std::string& textContent,
|
|
|
|
const std::string& fontName,
|
2015-07-28 14:48:01 +08:00
|
|
|
float fontSize);
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Changes the string value of label.
|
|
|
|
*
|
2015-03-25 10:13:17 +08:00
|
|
|
* @param text String value.
|
2014-03-11 17:13:54 +08:00
|
|
|
*/
|
2015-04-10 15:30:18 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE void setText(const std::string& text)
|
|
|
|
{
|
|
|
|
this->setString(text);
|
|
|
|
}
|
2014-05-14 15:26:14 +08:00
|
|
|
void setString(const std::string& text);
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the string value of label.
|
|
|
|
*
|
2015-03-25 10:13:17 +08:00
|
|
|
* @return String value.
|
2014-03-11 17:13:54 +08:00
|
|
|
*/
|
2015-04-10 15:30:18 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE const std::string& getStringValue()
|
|
|
|
{
|
|
|
|
return this->getString();
|
|
|
|
}
|
2014-05-14 15:26:14 +08:00
|
|
|
const std::string& getString()const;
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
/**
|
2014-05-14 15:26:14 +08:00
|
|
|
* Gets the string length of the label.
|
|
|
|
* Note: This length will be larger than the raw string length,
|
2015-04-10 15:30:18 +08:00
|
|
|
* if you want to get the raw string length,
|
|
|
|
* you should call this->getString().size() instead.
|
2014-03-11 17:13:54 +08:00
|
|
|
*
|
2015-03-25 10:13:17 +08:00
|
|
|
* @return String length.
|
2014-03-11 17:13:54 +08:00
|
|
|
*/
|
2014-05-14 16:25:30 +08:00
|
|
|
ssize_t getStringLength()const;
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the font size of label.
|
|
|
|
*
|
2015-03-25 10:13:17 +08:00
|
|
|
* @param size The font size.
|
2014-03-11 17:13:54 +08:00
|
|
|
*/
|
2015-07-28 14:48:01 +08:00
|
|
|
void setFontSize(float size);
|
2014-03-11 17:13:54 +08:00
|
|
|
|
2015-03-25 10:13:17 +08:00
|
|
|
/**
|
|
|
|
* Gets the font size of label.
|
|
|
|
*
|
|
|
|
* @return The font size.
|
|
|
|
*/
|
2015-07-28 14:48:01 +08:00
|
|
|
float getFontSize()const;
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the font name of label.
|
2014-05-14 09:37:21 +08:00
|
|
|
* If you are trying to use a system font, you could just pass a font name
|
|
|
|
* If you are trying to use a TTF, you should pass a file path to the TTF file
|
2015-04-10 15:30:18 +08:00
|
|
|
* Usage:
|
|
|
|
* @code
|
|
|
|
* //create a system font UIText
|
|
|
|
* Text *text = Text::create("Hello", "Arial", 20);
|
|
|
|
* // it will change the font to system font no matter the previous font type is TTF or system font
|
|
|
|
* text->setFontName("Marfelt");
|
|
|
|
* //it will change the font to TTF font no matter the previous font type is TTF or system font
|
|
|
|
* text->setFontName("xxxx/xxx.ttf");
|
|
|
|
* @endcode
|
2015-03-25 10:13:17 +08:00
|
|
|
* @param name Font name.
|
2014-03-11 17:13:54 +08:00
|
|
|
*/
|
|
|
|
void setFontName(const std::string& name);
|
|
|
|
|
2015-03-25 10:13:17 +08:00
|
|
|
/** Gets the font name.
|
|
|
|
*
|
|
|
|
* @return Font name.
|
|
|
|
*/
|
2014-05-27 10:54:47 +08:00
|
|
|
const std::string& getFontName()const;
|
2015-04-10 14:18:16 +08:00
|
|
|
|
2015-03-25 10:13:17 +08:00
|
|
|
/** Gets the font type.
|
|
|
|
* @return The font type.
|
|
|
|
*/
|
2014-05-13 17:45:15 +08:00
|
|
|
Type getType() const;
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the touch scale enabled of label.
|
|
|
|
*
|
2015-03-25 10:13:17 +08:00
|
|
|
* @param enabled Touch scale enabled of label.
|
2014-03-11 17:13:54 +08:00
|
|
|
*/
|
|
|
|
void setTouchScaleChangeEnabled(bool enabled);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the touch scale enabled of label.
|
|
|
|
*
|
2015-03-25 10:13:17 +08:00
|
|
|
* @return Touch scale enabled of label.
|
2014-03-11 17:13:54 +08:00
|
|
|
*/
|
2014-05-27 10:54:47 +08:00
|
|
|
bool isTouchScaleChangeEnabled()const;
|
2014-03-11 17:13:54 +08:00
|
|
|
|
2014-04-17 14:08:25 +08:00
|
|
|
//override "getVirtualRendererSize" method of widget.
|
2014-09-12 14:39:22 +08:00
|
|
|
virtual Size getVirtualRendererSize() const override;
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
//override "getVirtualRenderer" method of widget.
|
|
|
|
virtual Node* getVirtualRenderer() override;
|
|
|
|
|
2015-03-25 10:13:17 +08:00
|
|
|
/** Gets the render size in auto mode.
|
|
|
|
*
|
|
|
|
* @return The size of render size in auto mode.
|
|
|
|
*/
|
2015-01-20 18:40:25 +08:00
|
|
|
virtual Size getAutoRenderSize();
|
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
/**
|
|
|
|
* Returns the "class name" of widget.
|
|
|
|
*/
|
|
|
|
virtual std::string getDescription() const override;
|
|
|
|
|
2014-11-26 15:05:20 +08:00
|
|
|
/**
|
2015-03-25 10:13:17 +08:00
|
|
|
* Sets the rendering size of the text, you should call this method
|
2014-11-26 15:05:20 +08:00
|
|
|
* along with calling `ignoreContentAdaptWithSize(false)`, otherwise the text area
|
2015-10-23 15:37:33 +08:00
|
|
|
* size is calculated by the real size of the text content.
|
2015-03-25 10:13:17 +08:00
|
|
|
*
|
|
|
|
* @param size The text rendering area size.
|
2014-11-26 15:05:20 +08:00
|
|
|
*
|
|
|
|
*/
|
2014-03-11 17:13:54 +08:00
|
|
|
void setTextAreaSize(const Size &size);
|
|
|
|
|
2015-03-25 10:13:17 +08:00
|
|
|
/** Return the text rendering area size.
|
|
|
|
*
|
|
|
|
* @return The text rendering area size.
|
|
|
|
*/
|
2014-05-27 10:54:47 +08:00
|
|
|
const Size& getTextAreaSize()const;
|
2014-03-11 17:13:54 +08:00
|
|
|
|
2015-03-25 10:13:17 +08:00
|
|
|
/** Sets text horizontal alignment.
|
|
|
|
*
|
|
|
|
* @param alignment Horizontal text alignment type
|
|
|
|
*/
|
2014-03-11 17:13:54 +08:00
|
|
|
void setTextHorizontalAlignment(TextHAlignment alignment);
|
|
|
|
|
2015-03-25 10:13:17 +08:00
|
|
|
/** Gets text horizontal alignment.
|
|
|
|
*
|
|
|
|
* @return Horizontal text alignment type
|
|
|
|
*/
|
2014-05-27 10:54:47 +08:00
|
|
|
TextHAlignment getTextHorizontalAlignment()const;
|
2014-03-11 17:13:54 +08:00
|
|
|
|
2015-03-25 10:13:17 +08:00
|
|
|
/** Sets text vertical alignment.
|
|
|
|
*
|
|
|
|
* @param alignment vertical text alignment type
|
|
|
|
*/
|
2014-03-11 17:13:54 +08:00
|
|
|
void setTextVerticalAlignment(TextVAlignment alignment);
|
|
|
|
|
2015-03-25 10:13:17 +08:00
|
|
|
/** Gets text vertical alignment.
|
|
|
|
*
|
|
|
|
* @return Vertical text alignment type
|
|
|
|
*/
|
2014-05-27 10:54:47 +08:00
|
|
|
TextVAlignment getTextVerticalAlignment()const;
|
2015-04-10 14:18:16 +08:00
|
|
|
|
2015-03-25 10:13:17 +08:00
|
|
|
/** Sets text color.
|
|
|
|
*
|
|
|
|
* @param color Text color.
|
|
|
|
*/
|
2014-07-29 12:47:21 +08:00
|
|
|
void setTextColor(const Color4B color);
|
2015-04-10 14:18:16 +08:00
|
|
|
|
2015-03-25 10:13:17 +08:00
|
|
|
/** Gets text color.
|
|
|
|
*
|
|
|
|
* @return Text color.
|
|
|
|
*/
|
2014-07-29 12:47:21 +08:00
|
|
|
const Color4B& getTextColor() const;
|
2015-04-10 14:18:16 +08:00
|
|
|
|
2014-06-05 19:20:16 +08:00
|
|
|
/**
|
2015-03-25 10:13:17 +08:00
|
|
|
* Enable shadow for the label.
|
2014-06-05 19:20:16 +08:00
|
|
|
*
|
|
|
|
* @todo support blur for shadow effect
|
2015-03-25 10:13:17 +08:00
|
|
|
*
|
|
|
|
* @param shadowColor The color of shadow effect.
|
|
|
|
* @param offset The offset of shadow effect.
|
|
|
|
* @param blurRadius The blur radius of shadow effect.
|
2014-06-05 19:20:16 +08:00
|
|
|
*/
|
2015-04-10 15:30:18 +08:00
|
|
|
void enableShadow(const Color4B& shadowColor = Color4B::BLACK,
|
|
|
|
const Size &offset = Size(2,-2),
|
|
|
|
int blurRadius = 0);
|
2015-04-10 14:18:16 +08:00
|
|
|
|
2014-06-06 16:54:51 +08:00
|
|
|
/**
|
2015-03-25 10:13:17 +08:00
|
|
|
* Enable outline for the label.
|
|
|
|
* It only works on IOS and Android when you use System fonts.
|
|
|
|
*
|
|
|
|
* @param outlineColor The color of outline.
|
|
|
|
* @param outlineSize The size of outline.
|
2015-04-10 14:18:16 +08:00
|
|
|
*/
|
2014-06-09 14:37:56 +08:00
|
|
|
void enableOutline(const Color4B& outlineColor,int outlineSize = 1);
|
2015-04-10 14:18:16 +08:00
|
|
|
|
2015-03-25 10:13:17 +08:00
|
|
|
/** Only support for TTF.
|
|
|
|
*
|
|
|
|
* @param glowColor The color of glow.
|
|
|
|
*/
|
2014-06-05 19:20:16 +08:00
|
|
|
void enableGlow(const Color4B& glowColor);
|
2015-04-10 14:18:16 +08:00
|
|
|
|
|
|
|
/** Disable all text effects, including shadow, outline and glow.
|
2015-03-25 10:13:17 +08:00
|
|
|
*/
|
2014-06-05 19:20:16 +08:00
|
|
|
void disableEffect();
|
2015-04-10 14:18:16 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Disable specific text effect.
|
|
|
|
* Use LabelEffect parameter to specify which effect should be disabled.
|
|
|
|
*
|
|
|
|
* @see `LabelEffect`
|
|
|
|
*/
|
|
|
|
void disableEffect(LabelEffect effect);
|
|
|
|
|
2015-12-11 12:04:54 +08:00
|
|
|
/**
|
|
|
|
* Return whether the shadow effect is enabled.
|
|
|
|
*/
|
2015-12-10 13:17:31 +08:00
|
|
|
bool isShadowEnabled() const;
|
2015-12-11 12:04:54 +08:00
|
|
|
/**
|
|
|
|
* Return shadow effect offset value.
|
|
|
|
*/
|
2015-12-10 13:17:31 +08:00
|
|
|
Size getShadowOffset() const;
|
2015-12-11 12:04:54 +08:00
|
|
|
/**
|
|
|
|
* Return the shadow effect blur radius.
|
|
|
|
*/
|
2015-12-10 13:17:31 +08:00
|
|
|
float getShadowBlurRadius() const;
|
2015-12-11 12:04:54 +08:00
|
|
|
/**
|
|
|
|
* Return the shadow effect color value.
|
|
|
|
*/
|
2015-12-10 13:17:31 +08:00
|
|
|
Color4B getShadowColor() const;
|
2015-12-11 12:04:54 +08:00
|
|
|
/**
|
|
|
|
* Return the outline effect size value.
|
|
|
|
*/
|
2015-12-10 13:17:31 +08:00
|
|
|
int getOutlineSize() const;
|
2015-12-11 12:04:54 +08:00
|
|
|
/**
|
|
|
|
* Return current effect type.
|
|
|
|
*/
|
2015-12-10 13:17:31 +08:00
|
|
|
LabelEffect getLabelEffectType() const;
|
2015-12-11 12:04:54 +08:00
|
|
|
/**
|
2016-01-11 18:15:34 +08:00
|
|
|
* Return current effect color value.
|
2015-12-11 12:04:54 +08:00
|
|
|
*/
|
2015-12-10 13:17:31 +08:00
|
|
|
Color4B getEffectColor() const;
|
2017-01-13 18:06:01 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides a way to treat each character like a Sprite.
|
|
|
|
* @warning No support system font.
|
|
|
|
*/
|
|
|
|
virtual Sprite * getLetter(int lettetIndex);
|
2015-12-10 13:17:31 +08:00
|
|
|
|
2014-03-21 13:44:29 +08:00
|
|
|
CC_CONSTRUCTOR_ACCESS:
|
|
|
|
virtual bool init() override;
|
2014-04-02 17:40:09 +08:00
|
|
|
virtual bool init(const std::string& textContent,
|
|
|
|
const std::string& fontName,
|
2015-07-28 14:48:01 +08:00
|
|
|
float fontSize);
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void initRenderer() override;
|
|
|
|
virtual void onPressStateChangedToNormal() override;
|
|
|
|
virtual void onPressStateChangedToPressed() override;
|
|
|
|
virtual void onPressStateChangedToDisabled() override;
|
|
|
|
virtual void onSizeChanged() override;
|
2015-04-10 14:18:16 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
void labelScaleChangedWithSize();
|
|
|
|
virtual Widget* createCloneInstance() override;
|
|
|
|
virtual void copySpecialProperties(Widget* model) override;
|
2014-04-17 14:08:25 +08:00
|
|
|
virtual void adaptRenderers() override;
|
2014-03-11 17:13:54 +08:00
|
|
|
protected:
|
|
|
|
bool _touchScaleChangeEnabled;
|
|
|
|
float _normalScaleValueX;
|
|
|
|
float _normalScaleValueY;
|
|
|
|
std::string _fontName;
|
2015-07-28 14:48:01 +08:00
|
|
|
float _fontSize;
|
2014-03-11 17:13:54 +08:00
|
|
|
float _onSelectedScaleOffset;
|
2014-03-14 21:06:40 +08:00
|
|
|
Label* _labelRenderer;
|
2014-04-17 14:08:25 +08:00
|
|
|
bool _labelRendererAdaptDirty;
|
2014-05-13 17:45:15 +08:00
|
|
|
Type _type;
|
2014-03-11 17:13:54 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
2015-03-25 10:13:17 +08:00
|
|
|
// end of ui group
|
|
|
|
/// @}
|
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
#endif /* defined(__CocoGUI__Label__) */
|