defaults and append tag support

This commit is contained in:
Yukio Murakami 2016-03-07 10:50:47 +09:00
parent 5d3b654a74
commit a8ddbdc12c
6 changed files with 1863 additions and 390 deletions

File diff suppressed because it is too large Load Diff

View File

@ -27,6 +27,7 @@
#include "ui/UIWidget.h"
#include "ui/GUIExport.h"
#include "base/CCValue.h"
NS_CC_BEGIN
/**
@ -50,10 +51,10 @@ public:
*/
enum class Type
{
TEXT,
IMAGE,
CUSTOM,
NEWLINE
TEXT, /*!< RichElementText */
IMAGE, /*!< RichElementImage */
CUSTOM, /*!< RichElementCustomNode */
NEWLINE /*!< RichElementNewLine */
};
/**
@ -80,11 +81,14 @@ public:
* @return True if initialize success, false otherwise.
*/
bool init(int tag, const Color3B& color, GLubyte opacity);
bool equalType(Type type);
void setColor(const Color3B& color);
protected:
Type _type;
int _tag;
Color3B _color;
GLubyte _opacity;
Type _type; /*!< Rich element type. */
int _tag; /*!< A integer tag value. */
Color3B _color; /*!< A color in `Color3B`. */
GLubyte _opacity; /*!< A opacity value in `GLubyte`. */
friend class RichText;
};
@ -104,11 +108,14 @@ public:
{_type = Type::TEXT;};
enum {
ITALICS_FLAG = 1 << 0,
BOLD_FLAG = 1 << 1,
UNDERLINE_FLAG = 1 << 2,
STRIKETHROUGH_FLAG = 1 << 3,
URL_FLAG = 1 << 4
ITALICS_FLAG = 1 << 0, /*!< italic text */
BOLD_FLAG = 1 << 1, /*!< bold text */
UNDERLINE_FLAG = 1 << 2, /*!< underline */
STRIKETHROUGH_FLAG = 1 << 3, /*!< strikethrough */
URL_FLAG = 1 << 4, /*!< url of anchor */
OUTLINE_FLAG = 1 << 5, /*!< outline effect */
SHADOW_FLAG = 1 << 6, /*!< shadow effect */
GLOW_FLAG = 1 << 7 /*!< glow effect */
};
/**
@ -127,11 +134,21 @@ public:
* @param text Content string.
* @param fontName Content font name.
* @param fontSize Content font size.
* @param flags: italics, bold, underline or strikethrough
* @param flags: italics, bold, underline, strikethrough, url, outline, shadow or glow
* @param url uniform resource locator
* @param outlineColor the color of the outline
* @param outlineSize the outline effect size value
* @param shadowColor the shadow effect color value
* @param shadowOffset shadow effect offset value
* @param shadowBlurRadius the shadow effect blur radius
* @param glowColor glow color
* @return True if initialize success, false otherwise.
*/
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);
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,
const Color3B& outlineColor = Color3B::WHITE, int outlineSize = -1,
const Color3B& shadowColor = Color3B::BLACK, const cocos2d::Size& shadowOffset = Size(2.0, -2.0), int shadowBlurRadius = 0,
const Color3B& glowColor = Color3B::WHITE);
/**
* @brief Create a RichElementText with various arguments.
@ -142,19 +159,34 @@ public:
* @param text Content string.
* @param fontName Content font name.
* @param fontSize Content font size.
* @param flags: italics, bold, underline or strikethrough
* @param flags: italics, bold, underline, strikethrough, url, outline, shadow or glow
* @param url uniform resource locator
* @param outlineColor the color of the outline
* @param outlineSize the outline effect size value
* @param shadowColor the shadow effect color value
* @param shadowOffset shadow effect offset value
* @param shadowBlurRadius the shadow effect blur radius
* @param glowColor glow color
* @return RichElementText instance.
*/
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="");
const std::string& fontName, float fontSize, uint32_t flags=0, const std::string& url="",
const Color3B& outlineColor = Color3B::WHITE, int outlineSize = -1,
const Color3B& shadowColor = Color3B::BLACK, const cocos2d::Size& shadowOffset = Size(2.0, -2.0), int shadowBlurRadius = 0,
const Color3B& glowColor = Color3B::WHITE);
protected:
std::string _text;
std::string _fontName;
float _fontSize;
uint32_t _flags;
std::string _url;
Color3B _outlineColor; /*!< the color of the outline */
int _outlineSize; /*!< the outline effect size value */
Color3B _shadowColor; /*!< the shadow effect color value */
cocos2d::Size _shadowOffset; /*!< shadow effect offset value */
int _shadowBlurRadius; /*!< the shadow effect blur radius */
Color3B _glowColor; /*!< attributes of glow tag */
friend class RichText;
};
/**
@ -188,9 +220,10 @@ public:
* @param color A color in Color3B.
* @param opacity A opacity in GLubyte.
* @param filePath A image file name.
* @param url uniform resource locator
* @return True if initialize success, false otherwise.
*/
bool init(int tag, const Color3B& color, GLubyte opacity, const std::string& filePath);
bool init(int tag, const Color3B& color, GLubyte opacity, const std::string& filePath, const std::string& url = "");
/**
@ -200,12 +233,14 @@ public:
* @param color A color in Color3B.
* @param opacity A opacity in GLubyte.
* @param filePath A image file name.
* @param url uniform resource locator
* @return A RichElementImage instance.
*/
static RichElementImage* create(int tag, const Color3B& color, GLubyte opacity, const std::string& filePath);
static RichElementImage* create(int tag, const Color3B& color, GLubyte opacity, const std::string& filePath, const std::string& url = "");
void setWidth(int width);
void setHeight(int height);
void setUrl(const std::string& url);
protected:
std::string _filePath;
Rect _textureRect;
@ -213,6 +248,7 @@ protected:
friend class RichText;
int _width;
int _height;
std::string _url; /*!< attributes of anchor tag */
};
/**
@ -312,6 +348,58 @@ public:
WRAP_PER_CHAR
};
/**
* @brief call to open a resource specified by a URL
* @param url a URL
*/
typedef std::function<void(const std::string& url)> OpenUrlHandler;
/**
* @brief called on the specified tag
* @param tagAttrValueMap the attributes of a tag
* @result text attributes and RichElement
*/
typedef std::function<std::pair<ValueMap, RichElement*>(const ValueMap& tagAttrValueMap)> VisitEnterHandler;
static const std::string KEY_VERTICAL_SPACE; /*!< key of vertical space */
static const std::string KEY_WRAP_MODE; /*!< key of per word, or per char */
static const std::string KEY_FONT_COLOR_STRING; /*!< key of font color */
static const std::string KEY_FONT_SIZE; /*!< key of font size */
static const std::string KEY_FONT_SMALL; /*!< key of font size small */
static const std::string KEY_FONT_BIG; /*!< key of font size big */
static const std::string KEY_FONT_FACE; /*!< key of font name */
static const std::string KEY_TEXT_BOLD; /*!< key of text bold */
static const std::string KEY_TEXT_ITALIC; /*!< key of text italic */
static const std::string KEY_TEXT_LINE; /*!< key of line style */
static const std::string VALUE_TEXT_LINE_NONE; /*!< value of none */
static const std::string VALUE_TEXT_LINE_DEL; /*!< value of strikethrough line */
static const std::string VALUE_TEXT_LINE_UNDER; /*!< value of underline */
static const std::string KEY_TEXT_STYLE; /*!< key of effect style */
static const std::string VALUE_TEXT_STYLE_NONE; /*!< value of none */
static const std::string VALUE_TEXT_STYLE_OUTLINE; /*!< value of outline */
static const std::string VALUE_TEXT_STYLE_SHADOW; /*!< value of shadow */
static const std::string VALUE_TEXT_STYLE_GLOW; /*!< value of glow */
static const std::string KEY_TEXT_OUTLINE_COLOR; /*!< key of outline color */
static const std::string KEY_TEXT_OUTLINE_SIZE; /*!< key of outline size */
static const std::string KEY_TEXT_SHADOW_COLOR; /*!< key of shadow color */
static const std::string KEY_TEXT_SHADOW_OFFSET_WIDTH; /*!< key of shadow offset (width) */
static const std::string KEY_TEXT_SHADOW_OFFSET_HEIGHT; /*!< key of shadow offset (height) */
static const std::string KEY_TEXT_SHADOW_BLUR_RADIUS; /*!< key of shadow blur radius */
static const std::string KEY_TEXT_GLOW_COLOR; /*!< key of glow color */
static const std::string KEY_URL; /*!< key of url */
static const std::string KEY_ANCHOR_FONT_COLOR_STRING; /*!< key of font color of anchor tag */
static const std::string KEY_ANCHOR_TEXT_BOLD; /*!< key of text bold of anchor tag */
static const std::string KEY_ANCHOR_TEXT_ITALIC; /*!< key of text italic of anchor tag */
static const std::string KEY_ANCHOR_TEXT_LINE; /*!< key of line style of anchor tag */
static const std::string KEY_ANCHOR_TEXT_STYLE; /*!< key of effect style of anchor tag */
static const std::string KEY_ANCHOR_TEXT_OUTLINE_COLOR; /*!< key of outline color of anchor tag */
static const std::string KEY_ANCHOR_TEXT_OUTLINE_SIZE; /*!< key of outline size of anchor tag */
static const std::string KEY_ANCHOR_TEXT_SHADOW_COLOR; /*!< key of shadow color of anchor tag */
static const std::string KEY_ANCHOR_TEXT_SHADOW_OFFSET_WIDTH; /*!< key of shadow offset (width) of anchor tag */
static const std::string KEY_ANCHOR_TEXT_SHADOW_OFFSET_HEIGHT; /*!< key of shadow offset (height) of anchor tag */
static const std::string KEY_ANCHOR_TEXT_SHADOW_BLUR_RADIUS; /*!< key of shadow blur radius of anchor tag */
static const std::string KEY_ANCHOR_TEXT_GLOW_COLOR; /*!< key of glow color of anchor tag */
/**
* @brief Default constructor.
* @js ctor
@ -338,7 +426,7 @@ public:
*
* @return RichText instance.
*/
static RichText* createWithXML(const std::string& xml);
static RichText* createWithXML(const std::string& xml, const ValueMap& defaults = ValueMap(), const OpenUrlHandler& handleOpenUrl = nullptr);
/**
* @brief Insert a RichElement at a given index.
@ -386,25 +474,86 @@ public:
virtual void ignoreContentAdaptWithSize(bool ignore) override;
virtual std::string getDescription() const override;
/** @brief sets the wrapping mode: WRAP_PER_CHAR or WRAP_PER_WORD
*/
void setWrapMode(WrapMode wrapMode);
void setWrapMode(WrapMode wrapMode); /*!< sets the wrapping mode: WRAP_PER_CHAR or WRAP_PER_WORD */
WrapMode getWrapMode() const; /*!< returns the current wrapping mode */
void setFontColor(const std::string& color); /*!< Set the font color. @param color the #RRGGBB hexadecimal notation. */
std::string getFontColor(); /*!< return the current font color */
Color3B getFontColor3B(); /*!< return the current font color */
void setFontSize(float size); /*!< Set the font size. @param size the font size. */
float getFontSize(); /*!< return the current font size */
void setFontFace(const std::string& face); /*!< Set the font face. @param face the font face. */
std::string getFontFace(); /*!< return the current font face */
void setAnchorFontColor(const std::string& color); /*!< Set the font color of a-tag. @param face the font color. */
std::string getAnchorFontColor(); /*!< return the current font color of a-tag */
cocos2d::Color3B getAnchorFontColor3B(); /*!< return the current font color of a-tag */
void setAnchorTextBold(bool enable); /*!< enable bold text of a-tag */
bool isAnchorTextBoldEnabled(); /*!< valid style is bold text of a-tag? */
void setAnchorTextItalic(bool enable); /*!< enable italic text of a-tag */
bool isAnchorTextItalicEnabled(); /*!< valid style is italic text of a-tag? */
void setAnchorTextDel(bool enable); /*!< enable the strikethrough of a-tag */
bool isAnchorTextDelEnabled(); /*!< valid strikethrough of a-tag? */
void setAnchorTextUnderline(bool enable); /*!< enable the underline of a-tag */
bool isAnchorTextUnderlineEnabled(); /*!< valid underline of a-tag? */
/** @breif enable the outline of a-tag */
void setAnchorTextOutline(bool enable, const Color3B& outlineColor = Color3B::WHITE, int outlineSize = -1);
bool isAnchorTextOutlineEnabled(); /*!< valid outline of a-tag? */
Color3B getAnchorTextOutlineColor3B(); /*!< return the current text outline color of a-tag */
int getAnchorTextOutlineSize(); /*!< return the current text outline size of a-tag */
/** @breif enable the shadow of a-tag */
void setAnchorTextShadow(bool enable, const Color3B& shadowColor = Color3B::BLACK, const Size& offset = Size(2.0, -2.0), int blurRadius = 0);
bool isAnchorTextShadowEnabled(); /*!< valid shadow of a-tag? */
Color3B getAnchorTextShadowColor3B(); /*!< return the current text shadow color of a-tag */
Size getAnchorTextShadowOffset(); /*!< return the current text shadow offset of a-tag */
int getAnchorTextShadowBlurRadius(); /*!< return the current text shadow blur radius of a-tag */
void setAnchorTextGlow(bool enable, const Color3B& glowColor = Color3B::WHITE); /*!< enable the glow of a-tag */
bool isAnchorTextGlowEnabled(); /*!< valid glow of a-tag? */
Color3B getAnchorTextGlowColor3B(); /*!< return the current text glow color of a-tag */
void setDefaults(const ValueMap& defaults); /*!< set the default values */
ValueMap getDefaults() const; /*!< returns the default values */
/** @brief returns the current wrapping mode */
WrapMode getWrapMode() const;
cocos2d::Color3B color3BWithString(const std::string& color); /*!< convert a color string into a Color3B. */
std::string stringWithColor3B(const cocos2d::Color3B& color3b); /*!< convert a Color3B into a color string. */
std::string stringWithColor4B(const cocos2d::Color4B& color4b); /*!< convert a Color4B into a color string. */
/**
* @brief add a callback to own tag.
* @param tag tag's name
* @param isFontElement use attributes of text tag
* @param handleVisitEnter callback
*/
static void setTagDescription(const std::string& tag, bool isFontElement, VisitEnterHandler handleVisitEnter);
/**
* @brief remove a callback to own tag.
* @param tag tag's name
*/
static void removeTagDescription(const std::string& tag);
void openUrl(const std::string& url);
/**
* @brief Asks the callback to open a resource specified by a URL.
* @discussion If you set up your own URL in the anchor tag, it is used to intercept the URL open.
* @param handleOpenUrl the callback
*/
void setOpenUrlHandler(const OpenUrlHandler& handleOpenUrl);
CC_CONSTRUCTOR_ACCESS:
virtual bool init() override;
bool initWithXML(const std::string& xml);
bool initWithXML(const std::string& xml, const ValueMap& defaults = ValueMap(), const OpenUrlHandler& handleOpenUrl = nullptr);
protected:
virtual void adaptRenderers() override;
virtual void initRenderer() override;
void pushToContainer(Node* renderer);
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);
void handleTextRenderer(const std::string& text, const std::string& fontName, float fontSize, const Color3B& color,
GLubyte opacity, uint32_t flags, const std::string& url="",
const Color3B& outlineColor = Color3B::WHITE, int outlineSize = -1,
const Color3B& shadowColor = Color3B::BLACK, const cocos2d::Size& shadowOffset = Size(2.0, -2.0), int shadowBlurRadius = 0,
const Color3B& glowColor = Color3B::WHITE);
void handleImageRenderer(const std::string& fileParh, const Color3B& color, GLubyte opacity, int width, int height, const std::string url);
void handleCustomRenderer(Node* renderer);
void formarRenderers();
void addNewLine();
@ -415,10 +564,9 @@ protected:
Vector<RichElement*> _richElements;
std::vector<Vector<Node*>*> _elementRenders;
float _leftSpaceWidth;
float _verticalSpace;
// per word, or per char
WrapMode _wrapMode;
ValueMap _defaults; /*!< default values */
OpenUrlHandler _handleOpenUrl; /*!< the callback for open URL */
};
}

