UIBuyTimesView.lua 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. local UIBuyTimesView = require("UICommonTips/UIBuyTimesView_Generate")
  2. function UIBuyTimesView:OnAwake(data)
  3. self.controller = require("UICommonTips/UIBuyTimesCtr"):new()
  4. self.controller:Init(self)
  5. self.controller:SetData(data)
  6. --LogError("========UIBuyTimesView OnAwake================"..Inspect(data))
  7. end
  8. function UIBuyTimesView:AddEventListener()
  9. ManagerContainer.LuaEventMgr:RegisterUIEvent(self.uiData.name)
  10. end
  11. function UIBuyTimesView:FillContent(data, uiBase)
  12. self.uiBase = uiBase
  13. local gameObject = self.uiBase:GetRoot()
  14. if gameObject ~= nil then
  15. self.gameObject = gameObject
  16. self.transform = gameObject.transform
  17. end
  18. self:InitGenerate(self.transform, data)
  19. self:Init()
  20. self.BuyCount = 1
  21. self:SetUIData()
  22. end
  23. function UIBuyTimesView:Init()
  24. end
  25. function UIBuyTimesView:RefreshCostGold()
  26. if not self.controller:IsGuildDemon() then
  27. local cost = self.controller:GetCostNum()
  28. self.costText.text.text = tostring(cost * self.BuyCount)
  29. if not self.controller:IsCanBuy(self.BuyCount) then
  30. CommonUtil.SetTextColor(self.costText.text, Constant.RedColorText)
  31. end
  32. else
  33. local cost = self.controller:GetCostNumByGuildDemon(self.BuyCount)
  34. self.costText.text.text = tostring(cost)
  35. if not self.controller:IsCanBuyByGuildDemon(cost) then
  36. CommonUtil.SetTextColor(self.costText.text, Constant.RedColorText)
  37. end
  38. end
  39. end
  40. function UIBuyTimesView:SetUIData()
  41. local remainCount = self.controller:GetRemainCount()
  42. if remainCount then
  43. self.leftTimesText.text.text = tostring(remainCount)
  44. end
  45. if not self.controller:IsOver() then
  46. self:SetSliderMinMax(1,remainCount)
  47. if remainCount <= 1 then
  48. self:SetSliderMinMax(0,remainCount)
  49. end
  50. self.number.text.text = tostring(self.BuyCount)
  51. self.slider.slider.value = self.BuyCount
  52. else
  53. self:SetSliderMinMax(0,0)
  54. self.BuyCount = 0
  55. self.number.text.text = tostring(self.BuyCount)
  56. end
  57. self:RefreshCostGold()
  58. end
  59. function UIBuyTimesView:RemoveEventListener()
  60. ManagerContainer.LuaEventMgr:Unregister(self.uiData.name)
  61. self.slider.slider.onValueChanged:RemoveAllListeners()
  62. end
  63. function UIBuyTimesView:AddUIEventListener()
  64. self.uiBase:AddButtonEventListener(self.buyBtn.button,self, self.OnClickBuy)
  65. self.uiBase:AddButtonEventListener(self.closeBtn.button,self, self.OnClickClose)
  66. self.uiBase:AddButtonEventListener(self.AnyBtn.button,self, self.OnClickClose)
  67. self.uiBase:AddButtonEventListener(self.btnMinus.button,self, self.OnReduceClick)
  68. self.uiBase:AddButtonEventListener(self.btnPlus.button,self, self.OnSumClick)
  69. self.slider.slider.onValueChanged:RemoveAllListeners()
  70. self.slider.slider.onValueChanged:AddListener(function(value)
  71. self:OnValueChangedSlider(value)
  72. end)
  73. end
  74. function UIBuyTimesView:SetSliderMinMax(Min,Max)
  75. self.slider.slider.wholeNumbers = true
  76. self.slider.slider.minValue = Min
  77. self.slider.slider.maxValue = Max
  78. end
  79. function UIBuyTimesView:OnValueChangedSlider(value)
  80. local remainCount = self.controller:GetRemainCount()
  81. if not self.controller:IsOver() then
  82. if remainCount <= 1 then
  83. self.slider.slider.value = 1
  84. self.BuyCount = remainCount
  85. else
  86. self.BuyCount = value
  87. end
  88. self.number.text.text = tostring(self.BuyCount)
  89. else
  90. self.slider.slider.value = 1
  91. self.BuyCount = 0
  92. self.number.text.text = tostring(self.BuyCount)
  93. end
  94. self:RefreshCostGold()
  95. end
  96. function UIBuyTimesView:OnReduceClick()
  97. local remainCount = self.controller:GetRemainCount()
  98. self.BuyCount = self.BuyCount - 1
  99. if self.BuyCount < 1 then
  100. self.BuyCount = 1
  101. end
  102. if remainCount > 1 then
  103. self.slider.slider.value = self.BuyCount
  104. end
  105. end
  106. function UIBuyTimesView:OnSumClick()
  107. local remainCount = self.controller:GetRemainCount()
  108. self.BuyCount = self.BuyCount + 1
  109. if self.BuyCount > remainCount then
  110. self.BuyCount = remainCount
  111. end
  112. if remainCount > 1 then
  113. self.slider.slider.value = self.BuyCount
  114. end
  115. end
  116. function UIBuyTimesView:OnClickBuy()
  117. if self.controller:IsOver() then
  118. --购买次数达到上限
  119. CommonUtil.PopErrorTips('312')
  120. else
  121. --是否金钱足够购买
  122. if not self.controller:IsCanBuy(self.BuyCount) then
  123. CommonUtil.PopErrorTips('313')
  124. else
  125. self.controller:HandleCallback(self.BuyCount)
  126. self:OnClickClose()
  127. end
  128. end
  129. end
  130. function UIBuyTimesView:OnClickClose()
  131. self.delayTimer = FrameTimer.New(function()
  132. ManagerContainer.LuaUIMgr:ClosePage(self.uiBase.PageId)
  133. end, 1)
  134. self.delayTimer:Start()
  135. end
  136. function UIBuyTimesView:OnHide()
  137. end
  138. function UIBuyTimesView:OnClose()
  139. end
  140. function UIBuyTimesView:OnShow(data)
  141. self.controller:SetData(data)
  142. end
  143. function UIBuyTimesView:OnDispose()
  144. if self.delayTimer then
  145. self.delayTimer:Stop()
  146. end
  147. self.delayTimer = nil
  148. end
  149. return UIBuyTimesView