axmol/cocos/ui/UIText.h

301 lines
8.4 KiB
C
Raw Normal View History

2014-03-11 17:13:54 +08:00
/****************************************************************************
Copyright (c) 2013-2014 Chukong Technologies Inc.
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"
#include "ui/GUIExport.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;
2014-03-11 17:13:54 +08:00
namespace ui {
/**
* For creating a system font or a TTF font Text
*/
class CC_GUI_DLL Text : public Widget
2014-03-11 17:13:54 +08:00
{
DECLARE_CLASS_GUI_INFO
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.
2014-03-11 17:13:54 +08:00
*/
Text();
/**
2015-03-25 10:13:17 +08:00
* Default destructor.
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-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.
* Usage: Text *text = Text::create("Hello", "Arial", 20); //create a system font UIText.
* Text *text = Text::create("Hello", "xxx\xxx.ttf", 20); //create a TTF font UIText.
*
* @param textContent Text content string.
* @param fontName A given font name.
* @param fontSize A given font size.
* @return An autoreleased Text object.
*/
static Text* create(const std::string& textContent,
const std::string& fontName,
int 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
*/
2014-05-14 15:26:14 +08:00
CC_DEPRECATED_ATTRIBUTE void setText(const std::string& text){this->setString(text);}
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
*/
2014-05-14 15:26:14 +08:00
CC_DEPRECATED_ATTRIBUTE const std::string& getStringValue(){ return this->getString();}
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-03-25 10:13:17 +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
*/
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
*/
void setFontSize(int size);
2015-03-25 10:13:17 +08:00
/**
* Gets the font size of label.
*
* @return The font size.
*/
2014-05-27 10:54:47 +08:00
int 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
* Usage: Text *text = Text::create("Hello", "Arial", 20); //create a system font UIText
2014-05-14 09:55:55 +08:00
* text->setFontName("Marfelt"); // it will change the font to system font no matter the previous font type is TTF or system font
* text->setFontName("xxxx/xxx.ttf"); //it will change the font to TTF font no matter the previous font type is TTF or system font
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;
2014-05-13 17:45:15 +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.
*/
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-03-25 10:13:17 +08:00
* size is caculated by the real size of the text content.
*
* @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-03-25 10:13:17 +08:00
/** Sets text color.
*
* @param color Text color.
*/
void setTextColor(const Color4B color);
2015-03-25 10:13:17 +08:00
/** Gets text color.
*
* @return Text color.
*/
const Color4B& getTextColor() const;
/**
2015-03-25 10:13:17 +08:00
* Enable shadow for the label.
*
* @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.
*/
void enableShadow(const Color4B& shadowColor = Color4B::BLACK,const Size &offset = Size(2,-2), int blurRadius = 0);
/**
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.
*/
2014-06-09 14:37:56 +08:00
void enableOutline(const Color4B& outlineColor,int outlineSize = 1);
2015-03-25 10:13:17 +08:00
/** Only support for TTF.
*
* @param glowColor The color of glow.
*/
void enableGlow(const Color4B& glowColor);
2015-03-25 10:13:17 +08:00
/** Disable shadow/outline/glow rendering.
*/
void disableEffect();
CC_CONSTRUCTOR_ACCESS:
virtual bool init() override;
virtual bool init(const std::string& textContent,
const std::string& fontName,
int 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;
2014-06-06 16:08:31 +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;
int _fontSize;
float _onSelectedScaleOffset;
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__) */