View File

@ -16,9 +16,14 @@ UIRichTextTests::UIRichTextTests()
ADD_TEST_CASE(UIRichTextXMLSUIB3);
ADD_TEST_CASE(UIRichTextXMLImg);
ADD_TEST_CASE(UIRichTextXMLUrl);
ADD_TEST_CASE(UIRichTextXMLUrlImg);
ADD_TEST_CASE(UIRichTextXMLFace);
ADD_TEST_CASE(UIRichTextXMLBR);
ADD_TEST_CASE(UIRichTextXMLInvalid);
ADD_TEST_CASE(UIRichTextXMLOutline);
ADD_TEST_CASE(UIRichTextXMLShadow);
ADD_TEST_CASE(UIRichTextXMLGlow);
ADD_TEST_CASE(UIRichTextXMLExtend);
}
@ -826,6 +831,91 @@ void UIRichTextXMLUrl::switchWrapMode(Ref *pSender, Widget::TouchEventType type)
}
}
//
// UIRichTextXMLUrlImg
//
bool UIRichTextXMLUrlImg::init()
{
if (UIScene::init())
{
Size widgetSize = _widget->getContentSize();
// Add the alert
Text *alert = Text::create("RichText", "fonts/Marker Felt.ttf", 30);
alert->setColor(Color3B(159, 168, 176));
alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.125));
_widget->addChild(alert);
Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
button->setTouchEnabled(true);
button->setTitleText("switch");
button->setPosition(Vec2(widgetSize.width * 1 / 3, widgetSize.height / 2.0f + button->getContentSize().height * 2.5));
button->addTouchEventListener(CC_CALLBACK_2(UIRichTextXMLUrlImg::touchEvent, this));
button->setLocalZOrder(10);
_widget->addChild(button);
Button* button2 = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
button2->setTouchEnabled(true);
button2->setTitleText("wrap mode");
button2->setPosition(Vec2(widgetSize.width * 2 / 3, widgetSize.height / 2.0f + button2->getContentSize().height * 2.5));
button2->addTouchEventListener(CC_CALLBACK_2(UIRichTextXMLUrlImg::switchWrapMode, this));
button2->setLocalZOrder(10);
_widget->addChild(button2);
// RichText
_richText = RichText::createWithXML("And this link will redirect you to google: <a href='http://www.google.com'><img src=\"cocosui/ccicon.png\" height=\"48\" width=\"48\" /></a>");
_richText->ignoreContentAdaptWithSize(false);
_richText->setContentSize(Size(100, 100));
_richText->setPosition(Vec2(widgetSize.width / 2, widgetSize.height / 2));
_richText->setLocalZOrder(10);
_widget->addChild(_richText);
// test remove all children, this call won't effect the test
_richText->removeAllChildren();
return true;
}
return false;
}
void UIRichTextXMLUrlImg::touchEvent(Ref *pSender, Widget::TouchEventType type)
{
switch (type)
{
case Widget::TouchEventType::ENDED:
{
if (_richText->isIgnoreContentAdaptWithSize())
{
_richText->ignoreContentAdaptWithSize(false);
_richText->setContentSize(Size(100, 100));
}
else
{
_richText->ignoreContentAdaptWithSize(true);
}
}
break;
default:
break;
}
}
void UIRichTextXMLUrlImg::switchWrapMode(Ref *pSender, Widget::TouchEventType type)
{
if (type == Widget::TouchEventType::ENDED)
{
auto wrapMode = _richText->getWrapMode();
wrapMode = (wrapMode == RichText::WRAP_PER_WORD) ? RichText::WRAP_PER_CHAR : RichText::WRAP_PER_WORD;
_richText->setWrapMode(wrapMode);
}
}
//
// UIRichTextXMLFace
//
@ -1033,3 +1123,378 @@ bool UIRichTextXMLInvalid::init()
return false;
}
//
// UIRichTextXMLOutline
//
bool UIRichTextXMLOutline::init()
{
if (UIScene::init())
{
Size widgetSize = _widget->getContentSize();
// Add the alert
Text *alert = Text::create("Outline", "fonts/Marker Felt.ttf", 30);
alert->setColor(Color3B(159, 168, 176));
alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.125));
_widget->addChild(alert);
Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
button->setTouchEnabled(true);
button->setTitleText("switch");
button->setPosition(Vec2(widgetSize.width * 1 / 3, widgetSize.height / 2.0f + button->getContentSize().height * 2.5));
button->addTouchEventListener(CC_CALLBACK_2(UIRichTextXMLOutline::touchEvent, this));
button->setLocalZOrder(10);
_widget->addChild(button);
Button* button2 = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
button2->setTouchEnabled(true);
button2->setTitleText("wrap mode");
button2->setPosition(Vec2(widgetSize.width * 2 / 3, widgetSize.height / 2.0f + button2->getContentSize().height * 2.5));
button2->addTouchEventListener(CC_CALLBACK_2(UIRichTextXMLOutline::switchWrapMode, this));
button2->setLocalZOrder(10);
_widget->addChild(button2);
// RichText
_richText = RichText::createWithXML("<font face='fonts/Marker Felt.ttf' size=\"24\"><outline color=\"#D2B48C\" size=\"2\">OUTLINE</outline></font>");
_richText->ignoreContentAdaptWithSize(false);
_richText->setContentSize(Size(100, 100));
_richText->setPosition(Vec2(widgetSize.width / 2, widgetSize.height / 2));
_richText->setLocalZOrder(10);
_widget->addChild(_richText);
// test remove all children, this call won't effect the test
_richText->removeAllChildren();
return true;
}
return false;
}
void UIRichTextXMLOutline::touchEvent(Ref *pSender, Widget::TouchEventType type)
{
switch (type)
{
case Widget::TouchEventType::ENDED:
{
if (_richText->isIgnoreContentAdaptWithSize())
{
_richText->ignoreContentAdaptWithSize(false);
_richText->setContentSize(Size(100, 100));
}
else
{
_richText->ignoreContentAdaptWithSize(true);
}
}
break;
default:
break;
}
}
void UIRichTextXMLOutline::switchWrapMode(Ref *pSender, Widget::TouchEventType type)
{
if (type == Widget::TouchEventType::ENDED)
{
auto wrapMode = _richText->getWrapMode();
wrapMode = (wrapMode == RichText::WRAP_PER_WORD) ? RichText::WRAP_PER_CHAR : RichText::WRAP_PER_WORD;
_richText->setWrapMode(wrapMode);
}
}
//
// UIRichTextXMLShadow
//
bool UIRichTextXMLShadow::init()
{
if (UIScene::init())
{
Size widgetSize = _widget->getContentSize();
// Add the alert
Text *alert = Text::create("Shadow", "fonts/Marker Felt.ttf", 30);
alert->setColor(Color3B(159, 168, 176));
alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.125));
_widget->addChild(alert);
Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
button->setTouchEnabled(true);
button->setTitleText("switch");
button->setPosition(Vec2(widgetSize.width * 1 / 3, widgetSize.height / 2.0f + button->getContentSize().height * 2.5));
button->addTouchEventListener(CC_CALLBACK_2(UIRichTextXMLShadow::touchEvent, this));
button->setLocalZOrder(10);
_widget->addChild(button);
Button* button2 = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
button2->setTouchEnabled(true);
button2->setTitleText("wrap mode");
button2->setPosition(Vec2(widgetSize.width * 2 / 3, widgetSize.height / 2.0f + button2->getContentSize().height * 2.5));
button2->addTouchEventListener(CC_CALLBACK_2(UIRichTextXMLShadow::switchWrapMode, this));
button2->setLocalZOrder(10);
_widget->addChild(button2);
// RichText
_richText = RichText::createWithXML("<font size=\"24\"><shadow color=\"#4169E1\" offsetWidth=\"8\" offsetHeight=\"-8\" blurRadius=\"2\">SHADOW</shadow></font>");
_richText->ignoreContentAdaptWithSize(false);
_richText->setContentSize(Size(100, 100));
_richText->setPosition(Vec2(widgetSize.width / 2, widgetSize.height / 2));
_richText->setLocalZOrder(10);
_widget->addChild(_richText);
// test remove all children, this call won't effect the test
_richText->removeAllChildren();
return true;
}
return false;
}
void UIRichTextXMLShadow::touchEvent(Ref *pSender, Widget::TouchEventType type)
{
switch (type)
{
case Widget::TouchEventType::ENDED:
{
if (_richText->isIgnoreContentAdaptWithSize())
{
_richText->ignoreContentAdaptWithSize(false);
_richText->setContentSize(Size(100, 100));
}
else
{
_richText->ignoreContentAdaptWithSize(true);
}
}
break;
default:
break;
}
}
void UIRichTextXMLShadow::switchWrapMode(Ref *pSender, Widget::TouchEventType type)
{
if (type == Widget::TouchEventType::ENDED)
{
auto wrapMode = _richText->getWrapMode();
wrapMode = (wrapMode == RichText::WRAP_PER_WORD) ? RichText::WRAP_PER_CHAR : RichText::WRAP_PER_WORD;
_richText->setWrapMode(wrapMode);
}
}
//
// UIRichTextXMLGlow
//
bool UIRichTextXMLGlow::init()
{
if (UIScene::init())
{
Size widgetSize = _widget->getContentSize();
// Add the alert
Text *alert = Text::create("Glow", "fonts/Marker Felt.ttf", 30);
alert->setColor(Color3B(159, 168, 176));
alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.125));
_widget->addChild(alert);
Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
button->setTouchEnabled(true);
button->setTitleText("switch");
button->setPosition(Vec2(widgetSize.width * 1 / 3, widgetSize.height / 2.0f + button->getContentSize().height * 2.5));
button->addTouchEventListener(CC_CALLBACK_2(UIRichTextXMLGlow::touchEvent, this));
button->setLocalZOrder(10);
_widget->addChild(button);
Button* button2 = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
button2->setTouchEnabled(true);
button2->setTitleText("wrap mode");
button2->setPosition(Vec2(widgetSize.width * 2 / 3, widgetSize.height / 2.0f + button2->getContentSize().height * 2.5));
button2->addTouchEventListener(CC_CALLBACK_2(UIRichTextXMLGlow::switchWrapMode, this));
button2->setLocalZOrder(10);
_widget->addChild(button2);
// RichText
_richText = RichText::createWithXML("<font face=\"fonts/Marker Felt.ttf\" size=\"24\"><glow color=\"#AFEEEE\">GLOW</glow></font>");
_richText->ignoreContentAdaptWithSize(false);
_richText->setContentSize(Size(100, 100));
_richText->setPosition(Vec2(widgetSize.width / 2, widgetSize.height / 2));
_richText->setLocalZOrder(10);
_widget->addChild(_richText);
// test remove all children, this call won't effect the test
_richText->removeAllChildren();
return true;
}
return false;
}
void UIRichTextXMLGlow::touchEvent(Ref *pSender, Widget::TouchEventType type)
{
switch (type)
{
case Widget::TouchEventType::ENDED:
{
if (_richText->isIgnoreContentAdaptWithSize())
{
_richText->ignoreContentAdaptWithSize(false);
_richText->setContentSize(Size(100, 100));
}
else
{
_richText->ignoreContentAdaptWithSize(true);
}
}
break;
default:
break;
}
}
void UIRichTextXMLGlow::switchWrapMode(Ref *pSender, Widget::TouchEventType type)
{
if (type == Widget::TouchEventType::ENDED)
{
auto wrapMode = _richText->getWrapMode();
wrapMode = (wrapMode == RichText::WRAP_PER_WORD) ? RichText::WRAP_PER_CHAR : RichText::WRAP_PER_WORD;
_richText->setWrapMode(wrapMode);
}
}
//
// UIRichTextXMLExtend
//
bool UIRichTextXMLExtend::init()
{
if (UIScene::init())
{
Size widgetSize = _widget->getContentSize();
// Add the alert
Text *alert = Text::create("Extend", "fonts/Marker Felt.ttf", 30);
alert->setColor(Color3B(159, 168, 176));
alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.125));
_widget->addChild(alert);
Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
button->setTouchEnabled(true);
button->setTitleText("switch");
button->setPosition(Vec2(widgetSize.width * 1 / 3, widgetSize.height / 2.0f + button->getContentSize().height * 2.5));
button->addTouchEventListener(CC_CALLBACK_2(UIRichTextXMLExtend::touchEvent, this));
button->setLocalZOrder(10);
_widget->addChild(button);
Button* button2 = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
button2->setTouchEnabled(true);
button2->setTitleText("wrap mode");
button2->setPosition(Vec2(widgetSize.width * 2 / 3, widgetSize.height / 2.0f + button2->getContentSize().height * 2.5));
button2->addTouchEventListener(CC_CALLBACK_2(UIRichTextXMLExtend::switchWrapMode, this));
button2->setLocalZOrder(10);
_widget->addChild(button2);
/* Tag extension */
RichText::setTagDescription("CloseNormal", false, [](const ValueMap& tagAttrValueMap) {
RichElementImage* richElement = RichElementImage::create(0, Color3B::WHITE, 255, "cocosui/CloseNormal.png");
return make_pair(ValueMap(), richElement);
});
RichText::setTagDescription("CloseSelected", false, [](const ValueMap& tagAttrValueMap) {
RichElementImage* richElement = RichElementImage::create(0, Color3B::WHITE, 255, "cocosui/CloseSelected.png");
return make_pair(ValueMap(), richElement);
});
/* Defaults */
ValueMap defaults;
defaults[RichText::KEY_FONT_COLOR_STRING] = "#FFF";
defaults[RichText::KEY_FONT_SIZE] = 12.0f;
defaults[RichText::KEY_FONT_FACE] = "fonts/Marker Felt.ttf";
defaults[RichText::KEY_ANCHOR_FONT_COLOR_STRING] = "#f0f8ff";
defaults[RichText::KEY_ANCHOR_TEXT_BOLD] = false;
defaults[RichText::KEY_ANCHOR_TEXT_ITALIC] = false;
//defaults[RichText::KEY_ANCHOR_TEXT_LINE] = RichText::VALUE_TEXT_LINE_NONE;
//defaults[RichText::KEY_ANCHOR_TEXT_LINE] = RichText::VALUE_TEXT_LINE_DEL;
defaults[RichText::KEY_ANCHOR_TEXT_LINE] = RichText::VALUE_TEXT_LINE_UNDER;
//defaults[RichText::KEY_ANCHOR_TEXT_STYLE] = RichText::VALUE_TEXT_STYLE_NONE;
//defaults[RichText::KEY_ANCHOR_TEXT_STYLE] = RichText::VALUE_TEXT_STYLE_OUTLINE;
//defaults[RichText::KEY_ANCHOR_TEXT_STYLE] = RichText::VALUE_TEXT_STYLE_SHADOW;
//defaults[RichText::KEY_ANCHOR_TEXT_STYLE] = RichText::VALUE_TEXT_STYLE_GLOW;
defaults[RichText::KEY_ANCHOR_TEXT_OUTLINE_COLOR] = "#D2B48C";
defaults[RichText::KEY_ANCHOR_TEXT_OUTLINE_SIZE] = 4;
defaults[RichText::KEY_ANCHOR_TEXT_SHADOW_COLOR] = "#4169E1";
defaults[RichText::KEY_ANCHOR_TEXT_SHADOW_OFFSET_WIDTH] = 4.0f;
defaults[RichText::KEY_ANCHOR_TEXT_SHADOW_OFFSET_HEIGHT] = -4.0f;
defaults[RichText::KEY_ANCHOR_TEXT_SHADOW_BLUR_RADIUS] = 0;
defaults[RichText::KEY_ANCHOR_TEXT_GLOW_COLOR] = "#AFEEEE";
// RichText
_richText = RichText::createWithXML("<span>CloseNormal-tag:<br /><CloseNormal /><br /><br />CloseSelected-tag:<br /><CloseSelected></CloseSelected></span>",
defaults,
[](const std::string& url) {
Application::getInstance()->openURL(url);
});
_richText->ignoreContentAdaptWithSize(false);
_richText->setContentSize(Size(100, 100));
_richText->setPosition(Vec2(widgetSize.width / 2, widgetSize.height / 2));
_richText->setLocalZOrder(10);
_widget->addChild(_richText);
// test remove all children, this call won't effect the test
_richText->removeAllChildren();
return true;
}
return false;
}
void UIRichTextXMLExtend::touchEvent(Ref *pSender, Widget::TouchEventType type)
{
switch (type)
{
case Widget::TouchEventType::ENDED:
{
if (_richText->isIgnoreContentAdaptWithSize())
{
_richText->ignoreContentAdaptWithSize(false);
_richText->setContentSize(Size(100, 100));
}
else
{
_richText->ignoreContentAdaptWithSize(true);
}
}
break;
default:
break;
}
}
void UIRichTextXMLExtend::switchWrapMode(Ref *pSender, Widget::TouchEventType type)
{
if (type == Widget::TouchEventType::ENDED)
{
auto wrapMode = _richText->getWrapMode();
wrapMode = (wrapMode == RichText::WRAP_PER_WORD) ? RichText::WRAP_PER_CHAR : RichText::WRAP_PER_WORD;
_richText->setWrapMode(wrapMode);
}
}

