car_lib_page_item.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. let on_bag = false
  87. for (let i = 0; i < bag_car_suipian_list.length; i++) {
  88. const i_e:bag_list_item_data = bag_car_suipian_list[i];
  89. if(i_e.car_id==element.id) {
  90. element.temp_bag_list_item_data = i_e
  91. // bag_car_suipian_list.splice(i,1)
  92. on_bag = true
  93. if(i_e.quantity>=element.unlock_points) {
  94. is_jiesuo = true
  95. }
  96. break
  97. }
  98. }
  99. if(on_bag==false) { //不在背包中,补充一个空的
  100. element.temp_bag_list_item_data = new bag_list_item_data
  101. }
  102. } else { //不在用户车列表中,补充一个空的
  103. element.temp_bag_list_item_data = new bag_list_item_data
  104. // if(element.id==16) { element.temp_bag_list_item_data.quantity = 300} //用于测试
  105. }
  106. }
  107. // 业务逻辑
  108. let item = instantiate(this.list_item)
  109. item.parent = this.list_content
  110. let item_component = item.getComponent(car_lib_list_item)
  111. item_component.initView(element, index, this.onClickListItem.bind(this))
  112. if(element.stype==car_type.score) {
  113. is_jiesuo = userDataManager.user_car_list.car_list.some(obj => obj === element.id)
  114. }
  115. if(is_jiesuo) {
  116. item_component.setJiesuoSelectedStatus()
  117. } else {
  118. item_component.setJiesuoUnselectedStatus()
  119. }
  120. if(userDataManager.user_car_list.default_car_id == element.id) {
  121. item_component.setSelectedStatus()
  122. if(selected_cb) { selected_cb(this, item_component) }
  123. } else {
  124. item_component.setUnselectedStatus()
  125. }
  126. }
  127. }
  128. public getDataList():car_item_data[]{
  129. return this.m_data_list
  130. }
  131. public getIndex():number {
  132. return this.m_index
  133. }
  134. private onClickListItem(item:car_lib_list_item) {
  135. if(this.m_click_cb) {
  136. this.m_click_cb(this,item)
  137. }
  138. }
  139. }