Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow custom image resource path #24

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions src/AsepriteRaw.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import { AsepriteRawFrame } from './AsepriteRawFrame';

export class AsepriteRaw {
frames!: {
[key: string]: {
frame: { x: number, y: number, w: number, h: number },
rotated: boolean,
trimmed: boolean,
spriteSourceSize: { x: number, y: number, w: number, h: number },
sourceSize: { w: number, h: number },
duration: number
}
[key: string]: AsepriteRawFrame,
}
meta!: {
image: string,
Expand Down
8 changes: 8 additions & 0 deletions src/AsepriteRawFrame.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface AsepriteRawFrame {
frame: { x: number, y: number, w: number, h: number },
rotated: boolean,
trimmed: boolean,
spriteSourceSize: { x: number, y: number, w: number, h: number },
sourceSize: { w: number, h: number },
duration: number
}
8 changes: 5 additions & 3 deletions src/AsepriteResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import { AsepriteSpriteSheet } from './AsepriteSpriteSheet';

export class AsepriteResource implements Loadable<AsepriteSpriteSheet> {
private _resource: Resource<AsepriteRaw>;
private _imagePath?: string;
public data!: AsepriteSpriteSheet;
public rawAseprite!: AsepriteRaw;
public image!: ImageSource;

public convertPath: (originPath: string, relativePath: string) => string;
constructor(path: string, public bustCache = false) {
constructor(path: string, public bustCache = false, imagePath?: string) {
this._imagePath = imagePath;
this._resource = new Resource(path, 'json', bustCache);
this.convertPath = (originPath: string, relativePath: string) => {
// Use absolute path if specified
Expand All @@ -30,8 +32,8 @@ export class AsepriteResource implements Loadable<AsepriteSpriteSheet> {

public async load(): Promise<AsepriteSpriteSheet> {
const asepriteData = await this._resource.load();
const imagepath = this.convertPath(this._resource.path, asepriteData.meta.image);
const spriteSheetImage = new ImageSource(imagepath, this.bustCache);
const imagePath = this.convertPath(this._resource.path, this._imagePath || asepriteData.meta.image);
const spriteSheetImage = new ImageSource(imagePath, this.bustCache);
await spriteSheetImage.load();
this.rawAseprite = asepriteData;
this.image = spriteSheetImage;
Expand Down
2 changes: 1 addition & 1 deletion src/AsepriteSpriteSheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export class AsepriteSpriteSheet {
private _spriteSheet: SpriteSheet;
private _animations = new Map<string, Animation>()
constructor(public asepriteRaw: AsepriteRaw, public image: ImageSource) {
for (let frameTag of asepriteRaw.meta.frameTags) {
for (let frameTag of asepriteRaw.meta.frameTags || []) {
let from = frameTag.from;
let to = frameTag.to;
let frameIndices = range(from, to);
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

export * from './AsepriteRaw';
export * from './AsepriteRawFrame';
export * from './AsepriteResource';
export * from './AsepriteSpriteSheet';
10 changes: 10 additions & 0 deletions test/unit/AsepriteResource.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ describe('An Aseprite Resource', () => {
expect(sut.rawAseprite).toBeDefined();
});

it('can load a resource with custom image path', async () => {
const sut = new AsepriteResource('test/unit/beetle.json', false, 'beetle.png');
await sut.load();

expect(sut.isLoaded());
expect(sut.image).toBeDefined();
expect(sut.image.path).toBe('test/unit/beetle.png');
expect(sut.rawAseprite).toBeDefined();
});

it('will log a warning if not yet loaded', () => {
const sut = new AsepriteResource('test/unit/beetle.json');
const logger = Logger.getInstance();
Expand Down