UIStoryView.lua 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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 speekSite > 0 and curTalk.SpeakId ~= "" then
  203. table.insert(talkList,1,curTalk)
  204. self.scrollView.loopListView:SetListItemCount(#talkList, false);
  205. self.scrollView.loopListView:RefreshAllShownItem();
  206. --self.scrollView.loopListView:MovePanelToItemIndex(0, 0)
  207. else
  208. if curIdx >= #curTalkCfgDatas then
  209. --LogError("over")
  210. self:CurStoryOver()
  211. else
  212. DG.Tweening.DOTween.Kill(self.transform)
  213. local overTime = 0.5
  214. if needFihgterPerformance and not need2DPerformance then
  215. overTime = fighterAniOverTime
  216. end
  217. self.transform:DOScaleZ(1, overTime):OnComplete(function ()
  218. --LogError("next11111")
  219. DG.Tweening.DOTween.Kill(self.left.transform)
  220. self:PlayStoryPerform()
  221. end):SetAutoKill()
  222. end
  223. end
  224. end
  225. function UIStoryView:CurStoryOver()
  226. local curCfgData = self.controller:GetData()
  227. if curCfgData.SelectionTexts then
  228. --显示选择选项
  229. self:RefreshSelectionGroup()
  230. elseif curCfgData.PauseType then
  231. local pauseId = curCfgData.PauseType[1]
  232. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.UISTORY_PAUSE, pauseId)
  233. if pauseId == 1 then
  234. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UIActivityBackGift)
  235. self.selectionIdx = curCfgData.PauseType[2]
  236. end
  237. else
  238. self:StoryOver()
  239. end
  240. end
  241. function UIStoryView:GetItemByRowColumn(gridView, itemIndex, row, column)
  242. local item = gridView:NewListViewItem('StoryTalkItem')
  243. item.gameObject.name = itemIndex
  244. local itemlua = CommonUtil.BindGridViewItem2Lua(self, 'StoryTalkItem', item.gameObject)
  245. local curCfgData = self.controller:GetData()
  246. local logicData = talkList[itemIndex + 1]
  247. local data = {curCfgData, logicData,
  248. logicData.SpeakSite == 1 and self[logicData.LeftNameColour].image.sprite or self[logicData.RightNameColour].image.sprite,
  249. itemIndex == 0, item}
  250. CommonUtil.UpdateItemPrefab(self, itemlua, data)
  251. if itemIndex == 0 then
  252. if not self:GetIsDraging() then
  253. self.arrowLft:SetActive(logicData.SpeakSite == 1)
  254. self.arrowRgt:SetActive(logicData.SpeakSite == 2)
  255. end
  256. end
  257. return item
  258. end
  259. function UIStoryView:StoryOver()
  260. if isStoryOver then return end
  261. isStoryOver = true
  262. CommonUtil.ResetAnimator(self.left.animator)
  263. CommonUtil.ResetAnimator(self.right.animator)
  264. --CommonUtil.ResetAnimator(self.selectionBg.animator)
  265. self.arrowLft:SetActive(false)
  266. self.arrowRgt:SetActive(false)
  267. if self.oriFighterState ~= nil then
  268. for i = 1, #self.oriFighterState do
  269. local data = self.oriFighterState[i]
  270. LuaBattleBridge.DoFighterRotate(data.cfgId, data.camp, data.oriDir)
  271. end
  272. self.oriFighterState = nil
  273. end
  274. if ManagerContainer.UIStoryMgr:NeedNextStory() then
  275. DG.Tweening.DOTween.Kill(self.transform)
  276. local curData = ManagerContainer.UIStoryMgr:StartNextStory(self.selectionIdx)
  277. self.controller:SetData(curData)
  278. self:Init()
  279. else
  280. self.transform:DOScaleZ(1, closeDelay):OnComplete(function ()
  281. self:CloseUI()
  282. end)
  283. end
  284. end
  285. function UIStoryView:GetPlayerRoleCartoonIcon()
  286. local roleId = ManagerContainer.DataMgr.UserData:GetUserRoleId()
  287. local roleData = ManagerContainer.CfgMgr:GetRoleDataById(roleId)
  288. if roleData == nil then
  289. return ""
  290. end
  291. return roleData.CartoonImg
  292. end
  293. function UIStoryView:RefreshSelectionGroup()
  294. self.arrowLft:SetActive(false)
  295. self.arrowRgt:SetActive(false)
  296. local curCfgData = self.controller:GetData()
  297. for i = 1, 3 do
  298. local selection = self.selectionBg["selection"..i]
  299. local state = curCfgData.SelectionTexts ~= nil and curCfgData.SelectionTexts[i] ~= nil
  300. selection:SetActive(state)
  301. if state then
  302. selection.text.text.text = I18N.T(curCfgData.SelectionTexts[i])
  303. end
  304. end
  305. self.selectionBg:SetActive(true)
  306. end
  307. local function GetCurTalkData(index)
  308. local idx = 0
  309. for i = 1,#curTalkCfgDatas do
  310. if curTalkCfgDatas[i].SpeakId ~= "" and curTalkCfgDatas[i].SpeakSite > 0 then
  311. idx = idx + 1
  312. if idx == index then
  313. return curTalkCfgDatas[i]
  314. end
  315. end
  316. end
  317. return nil
  318. end
  319. function UIStoryView:PreloadAllStoryAssets()
  320. local list = {}
  321. for _,v in pairs(curTalkCfgDatas) do
  322. if v.LeftName == "player" then
  323. local icon = self:GetPlayerRoleCartoonIcon()
  324. local path = CommonUtil.GetIconsTotalPath(icon)
  325. if icon ~= "" and not CommonUtil.EleInTable(path, list) then
  326. list[#list + 1] = path
  327. end
  328. else
  329. local path = CommonUtil.GetIconsTotalPath(v.LeftImg)
  330. if v.LeftImg ~= "" and not CommonUtil.EleInTable(path, list) then
  331. list[#list + 1] = path
  332. end
  333. end
  334. if v.RightName == "player" then
  335. local icon = self:GetPlayerRoleCartoonIcon()
  336. local path = CommonUtil.GetIconsTotalPath(icon)
  337. if icon ~= "" and not CommonUtil.EleInTable(path, list) then
  338. list[#list + 1] = path
  339. end
  340. else
  341. local path = CommonUtil.GetIconsTotalPath(v.RightImg)
  342. if v.RightImg ~= "" and not CommonUtil.EleInTable(path, list) then
  343. list[#list + 1] = path
  344. end
  345. end
  346. end
  347. return list
  348. end
  349. function UIStoryView:RemoveEventListener()
  350. ManagerContainer.LuaEventMgr:Unregister(self.uiData.name)
  351. end
  352. function UIStoryView:AddUIEventListener()
  353. ManagerContainer.LuaEventMgr:RegisterUIEvent(self.uiData.name)
  354. self.uiBase:AddButtonEventListener(self.skip.button, self, self.CloseUI)
  355. self.window.uIEventTriggerListener.onPointerUp = function ()
  356. if isDraging then
  357. isDraging = false
  358. return
  359. end
  360. MusicMgr.Instance:PlayUISound(UIClickSound, false)
  361. self:OnContentClick()
  362. end
  363. self.window.uIEventTriggerListener.onPointerExit = function ()
  364. if isDraging then
  365. isDraging = false
  366. return
  367. end
  368. self:OnContentClick()
  369. end
  370. self.scrollView.uIEventTriggerListener.onPointerUp = function ()
  371. if isDraging then
  372. isDraging = false
  373. return
  374. end
  375. MusicMgr.Instance:PlayUISound(UIClickSound, false)
  376. self:OnContentClick()
  377. end
  378. self.scrollView.loopListView.mOnBeginDragAction = function ()
  379. self:OnBeginDragAction()
  380. end
  381. self.uiBase:AddButtonEventListener(self.selectionBg.selection1.button, self, self.OnSelectionClick, 1)
  382. self.uiBase:AddButtonEventListener(self.selectionBg.selection2.button, self, self.OnSelectionClick, 2)
  383. self.uiBase:AddButtonEventListener(self.selectionBg.selection3.button, self, self.OnSelectionClick, 3)
  384. end
  385. function UIStoryView:GetIsDraging()
  386. return isDraging
  387. end
  388. function UIStoryView:OnSelectionClick(button, params)
  389. local data = params.Length > 0 and params[0] or nil
  390. if data ~= nil then
  391. if data == 1 then
  392. self.selectionBg.animator:Play("SelectionA")
  393. elseif data == 2 then
  394. self.selectionBg.animator:Play("SelectionB")
  395. elseif data == 3 then
  396. self.selectionBg.animator:Play("SelectionC")
  397. end
  398. local curCfgData = self.controller:GetData()
  399. if curCfgData and curCfgData.SelectionNext then
  400. self.selectionIdx = curCfgData.SelectionNext[data]
  401. end
  402. end
  403. end
  404. function UIStoryView:StartSelectStory()
  405. DG.Tweening.DOTween.Kill(self.transform)
  406. self:Reset()
  407. local curData = ManagerContainer.UIStoryMgr:StartNextStory(self.selectionIdx)
  408. self.controller:SetData(curData)
  409. self:Init()
  410. end
  411. function UIStoryView:OnSubClose(path)
  412. local target = CommonUtil.ParseUITargetPath(self, path)
  413. if target == nil then
  414. return
  415. end
  416. if target == self.selectionBg then
  417. target:SetActive(false)
  418. local curCfgData = self.controller:GetData()
  419. if curCfgData.SelectionNext == nil then
  420. self:StoryOver()
  421. else
  422. self:StartSelectStory()
  423. end
  424. end
  425. end
  426. function UIStoryView:CloseUI()
  427. DG.Tweening.DOTween.Kill(self.transform)
  428. local curCfgData = self.controller:GetData()
  429. if curCfgData.Transition then
  430. if curCfgData.TransitionType == TransitionType.OUT or curCfgData.TransitionType == TransitionType.IN_AND_OUT then
  431. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.UISTORY_UI_START_OR_OVER, false, self.selectionIdx)
  432. end
  433. else
  434. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.UISTORY_UI_START_OR_OVER, false, self.selectionIdx)
  435. end
  436. end
  437. function UIStoryView:OnContentClick()
  438. --LogError("OnContentClick")
  439. if isStoryOver then
  440. return
  441. end
  442. --self.scrollView.loopVerticalScrollRect:MoveTo(0);
  443. if self.curTalkDsc ~= nil then
  444. if not self.curTalkOverStatus then
  445. self.curTalkOverStatus = true
  446. self.curTalkTweener:Kill(false)
  447. DG.Tweening.DOTween.Kill(self.curTalkDsc.transform)
  448. self.curTalkDsc.text = self.curTalkContent
  449. else
  450. DG.Tweening.DOTween.Kill(self.curTalkDsc.transform)
  451. self.lastNext:SetActive(false)
  452. --LogError("next3333")
  453. self:PlayStoryPerform()
  454. end
  455. end
  456. end
  457. function UIStoryView:OnBeginDragAction()
  458. --LogError("OnBeginDragAction")
  459. isDraging = true
  460. self:OnContentClick()
  461. self.arrowLft:SetActive(false)
  462. self.arrowRgt:SetActive(false)
  463. end
  464. function UIStoryView:OnHide()
  465. end
  466. function UIStoryView:OnShow(data)
  467. self.controller:SetData(data)
  468. --DG.Tweening.DOTween.Kill(self.transform)
  469. --
  470. --self:Init()
  471. end
  472. function UIStoryView:OnClose()
  473. self:Reset()
  474. end
  475. function UIStoryView:Reset()
  476. DG.Tweening.DOTween.Kill(self.transform)
  477. DG.Tweening.DOTween.Kill(self.left.transform)
  478. DG.Tweening.DOTween.Kill(self.right.transform)
  479. CommonUtil.ResetAnimator(self.left.animator)
  480. CommonUtil.ResetAnimator(self.right.animator)
  481. if self.curTalkDsc ~= nil then
  482. self.curTalkOverStatus = true
  483. self.curTalkTweener:Kill(false)
  484. DG.Tweening.DOTween.Kill(self.curTalkDsc.transform)
  485. self.curTalkDsc.text = self.curTalkContent
  486. end
  487. if self.delayTimer then
  488. self.delayTimer:Stop()
  489. self.delayTimer = nil
  490. end
  491. if assetSeqId then
  492. ManagerContainer.ResMgr:UnloadAssetBySeqId(assetSeqId)
  493. assetSeqId = nil
  494. end
  495. self.curTalkCfgDatas = nil
  496. self.lastNext = nil
  497. self.curTalkContent = nil
  498. self.curTalkDsc = nil
  499. isDraging = false
  500. storyInAnimOver = false
  501. prelaodSuccess = false
  502. end
  503. function UIStoryView:OnDispose()
  504. if self.inited then
  505. self:Reset()
  506. self.window.uIEventTriggerListener.onPointerUp = nil
  507. self.window.uIEventTriggerListener.onPointerExit = nil
  508. self.scrollView.uIEventTriggerListener.onPointerUp = nil
  509. self.scrollView.loopListView.mOnBeginDragAction = nil
  510. --self.scrollView.loopVerticalScrollRect:ClearCells()
  511. self.scrollView.loopListView:Dispose();
  512. talkList = nil
  513. end
  514. end
  515. return UIStoryView