ActorSayCommand.lua 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. local ActorSayCommand = class('ActorSayCommand', framework.SimpleCommand)
  2. local Border = 28
  3. local ContentWidth = 200
  4. local OffY = 60
  5. local CornerOffY = 96.5
  6. local contentColorID = 255
  7. local ChatParseHelp = requireView("layersui/chat_layer/ChatParseHelp")
  8. function ActorSayCommand:ctor()
  9. end
  10. local function rmv_bg_callback( sender, delta )
  11. local actorID = sender:getName()
  12. sender:stopAllActions()
  13. global.HUDManager:RemoveHUD( actorID, global.MMO.HUD_TYPE_SPRITE, global.MMO.HUD_SPRITE_TEXT_BG )
  14. end
  15. local function rmv_bg_corner_callback( sender, delta )
  16. local actorID = sender:getName()
  17. sender:stopAllActions()
  18. global.HUDManager:RemoveHUD( actorID, global.MMO.HUD_TYPE_SPRITE, global.MMO.HUD_SPRITE_TEXT_BG_CORNER )
  19. end
  20. local function rmv_content_callback( sender, delta )
  21. local actorID = sender:getName()
  22. sender:stopAllActions()
  23. sender:removeAllChildren()
  24. global.HUDManager:RemoveHUD( actorID, global.MMO.HUD_TYPE_TIPS, global.MMO.HUD_TEXT_SPEECH )
  25. end
  26. -- 普通
  27. function ActorSayCommand:createNormalElements(msg)
  28. local ChatProxy = global.Facade:retrieveProxy(global.ProxyTable.Chat)
  29. local emconfig = ChatProxy:GetEmoji()
  30. local fontPath = global.MMO.PATH_FONT_HUD2
  31. local elements = {}
  32. local fontSize = 12
  33. local outline = cc.Color4B.BLACK
  34. local outlineSize = 1
  35. msg = string.gsub(msg, "\n", " ")
  36. while string.len(msg) > 0 do
  37. local findHead = nil
  38. local findReplace = nil
  39. local findSfxId = nil
  40. for _, v in pairs(emconfig) do
  41. local replace = v.replace
  42. local sfxid = v.sfxid
  43. if not v.type or v.type == 0 then
  44. local head, _ = string.find(msg, replace)
  45. if head then
  46. if findHead then
  47. if findHead > head then
  48. findHead = head
  49. findReplace = replace
  50. findSfxId = sfxid
  51. end
  52. else
  53. findHead = head
  54. findReplace = replace
  55. findSfxId = sfxid
  56. end
  57. end
  58. end
  59. end
  60. if findHead and findReplace and findSfxId then
  61. -- 表情前面的文本截取出来
  62. local str = string.sub(msg, 1, findHead - 1)
  63. if str and string.len(str) > 0 then
  64. local element = ccui.RichElementText:create(0, cc.Color3B.WHITE, 255, str, fontPath, fontSize, 32,"",outline,outlineSize)
  65. table.insert(elements, element)
  66. end
  67. -- 表情字符串替换为空
  68. msg = string.sub(msg, findHead, -1)
  69. msg = string.gsub(msg, findReplace, "", 1)
  70. -- 创建一个表情
  71. local layout = ccui.Layout:create()
  72. layout:setContentSize(35, 35)
  73. layout:registerScriptHandler( function(state)
  74. if state == "enter" then
  75. local emojiSfx = global.FrameAnimManager:CreateSFXAnim(findSfxId)
  76. layout:removeAllChildren()
  77. layout:addChild(emojiSfx)
  78. emojiSfx:setPosition(35/2, 35/2-5)
  79. emojiSfx:setScale(0.7)
  80. emojiSfx:Play(0, 0, true)
  81. end
  82. end )
  83. local element = ccui.RichElementCustomNode:create(0, cc.Color3B.WHITE, 255, layout)
  84. table.insert(elements, element)
  85. else
  86. if msg and string.len(msg) > 0 then
  87. local element = ccui.RichElementText:create(0, cc.Color3B.WHITE, 255, msg, fontPath, fontSize, 32,"",outline,outlineSize)
  88. table.insert(elements, element)
  89. msg = ""
  90. end
  91. end
  92. end
  93. return elements
  94. end
  95. function ActorSayCommand:execute(notification)
  96. local data = notification:getBody()
  97. local actorID = data.SendId
  98. local actor = global.actorManager:GetActor( actorID )
  99. if not actor then
  100. return
  101. end
  102. local textOffY = OffY
  103. local cornerOffY = CornerOffY
  104. -----------------
  105. local content = string.format("%s:%s", data.SendName, data.Msg)
  106. local telements = self:createNormalElements(content)
  107. -- 填充
  108. local richText = ccui.RichText:create()
  109. for _, v in ipairs(telements) do
  110. richText:pushBackElement(v)
  111. end
  112. richText:ignoreContentAdaptWithSize(false)
  113. richText:setContentSize(ContentWidth, 0)
  114. richText:setAnchorPoint(0.5, 0)
  115. richText:setPosition(0, 0)
  116. richText:formatText()
  117. -------------------
  118. local hudSpeech = global.HUDManager:GetHUD( actorID, global.MMO.HUD_TYPE_TIPS, global.MMO.HUD_TEXT_SPEECH )
  119. if not hudSpeech then
  120. hudSpeech = global.HUDManager:CreateHUD( actorID, global.MMO.HUD_TYPE_TIPS, global.MMO.HUD_TEXT_SPEECH, cc.p( 0, textOffY + Border * 0.5 ) )
  121. -- hudSpeech:enableOutline( cc.Color4B.BLACK, 1 )
  122. -- hudSpeech:setMaxLineWidth( ContentWidth )
  123. -- hudSpeech:setAnchorPoint( cc.p( 0.5, 0 ) )
  124. -- hudSpeech:setColor(GET_COLOR_BYID_C3B(contentColorID))
  125. else
  126. global.HUDManager:SetHUDOffset( actorID, global.MMO.HUD_TYPE_TIPS, global.MMO.HUD_TEXT_SPEECH, cc.p( 0, textOffY + Border * 0.5 ) )
  127. end
  128. -- hudSpeech:setString( content )
  129. hudSpeech:stopAllActions()
  130. hudSpeech:removeAllChildren()
  131. hudSpeech:addChild(richText)
  132. refPositionByParent(richText)
  133. -- run action
  134. local duration = tonumber( data.time ) or 4.5
  135. self:createAction( hudSpeech, actorID, rmv_content_callback, duration )
  136. -- refresh actor position
  137. local pos = actor:getPosition()
  138. actor:setPosition( pos.x, pos.y )
  139. end
  140. function ActorSayCommand:createAction( node, actorID, callback, duration )
  141. node:stopAllActions()
  142. node:setName( actorID )
  143. local aDelay = cc.DelayTime:create( duration )
  144. node:runAction( cc.Sequence:create( aDelay, cc.CallFunc:create( callback ) ) )
  145. end
  146. return ActorSayCommand