From d3f71277577ccbcf9b4fc5feee8e47f15a7cf435 Mon Sep 17 00:00:00 2001 From: minggo Date: Wed, 19 Jul 2017 17:03:45 +0800 Subject: [PATCH] add img support --- .../core/parser/RichText.js | 24 ++++++++++++++++--- .../reader/RichtextStringVisitor.cpp | 17 +++++++++---- .../reader/RichtextStringVisitor.h | 3 ++- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/creator_project/packages/creator-luacpp-support/core/parser/RichText.js b/creator_project/packages/creator-luacpp-support/core/parser/RichText.js index 48573076..66a43433 100644 --- a/creator_project/packages/creator-luacpp-support/core/parser/RichText.js +++ b/creator_project/packages/creator-luacpp-support/core/parser/RichText.js @@ -1,6 +1,6 @@ const Node = require('./Node'); const Label = require('./Label'); -const get_font_path_by_uuid = require('./Utils').get_font_path_by_uuid; +const Utils = require('./Utils'); class RichText extends Node { constructor(data) { @@ -19,7 +19,6 @@ class RichText extends Node { // -> var text = component._N$string; let regex = /( -> \n @@ -29,6 +28,25 @@ class RichText extends Node { if (text[0] !== '<') text = '' + text + ''; + // add sprite frames if there is img component + if (component._N$imageAtlas) { + // find sprite frame name + regex = / -> + let resource_json_content = Utils.get_sprite_frame_json_by_uuid(resource_uuid); + let resource_size = {width: resource_json_content.content.originalSize[0], + height: resource_json_content.content.originalSize[1]}; + text = text.replace(/( RichtextStringVisitor::COLOR_MAP = { {"white", "#ffffff"}, @@ -64,10 +65,15 @@ void RichtextStringVisitor::startElement(void *ctx, const char *name, const char _outputXML.append("="); _outputXML.append("\""); - std::string attributeValue = convertAttributeValue(attributeName, *atts++); + std::string attributeValue = convertAttributeValue(name, attributeName, *atts++); _outputXML.append(attributeValue); _outputXML.append("\""); } + // the resource type is a sprite frame name + // creator only supports sprite frame for img tag + if (name == RichtextStringVisitor::IMG_FLAG) + _outputXML.append(" type='1' "); + _outputXML.append(">"); _addFontEndFlags.push(false); @@ -126,10 +132,13 @@ std::string RichtextStringVisitor::convertAttributeName(const std::string& tagNa return attributeName; } -std::string RichtextStringVisitor::convertAttributeValue(const std::string& attributeName, const std::string& attributeValue) const +std::string RichtextStringVisitor::convertAttributeValue(const std::string& tagName, const std::string& attributeName, const std::string& attributeValue) const { if (attributeName == "color") return convertColorString2Hex(attributeValue); - else - return attributeValue; + + if (tagName == RichtextStringVisitor::IMG_FLAG && attributeName == "src") + return attributeValue + ".png"; + + return attributeValue; } diff --git a/creator_project/packages/creator-luacpp-support/reader/RichtextStringVisitor.h b/creator_project/packages/creator-luacpp-support/reader/RichtextStringVisitor.h index a8eedb32..82e8a1b8 100644 --- a/creator_project/packages/creator-luacpp-support/reader/RichtextStringVisitor.h +++ b/creator_project/packages/creator-luacpp-support/reader/RichtextStringVisitor.h @@ -19,10 +19,11 @@ class RichtextStringVisitor : public cocos2d::SAXDelegator std::string convertColorString2Hex(const std::string& colorString) const; std::string convertAttributeName(const std::string& tagName, const std::string& attributeName) const; - std::string convertAttributeValue(const std::string& attributeName, const std::string& attributeValue) const; + std::string convertAttributeValue(const std::string& tagName, const std::string& attributeName, const std::string& attributeValue) const; const static std::string COLOR_FLAG; const static std::string SIZE_FLAG; + const static std::string IMG_FLAG; const static std::map COLOR_MAP; std::string _outputXML;