scene_select_list.ts 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { _decorator, Component, instantiate, Node, Prefab } from 'cc';
  2. import { main } from '../main';
  3. import { scene_item_data } from '../../data/data';
  4. import { scene_select_list_item } from './scene_select_list_item';
  5. import { ClientEvent } from '../clientEvent';
  6. import { config } from '../config';
  7. import { tools } from '../tools';
  8. const { ccclass, property } = _decorator;
  9. @ccclass('scene_select_list')
  10. export class scene_select_list extends Component {
  11. @property(Prefab) scene_select_list_item_prefab:Prefab = null;
  12. @property(Node) content:Node = null;
  13. private cur_select_index = 0;
  14. private m_main:main = null;
  15. public initView(_main:main){
  16. this.m_main = _main;
  17. this.content.removeAllChildren()
  18. this.onUpdateSceneList()
  19. ClientEvent.off(config.Event.UpdateSceneList,this.onUpdateSceneList,this)
  20. ClientEvent.on(config.Event.UpdateSceneList,this.onUpdateSceneList,this)
  21. }
  22. onUpdateSceneList(is_move:boolean = false){
  23. let list = this.m_main.control_view.get_bag_data().content;
  24. if(is_move==false) {
  25. this.cur_select_index = list.length>0?list.length-1:0;
  26. }
  27. this.content.removeAllChildren()
  28. for (let index = 0; index < list.length; index++) {
  29. const element:scene_item_data = list[index];
  30. let item = instantiate(this.scene_select_list_item_prefab)
  31. item.parent = this.content;
  32. let scene_select_list_item_component = item.getComponent(scene_select_list_item)
  33. scene_select_list_item_component.initView(list.length,element,this.onItemSelect.bind(this),index)
  34. scene_select_list_item_component.moveCallback(this.onItemMoveClick.bind(this))
  35. }
  36. this.updaetSelectStatus()
  37. }
  38. onItemSelect(item:scene_select_list_item){
  39. if(item.getSelect()){
  40. }else{
  41. this.cur_select_index = item.getIndex()
  42. this.updaetSelectStatus()
  43. }
  44. }
  45. onItemMoveClick(item:scene_select_list_item, is_up:boolean) {
  46. let list = this.m_main.control_view.get_bag_data().content;
  47. let c_index = item.getIndex()
  48. let c_data = list[c_index]
  49. let move_index=-1;
  50. if(is_up) {
  51. move_index = c_index - 1
  52. if(item.getSelect()) {
  53. this.cur_select_index -=1
  54. }
  55. } else {
  56. move_index = c_index + 1
  57. if(item.getSelect()) {
  58. this.cur_select_index +=1
  59. }
  60. }
  61. if(move_index == -1) {return}
  62. let move_data = list[move_index]
  63. list[c_index] = move_data
  64. list[move_index] = c_data
  65. this.onUpdateSceneList(true)
  66. }
  67. updaetSelectStatus(){
  68. for (let index = 0; index < this.content.children.length; index++) {
  69. const element = this.content.children[index];
  70. element.getComponent(scene_select_list_item).setIsSelect(element.getComponent(scene_select_list_item).getIndex()==this.cur_select_index)
  71. element.getComponent(scene_select_list_item).updatSelectStatus()
  72. }
  73. ClientEvent.dispatchEvent(config.Event.UpdateEditScene)
  74. }
  75. public getCurSelectScene(){
  76. if(this.content.children.length<=0){
  77. return null;
  78. }
  79. return this.content.children[this.cur_select_index].getComponent(scene_select_list_item).getData()
  80. }
  81. public getCurSelectSceneIndex(){
  82. return this.cur_select_index;
  83. }
  84. }