axmol/cocos/ui/UIText.h

219 lines
6.5 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
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
*@js
*@lua NA
*/
class CC_GUI_DLL Text : public Widget
2014-03-11 17:13:54 +08:00
{
DECLARE_CLASS_GUI_INFO
public:
2014-05-13 17:45:15 +08:00
enum class Type
{
SYSTEM,
TTF
};
2014-03-11 17:13:54 +08:00
/**
* Default constructor
*/
Text();
/**
* Default destructor
*/
virtual ~Text();
/**
* Allocates and initializes.
*/
static Text* create();
/**
* create a Text object with textContent, fontName and fontSize
2014-05-14 09:37:21 +08:00
* 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
2014-05-14 09:55:55 +08:00
* Text *text = Text::create("Hello", "xxx\xxx.ttf", 20); //create a TTF font UIText
*/
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.
*
* @param text string value.
*/
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.
*
* @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,
* if you want to get the raw string length, you should call this->getString().size() instead
2014-03-11 17:13:54 +08:00
*
* @return string length.
*/
ssize_t getStringLength()const;
2014-03-11 17:13:54 +08:00
/**
* Sets the font size of label.
*
* @param size font size.
2014-03-11 17:13:54 +08:00
*/
void setFontSize(int 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
* @param name font name.
2014-03-11 17:13:54 +08:00
*/
void setFontName(const std::string& name);
2014-05-27 10:54:47 +08:00
const std::string& getFontName()const;
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.
*
* @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.
*
* @return touch scale enabled of label.
*/
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.
virtual const Size& getVirtualRendererSize() const override;
2014-03-11 17:13:54 +08:00
//override "getVirtualRenderer" method of widget.
virtual Node* getVirtualRenderer() override;
/**
* Returns the "class name" of widget.
*/
virtual std::string getDescription() const override;
void setTextAreaSize(const Size &size);
2014-05-27 10:54:47 +08:00
const Size& getTextAreaSize()const;
2014-03-11 17:13:54 +08:00
void setTextHorizontalAlignment(TextHAlignment alignment);
2014-05-27 10:54:47 +08:00
TextHAlignment getTextHorizontalAlignment()const;
2014-03-11 17:13:54 +08:00
void setTextVerticalAlignment(TextVAlignment alignment);
2014-05-27 10:54:47 +08:00
TextVAlignment getTextVerticalAlignment()const;
/**
* Enable shadow for the label
*
* @todo support blur for shadow effect
*/
void enableShadow(const Color4B& shadowColor = Color4B::BLACK,const Size &offset = Size(2,-2), int blurRadius = 0);
/**
* Enable outline for the label
* It only works on IOS and Android when you use System fonts
*/
2014-06-09 14:37:56 +08:00
void enableOutline(const Color4B& outlineColor,int outlineSize = 1);
/** only support for TTF */
void enableGlow(const Color4B& glowColor);
/** 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
virtual void updateFlippedX() override;
virtual void updateFlippedY() override;
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
#endif /* defined(__CocoGUI__Label__) */