CCViewBase.ts
7.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import { Emitter } from "simba-eventkit";
import TouchSpecialEffComp from "../components/TouchSpecialEffComp";
import { View, PureView, Subview, PureSubview, OnSubviewCreated } from "./ViewBase";
type PickKeysForType<T extends {}, U> = {
[P in keyof T]: T[P] extends U ? P : never
}[keyof T];
class CCViewBase extends cc.Component {
protected _emitter: Emitter = new Emitter;
protected _showed = false;
private _isHidden = false;
get isHidden() { return this._isHidden || !this.node.active }
onShow = this._emitter.createEvent<() => void>();
onHide = this._emitter.createEvent<() => void>();
private _posX: number;
show(): void {
if (!this.node.parent || !this.isHidden) return;
if (this.isHidden) {
this.node.active = true;
this._isHidden = false;
this.node.opacity = 255;
if (this._posX !== undefined)
this.node.x = this._posX;
this.onShow.emit();
}
}
hide(): void {
if (!this.node.parent || this.isHidden) return;
if (!this.isHidden) {
this._isHidden = true;
this._posX = this.node.x;
this.node.opacity = 0;
this.node.x = 10000;
this.onHide.emit();
}
}
}
export class CCView extends CCViewBase implements View {
readonly _type: "View" = "View";
onClose = this._emitter.createEvent<(destroy?: boolean) => void>();
open(parent: CCView) {
this.node.parent = parent.node;
}
close(destroy?: boolean): void {
if (!this.node.parent) return;
let com = this.node.getComponent('AlterAnim');
let yes = com ? this.node.getComponent('AlterAnim').enabled : false;
if (destroy) {
if (yes) {
com.hideUIAnim({
callback: () => {
this.node.destroy();
},
})
} else {
this.node.destroy();
}
} else {
if (yes) {
com.hideUIAnim({
callback: () => {
this.node.removeFromParent(false);
},
})
} else {
this.node.removeFromParent(false);
}
}
this.onClose.emit(destroy);
}
/**
* @param node
* @param gray 是否置灰 true:置灰 false:正常
* @param childrenIsGray node传node时孩子节点是否置灰(将递归所有节点)
*/
setGray(node: cc.Node | cc.Label | cc.Sprite, gray: boolean = true, childrenIsGray?: boolean): void {
let target: any = node;
let str = gray ? cc.Material.BUILTIN_NAME.GRAY_SPRITE : cc.Material.BUILTIN_NAME.SPRITE;
var material = cc.Material.getBuiltinMaterial(str + "");
if (node instanceof cc.Node) {
target = node.getComponent(cc.Sprite);
target && target.setMaterial(0, material);
target = node.getComponent(cc.Label);
target && target.setMaterial(0, material);
childrenIsGray && node.children.forEach(subNode => {
this.setGray(subNode, gray, childrenIsGray);
}, this)
} else {
target && target.setMaterial(0, material);
}
}
}
export class CCSubview extends CCViewBase implements Subview {
readonly _type: "Subview" = "Subview";
constructor() {
super();
OnSubviewCreated.emit(this);
}
onLoad() {
this.name = this.node.name;
}
attachParent(parent: CCView) {
this.node.parent = parent.node;
}
}
abstract class PureViewBase<P> implements PureViewBase<P> {
protected _prevProps: Readonly<P>;
protected _props: Readonly<P>;
private _tempProps: Readonly<P>;
private _prepareUpdate = false;
private _propBinder: { [key: string]: { target: object | Function, prop: string }[] } = {};
abstract scheduleOnce(callback: Function, delay?: number): void;
setProps(props: P) {
this._tempProps = props;
if (!this._props) {
this._setProps();
} else {
if (!this._prepareUpdate) {
this._prepareUpdate = true;
this.scheduleOnce(() => { // delay one frame, update at most once per frame
this._setProps();
});
}
}
}
protected _setProps() {
this._prepareUpdate = false;
this._prevProps = this._props;
this._props = { ...this._tempProps };
delete (this as any)._tempProps;
this.onPropsReceive();
if (!this._prevProps) {
this.onPropsLoad(this._props);
} else {
let keys = new Set([...Object.keys(this._prevProps), ...Object.keys(this._props)]);
for (let key of keys) {
if (this._props[key] !== this._prevProps[key]) {
this.onPropChange(key as Extract<keyof P, string>);
}
}
}
}
updateProps(props: Partial<P>) {
if (!this._props) throw new Error("setProps first.");
let fullProps = this._tempProps ? { ...this._tempProps, ...props } : { ...this._props, ...props };
this.setProps(fullProps);
}
/**
* onLoad() {
* this.bindProp("title", this.titleLabel, "string");
* this.bindProp("percent", (value) => this.percentLabel.string = value + "%");
* }
*/
protected bindProp<T extends {}, K extends keyof P>(key: Extract<K, string>, target: (v: P[K]) => void): void;
protected bindProp<T extends {}, K extends keyof P>(key: Extract<K, string>, target: T, propName: Extract<PickKeysForType<T, P[K]>, string>): void;
protected bindProp(key: string, target, prop?) {
if (!this._propBinder[key]) this._propBinder[key] = [];
this._propBinder[key].push({ target, prop });
}
protected onPropsReceive(): void {
}
protected onPropsLoad(props: Readonly<P>) {
for (let key in props) {
if (this._propBinder[key]) {
for (const { target, prop } of this._propBinder[key]) {
if (typeof target === "function") {
target(this._props[key]);
} else {
target[prop] = this._props[key];
}
}
}
}
}
protected onPropChange(key: Extract<keyof P, string>) {
if (this._propBinder[key]) {
for (const { target, prop } of this._propBinder[key]) {
if (typeof target === "function") {
target(this._props[key]);
} else {
target[prop] = this._props[key];
}
}
}
}
}
export class CCPureView<P> extends CCView {
constructor() {
super();
(this as any)._propBinder = {};
}
onLoad() { }
}
export interface CCPureView<P> extends PureViewBase<P> { }
export class CCPureSubview<P> extends CCSubview {
constructor() {
super();
(this as any)._propBinder = {};
}
}
export interface CCPureSubview<P> extends PureViewBase<P> { }
function applyMixins(derivedCtor: any, constructors: any[]) {
constructors.forEach((baseCtor) => {
Object.getOwnPropertyNames(baseCtor.prototype).forEach((name) => {
if (name !== "constructor") {
Object.defineProperty(
derivedCtor.prototype,
name,
Object.getOwnPropertyDescriptor(baseCtor.prototype, name)!
);
}
});
});
}
applyMixins(CCPureView, [PureViewBase]);
applyMixins(CCPureSubview, [PureViewBase]);