源战役客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 regels
3.7 KiB

4 weken geleden
  1. PopupComponent = PopupComponent or BaseClass(BaseComponent)
  2. function PopupComponent:__init(parent,layout_file,default_text,popup_list,call_back)
  3. self.show_list = false
  4. self:InitData(layout_file,default_text,popup_list,call_back)
  5. self:CreateGameObject(UIType.PopupComponent)
  6. end
  7. function PopupComponent:InitData(layout_file,default_text,popup_list,call_back)
  8. self.popup_item_list = {}
  9. self.layout_file = layout_file
  10. self.default_text = default_text or ""
  11. self.popup_list = popup_list or {}
  12. self.call_back = call_back
  13. end
  14. function PopupComponent:LoadSuccess()
  15. self.item_parent = self:GetChild("Mask/Parent/ItemParent")
  16. self.popup_parent = self:GetChild("Mask/Parent")
  17. self.popup_bg = self:GetChild("Mask/Parent/Bg")
  18. self.hideListBtn = self:GetChild("HideListBtn").gameObject
  19. self.arrow = self:GetChild("PopupBtn/Arrow")
  20. self.popup_btn = self:GetChild("PopupBtn").gameObject
  21. self.popup_btn_txt = self:GetChild("PopupBtn/Value"):GetComponent("Text")
  22. self:InitEvent()
  23. self:LayoutUI()
  24. end
  25. --在父窗口调用
  26. function PopupComponent:__delete()
  27. if self.select_item_id then
  28. GlobalEventSystem:UnBind(self.select_item_id)
  29. self.select_item_id = nil
  30. end
  31. for i,item in pairs(self.popup_item_list) do
  32. item:DeleteMe()
  33. end
  34. self.popup_item_list = {}
  35. end
  36. function PopupComponent:LayoutUI()
  37. self.popup_btn_txt.text = self.default_text
  38. self:SetPopupItemList()
  39. end
  40. function PopupComponent:PopupBtnClick()
  41. self.show_list = not self.show_list
  42. if self.show_list then
  43. self:ShowPopupList()
  44. else
  45. self:HidePopupList()
  46. end
  47. end
  48. function PopupComponent:InitEvent()
  49. local function onBtnClickHandler(target)
  50. if target == self.popup_btn then
  51. self:PopupBtnClick()
  52. elseif target == self.hideListBtn then
  53. self:HidePopupList()
  54. end
  55. end
  56. AddClickEvent(self.popup_btn, onBtnClickHandler,LuaSoundManager.SOUND_UI.SWITCH)
  57. AddClickEvent(self.hideListBtn, onBtnClickHandler,LuaSoundManager.SOUND_UI.SWITCH)
  58. local function onSelectItem(layout_file,data)
  59. if self.layout_file == layout_file then
  60. self:SetPopupBtnText(data.content)
  61. self:HidePopupList()
  62. end
  63. end
  64. self.select_item_id = GlobalEventSystem:Bind(EventName.SELECT_POPUP_LIST_ITEM,onSelectItem)
  65. end
  66. function PopupComponent:SetPopupBtnText(str)
  67. self.popup_btn_txt.text = str
  68. end
  69. function PopupComponent:ShowPopupList()
  70. if self.is_moving then
  71. return
  72. end
  73. self.show_list = true
  74. self.hideListBtn:SetActive(true)
  75. self.arrow.localRotation = Quaternion.Euler(0,0,90)
  76. local sizeDelta = self.popup_bg.sizeDelta
  77. self.popup_parent.localPosition = Vector3(0,sizeDelta.y,0)
  78. self.popup_parent.gameObject:SetActive(true)
  79. local function onCompleted()
  80. self.is_moving = false
  81. end
  82. TweenLite.to(self, self.popup_parent,TweenLite.UiAnimationType.POSY,0,0.1,onCompleted)
  83. end
  84. function PopupComponent:HidePopupList()
  85. if self.is_moving then
  86. return
  87. end
  88. self.show_list = false
  89. self.hideListBtn:SetActive(false)
  90. self.arrow.localRotation = Quaternion.Euler(0,0,270)
  91. local function onCompleted()
  92. self.is_moving = false
  93. self.popup_parent.gameObject:SetActive(false)
  94. end
  95. local sizeDelta = self.popup_bg.sizeDelta
  96. TweenLite.to(self, self.popup_parent,TweenLite.UiAnimationType.POSY,sizeDelta.y,0.1,onCompleted)
  97. end
  98. function PopupComponent:SetPopupItemList()
  99. local popup_list = self.popup_list
  100. local len = #popup_list
  101. for i,v in ipairs(popup_list) do
  102. item = self.popup_item_list[i]
  103. if item == nil then
  104. item = PopupSelectItem.New(self.item_parent)
  105. table.insert(self.popup_item_list,item)
  106. end
  107. item:SetVisible(true)
  108. item:SetData(i,self.layout_file,v,self.call_back)
  109. end
  110. self.popup_bg.sizeDelta = Vector2(152,len*50 + 8)
  111. for i=#popup_list + 1,#self.popup_item_list do
  112. self.popup_item_list[i]:SetVisible(false)
  113. end
  114. end