SelectMapPanel.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import { _decorator, Button, Component, EditBox, instantiate, Label, Node, ScrollView, UITransform, Vec2, Vec3 } from 'cc';
  2. import { LayerMgr } from '../../script/Manager/LayerMgr';
  3. import { allMapList, getAllMapKeys, getOneTypeImgKeyList, imgTypeList, MapData, MapItemData, SuperFind, UserMap } from '../../script/Manager/LocalDataMgr';
  4. import { MapItem } from './MapItem';
  5. const { ccclass, property } = _decorator;
  6. @ccclass('SelectMapPanel')
  7. export class SelectMapPanel extends Component {
  8. @property(Node)
  9. itemRoot: Node = null;
  10. @property(Node)
  11. itemPrefab: Node = null;
  12. @property(Button)
  13. btnClose: Button = null;
  14. @property(ScrollView)
  15. scrollView:ScrollView = null;
  16. @property(EditBox)
  17. editBox:EditBox = null;
  18. @property(Button)
  19. btnTurn:Button = null;
  20. @property(Button)
  21. btnAll:Button = null;
  22. @property(Node)
  23. btnsMoreContent:Node = null;
  24. @property(Node)
  25. selectTypeBtns:Node = null
  26. _mapData:MapData = null
  27. _allMapList:{ [key: number]: MapItemData };
  28. _items: Node[] = [];
  29. _hasCreatedItems:boolean = false;
  30. _selectMapType:string = "全部"
  31. protected start(): void {
  32. this.btnClose.node.on(Node.EventType.TOUCH_END,this.onBtnCloseClick,this)
  33. this.btnTurn.node.on(Node.EventType.TOUCH_END,this.scrollToItem,this)
  34. this.btnAll.node.on(Node.EventType.TOUCH_END,this.onBtnAllClick,this)
  35. }
  36. init(mapList:number[] = null) {
  37. if(this._hasCreatedItems) return;
  38. this._hasCreatedItems = true;
  39. this._allMapList = allMapList
  40. this._mapData = UserMap.getMapData()
  41. let allMapKeys = mapList? mapList : getAllMapKeys()
  42. let count = allMapKeys.length;
  43. for (let i = 0; i < count; i++) {
  44. this.createItem(allMapKeys[i]);
  45. }
  46. }
  47. onBtnCloseClick()
  48. {
  49. this.node.active = false
  50. }
  51. createItem(key: number) {
  52. let itemNode = instantiate(this.itemPrefab);
  53. itemNode.parent = this.itemRoot;
  54. this._items.push(itemNode);
  55. itemNode.active = true
  56. let mapItem = itemNode.getComponent(MapItem)
  57. mapItem.init(key,this.judgeSelect(key))
  58. }
  59. judgeSelect(key)
  60. {
  61. let selMaps = UserMap.getMapData().selectedMaps
  62. let index = selMaps.indexOf(key)
  63. // console.log("key",key,"存在",index !== -1)
  64. return index !== -1
  65. }
  66. clearSelectAllMaps()
  67. {
  68. UserMap.removeAllSelectMap()
  69. this.refreshMapSelectioin()
  70. }
  71. refreshMapSelectioin()
  72. {
  73. let allMapKeys = getAllMapKeys()
  74. let count = allMapKeys.length;
  75. for(let i = 0; i < count; i++){
  76. let item = this._items[i]
  77. if(!item || !item.isValid) continue
  78. item.getComponent(MapItem).setSelectMap(false)
  79. }
  80. }
  81. slideToBottom()
  82. {
  83. this.scrollView.scrollToBottom()
  84. }
  85. selectAllMap()
  86. {
  87. this.reset(this._selectMapType)
  88. this._items.forEach((item)=>{
  89. item.getComponent(MapItem).setSelectMap(true)
  90. })
  91. }
  92. scrollToItem() {
  93. let page = parseInt(this.editBox.string);
  94. if (isNaN(page)) return;
  95. let itemNode = this.itemRoot.children[page];
  96. if (!itemNode) return;
  97. const viewHeight = this.scrollView.node.getComponent(UITransform).height;
  98. const itemHeight = itemNode.getComponent(UITransform).height;
  99. // 获取itemNode在content中的位置(局部坐标)
  100. const itemPos = itemNode.position;
  101. const itemTop = -itemPos.y;
  102. // 使item在视图中居中需要滚动的偏移量(从content顶部到视图顶部的距离)
  103. let offset = itemTop - (viewHeight - itemHeight) / 2;
  104. // 获取内容总高度(content节点的高度)
  105. const contentHeight = this.itemRoot.getComponent(UITransform).height;
  106. // 最大可滚动的偏移量
  107. const maxScrollOffset = Math.max(0, contentHeight - viewHeight);
  108. // 限制offset在[0, maxScrollOffset]之间
  109. offset = Math.max(0, Math.min(maxScrollOffset, offset));
  110. // 滚动到该偏移量
  111. this.scrollView.scrollToOffset(new Vec2(0, offset), 0.5);
  112. }
  113. reset(type:string)
  114. {
  115. this.clearItems()
  116. let mapList = getOneTypeImgKeyList(type)
  117. this.init(mapList)
  118. }
  119. clearItems()
  120. {
  121. this._items.forEach((item)=>{
  122. item.destroy()
  123. })
  124. this._items = []
  125. this._hasCreatedItems = false
  126. }
  127. onBtnAllClick()
  128. {
  129. this.selectTypeBtns.active = true
  130. this.appearMoreBtns()
  131. }
  132. typeBtns:Node[] = []
  133. appearMoreBtns()
  134. {
  135. if(this.typeBtns.length > 0)
  136. {
  137. this.selectTypeBtns.active = true
  138. return
  139. }
  140. let count = imgTypeList.length
  141. for(let i = 0; i < count; i++){
  142. let itemNode = instantiate(this.btnAll.node);
  143. itemNode.parent = this.btnsMoreContent;
  144. itemNode.setPosition(0,0,0)
  145. itemNode.getComponentInChildren(Label).string = imgTypeList[i]
  146. this.typeBtns.push(itemNode)
  147. }
  148. this.typeBtns.forEach((item)=>{
  149. item.on(Node.EventType.TOUCH_END,this.onBtnTypeClick,this)
  150. })
  151. }
  152. onBtnTypeClick(event: Event) {
  153. let item = event.target as unknown as Node
  154. let type = item.getComponentInChildren(Label).string
  155. this.reset(type)
  156. this.selectTypeBtns.active = false
  157. this._selectMapType = type
  158. }
  159. }