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"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
namespace ui {
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
RICH_TEXT,
|
|
|
|
RICH_IMAGE,
|
|
|
|
RICH_CUSTOM
|
|
|
|
}RichElementType;
|
|
|
|
|
|
|
|
class RichElement : public Ref
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RichElement(){};
|
|
|
|
virtual ~RichElement(){};
|
|
|
|
bool init(int tag, const Color3B& color, GLubyte opacity);
|
|
|
|
protected:
|
|
|
|
RichElementType _type;
|
|
|
|
int _tag;
|
|
|
|
Color3B _color;
|
|
|
|
GLubyte _opacity;
|
|
|
|
friend class RichText;
|
|
|
|
};
|
|
|
|
|
|
|
|
class RichElementText : public RichElement
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RichElementText(){_type = RICH_TEXT;};
|
|
|
|
virtual ~RichElementText(){};
|
|
|
|
bool init(int tag, const Color3B& color, GLubyte opacity, const char* text, const char* fontName, float fontSize);
|
|
|
|
static RichElementText* create(int tag, const Color3B& color, GLubyte opacity, const char* text, const char* fontName, float fontSize);
|
|
|
|
protected:
|
|
|
|
std::string _text;
|
|
|
|
std::string _fontName;
|
|
|
|
float _fontSize;
|
|
|
|
friend class RichText;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class RichElementImage : public RichElement
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RichElementImage(){_type = RICH_IMAGE;};
|
|
|
|
virtual ~RichElementImage(){};
|
|
|
|
bool init(int tag, const Color3B& color, GLubyte opacity, const char* filePath);
|
|
|
|
static RichElementImage* create(int tag, const Color3B& color, GLubyte opacity, const char* filePath);
|
|
|
|
protected:
|
|
|
|
std::string _filePath;
|
|
|
|
Rect _textureRect;
|
|
|
|
int _textureType;
|
|
|
|
friend class RichText;
|
|
|
|
};
|
|
|
|
|
|
|
|
class RichElementCustomNode : public RichElement
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RichElementCustomNode(){_type = RICH_CUSTOM;};
|
|
|
|
virtual ~RichElementCustomNode(){CC_SAFE_RELEASE(_customNode);};
|
|
|
|
bool init(int tag, const Color3B& color, GLubyte opacity, Node* customNode);
|
|
|
|
static RichElementCustomNode* create(int tag, const Color3B& color, GLubyte opacity, Node* customNode);
|
|
|
|
protected:
|
|
|
|
Node* _customNode;
|
|
|
|
friend class RichText;
|
|
|
|
};
|
|
|
|
|
|
|
|
class RichText : public Widget
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RichText();
|
|
|
|
virtual ~RichText();
|
|
|
|
static RichText* create();
|
|
|
|
void insertElement(RichElement* element, int index);
|
|
|
|
void pushBackElement(RichElement* element);
|
|
|
|
void removeElement(int index);
|
|
|
|
void removeElement(RichElement* element);
|
2014-04-08 21:45:54 +08:00
|
|
|
virtual void visit(cocos2d::Renderer *renderer, const Matrix &parentTransform, bool parentTransformUpdated) override;
|
2014-03-11 17:13:54 +08:00
|
|
|
void setVerticalSpace(float space);
|
2014-04-15 16:47:29 +08:00
|
|
|
virtual void setAnchorPoint(const Vector2 &pt);
|
2014-04-17 14:08:25 +08:00
|
|
|
virtual const Size& getVirtualRendererSize() const override;
|
2014-03-11 17:13:54 +08:00
|
|
|
void formatText();
|
|
|
|
virtual void ignoreContentAdaptWithSize(bool ignore);
|
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;
|
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
protected:
|
|
|
|
virtual void initRenderer();
|
|
|
|
void pushToContainer(Node* renderer);
|
|
|
|
void handleTextRenderer(const char* text, const char* fontName, float fontSize, const Color3B& color, GLubyte opacity);
|
|
|
|
void handleImageRenderer(const char* fileParh, const Color3B& color, GLubyte opacity);
|
|
|
|
void handleCustomRenderer(Node* renderer);
|
|
|
|
void formarRenderers();
|
|
|
|
void addNewLine();
|
|
|
|
protected:
|
|
|
|
bool _formatTextDirty;
|
|
|
|
Vector<RichElement*> _richElements;
|
|
|
|
std::vector<Vector<Node*>*> _elementRenders;
|
|
|
|
float _leftSpaceWidth;
|
|
|
|
float _verticalSpace;
|
|
|
|
Node* _elementRenderersContainer;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
#endif /* defined(__UIRichText__) */
|