View File

@ -124,6 +124,19 @@ protected:
cocos2d::ui::RichText* _richText;
};
class UIRichTextXMLUrlImg : public UIScene
{
public:
CREATE_FUNC(UIRichTextXMLUrlImg);
bool init() override;
void touchEvent(cocos2d::Ref* sender, cocos2d::ui::Widget::TouchEventType type);
void switchWrapMode(cocos2d::Ref* sender, cocos2d::ui::Widget::TouchEventType type);
protected:
cocos2d::ui::RichText* _richText;
};
class UIRichTextXMLFace : public UIScene
{
public:
@ -161,6 +174,56 @@ protected:
cocos2d::ui::RichText* _richText;
};
class UIRichTextXMLOutline : public UIScene
{
public:
CREATE_FUNC(UIRichTextXMLOutline);
bool init() override;
void touchEvent(cocos2d::Ref* sender, cocos2d::ui::Widget::TouchEventType type);
void switchWrapMode(cocos2d::Ref* sender, cocos2d::ui::Widget::TouchEventType type);
protected:
cocos2d::ui::RichText* _richText;
};
class UIRichTextXMLShadow : public UIScene
{
public:
CREATE_FUNC(UIRichTextXMLShadow);
bool init() override;
void touchEvent(cocos2d::Ref* sender, cocos2d::ui::Widget::TouchEventType type);
void switchWrapMode(cocos2d::Ref* sender, cocos2d::ui::Widget::TouchEventType type);
protected:
cocos2d::ui::RichText* _richText;
};
class UIRichTextXMLGlow : public UIScene
{
public:
CREATE_FUNC(UIRichTextXMLGlow);
bool init() override;
void touchEvent(cocos2d::Ref* sender, cocos2d::ui::Widget::TouchEventType type);
void switchWrapMode(cocos2d::Ref* sender, cocos2d::ui::Widget::TouchEventType type);
protected:
cocos2d::ui::RichText* _richText;
};
class UIRichTextXMLExtend : public UIScene
{
public:
CREATE_FUNC(UIRichTextXMLExtend);
bool init() override;
void touchEvent(cocos2d::Ref* sender, cocos2d::ui::Widget::TouchEventType type);
void switchWrapMode(cocos2d::Ref* sender, cocos2d::ui::Widget::TouchEventType type);
protected:
cocos2d::ui::RichText* _richText;
};
#endif /* defined(__TestCpp__UIRichTextTest__) */

