2014-03-11 17:13:54 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013 cocos2d-x.org
|
|
|
|
|
|
|
|
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 __UIRICHTEXT_H__
|
|
|
|
#define __UIRICHTEXT_H__
|
|
|
|
|
|
|
|
#include "ui/UIWidget.h"
|
2014-07-21 17:45:56 +08:00
|
|
|
#include "ui/GUIExport.h"
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
2015-03-25 18:17:58 +08:00
|
|
|
/**
|
|
|
|
* @addtogroup ui
|
|
|
|
* @{
|
|
|
|
*/
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
namespace ui {
|
|
|
|
|
2015-03-25 18:17:58 +08:00
|
|
|
/**
|
|
|
|
*@brief Rich text element base class.
|
|
|
|
* It defines the basic common properties for all rich text element.
|
|
|
|
*/
|
2014-07-21 17:45:56 +08:00
|
|
|
class CC_GUI_DLL RichElement : public Ref
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
public:
|
2015-03-25 18:17:58 +08:00
|
|
|
/**
|
|
|
|
*@brief Rich element type.
|
|
|
|
*/
|
2014-05-12 10:39:30 +08:00
|
|
|
enum class Type
|
|
|
|
{
|
|
|
|
TEXT,
|
|
|
|
IMAGE,
|
2015-09-28 13:33:17 +08:00
|
|
|
CUSTOM,
|
|
|
|
NEWLINE
|
2014-05-12 10:39:30 +08:00
|
|
|
};
|
2015-03-25 18:17:58 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Default constructor.
|
2015-03-28 16:02:47 +08:00
|
|
|
* @js ctor
|
2015-03-30 15:47:47 +08:00
|
|
|
* @lua new
|
2015-03-25 18:17:58 +08:00
|
|
|
*/
|
2014-03-11 17:13:54 +08:00
|
|
|
RichElement(){};
|
2015-03-25 18:17:58 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Default destructor.
|
2015-03-28 16:02:47 +08:00
|
|
|
* @js NA
|
2015-03-30 15:47:47 +08:00
|
|
|
* @lua NA
|
2015-03-25 18:17:58 +08:00
|
|
|
*/
|
2014-03-11 17:13:54 +08:00
|
|
|
virtual ~RichElement(){};
|
2015-03-25 18:17:58 +08:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Initialize a rich element with different arguments.
|
|
|
|
*
|
|
|
|
* @param tag A integer tag value.
|
|
|
|
* @param color A color in @see `Color3B`.
|
|
|
|
* @param opacity A opacity value in `GLubyte`.
|
|
|
|
* @return True if initialize success, false otherwise.
|
|
|
|
*/
|
2014-03-11 17:13:54 +08:00
|
|
|
bool init(int tag, const Color3B& color, GLubyte opacity);
|
|
|
|
protected:
|
2014-05-12 10:39:30 +08:00
|
|
|
Type _type;
|
2014-03-11 17:13:54 +08:00
|
|
|
int _tag;
|
|
|
|
Color3B _color;
|
|
|
|
GLubyte _opacity;
|
|
|
|
friend class RichText;
|
|
|
|
};
|
|
|
|
|
2015-03-25 18:17:58 +08:00
|
|
|
/**
|
|
|
|
*@brief Rich element for displaying text.
|
|
|
|
*/
|
2014-07-21 17:45:56 +08:00
|
|
|
class CC_GUI_DLL RichElementText : public RichElement
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
public:
|
2015-03-25 18:17:58 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
*@brief Default constructor.
|
2015-03-28 16:02:47 +08:00
|
|
|
* @js ctor
|
2015-03-30 15:47:47 +08:00
|
|
|
* @lua new
|
2015-03-25 18:17:58 +08:00
|
|
|
*/
|
2016-01-20 12:41:31 +08:00
|
|
|
RichElementText()
|
|
|
|
{_type = Type::TEXT;};
|
2015-03-25 18:17:58 +08:00
|
|
|
|
2016-01-20 12:41:31 +08:00
|
|
|
enum {
|
|
|
|
ITALICS_FLAG = 1 << 0,
|
|
|
|
BOLD_FLAG = 1 << 1,
|
|
|
|
UNDERLINE_FLAG = 1 << 2,
|
|
|
|
STRIKETHROUGH_FLAG = 1 << 3,
|
|
|
|
URL_FLAG = 1 << 4
|
|
|
|
};
|
2015-03-25 18:17:58 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
*@brief Default destructor.
|
2015-03-28 16:02:47 +08:00
|
|
|
* @js NA
|
2015-03-30 15:47:47 +08:00
|
|
|
* @lua NA
|
2015-03-25 18:17:58 +08:00
|
|
|
*/
|
2014-03-11 17:13:54 +08:00
|
|
|
virtual ~RichElementText(){};
|
2015-03-25 18:17:58 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Initialize a RichElementText with various arguments.
|
|
|
|
*
|
|
|
|
* @param tag A integer tag value.
|
|
|
|
* @param color A color in Color3B.
|
|
|
|
* @param opacity A opacity in GLubyte.
|
|
|
|
* @param text Content string.
|
|
|
|
* @param fontName Content font name.
|
|
|
|
* @param fontSize Content font size.
|
2016-01-20 12:41:31 +08:00
|
|
|
* @param flags: italics, bold, underline or strikethrough
|
2015-12-21 07:21:45 +08:00
|
|
|
* @return True if initialize success, false otherwise.
|
2015-03-25 18:17:58 +08:00
|
|
|
*/
|
2016-01-20 12:41:31 +08:00
|
|
|
bool init(int tag, const Color3B& color, GLubyte opacity, const std::string& text, const std::string& fontName, float fontSize, uint32_t flags, const std::string& url);
|
2015-03-25 18:17:58 +08:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Create a RichElementText with various arguments.
|
|
|
|
*
|
|
|
|
* @param tag A integer tag value.
|
|
|
|
* @param color A color in Color3B.
|
|
|
|
* @param opacity A opacity in GLubyte.
|
|
|
|
* @param text Content string.
|
|
|
|
* @param fontName Content font name.
|
|
|
|
* @param fontSize Content font size.
|
2016-01-20 12:41:31 +08:00
|
|
|
* @param flags: italics, bold, underline or strikethrough
|
2015-03-25 18:17:58 +08:00
|
|
|
* @return RichElementText instance.
|
|
|
|
*/
|
2016-01-20 12:41:31 +08:00
|
|
|
static RichElementText* create(int tag, const Color3B& color, GLubyte opacity, const std::string& text,
|
|
|
|
const std::string& fontName, float fontSize, uint32_t flags=0, const std::string& url="");
|
2014-03-11 17:13:54 +08:00
|
|
|
protected:
|
|
|
|
std::string _text;
|
|
|
|
std::string _fontName;
|
|
|
|
float _fontSize;
|
2016-01-20 12:41:31 +08:00
|
|
|
uint32_t _flags;
|
|
|
|
std::string _url;
|
2014-03-11 17:13:54 +08:00
|
|
|
friend class RichText;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2015-03-25 18:17:58 +08:00
|
|
|
/**
|
|
|
|
*@brief Rich element for displaying images.
|
|
|
|
*/
|
2014-07-21 17:45:56 +08:00
|
|
|
class CC_GUI_DLL RichElementImage : public RichElement
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
public:
|
2015-03-25 18:17:58 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Default constructor.
|
2015-03-28 16:02:47 +08:00
|
|
|
* @js ctor
|
2015-03-30 15:47:47 +08:00
|
|
|
* @lua new
|
2015-03-25 18:17:58 +08:00
|
|
|
*
|
|
|
|
*/
|
2014-05-12 10:39:30 +08:00
|
|
|
RichElementImage(){_type = Type::IMAGE;};
|
2015-03-25 18:17:58 +08:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Default destructor.
|
2015-03-28 16:02:47 +08:00
|
|
|
* @js NA
|
2015-03-30 15:47:47 +08:00
|
|
|
* @lua NA
|
2015-03-25 18:17:58 +08:00
|
|
|
*/
|
2014-03-11 17:13:54 +08:00
|
|
|
virtual ~RichElementImage(){};
|
2015-03-25 18:17:58 +08:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Initialize a RichElementImage with various arguments.
|
|
|
|
*
|
|
|
|
* @param tag A integer tag value.
|
|
|
|
* @param color A color in Color3B.
|
|
|
|
* @param opacity A opacity in GLubyte.
|
|
|
|
* @param filePath A image file name.
|
|
|
|
* @return True if initialize success, false otherwise.
|
|
|
|
*/
|
2014-05-12 14:59:00 +08:00
|
|
|
bool init(int tag, const Color3B& color, GLubyte opacity, const std::string& filePath);
|
2015-03-25 18:17:58 +08:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Create a RichElementImage with various arguments.
|
|
|
|
*
|
|
|
|
* @param tag A integer tag value.
|
|
|
|
* @param color A color in Color3B.
|
|
|
|
* @param opacity A opacity in GLubyte.
|
|
|
|
* @param filePath A image file name.
|
|
|
|
* @return A RichElementImage instance.
|
|
|
|
*/
|
2014-05-12 14:59:00 +08:00
|
|
|
static RichElementImage* create(int tag, const Color3B& color, GLubyte opacity, const std::string& filePath);
|
2016-01-20 12:41:31 +08:00
|
|
|
|
|
|
|
void setWidth(int width);
|
|
|
|
void setHeight(int height);
|
2014-03-11 17:13:54 +08:00
|
|
|
protected:
|
|
|
|
std::string _filePath;
|
|
|
|
Rect _textureRect;
|
|
|
|
int _textureType;
|
|
|
|
friend class RichText;
|
2016-01-20 12:41:31 +08:00
|
|
|
int _width;
|
|
|
|
int _height;
|
2014-03-11 17:13:54 +08:00
|
|
|
};
|
|
|
|
|
2015-03-25 18:17:58 +08:00
|
|
|
/**
|
|
|
|
*@brief Rich element for displaying custom node type.
|
|
|
|
*/
|
2014-07-21 17:45:56 +08:00
|
|
|
class CC_GUI_DLL RichElementCustomNode : public RichElement
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
public:
|
2015-03-25 18:17:58 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Default constructor.
|
2015-03-28 16:02:47 +08:00
|
|
|
* @js ctor
|
2015-03-30 15:47:47 +08:00
|
|
|
* @lua new
|
2015-03-25 18:17:58 +08:00
|
|
|
*/
|
2014-05-12 10:39:30 +08:00
|
|
|
RichElementCustomNode(){_type = Type::CUSTOM;};
|
2015-03-25 18:17:58 +08:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Default destructor.
|
2015-03-28 16:02:47 +08:00
|
|
|
* @js NA
|
2015-03-30 15:47:47 +08:00
|
|
|
* @lua NA
|
2015-03-25 18:17:58 +08:00
|
|
|
*/
|
2014-03-11 17:13:54 +08:00
|
|
|
virtual ~RichElementCustomNode(){CC_SAFE_RELEASE(_customNode);};
|
2015-03-25 18:17:58 +08:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Initialize a RichElementCustomNode with various arguments.
|
|
|
|
*
|
|
|
|
* @param tag A integer tag value.
|
|
|
|
* @param color A color in Color3B.
|
|
|
|
* @param opacity A opacity in GLubyte.
|
|
|
|
* @param customNode A custom node pointer.
|
|
|
|
* @return True if initialize success, false otherwise.
|
|
|
|
*/
|
2014-03-11 17:13:54 +08:00
|
|
|
bool init(int tag, const Color3B& color, GLubyte opacity, Node* customNode);
|
2015-03-25 18:17:58 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Create a RichElementCustomNode with various arguments.
|
|
|
|
*
|
|
|
|
* @param tag A integer tag value.
|
|
|
|
* @param color A color in Color3B.
|
|
|
|
* @param opacity A opacity in GLubyte.
|
|
|
|
* @param customNode A custom node pointer.
|
|
|
|
* @return A RichElementCustomNode instance.
|
|
|
|
*/
|
2014-03-11 17:13:54 +08:00
|
|
|
static RichElementCustomNode* create(int tag, const Color3B& color, GLubyte opacity, Node* customNode);
|
|
|
|
protected:
|
|
|
|
Node* _customNode;
|
|
|
|
friend class RichText;
|
|
|
|
};
|
|
|
|
|
2015-09-28 13:33:17 +08:00
|
|
|
/**
|
|
|
|
*@brief Rich element for new line.
|
|
|
|
*/
|
|
|
|
class CC_GUI_DLL RichElementNewLine : public RichElement
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Default constructor.
|
|
|
|
* @js ctor
|
|
|
|
* @lua new
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
RichElementNewLine(){_type = Type::NEWLINE;};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Default destructor.
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
|
|
|
virtual ~RichElementNewLine(){};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Create a RichElementNewLine with various arguments.
|
|
|
|
*
|
|
|
|
* @param tag A integer tag value.
|
|
|
|
* @param color A color in Color3B.
|
|
|
|
* @param opacity A opacity in GLubyte.
|
|
|
|
* @return A RichElementNewLine instance.
|
|
|
|
*/
|
|
|
|
static RichElementNewLine* create(int tag, const Color3B& color, GLubyte opacity);
|
|
|
|
protected:
|
|
|
|
friend class RichText;
|
|
|
|
};
|
|
|
|
|
2015-03-25 18:17:58 +08:00
|
|
|
/**
|
|
|
|
*@brief A container for displaying various RichElements.
|
|
|
|
* We could use it to display texts with images easily.
|
|
|
|
*/
|
2014-07-21 17:45:56 +08:00
|
|
|
class CC_GUI_DLL RichText : public Widget
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
public:
|
2015-03-25 18:17:58 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Default constructor.
|
2015-03-28 16:02:47 +08:00
|
|
|
* @js ctor
|
2015-03-30 15:47:47 +08:00
|
|
|
* @lua new
|
2015-03-25 18:17:58 +08:00
|
|
|
*/
|
2014-03-11 17:13:54 +08:00
|
|
|
RichText();
|
2015-03-25 18:17:58 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Default destructor.
|
2015-03-28 16:02:47 +08:00
|
|
|
* @js NA
|
2015-03-30 15:47:47 +08:00
|
|
|
* @lua NA
|
2015-03-25 18:17:58 +08:00
|
|
|
*/
|
2014-03-11 17:13:54 +08:00
|
|
|
virtual ~RichText();
|
2015-03-25 18:17:58 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Create a empty RichText.
|
|
|
|
*
|
|
|
|
* @return RichText instance.
|
|
|
|
*/
|
2014-03-11 17:13:54 +08:00
|
|
|
static RichText* create();
|
2016-01-20 12:41:31 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Create a RichText from an XML
|
|
|
|
*
|
|
|
|
* @return RichText instance.
|
|
|
|
*/
|
|
|
|
static RichText* createWithXML(const std::string& xml);
|
|
|
|
|
2015-03-25 18:17:58 +08:00
|
|
|
/**
|
|
|
|
* @brief Insert a RichElement at a given index.
|
|
|
|
*
|
|
|
|
* @param element A RichElement type.
|
|
|
|
* @param index A given index.
|
|
|
|
*/
|
2014-03-11 17:13:54 +08:00
|
|
|
void insertElement(RichElement* element, int index);
|
2015-03-25 18:17:58 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Add a RichElement at the end of RichText.
|
|
|
|
*
|
|
|
|
* @param element A RichElement instance.
|
|
|
|
*/
|
2014-03-11 17:13:54 +08:00
|
|
|
void pushBackElement(RichElement* element);
|
2015-03-25 18:17:58 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Remove a RichElement at a given index.
|
|
|
|
*
|
|
|
|
* @param index A integer index value.
|
|
|
|
*/
|
2014-03-11 17:13:54 +08:00
|
|
|
void removeElement(int index);
|
2015-03-25 18:17:58 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Remove specific RichElement.
|
|
|
|
*
|
|
|
|
* @param element A RichElement type.
|
|
|
|
*/
|
2014-03-11 17:13:54 +08:00
|
|
|
void removeElement(RichElement* element);
|
2015-03-25 18:17:58 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set vertical space between each RichElement.
|
|
|
|
*
|
|
|
|
* @param space Point in float.
|
|
|
|
*/
|
2014-03-11 17:13:54 +08:00
|
|
|
void setVerticalSpace(float space);
|
2015-03-25 18:17:58 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Rearrange all RichElement in the RichText.
|
|
|
|
* It's usually called internally.
|
|
|
|
*/
|
|
|
|
void formatText();
|
|
|
|
|
|
|
|
//override functions.
|
2015-03-15 02:33:15 +08:00
|
|
|
virtual void ignoreContentAdaptWithSize(bool ignore) override;
|
2014-03-24 15:25:44 +08:00
|
|
|
virtual std::string getDescription() const override;
|
2014-04-19 00:30:34 +08:00
|
|
|
|
2014-03-21 13:44:29 +08:00
|
|
|
CC_CONSTRUCTOR_ACCESS:
|
|
|
|
virtual bool init() override;
|
2016-01-20 12:41:31 +08:00
|
|
|
|
|
|
|
bool initWithXML(const std::string& xml);
|
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
protected:
|
2015-03-15 02:33:15 +08:00
|
|
|
virtual void adaptRenderers() override;
|
2014-07-24 11:51:47 +08:00
|
|
|
|
2015-03-15 02:33:15 +08:00
|
|
|
virtual void initRenderer() override;
|
2014-03-11 17:13:54 +08:00
|
|
|
void pushToContainer(Node* renderer);
|
2016-01-20 12:41:31 +08:00
|
|
|
void handleTextRenderer(const std::string& text, const std::string& fontName, float fontSize, const Color3B& color, GLubyte opacity, uint32_t flags, const std::string& url="");
|
|
|
|
void handleImageRenderer(const std::string& fileParh, const Color3B& color, GLubyte opacity, int width, int height);
|
2014-03-11 17:13:54 +08:00
|
|
|
void handleCustomRenderer(Node* renderer);
|
|
|
|
void formarRenderers();
|
|
|
|
void addNewLine();
|
|
|
|
protected:
|
|
|
|
bool _formatTextDirty;
|
|
|
|
Vector<RichElement*> _richElements;
|
|
|
|
std::vector<Vector<Node*>*> _elementRenders;
|
|
|
|
float _leftSpaceWidth;
|
|
|
|
float _verticalSpace;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-03-25 18:17:58 +08:00
|
|
|
// end of ui group
|
|
|
|
/// @}
|
2014-03-11 17:13:54 +08:00
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
#endif /* defined(__UIRichText__) */
|