car_lib_page_item.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { _decorator, Component, EventTouch, instantiate, Layout, Node, PageView, Prefab, UITransform, Vec2 } from 'cc';
  2. import { bag_list_item_data, car_item_data, car_type } from '../../data';
  3. import { userDataManager } from '../../manager/userDataManager';
  4. import { car_lib_list_item } from './car_lib_list_item';
  5. import { dataManager } from '../../manager/dataManager';
  6. const { ccclass, property } = _decorator;
  7. @ccclass('car_lib_page_item')
  8. export class car_lib_page_item extends Component {
  9. @property(Node) list:Node = null
  10. @property(Node) list_content:Node = null
  11. @property(Prefab) list_item:Prefab = null
  12. private m_pageView:Node = null
  13. private m_index = -1
  14. private m_data_list:car_item_data[] = []
  15. private m_click_cb = null
  16. private chuandi:boolean = true
  17. private startPosition:Vec2 = new Vec2(0,0)
  18. private movePosition:Vec2 = new Vec2(0,0)
  19. private endPosition:Vec2 = new Vec2(0,0)
  20. private pageIdx = 0
  21. protected start(): void {
  22. let list_content_size = this.list_content.getComponent(UITransform).contentSize
  23. let item_contenteSize = instantiate(this.list_item).getComponent(UITransform).contentSize
  24. let horizontal_padding = (list_content_size.width - item_contenteSize.width * 2) / 3
  25. if(horizontal_padding > 0) {
  26. this.list_content.getComponent(Layout).paddingLeft = horizontal_padding
  27. this.list_content.getComponent(Layout).paddingRight = horizontal_padding
  28. this.list_content.getComponent(Layout).spacingX = horizontal_padding
  29. }
  30. this.list.on(Node.EventType.TOUCH_START, this.touchStart, this)
  31. this.list.on(Node.EventType.TOUCH_MOVE, this.touchMove, this)
  32. this.list.on(Node.EventType.TOUCH_END, this.touchEnd, this)
  33. this.list.on(Node.EventType.TOUCH_CANCEL, this.touchEnd, this)
  34. }
  35. public touchStart(event:EventTouch) {
  36. this.chuandi = true
  37. this.startPosition = event.getLocation()
  38. this.pageIdx = this.m_pageView.getComponent(PageView).getCurrentPageIndex()
  39. }
  40. private touchMove(event:EventTouch) {
  41. // console.log('touchMove=',event)
  42. if(this.chuandi == false) {
  43. return
  44. }
  45. this.chuandi = true
  46. this.movePosition = event.getLocation();
  47. let distance_x = this.movePosition.x - this.startPosition.x;
  48. let distance_y = this.movePosition.y - this.startPosition.y;
  49. // console.log("距离差== ", distance_x, distance_y);
  50. //判断是否需要翻页
  51. if (Math.abs(distance_x) > 50 && distance_x > 0) {
  52. // console.log("向前翻页");
  53. this.m_pageView.getComponent(PageView).scrollToPage(this.pageIdx - 1);
  54. this.chuandi = false;
  55. } else if (Math.abs(distance_x) > 50 && distance_x < 0) {
  56. // console.log("向后翻页");
  57. this.m_pageView.getComponent(PageView).scrollToPage(this.pageIdx + 1);
  58. this.chuandi = false;
  59. }
  60. }
  61. private touchEnd(event:EventTouch) {
  62. // console.log('touchEnd=',event)
  63. this.endPosition = event.getLocation();
  64. let distance_x = this.endPosition.x - this.startPosition.x;
  65. let distance_y = this.endPosition.y - this.startPosition.y;
  66. //判断是否是点击
  67. if (Math.abs(distance_y) < 50 && Math.abs(distance_x) < 50) {
  68. // console.log("触摸结束,是点击");
  69. } else {
  70. // console.log("结束1");
  71. }
  72. }
  73. init(pageView, index, data_list:car_item_data[], bag_car_suipian_list, selected_cb, click_cb) {
  74. this.m_pageView = pageView
  75. this.m_index = index
  76. this.m_data_list = data_list
  77. this.m_click_cb = click_cb
  78. this.list_content.removeAllChildren()
  79. for (let index = 0; index < data_list.length; index++) {
  80. const element = data_list[index];
  81. // 是否解锁
  82. var is_jiesuo = false
  83. // 汽车碎片逻辑
  84. if(element.stype==car_type.suipian) {
  85. if(dataManager.userCarListHavCar(element.id)) {
  86. is_jiesuo = true
  87. element.temp_bag_list_item_data = new bag_list_item_data
  88. } else {
  89. let on_bag = false
  90. for (let i = 0; i < bag_car_suipian_list.length; i++) {
  91. const i_e:bag_list_item_data = bag_car_suipian_list[i];
  92. if(i_e.car_id==element.id) {
  93. element.temp_bag_list_item_data = i_e
  94. on_bag = true
  95. // if(i_e.quantity>=element.unlock_points) {is_jiesuo = true} //自动解锁
  96. break
  97. }
  98. }
  99. if(on_bag==false) { //不在背包中,补充一个空的
  100. element.temp_bag_list_item_data = new bag_list_item_data
  101. }
  102. }
  103. }
  104. // 业务逻辑
  105. let item = instantiate(this.list_item)
  106. item.parent = this.list_content
  107. let item_component = item.getComponent(car_lib_list_item)
  108. item_component.initView(element, index, this.onClickListItem.bind(this))
  109. if(element.stype==car_type.score) {
  110. is_jiesuo = userDataManager.user_car_list.car_list.some(obj => obj === element.id)
  111. }
  112. if(is_jiesuo) {
  113. item_component.setJiesuoSelectedStatus()
  114. } else {
  115. item_component.setJiesuoUnselectedStatus()
  116. }
  117. if(userDataManager.user_car_list.default_car_id == element.id) {
  118. item_component.setSelectedStatus()
  119. if(selected_cb) { selected_cb(this, item_component) }
  120. } else {
  121. item_component.setUnselectedStatus()
  122. }
  123. }
  124. }
  125. public getDataList():car_item_data[]{
  126. return this.m_data_list
  127. }
  128. public getIndex():number {
  129. return this.m_index
  130. }
  131. private onClickListItem(item:car_lib_list_item) {
  132. if(this.m_click_cb) {
  133. this.m_click_cb(this,item)
  134. }
  135. }
  136. }