RichTextHelpEx.lua 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. local RichTextHelpEx = class("RichTextHelpEx")
  2. local RTextHelperEx = require("util/RTextHelperEx")
  3. local RichTextHelp = require("util/RichTextHelp")
  4. local default_width = 100
  5. local default_vspace = SL:GetMetaValue("GAME_DATA","DEFAULT_VSPACE")
  6. local default_fontSize = SL:GetMetaValue("GAME_DATA","DEFAULT_FONT_SIZE")
  7. local default_fontPath = global.MMO.PATH_FONT2
  8. local default_fontColor = "#FFFFFF"
  9. function RichTextHelpEx:addElementBr(richText)
  10. richText:pushBackElement(ccui.RichElementNewLine:create(0, cc.c3b(0, 0, 0), 255))
  11. end
  12. function RichTextHelpEx:addElementItem(richText, attr)
  13. local itemID = attr.itemID
  14. local bgVisible = attr.bgVisible == 1
  15. local size = attr.size or {width = 60, height = 60}
  16. local nWidth = size.width + 5
  17. local node = ccui.Layout:create()
  18. node:setContentSize({width = nWidth, height = size.height})
  19. local goodItem = GoodsItem:create({index = itemID, look = true, bgVisible = bgVisible})
  20. node:addChild(goodItem)
  21. local realSize = goodItem:getContentSize()
  22. goodItem:setScaleX(size.width / realSize.width)
  23. goodItem:setScaleY(size.height / realSize.height)
  24. goodItem:setPosition({x = nWidth / 2, y = size.height / 2})
  25. richText:pushBackElement(ccui.RichElementCustomNode:create(0, cc.c3b(0, 0, 0), 255, node))
  26. end
  27. function RichTextHelpEx:addElementEffect(richText, attr)
  28. local sfxID = attr.sfxID
  29. local scale = (attr.scale or 100) / 100
  30. local speed = attr.speed or 1
  31. local x = attr.x
  32. local y = attr.y
  33. local node = cc.Node:create()
  34. local effect = global.FrameAnimManager:CreateSFXAnim(sfxID)
  35. effect:Play(0, 0, true, speed)
  36. node:addChild(effect)
  37. local bBox = effect:GetBoundingBox()
  38. local width = scale * bBox.width + 5
  39. local height = scale * bBox.height
  40. if not x then
  41. x = width / 2
  42. end
  43. if not y then
  44. y = height / 2
  45. end
  46. node:setContentSize({width = width, height = height})
  47. effect:setPosition({x = x, y = y})
  48. effect:setScale(scale)
  49. richText:pushBackElement(ccui.RichElementCustomNode:create(0, cc.c3b(0, 0, 0), 255, node))
  50. end
  51. function RichTextHelpEx:addElementText(richText, text, attr)
  52. local outlineColor = cc.c3b(255, 255, 255)
  53. local outlineSize = 0
  54. local color = attr.color or default_fontColor
  55. local size = attr.size or default_fontSize
  56. local font = attr.fpath or default_fontPath
  57. local link = attr.link
  58. local rich_element = nil
  59. if link then
  60. rich_element = ccui.RichElementText:create(0, GetColorFromHexString(color), 255, text, font, size, 52, link)
  61. else
  62. rich_element = ccui.RichElementText:create(0, GetColorFromHexString(color), 255, text, font, size, 32, "", outlineColor, outlineSize)
  63. end
  64. richText:pushBackElement(rich_element)
  65. end
  66. function RichTextHelpEx:CreateRichTextWithCustom(str, width, vspace)
  67. if string.len(str or "") < 1 then
  68. return false
  69. end
  70. width = width or default_width
  71. vspace = vspace or default_vspace
  72. local richText = ccui.RichText:create()
  73. richText:ignoreContentAdaptWithSize(false)
  74. richText:setContentSize(width, 0)
  75. richText:setAnchorPoint(0, 1)
  76. richText:setVerticalSpace(vspace)
  77. str = RichTextHelpEx:TextFormat(str)
  78. local elements = RTextHelperEx:ParseCustomRText(str)
  79. for _, element in ipairs(elements) do
  80. if element.type == RTextHelperEx.Type.NEW_LINE then
  81. self:addElementBr(richText)
  82. elseif element.type == RTextHelperEx.Type.TEXT then
  83. self:addElementText(richText, element.content, element.attr)
  84. elseif element.type == RTextHelperEx.Type.ITEM then
  85. self:addElementItem(richText, element.attr)
  86. elseif element.type == RTextHelperEx.Type.EFFECT then
  87. self:addElementEffect(richText, element.attr)
  88. end
  89. end
  90. richText:formatText()
  91. return richText
  92. end
  93. return RichTextHelpEx