car_lib_bottom.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { _decorator, Component, instantiate, Label, Layout, Node, Prefab, Sprite, SpriteFrame, UITransform } from 'cc';
  2. import { base_ui } from '../../fw/base_ui';
  3. import { tools } from '../../tools';
  4. import { car_lib_list_item } from './car_lib_list_item';
  5. import { userDataManager } from '../../manager/userDataManager';
  6. import { car_item_data } from '../../data';
  7. const { ccclass, property } = _decorator;
  8. @ccclass('car_lib_bottom')
  9. export class car_lib_bottom extends base_ui {
  10. @property(Node) btn_num_left:Node = null
  11. @property(Node) btn_num_right:Node = null
  12. @property(Node) lab_num:Node = null
  13. @property(Node) list:Node = null
  14. @property(Node) list_content:Node = null
  15. @property(Prefab) list_item:Prefab = null
  16. private data_list:car_item_data[] = []
  17. private num_current_count:number = 1
  18. private num_total_count:number = 1
  19. private current_select_list_item:car_lib_list_item = null
  20. private m_click_item_cb = null
  21. start() {
  22. this.onButtonListen(this.btn_num_left, ()=>{
  23. if(this.num_current_count==1) {
  24. return
  25. }
  26. this.num_current_count -=1
  27. this.updateNumStatus(true)
  28. })
  29. this.onButtonListen(this.btn_num_right, ()=>{
  30. if(this.num_current_count>=this.num_total_count) {
  31. return
  32. }
  33. this.num_current_count +=1
  34. this.updateNumStatus(true)
  35. })
  36. }
  37. public init(click_item_cb) {
  38. this.m_click_item_cb = click_item_cb
  39. this.num_total_count = tools.all_car_page_list.length
  40. // this.data_list = JSON.parse(JSON.stringify(tools.all_car_list))
  41. this.data_list = tools.all_car_page_list[this.getNumCurrentIndex()]
  42. console.log('this.data_list=',this.data_list)
  43. this.updateNumStatus()
  44. this.reloadListContentData()
  45. let list_content_size = this.list_content.getComponent(UITransform).contentSize
  46. let item_contenteSize = instantiate(this.list_item).getComponent(UITransform).contentSize
  47. let horizontal_padding = (list_content_size.width - item_contenteSize.width * 2) / 3
  48. if(horizontal_padding > 0) {
  49. this.list_content.getComponent(Layout).paddingLeft = horizontal_padding
  50. this.list_content.getComponent(Layout).paddingRight = horizontal_padding
  51. this.list_content.getComponent(Layout).spacingX = horizontal_padding
  52. }
  53. }
  54. private getNumCurrentIndex():number {
  55. let index = this.num_current_count - 1
  56. if(index<0) {
  57. index = 0
  58. }
  59. return index
  60. }
  61. updateNumStatus(is_reload_data:boolean=false) {
  62. this.lab_num.getComponent(Label).string = this.num_current_count + '/' + this.num_total_count
  63. if(is_reload_data) {
  64. this.current_select_list_item = null
  65. this.data_list = tools.all_car_page_list[this.getNumCurrentIndex()]
  66. this.reloadListContentData()
  67. }
  68. }
  69. reloadListContentData() {
  70. this.list_content.removeAllChildren()
  71. this.current_select_list_item = null
  72. for (let index = 0; index < this.data_list.length; index++) {
  73. const element = this.data_list[index];
  74. let is_jiesuo = userDataManager.user_car_list.car_list.some(obj => obj === element.id)
  75. let item = instantiate(this.list_item)
  76. if(item!=null) {
  77. item.parent = this.list_content
  78. let item_component = item.getComponent(car_lib_list_item)
  79. item_component.initView(element, index, this.onClickListItem.bind(this))
  80. if(is_jiesuo) {
  81. item_component.setJiesuoSelectedStatus()
  82. } else {
  83. item_component.setJiesuoUnselectedStatus()
  84. }
  85. if(userDataManager.user_car_list.default_car_id == element.id) {
  86. this.current_select_list_item = item_component
  87. item_component.setSelectedStatus()
  88. } else {
  89. item_component.setUnselectedStatus()
  90. }
  91. }
  92. }
  93. }
  94. onClickListItem(item:car_lib_list_item) {
  95. if(this.current_select_list_item!=null) {
  96. if(this.current_select_list_item.getData().id == item.getData().id) {
  97. return
  98. }
  99. }
  100. if(item.getIsJiesuo()==false) {
  101. console.log('未解锁 未解锁 未解锁')
  102. return
  103. }
  104. if(this.current_select_list_item!=null) {
  105. this.current_select_list_item.setUnselectedStatus()
  106. }
  107. item.setSelectedStatus()
  108. this.current_select_list_item = item
  109. if(this.m_click_item_cb!=null) {
  110. this.m_click_item_cb(item)
  111. }
  112. }
  113. }