PageView.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { _decorator, Component, Toggle, ToggleContainer } from 'cc';
  2. import { Tabbar } from '../ui/Tabbar';
  3. import { Window } from '../ui/window/Window';
  4. import { Page } from './Page';
  5. const { ccclass, property } = _decorator;
  6. /**
  7. * 多页面控制组件
  8. * @description 一个简单的多页面控制组件,需要给多选框设置onTabBar给本组件
  9. * @author 袁浩
  10. */
  11. @ccclass('PageView')
  12. export class PageView extends Component {
  13. @property({ tooltip: "页面列表", type: Page })
  14. public pages: Page[] = [];
  15. @property({ tooltip: "窗口参数是否可以改变页面索引" })
  16. public useWindowParam = true;
  17. @property({ tooltip: "页签", type: [Tabbar] })
  18. public tabbars: Tabbar[] = [];
  19. @property({ tooltip: "页签", type: [ToggleContainer] })
  20. public toggleContainer: ToggleContainer;
  21. private _selectedIndex = 0;
  22. public set selectedIndex(value: number) {
  23. if (this.selectedIndex != value) {
  24. this._selectedIndex = value;
  25. this.setSelectedIndex()
  26. }
  27. }
  28. public get selectedIndex() {
  29. return this._selectedIndex;
  30. }
  31. start() {
  32. let _window = this.getComponent(Window);
  33. if (_window && _window.params) {
  34. this._selectedIndex = parseInt(_window.params[0]) || 0;
  35. }
  36. this.setSelectedIndex();
  37. }
  38. onTabBar(toggle: Toggle) {
  39. let items = toggle._toggleContainer.toggleItems;
  40. let index = items.indexOf(toggle);
  41. if (index != this.selectedIndex) {
  42. this.selectedIndex = index;
  43. }
  44. }
  45. protected setSelectedIndex() {
  46. for (let i = 0; i < this.pages.length; i++) {
  47. const page = this.pages[i];
  48. if (this.selectedIndex == i) {
  49. page.show(this);
  50. this.toggleContainer && (this.toggleContainer.toggleItems[i].isChecked = true);
  51. this.tabbars[i] && (this.tabbars[i].isChecked = true);
  52. }
  53. else {
  54. page.hide(this);
  55. }
  56. }
  57. }
  58. }