-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
ui-component.ts
174 lines (148 loc) · 5.07 KB
/
ui-component.ts
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
/*
Copyright (c) 2017-2019 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/**
* @category ui
*/
import { Component } from '../component';
import { ccclass, disallowMultiple, executeInEditMode, executionOrder, property } from '../../../core/data/class-decorator';
import { SystemEventType } from '../../../core/platform/event-manager/event-enum';
import { INode } from '../../../core/utils/interfaces';
import { UI } from '../../renderer/ui/ui';
import { CanvasComponent } from './canvas-component';
/**
* @zh
* UI 及 UI 模型渲染基类。
*/
@ccclass('cc.UIComponent')
@executionOrder(110)
@disallowMultiple
@executeInEditMode
export class UIComponent extends Component {
/**
* @zh
* 渲染先后顺序,按照广度渲染排列,按同级节点下进行一次排列。
*/
@property
get priority () {
return this._priority;
}
set priority (value) {
if (this._priority === value) {
return;
}
this._priority = value;
this._sortSiblings();
}
/**
* @zh
* 查找被渲染相机。
*/
get visibility () {
return this._visibility;
}
@property
protected _priority = 0;
protected _visibility = -1;
private _lastParent: INode | null = null;
public onEnable () {
this._lastParent = this.node.parent;
this._updateVisibility();
if (this._lastParent) {
this.node.on(SystemEventType.CHILD_REMOVED, this._parentChanged, this);
}
this.node._uiComp = this;
this._sortSiblings();
}
public onDisable () {
this._cancelEventFromParent();
if (this.node._uiComp === this) {
this.node._uiComp = null;
}
}
/**
* @zh
* 渲染数据收集。每个渲染组件都由此自身决定是否渲染以及渲染状态的更新。
*
* @param render 数据处理中转站。
*/
public updateAssembler(render: UI) {
}
/**
* @zh
* 后渲染数据收集。每个渲染组件都由此接口决定是否渲染以及渲染状态的更新。
* 一般是在自身子节点 updateAssembler 执行完调用。
*
* @param render 数据处理中转站。
*/
public postUpdateAssembler(render: UI) {
}
/**
* @zh
* 设置当前组件的可视编号。(我们不希望用户自行做处理,除非用户自己知道在做什么)
*/
public setVisibility (value: number) {
this._visibility = value;
}
protected _parentChanged (node: INode) {
if (node === this.node) {
this._updateVisibility();
this._cancelEventFromParent();
this._lastParent = this.node.parent;
this._sortSiblings();
return true;
}
return false;
}
private _sortSiblings () {
const siblings = this.node.parent && this.node.parent.children as Mutable<INode[]>;
if (siblings) {
siblings.sort((a: INode, b: INode) => {
const aComp = a._uiComp;
const bComp = b._uiComp;
const ca = aComp ? aComp.priority : 0;
const cb = bComp ? bComp.priority : 0;
return ca - cb;
});
}
}
private _updateVisibility () {
let parent = this.node;
// 获取被渲染相机的 visibility
while (parent) {
if (parent) {
const canvasComp = parent.getComponent(CanvasComponent);
if (canvasComp) {
this._visibility = canvasComp.visibility;
break;
}
}
// @ts-ignore
parent = parent.parent;
}
}
private _cancelEventFromParent () {
if (this._lastParent) {
this._lastParent.off(SystemEventType.CHILD_REMOVED, this._parentChanged, this);
this._lastParent = null;
}
this._visibility = -1;
}
}