找物源码导读Cocos Creator 3.8 · assets/scripts
0%
已读 0 / 424
目录— 个文件
目录加载中…
…/module/component/CustomToggleUnSelect.ts
1
2import { CCBoolean, Component, Enum, EventHandler, EventTouch, Node, NodeEventType, Toggle, _decorator } from "cc";
3import { Framework } from "../../Framework";
4import { EventFrameCode } from "../../core/event/EventFrameCode";
5const { ccclass, property, requireComponent, disallowMultiple } = _decorator;
6
7export const CUSTOM_TOGGLE_CHANGED_EVENT = "custom-toggle-changed";
8
9declare global {
10 interface EventConfig {
11 'CustomToggle': { bool: boolean, origin: Node, type: toggleType, touchEvent: EventTouch };
12 }
13}
14
15export enum toggleType {
16 rank,
17 setting,
18 archive,
19 signature
20}
21
22/**
23 * 自定义未选中状态插件
24 */
25@ccclass
26@disallowMultiple(true)
27// @requireComponent(Toggle)
28export class CustomToggleUnselect extends Component {
29
30 @property(Node)
31 unselectMark!: Node;
32 @property(Node)
33 selectMark!: Node;
34 // 布尔属性使用 Cocos 类型,保持编辑器序列化声明有效。
35 @property({ type: CCBoolean })
36 isContinue!: boolean;
37 @property({ type: CCBoolean, tooltip: '勾选此选项后则会与其他toggle一起变换' })
38 isLink!: boolean;
39 @property({ type: Enum(toggleType) })
40 type: toggleType = toggleType.rank;
41
42 @property({ type: [EventHandler] })
43 private events: EventHandler[] = [];
44
45 @property(CCBoolean)
46 private _isChecked!: boolean;
47 private touchBound = false;
48 public get isChecked(): boolean {
49 return this._isChecked;
50 }
51 @property(CCBoolean)
52 public set isChecked(value: boolean) {
53 this.unselectMark && (this.unselectMark.active = !value);
54 this.selectMark && (this.selectMark.active = value);
55 this._isChecked = value;
56 }
57 onLoad() {
58 this.bindToggleEvents();
59 }
60
61 protected onEnable(): void {
62 this.bindToggleEvents();
63 }
64
65 protected onDisable(): void {
66 Framework.Event.UnRegisterEvent(EventFrameCode.CustomToggle, this._clickToggle, this);
67 this.unbindTouchEvent();
68
69 }
70
71 public onToggleClick(e: EventTouch) {
72 if (this.isChecked && !this.isContinue) return;
73 this.isChecked = !this._isChecked;
74 EventHandler.emitEvents(this.events, { ...e, isChecked: this.isChecked });
75 Framework.Event.SendEvent(EventFrameCode.CustomToggle, { 'bool': this.isChecked, origin: this.node, 'type': this.type, touchEvent: e });
76 this.node.emit(CUSTOM_TOGGLE_CHANGED_EVENT, this.isChecked, e);
77 if (e) e.bubbles = false;
78 }
79
80 private _clickToggle(opt: { bool: boolean, origin: any, type?: toggleType }) {
81 if (this.node !== opt.origin && this.isLink && (opt.type ? opt.type == this.type : true))
82 this.isChecked = !opt.bool;
83 }
84
85 private bindToggleEvents(): void {
86 if (!Framework.Event.HasRegisterEvent(EventFrameCode.CustomToggle, this._clickToggle, this))
87 Framework.Event.RegisterEvent(EventFrameCode.CustomToggle, this._clickToggle, this);
88 if (!this.touchBound) {
89 this.node.on(NodeEventType.TOUCH_END, this.onToggleClick, this, true);
90 this.touchBound = true;
91 }
92 }
93
94 private unbindTouchEvent(): void {
95 if (!this.touchBound) return;
96 this.node.off(NodeEventType.TOUCH_END, this.onToggleClick, this, true);
97 this.touchBound = false;
98 }
99
100 protected onDestroy(): void {
101 Framework.Event.UnRegisterEvent(EventFrameCode.CustomToggle, this._clickToggle, this);
102 this.unbindTouchEvent();
103 }
104
105}
讲解106 行
互斥选项人工通读

CustomToggleUnSelect.ts

组件自行维护选中标记,不要求挂载 Toggle。触摸结束翻转状态并通知编辑器事件、框架事件及节点事件,其他启用联动的实例再响应框架广播。绑定在加载与启用时去重,禁用和销毁解绑;类型零值的条件判断会让排行榜组跨类型联动。

先看 isChecked 更新两套标记,再看 onToggleClick 广播和 _clickToggle 过滤。

特殊处理与易错点

  • 易错点opt.type 为零时过滤条件视为未指定类型,会影响不同类型的联动项。
  • 特殊处理已选中且 isContinue 为假时不响应点击;联动只是写状态,不再次广播。
25注释自定义未选中状态插件
28注释CustomToggleUnselect:自定义未选中状态插件
35注释布尔属性使用 Cocos 类型,保持编辑器序列化声明有效。
40属性类型(toggleType):该模块的运行态字段。
43属性事件(EventHandler[]):该模块的运行态字段。
48方法读取实例当前选中状态,写入时同步两套标记节点。
57方法加载时绑定框架互斥广播与捕获阶段触摸结束。
61方法节点重新启用时恢复已被禁用回调移除的监听。
65方法禁用时注销框架选项事件并移除节点触摸监听。
71方法翻转允许操作的选项并广播新状态和原始触摸。
80方法对其他节点广播按联动开关和类型写入相反状态。
85方法去重绑定全局选项事件及节点捕获触摸结束事件。
94方法仅在已绑定时移除同一回调和捕获标志的监听。
100方法销毁时再次解除全局广播订阅及节点触摸监听。