SelectMapPanel.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. item.getComponent(MapItem).setSelectMap(false)
  77. }
  78. }
  79. slideToBottom()
  80. {
  81. this.scrollView.scrollToBottom()
  82. }
  83. selectAllMap()
  84. {
  85. UserMap.addAllSelectMap()
  86. this._items.forEach((item)=>{
  87. item.getComponent(MapItem).setSelectMap(true)
  88. })
  89. }
  90. scrollToItem() {
  91. let page = parseInt(this.editBox.string);
  92. if (isNaN(page)) return;
  93. let itemNode = this.itemRoot.children[page];
  94. if (!itemNode) return;
  95. const viewHeight = this.scrollView.node.getComponent(UITransform).height;
  96. const itemHeight = itemNode.getComponent(UITransform).height;
  97. // 获取itemNode在content中的位置(局部坐标)
  98. const itemPos = itemNode.position;
  99. const itemTop = -itemPos.y;
  100. // 使item在视图中居中需要滚动的偏移量(从content顶部到视图顶部的距离)
  101. let offset = itemTop - (viewHeight - itemHeight) / 2;
  102. // 获取内容总高度(content节点的高度)
  103. const contentHeight = this.itemRoot.getComponent(UITransform).height;
  104. // 最大可滚动的偏移量
  105. const maxScrollOffset = Math.max(0, contentHeight - viewHeight);
  106. // 限制offset在[0, maxScrollOffset]之间
  107. offset = Math.max(0, Math.min(maxScrollOffset, offset));
  108. // 滚动到该偏移量
  109. this.scrollView.scrollToOffset(new Vec2(0, offset), 0.5);
  110. }
  111. reset(type:string)
  112. {
  113. this.clearItems()
  114. let mapList = getOneTypeImgKeyList(type)
  115. this.init(mapList)
  116. }
  117. clearItems()
  118. {
  119. this._items.forEach((item)=>{
  120. item.destroy()
  121. })
  122. this._items = []
  123. this._hasCreatedItems = false
  124. }
  125. onBtnAllClick()
  126. {
  127. this.selectTypeBtns.active = true
  128. this.appearMoreBtns()
  129. }
  130. typeBtns:Node[] = []
  131. appearMoreBtns()
  132. {
  133. if(this.typeBtns.length > 0)
  134. {
  135. this.selectTypeBtns.active = true
  136. return
  137. }
  138. let count = imgTypeList.length
  139. for(let i = 0; i < count; i++){
  140. let itemNode = instantiate(this.btnAll.node);
  141. itemNode.parent = this.btnsMoreContent;
  142. itemNode.setPosition(0,0,0)
  143. itemNode.getComponentInChildren(Label).string = imgTypeList[i]
  144. this.typeBtns.push(itemNode)
  145. }
  146. this.typeBtns.forEach((item)=>{
  147. item.on(Node.EventType.TOUCH_END,this.onBtnTypeClick,this)
  148. })
  149. }
  150. onBtnTypeClick(event: Event) {
  151. let item = event.target as unknown as Node
  152. let type = item.getComponentInChildren(Label).string
  153. this.reset(type)
  154. this.selectTypeBtns.active = false
  155. }
  156. }