SelectMapPanel.ts 5.3 KB

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