-
-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat:Support atlas #197 Co-authored-by: azhan <zhanyingwei.zyw@antgroup.com> Co-authored-by: GuoLei1990 <gl3336563@163.com>
- Loading branch information
1 parent
3490059
commit d9f1b9a
Showing
9 changed files
with
266 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { RefObject } from "../../asset/RefObject"; | ||
import { Engine } from "../../Engine"; | ||
import { Sprite } from "../sprite/Sprite"; | ||
|
||
/** | ||
* Sprite Atlas. | ||
*/ | ||
export class SpriteAtlas extends RefObject { | ||
private _sprites: Sprite[] = new Array<Sprite>(); | ||
private _spriteNamesToIndex: Record<string, number> = {}; | ||
|
||
/** | ||
* All the sprites in the atlas. | ||
*/ | ||
get sprites(): Readonly<Sprite[]> { | ||
return this._sprites; | ||
} | ||
|
||
/** | ||
* Get the last sprite named 'name' from the atlas. | ||
* @param name - The name of the sprite you want to find | ||
* @returns The sprite you want to find | ||
*/ | ||
getSprite(name: string): Sprite { | ||
const sprite = this._sprites[this._spriteNamesToIndex[name]]; | ||
if (!sprite) { | ||
console.warn("There is no sprite named " + name + " in the atlas."); | ||
} | ||
return sprite; | ||
} | ||
|
||
/** | ||
* Get all the sprite named 'name' from the atlas. | ||
* @param name - The name of the sprites you want to find | ||
* @param outSprites - This array holds the sprites found | ||
* @returns The sprites you want to find | ||
*/ | ||
getSprites(name: string, outSprites: Sprite[]): Sprite[] { | ||
outSprites.length = 0; | ||
let i = this._spriteNamesToIndex[name]; | ||
if (i !== undefined) { | ||
const { _sprites } = this; | ||
for (; i >= 0; i--) { | ||
const sprite = _sprites[i]; | ||
sprite.name === name && outSprites.push(sprite); | ||
} | ||
} else { | ||
console.warn("The name of the sprite you want to find is not exit in SpriteAtlas."); | ||
} | ||
return outSprites; | ||
} | ||
|
||
/** | ||
* Constructor a SpriteAtlas. | ||
* @param engine - Engine to which the SpriteAtlas belongs | ||
*/ | ||
constructor(engine: Engine) { | ||
super(engine); | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
_addSprite(sprite: Sprite): void { | ||
this._spriteNamesToIndex[sprite.name] = this._sprites.push(sprite) - 1; | ||
} | ||
|
||
/** | ||
* @override | ||
*/ | ||
_onDestroy(): void { | ||
this._sprites = null; | ||
this._spriteNamesToIndex = null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { TextureFormat } from "../../texture"; | ||
|
||
/** | ||
* The original data type of the atlas. | ||
*/ | ||
export interface AtlasConfig { | ||
/** Version of Atlas. */ | ||
version: number; | ||
/** Texture format. */ | ||
format: TextureFormat; | ||
/** The sub atlas array, each sub atlas contains multiple sprites. */ | ||
atlasItems: { | ||
/** The url of the sub atlas. */ | ||
img: string; | ||
/** Sprites contained in the sub atlas. */ | ||
sprites: AtlasSprite[]; | ||
}[]; | ||
} | ||
|
||
/** | ||
* The original data type of each sprite. | ||
*/ | ||
export interface AtlasSprite { | ||
/** The name the sprite. */ | ||
name: string; | ||
/** The range of the sprites on the big picture. */ | ||
atlasRegion: { x: number; y: number; w: number; h: number }; | ||
/** If there is trimming, the offset of the sprite relative to the original sprite. */ | ||
atlasRegionOffset: { x: number; y: number }; | ||
region: { x: number; y: number; w: number; h: number }; | ||
pivot: { x: number; y: number }; | ||
pixelsPerUnit: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { SpriteMaskInteraction } from "./enums/SpriteMaskInteraction"; | ||
export { SpriteMaskLayer } from "./enums/SpriteMaskLayer"; | ||
export { SpriteAtlas } from "./atlas/SpriteAtlas"; | ||
export * from "./sprite/index"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.