View File

@ -30,9 +30,9 @@ headers = %(cocosdir)s/cocos/ui/CocosGUI.h %(cocosdir)s/cocos/ui/UIScrollViewBar
# what classes to produce code for. You can use regular expressions here. When testing the regular
# expression, it will be enclosed in "^$", like this: "^Menu*$".
classes = Helper Layout Widget Button CheckBox ImageView Text TextAtlas TextBMFont RichText RichElement RichElementText RichElementImage RichElementCustomNode LoadingBar Slider TextField UICCTextField ScrollView ScrollViewBar PageView ListView LayoutParameter LinearLayoutParameter RelativeLayoutParameter VideoPlayer HBox VBox RelativeBox Scale9Sprite EditBox$ LayoutComponent RadioButtonGroup RadioButton AbstractCheckButton TabControl TabHeader
classes = Helper Layout Widget Button CheckBox ImageView Text TextAtlas TextBMFont RichText RichElement RichElementText RichElementImage RichElementCustomNode RichElementNewLine LoadingBar Slider TextField UICCTextField ScrollView ScrollViewBar PageView ListView LayoutParameter LinearLayoutParameter RelativeLayoutParameter VideoPlayer HBox VBox RelativeBox Scale9Sprite EditBox$ LayoutComponent RadioButtonGroup RadioButton AbstractCheckButton TabControl TabHeader
classes_need_extend = Layout Widget Button CheckBox ImageView Text TextAtlas TextBMFont RichText RichElement RichElementText RichElementImage RichElementCustomNode LoadingBar Slider TextField ScrollView ScrollViewBar PageView ListView VideoPlayer HBox VBox RelativeBox Scale9Sprite EditBox$ LayoutComponent RadioButtonGroup RadioButton AbstractCheckButton TabControl TabHeader
classes_need_extend = Layout Widget Button CheckBox ImageView Text TextAtlas TextBMFont RichText RichElement RichElementText RichElementImage RichElementCustomNode RichElementNewLine LoadingBar Slider TextField ScrollView ScrollViewBar PageView ListView VideoPlayer HBox VBox RelativeBox Scale9Sprite EditBox$ LayoutComponent RadioButtonGroup RadioButton AbstractCheckButton TabControl TabHeader
# what should we skip? in the format ClassName::[function function]
# ClassName is a regular expression, but will be used like this: "^ClassName$" functions are also
@ -46,7 +46,7 @@ skip = *::[^visit$ copyWith.* onEnter.* onExit.* ^description$ getObjectType .*H
Layer::[getInputManager],
LayoutParameter::[(s|g)etMargin],
ImageView::[doubleClickEvent checkDoubleClick],
RichText::[getVirtualRendererSize],
RichText::[getVirtualRendererSize setTagDescription removeTagDescription setOpenUrlHandler],
EditBox::[(g|s)etDelegate ^keyboard.* touchDownAction getScriptEditBoxHandler registerScriptEditBoxHandler unregisterScriptEditBoxHandler]
rename_functions = Widget::[init=_init],

View File

@ -41,7 +41,8 @@ classes = Helper Widget Layout Button CheckBox ImageView Text TextAtlas TextBMFo
skip = *::[^visit$ copyWith.* onEnter.* onExit.* ^description$ getObjectType .*HSV onTouch.* onAcc.* onKey.* onRegisterTouchListener ccTouch.* (g|s)etDelegate],
Widget::[addTouchEventListener addClickEventListener addCCSEventListener],
LayoutParameter::[(s|g)etMargin]
LayoutParameter::[(s|g)etMargin],
RichText::[setTagDescription removeTagDescription setOpenUrlHandler]
rename_functions =