/
sprite-atlas.ts
139 lines (121 loc) · 4.13 KB
/
sprite-atlas.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
/*
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 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 asset
*/
import { ccclass, property } from '../data/class-decorator';
import * as js from '../utils/js';
import { Asset } from './asset';
import { SpriteFrame } from './sprite-frame';
interface ISpriteAtlasSerializeData{
name: string;
spriteFrames: string[];
}
interface ISpriteFrameList {
[key: string]: SpriteFrame | null;
}
/**
* @en
* Class for sprite atlas handling.
*
* @zh
* 精灵图集资源类。
* 可通过 cc.SpriteAtlas 获取该组件。
*/
@ccclass('cc.SpriteAtlas')
export class SpriteAtlas extends Asset {
@property
public spriteFrames: ISpriteFrameList = js.createMap();
/**
* @zh
* 获取精灵图集的贴图。请注意,由于结构调整优化,在 v1.1 版本之前,此函数的返回值为 imageAsset,在 v1.1 版本之后修正为 texture,想要获取 imageAsset 可使用 getTexture().image 获取
*
* @returns - 精灵贴图。
*/
public getTexture () {
const keys = Object.keys(this.spriteFrames);
if (keys.length > 0) {
const spriteFrame = this.spriteFrames[keys[0]];
return spriteFrame && spriteFrame.texture;
}
else {
return null;
}
}
/**
* @zh
* 根据键值获取精灵。
*
* @param key - 精灵名。
* @returns - 精灵。
*/
public getSpriteFrame (key: string) {
const sf = this.spriteFrames[key];
if (!sf) {
return null;
}
if (!sf.name) {
sf.name = key;
}
return sf;
}
/**
* @zh
* 获取精灵图集所有精灵。
*
* @returns - 返回所有精灵。
*/
public getSpriteFrames () {
const frames: Array<SpriteFrame | null> = [];
const spriteFrames = this.spriteFrames;
for (const key of Object.keys(spriteFrames)) {
frames.push(spriteFrames[key]);
}
return frames;
}
public _serialize (exporting?: any) {
const frames: string[] = [];
for (const key of Object.keys(this.spriteFrames)) {
const spriteFrame = this.spriteFrames[key];
let id = spriteFrame ? spriteFrame._uuid : '';
if (id && exporting) {
id = EditorExtends.UuidUtils.compressUuid(id, true);
}
frames.push(key);
frames.push(id);
}
return {
name: this._name,
spriteFrames: frames,
};
}
public _deserialize (serializeData: any, handle: any){
const data = serializeData as ISpriteAtlasSerializeData;
this._name = data.name;
const frames = data.spriteFrames;
this.spriteFrames = js.createMap();
for (let i = 0; i < frames.length; i += 2) {
handle.result.push(this.spriteFrames, frames[i], frames[i + 1]);
}
}
}
cc.SpriteAtlas = SpriteAtlas;