actorHUDManager.lua 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. local actorHUDManager = class("actorHUDManager")
  2. local DEFAULT_SPRITE_ID = 9999
  3. local fontSizeMob = SL:GetMetaValue("GAME_DATA","sceneFontSize") or SL:GetMetaValue("GAME_DATA","SCENE_FONT_SIZE")
  4. local fontSizeWin = SL:GetMetaValue("GAME_DATA","sceneFontSize_pc") or SL:GetMetaValue("GAME_DATA","SCENE_FONT_SIZE_PC")
  5. local fontSize = global.isWinPlayMode and fontSizeWin or fontSizeMob
  6. local HUD_LABEL_FONTS_SIZE = fontSize
  7. local HUD_LABEL_FONTS_SIZE2 = fontSize
  8. local HUD_LABEL_FONTS_SIZE3 = fontSize
  9. local HUD_LABEL_OUTLINE_SIZE = 1
  10. local HUD_SPRITE_FORMAT = "actor_hud_%04d.png"
  11. local fontZtPath = global.MMO.PATH_FONT2
  12. local DEFAULT_NODE_INDEX = global.MMO.HUD_TYPE_MAX + 1
  13. local DEFAULT_NODE_ZLOCAL = {
  14. [global.MMO.HUD_TYPE_BATCH_LABEL] = 0,
  15. [global.MMO.HUD_TYPE_SPRITE] = 1000000,
  16. [global.MMO.HUD_TYPE_ANIM] = 2000000,
  17. }
  18. function actorHUDManager:ctor()
  19. self._hudRoot = {}
  20. self._hudRootB = {}
  21. self._actorsHUD = {}
  22. self._hudCache = {}
  23. self._titleAnimCount = 0
  24. self._titleMaXCount = 50
  25. self._horseOffset = {}
  26. end
  27. function actorHUDManager:destory()
  28. if actorHUDManager.instance then
  29. actorHUDManager.instance = nil
  30. end
  31. end
  32. function actorHUDManager:Inst()
  33. if not actorHUDManager.instance then
  34. actorHUDManager.instance = actorHUDManager.new()
  35. end
  36. return actorHUDManager.instance
  37. end
  38. function actorHUDManager:checkParam(actorID, htype, index)
  39. if htype < 0 or (htype > global.MMO.HUD_TYPE_MAX) or (index <= 0) or (index > global.MMO.HUD_COUNT_MAX) then
  40. return nil
  41. end
  42. local itr = self._actorsHUD[actorID]
  43. if itr == nil then
  44. return nil
  45. end
  46. local hudArray = itr
  47. local hud = hudArray[htype * global.MMO.HUD_COUNT_MAX + index]
  48. if not hud then
  49. return nil
  50. end
  51. return hudArray
  52. end
  53. function actorHUDManager:Init()
  54. local mmo = global.MMO
  55. local actorHUDRoot = global.sceneGraphCtl:GetSceneNode(mmo.NODE_ACTOR_HUD)
  56. local actorHUDRootB = global.sceneGraphCtl:GetSceneNode(mmo.NODE_SKILL_BEHIND)
  57. self._useBmpFont = false
  58. if global.isWinPlayMode then
  59. self._useBmpFont = SL:GetMetaValue("GAME_DATA", "HudNotUseBmpFont") ~= 1
  60. else
  61. self._useBmpFont = SL:GetMetaValue("GAME_DATA", "MobileHudNotUseBmpFont") == 1
  62. end
  63. if self._useBmpFont then
  64. fontZtPath = global.MMO.PATH_ST_FONT
  65. end
  66. for i = 0, global.MMO.HUD_TYPE_MAX do
  67. if i == global.MMO.HUD_TYPE_BATCH_LABEL and not self._useBmpFont then
  68. self._hudRoot[mmo.HUD_TYPE_BATCH_LABEL] = LabelBatchNode:create( fontZtPath, HUD_LABEL_FONTS_SIZE, HUD_LABEL_OUTLINE_SIZE )
  69. self._hudRootB[mmo.HUD_TYPE_BATCH_LABEL] = LabelBatchNode:create( fontZtPath, HUD_LABEL_FONTS_SIZE, HUD_LABEL_OUTLINE_SIZE )
  70. else
  71. self._hudRoot[i] = cc.Node:create()
  72. self._hudRootB[i] = cc.Node:create()
  73. end
  74. actorHUDRoot:addChild(self._hudRoot[i])
  75. actorHUDRootB:addChild(self._hudRootB[i])
  76. end
  77. end
  78. function actorHUDManager:refCameraFontScale()
  79. if not global.isWinPlayMode
  80. and global.LuaBridgeCtl:GetModulesSwitch(global.MMO.Modules_Index_Cpp_Version) >= global.MMO.CPP_VERSION_LABEL_TTF
  81. and self._hudRoot[global.MMO.HUD_TYPE_BATCH_LABEL].refCameraFontScale then
  82. self._hudRoot[global.MMO.HUD_TYPE_BATCH_LABEL]:refCameraFontScale()
  83. self._hudRootB[global.MMO.HUD_TYPE_BATCH_LABEL]:refCameraFontScale()
  84. end
  85. end
  86. function actorHUDManager:Cleanup()
  87. self._hudRoot = {}
  88. self._hudRootB = {}
  89. self._horseOffset = {}
  90. self._titleAnimCount = 0
  91. -- only release hud cache
  92. local hud = nil
  93. for i = 0, global.MMO.HUD_TYPE_MAX do
  94. local cache = self._hudCache[i]
  95. if cache ~= nil and #cache > 0 then
  96. for k,v in pairs(cache) do
  97. hud = v
  98. if hud.type == global.MMO.HUD_TYPE_BATCH_LABEL and not self._useBmpFont then
  99. if hud.hud.cleanupGL then
  100. hud.hud:cleanupGL()
  101. end
  102. else
  103. hud.hud:release()
  104. end
  105. cache[k] = nil
  106. end
  107. end
  108. end
  109. end
  110. function actorHUDManager:newHUD()
  111. local hud = {}
  112. hud.type = 0
  113. hud.hud = nil
  114. hud.offset = cc.p(0,0)
  115. return hud
  116. end
  117. function actorHUDManager:CreateHUD(actorID, htype, index, offset, id, isBehide)
  118. if not id then id = 0 end
  119. local ret = nil
  120. if htype < 0 or htype > global.MMO.HUD_TYPE_MAX or index <= 0 or index > global.MMO.HUD_COUNT_MAX then
  121. return ret
  122. end
  123. -- 1.create HUD
  124. local hud = nil
  125. if self._hudCache[htype] and #self._hudCache[htype] > 0 then
  126. hud = table.remove(self._hudCache[htype], 1)
  127. ret = hud.hud
  128. if htype == global.MMO.HUD_TYPE_BATCH_LABEL and self._useBmpFont then
  129. if ret.setColor then
  130. ret:setColor(cc.c3b(255,255,255))
  131. end
  132. ret:setString("")
  133. end
  134. else
  135. hud = self:newHUD()
  136. if htype == global.MMO.HUD_TYPE_BATCH_LABEL then
  137. if self._useBmpFont then
  138. ret = cc.Label:createWithBMFont(fontZtPath, "")
  139. ret:retain()
  140. else
  141. ret = LabelBatchString:new(0, 2)
  142. --ret:setAnchorPoint(0.5,0)
  143. end
  144. elseif htype == global.MMO.HUD_TYPE_SPRITE then
  145. ret = ccui.Scale9Sprite:create()
  146. ret:retain()
  147. elseif htype == global.MMO.HUD_TYPE_TIPS then
  148. ret = cc.Node:create()
  149. ret:retain()
  150. elseif htype == global.MMO.HUD_TYPE_ANIM then
  151. ret = cc.Node:create()
  152. ret:retain()
  153. end
  154. end
  155. local hudRoot = isBehide and self._hudRootB or self._hudRoot
  156. -- 2. add to scene
  157. if htype == global.MMO.HUD_TYPE_BATCH_LABEL and not self._useBmpFont then
  158. local lStr = ret
  159. lStr:cleanup()
  160. hudRoot[htype]:addLabelString( lStr )
  161. elseif htype == global.MMO.HUD_TYPE_SPRITE then
  162. local sprite = ret
  163. sprite:setAnchorPoint( 0.5, 0.5 )
  164. sprite:setVisible( true )
  165. sprite:setOpacity( 255 )
  166. sprite:setTag( 0 )
  167. self:SetSpriteFrame( sprite, id )
  168. self:AddAnimCount(1)
  169. hudRoot[htype]:addChild( sprite, 0)
  170. elseif htype == global.MMO.HUD_TYPE_ANIM then
  171. local node = ret
  172. node:setVisible( true )
  173. node:setOpacity( 255 )
  174. node:setTag( 0 )
  175. self:SetAnim( node, id )
  176. self:AddAnimCount(1)
  177. hudRoot[htype]:addChild( ret, id )
  178. hud.oz = index
  179. else
  180. local node = ret
  181. node:setVisible( true )
  182. node:setOpacity( 255 )
  183. node:setTag( 0 )
  184. hudRoot[htype]:addChild( ret, 0 )
  185. end
  186. hud.hud = ret
  187. hud.type = htype
  188. hud.offset = offset
  189. hud.isBehide = isBehide
  190. -- 3. trace HUD
  191. local hudArray = nil
  192. local itr = self._actorsHUD[actorID]
  193. if nil == itr then
  194. hudArray = {}
  195. self._actorsHUD[actorID] = hudArray
  196. else
  197. hudArray = itr
  198. end
  199. hudArray[htype * global.MMO.HUD_COUNT_MAX + index] = hud
  200. return ret
  201. end
  202. function actorHUDManager:CreateHUDBar(actorID, index, offset, id, isBehide)
  203. local bar = self:CreateHUD( actorID, global.MMO.HUD_TYPE_SPRITE, index, offset, id, isBehide )
  204. bar:setAnchorPoint( 0.0, 0.0 )
  205. bar:setVisible( true )
  206. return bar
  207. end
  208. function actorHUDManager:SetHUDBarPercent( bar, percent )
  209. if percent > 1.0 then
  210. percent = 1.0
  211. end
  212. local contentSize = bar:getSpriteFrame():getOriginalSize()
  213. local hpRect = bar:getTextureRect()
  214. hpRect.width = percent * ( contentSize.width )
  215. bar:setTextureRect( hpRect, bar:isTextureRectRotated(), hpRect )
  216. end
  217. function actorHUDManager:GetHUD(actorID, htype, index, isBehide)
  218. local hudArray = self:checkParam(actorID, htype, index)
  219. if not hudArray or hudArray == 0 then
  220. return nil
  221. end
  222. local arrayIndex = htype * global.MMO.HUD_COUNT_MAX + index
  223. local hud = hudArray[arrayIndex]
  224. if isBehide ~= nil and hud.isBehide ~= isBehide then
  225. self:RemoveHUD( actorID, htype, index )
  226. return nil
  227. end
  228. return hud.hud
  229. end
  230. function actorHUDManager:setPosition(actorID, x, y)
  231. local itr = self._actorsHUD[actorID]
  232. if itr == nil then
  233. return
  234. end
  235. local horseOffset = self:GetHorseOffset( actorID )
  236. local horseOffsetLabel = self:GetHorseOffsetLabel( actorID )
  237. local hudTopOffX, hudTopOffY = 0, 0
  238. local hudArray = itr
  239. local hud = 0
  240. for i = 0, global.MMO.HUD_TYPE_MAX do
  241. for j = 1, global.MMO.HUD_COUNT_MAX do
  242. hud = hudArray[i * global.MMO.HUD_COUNT_MAX + j]
  243. if hud then
  244. if GUIFunction and GUIFunction.GetHUDTopOffset then
  245. hudTopOffX, hudTopOffY = GUIFunction:GetHUDTopOffset(actorID, i, j)
  246. end
  247. if global.MMO.HUD_TYPE_BATCH_LABEL == i and not self._useBmpFont then
  248. hud.hud:setPosition( x + hud.offset.x + horseOffsetLabel.x + hudTopOffX, y + hud.offset.y + horseOffsetLabel.y + hudTopOffY )
  249. hud.hud:setLocalZ( math.floor(-y) )
  250. else
  251. local node = hud.hud
  252. node:setPosition( x + hud.offset.x + horseOffset.x + hudTopOffX, y + hud.offset.y + horseOffset.y + hudTopOffY )
  253. if global.MMO.ACTOR_NPC ~= node:getTag() then
  254. node:setLocalZOrder( math.floor(-y) )
  255. end
  256. if self:CheckAnimCountOM() then
  257. node:setLocalZOrder(hud.oz or 0)
  258. end
  259. end
  260. end
  261. end
  262. end
  263. end
  264. function actorHUDManager:setVisible(actorID, htype, index, visible)
  265. local hudArray = self:checkParam(actorID, htype, index)
  266. if (not hudArray) or 0 == hudArray then
  267. return
  268. end
  269. local arrayIndex = htype * global.MMO.HUD_COUNT_MAX + index
  270. local hud = hudArray[arrayIndex]
  271. if global.MMO.HUD_TYPE_TIPS == htype then
  272. local childrens = hud.hud:getChildren()
  273. if #childrens > 0 then
  274. for _, v in pairs(childrens) do
  275. v:setVisible( visible )
  276. end
  277. end
  278. hud.hud:setVisible( visible )
  279. else
  280. hud.hud:setVisible( visible )
  281. end
  282. end
  283. function actorHUDManager:RemoveHUD(actorID, htype, index)
  284. local hudArray = self:checkParam( actorID, htype, index )
  285. if (not hudArray) or 0 == hudArray then
  286. return
  287. end
  288. local arrayIndex = htype * global.MMO.HUD_COUNT_MAX + index
  289. local hud = hudArray[arrayIndex]
  290. if not hud then
  291. return
  292. end
  293. if global.MMO.HUD_TYPE_BATCH_LABEL == htype and not self._useBmpFont then
  294. local hudRoot = hud.isBehide and self._hudRootB[htype] or self._hudRoot[htype]
  295. hudRoot:removeLabelString( hud.hud )
  296. elseif global.MMO.HUD_TYPE_SPRITE == htype then
  297. hud.hud:removeFromParent()
  298. self:AddAnimCount(-1)
  299. elseif global.MMO.HUD_TYPE_ANIM == htype then
  300. hud.hud:removeAllChildren()
  301. hud.hud:removeFromParent()
  302. self:AddAnimCount(-1)
  303. else
  304. hud.hud:removeFromParent()
  305. end
  306. hudArray[arrayIndex] = nil
  307. self._hudCache[htype] = self._hudCache[htype] or {}
  308. self._hudCache[htype][#self._hudCache[htype] + 1] = hud
  309. end
  310. function actorHUDManager:RemoveActorAllHUD(actorID)
  311. local itr = self._actorsHUD[actorID]
  312. if itr == nil then
  313. return
  314. end
  315. local hudArray = itr
  316. local hud = 0
  317. local arrayIndex = 0
  318. for i = 0, global.MMO.HUD_TYPE_MAX do
  319. for j = 1, global.MMO.HUD_COUNT_MAX do
  320. arrayIndex = i * global.MMO.HUD_COUNT_MAX + j
  321. hud = hudArray[arrayIndex]
  322. if hud then
  323. if global.MMO.HUD_TYPE_BATCH_LABEL == i and not self._useBmpFont then
  324. local hudRoot = hud.isBehide and self._hudRootB[i] or self._hudRoot[i]
  325. hudRoot:removeLabelString(hud.hud)
  326. elseif global.MMO.HUD_TYPE_ANIM == i then
  327. hud.hud:removeAllChildren()
  328. hud.hud:removeFromParent()
  329. self:AddAnimCount(-1)
  330. elseif global.MMO.HUD_TYPE_SPRITE == i then
  331. hud.hud:removeFromParent()
  332. self:AddAnimCount(-1)
  333. else
  334. hud.hud:removeFromParent()
  335. end
  336. hudArray[arrayIndex] = nil
  337. if not self._hudCache[i] then self._hudCache[i] = {} end
  338. self._hudCache[i] = self._hudCache[i] or {}
  339. self._hudCache[i][#self._hudCache[i] + 1] = hud
  340. end
  341. end
  342. end
  343. self._actorsHUD[actorID] = nil
  344. self._horseOffset[actorID] = nil
  345. end
  346. function actorHUDManager:GetHUDOffset(actorID, htype, index)
  347. local hudArray = self:checkParam( actorID, htype, index )
  348. if not hudArray or hudArray == 0 then
  349. return cc.p(0,0)
  350. end
  351. local hud = hudArray[htype * global.MMO.HUD_COUNT_MAX + index]
  352. return hud.offset
  353. end
  354. function actorHUDManager:SetHUDOffset(actorID, htype, index, offset)
  355. local hudArray = self:checkParam( actorID, htype, index )
  356. if not hudArray or hudArray == 0 then
  357. return
  358. end
  359. local hud = hudArray[htype * global.MMO.HUD_COUNT_MAX + index]
  360. hud.offset = offset
  361. end
  362. function actorHUDManager:SetSpriteFrame(sprite, id)
  363. if not id or id == 0 then
  364. return
  365. end
  366. local frameName = string.format( HUD_SPRITE_FORMAT, id )
  367. local spriteFrame = global.SpriteFrameCache:getSpriteFrame( frameName )
  368. if not spriteFrame then
  369. frameName = string.format( HUD_SPRITE_FORMAT, global.MMO.HUD_SPRITE_DEFAULT_ID )
  370. spriteFrame = global.SpriteFrameCache:getSpriteFrame( frameName )
  371. end
  372. sprite:setSpriteFrame( spriteFrame )
  373. end
  374. function actorHUDManager:SetAnim( node, animId )
  375. node:removeAllChildren()
  376. local anim = global.FrameAnimManager:CreateSFXAnim( animId )
  377. anim:SetGlobalElapseEnable(true)
  378. anim:setTag(animId)
  379. anim:Play( 0, 0, true )
  380. node:addChild(anim)
  381. end
  382. function actorHUDManager:AddAnimCount(count)
  383. self._titleAnimCount = self._titleAnimCount + count
  384. end
  385. function actorHUDManager:CheckAnimCountOM()
  386. return self._titleAnimCount > self._titleMaXCount
  387. end
  388. function actorHUDManager:Pick(pickPos)
  389. local count = 0
  390. for k,v in pairs(self._actorsHUD) do
  391. count = count + 1
  392. break
  393. end
  394. if count == 0 then
  395. return nil
  396. end
  397. local hudArray = 0
  398. local hud = 0
  399. local node = 0
  400. local boundBox = cc.rect(0,0,0,0)
  401. local anchorPoint = nil
  402. local contentSize = nil
  403. for k,v in pairs(self._actorsHUD) do
  404. hudArray = v
  405. hud = hudArray[global.MMO.HUD_TYPE_SPRITE * global.MMO.HUD_COUNT_MAX + global.MMO.HUD_SPRITE_TITLE_1]
  406. if hud then
  407. node = hud.hud
  408. if global.MMO.ACTOR_NPC == node:getTag() or global.MMO.ACTOR_MONSTER == node:getTag() or global.MMO.ACTOR_PLAYER == node:getTag() then
  409. anchorPoint = node:getAnchorPoint()
  410. contentSize = node:getContentSize()
  411. boundBox.width = contentSize.width
  412. boundBox.height = contentSize.height
  413. boundBox.x = node:getPositionX() - contentSize.width * anchorPoint.x
  414. boundBox.y = node:getPositionY() - contentSize.height * anchorPoint.y
  415. if cc.rectContainsPoint( boundBox, pickPos ) then
  416. return global.actorManager:GetActor( k )
  417. end
  418. end
  419. end
  420. end
  421. return nil
  422. end
  423. --------------------------------骑马时的偏移 BEGIN---------------------------------
  424. function actorHUDManager:GetHorseOffset( actorID )
  425. if not actorID or not self._horseOffset[actorID] then
  426. return cc.p(0,0)
  427. end
  428. return self._horseOffset[actorID].default or cc.p(0,0)
  429. end
  430. function actorHUDManager:SetHorseOffset( actorID,offset )
  431. if not actorID then
  432. return
  433. end
  434. if not self._horseOffset[actorID] then
  435. self._horseOffset[actorID] = {}
  436. end
  437. self._horseOffset[actorID].default = offset or cc.p(0,0)
  438. end
  439. function actorHUDManager:GetHorseOffsetLabel( actorID )
  440. if not actorID or not self._horseOffset[actorID] then
  441. return cc.p(0,0)
  442. end
  443. return self._horseOffset[actorID].labelDefault or cc.p(0,0)
  444. end
  445. function actorHUDManager:SetHorseOffsetLabel( actorID,offset )
  446. if not self._horseOffset[actorID] then
  447. self._horseOffset[actorID] = {}
  448. end
  449. self._horseOffset[actorID].labelDefault = offset or cc.p(0,0)
  450. end
  451. --------------------------------骑马时的偏移 END---------------------------------
  452. return actorHUDManager