-
Notifications
You must be signed in to change notification settings - Fork 1
/
NinePanel.ts
85 lines (77 loc) · 1.91 KB
/
NinePanel.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
import { Texture } from "../texture";
import { Transform } from "../transform";
import { Component } from "./Component";
import { Sprite } from "./Sprite";
/**
* 九宫格绘制
* 将原图拆分9部分
* ```ts
* // 创建一个图片节点
* const node = new Transform(Sprite);
* // 获取图片组件
* const sprite = node.getComponent(Sprite);
* // 加载图片资源
* Resource.load('1.png').then((res: IRes) => {
* // 设置纹理素材
* sprite.texture = new ImageTexture(res.data);
* // 根据纹理重新设置挂载节点的尺寸
* sprite.resize();
*
* const ninePanel = node.addComponent(NinePanel);
* ninePanel.left = 7;
* ninePanel.right = 7;
* ninePanel.top = 7;
* ninePanel.bottom = 7;
* node.width = 400;
* node.height = 200;
* });
* ```
*/
export class NinePanel extends Component {
private _left = 0;
get left() {
return this._left;
}
set left(val: number) {
this._left = val;
this.toRedraw();
}
private _top = 0;
get top() {
return this._top;
}
set top(val: number) {
this._top = val;
this.toRedraw();
}
private _right = 0;
get right() {
return this._right;
}
set right(val: number) {
this._right = val;
this.toRedraw();
}
private _bottom = 0;
get bottom() {
return this._bottom;
}
set bottom(val: number) {
this._bottom = val;
this.toRedraw();
}
texture?: Texture;
start(): void {
if (!this.texture) {
this.texture = (<Sprite>this.node.container).texture;
}
this.node.emitter.on(Transform.Event.RESIZE, this.toRedraw, this);
}
/**
* 重新生成
*/
toRedraw() {
this.redraw = true;
}
redraw = false;
}