local RichTextHelp = class('RichTextHelp')
local RTextHelper = require("sui/RTextHelper")
local SRTextHelper = require("sui/SRTextHelper")
local default_width = 100
local default_vspace = global.ConstantConfig.DEFAULT_VSPACE
local default_fontSize = global.ConstantConfig.DEFAULT_FONT_SIZE
local default_fontPath = global.MMO.PATH_FONT2
local default_fontColor = "#FFFFFF"
function RichTextHelp:push_br(richText)
richText:pushBackElement(ccui.RichElementNewLine:create(0, cc.c3b(0, 0, 0), 255))
end
function RichTextHelp:push_text(richText, text, colorParam, size, link, ttfConfig, fontName)
local color = colorParam
if type(colorParam) ~= "table" then
if tonumber(colorParam) then
color = GET_COLOR_BYID_C3B(tonumber(colorParam))
elseif string.find(tostring(colorParam), "#") then
color = GetColorFromHexString(colorParam)
end
end
local outlineColor = ttfConfig.outlineColor or cc.c3b(0, 0, 0)
local outlineSize = ttfConfig.outlineSize or 1
local fontpath = fontName or global.MMO.PATH_FONT2
local rich_element = nil
if link then
rich_element = ccui.RichElementText:create(0, color, 255, text, fontpath, size, 52, link)
else
rich_element = ccui.RichElementText:create(0, color, 255, text, fontpath, size, 32, "", outlineColor, outlineSize)
end
richText:pushBackElement(rich_element)
end
function RichTextHelp:set_auto_color(richText, richElement, colorParam)
local auto_color = {
colors = colorParam,
idx = 1
}
if not richText.auto_color_elements then
richText.auto_color_elements = {}
local function callback()
for a_rich_element, a_auto_color in pairs(richText.auto_color_elements) do
local a_colorParam = a_auto_color.colors
local a_idx = a_auto_color.idx + 1
if a_idx > #a_colorParam then
a_idx = 1
end
a_auto_color.idx = a_idx
local colorParam = a_colorParam[a_idx]
local color = colorParam
if tonumber(colorParam) then
color = GET_COLOR_BYID_C3B(tonumber(colorParam))
elseif string.find(tostring(colorParam), "#") then
color = GetColorFromHexString(colorParam)
end
richText:setElementColor(a_rich_element, color)
end
end
schedule(richText, callback, 1)
end
richText.auto_color_elements[richElement] = auto_color
end
function RichTextHelp:push_auto_text(richText, text, colorParam, size, link, ttfConfig, fontName)
local color = colorParam[1]
if tonumber(color) then
color = GET_COLOR_BYID_C3B(tonumber(color))
elseif string.find(tostring(color), "#") then
color = GetColorFromHexString(color)
end
local outlineColor = ttfConfig.outlineColor or cc.c3b(0, 0, 0)
local outlineSize = ttfConfig.outlineSize or 1
local fontpath = fontName or global.MMO.PATH_FONT2
local rich_element = nil
if link then
rich_element = ccui.RichElementText:create(0, color, 255, text, fontpath, size, 52, link)
else
rich_element = ccui.RichElementText:create(0, color, 255, text, fontpath, size, 32, "", outlineColor, outlineSize)
end
self:set_auto_color(richText, rich_element, colorParam)
richText:pushBackElement(rich_element)
end
function RichTextHelp:CreateRichTextWithFCOLOR(str, width, defaultSize, defaultColor, ttfConfig, callback, vspace, defaultFontFace)
local vspace = vspace or default_vspace
local richText = ccui.RichText:create()
richText:ignoreContentAdaptWithSize(false)
richText:setContentSize(width, 0)
richText:setAnchorPoint(0, 1)
richText:setVerticalSpace(vspace)
width = width or 100
defaultColor = defaultColor or cc.c3b(0, 0, 0)
defaultSize = defaultSize or global.ConstantConfig.DEFAULT_FONT_SIZE
ttfConfig = ttfConfig or {}
local elements = RTextHelper:Parse(str)
if #elements > 0 then
for _, element in ipairs(elements) do
if element.type == RTextHelper.Type.NEW_LINE then
self:push_br(richText)
elseif element.type == RTextHelper.Type.TEXT then
local text = element.content
local color = defaultColor
self:push_text(richText, text, color, defaultSize, nil, ttfConfig, defaultFontFace)
elseif element.type == RTextHelper.Type.COLOR_TEXT then
local text = element.content
local colorID = element.attr[RTextHelper.Attr.COLOR]
self:push_text(richText, text, colorID, defaultSize, nil, ttfConfig, defaultFontFace)
elseif element.type == RTextHelper.Type.AUTO_COLOR_TEXT then
local text = element.content
local colors = element.attr[RTextHelper.Attr.AUTO_COLOR]
self:push_auto_text(richText, text, colors, defaultSize, nil, ttfConfig, defaultFontFace)
elseif element.type == RTextHelper.Type.LINK_COLOR_TEXT then
local text = element.content
local colorID = element.attr[RTextHelper.Attr.COLOR]
local link = element.attr[RTextHelper.Attr.LINK]
self:push_text(richText, text, colorID, defaultSize, link, ttfConfig, defaultFontFace)
elseif element.type == RTextHelper.Type.LINK_AUTO_COLOR_TEXT then
local text = element.content
local colors = element.attr[RTextHelper.Attr.AUTO_COLOR]
local link = element.attr[RTextHelper.Attr.LINK]
self:push_auto_text(richText, text, colors, defaultSize, link, ttfConfig, defaultFontFace)
end
end
end
richText:setOpenUrlHandler(function(sender, str)
if not sender.originScale then
sender.originScale = sender:getScale()
end
local originScale = sender.originScale
sender:setScale(originScale + 0.2)
local function reback()
sender:setScale(originScale)
end
performWithDelay(sender, reback, 0.03)
if callback then
callback(str)
end
ssr.ssrBridge:OnRichTextOpenUrl(str)
SLBridge:onLUAEvent(LUA_EVENT_RICHTEXT_OPEN_URL, str)
end)
richText:formatText()
-- refresh UnderLine positionY
if global.isWinPlayMode then
local children = richText:getChildren()
for _, child in ipairs(children) do
if tolua.iskindof(child, "ccui.Text") then
local label = child:getVirtualRenderer()
if label then
local labelChildren = label:getChildren()
for _, labelChild in ipairs(labelChildren) do
if tolua.iskindof(labelChild, "cc.DrawNode") then
labelChild:setPositionY(0)
end
end
end
end
end
end
return richText
end
function RichTextHelp:CreateRichTextWidthSRText(str, width, defaultSize, defaultColor, defaultFontFace, callback, vspace)
defaultSize = defaultSize or global.ConstantConfig.DEFAULT_FONT_SIZE
defaultColor = defaultColor or "#FFFFFF"
str = SRTextHelper:convertToRichText(str, defaultSize, defaultColor)
str = string.format("
")
str = string.gsub(str, "\n", "
")
str = string.gsub(str, "
", "") str = string.gsub(str, "
", "