2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013 cocos2d-x.org
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-31 12:12:40 +08:00
|
|
|
Copyright (c) 2021 Bytedance Inc.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
https://adxeproject.github.io
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
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:
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "ui/UIRichText.h"
|
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
#include <vector>
|
|
|
|
#include <locale>
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
#include "platform/CCApplication.h"
|
|
|
|
#include "base/CCEventListenerTouch.h"
|
|
|
|
#include "base/CCEventDispatcher.h"
|
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "2d/CCLabel.h"
|
|
|
|
#include "2d/CCSprite.h"
|
|
|
|
#include "base/ccUTF8.h"
|
|
|
|
#include "ui/UIHelper.h"
|
|
|
|
|
|
|
|
#include "platform/CCSAXParser.h"
|
|
|
|
|
|
|
|
USING_NS_CC;
|
|
|
|
using namespace cocos2d::ui;
|
|
|
|
|
|
|
|
class ListenerComponent : public Component
|
|
|
|
{
|
|
|
|
public:
|
2021-12-25 10:04:45 +08:00
|
|
|
static const std::string COMPONENT_NAME; /*!< component name */
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
static ListenerComponent* create(Node* parent,
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string_view url,
|
2021-12-25 10:04:45 +08:00
|
|
|
const RichText::OpenUrlHandler handleOpenUrl = nullptr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto component = new ListenerComponent(parent, url, std::move(handleOpenUrl));
|
2019-11-23 20:27:39 +08:00
|
|
|
component->autorelease();
|
|
|
|
return component;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
explicit ListenerComponent(Node* parent, std::string_view url, const RichText::OpenUrlHandler handleOpenUrl)
|
2021-12-25 10:04:45 +08:00
|
|
|
: _parent(parent), _url(url), _handleOpenUrl(std::move(handleOpenUrl))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
setName(ListenerComponent::COMPONENT_NAME);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
_touchListener = cocos2d::EventListenerTouchAllAtOnce::create();
|
2019-11-23 20:27:39 +08:00
|
|
|
_touchListener->onTouchesEnded = CC_CALLBACK_2(ListenerComponent::onTouchesEnded, this);
|
|
|
|
|
|
|
|
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(_touchListener, _parent);
|
|
|
|
_touchListener->retain();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~ListenerComponent()
|
|
|
|
{
|
|
|
|
Director::getInstance()->getEventDispatcher()->removeEventListener(_touchListener);
|
|
|
|
_touchListener->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void onTouchesEnded(const std::vector<Touch*>& touches, Event* /*event*/)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& touch : touches)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
// FIXME: Node::getBoundBox() doesn't return it in local coordinates... so create one manually.
|
|
|
|
Rect localRect = Rect(Vec2::ZERO, _parent->getContentSize());
|
2021-12-25 10:04:45 +08:00
|
|
|
if (localRect.containsPoint(_parent->convertTouchToNodeSpace(touch)))
|
|
|
|
{
|
|
|
|
if (_handleOpenUrl)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
_handleOpenUrl(_url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
void setOpenUrlHandler(const RichText::OpenUrlHandler& handleOpenUrl) { _handleOpenUrl = handleOpenUrl; }
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
private:
|
2021-12-25 10:04:45 +08:00
|
|
|
Node* _parent; // weak ref.
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string _url;
|
|
|
|
RichText::OpenUrlHandler _handleOpenUrl;
|
2021-12-25 10:04:45 +08:00
|
|
|
EventListenerTouchAllAtOnce* _touchListener; // strong ref.
|
2019-11-23 20:27:39 +08:00
|
|
|
};
|
|
|
|
const std::string ListenerComponent::COMPONENT_NAME("cocos2d_ui_UIRichText_ListenerComponent");
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool RichElement::init(int tag, const Color3B& color, uint8_t opacity)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_tag = tag;
|
|
|
|
_color = color;
|
2019-11-23 20:27:39 +08:00
|
|
|
_opacity = opacity;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RichElement::equalType(Type type)
|
|
|
|
{
|
|
|
|
return (_type == type);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RichElement::setColor(const Color3B& color)
|
|
|
|
{
|
|
|
|
_color = color;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
RichElementText* RichElementText::create(int tag,
|
|
|
|
const Color3B& color,
|
|
|
|
uint8_t opacity,
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string_view text,
|
|
|
|
std::string_view fontName,
|
2021-12-25 10:04:45 +08:00
|
|
|
float fontSize,
|
|
|
|
uint32_t flags,
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string_view url,
|
2021-12-25 10:04:45 +08:00
|
|
|
const Color3B& outlineColor,
|
|
|
|
int outlineSize,
|
|
|
|
const Color3B& shadowColor,
|
|
|
|
const Vec2& shadowOffset,
|
|
|
|
int shadowBlurRadius,
|
2019-11-23 20:27:39 +08:00
|
|
|
const Color3B& glowColor)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
RichElementText* element = new RichElementText();
|
2021-12-25 10:04:45 +08:00
|
|
|
if (element->init(tag, color, opacity, text, fontName, fontSize, flags, url, outlineColor, outlineSize, shadowColor,
|
|
|
|
shadowOffset, shadowBlurRadius, glowColor))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
element->autorelease();
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(element);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
bool RichElementText::init(int tag,
|
|
|
|
const Color3B& color,
|
|
|
|
uint8_t opacity,
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string_view text,
|
|
|
|
std::string_view fontName,
|
2021-12-25 10:04:45 +08:00
|
|
|
float fontSize,
|
|
|
|
uint32_t flags,
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string_view url,
|
2021-12-25 10:04:45 +08:00
|
|
|
const Color3B& outlineColor,
|
|
|
|
int outlineSize,
|
|
|
|
const Color3B& shadowColor,
|
|
|
|
const Vec2& shadowOffset,
|
|
|
|
int shadowBlurRadius,
|
2019-11-23 20:27:39 +08:00
|
|
|
const Color3B& glowColor)
|
|
|
|
{
|
|
|
|
if (RichElement::init(tag, color, opacity))
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_text = text;
|
|
|
|
_fontName = fontName;
|
|
|
|
_fontSize = fontSize;
|
|
|
|
_flags = flags;
|
|
|
|
_url = url;
|
|
|
|
_outlineColor = outlineColor;
|
|
|
|
_outlineSize = outlineSize;
|
|
|
|
_shadowColor = shadowColor;
|
|
|
|
_shadowOffset = shadowOffset;
|
2019-11-23 20:27:39 +08:00
|
|
|
_shadowBlurRadius = shadowBlurRadius;
|
2021-12-25 10:04:45 +08:00
|
|
|
_glowColor = glowColor;
|
2019-11-23 20:27:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
RichElementImage* RichElementImage::create(int tag,
|
|
|
|
const Color3B& color,
|
|
|
|
uint8_t opacity,
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string_view filePath,
|
|
|
|
std::string_view url,
|
2021-12-25 10:04:45 +08:00
|
|
|
Widget::TextureResType texType)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
RichElementImage* element = new RichElementImage();
|
|
|
|
if (element->init(tag, color, opacity, filePath, url, texType))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
element->autorelease();
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(element);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
bool RichElementImage::init(int tag,
|
|
|
|
const Color3B& color,
|
|
|
|
uint8_t opacity,
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string_view filePath,
|
|
|
|
std::string_view url,
|
2021-12-25 10:04:45 +08:00
|
|
|
Widget::TextureResType texType)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (RichElement::init(tag, color, opacity))
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_filePath = filePath;
|
|
|
|
_width = -1;
|
|
|
|
_height = -1;
|
|
|
|
_url = url;
|
2019-11-23 20:27:39 +08:00
|
|
|
_textureType = texType;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RichElementImage::setWidth(int width)
|
|
|
|
{
|
|
|
|
_width = width;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RichElementImage::setHeight(int height)
|
|
|
|
{
|
|
|
|
_height = height;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void RichElementImage::setUrl(std::string_view url)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_url = url;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
RichElementCustomNode* RichElementCustomNode::create(int tag,
|
|
|
|
const Color3B& color,
|
|
|
|
uint8_t opacity,
|
|
|
|
cocos2d::Node* customNode)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
RichElementCustomNode* element = new RichElementCustomNode();
|
|
|
|
if (element->init(tag, color, opacity, customNode))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
element->autorelease();
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(element);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
bool RichElementCustomNode::init(int tag, const Color3B& color, uint8_t opacity, cocos2d::Node* customNode)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (RichElement::init(tag, color, opacity))
|
|
|
|
{
|
|
|
|
_customNode = customNode;
|
|
|
|
_customNode->retain();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
RichElementNewLine* RichElementNewLine::create(int tag, const Color3B& color, uint8_t opacity)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
RichElementNewLine* element = new RichElementNewLine();
|
|
|
|
if (element->init(tag, color, opacity))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
element->autorelease();
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(element);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @brief parse a XML. */
|
|
|
|
class MyXMLVisitor : public SAXDelegator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/** @brief underline or strikethrough */
|
2021-12-25 10:04:45 +08:00
|
|
|
enum class StyleLine
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
NONE,
|
2021-12-25 10:04:45 +08:00
|
|
|
UNDERLINE, /*!< underline */
|
|
|
|
STRIKETHROUGH /*!< a typographical presentation of words with a horizontal line through their center */
|
2019-11-23 20:27:39 +08:00
|
|
|
};
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/** @brief outline, shadow or glow */
|
2021-12-25 10:04:45 +08:00
|
|
|
enum class StyleEffect
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
NONE,
|
2021-12-25 10:04:45 +08:00
|
|
|
OUTLINE, /*!< outline effect enabled */
|
|
|
|
SHADOW, /*!< shadow effect enabled */
|
|
|
|
GLOW /*!< glow effect enabled @discussion Limiting use to only when the Label created with true type font. */
|
2019-11-23 20:27:39 +08:00
|
|
|
};
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/** @brief the attributes of text tag */
|
|
|
|
struct Attributes
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
std::string face; /*!< font name */
|
|
|
|
std::string url; /*!< url is a attribute of a anchor tag */
|
|
|
|
float fontSize; /*!< font size */
|
|
|
|
Color3B color; /*!< font color */
|
|
|
|
bool hasColor; /*!< or color is specified? */
|
|
|
|
bool bold; /*!< bold text */
|
|
|
|
bool italics; /*!< italic text */
|
|
|
|
StyleLine line; /*!< underline or strikethrough */
|
|
|
|
StyleEffect effect; /*!< outline, shadow or glow */
|
|
|
|
Color3B outlineColor; /*!< the color of the outline */
|
|
|
|
int outlineSize; /*!< the outline effect size value */
|
|
|
|
Color3B shadowColor; /*!< the shadow effect color value */
|
|
|
|
Vec2 shadowOffset; /*!< shadow effect offset value */
|
|
|
|
int shadowBlurRadius; /*!< the shadow effect blur radius */
|
|
|
|
Color3B glowColor; /*!< the glow effect color value */
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
void setColor(const Color3B& acolor)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
color = acolor;
|
2019-11-23 20:27:39 +08:00
|
|
|
hasColor = true;
|
|
|
|
}
|
|
|
|
Attributes()
|
2021-12-25 10:04:45 +08:00
|
|
|
: fontSize(-1)
|
|
|
|
, hasColor(false)
|
|
|
|
, bold(false)
|
|
|
|
, italics(false)
|
|
|
|
, line(StyleLine::NONE)
|
|
|
|
, effect(StyleEffect::NONE)
|
|
|
|
{}
|
2019-11-23 20:27:39 +08:00
|
|
|
};
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
private:
|
|
|
|
std::vector<Attributes> _fontElements;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
RichText* _richText;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
struct TagBehavior
|
|
|
|
{
|
|
|
|
bool isFontElement;
|
|
|
|
RichText::VisitEnterHandler handleVisitEnter;
|
2019-11-23 20:27:39 +08:00
|
|
|
};
|
2021-12-31 12:12:40 +08:00
|
|
|
typedef hlookup::string_map<TagBehavior> TagTables;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
static TagTables _tagTables;
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
public:
|
|
|
|
explicit MyXMLVisitor(RichText* richText);
|
|
|
|
virtual ~MyXMLVisitor();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Color3B getColor() const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
float getFontSize() const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string getFace() const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string getURL() const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
bool getBold() const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
bool getItalics() const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
bool getUnderline() const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
bool getStrikethrough() const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
std::tuple<bool, Color3B, int> getOutline() const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-10-23 23:27:14 +08:00
|
|
|
std::tuple<bool, Color3B, Vec2, int> getShadow() const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
std::tuple<bool, Color3B> getGlow() const;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void startElement(void* ctx, const char* name, const char** atts) override;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void endElement(void* ctx, const char* name) override;
|
|
|
|
|
|
|
|
void textHandler(void* ctx, const char* s, size_t len) override;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
void pushBackFontElement(const Attributes& attribs);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
void popBackFontElement();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
void pushBackElement(RichElement* element);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
static void setTagDescription(std::string_view tag,
|
2021-12-25 10:04:45 +08:00
|
|
|
bool isFontElement,
|
|
|
|
RichText::VisitEnterHandler&& handleVisitEnter);
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
static void removeTagDescription(std::string_view tag);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
private:
|
2021-12-25 10:04:45 +08:00
|
|
|
ValueMap tagAttrMapWithXMLElement(const char** attrs);
|
2019-11-23 20:27:39 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
MyXMLVisitor::TagTables MyXMLVisitor::_tagTables;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
MyXMLVisitor::MyXMLVisitor(RichText* richText) : _fontElements(20), _richText(richText)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
MyXMLVisitor::setTagDescription("font", true, [](const ValueMap& tagAttrValueMap) {
|
|
|
|
// supported attributes:
|
|
|
|
// size, color, align, face
|
|
|
|
ValueMap attrValueMap;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (tagAttrValueMap.find("size") != tagAttrValueMap.end())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
attrValueMap[RichText::KEY_FONT_SIZE] = tagAttrValueMap.at("size").asString();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (tagAttrValueMap.find("color") != tagAttrValueMap.end())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
attrValueMap[RichText::KEY_FONT_COLOR_STRING] = tagAttrValueMap.at("color").asString();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (tagAttrValueMap.find("face") != tagAttrValueMap.end())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
attrValueMap[RichText::KEY_FONT_FACE] = tagAttrValueMap.at("face").asString();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return make_pair(attrValueMap, nullptr);
|
|
|
|
});
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
MyXMLVisitor::setTagDescription("b", true, [](const ValueMap& /*tagAttrValueMap*/) {
|
|
|
|
// no supported attributes
|
|
|
|
ValueMap attrValueMap;
|
|
|
|
attrValueMap[RichText::KEY_TEXT_BOLD] = true;
|
|
|
|
return make_pair(attrValueMap, nullptr);
|
|
|
|
});
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
MyXMLVisitor::setTagDescription("i", true, [](const ValueMap& /*tagAttrValueMap*/) {
|
|
|
|
// no supported attributes
|
|
|
|
ValueMap attrValueMap;
|
|
|
|
attrValueMap[RichText::KEY_TEXT_ITALIC] = true;
|
|
|
|
return make_pair(attrValueMap, nullptr);
|
|
|
|
});
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
MyXMLVisitor::setTagDescription("del", true, [](const ValueMap& /*tagAttrValueMap*/) {
|
|
|
|
// no supported attributes
|
|
|
|
ValueMap attrValueMap;
|
|
|
|
attrValueMap[RichText::KEY_TEXT_LINE] = RichText::VALUE_TEXT_LINE_DEL;
|
|
|
|
return make_pair(attrValueMap, nullptr);
|
|
|
|
});
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
MyXMLVisitor::setTagDescription("u", true, [](const ValueMap& /*tagAttrValueMap*/) {
|
|
|
|
// no supported attributes
|
|
|
|
ValueMap attrValueMap;
|
|
|
|
attrValueMap[RichText::KEY_TEXT_LINE] = RichText::VALUE_TEXT_LINE_UNDER;
|
|
|
|
return make_pair(attrValueMap, nullptr);
|
|
|
|
});
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
MyXMLVisitor::setTagDescription("small", true, [](const ValueMap& /*tagAttrValueMap*/) {
|
|
|
|
ValueMap attrValueMap;
|
|
|
|
attrValueMap[RichText::KEY_FONT_SMALL] = true;
|
|
|
|
return make_pair(attrValueMap, nullptr);
|
|
|
|
});
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
MyXMLVisitor::setTagDescription("big", true, [](const ValueMap& /*tagAttrValueMap*/) {
|
|
|
|
ValueMap attrValueMap;
|
|
|
|
attrValueMap[RichText::KEY_FONT_BIG] = true;
|
|
|
|
return make_pair(attrValueMap, nullptr);
|
|
|
|
});
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
MyXMLVisitor::setTagDescription("img", false, [](const ValueMap& tagAttrValueMap) {
|
|
|
|
// supported attributes:
|
|
|
|
// src, height, width
|
|
|
|
std::string src;
|
2021-12-25 10:04:45 +08:00
|
|
|
int height = -1;
|
|
|
|
int width = -1;
|
2019-11-23 20:27:39 +08:00
|
|
|
Widget::TextureResType resType = Widget::TextureResType::LOCAL;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (tagAttrValueMap.find("src") != tagAttrValueMap.end())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
src = tagAttrValueMap.at("src").asString();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (tagAttrValueMap.find("height") != tagAttrValueMap.end())
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
height = tagAttrValueMap.at("height").asInt();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (tagAttrValueMap.find("width") != tagAttrValueMap.end())
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
width = tagAttrValueMap.at("width").asInt();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (tagAttrValueMap.find("type") != tagAttrValueMap.end())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
// texture type
|
|
|
|
// 0: normal file path
|
|
|
|
// 1: sprite frame name
|
2020-08-18 11:33:18 +08:00
|
|
|
int type = tagAttrValueMap.at("type").asInt();
|
2021-12-25 10:04:45 +08:00
|
|
|
resType = type == 0 ? Widget::TextureResType::LOCAL : Widget::TextureResType::PLIST;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
RichElementImage* elementImg = nullptr;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (src.length())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
elementImg = RichElementImage::create(0, Color3B::WHITE, 255, src, "", resType);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (0 <= height)
|
|
|
|
elementImg->setHeight(height);
|
|
|
|
if (0 <= width)
|
|
|
|
elementImg->setWidth(width);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
return make_pair(ValueMap(), elementImg);
|
|
|
|
});
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
MyXMLVisitor::setTagDescription("a", true, [](const ValueMap& tagAttrValueMap) {
|
|
|
|
// supported attributes:
|
|
|
|
ValueMap attrValueMap;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (tagAttrValueMap.find("href") != tagAttrValueMap.end())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
attrValueMap[RichText::KEY_URL] = tagAttrValueMap.at("href").asString();
|
|
|
|
}
|
|
|
|
return make_pair(attrValueMap, nullptr);
|
|
|
|
});
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
MyXMLVisitor::setTagDescription("br", false, [](const ValueMap& /*tagAttrValueMap*/) {
|
2019-11-23 20:27:39 +08:00
|
|
|
RichElementNewLine* richElement = RichElementNewLine::create(0, Color3B::WHITE, 255);
|
|
|
|
return make_pair(ValueMap(), richElement);
|
|
|
|
});
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
MyXMLVisitor::setTagDescription("outline", true, [](const ValueMap& tagAttrValueMap) {
|
|
|
|
// supported attributes:
|
|
|
|
// color, size
|
|
|
|
ValueMap attrValueMap;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
attrValueMap[RichText::KEY_TEXT_STYLE] = RichText::VALUE_TEXT_STYLE_OUTLINE;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (tagAttrValueMap.find("color") != tagAttrValueMap.end())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
attrValueMap[RichText::KEY_TEXT_OUTLINE_COLOR] = tagAttrValueMap.at("color").asString();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (tagAttrValueMap.find("size") != tagAttrValueMap.end())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
attrValueMap[RichText::KEY_TEXT_OUTLINE_SIZE] = tagAttrValueMap.at("size").asString();
|
|
|
|
}
|
|
|
|
return make_pair(attrValueMap, nullptr);
|
|
|
|
});
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
MyXMLVisitor::setTagDescription("shadow", true, [](const ValueMap& tagAttrValueMap) {
|
|
|
|
// supported attributes:
|
|
|
|
// color, offsetWidth, offsetHeight, blurRadius
|
|
|
|
ValueMap attrValueMap;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
attrValueMap[RichText::KEY_TEXT_STYLE] = RichText::VALUE_TEXT_STYLE_SHADOW;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (tagAttrValueMap.find("color") != tagAttrValueMap.end())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
attrValueMap[RichText::KEY_TEXT_SHADOW_COLOR] = tagAttrValueMap.at("color").asString();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (tagAttrValueMap.find("offsetWidth") != tagAttrValueMap.end())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
attrValueMap[RichText::KEY_TEXT_SHADOW_OFFSET_WIDTH] = tagAttrValueMap.at("offsetWidth").asString();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (tagAttrValueMap.find("offsetHeight") != tagAttrValueMap.end())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
attrValueMap[RichText::KEY_TEXT_SHADOW_OFFSET_HEIGHT] = tagAttrValueMap.at("offsetHeight").asString();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (tagAttrValueMap.find("blurRadius") != tagAttrValueMap.end())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
attrValueMap[RichText::KEY_TEXT_SHADOW_BLUR_RADIUS] = tagAttrValueMap.at("blurRadius").asString();
|
|
|
|
}
|
|
|
|
return make_pair(attrValueMap, nullptr);
|
|
|
|
});
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
MyXMLVisitor::setTagDescription("glow", true, [](const ValueMap& tagAttrValueMap) {
|
|
|
|
// supported attributes:
|
|
|
|
// color
|
|
|
|
ValueMap attrValueMap;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
attrValueMap[RichText::KEY_TEXT_STYLE] = RichText::VALUE_TEXT_STYLE_GLOW;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (tagAttrValueMap.find("color") != tagAttrValueMap.end())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
attrValueMap[RichText::KEY_TEXT_GLOW_COLOR] = tagAttrValueMap.at("color").asString();
|
|
|
|
}
|
|
|
|
return make_pair(attrValueMap, nullptr);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
MyXMLVisitor::~MyXMLVisitor() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
Color3B MyXMLVisitor::getColor() const
|
|
|
|
{
|
|
|
|
for (auto i = _fontElements.rbegin(), iRend = _fontElements.rend(); i != iRend; ++i)
|
|
|
|
{
|
|
|
|
if (i->hasColor)
|
|
|
|
return i->color;
|
|
|
|
}
|
|
|
|
return Color3B::WHITE;
|
|
|
|
}
|
|
|
|
|
|
|
|
float MyXMLVisitor::getFontSize() const
|
|
|
|
{
|
|
|
|
for (auto i = _fontElements.rbegin(), iRend = _fontElements.rend(); i != iRend; ++i)
|
|
|
|
{
|
|
|
|
if (i->fontSize != -1)
|
|
|
|
return i->fontSize;
|
|
|
|
}
|
|
|
|
return 12;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string MyXMLVisitor::getFace() const
|
|
|
|
{
|
|
|
|
for (auto i = _fontElements.rbegin(), iRend = _fontElements.rend(); i != iRend; ++i)
|
|
|
|
{
|
2020-08-06 17:37:34 +08:00
|
|
|
if (!i->face.empty())
|
2019-11-23 20:27:39 +08:00
|
|
|
return i->face;
|
|
|
|
}
|
|
|
|
return "fonts/Marker Felt.ttf";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string MyXMLVisitor::getURL() const
|
|
|
|
{
|
|
|
|
for (auto i = _fontElements.rbegin(), iRend = _fontElements.rend(); i != iRend; ++i)
|
|
|
|
{
|
2020-08-06 17:37:34 +08:00
|
|
|
if (!i->url.empty())
|
2019-11-23 20:27:39 +08:00
|
|
|
return i->url;
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MyXMLVisitor::getBold() const
|
|
|
|
{
|
|
|
|
for (auto i = _fontElements.rbegin(), iRend = _fontElements.rend(); i != iRend; ++i)
|
|
|
|
{
|
|
|
|
if (i->bold)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MyXMLVisitor::getItalics() const
|
|
|
|
{
|
|
|
|
for (auto i = _fontElements.rbegin(), iRend = _fontElements.rend(); i != iRend; ++i)
|
|
|
|
{
|
|
|
|
if (i->italics)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MyXMLVisitor::getUnderline() const
|
|
|
|
{
|
|
|
|
for (auto i = _fontElements.rbegin(), iRend = _fontElements.rend(); i != iRend; ++i)
|
|
|
|
{
|
|
|
|
if (i->line == StyleLine::UNDERLINE)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MyXMLVisitor::getStrikethrough() const
|
|
|
|
{
|
|
|
|
for (auto i = _fontElements.rbegin(), iRend = _fontElements.rend(); i != iRend; ++i)
|
|
|
|
{
|
|
|
|
if (i->line == StyleLine::STRIKETHROUGH)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::tuple<bool, Color3B, int> MyXMLVisitor::getOutline() const
|
|
|
|
{
|
|
|
|
for (auto i = _fontElements.rbegin(), iRend = _fontElements.rend(); i != iRend; ++i)
|
|
|
|
{
|
|
|
|
if (i->effect == StyleEffect::OUTLINE)
|
|
|
|
return std::make_tuple(true, i->outlineColor, i->outlineSize);
|
|
|
|
}
|
|
|
|
return std::make_tuple(false, Color3B::WHITE, -1);
|
|
|
|
}
|
|
|
|
|
2021-10-23 23:27:14 +08:00
|
|
|
std::tuple<bool, Color3B, Vec2, int> MyXMLVisitor::getShadow() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
for (auto i = _fontElements.rbegin(), iRend = _fontElements.rend(); i != iRend; ++i)
|
|
|
|
{
|
|
|
|
if (i->effect == StyleEffect::SHADOW)
|
|
|
|
return std::make_tuple(true, i->shadowColor, i->shadowOffset, i->shadowBlurRadius);
|
|
|
|
}
|
2021-10-23 23:27:14 +08:00
|
|
|
return std::make_tuple(false, Color3B::BLACK, Vec2(2.0, -2.0), 0);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::tuple<bool, Color3B> MyXMLVisitor::getGlow() const
|
|
|
|
{
|
|
|
|
for (auto i = _fontElements.rbegin(), iRend = _fontElements.rend(); i != iRend; ++i)
|
|
|
|
{
|
|
|
|
if (i->effect == StyleEffect::GLOW)
|
|
|
|
return std::make_tuple(true, i->glowColor);
|
|
|
|
}
|
|
|
|
return std::make_tuple(false, Color3B::WHITE);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void MyXMLVisitor::startElement(void* /*ctx*/, const char* elementName, const char** atts)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto it = _tagTables.find(elementName);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (it != _tagTables.end())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
auto tagBehavior = it->second;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (tagBehavior.handleVisitEnter != nullptr)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
ValueMap&& tagAttrValueMap = tagAttrMapWithXMLElement(atts);
|
2021-12-25 10:04:45 +08:00
|
|
|
auto result = tagBehavior.handleVisitEnter(tagAttrValueMap);
|
|
|
|
ValueMap& attrValueMap = result.first;
|
|
|
|
RichElement* richElement = result.second;
|
|
|
|
if (!attrValueMap.empty())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
Attributes attributes;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (attrValueMap.find(RichText::KEY_FONT_SIZE) != attrValueMap.end())
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
attributes.fontSize = attrValueMap.at(RichText::KEY_FONT_SIZE).asFloat();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (attrValueMap.find(RichText::KEY_FONT_SMALL) != attrValueMap.end())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
attributes.fontSize = getFontSize() * 0.8f;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (attrValueMap.find(RichText::KEY_FONT_BIG) != attrValueMap.end())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
attributes.fontSize = getFontSize() * 1.25f;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (attrValueMap.find(RichText::KEY_FONT_COLOR_STRING) != attrValueMap.end())
|
|
|
|
{
|
|
|
|
attributes.setColor(
|
|
|
|
_richText->color3BWithString(attrValueMap.at(RichText::KEY_FONT_COLOR_STRING).asString()));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (attrValueMap.find(RichText::KEY_FONT_FACE) != attrValueMap.end())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
attributes.face = attrValueMap.at(RichText::KEY_FONT_FACE).asString();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (attrValueMap.find(RichText::KEY_TEXT_BOLD) != attrValueMap.end())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
attributes.bold = true;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (attrValueMap.find(RichText::KEY_TEXT_ITALIC) != attrValueMap.end())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
attributes.italics = true;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (attrValueMap.find(RichText::KEY_TEXT_LINE) != attrValueMap.end())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
auto keyTextLine = attrValueMap.at(RichText::KEY_TEXT_LINE).asString();
|
2021-12-25 10:04:45 +08:00
|
|
|
if (keyTextLine == RichText::VALUE_TEXT_LINE_DEL)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
attributes.line = StyleLine::STRIKETHROUGH;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (keyTextLine == RichText::VALUE_TEXT_LINE_UNDER)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
attributes.line = StyleLine::UNDERLINE;
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (attrValueMap.find(RichText::KEY_URL) != attrValueMap.end())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
attributes.url = attrValueMap.at(RichText::KEY_URL).asString();
|
|
|
|
attributes.setColor(_richText->getAnchorFontColor3B());
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_richText->isAnchorTextBoldEnabled())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
attributes.bold = true;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_richText->isAnchorTextItalicEnabled())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
attributes.italics = true;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_richText->isAnchorTextUnderlineEnabled())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
attributes.line = StyleLine::UNDERLINE;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_richText->isAnchorTextDelEnabled())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
attributes.line = StyleLine::STRIKETHROUGH;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_richText->isAnchorTextOutlineEnabled())
|
|
|
|
{
|
|
|
|
attributes.effect = StyleEffect::OUTLINE;
|
2019-11-23 20:27:39 +08:00
|
|
|
attributes.outlineColor = _richText->getAnchorTextOutlineColor3B();
|
2021-12-25 10:04:45 +08:00
|
|
|
attributes.outlineSize = _richText->getAnchorTextOutlineSize();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_richText->isAnchorTextShadowEnabled())
|
|
|
|
{
|
|
|
|
attributes.effect = StyleEffect::SHADOW;
|
|
|
|
attributes.shadowColor = _richText->getAnchorTextShadowColor3B();
|
|
|
|
attributes.shadowOffset = _richText->getAnchorTextShadowOffset();
|
2019-11-23 20:27:39 +08:00
|
|
|
attributes.shadowBlurRadius = _richText->getAnchorTextShadowBlurRadius();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_richText->isAnchorTextGlowEnabled())
|
|
|
|
{
|
|
|
|
attributes.effect = StyleEffect::GLOW;
|
2019-11-23 20:27:39 +08:00
|
|
|
attributes.glowColor = _richText->getAnchorTextGlowColor3B();
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (attrValueMap.find(RichText::KEY_TEXT_STYLE) != attrValueMap.end())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
auto keyTextStyle = attrValueMap.at(RichText::KEY_TEXT_STYLE).asString();
|
2021-12-25 10:04:45 +08:00
|
|
|
if (keyTextStyle == RichText::VALUE_TEXT_STYLE_OUTLINE)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
attributes.effect = StyleEffect::OUTLINE;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (attrValueMap.find(RichText::KEY_TEXT_OUTLINE_COLOR) != attrValueMap.end())
|
|
|
|
{
|
|
|
|
attributes.outlineColor = _richText->color3BWithString(
|
|
|
|
attrValueMap.at(RichText::KEY_TEXT_OUTLINE_COLOR).asString());
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (attrValueMap.find(RichText::KEY_TEXT_OUTLINE_SIZE) != attrValueMap.end())
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
attributes.outlineSize = attrValueMap.at(RichText::KEY_TEXT_OUTLINE_SIZE).asInt();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (keyTextStyle == RichText::VALUE_TEXT_STYLE_SHADOW)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
attributes.effect = StyleEffect::SHADOW;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (attrValueMap.find(RichText::KEY_TEXT_SHADOW_COLOR) != attrValueMap.end())
|
|
|
|
{
|
|
|
|
attributes.shadowColor = _richText->color3BWithString(
|
|
|
|
attrValueMap.at(RichText::KEY_TEXT_SHADOW_COLOR).asString());
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if ((attrValueMap.find(RichText::KEY_TEXT_SHADOW_OFFSET_WIDTH) != attrValueMap.end()) &&
|
|
|
|
(attrValueMap.find(RichText::KEY_TEXT_SHADOW_OFFSET_HEIGHT) != attrValueMap.end()))
|
|
|
|
{
|
|
|
|
attributes.shadowOffset =
|
|
|
|
Vec2(attrValueMap.at(RichText::KEY_TEXT_SHADOW_OFFSET_WIDTH).asFloat(),
|
|
|
|
attrValueMap.at(RichText::KEY_TEXT_SHADOW_OFFSET_HEIGHT).asFloat());
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (attrValueMap.find(RichText::KEY_TEXT_SHADOW_BLUR_RADIUS) != attrValueMap.end())
|
|
|
|
{
|
|
|
|
attributes.shadowBlurRadius =
|
|
|
|
attrValueMap.at(RichText::KEY_TEXT_SHADOW_BLUR_RADIUS).asInt();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (keyTextStyle == RichText::VALUE_TEXT_STYLE_GLOW)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
attributes.effect = StyleEffect::GLOW;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (attrValueMap.find(RichText::KEY_TEXT_GLOW_COLOR) != attrValueMap.end())
|
|
|
|
{
|
|
|
|
attributes.glowColor =
|
|
|
|
_richText->color3BWithString(attrValueMap.at(RichText::KEY_TEXT_GLOW_COLOR).asString());
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
pushBackFontElement(attributes);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (richElement)
|
|
|
|
{
|
|
|
|
if (richElement->equalType(RichElement::Type::IMAGE))
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
richElement->setColor(getColor());
|
|
|
|
auto* richElementImage = static_cast<RichElementImage*>(richElement);
|
|
|
|
richElementImage->setUrl(getURL());
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (richElement->equalType(RichElement::Type::NEWLINE))
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
richElement->setColor(getColor());
|
|
|
|
}
|
|
|
|
pushBackElement(richElement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void MyXMLVisitor::endElement(void* /*ctx*/, const char* elementName)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto it = _tagTables.find(elementName);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (it != _tagTables.end())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
auto tagBehavior = it->second;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (tagBehavior.isFontElement)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
popBackFontElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void MyXMLVisitor::textHandler(void* /*ctx*/, const char* str, size_t len)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
std::string text(str, len);
|
2021-12-25 10:04:45 +08:00
|
|
|
auto color = getColor();
|
|
|
|
auto face = getFace();
|
|
|
|
auto fontSize = getFontSize();
|
|
|
|
auto italics = getItalics();
|
|
|
|
auto underline = getUnderline();
|
2019-11-23 20:27:39 +08:00
|
|
|
auto strikethrough = getStrikethrough();
|
2021-12-25 10:04:45 +08:00
|
|
|
auto bold = getBold();
|
|
|
|
auto url = getURL();
|
|
|
|
auto outline = getOutline();
|
|
|
|
auto shadow = getShadow();
|
|
|
|
auto glow = getGlow();
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
uint32_t flags = 0;
|
|
|
|
if (italics)
|
|
|
|
flags |= RichElementText::ITALICS_FLAG;
|
|
|
|
if (bold)
|
|
|
|
flags |= RichElementText::BOLD_FLAG;
|
|
|
|
if (underline)
|
|
|
|
flags |= RichElementText::UNDERLINE_FLAG;
|
|
|
|
if (strikethrough)
|
|
|
|
flags |= RichElementText::STRIKETHROUGH_FLAG;
|
2020-08-06 17:37:34 +08:00
|
|
|
if (!url.empty())
|
2019-11-23 20:27:39 +08:00
|
|
|
flags |= RichElementText::URL_FLAG;
|
|
|
|
if (std::get<0>(outline))
|
|
|
|
flags |= RichElementText::OUTLINE_FLAG;
|
|
|
|
if (std::get<0>(shadow))
|
|
|
|
flags |= RichElementText::SHADOW_FLAG;
|
|
|
|
if (std::get<0>(glow))
|
|
|
|
flags |= RichElementText::GLOW_FLAG;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
auto element = RichElementText::create(0, color, 255, text, face, fontSize, flags, url, std::get<1>(outline),
|
|
|
|
std::get<2>(outline), std::get<1>(shadow), std::get<2>(shadow),
|
|
|
|
std::get<3>(shadow), std::get<1>(glow));
|
2019-11-23 20:27:39 +08:00
|
|
|
_richText->pushBackElement(element);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyXMLVisitor::pushBackFontElement(const MyXMLVisitor::Attributes& attribs)
|
|
|
|
{
|
|
|
|
_fontElements.push_back(attribs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyXMLVisitor::popBackFontElement()
|
|
|
|
{
|
|
|
|
_fontElements.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyXMLVisitor::pushBackElement(RichElement* element)
|
|
|
|
{
|
|
|
|
_richText->pushBackElement(element);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void MyXMLVisitor::setTagDescription(std::string_view tag,
|
2021-12-25 10:04:45 +08:00
|
|
|
bool isFontElement,
|
|
|
|
RichText::VisitEnterHandler&& handleVisitEnter)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
hlookup::set_item(
|
|
|
|
MyXMLVisitor::_tagTables, tag,
|
|
|
|
TagBehavior{
|
|
|
|
isFontElement,
|
|
|
|
std::move(
|
|
|
|
handleVisitEnter)}); // MyXMLVisitor::_tagTables[tag] = {isFontElement, std::move(handleVisitEnter)};
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void MyXMLVisitor::removeTagDescription(std::string_view tag)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
MyXMLVisitor::_tagTables.erase(tag);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
ValueMap MyXMLVisitor::tagAttrMapWithXMLElement(const char** attrs)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
ValueMap tagAttrValueMap;
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const char** attr = attrs; *attr != nullptr; attr = (attrs += 2))
|
|
|
|
{
|
|
|
|
if (attr[0] && attr[1])
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
tagAttrValueMap[attr[0]] = attr[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tagAttrValueMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string RichText::KEY_VERTICAL_SPACE("KEY_VERTICAL_SPACE");
|
|
|
|
const std::string RichText::KEY_WRAP_MODE("KEY_WRAP_MODE");
|
|
|
|
const std::string RichText::KEY_HORIZONTAL_ALIGNMENT("KEY_HORIZONTAL_ALIGNMENT");
|
|
|
|
const std::string RichText::KEY_FONT_COLOR_STRING("KEY_FONT_COLOR_STRING");
|
|
|
|
const std::string RichText::KEY_FONT_SIZE("KEY_FONT_SIZE");
|
|
|
|
const std::string RichText::KEY_FONT_SMALL("KEY_FONT_SMALL");
|
|
|
|
const std::string RichText::KEY_FONT_BIG("KEY_FONT_BIG");
|
|
|
|
const std::string RichText::KEY_FONT_FACE("KEY_FONT_FACE");
|
|
|
|
const std::string RichText::KEY_TEXT_BOLD("KEY_TEXT_BOLD");
|
|
|
|
const std::string RichText::KEY_TEXT_ITALIC("KEY_TEXT_ITALIC");
|
|
|
|
const std::string RichText::KEY_TEXT_LINE("KEY_TEXT_LINE");
|
|
|
|
const std::string RichText::VALUE_TEXT_LINE_NONE("VALUE_TEXT_LINE_NONE");
|
|
|
|
const std::string RichText::VALUE_TEXT_LINE_DEL("VALUE_TEXT_LINE_DEL");
|
|
|
|
const std::string RichText::VALUE_TEXT_LINE_UNDER("VALUE_TEXT_LINE_UNDER");
|
|
|
|
const std::string RichText::KEY_TEXT_STYLE("KEY_TEXT_STYLE");
|
|
|
|
const std::string RichText::VALUE_TEXT_STYLE_NONE("VALUE_TEXT_STYLE_NONE");
|
|
|
|
const std::string RichText::VALUE_TEXT_STYLE_OUTLINE("VALUE_TEXT_STYLE_OUTLINE");
|
|
|
|
const std::string RichText::VALUE_TEXT_STYLE_SHADOW("VALUE_TEXT_STYLE_SHADOW");
|
|
|
|
const std::string RichText::VALUE_TEXT_STYLE_GLOW("VALUE_TEXT_STYLE_GLOW");
|
|
|
|
const std::string RichText::KEY_TEXT_OUTLINE_COLOR("KEY_TEXT_OUTLINE_COLOR");
|
|
|
|
const std::string RichText::KEY_TEXT_OUTLINE_SIZE("KEY_TEXT_OUTLINE_SIZE");
|
|
|
|
const std::string RichText::KEY_TEXT_SHADOW_COLOR("KEY_TEXT_SHADOW_COLOR");
|
|
|
|
const std::string RichText::KEY_TEXT_SHADOW_OFFSET_WIDTH("KEY_TEXT_SHADOW_OFFSET_WIDTH");
|
|
|
|
const std::string RichText::KEY_TEXT_SHADOW_OFFSET_HEIGHT("KEY_TEXT_SHADOW_OFFSET_HEIGHT");
|
|
|
|
const std::string RichText::KEY_TEXT_SHADOW_BLUR_RADIUS("KEY_TEXT_SHADOW_BLUR_RADIUS");
|
|
|
|
const std::string RichText::KEY_TEXT_GLOW_COLOR("KEY_TEXT_GLOW_COLOR");
|
|
|
|
const std::string RichText::KEY_URL("KEY_URL");
|
|
|
|
const std::string RichText::KEY_ANCHOR_FONT_COLOR_STRING("KEY_ANCHOR_FONT_COLOR_STRING");
|
|
|
|
const std::string RichText::KEY_ANCHOR_TEXT_BOLD("KEY_ANCHOR_TEXT_BOLD");
|
|
|
|
const std::string RichText::KEY_ANCHOR_TEXT_ITALIC("KEY_ANCHOR_TEXT_ITALIC");
|
|
|
|
const std::string RichText::KEY_ANCHOR_TEXT_LINE("KEY_ANCHOR_TEXT_LINE");
|
|
|
|
const std::string RichText::KEY_ANCHOR_TEXT_STYLE("KEY_ANCHOR_TEXT_STYLE");
|
|
|
|
const std::string RichText::KEY_ANCHOR_TEXT_OUTLINE_COLOR("KEY_ANCHOR_TEXT_OUTLINE_COLOR");
|
|
|
|
const std::string RichText::KEY_ANCHOR_TEXT_OUTLINE_SIZE("KEY_ANCHOR_TEXT_OUTLINE_SIZE");
|
|
|
|
const std::string RichText::KEY_ANCHOR_TEXT_SHADOW_COLOR("KEY_ANCHOR_TEXT_SHADOW_COLOR");
|
|
|
|
const std::string RichText::KEY_ANCHOR_TEXT_SHADOW_OFFSET_WIDTH("KEY_ANCHOR_TEXT_SHADOW_OFFSET_WIDTH");
|
|
|
|
const std::string RichText::KEY_ANCHOR_TEXT_SHADOW_OFFSET_HEIGHT("KEY_ANCHOR_TEXT_SHADOW_OFFSET_HEIGHT");
|
|
|
|
const std::string RichText::KEY_ANCHOR_TEXT_SHADOW_BLUR_RADIUS("KEY_ANCHOR_TEXT_SHADOW_BLUR_RADIUS");
|
|
|
|
const std::string RichText::KEY_ANCHOR_TEXT_GLOW_COLOR("KEY_ANCHOR_TEXT_GLOW_COLOR");
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
RichText::RichText() : _formatTextDirty(true), _leftSpaceWidth(0.0f)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_defaults[KEY_VERTICAL_SPACE] = 0.0f;
|
|
|
|
_defaults[KEY_WRAP_MODE] = static_cast<int>(WrapMode::WRAP_PER_WORD);
|
|
|
|
_defaults[KEY_HORIZONTAL_ALIGNMENT] = static_cast<int>(HorizontalAlignment::LEFT);
|
|
|
|
_defaults[KEY_FONT_COLOR_STRING] = "#ffffff";
|
|
|
|
_defaults[KEY_FONT_SIZE] = 12.0f;
|
|
|
|
_defaults[KEY_FONT_FACE] = "Verdana";
|
2019-11-23 20:27:39 +08:00
|
|
|
_defaults[KEY_ANCHOR_FONT_COLOR_STRING] = "#0000FF";
|
2021-12-25 10:04:45 +08:00
|
|
|
_defaults[KEY_ANCHOR_TEXT_BOLD] = false;
|
|
|
|
_defaults[KEY_ANCHOR_TEXT_ITALIC] = false;
|
|
|
|
_defaults[KEY_ANCHOR_TEXT_LINE] = VALUE_TEXT_LINE_NONE;
|
|
|
|
_defaults[KEY_ANCHOR_TEXT_STYLE] = VALUE_TEXT_STYLE_NONE;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
RichText::~RichText()
|
|
|
|
{
|
|
|
|
_richElements.clear();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
RichText* RichText::create()
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
RichText* widget = new RichText();
|
|
|
|
if (widget->init())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
widget->autorelease();
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(widget);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
RichText* RichText::createWithXML(std::string_view xml, const ValueMap& defaults, const OpenUrlHandler& handleOpenUrl)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
RichText* widget = new RichText();
|
|
|
|
if (widget->initWithXML(xml, defaults, handleOpenUrl))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
widget->autorelease();
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(widget);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RichText::init()
|
|
|
|
{
|
|
|
|
if (Widget::init())
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
bool RichText::initWithXML(std::string_view origxml, const ValueMap& defaults, const OpenUrlHandler& handleOpenUrl)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
static std::function<std::string(RichText*)> startTagFont = [](RichText* richText) {
|
|
|
|
std::string fontFace = richText->getFontFace();
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << richText->getFontSize();
|
2021-12-25 10:04:45 +08:00
|
|
|
std::string fontSize = ss.str();
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string fontColor = richText->getFontColor();
|
|
|
|
return "<font face=\"" + fontFace + "\" size=\"" + fontSize + "\" color=\"" + fontColor + "\">";
|
|
|
|
};
|
|
|
|
if (Widget::init())
|
|
|
|
{
|
|
|
|
setDefaults(defaults);
|
|
|
|
setOpenUrlHandler(handleOpenUrl);
|
|
|
|
|
|
|
|
// solves to issues:
|
|
|
|
// - creates defaults values
|
|
|
|
// - makes sure that the xml well formed and starts with an element
|
|
|
|
std::string xml = startTagFont(this);
|
|
|
|
xml += origxml;
|
|
|
|
xml += "</font>";
|
|
|
|
|
|
|
|
MyXMLVisitor visitor(this);
|
|
|
|
SAXParser parser;
|
|
|
|
parser.setDelegator(&visitor);
|
|
|
|
return parser.parseIntrusive(&xml.front(), xml.length());
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void RichText::initRenderer() {}
|
|
|
|
|
|
|
|
void RichText::insertElement(RichElement* element, int index)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_richElements.insert(index, element);
|
|
|
|
_formatTextDirty = true;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
void RichText::pushBackElement(RichElement* element)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_richElements.pushBack(element);
|
|
|
|
_formatTextDirty = true;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
void RichText::removeElement(int index)
|
|
|
|
{
|
|
|
|
_richElements.erase(index);
|
|
|
|
_formatTextDirty = true;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
void RichText::removeElement(RichElement* element)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_richElements.eraseObject(element);
|
|
|
|
_formatTextDirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
RichText::WrapMode RichText::getWrapMode() const
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
return static_cast<RichText::WrapMode>(_defaults.at(KEY_WRAP_MODE).asInt());
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void RichText::setWrapMode(RichText::WrapMode wrapMode)
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
if (static_cast<RichText::WrapMode>(_defaults.at(KEY_WRAP_MODE).asInt()) != wrapMode)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_defaults[KEY_WRAP_MODE] = static_cast<int>(wrapMode);
|
2021-12-25 10:04:45 +08:00
|
|
|
_formatTextDirty = true;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RichText::HorizontalAlignment RichText::getHorizontalAlignment() const
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return static_cast<RichText::HorizontalAlignment>(_defaults.at(KEY_HORIZONTAL_ALIGNMENT).asInt());
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void RichText::setHorizontalAlignment(cocos2d::ui::RichText::HorizontalAlignment a)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (static_cast<RichText::HorizontalAlignment>(_defaults.at(KEY_HORIZONTAL_ALIGNMENT).asInt()) != a)
|
|
|
|
{
|
|
|
|
_defaults[KEY_HORIZONTAL_ALIGNMENT] = static_cast<int>(a);
|
|
|
|
_formatTextDirty = true;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void RichText::setFontColor(std::string_view color)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_defaults[KEY_FONT_COLOR_STRING] = color;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string RichText::getFontColor()
|
|
|
|
{
|
|
|
|
return _defaults.at(KEY_FONT_COLOR_STRING).asString();
|
|
|
|
}
|
|
|
|
|
|
|
|
cocos2d::Color3B RichText::getFontColor3B()
|
|
|
|
{
|
|
|
|
return color3BWithString(getFontColor());
|
|
|
|
}
|
|
|
|
|
|
|
|
void RichText::setFontSize(float size)
|
|
|
|
{
|
|
|
|
_defaults[KEY_FONT_SIZE] = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
float RichText::getFontSize()
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
return _defaults.at(KEY_FONT_SIZE).asFloat();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void RichText::setFontFace(std::string_view face)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_defaults[KEY_FONT_FACE] = face;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string RichText::getFontFace()
|
|
|
|
{
|
|
|
|
return _defaults.at(KEY_FONT_FACE).asString();
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void RichText::setAnchorFontColor(std::string_view color)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_defaults[KEY_ANCHOR_FONT_COLOR_STRING] = color;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string RichText::getAnchorFontColor()
|
|
|
|
{
|
|
|
|
return _defaults.at(KEY_ANCHOR_FONT_COLOR_STRING).asString();
|
|
|
|
}
|
|
|
|
|
|
|
|
cocos2d::Color3B RichText::getAnchorFontColor3B()
|
|
|
|
{
|
|
|
|
return color3BWithString(getAnchorFontColor());
|
|
|
|
}
|
|
|
|
|
|
|
|
void RichText::setAnchorTextBold(bool enable)
|
|
|
|
{
|
|
|
|
_defaults[KEY_ANCHOR_TEXT_BOLD] = enable;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RichText::isAnchorTextBoldEnabled()
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
return _defaults[KEY_ANCHOR_TEXT_BOLD].asBool();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void RichText::setAnchorTextItalic(bool enable)
|
|
|
|
{
|
|
|
|
_defaults[KEY_ANCHOR_TEXT_ITALIC] = enable;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RichText::isAnchorTextItalicEnabled()
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
return _defaults[KEY_ANCHOR_TEXT_ITALIC].asBool();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void RichText::setAnchorTextDel(bool enable)
|
|
|
|
{
|
|
|
|
if (enable)
|
|
|
|
_defaults[KEY_ANCHOR_TEXT_LINE] = VALUE_TEXT_LINE_DEL;
|
|
|
|
else if (_defaults[KEY_ANCHOR_TEXT_LINE].asString() == VALUE_TEXT_LINE_DEL)
|
|
|
|
_defaults[KEY_ANCHOR_TEXT_LINE] = VALUE_TEXT_LINE_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RichText::isAnchorTextDelEnabled()
|
|
|
|
{
|
|
|
|
return (_defaults[KEY_ANCHOR_TEXT_LINE].asString() == VALUE_TEXT_LINE_DEL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RichText::setAnchorTextUnderline(bool enable)
|
|
|
|
{
|
|
|
|
if (enable)
|
|
|
|
_defaults[KEY_ANCHOR_TEXT_LINE] = VALUE_TEXT_LINE_UNDER;
|
|
|
|
else if (_defaults[KEY_ANCHOR_TEXT_LINE].asString() == VALUE_TEXT_LINE_UNDER)
|
|
|
|
_defaults[KEY_ANCHOR_TEXT_LINE] = VALUE_TEXT_LINE_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RichText::isAnchorTextUnderlineEnabled()
|
|
|
|
{
|
|
|
|
return (_defaults[KEY_ANCHOR_TEXT_LINE].asString() == VALUE_TEXT_LINE_UNDER);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RichText::setAnchorTextOutline(bool enable, const Color3B& outlineColor, int outlineSize)
|
|
|
|
{
|
|
|
|
if (enable)
|
|
|
|
_defaults[KEY_ANCHOR_TEXT_STYLE] = VALUE_TEXT_STYLE_OUTLINE;
|
|
|
|
else if (_defaults[KEY_ANCHOR_TEXT_STYLE].asString() == VALUE_TEXT_STYLE_OUTLINE)
|
|
|
|
_defaults[KEY_ANCHOR_TEXT_STYLE] = VALUE_TEXT_STYLE_NONE;
|
|
|
|
_defaults[KEY_ANCHOR_TEXT_OUTLINE_COLOR] = stringWithColor3B(outlineColor);
|
2021-12-25 10:04:45 +08:00
|
|
|
_defaults[KEY_ANCHOR_TEXT_OUTLINE_SIZE] = outlineSize;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RichText::isAnchorTextOutlineEnabled()
|
|
|
|
{
|
|
|
|
return (_defaults[KEY_ANCHOR_TEXT_STYLE].asString() == VALUE_TEXT_STYLE_OUTLINE);
|
|
|
|
}
|
|
|
|
|
|
|
|
Color3B RichText::getAnchorTextOutlineColor3B()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_defaults.find(KEY_ANCHOR_TEXT_OUTLINE_COLOR) != _defaults.end())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
return color3BWithString(_defaults.at(KEY_ANCHOR_TEXT_OUTLINE_COLOR).asString());
|
|
|
|
}
|
|
|
|
return Color3B();
|
|
|
|
}
|
|
|
|
|
|
|
|
int RichText::getAnchorTextOutlineSize()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_defaults.find(KEY_ANCHOR_TEXT_OUTLINE_SIZE) != _defaults.end())
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
return _defaults.at(KEY_ANCHOR_TEXT_OUTLINE_SIZE).asInt();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-10-23 23:27:14 +08:00
|
|
|
void RichText::setAnchorTextShadow(bool enable, const Color3B& shadowColor, const Vec2& offset, int blurRadius)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (enable)
|
|
|
|
_defaults[KEY_ANCHOR_TEXT_STYLE] = VALUE_TEXT_STYLE_SHADOW;
|
|
|
|
else if (_defaults[KEY_ANCHOR_TEXT_STYLE].asString() == VALUE_TEXT_STYLE_SHADOW)
|
|
|
|
_defaults[KEY_ANCHOR_TEXT_STYLE] = VALUE_TEXT_STYLE_NONE;
|
2021-12-25 10:04:45 +08:00
|
|
|
_defaults[KEY_ANCHOR_TEXT_SHADOW_COLOR] = stringWithColor3B(shadowColor);
|
|
|
|
_defaults[KEY_ANCHOR_TEXT_SHADOW_OFFSET_WIDTH] = offset.width;
|
2019-11-23 20:27:39 +08:00
|
|
|
_defaults[KEY_ANCHOR_TEXT_SHADOW_OFFSET_HEIGHT] = offset.height;
|
2021-12-25 10:04:45 +08:00
|
|
|
_defaults[KEY_ANCHOR_TEXT_SHADOW_BLUR_RADIUS] = blurRadius;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RichText::isAnchorTextShadowEnabled()
|
|
|
|
{
|
|
|
|
return (_defaults[KEY_ANCHOR_TEXT_STYLE].asString() == VALUE_TEXT_STYLE_SHADOW);
|
|
|
|
}
|
|
|
|
|
|
|
|
Color3B RichText::getAnchorTextShadowColor3B()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_defaults.find(KEY_ANCHOR_TEXT_SHADOW_COLOR) != _defaults.end())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
return color3BWithString(_defaults.at(KEY_ANCHOR_TEXT_SHADOW_COLOR).asString());
|
|
|
|
}
|
|
|
|
return Color3B();
|
|
|
|
}
|
|
|
|
|
2021-10-23 23:27:14 +08:00
|
|
|
Vec2 RichText::getAnchorTextShadowOffset()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
float width = 2.0f;
|
2019-11-23 20:27:39 +08:00
|
|
|
float height = -2.0f;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_defaults.find(KEY_ANCHOR_TEXT_SHADOW_OFFSET_WIDTH) != _defaults.end())
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
width = _defaults.at(KEY_ANCHOR_TEXT_SHADOW_OFFSET_WIDTH).asFloat();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_defaults.find(KEY_ANCHOR_TEXT_SHADOW_OFFSET_HEIGHT) != _defaults.end())
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
height = _defaults.at(KEY_ANCHOR_TEXT_SHADOW_OFFSET_HEIGHT).asFloat();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-10-23 23:27:14 +08:00
|
|
|
return Vec2(width, height);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int RichText::getAnchorTextShadowBlurRadius()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_defaults.find(KEY_ANCHOR_TEXT_SHADOW_BLUR_RADIUS) != _defaults.end())
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
return _defaults.at(KEY_ANCHOR_TEXT_SHADOW_BLUR_RADIUS).asInt();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RichText::setAnchorTextGlow(bool enable, const Color3B& glowColor)
|
|
|
|
{
|
|
|
|
if (enable)
|
|
|
|
_defaults[KEY_ANCHOR_TEXT_STYLE] = VALUE_TEXT_STYLE_GLOW;
|
|
|
|
else if (_defaults[KEY_ANCHOR_TEXT_STYLE].asString() == VALUE_TEXT_STYLE_GLOW)
|
|
|
|
_defaults[KEY_ANCHOR_TEXT_STYLE] = VALUE_TEXT_STYLE_NONE;
|
|
|
|
_defaults[KEY_ANCHOR_TEXT_GLOW_COLOR] = stringWithColor3B(glowColor);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RichText::isAnchorTextGlowEnabled()
|
|
|
|
{
|
|
|
|
return (_defaults[KEY_ANCHOR_TEXT_STYLE].asString() == VALUE_TEXT_STYLE_GLOW);
|
|
|
|
}
|
|
|
|
|
|
|
|
Color3B RichText::getAnchorTextGlowColor3B()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_defaults.find(KEY_ANCHOR_TEXT_GLOW_COLOR) != _defaults.end())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
return color3BWithString(_defaults.at(KEY_ANCHOR_TEXT_GLOW_COLOR).asString());
|
|
|
|
}
|
|
|
|
return Color3B();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RichText::setDefaults(const ValueMap& defaults)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (defaults.find(KEY_VERTICAL_SPACE) != defaults.end())
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
_defaults[KEY_VERTICAL_SPACE] = defaults.at(KEY_VERTICAL_SPACE).asFloat();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (defaults.find(KEY_WRAP_MODE) != defaults.end())
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
_defaults[KEY_WRAP_MODE] = defaults.at(KEY_WRAP_MODE).asInt();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (defaults.find(KEY_HORIZONTAL_ALIGNMENT) != defaults.end())
|
|
|
|
{
|
|
|
|
_defaults[KEY_HORIZONTAL_ALIGNMENT] = defaults.at(KEY_HORIZONTAL_ALIGNMENT).asInt();
|
|
|
|
}
|
|
|
|
if (defaults.find(KEY_FONT_COLOR_STRING) != defaults.end())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
_defaults[KEY_FONT_COLOR_STRING] = defaults.at(KEY_FONT_COLOR_STRING).asString();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (defaults.find(KEY_FONT_SIZE) != defaults.end())
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
_defaults[KEY_FONT_SIZE] = defaults.at(KEY_FONT_SIZE).asFloat();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (defaults.find(KEY_FONT_FACE) != defaults.end())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
_defaults[KEY_FONT_FACE] = defaults.at(KEY_FONT_FACE).asString();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (defaults.find(KEY_ANCHOR_FONT_COLOR_STRING) != defaults.end())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
_defaults[KEY_ANCHOR_FONT_COLOR_STRING] = defaults.at(KEY_ANCHOR_FONT_COLOR_STRING).asString();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (defaults.find(KEY_ANCHOR_TEXT_BOLD) != defaults.end())
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
_defaults[KEY_ANCHOR_TEXT_BOLD] = defaults.at(KEY_ANCHOR_TEXT_BOLD).asBool();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (defaults.find(KEY_ANCHOR_TEXT_ITALIC) != defaults.end())
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
_defaults[KEY_ANCHOR_TEXT_ITALIC] = defaults.at(KEY_ANCHOR_TEXT_ITALIC).asBool();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (defaults.find(KEY_ANCHOR_TEXT_LINE) != defaults.end())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
_defaults[KEY_ANCHOR_TEXT_LINE] = defaults.at(KEY_ANCHOR_TEXT_LINE).asString();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (defaults.find(KEY_ANCHOR_TEXT_STYLE) != defaults.end())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
_defaults[KEY_ANCHOR_TEXT_STYLE] = defaults.at(KEY_ANCHOR_TEXT_STYLE).asString();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (defaults.find(KEY_ANCHOR_TEXT_OUTLINE_COLOR) != defaults.end())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
_defaults[KEY_ANCHOR_TEXT_OUTLINE_COLOR] = defaults.at(KEY_ANCHOR_TEXT_OUTLINE_COLOR).asString();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (defaults.find(KEY_ANCHOR_TEXT_OUTLINE_SIZE) != defaults.end())
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
_defaults[KEY_ANCHOR_TEXT_OUTLINE_SIZE] = defaults.at(KEY_ANCHOR_TEXT_OUTLINE_SIZE).asInt();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (defaults.find(KEY_ANCHOR_TEXT_SHADOW_COLOR) != defaults.end())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
_defaults[KEY_ANCHOR_TEXT_SHADOW_COLOR] = defaults.at(KEY_ANCHOR_TEXT_SHADOW_COLOR).asString();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (defaults.find(KEY_ANCHOR_TEXT_SHADOW_OFFSET_WIDTH) != defaults.end())
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
_defaults[KEY_ANCHOR_TEXT_SHADOW_OFFSET_WIDTH] = defaults.at(KEY_ANCHOR_TEXT_SHADOW_OFFSET_WIDTH).asFloat();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (defaults.find(KEY_ANCHOR_TEXT_SHADOW_OFFSET_HEIGHT) != defaults.end())
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
_defaults[KEY_ANCHOR_TEXT_SHADOW_OFFSET_HEIGHT] = defaults.at(KEY_ANCHOR_TEXT_SHADOW_OFFSET_HEIGHT).asFloat();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (defaults.find(KEY_ANCHOR_TEXT_SHADOW_BLUR_RADIUS) != defaults.end())
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
_defaults[KEY_ANCHOR_TEXT_SHADOW_BLUR_RADIUS] = defaults.at(KEY_ANCHOR_TEXT_SHADOW_BLUR_RADIUS).asInt();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (defaults.find(KEY_ANCHOR_TEXT_GLOW_COLOR) != defaults.end())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
_defaults[KEY_ANCHOR_TEXT_GLOW_COLOR] = defaults.at(KEY_ANCHOR_TEXT_GLOW_COLOR).asString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ValueMap RichText::getDefaults() const
|
|
|
|
{
|
|
|
|
ValueMap defaults;
|
|
|
|
return defaults;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
cocos2d::Color3B RichText::color3BWithString(std::string_view color)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (color.length() == 4)
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
unsigned int r, g, b;
|
|
|
|
sscanf(color.data(), "%*c%1x%1x%1x", &r, &g, &b);
|
2019-11-23 20:27:39 +08:00
|
|
|
r += r * 16;
|
|
|
|
g += g * 16;
|
|
|
|
b += b * 16;
|
|
|
|
return Color3B(r, g, b);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (color.length() == 7)
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
unsigned int r, g, b;
|
|
|
|
sscanf(color.data(), "%*c%2x%2x%2x", &r, &g, &b);
|
2019-11-23 20:27:39 +08:00
|
|
|
return Color3B(r, g, b);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (color.length() == 9)
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
unsigned int r, g, b, a;
|
|
|
|
sscanf(color.data(), "%*c%2x%2x%2x%2x", &r, &g, &b, &a);
|
2019-11-23 20:27:39 +08:00
|
|
|
return Color3B(r, g, b);
|
|
|
|
}
|
|
|
|
return Color3B::WHITE;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string RichText::stringWithColor3B(const cocos2d::Color3B& color3b)
|
|
|
|
{
|
|
|
|
int r = color3b.r;
|
|
|
|
int g = color3b.g;
|
|
|
|
int b = color3b.b;
|
|
|
|
char buf[8];
|
|
|
|
snprintf(buf, sizeof(buf), "#%02x%02x%02x", r, g, b);
|
|
|
|
return std::string(buf, 7);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string RichText::stringWithColor4B(const cocos2d::Color4B& color4b)
|
|
|
|
{
|
|
|
|
int r = color4b.r;
|
|
|
|
int g = color4b.g;
|
|
|
|
int b = color4b.b;
|
|
|
|
int a = color4b.a;
|
|
|
|
char buf[10];
|
|
|
|
snprintf(buf, sizeof(buf), "#%02x%02x%02x%02x", r, g, b, a);
|
|
|
|
return std::string(buf, 9);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void RichText::setTagDescription(std::string_view tag, bool isFontElement, VisitEnterHandler handleVisitEnter)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2020-08-06 17:37:34 +08:00
|
|
|
MyXMLVisitor::setTagDescription(tag, isFontElement, std::move(handleVisitEnter));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void RichText::removeTagDescription(std::string_view tag)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
MyXMLVisitor::removeTagDescription(tag);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void RichText::openUrl(std::string_view url)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_handleOpenUrl)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
_handleOpenUrl(url);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
Application::getInstance()->openURL(url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RichText::setOpenUrlHandler(const OpenUrlHandler& handleOpenUrl)
|
|
|
|
{
|
|
|
|
_handleOpenUrl = handleOpenUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RichText::formatText()
|
|
|
|
{
|
|
|
|
if (_formatTextDirty)
|
|
|
|
{
|
|
|
|
this->removeAllProtectedChildren();
|
|
|
|
_elementRenders.clear();
|
|
|
|
_lineHeights.clear();
|
|
|
|
if (_ignoreSize)
|
|
|
|
{
|
|
|
|
addNewLine();
|
2021-12-25 10:04:45 +08:00
|
|
|
for (ssize_t i = 0, size = _richElements.size(); i < size; ++i)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
RichElement* element = _richElements.at(i);
|
2019-11-23 20:27:39 +08:00
|
|
|
Node* elementRenderer = nullptr;
|
|
|
|
switch (element->_type)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
case RichElement::Type::TEXT:
|
|
|
|
{
|
|
|
|
RichElementText* elmtText = static_cast<RichElementText*>(element);
|
|
|
|
Label* label;
|
|
|
|
if (FileUtils::getInstance()->isFileExist(elmtText->_fontName))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
label = Label::createWithTTF(elmtText->_text, elmtText->_fontName, elmtText->_fontSize);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
label = Label::createWithSystemFont(elmtText->_text, elmtText->_fontName, elmtText->_fontSize);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (elmtText->_flags & RichElementText::ITALICS_FLAG)
|
|
|
|
label->enableItalics();
|
|
|
|
if (elmtText->_flags & RichElementText::BOLD_FLAG)
|
|
|
|
label->enableBold();
|
|
|
|
if (elmtText->_flags & RichElementText::UNDERLINE_FLAG)
|
|
|
|
label->enableUnderline();
|
|
|
|
if (elmtText->_flags & RichElementText::STRIKETHROUGH_FLAG)
|
|
|
|
label->enableStrikethrough();
|
|
|
|
if (elmtText->_flags & RichElementText::URL_FLAG)
|
|
|
|
label->addComponent(ListenerComponent::create(
|
|
|
|
label, elmtText->_url, std::bind(&RichText::openUrl, this, std::placeholders::_1)));
|
|
|
|
if (elmtText->_flags & RichElementText::OUTLINE_FLAG)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
label->enableOutline(Color4B(elmtText->_outlineColor), elmtText->_outlineSize);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (elmtText->_flags & RichElementText::SHADOW_FLAG)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
label->enableShadow(Color4B(elmtText->_shadowColor), elmtText->_shadowOffset,
|
|
|
|
elmtText->_shadowBlurRadius);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (elmtText->_flags & RichElementText::GLOW_FLAG)
|
|
|
|
{
|
|
|
|
label->enableGlow(Color4B(elmtText->_glowColor));
|
|
|
|
}
|
|
|
|
label->setTextColor(Color4B(elmtText->_color));
|
|
|
|
elementRenderer = label;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case RichElement::Type::IMAGE:
|
|
|
|
{
|
|
|
|
RichElementImage* elmtImage = static_cast<RichElementImage*>(element);
|
|
|
|
if (elmtImage->_textureType == Widget::TextureResType::LOCAL)
|
|
|
|
elementRenderer = Sprite::create(elmtImage->_filePath);
|
|
|
|
else
|
|
|
|
elementRenderer = Sprite::createWithSpriteFrameName(elmtImage->_filePath);
|
|
|
|
|
|
|
|
if (elementRenderer && (elmtImage->_height != -1 || elmtImage->_width != -1))
|
|
|
|
{
|
|
|
|
auto currentSize = elementRenderer->getContentSize();
|
|
|
|
if (elmtImage->_width != -1)
|
|
|
|
elementRenderer->setScaleX(elmtImage->_width / currentSize.width);
|
|
|
|
if (elmtImage->_height != -1)
|
|
|
|
elementRenderer->setScaleY(elmtImage->_height / currentSize.height);
|
|
|
|
elementRenderer->setContentSize(Vec2(currentSize.width * elementRenderer->getScaleX(),
|
|
|
|
currentSize.height * elementRenderer->getScaleY()));
|
|
|
|
elementRenderer->addComponent(
|
|
|
|
ListenerComponent::create(elementRenderer, elmtImage->_url,
|
|
|
|
std::bind(&RichText::openUrl, this, std::placeholders::_1)));
|
|
|
|
elementRenderer->setColor(element->_color);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case RichElement::Type::CUSTOM:
|
|
|
|
{
|
|
|
|
RichElementCustomNode* elmtCustom = static_cast<RichElementCustomNode*>(element);
|
|
|
|
elementRenderer = elmtCustom->_customNode;
|
|
|
|
elementRenderer->setColor(element->_color);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case RichElement::Type::NEWLINE:
|
|
|
|
{
|
|
|
|
addNewLine();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (elementRenderer)
|
|
|
|
{
|
|
|
|
elementRenderer->setOpacity(element->_opacity);
|
|
|
|
pushToContainer(elementRenderer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
addNewLine();
|
2021-12-25 10:04:45 +08:00
|
|
|
for (ssize_t i = 0, size = _richElements.size(); i < size; ++i)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
RichElement* element = static_cast<RichElement*>(_richElements.at(i));
|
|
|
|
switch (element->_type)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
case RichElement::Type::TEXT:
|
|
|
|
{
|
|
|
|
RichElementText* elmtText = static_cast<RichElementText*>(element);
|
|
|
|
handleTextRenderer(elmtText->_text, elmtText->_fontName, elmtText->_fontSize, elmtText->_color,
|
|
|
|
elmtText->_opacity, elmtText->_flags, elmtText->_url, elmtText->_outlineColor,
|
|
|
|
elmtText->_outlineSize, elmtText->_shadowColor, elmtText->_shadowOffset,
|
|
|
|
elmtText->_shadowBlurRadius, elmtText->_glowColor);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case RichElement::Type::IMAGE:
|
|
|
|
{
|
|
|
|
RichElementImage* elmtImage = static_cast<RichElementImage*>(element);
|
|
|
|
handleImageRenderer(elmtImage->_filePath, elmtImage->_textureType, elmtImage->_color,
|
|
|
|
elmtImage->_opacity, elmtImage->_width, elmtImage->_height, elmtImage->_url);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case RichElement::Type::CUSTOM:
|
|
|
|
{
|
|
|
|
RichElementCustomNode* elmtCustom = static_cast<RichElementCustomNode*>(element);
|
|
|
|
handleCustomRenderer(elmtCustom->_customNode);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case RichElement::Type::NEWLINE:
|
|
|
|
{
|
|
|
|
addNewLine();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
formatRenderers();
|
|
|
|
_formatTextDirty = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
inline bool isUTF8CharWrappable(const StringUtils::StringUTF8::CharUTF8& ch)
|
|
|
|
{
|
|
|
|
return (!ch.isASCII() || !std::isalnum(ch._char[0], std::locale()));
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
int getPrevWordPos(const StringUtils::StringUTF8& text, int idx)
|
|
|
|
{
|
|
|
|
if (idx <= 0)
|
|
|
|
return -1;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// start from idx-1
|
|
|
|
const StringUtils::StringUTF8::CharUTF8Store& str = text.getString();
|
|
|
|
auto it = std::find_if(str.rbegin() + (str.size() - idx + 1), str.rend(), isUTF8CharWrappable);
|
|
|
|
if (it == str.rend())
|
|
|
|
return -1;
|
|
|
|
return static_cast<int>(it.base() - str.begin());
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
int getNextWordPos(const StringUtils::StringUTF8& text, int idx)
|
|
|
|
{
|
|
|
|
const StringUtils::StringUTF8::CharUTF8Store& str = text.getString();
|
|
|
|
if (idx + 1 >= static_cast<int>(str.size()))
|
|
|
|
return static_cast<int>(str.size());
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
auto it = std::find_if(str.begin() + idx + 1, str.end(), isUTF8CharWrappable);
|
|
|
|
return static_cast<int>(it - str.begin());
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool isWrappable(const StringUtils::StringUTF8& text)
|
|
|
|
{
|
|
|
|
const StringUtils::StringUTF8::CharUTF8Store& str = text.getString();
|
|
|
|
return std::any_of(str.begin(), str.end(), isUTF8CharWrappable);
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
int findSplitPositionForWord(Label* label,
|
|
|
|
const StringUtils::StringUTF8& text,
|
|
|
|
int estimatedIdx,
|
|
|
|
float originalLeftSpaceWidth,
|
|
|
|
float newLineWidth)
|
|
|
|
{
|
|
|
|
bool startingNewLine = (newLineWidth == originalLeftSpaceWidth);
|
|
|
|
if (!isWrappable(text))
|
|
|
|
return (startingNewLine ? static_cast<int>(text.length()) : 0);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// The adjustment of the new line position
|
|
|
|
int idx = getNextWordPos(text, estimatedIdx);
|
|
|
|
std::string leftStr = text.getAsCharSequence(0, idx);
|
|
|
|
label->setString(leftStr);
|
|
|
|
float textRendererWidth = label->getContentSize().width;
|
|
|
|
if (originalLeftSpaceWidth < textRendererWidth) // Have protruding
|
|
|
|
{
|
|
|
|
while (1)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// try to erase a word
|
|
|
|
int newidx = getPrevWordPos(text, idx);
|
|
|
|
if (newidx >= 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
leftStr = text.getAsCharSequence(0, newidx);
|
|
|
|
label->setString(leftStr);
|
|
|
|
textRendererWidth = label->getContentSize().width;
|
|
|
|
if (textRendererWidth <= originalLeftSpaceWidth) // is fitted
|
|
|
|
return newidx;
|
|
|
|
idx = newidx;
|
|
|
|
continue;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
// newidx < 0 means no prev word
|
|
|
|
return (startingNewLine ? idx : 0);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else if (textRendererWidth < originalLeftSpaceWidth) // A wide margin
|
|
|
|
{
|
|
|
|
while (1)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// try to append a word
|
|
|
|
int newidx = getNextWordPos(text, idx);
|
|
|
|
leftStr = text.getAsCharSequence(0, newidx);
|
|
|
|
label->setString(leftStr);
|
|
|
|
textRendererWidth = label->getContentSize().width;
|
|
|
|
if (textRendererWidth < originalLeftSpaceWidth)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// the whole string is tested
|
|
|
|
if (newidx == static_cast<int>(text.length()))
|
|
|
|
return newidx;
|
|
|
|
idx = newidx;
|
|
|
|
continue;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
// protruded ? undo add, or quite fit
|
|
|
|
return (textRendererWidth > originalLeftSpaceWidth ? idx : newidx);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
int findSplitPositionForChar(Label* label,
|
|
|
|
const StringUtils::StringUTF8& text,
|
|
|
|
int estimatedIdx,
|
|
|
|
float originalLeftSpaceWidth,
|
|
|
|
float newLineWidth)
|
|
|
|
{
|
|
|
|
bool startingNewLine = (newLineWidth == originalLeftSpaceWidth);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
int stringLength = static_cast<int>(text.length());
|
|
|
|
int leftLength = estimatedIdx;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// The adjustment of the new line position
|
|
|
|
std::string leftStr = text.getAsCharSequence(0, leftLength);
|
|
|
|
label->setString(leftStr);
|
|
|
|
float textRendererWidth = label->getContentSize().width;
|
|
|
|
if (originalLeftSpaceWidth < textRendererWidth) // Have protruding
|
|
|
|
{
|
|
|
|
while (leftLength-- > 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// try to erase a char
|
|
|
|
auto& ch = text.getString().at(leftLength);
|
|
|
|
leftStr.erase(leftStr.end() - ch._char.length(), leftStr.end());
|
|
|
|
label->setString(leftStr);
|
|
|
|
textRendererWidth = label->getContentSize().width;
|
|
|
|
if (textRendererWidth <= originalLeftSpaceWidth) // is fitted
|
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else if (textRendererWidth < originalLeftSpaceWidth) // A wide margin
|
|
|
|
{
|
|
|
|
while (leftLength < stringLength)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// try to append a char
|
|
|
|
auto& ch = text.getString().at(leftLength);
|
|
|
|
++leftLength;
|
|
|
|
leftStr.append(ch._char);
|
|
|
|
label->setString(leftStr);
|
|
|
|
textRendererWidth = label->getContentSize().width;
|
|
|
|
if (originalLeftSpaceWidth < textRendererWidth) // protruded, undo add
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
--leftLength;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (originalLeftSpaceWidth == textRendererWidth) // quite fit
|
|
|
|
{
|
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (leftLength <= 0)
|
|
|
|
return (startingNewLine) ? 1 : 0;
|
|
|
|
return leftLength;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void RichText::handleTextRenderer(std::string_view text,
|
|
|
|
std::string_view fontName,
|
2021-12-25 10:04:45 +08:00
|
|
|
float fontSize,
|
|
|
|
const Color3B& color,
|
|
|
|
uint8_t opacity,
|
|
|
|
uint32_t flags,
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string_view url,
|
2021-12-25 10:04:45 +08:00
|
|
|
const Color3B& outlineColor,
|
|
|
|
int outlineSize,
|
|
|
|
const Color3B& shadowColor,
|
|
|
|
const Vec2& shadowOffset,
|
|
|
|
int shadowBlurRadius,
|
2019-11-23 20:27:39 +08:00
|
|
|
const Color3B& glowColor)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
bool fileExist = FileUtils::getInstance()->isFileExist(fontName);
|
2020-08-18 11:33:18 +08:00
|
|
|
RichText::WrapMode wrapMode = static_cast<RichText::WrapMode>(_defaults.at(KEY_WRAP_MODE).asInt());
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// split text by \n
|
2021-12-31 12:12:40 +08:00
|
|
|
std::stringstream ss;
|
|
|
|
ss << text;
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string currentText;
|
|
|
|
size_t realLines = 0;
|
|
|
|
while (std::getline(ss, currentText, '\n'))
|
|
|
|
{
|
|
|
|
if (realLines > 0)
|
|
|
|
{
|
|
|
|
addNewLine();
|
|
|
|
_lineHeights.back() = fontSize;
|
|
|
|
}
|
|
|
|
++realLines;
|
|
|
|
|
|
|
|
size_t splitParts = 0;
|
|
|
|
StringUtils::StringUTF8 utf8Text(currentText);
|
|
|
|
while (!currentText.empty())
|
|
|
|
{
|
|
|
|
if (splitParts > 0)
|
|
|
|
{
|
|
|
|
addNewLine();
|
|
|
|
_lineHeights.back() = fontSize;
|
|
|
|
}
|
|
|
|
++splitParts;
|
|
|
|
|
|
|
|
Label* textRenderer = fileExist ? Label::createWithTTF(currentText, fontName, fontSize)
|
2021-12-25 10:04:45 +08:00
|
|
|
: Label::createWithSystemFont(currentText, fontName, fontSize);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
if (flags & RichElementText::ITALICS_FLAG)
|
|
|
|
textRenderer->enableItalics();
|
|
|
|
if (flags & RichElementText::BOLD_FLAG)
|
|
|
|
textRenderer->enableBold();
|
|
|
|
if (flags & RichElementText::UNDERLINE_FLAG)
|
|
|
|
textRenderer->enableUnderline();
|
|
|
|
if (flags & RichElementText::STRIKETHROUGH_FLAG)
|
|
|
|
textRenderer->enableStrikethrough();
|
|
|
|
if (flags & RichElementText::URL_FLAG)
|
2021-12-25 10:04:45 +08:00
|
|
|
textRenderer->addComponent(ListenerComponent::create(
|
|
|
|
textRenderer, url, std::bind(&RichText::openUrl, this, std::placeholders::_1)));
|
2019-11-23 20:27:39 +08:00
|
|
|
if (flags & RichElementText::OUTLINE_FLAG)
|
|
|
|
textRenderer->enableOutline(Color4B(outlineColor), outlineSize);
|
|
|
|
if (flags & RichElementText::SHADOW_FLAG)
|
|
|
|
textRenderer->enableShadow(Color4B(shadowColor), shadowOffset, shadowBlurRadius);
|
|
|
|
if (flags & RichElementText::GLOW_FLAG)
|
|
|
|
textRenderer->enableGlow(Color4B(glowColor));
|
|
|
|
|
|
|
|
textRenderer->setTextColor(Color4B(color));
|
|
|
|
textRenderer->setOpacity(opacity);
|
|
|
|
|
|
|
|
// textRendererWidth will get 0.0f, when we've got glError: 0x0501 in Label::getContentSize
|
|
|
|
// It happens when currentText is very very long so that can't generate a texture
|
|
|
|
float textRendererWidth = textRenderer->getContentSize().width;
|
|
|
|
|
|
|
|
// no splitting
|
|
|
|
if (textRendererWidth > 0.0f && _leftSpaceWidth >= textRendererWidth)
|
|
|
|
{
|
|
|
|
_leftSpaceWidth -= textRendererWidth;
|
|
|
|
pushToContainer(textRenderer);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// rough estimate
|
|
|
|
// when textRendererWidth == 0.0f, use fontSize as the rough estimate of width for each char,
|
|
|
|
// (_leftSpaceWidth / fontSize) means how many chars can be aligned in leftSpaceWidth.
|
|
|
|
int estimatedIdx = 0;
|
|
|
|
if (textRendererWidth > 0.0f)
|
|
|
|
estimatedIdx = static_cast<int>(_leftSpaceWidth / textRendererWidth * utf8Text.length());
|
|
|
|
else
|
|
|
|
estimatedIdx = static_cast<int>(_leftSpaceWidth / fontSize);
|
|
|
|
|
|
|
|
int leftLength = 0;
|
|
|
|
if (wrapMode == WRAP_PER_WORD)
|
2021-12-25 10:04:45 +08:00
|
|
|
leftLength =
|
|
|
|
findSplitPositionForWord(textRenderer, utf8Text, estimatedIdx, _leftSpaceWidth, _customSize.width);
|
2019-11-23 20:27:39 +08:00
|
|
|
else
|
2021-12-25 10:04:45 +08:00
|
|
|
leftLength =
|
|
|
|
findSplitPositionForChar(textRenderer, utf8Text, estimatedIdx, _leftSpaceWidth, _customSize.width);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// split string
|
|
|
|
if (leftLength > 0)
|
|
|
|
{
|
|
|
|
textRenderer->setString(utf8Text.getAsCharSequence(0, leftLength));
|
|
|
|
pushToContainer(textRenderer);
|
|
|
|
}
|
|
|
|
|
|
|
|
StringUtils::StringUTF8::CharUTF8Store& str = utf8Text.getString();
|
|
|
|
|
|
|
|
// erase the chars which are processed
|
|
|
|
str.erase(str.begin(), str.begin() + leftLength);
|
|
|
|
currentText = utf8Text.getAsCharSequence();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void RichText::handleImageRenderer(std::string_view filePath,
|
2021-12-25 10:04:45 +08:00
|
|
|
Widget::TextureResType textureType,
|
|
|
|
const Color3B& /*color*/,
|
|
|
|
uint8_t /*opacity*/,
|
|
|
|
int width,
|
|
|
|
int height,
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string_view url)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2020-08-06 17:37:34 +08:00
|
|
|
Sprite* imageRenderer;
|
|
|
|
if (textureType == Widget::TextureResType::LOCAL)
|
|
|
|
imageRenderer = Sprite::create(filePath);
|
|
|
|
else
|
|
|
|
imageRenderer = Sprite::createWithSpriteFrameName(filePath);
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (imageRenderer)
|
|
|
|
{
|
|
|
|
auto currentSize = imageRenderer->getContentSize();
|
|
|
|
if (width != -1)
|
|
|
|
imageRenderer->setScaleX(width / currentSize.width);
|
|
|
|
if (height != -1)
|
|
|
|
imageRenderer->setScaleY(height / currentSize.height);
|
2021-12-25 10:04:45 +08:00
|
|
|
imageRenderer->setContentSize(
|
|
|
|
Vec2(currentSize.width * imageRenderer->getScaleX(), currentSize.height * imageRenderer->getScaleY()));
|
2019-11-23 20:27:39 +08:00
|
|
|
imageRenderer->setScale(1.f, 1.f);
|
|
|
|
handleCustomRenderer(imageRenderer);
|
2021-12-25 10:04:45 +08:00
|
|
|
imageRenderer->addComponent(
|
|
|
|
ListenerComponent::create(imageRenderer, url, std::bind(&RichText::openUrl, this, std::placeholders::_1)));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void RichText::handleCustomRenderer(cocos2d::Node* renderer)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-10-23 23:27:14 +08:00
|
|
|
Vec2 imgSize = renderer->getContentSize();
|
2019-11-23 20:27:39 +08:00
|
|
|
_leftSpaceWidth -= imgSize.width;
|
|
|
|
if (_leftSpaceWidth < 0.0f)
|
|
|
|
{
|
|
|
|
addNewLine();
|
|
|
|
pushToContainer(renderer);
|
|
|
|
_leftSpaceWidth -= imgSize.width;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pushToContainer(renderer);
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
void RichText::addNewLine()
|
|
|
|
{
|
|
|
|
_leftSpaceWidth = _customSize.width;
|
|
|
|
_elementRenders.emplace_back();
|
|
|
|
_lineHeights.emplace_back();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
void RichText::formatRenderers()
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
float verticalSpace = _defaults[KEY_VERTICAL_SPACE].asFloat();
|
2021-12-25 10:04:45 +08:00
|
|
|
float fontSize = _defaults[KEY_FONT_SIZE].asFloat();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
if (_ignoreSize)
|
|
|
|
{
|
|
|
|
float newContentSizeWidth = 0.0f;
|
2021-12-25 10:04:45 +08:00
|
|
|
float nextPosY = 0.0f;
|
|
|
|
std::vector<std::pair<Vector<Node*>*, float>> rowWidthPairs;
|
2019-11-23 20:27:39 +08:00
|
|
|
rowWidthPairs.reserve(_elementRenders.size());
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto& element : _elementRenders)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
float nextPosX = 0.0f;
|
2021-12-25 10:04:45 +08:00
|
|
|
float maxY = 0.0f;
|
2019-11-23 20:27:39 +08:00
|
|
|
for (auto& iter : element)
|
|
|
|
{
|
|
|
|
iter->setAnchorPoint(Vec2::ZERO);
|
|
|
|
iter->setPosition(nextPosX, nextPosY);
|
|
|
|
this->addProtectedChild(iter, 1);
|
2021-10-23 23:27:14 +08:00
|
|
|
Vec2 iSize = iter->getContentSize();
|
2019-11-23 20:27:39 +08:00
|
|
|
newContentSizeWidth += iSize.width;
|
|
|
|
nextPosX += iSize.width;
|
|
|
|
maxY = std::max(maxY, iSize.height);
|
|
|
|
}
|
|
|
|
nextPosY -= maxY;
|
|
|
|
rowWidthPairs.emplace_back(&element, nextPosX);
|
|
|
|
}
|
2021-10-23 23:27:14 +08:00
|
|
|
this->setContentSize(Vec2(newContentSizeWidth, -nextPosY));
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto& row : rowWidthPairs)
|
2019-11-23 20:27:39 +08:00
|
|
|
doHorizontalAlignment(*row.first, row.second);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// calculate real height
|
|
|
|
float newContentSizeHeight = 0.0f;
|
|
|
|
std::vector<float> maxHeights(_elementRenders.size());
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
for (size_t i = 0, size = _elementRenders.size(); i < size; i++)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
Vector<Node*>& row = _elementRenders[i];
|
2021-12-25 10:04:45 +08:00
|
|
|
float maxHeight = 0.0f;
|
2019-11-23 20:27:39 +08:00
|
|
|
for (auto& iter : row)
|
|
|
|
{
|
|
|
|
maxHeight = std::max(iter->getContentSize().height, maxHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
// gap for empty line, if _lineHeights[i] == 0, use current RichText's fontSize
|
|
|
|
if (row.empty())
|
|
|
|
{
|
|
|
|
maxHeight = (_lineHeights[i] != 0.0f ? _lineHeights[i] : fontSize);
|
|
|
|
}
|
|
|
|
maxHeights[i] = maxHeight;
|
|
|
|
|
|
|
|
// vertical space except for first line
|
|
|
|
newContentSizeHeight += (i != 0 ? maxHeight + verticalSpace : maxHeight);
|
|
|
|
}
|
|
|
|
_customSize.height = newContentSizeHeight;
|
|
|
|
|
|
|
|
// align renders
|
|
|
|
float nextPosY = _customSize.height;
|
2021-12-25 10:04:45 +08:00
|
|
|
for (size_t i = 0, size = _elementRenders.size(); i < size; i++)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
Vector<Node*>& row = _elementRenders[i];
|
2021-12-25 10:04:45 +08:00
|
|
|
float nextPosX = 0.0f;
|
2019-11-23 20:27:39 +08:00
|
|
|
nextPosY -= (i != 0 ? maxHeights[i] + verticalSpace : maxHeights[i]);
|
|
|
|
for (auto& iter : row)
|
|
|
|
{
|
|
|
|
iter->setAnchorPoint(Vec2::ZERO);
|
|
|
|
iter->setPosition(nextPosX, nextPosY);
|
|
|
|
this->addProtectedChild(iter, 1);
|
|
|
|
nextPosX += iter->getContentSize().width;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
doHorizontalAlignment(row, nextPosX);
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
_elementRenders.clear();
|
|
|
|
_lineHeights.clear();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (_ignoreSize)
|
|
|
|
{
|
2021-10-23 23:27:14 +08:00
|
|
|
Vec2 s = getVirtualRendererSize();
|
2019-11-23 20:27:39 +08:00
|
|
|
this->setContentSize(s);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->setContentSize(_customSize);
|
|
|
|
}
|
|
|
|
updateContentSizeWithTextureSize(_contentSize);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
float getPaddingAmount(const RichText::HorizontalAlignment alignment, const float leftOver)
|
|
|
|
{
|
|
|
|
switch (alignment)
|
|
|
|
{
|
|
|
|
case RichText::HorizontalAlignment::CENTER:
|
|
|
|
return leftOver / 2.f;
|
|
|
|
case RichText::HorizontalAlignment::RIGHT:
|
|
|
|
return leftOver;
|
|
|
|
default:
|
|
|
|
CCASSERT(false, "invalid horizontal alignment!");
|
|
|
|
return 0.f;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
} // namespace
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void RichText::doHorizontalAlignment(const Vector<cocos2d::Node*>& row, float rowWidth)
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
const auto alignment = static_cast<HorizontalAlignment>(_defaults.at(KEY_HORIZONTAL_ALIGNMENT).asInt());
|
2021-12-25 10:04:45 +08:00
|
|
|
if (alignment != HorizontalAlignment::LEFT)
|
|
|
|
{
|
|
|
|
const auto diff = stripTrailingWhitespace(row);
|
|
|
|
const auto leftOver = getContentSize().width - (rowWidth + diff);
|
2019-11-23 20:27:39 +08:00
|
|
|
const float leftPadding = getPaddingAmount(alignment, leftOver);
|
|
|
|
const Vec2 offset(leftPadding, 0.f);
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto& node : row)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
node->setPosition(node->getPosition() + offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
bool isWhitespace(char c)
|
|
|
|
{
|
|
|
|
return std::isspace(c, std::locale());
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
void rtrim(std::string& s)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
|
|
|
s.erase(std::find_if_not(s.rbegin(), s.rend(), isWhitespace).base(), s.end());
|
|
|
|
}
|
|
|
|
} // namespace
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
float RichText::stripTrailingWhitespace(const Vector<cocos2d::Node*>& row)
|
|
|
|
{
|
|
|
|
if (!row.empty())
|
|
|
|
{
|
|
|
|
if (auto label = dynamic_cast<Label*>(row.back()))
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
const auto width = label->getContentSize().width;
|
|
|
|
std::string trimmedString{label->getString()};
|
|
|
|
rtrim(trimmedString);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (label->getString() != trimmedString)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
label->setString(trimmedString);
|
|
|
|
return label->getContentSize().width - width;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RichText::adaptRenderers()
|
|
|
|
{
|
|
|
|
this->formatText();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void RichText::pushToContainer(cocos2d::Node* renderer)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2020-08-06 17:37:34 +08:00
|
|
|
if (_elementRenders.empty())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
_elementRenders[_elementRenders.size() - 1].pushBack(renderer);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
void RichText::setVerticalSpace(float space)
|
|
|
|
{
|
|
|
|
_defaults[KEY_VERTICAL_SPACE] = space;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RichText::ignoreContentAdaptWithSize(bool ignore)
|
|
|
|
{
|
|
|
|
if (_ignoreSize != ignore)
|
|
|
|
{
|
|
|
|
_formatTextDirty = true;
|
|
|
|
Widget::ignoreContentAdaptWithSize(ignore);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string RichText::getDescription() const
|
|
|
|
{
|
|
|
|
return "RichText";
|
|
|
|
}
|