RichTextHelp.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. local RichTextHelp = class('RichTextHelp')
  2. local RTextHelper = require("sui/RTextHelper")
  3. local SRTextHelper = require("sui/SRTextHelper")
  4. local default_width = 100
  5. local default_vspace = global.ConstantConfig.DEFAULT_VSPACE
  6. local default_fontSize = global.ConstantConfig.DEFAULT_FONT_SIZE
  7. local default_fontPath = global.MMO.PATH_FONT2
  8. local default_fontColor = "#FFFFFF"
  9. function RichTextHelp:push_br(richText)
  10. richText:pushBackElement(ccui.RichElementNewLine:create(0, cc.c3b(0, 0, 0), 255))
  11. end
  12. function RichTextHelp:push_text(richText, text, colorParam, size, link, ttfConfig, fontName)
  13. local color = colorParam
  14. if type(colorParam) ~= "table" then
  15. if tonumber(colorParam) then
  16. color = GET_COLOR_BYID_C3B(tonumber(colorParam))
  17. elseif string.find(tostring(colorParam), "#") then
  18. color = GetColorFromHexString(colorParam)
  19. end
  20. end
  21. local outlineColor = ttfConfig.outlineColor or cc.c3b(0, 0, 0)
  22. local outlineSize = ttfConfig.outlineSize or 1
  23. local fontpath = fontName or global.MMO.PATH_FONT2
  24. local rich_element = nil
  25. if link then
  26. rich_element = ccui.RichElementText:create(0, color, 255, text, fontpath, size, 52, link)
  27. else
  28. rich_element = ccui.RichElementText:create(0, color, 255, text, fontpath, size, 32, "", outlineColor, outlineSize)
  29. end
  30. richText:pushBackElement(rich_element)
  31. end
  32. function RichTextHelp:set_auto_color(richText, richElement, colorParam)
  33. local auto_color = {
  34. colors = colorParam,
  35. idx = 1
  36. }
  37. if not richText.auto_color_elements then
  38. richText.auto_color_elements = {}
  39. local function callback()
  40. for a_rich_element, a_auto_color in pairs(richText.auto_color_elements) do
  41. local a_colorParam = a_auto_color.colors
  42. local a_idx = a_auto_color.idx + 1
  43. if a_idx > #a_colorParam then
  44. a_idx = 1
  45. end
  46. a_auto_color.idx = a_idx
  47. local colorParam = a_colorParam[a_idx]
  48. local color = colorParam
  49. if tonumber(colorParam) then
  50. color = GET_COLOR_BYID_C3B(tonumber(colorParam))
  51. elseif string.find(tostring(colorParam), "#") then
  52. color = GetColorFromHexString(colorParam)
  53. end
  54. richText:setElementColor(a_rich_element, color)
  55. end
  56. end
  57. schedule(richText, callback, 1)
  58. end
  59. richText.auto_color_elements[richElement] = auto_color
  60. end
  61. function RichTextHelp:push_auto_text(richText, text, colorParam, size, link, ttfConfig, fontName)
  62. local color = colorParam[1]
  63. if tonumber(color) then
  64. color = GET_COLOR_BYID_C3B(tonumber(color))
  65. elseif string.find(tostring(color), "#") then
  66. color = GetColorFromHexString(color)
  67. end
  68. local outlineColor = ttfConfig.outlineColor or cc.c3b(0, 0, 0)
  69. local outlineSize = ttfConfig.outlineSize or 1
  70. local fontpath = fontName or global.MMO.PATH_FONT2
  71. local rich_element = nil
  72. if link then
  73. rich_element = ccui.RichElementText:create(0, color, 255, text, fontpath, size, 52, link)
  74. else
  75. rich_element = ccui.RichElementText:create(0, color, 255, text, fontpath, size, 32, "", outlineColor, outlineSize)
  76. end
  77. self:set_auto_color(richText, rich_element, colorParam)
  78. richText:pushBackElement(rich_element)
  79. end
  80. function RichTextHelp:CreateRichTextWithFCOLOR(str, width, defaultSize, defaultColor, ttfConfig, callback, vspace, defaultFontFace)
  81. local vspace = vspace or default_vspace
  82. local richText = ccui.RichText:create()
  83. richText:ignoreContentAdaptWithSize(false)
  84. richText:setContentSize(width, 0)
  85. richText:setAnchorPoint(0, 1)
  86. richText:setVerticalSpace(vspace)
  87. width = width or 100
  88. defaultColor = defaultColor or cc.c3b(0, 0, 0)
  89. defaultSize = defaultSize or global.ConstantConfig.DEFAULT_FONT_SIZE
  90. ttfConfig = ttfConfig or {}
  91. local elements = RTextHelper:Parse(str)
  92. if #elements > 0 then
  93. for _, element in ipairs(elements) do
  94. if element.type == RTextHelper.Type.NEW_LINE then
  95. self:push_br(richText)
  96. elseif element.type == RTextHelper.Type.TEXT then
  97. local text = element.content
  98. local color = defaultColor
  99. self:push_text(richText, text, color, defaultSize, nil, ttfConfig, defaultFontFace)
  100. elseif element.type == RTextHelper.Type.COLOR_TEXT then
  101. local text = element.content
  102. local colorID = element.attr[RTextHelper.Attr.COLOR]
  103. self:push_text(richText, text, colorID, defaultSize, nil, ttfConfig, defaultFontFace)
  104. elseif element.type == RTextHelper.Type.AUTO_COLOR_TEXT then
  105. local text = element.content
  106. local colors = element.attr[RTextHelper.Attr.AUTO_COLOR]
  107. self:push_auto_text(richText, text, colors, defaultSize, nil, ttfConfig, defaultFontFace)
  108. elseif element.type == RTextHelper.Type.LINK_COLOR_TEXT then
  109. local text = element.content
  110. local colorID = element.attr[RTextHelper.Attr.COLOR]
  111. local link = element.attr[RTextHelper.Attr.LINK]
  112. self:push_text(richText, text, colorID, defaultSize, link, ttfConfig, defaultFontFace)
  113. elseif element.type == RTextHelper.Type.LINK_AUTO_COLOR_TEXT then
  114. local text = element.content
  115. local colors = element.attr[RTextHelper.Attr.AUTO_COLOR]
  116. local link = element.attr[RTextHelper.Attr.LINK]
  117. self:push_auto_text(richText, text, colors, defaultSize, link, ttfConfig, defaultFontFace)
  118. end
  119. end
  120. end
  121. richText:setOpenUrlHandler(function(sender, str)
  122. if not sender.originScale then
  123. sender.originScale = sender:getScale()
  124. end
  125. local originScale = sender.originScale
  126. sender:setScale(originScale + 0.2)
  127. local function reback()
  128. sender:setScale(originScale)
  129. end
  130. performWithDelay(sender, reback, 0.03)
  131. if callback then
  132. callback(str)
  133. end
  134. ssr.ssrBridge:OnRichTextOpenUrl(str)
  135. SLBridge:onLUAEvent(LUA_EVENT_RICHTEXT_OPEN_URL, str)
  136. end)
  137. richText:formatText()
  138. -- refresh UnderLine positionY
  139. if global.isWinPlayMode then
  140. local children = richText:getChildren()
  141. for _, child in ipairs(children) do
  142. if tolua.iskindof(child, "ccui.Text") then
  143. local label = child:getVirtualRenderer()
  144. if label then
  145. local labelChildren = label:getChildren()
  146. for _, labelChild in ipairs(labelChildren) do
  147. if tolua.iskindof(labelChild, "cc.DrawNode") then
  148. labelChild:setPositionY(0)
  149. end
  150. end
  151. end
  152. end
  153. end
  154. end
  155. return richText
  156. end
  157. function RichTextHelp:CreateRichTextWidthSRText(str, width, defaultSize, defaultColor, defaultFontFace, callback, vspace)
  158. defaultSize = defaultSize or global.ConstantConfig.DEFAULT_FONT_SIZE
  159. defaultColor = defaultColor or "#FFFFFF"
  160. str = SRTextHelper:convertToRichText(str, defaultSize, defaultColor)
  161. str = string.format("<outline size='0'>%s</outline>", str)
  162. return self:CreateRichTextWithXML(str, width, defaultSize, defaultColor, defaultFontFace, callback, vspace)
  163. end
  164. function RichTextHelp:CreateRichTextWithXML(str, width, defaultSize, defaultColor, defaultFontFace, callback, vspace)
  165. -- defaultColor = "#FFFFFF" 颜色采用这种格式
  166. -- callback 将 a href = "" href中的参数传回
  167. if str == nil then
  168. return self:CreateRichTextWithXML("空富文本")
  169. end
  170. if width == 0 then
  171. width = nil
  172. end
  173. if defaultSize == 0 then
  174. defaultSize = default_fontSize
  175. end
  176. str = self:TextFormat(str)
  177. width = width or 100
  178. vspace = vspace or default_vspace
  179. defaultSize = defaultSize or default_fontSize
  180. defaultColor = defaultColor or default_fontColor
  181. defaultFontFace = defaultFontFace or default_fontPath
  182. local defaults = { KEY_FONT_SIZE = defaultSize, KEY_FONT_FACE = defaultFontFace, KEY_FONT_COLOR_STRING = defaultColor, KEY_ANCHOR_FONT_COLOR_STRING = defaultColor, KEY_ANCHOR_TEXT_LINE = "VALUE_TEXT_LINE_UNDER", KEY_ANCHOR_TEXT_OUTLINE_SIZE = 1 }
  183. local richText = ccui.RichText:createWithXML(str, defaults)
  184. if not richText then
  185. print("富文本错误:", str)
  186. return self:CreateRichTextWithXML("富文本错误")
  187. end
  188. richText:setOpenUrlHandler(function(sender, str)
  189. local tokens = string.split(str, "#")
  190. if tokens[1] == "jump" then
  191. local worldPos = sender:getParent():convertToWorldSpace(cc.p(sender:getPositionX(), sender:getPositionY()))
  192. local jumpProxy = global.Facade:retrieveProxy(global.ProxyTable.JumpProxy)
  193. local command = string.sub(str, string.find(str, "#") + 1, string.len(str))
  194. jumpProxy:ExecuteByParam(command, nil, worldPos)
  195. end
  196. if callback then
  197. callback(str, richText)
  198. else
  199. if string.len(str) > 0 and string.find(str, "@") then
  200. local SUIComponentProxy = global.Facade:retrieveProxy(global.ProxyTable.SUIComponentProxy)
  201. SUIComponentProxy:SubmitAct({ Act = str })
  202. end
  203. end
  204. ssr.ssrBridge:OnRichTextOpenUrl(str)
  205. SLBridge:onLUAEvent(LUA_EVENT_RICHTEXT_OPEN_URL, str)
  206. end)
  207. richText:ignoreContentAdaptWithSize(false)
  208. richText:setContentSize(width, 0)
  209. richText:setVerticalSpace(vspace)
  210. return richText
  211. end
  212. function RichTextHelp:TextFormat(str)
  213. -- 兼容一下原先的配置
  214. --str = string.gsub(str, "'", "\"")
  215. str = string.gsub(str, "\r\n", "<br>")
  216. str = string.gsub(str, "\n", "<br>")
  217. str = string.gsub(str, "<p>", "")
  218. str = string.gsub(str, "</p>", "<br>")
  219. str = string.gsub(str, "<br></br>", "<br>")
  220. str = string.gsub(str, "<strong>", "<big>")
  221. str = string.gsub(str, "</strong>", "</big>")
  222. str = string.gsub(str, "<br>", "<br></br>")
  223. -- 换行只会生效一个问题
  224. while true do
  225. if string.find(str, "<br></br><br></br>") then
  226. str = string.gsub(str, "<br></br><br></br>", "<br></br> <br></br>")
  227. else
  228. break
  229. end
  230. end
  231. -- 元变量替换
  232. local metaValueProxy = global.Facade:retrieveProxy(global.ProxyTable.MetaValueProxy)
  233. str = metaValueProxy:MetaValueFormat(str)
  234. -- lua条件参数
  235. while true do
  236. local _, _, source, luaString = string.find(str, "(@(.-)@)")
  237. if not luaString then
  238. break
  239. end
  240. -- 不知道咋回事,策划那边的空格老有问题
  241. -- 在有function时,替换 ? 为 空格
  242. str = string.gsub(str, "%?", " ")
  243. luaString = string.gsub(luaString, "%?", " ")
  244. str = string.gsub(str, " ", " ")
  245. luaString = string.gsub(luaString, " ", " ")
  246. -- 中文,替换为英文,
  247. luaString = string.gsub(luaString, ",", ",")
  248. -- 将( ======> %(
  249. -- 将) ======> %)
  250. source = string.gsub(source, "%%", "%%%%")
  251. source = string.gsub(source, "%(", "%%(")
  252. source = string.gsub(source, "%)", "%%)")
  253. source = string.gsub(source, "%-", "%%-")
  254. source = string.gsub(source, "%+", "%%+")
  255. source = string.gsub(source, "%*", "%%*")
  256. local func = loadstring(luaString)
  257. if not func then
  258. break
  259. end
  260. local result = func()
  261. if result == nil then
  262. print("富文本错误lua执行错误:", luaString)
  263. end
  264. result = result or ""
  265. result = string.gsub(result, "%%", "%%%%")
  266. str = string.gsub(str, source, result)
  267. end
  268. return str
  269. end
  270. -- 原始字符串,不做处理
  271. function RichTextHelp:CreateDefaultRichTextWithXML(str, width, defaultSize, defaultColor, callback)
  272. -- defaultColor = "#FFFFFF" 颜色采用这种格式
  273. -- callback 将 a href = "" href中的参数传回
  274. if str == nil then
  275. return self:CreateRichTextWithXML("空富文本")
  276. end
  277. width = width or 100
  278. defaultSize = defaultSize or 100
  279. defaultColor = defaultColor or "#FFFFFF"
  280. local richText = ccui.RichText:createWithXML(str, { KEY_FONT_SIZE = defaultSize, KEY_FONT_FACE = "fonts/font2.ttf", KEY_FONT_COLOR_STRING = defaultColor, KEY_ANCHOR_FONT_COLOR_STRING = "#FFFFFF", KEY_ANCHOR_TEXT_LINE = "VALUE_TEXT_LINE_UNDER", KEY_ANCHOR_TEXT_OUTLINE_SIZE = 1 })
  281. if not richText then
  282. print("富文本错误:", str)
  283. return self:CreateRichTextWithXML("富文本错误")
  284. end
  285. richText:ignoreContentAdaptWithSize(false)
  286. richText:setContentSize(width, 0)
  287. richText:setTouchEnabled(false)
  288. return richText
  289. end
  290. return RichTextHelp