UIStoryView.lua 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. local UIStoryView = require("UIStory/UIStoryView_Generate")
  2. local closeDelay = 1
  3. local curIdx = 0
  4. local isStoryOver = false
  5. local prelaodSuccess = false
  6. local storyInAnimOver = false
  7. local curTalkCfgDatas
  8. local assetSeqId
  9. local isDraging = false
  10. local talkList = {}
  11. local TransitionType = {
  12. IN = 1,
  13. OUT = 2,
  14. IN_AND_OUT = 3,
  15. }
  16. local UIClickSound
  17. function UIStoryView:OnAwake(data)
  18. self.controller = require("UIStory/UIStoryCtr"):new()
  19. self.controller:Init(self)
  20. self.controller:SetData(data)
  21. end
  22. function UIStoryView:AddEventListener()
  23. ManagerContainer.LuaEventMgr:RegisterUIEvent(self.uiBase.name, UIEventNames.UISTORY_CONTINUE, self, self.StartSelectStory)
  24. end
  25. function UIStoryView:FillContent(data, uiBase)
  26. self.uiBase = uiBase
  27. local gameObject = self.uiBase:GetRoot()
  28. if gameObject ~= nil then
  29. self.gameObject = gameObject
  30. self.transform = gameObject.transform
  31. end
  32. self:InitGenerate(self.transform, data)
  33. self:InitOnce()
  34. self:Init()
  35. end
  36. function UIStoryView:InitOnce()
  37. UIClickSound = GlobalConfig.Instance:GetConfigStrValue(176)
  38. self.scrollView.loopListView:InitListView(0, function(gridView, itemIndex, row, column)
  39. return self:GetItemByRowColumn(gridView, itemIndex, row, column)
  40. end, nil)
  41. end
  42. function UIStoryView:Init()
  43. self.selectionBg:SetActive(false)
  44. self.selectionIdx = nil
  45. isStoryOver = false
  46. self.curTalkOverStatus = false
  47. local curCfgData = self.controller:GetData()
  48. -- LogError(Inspect(curCfgData))
  49. curTalkCfgDatas = ManagerContainer.CfgMgr:GetStoryPerformDataById(curCfgData.StoryId)
  50. -- LogError(Inspect(curTalkCfgDatas))
  51. if curCfgData.Transition then
  52. if curCfgData.TransitionType == TransitionType.IN or curCfgData.TransitionType == TransitionType.IN_AND_OUT then
  53. self.darkScreen:SetActive(true)
  54. self.transform:DOScale(1, 0.5):OnComplete(function ()
  55. storyInAnimOver = true
  56. self:StartStory()
  57. end)
  58. end
  59. else
  60. storyInAnimOver = true
  61. self:StartStory()
  62. end
  63. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.UISTORY_UI_START_OR_OVER, true)
  64. self.skip:SetActive(curCfgData.Skip)
  65. self.bgMove:SetActive(curCfgData.NeedBg)
  66. local preloadAssets = self:PreloadAllStoryAssets(curCfgData.StoryId)
  67. if #preloadAssets > 0 then
  68. assetSeqId = ManagerContainer.ResMgr:LuaLoadAssets(Enum.ResourceType.Sprite, nil, preloadAssets, nil, function()
  69. if ManagerContainer.ResMgr:SeqIdEquals(assetSeqId, 0) then
  70. assetSeqId = nil
  71. end
  72. prelaodSuccess = true
  73. self:StartStory()
  74. end)
  75. else
  76. prelaodSuccess = true
  77. end
  78. self.arrowLft:SetActive(false)
  79. self.arrowRgt:SetActive(false)
  80. end
  81. function UIStoryView:StartStory()
  82. if not storyInAnimOver or not prelaodSuccess then return end
  83. self.darkScreen:SetActive(false)
  84. curIdx = 0
  85. --LogError("StartStory")
  86. self.delayTimer = FrameTimer.New(function()
  87. self:PlayStoryPerform()
  88. end, 1)
  89. self.delayTimer:Start()
  90. end
  91. function UIStoryView:PlayStoryPerform()
  92. if self.controller == nil then return end
  93. curIdx = curIdx + 1
  94. --LogError("PlayStoryPerform "..curIdx)
  95. local curTalk = curTalkCfgDatas[curIdx]
  96. if curTalk == nil then
  97. self:CurStoryOver()
  98. return
  99. end
  100. local needFihgterPerformance = false
  101. local need2DPerformance = false
  102. local fighterAniOverTime = curTalk.OverTime > 0 and curTalk.OverTime/100 or 0
  103. if curTalk.Camp ~= nil then
  104. needFihgterPerformance = true
  105. self.oriFighterState = {}
  106. for i = 1, #curTalk.Camp do
  107. local camp = curTalk.Camp[i] or 0
  108. local id = curTalk.CfgId[i]
  109. local dir = curTalk.Dir[i] or 0
  110. local dirStartTime = curTalk.DirStartTime[i] or 0
  111. local aniName = curTalk.AniName[i]
  112. local aniStartTime = curTalk.AniStartTime[i] or 0
  113. local eff = curTalk.Eff[i]
  114. local effStartTime = curTalk.EffStartTime[i] or 0
  115. if id == nil then
  116. break
  117. end
  118. local cfgId = id
  119. if id == 1 then
  120. cfgId = ManagerContainer.DataMgr.UserData:GetUserRoleId()
  121. end
  122. local data = {cfgId = cfgId, camp = camp, oriDir = LuaBattleBridge.GetOriFighterRotate(cfgId, camp)}
  123. self.oriFighterState[i] = data
  124. self.left.transform:DOScaleZ(1, dirStartTime):OnComplete(function ()
  125. LuaBattleBridge.DoFighterRotate(cfgId, camp, dir)
  126. end):SetAutoKill()
  127. if aniName ~= "" then
  128. self.left.transform:DOScaleZ(1, aniStartTime):OnComplete(function ()
  129. LuaBattleBridge.DoFighterPlayAnimation(cfgId, camp, aniName)
  130. end):SetAutoKill()
  131. end
  132. if eff ~= "" and eff ~= 0 then
  133. self.left.transform:DOScaleZ(1, effStartTime):OnComplete(function ()
  134. LuaBattleBridge.DoFighterPlayEffect(cfgId, camp, eff)
  135. end):SetAutoKill()
  136. end
  137. end
  138. else
  139. self.oriFighterState = nil
  140. end
  141. local leftSprite
  142. if curTalk.LeftName == "player" then
  143. need2DPerformance = true
  144. local icon = self:GetPlayerRoleCartoonIcon()
  145. leftSprite = ManagerContainer.ResMgr:LuaFindSpriteByPathAndName(nil, CommonUtil.GetIconsTotalPath(icon))
  146. else
  147. if curTalk.LeftImg ~= "" then
  148. need2DPerformance = true
  149. leftSprite = ManagerContainer.ResMgr:LuaFindSpriteByPathAndName(nil, CommonUtil.GetIconsTotalPath(curTalk.LeftImg))
  150. end
  151. end
  152. if leftSprite ~= nil then
  153. need2DPerformance = true
  154. self.left.role.image.sprite = leftSprite
  155. end
  156. local rightSprite
  157. if curTalk.RightName == "player" then
  158. need2DPerformance = true
  159. local icon = self:GetPlayerRoleCartoonIcon()
  160. rightSprite = ManagerContainer.ResMgr:LuaFindSpriteByPathAndName(nil, CommonUtil.GetIconsTotalPath(icon))
  161. else
  162. if curTalk.RightImg ~= "" then
  163. need2DPerformance = true
  164. rightSprite = ManagerContainer.ResMgr:LuaFindSpriteByPathAndName(nil, CommonUtil.GetIconsTotalPath(curTalk.RightImg))
  165. end
  166. end
  167. if rightSprite ~= nil then
  168. need2DPerformance = true
  169. self.right.role.image.sprite = rightSprite
  170. end
  171. if curTalk.LeftAct ~= "" then
  172. need2DPerformance = true
  173. if curTalk.LeftActBgTime > 0 then
  174. self.left.transform:DOScaleZ(1, curTalk.LeftActBgTime*0.001):OnComplete(function ()
  175. self.left.animator:Play(curTalk.LeftAct)
  176. end)
  177. else
  178. self.left.animator:Play(curTalk.LeftAct)
  179. end
  180. end
  181. if curTalk.RightAct ~= "" then
  182. need2DPerformance = true
  183. if curTalk.RightActBgTime > 0 then
  184. self.right.transform:DOScaleZ(1, curTalk.RightActBgTime*0.001):OnComplete(function ()
  185. self.right.animator:Play(curTalk.RightAct)
  186. end)
  187. else
  188. self.right.animator:Play(curTalk.RightAct)
  189. end
  190. end
  191. local speekSite = 0
  192. if curTalk.SpeakId ~= "" then
  193. need2DPerformance = true
  194. if curTalk.SpeakSite > 0 then
  195. speekSite = curTalk.SpeakSite
  196. end
  197. end
  198. self.curTalkContent = nil
  199. self.curTalkDsc = nil
  200. self.curTalkDscContent = nil
  201. self.curTalkOverStatus = false
  202. if talkList == nil then
  203. --LogError("talkList = nil")
  204. talkList = {}
  205. end
  206. if speekSite > 0 and curTalk.SpeakId ~= "" then
  207. table.insert(talkList,1,curTalk)
  208. self.scrollView.loopListView:SetListItemCount(#talkList, false);
  209. self.scrollView.loopListView:RefreshAllShownItem();
  210. --self.scrollView.loopListView:MovePanelToItemIndex(0, 0)
  211. else
  212. if curIdx >= #curTalkCfgDatas then
  213. --LogError("over")
  214. self:CurStoryOver()
  215. else
  216. DG.Tweening.DOTween.Kill(self.transform)
  217. local overTime = 0.5
  218. if needFihgterPerformance and not need2DPerformance then
  219. overTime = fighterAniOverTime
  220. end
  221. self.transform:DOScaleZ(1, overTime):OnComplete(function ()
  222. --LogError("next11111")
  223. DG.Tweening.DOTween.Kill(self.left.transform)
  224. self:PlayStoryPerform()
  225. end):SetAutoKill()
  226. end
  227. end
  228. end
  229. function UIStoryView:CurStoryOver()
  230. local curCfgData = self.controller:GetData()
  231. if curCfgData.SelectionTexts then
  232. --显示选择选项
  233. self:RefreshSelectionGroup()
  234. elseif curCfgData.PauseType then
  235. local pauseId = curCfgData.PauseType[1]
  236. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.UISTORY_PAUSE, pauseId)
  237. if pauseId == 1 then
  238. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UIActivityBackGift)
  239. self.selectionIdx = curCfgData.PauseType[2]
  240. end
  241. else
  242. self:StoryOver()
  243. end
  244. end
  245. function UIStoryView:GetItemByRowColumn(gridView, itemIndex, row, column)
  246. local item = gridView:NewListViewItem('StoryTalkItem')
  247. item.gameObject.name = itemIndex
  248. local itemlua = CommonUtil.BindGridViewItem2Lua(self, 'StoryTalkItem', item.gameObject)
  249. local curCfgData = self.controller:GetData()
  250. local logicData = talkList[itemIndex + 1]
  251. local data = {curCfgData, logicData,
  252. logicData.SpeakSite == 1 and self[logicData.LeftNameColour].image.sprite or self[logicData.RightNameColour].image.sprite,
  253. itemIndex == 0, item}
  254. CommonUtil.UpdateItemPrefab(self, itemlua, data)
  255. if itemIndex == 0 then
  256. if not self:GetIsDraging() then
  257. self.arrowLft:SetActive(logicData.SpeakSite == 1)
  258. self.arrowRgt:SetActive(logicData.SpeakSite == 2)
  259. end
  260. end
  261. return item
  262. end
  263. function UIStoryView:StoryOver()
  264. if isStoryOver then return end
  265. isStoryOver = true
  266. CommonUtil.ResetAnimator(self.left.animator)
  267. CommonUtil.ResetAnimator(self.right.animator)
  268. --CommonUtil.ResetAnimator(self.selectionBg.animator)
  269. self.arrowLft:SetActive(false)
  270. self.arrowRgt:SetActive(false)
  271. if self.oriFighterState ~= nil then
  272. for i = 1, #self.oriFighterState do
  273. local data = self.oriFighterState[i]
  274. LuaBattleBridge.DoFighterRotate(data.cfgId, data.camp, data.oriDir)
  275. end
  276. self.oriFighterState = nil
  277. end
  278. if ManagerContainer.UIStoryMgr:NeedNextStory() then
  279. DG.Tweening.DOTween.Kill(self.transform)
  280. local curData = ManagerContainer.UIStoryMgr:StartNextStory(self.selectionIdx)
  281. self.controller:SetData(curData)
  282. self:Init()
  283. else
  284. self.transform:DOScaleZ(1, closeDelay):OnComplete(function ()
  285. self:CloseUI()
  286. end)
  287. end
  288. end
  289. function UIStoryView:GetPlayerRoleCartoonIcon()
  290. local roleId = ManagerContainer.DataMgr.UserData:GetUserRoleId()
  291. local roleData = ManagerContainer.CfgMgr:GetRoleDataById(roleId)
  292. if roleData == nil then
  293. return ""
  294. end
  295. return roleData.CartoonImg
  296. end
  297. function UIStoryView:RefreshSelectionGroup()
  298. self.arrowLft:SetActive(false)
  299. self.arrowRgt:SetActive(false)
  300. local curCfgData = self.controller:GetData()
  301. for i = 1, 3 do
  302. local selection = self.selectionBg["selection"..i]
  303. local state = curCfgData.SelectionTexts ~= nil and curCfgData.SelectionTexts[i] ~= nil
  304. selection:SetActive(state)
  305. if state then
  306. selection.text.text.text = I18N.T(curCfgData.SelectionTexts[i])
  307. end
  308. end
  309. self.selectionBg:SetActive(true)
  310. end
  311. local function GetCurTalkData(index)
  312. local idx = 0
  313. for i = 1,#curTalkCfgDatas do
  314. if curTalkCfgDatas[i].SpeakId ~= "" and curTalkCfgDatas[i].SpeakSite > 0 then
  315. idx = idx + 1
  316. if idx == index then
  317. return curTalkCfgDatas[i]
  318. end
  319. end
  320. end
  321. return nil
  322. end
  323. function UIStoryView:PreloadAllStoryAssets()
  324. local list = {}
  325. for _,v in pairs(curTalkCfgDatas) do
  326. if v.LeftName == "player" then
  327. local icon = self:GetPlayerRoleCartoonIcon()
  328. local path = CommonUtil.GetIconsTotalPath(icon)
  329. if icon ~= "" and not CommonUtil.EleInTable(path, list) then
  330. list[#list + 1] = path
  331. end
  332. else
  333. local path = CommonUtil.GetIconsTotalPath(v.LeftImg)
  334. if v.LeftImg ~= "" and not CommonUtil.EleInTable(path, list) then
  335. list[#list + 1] = path
  336. end
  337. end
  338. if v.RightName == "player" then
  339. local icon = self:GetPlayerRoleCartoonIcon()
  340. local path = CommonUtil.GetIconsTotalPath(icon)
  341. if icon ~= "" and not CommonUtil.EleInTable(path, list) then
  342. list[#list + 1] = path
  343. end
  344. else
  345. local path = CommonUtil.GetIconsTotalPath(v.RightImg)
  346. if v.RightImg ~= "" and not CommonUtil.EleInTable(path, list) then
  347. list[#list + 1] = path
  348. end
  349. end
  350. end
  351. return list
  352. end
  353. function UIStoryView:RemoveEventListener()
  354. ManagerContainer.LuaEventMgr:Unregister(self.uiData.name)
  355. end
  356. function UIStoryView:AddUIEventListener()
  357. ManagerContainer.LuaEventMgr:RegisterUIEvent(self.uiData.name)
  358. self.uiBase:AddButtonEventListener(self.skip.button, self, self.CloseUI)
  359. self.window.uIEventTriggerListener.onPointerUp = function ()
  360. if isDraging then
  361. isDraging = false
  362. return
  363. end
  364. MusicMgr.Instance:PlayUISound(UIClickSound, false)
  365. self:OnContentClick()
  366. end
  367. self.window.uIEventTriggerListener.onPointerExit = function ()
  368. if isDraging then
  369. isDraging = false
  370. return
  371. end
  372. self:OnContentClick()
  373. end
  374. self.scrollView.uIEventTriggerListener.onPointerUp = function ()
  375. if isDraging then
  376. isDraging = false
  377. return
  378. end
  379. MusicMgr.Instance:PlayUISound(UIClickSound, false)
  380. self:OnContentClick()
  381. end
  382. self.scrollView.loopListView.mOnBeginDragAction = function ()
  383. self:OnBeginDragAction()
  384. end
  385. self.uiBase:AddButtonEventListener(self.selectionBg.selection1.button, self, self.OnSelectionClick, 1)
  386. self.uiBase:AddButtonEventListener(self.selectionBg.selection2.button, self, self.OnSelectionClick, 2)
  387. self.uiBase:AddButtonEventListener(self.selectionBg.selection3.button, self, self.OnSelectionClick, 3)
  388. end
  389. function UIStoryView:GetIsDraging()
  390. return isDraging
  391. end
  392. function UIStoryView:OnSelectionClick(button, params)
  393. local data = params.Length > 0 and params[0] or nil
  394. if data ~= nil then
  395. if data == 1 then
  396. self.selectionBg.animator:Play("SelectionA")
  397. elseif data == 2 then
  398. self.selectionBg.animator:Play("SelectionB")
  399. elseif data == 3 then
  400. self.selectionBg.animator:Play("SelectionC")
  401. end
  402. local curCfgData = self.controller:GetData()
  403. if curCfgData and curCfgData.SelectionNext then
  404. self.selectionIdx = curCfgData.SelectionNext[data]
  405. end
  406. end
  407. end
  408. function UIStoryView:StartSelectStory()
  409. DG.Tweening.DOTween.Kill(self.transform)
  410. self:Reset()
  411. local curData = ManagerContainer.UIStoryMgr:StartNextStory(self.selectionIdx)
  412. self.controller:SetData(curData)
  413. self:Init()
  414. end
  415. function UIStoryView:OnSubClose(path)
  416. local target = CommonUtil.ParseUITargetPath(self, path)
  417. if target == nil then
  418. return
  419. end
  420. if target == self.selectionBg then
  421. target:SetActive(false)
  422. local curCfgData = self.controller:GetData()
  423. if curCfgData.SelectionNext == nil then
  424. self:StoryOver()
  425. else
  426. self:StartSelectStory()
  427. end
  428. end
  429. end
  430. function UIStoryView:CloseUI()
  431. DG.Tweening.DOTween.Kill(self.transform)
  432. local curCfgData = self.controller:GetData()
  433. if curCfgData.Transition then
  434. if curCfgData.TransitionType == TransitionType.OUT or curCfgData.TransitionType == TransitionType.IN_AND_OUT then
  435. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.UISTORY_UI_START_OR_OVER, false, self.selectionIdx)
  436. end
  437. else
  438. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.UISTORY_UI_START_OR_OVER, false, self.selectionIdx)
  439. end
  440. end
  441. function UIStoryView:OnContentClick()
  442. --LogError("OnContentClick")
  443. if isStoryOver then
  444. return
  445. end
  446. --self.scrollView.loopVerticalScrollRect:MoveTo(0);
  447. if self.curTalkDsc ~= nil then
  448. if not self.curTalkOverStatus then
  449. self.curTalkOverStatus = true
  450. self.curTalkTweener:Kill(false)
  451. DG.Tweening.DOTween.Kill(self.curTalkDsc.transform)
  452. self.curTalkDsc.text = self.curTalkContent
  453. else
  454. DG.Tweening.DOTween.Kill(self.curTalkDsc.transform)
  455. self.lastNext:SetActive(false)
  456. --LogError("next3333")
  457. self:PlayStoryPerform()
  458. end
  459. end
  460. end
  461. function UIStoryView:OnBeginDragAction()
  462. --LogError("OnBeginDragAction")
  463. isDraging = true
  464. self:OnContentClick()
  465. self.arrowLft:SetActive(false)
  466. self.arrowRgt:SetActive(false)
  467. end
  468. function UIStoryView:OnHide()
  469. end
  470. function UIStoryView:OnShow(data)
  471. self.controller:SetData(data)
  472. --DG.Tweening.DOTween.Kill(self.transform)
  473. --
  474. --self:Init()
  475. end
  476. function UIStoryView:OnClose()
  477. self:Reset()
  478. end
  479. function UIStoryView:Reset()
  480. DG.Tweening.DOTween.Kill(self.transform)
  481. DG.Tweening.DOTween.Kill(self.left.transform)
  482. DG.Tweening.DOTween.Kill(self.right.transform)
  483. CommonUtil.ResetAnimator(self.left.animator)
  484. CommonUtil.ResetAnimator(self.right.animator)
  485. if self.curTalkDsc ~= nil then
  486. self.curTalkOverStatus = true
  487. self.curTalkTweener:Kill(false)
  488. DG.Tweening.DOTween.Kill(self.curTalkDsc.transform)
  489. self.curTalkDsc.text = self.curTalkContent
  490. end
  491. if self.delayTimer then
  492. self.delayTimer:Stop()
  493. self.delayTimer = nil
  494. end
  495. if assetSeqId then
  496. ManagerContainer.ResMgr:UnloadAssetBySeqId(assetSeqId)
  497. assetSeqId = nil
  498. end
  499. self.curTalkCfgDatas = nil
  500. self.lastNext = nil
  501. self.curTalkContent = nil
  502. self.curTalkDsc = nil
  503. isDraging = false
  504. storyInAnimOver = false
  505. prelaodSuccess = false
  506. end
  507. function UIStoryView:OnDispose()
  508. if self.inited then
  509. self:Reset()
  510. self.window.uIEventTriggerListener.onPointerUp = nil
  511. self.window.uIEventTriggerListener.onPointerExit = nil
  512. self.scrollView.uIEventTriggerListener.onPointerUp = nil
  513. self.scrollView.loopListView.mOnBeginDragAction = nil
  514. --self.scrollView.loopVerticalScrollRect:ClearCells()
  515. self.scrollView.loopListView:Dispose();
  516. --LogError("释放 talkList")
  517. talkList = nil
  518. end
  519. end
  520. return UIStoryView