UIFuncMenuView.lua 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. local UIFuncMenuView = require("UIGuild/UIFuncMenuView_Generate")
  2. local AlignmentMinMax =
  3. {
  4. [Enum.Alignment.UpperLeft] = {Vector2(0, -1), Vector2(1, 0), Vector2(0, 0)},
  5. [Enum.Alignment.UpperCenter] = {Vector2(-0.5, -1), Vector2(0.5, 0), Vector2(-0.5, 0)},
  6. [Enum.Alignment.UpperRight] = {Vector2(-1, -1), Vector2(0, 0), Vector2(-1, 0)},
  7. [Enum.Alignment.MiddleLeft] = {Vector2(0, -0.5), Vector2(1, 0.5), Vector2(0, 0.5)},
  8. [Enum.Alignment.MiddleCenter] = {Vector2(-0.5, -0.5), Vector2(0.5, 0.5), Vector2(-0.5, 0.5)},
  9. [Enum.Alignment.MiddleRight] = {Vector2(-1, -0.5), Vector2(0, 0.5), Vector2(-1, 0.5)},
  10. [Enum.Alignment.LowerLeft] = {Vector2(0, 0), Vector2(1, 1), Vector2(0, 1)},
  11. [Enum.Alignment.LowerCenter] = {Vector2(-0.5, 0), Vector2(0.5, 1), Vector2(-0.5, 1)},
  12. [Enum.Alignment.LowerRight] = {Vector2(-1, 0), Vector2(0, 1), Vector2(-1, 1)},
  13. }
  14. local AlignmentPriority =
  15. {
  16. [Enum.Alignment.UpperLeft] = {
  17. Enum.Alignment.UpperCenter, Enum.Alignment.UpperRight,
  18. Enum.Alignment.MiddleLeft, Enum.Alignment.LowerLeft, Enum.Alignment.MiddleCenter,
  19. Enum.Alignment.LowerCenter, Enum.Alignment.MiddleRight, Enum.Alignment.LowerRight
  20. },
  21. [Enum.Alignment.UpperCenter] = {
  22. Enum.Alignment.UpperLeft, Enum.Alignment.UpperRight,
  23. Enum.Alignment.MiddleCenter, Enum.Alignment.LowerCenter, Enum.Alignment.MiddleLeft,
  24. Enum.Alignment.LowerLeft, Enum.Alignment.MiddleRight, Enum.Alignment.LowerRight
  25. },
  26. [Enum.Alignment.UpperRight] = {
  27. Enum.Alignment.UpperCenter, Enum.Alignment.UpperLeft,
  28. Enum.Alignment.MiddleRight, Enum.Alignment.LowerRight, Enum.Alignment.MiddleCenter,
  29. Enum.Alignment.LowerCenter, Enum.Alignment.MiddleLeft, Enum.Alignment.LowerLeft
  30. },
  31. [Enum.Alignment.MiddleLeft] = {
  32. Enum.Alignment.MiddleCenter, Enum.Alignment.MiddleRight,
  33. Enum.Alignment.UpperLeft, Enum.Alignment.LowerLeft, Enum.Alignment.UpperCenter,
  34. Enum.Alignment.LowerCenter, Enum.Alignment.UpperRight, Enum.Alignment.LowerRight
  35. },
  36. [Enum.Alignment.MiddleCenter] = {
  37. Enum.Alignment.MiddleLeft, Enum.Alignment.MiddleRight,
  38. Enum.Alignment.UpperCenter, Enum.Alignment.LowerCenter, Enum.Alignment.UpperLeft,
  39. Enum.Alignment.UpperRight, Enum.Alignment.LowerLeft, Enum.Alignment.LowerRight
  40. },
  41. [Enum.Alignment.MiddleRight] = {
  42. Enum.Alignment.MiddleCenter, Enum.Alignment.MiddleLeft,
  43. Enum.Alignment.UpperRight, Enum.Alignment.LowerRight, Enum.Alignment.UpperCenter,
  44. Enum.Alignment.LowerCenter, Enum.Alignment.UpperLeft, Enum.Alignment.LowerLeft
  45. },
  46. [Enum.Alignment.LowerLeft] = {
  47. Enum.Alignment.LowerCenter, Enum.Alignment.LowerRight,
  48. Enum.Alignment.MiddleLeft, Enum.Alignment.UpperLeft, Enum.Alignment.MiddleCenter,
  49. Enum.Alignment.MiddleRight, Enum.Alignment.UpperCenter, Enum.Alignment.UpperRight
  50. },
  51. [Enum.Alignment.LowerCenter] = {
  52. Enum.Alignment.LowerLeft, Enum.Alignment.LowerRight,
  53. Enum.Alignment.MiddleCenter, Enum.Alignment.UpperCenter, Enum.Alignment.MiddleLeft,
  54. Enum.Alignment.MiddleRight, Enum.Alignment.UpperLeft, Enum.Alignment.UpperRight
  55. },
  56. [Enum.Alignment.LowerRight] = {
  57. Enum.Alignment.LowerCenter, Enum.Alignment.LowerLeft,
  58. Enum.Alignment.MiddleRight, Enum.Alignment.UpperRight, Enum.Alignment.MiddleCenter,
  59. Enum.Alignment.MiddleLeft, Enum.Alignment.UpperCenter, Enum.Alignment.UpperLeft
  60. },
  61. }
  62. function UIFuncMenuView:OnAwake(data)
  63. self.controller = require("UIGuild/UIFuncMenuCtr")
  64. self.controller:Init(self)
  65. self.controller:SetData(data)
  66. end
  67. function UIFuncMenuView:AddEventListener()
  68. end
  69. function UIFuncMenuView:FillContent(data, uiBase)
  70. self.uiBase = uiBase
  71. local gameObject = self.uiBase:GetRoot()
  72. if gameObject ~= nil then
  73. self.gameObject = gameObject
  74. self.transform = gameObject.transform
  75. end
  76. self:InitGenerate(self.transform, data)
  77. self:Init()
  78. end
  79. function UIFuncMenuView:Init()
  80. -- 初始化按钮列表
  81. self.menuBtns = {self.btn}
  82. self:RefreshMenus()
  83. self:RefreshAlignPos()
  84. end
  85. function UIFuncMenuView:RemoveEventListener()
  86. ManagerContainer.LuaEventMgr:Unregister(self.uiData.name)
  87. end
  88. function UIFuncMenuView:AddUIEventListener()
  89. ManagerContainer.LuaEventMgr:RegisterUIEvent(self.uiData.name)
  90. self.uiBase:AddButtonUniqueEventListener(self.mask.button, self, self.OnClickEmptyArea)
  91. end
  92. function UIFuncMenuView:OnHide()
  93. end
  94. function UIFuncMenuView:OnShow(data)
  95. self.controller:SetData(data)
  96. end
  97. function UIFuncMenuView:OnClose()
  98. end
  99. function UIFuncMenuView:OnDispose()
  100. self.controller:OnDispose()
  101. for _, menuBtn in pairs(self.menuBtns) do
  102. if menuBtn ~= self.btn then
  103. menuBtn.button = nil
  104. menuBtn.text.text = nil
  105. if tolua.getpeer(menuBtn.text) ~= nil then
  106. tolua.setpeer(menuBtn.text, nil)
  107. end
  108. menuBtn.text = nil
  109. CommonUtil.DestroyGO(menuBtn)
  110. if tolua.getpeer(menuBtn) ~= nil then
  111. tolua.setpeer(menuBtn, nil)
  112. end
  113. end
  114. end
  115. self.menuBtns = nil
  116. end
  117. function UIFuncMenuView:RefreshMenus()
  118. local menus = self.controller:GetMenus()
  119. local hasNum = #self.menuBtns
  120. local menuNum = #menus
  121. local btn, isChangeNum = false
  122. for i = 1, menuNum do
  123. local menu = menus[i]
  124. if hasNum < i then
  125. local go = UnityEngine.GameObject.Instantiate(self.btn)
  126. local trans = go.transform
  127. trans:SetParent(self.btnContainer.rectTransform)
  128. trans.localPosition = Vector3.zero
  129. trans.localRotation = Quaternion.identity
  130. trans.localScale = self.btn.transform.localScale
  131. if tolua.getpeer(go) == nil then
  132. tolua.setpeer(go, {})
  133. end
  134. btn = go
  135. btn.button = go:GetComponent(Enum.TypeInfo.Button)
  136. local textGo = trans:Find("Text").gameObject
  137. if tolua.getpeer(textGo) == nil then
  138. tolua.setpeer(textGo, {})
  139. end
  140. btn.text = textGo
  141. btn.text.text = textGo:GetComponent(Enum.TypeInfo.TextMeshProUGUI)
  142. self.menuBtns[i] = btn
  143. isChangeNum = true
  144. else
  145. btn = self.menuBtns[i]
  146. end
  147. isChangeNum = isChangeNum or not btn.activeSelf
  148. btn:SetActive(true)
  149. btn.text.text.text = string.formatbykey(menu[1])
  150. if menu[3] then
  151. btn.button.interactable = false
  152. btn.button.onClick:RemoveAllListeners()
  153. else
  154. btn.button.interactable = true
  155. self.uiBase:AddButtonUniqueEventListener(btn.button, self, self.OnClickMenuBtn, i)
  156. end
  157. end
  158. for i = menuNum, hasNum do
  159. btn = self.menuBtns[i]
  160. isChangeNum = isChangeNum or btn.activeSelf
  161. if btn then btn:SetActive(false) end
  162. end
  163. -- 需要重新布局,计算Size
  164. if isChangeNum then
  165. local layoutGroup = self.btnContainer.verticalLayoutGroup
  166. layoutGroup:CalculateLayoutInputHorizontal()
  167. layoutGroup:SetLayoutHorizontal()
  168. layoutGroup:CalculateLayoutInputVertical()
  169. layoutGroup:SetLayoutVertical()
  170. local contentSizeFitter = self.btnContainer.contentSizeFitter
  171. contentSizeFitter:SetLayoutHorizontal()
  172. contentSizeFitter:SetLayoutVertical()
  173. end
  174. end
  175. function UIFuncMenuView:RefreshAlignPos()
  176. local parent = self.btnContainer.rectTransform.parent
  177. local parentRect = parent.rect
  178. local alignPos = self.controller:GetAlignPos()
  179. local alignment = self.controller:GetAlignment()
  180. local vaildMin = self.controller:GetVaildMin()
  181. local vaildMax = self.controller:GetVaildMax()
  182. if alignPos then
  183. alignPos = parent:InverseTransformPoint(alignPos)
  184. alignPos = Vector2(alignPos.x, alignPos.y)
  185. else
  186. alignPos = Vector2.zero
  187. end
  188. alignPos.z = 0
  189. if vaildMin then
  190. vaildMin = parent:InverseTransformPoint(vaildMin)
  191. vaildMin = Vector2(vaildMin.x, vaildMin.y)
  192. else
  193. vaildMin = parentRect.min
  194. end
  195. if vaildMax then
  196. vaildMax = parent:InverseTransformPoint(vaildMax)
  197. vaildMax = Vector2(vaildMax.x, vaildMax.y)
  198. else
  199. vaildMax = parentRect.max
  200. end
  201. local size = self.btnContainer.rectTransform.rect.size
  202. local min, max
  203. local offMin, offMax, offAnchor
  204. -- 默认对齐是否按钮
  205. if alignment then
  206. offMin, offMax, offAnchor = self:_GetPosByAlignment(alignment)
  207. min = Vector2.Scale(size, offMin) + alignPos
  208. max = Vector2.Scale(size, offMax) + alignPos
  209. self.btnContainer.rectTransform.localPosition = alignPos + Vector2.Scale(size, offAnchor)
  210. if self:_CheckSafe(min, max, vaildMin, vaildMax) then
  211. return
  212. end
  213. end
  214. local priority = AlignmentPriority[alignment]
  215. if priority then
  216. for i = 1, #priority do
  217. offMin, offMax, offAnchor = self:_GetPosByAlignment(priority[i])
  218. min = Vector2.Scale(size, offMin) + alignPos
  219. max = Vector2.Scale(size, offMax) + alignPos
  220. if self:_CheckSafe(min, max, vaildMin, vaildMax) then
  221. self.btnContainer.rectTransform.localPosition = alignPos + Vector2.Scale(size, offAnchor)
  222. return
  223. end
  224. end
  225. else
  226. for i = Enum.Alignment.UpperLeft, Enum.Alignment.LowerRight do
  227. offMin, offMax, offAnchor = self:_GetPosByAlignment(i)
  228. min = Vector2.Scale(size, offMin) + alignPos
  229. max = Vector2.Scale(size, offMax) + alignPos
  230. if self:_CheckSafe(min, max, vaildMin, vaildMax) then
  231. self.btnContainer.rectTransform.localPosition = alignPos + Vector2.Scale(size, offAnchor)
  232. return
  233. end
  234. end
  235. end
  236. end
  237. function UIFuncMenuView:OnClickEmptyArea()
  238. self.controller:ExecuteCancelCallBack(nil)
  239. self:UIClose()
  240. end
  241. function UIFuncMenuView:OnClickMenuBtn(_, params)
  242. local idx = params[0]
  243. self.controller:ExecuteCallBack(idx)
  244. self:UIClose()
  245. end
  246. --- btnContainer为左上锚点
  247. function UIFuncMenuView:_GetPosByAlignment(alignment)
  248. local rect = AlignmentMinMax[alignment]
  249. if not rect then
  250. rect = AlignmentMinMax[Enum.Alignment.MiddleCenter]
  251. end
  252. return rect[1], rect[2], rect[3]
  253. end
  254. function UIFuncMenuView:_CheckSafe(min, max, vaildMin, vaildMax)
  255. return vaildMin.x <= min.x and vaildMin.y <= min.y and vaildMax.x >= max.x and vaildMax.y >= max.y
  256. end
  257. return UIFuncMenuView