目录— 个文件
⌕
目录加载中…
…/core/ui/UIActionDefine.ts
1import { CCBoolean, CCFloat, _decorator, Enum, Node, UITransform, v3, Vec3 } from "cc";
2import { MoveFromType } from "../utility/UtilityAction/contents/simple/moveAction";
3import { ActionType } from "../utility/UtilityAction/UtilityAction";
4
5const { ccclass, property } = _decorator;
6enum EasingEnum {
7 "linear" = "linear",
8 "smooth" = "smooth",
9 "fade" = "fade",
10 "constant" = "constant",
11 "quadIn" = "quadIn",
12 "quadOut" = "quadOut",
13 "quadInOut" = "quadInOut",
14 "quadOutIn" = "quadOutIn",
15 "cubicIn" = "cubicIn",
16 "cubicOut" = "cubicOut",
17 "cubicInOut" = "cubicInOut",
18 "cubicOutIn" = "cubicOutIn",
19 "quartIn" = "quartIn",
20 "quartOut" = "quartOut",
21 "quartInOut" = "quartInOut",
22 "quartOutIn" = "quartOutIn",
23 "quintIn" = "quintIn",
24 "quintOut" = "quintOut",
25 "quintInOut" = "quintInOut",
26 "quintOutIn" = "quintOutIn",
27 "sineIn" = "sineIn",
28 "sineOut" = "sineOut",
29 "sineInOut" = "sineInOut",
30 "sineOutIn" = "sineOutIn",
31 "expoIn" = "expoIn",
32 "expoOut" = "expoOut",
33 "expoInOut" = "expoInOut",
34 "expoOutIn" = "expoOutIn",
35 "circIn" = "circIn",
36 "circOut" = "circOut",
37 "circInOut" = "circInOut",
38 "circOutIn" = "circOutIn",
39 "elasticIn" = "elasticIn",
40 "elasticOut" = "elasticOut",
41 "elasticInOut" = "elasticInOut",
42 "elasticOutIn" = "elasticOutIn",
43 "backIn" = "backIn",
44 "backOut" = "backOut",
45 "backInOut" = "backInOut",
46 "backOutIn" = "backOutIn",
47 "bounceIn" = "bounceIn",
48 "bounceOut" = "bounceOut",
49 "bounceInOut" = "bounceInOut",
50 "bounceOutIn" = "bounceOutIn",
51}
52Enum(EasingEnum)
53
54@ccclass
55export abstract class UIActionDefine {
56 @property({ type: CCFloat, slide: true, min: 0.1, max: 1, step: 0.05, tooltip: "动画时长" })
57 public duration: number = 0.5; //动画时长
58 @property({ type: EasingEnum, tooltip: "动画曲线" })
59 public easing: EasingEnum = EasingEnum.backInOut;
60
61 public abstract get OpenValue();
62 public abstract set OpenValue(value: number | Vec3 | undefined);
63 public abstract get CloseValue();
64 public abstract set CloseValue(value: number | Vec3 | undefined);
65}
66
67/**
68 * UI界面淡入淡出动画
69 * @class
70 * @extends {Component}
71 * @description 界面淡入淡出动画
72 */
73@ccclass("UIBaseFadeAction")
74export class UIBaseFadeAction extends UIActionDefine {
75 @property({ type: CCFloat, slide: true, min: 0, max: 255, step: 1, tooltip: "透明度" })
76 public override OpenValue: number = 255;
77 @property({ type: CCFloat, slide: true, min: 0, max: 255, step: 1, tooltip: "透明度" })
78 public override CloseValue: number = 0;
79}
80
81/**
82 * UI界面缩放动画
83 * @class
84 * @extends {Component}
85 * @description 界面缩放动画
86 */
87@ccclass("UIBaseScaleAction")
88export class UIBaseScaleAction extends UIActionDefine {
89
90 @property
91 private _openValue: number = 1;
92 @property
93 private _closeValue: number = 0;
94 public get openValue$(): number { return this._openValue; }
95 public get closeValue$(): number { return this._closeValue; }
96
97 /**
98 * 打开缩放比例 -本地缓存
99 */
100 private _openValue$: Vec3 = new Vec3(0, 0, 1);
101 /**
102 * 关闭缩放比例 -本地缓存
103 */
104 private _closeValue$: Vec3 = new Vec3(0, 0, 1);
105
106 @property({ type: CCFloat, slide: true, min: 0, max: 2, step: 0.05, tooltip: "缩放比例" })
107 public set openValue$(value: number) {
108 this._openValue = value;
109 }
110 @property({ type: CCFloat, slide: true, min: 0, max: 2, step: 0.05, tooltip: "缩放比例" })
111 public set closeValue$(value: number) {
112 this._closeValue = value;
113 }
114
115 public get OpenValue(): Vec3 { return this._openValue$.set(this._openValue, this._openValue, 1); }
116 public set OpenValue(value: Vec3) { this._openValue$ = value; }
117 public get CloseValue(): Vec3 { return this._closeValue$.set(this._closeValue, this._closeValue, 1); }
118 public set CloseValue(value: Vec3) { this._closeValue$ = value; }
119}
120
121/**
122 * UI界面移动动画
123 * @class
124 * @extends {Component}
125 * @description 界面移动动画
126 */
127@ccclass("UIBaseMoveAction")
128export class UIBaseMoveAction extends UIActionDefine {
129 @property({ type: MoveFromType, tooltip: "移动方向(从哪儿来或者从哪儿退场)" })
130 public formType: MoveFromType = MoveFromType.down;
131
132 private _startValue: Vec3 | undefined;
133 private _endValue: Vec3 | undefined;
134
135 public get OpenValue(): Vec3 | undefined { return this._startValue; }
136 public set OpenValue(value: Vec3 | undefined) { this._startValue = value; }
137 public get CloseValue(): Vec3 | undefined { return this._endValue; }
138 public set CloseValue(value: Vec3 | undefined) { this._endValue = value; }
139
140 /**
141 * 解析 move 动画的显示/隐藏坐标
142 */
143 public getMovePositions(node: Node): { show: Vec3, hide: Vec3 } | null {
144 if (!this._startValue) {
145 this._startValue = node.position.clone();
146 }
147
148 const showPosition = this._startValue.clone();
149 const hidePosition = this._endValue?.clone() || this.getEdgeHidePosition(node, showPosition);
150 if (!hidePosition) {
151 return null;
152 }
153
154 if (!this._endValue) {
155 this._endValue = hidePosition.clone();
156 }
157
158 return {
159 show: showPosition,
160 hide: hidePosition.clone(),
161 };
162 }
163
164 /**
165 * 根据方向计算屏幕外隐藏坐标
166 */
167 private getEdgeHidePosition(node: Node, showPosition: Vec3): Vec3 | null {
168 const transform = node.getComponent(UITransform);
169 if (!transform) {
170 console.error("未找到 UITransform 组件,不能设置移动动画");
171 return null;
172 }
173
174 const parentTransform = node.parent?.getComponent(UITransform);
175 if (!parentTransform) {
176 console.error("未找到父节点 UITransform 组件,不能自动计算移动边界");
177 return null;
178 }
179
180 const hidePosition = showPosition.clone();
181 switch (this.formType) {
182 case MoveFromType.left:
183 hidePosition.x = -(parentTransform.width * parentTransform.anchorX) - transform.width * (1 - transform.anchorX);
184 break;
185 case MoveFromType.right:
186 hidePosition.x = parentTransform.width * (1 - parentTransform.anchorX) + transform.width * transform.anchorX;
187 break;
188 case MoveFromType.up:
189 hidePosition.y = parentTransform.height * (1 - parentTransform.anchorY) + transform.height * transform.anchorY;
190 break;
191 case MoveFromType.down:
192 hidePosition.y = -(parentTransform.height * parentTransform.anchorY) - transform.height * (1 - transform.anchorY);
193 break;
194 default:
195 return v3(showPosition);
196 }
197 return hidePosition;
198 }
199}
200
201@ccclass("UIActionClass")
202export class UIActionClass {
203 public get fadeAction(): UIBaseFadeAction { return this._fadeAction; } // 动画定义
204 public get scaleAction(): UIBaseScaleAction { return this._scaleAction; } // 动画定义
205 public get moveAction(): UIBaseMoveAction { return this._moveAction; } // 动画定义
206 public get actionType(): ActionType { return this._actionType; }
207 public get hasHideAction(): boolean { return this._hasHideAction; }
208
209
210
211 @property
212 private _actionType: ActionType = ActionType.fade;
213 @property
214 private _fadeAction: UIBaseFadeAction = new UIBaseFadeAction();
215 @property
216 private _scaleAction: UIBaseScaleAction = new UIBaseScaleAction();
217 @property
218 private _moveAction: UIBaseMoveAction = new UIBaseMoveAction();
219 @property
220 private _hasHideAction: boolean = false;
221
222
223 @property({ type: ActionType })
224 private set actionType(value: ActionType) {
225 this._actionType = value;
226 }
227 @property({ type: UIBaseFadeAction, visible: function () { const self = <UIActionClass>this; return self.actionType === ActionType.fade } })
228 public set fadeAction(value: UIBaseFadeAction) {
229 this._fadeAction = value;
230 }
231 @property({ type: UIBaseScaleAction, visible: function () { const self = <UIActionClass>this; return self.actionType === ActionType.scale } })
232 public set scaleAction(value: UIBaseScaleAction) {
233 this._scaleAction = value;
234 }
235 @property({ type: UIBaseMoveAction, visible: function () { const self = <UIActionClass>this; return self.actionType === ActionType.move } })
236 public set moveAction(value: UIBaseMoveAction) {
237 this._moveAction = value;
238 }
239 // 使用 Cocos 布尔类型注册隐藏动画开关。
240 @property({ type: CCBoolean })
241 public set hasHideAction(value: boolean) {
242 this._hasHideAction = value;
243 }
244 /**
245 * 获取动画类型
246 * @returns 动画类型
247 */
248 public getAniType(): UIBaseFadeAction | UIBaseScaleAction | UIBaseMoveAction {
249 switch (this._actionType) {
250 case ActionType.fade:
251 return this._fadeAction;
252 case ActionType.scale:
253 return this._scaleAction;
254 case ActionType.move:
255 return this._moveAction;
256 default:
257 return this._fadeAction;
258 }
259 }
260
261 /**
262 * 动画类释放
263 */
264 public Dispose() {
265 this._fadeAction = null!;
266 this._scaleAction = null!;
267 this._moveAction = null!;
268 }
269
270
271}
讲解272 行
面板动画定义人工通读
UIActionDefine.ts
定义面板开闭动画的数据与策略:抽象基类约定开闭值、时长与曲线,淡入淡出、缩放、移动三种实现,UIActionClass 按类型选出当前动画。UIBasePanel 读取这些对象计算起止值并驱动 UtilityAction 播放。
先看 UIActionDefine 的三个公共字段,再看三个实现类的 OpenValue 与 CloseValue,最后看 UIActionClass.getAniType。
特殊处理与易错点
- 特殊处理getAniType 按 actionType 返回对应动画对象,未识别类型统一回退到淡入淡出。L248
- 易错点缩放动画的 OpenValue 取值复用同一个内部 Vec3 并每次 set,调用方修改返回值会污染下次计算。L115
- 特殊处理移动动画首次调用时把节点当前坐标记为显示位置,隐藏位置由配置值或方向边界计算得出。L143
- 易错点Dispose 把三种动画对象置为 null,之后 getAniType 会返回空引用,面板销毁后不能再读取动画配置。L264
- 异常兜底计算屏幕外坐标时若节点或父节点缺少 UITransform,只打印错误并返回空,面板移动动画不会执行。L167
55注释UIActionDefine:UI界面淡入淡出动画
57属性动画时长秒数,默认 0.5,三种动画共用。
59属性缓动曲线枚举值,默认 backInOut。
61方法定义或读写展开值,缩放读取复用向量,移动读取缓存起点。
63方法定义或读写收起值,缩放读取复用向量,移动读取缓存终点。
73注释UI界面淡入淡出动画
87注释UI界面缩放动画
91属性打开值(number):该模块的运行态字段。
93属性关闭值(number):该模块的运行态字段。
94方法读写缩放动画的展开倍率标量,供展开向量读取时同步XY。
95方法读写缩放动画的收起倍率标量,供收起向量读取时同步XY。
100属性打开缩放比例 -本地缓存
104属性关闭缩放比例 -本地缓存
127注释UI界面移动动画
130属性移动动画的进入与退场方向,决定隐藏坐标。
132属性开始值(Vec3 | undefined):该模块的运行态字段。
133属性结束值(Vec3 | undefined):该模块的运行态字段。
143方法首次读取时缓存节点当前坐标作为显示位置,再取配置值或按方向算出隐藏位置。
167方法按移动方向结合父子节点尺寸与锚点算出屏幕外坐标,缺少 UITransform 时返回空。
203方法读写淡入淡出定义引用,编辑器按当前动作类型显示配置。
204方法读写缩放动画定义引用,编辑器按当前动作类型显示配置。
205方法读写移动动画定义引用,编辑器按当前动作类型显示配置。
206方法读取当前动作类型并通过编辑器私有setter更新选中类型。
207方法读写是否采用指定收起动画的开关,关闭时面板默认淡出。
212属性UIActionClass 当前选中的动画类型,决定 getAniType 返回谁。
214属性淡入淡出动画配置,透明度取值范围零到二百五十五。
216属性缩放动画配置,读写内部开闭缩放值。
218属性移动动画配置,保存起止坐标并计算边界。
220属性是否播放真实的退场动画,关闭时退化为淡出。
240注释使用 Cocos 布尔类型注册隐藏动画开关。
248方法按当前 actionType 返回淡入淡出、缩放或移动动画对象,未知类型回退到淡入淡出。
264方法把三种动画对象置空并保留类型字段,供面板销毁时断开引用。