Skip to content

Commit

Permalink
refine code
Browse files Browse the repository at this point in the history
  • Loading branch information
knoxHuang committed Oct 17, 2024
1 parent 4a8040c commit ba35e39
Showing 1 changed file with 35 additions and 32 deletions.
67 changes: 35 additions & 32 deletions cocos/2d/components/sprite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -639,24 +639,26 @@ export class Sprite extends UIRenderer {
}

private _resized (): void {
if (EDITOR) {
if (this._spriteFrame) {
const actualSize = this.node._uiProps.uiTransformComp!.contentSize;
let expectedW = actualSize.width;
let expectedH = actualSize.height;
if (this._sizeMode === SizeMode.RAW) {
const size = this._spriteFrame.originalSize;
expectedW = size.width;
expectedH = size.height;
} else if (this._sizeMode === SizeMode.TRIMMED) {
const rect = this._spriteFrame.rect;
expectedW = rect.width;
expectedH = rect.height;
}
if (!EDITOR) {
return;
}

if (expectedW !== actualSize.width || expectedH !== actualSize.height) {
this._sizeMode = SizeMode.CUSTOM;
}
if (this._spriteFrame) {
const actualSize = this.node._uiProps.uiTransformComp!.contentSize;
let expectedW = actualSize.width;
let expectedH = actualSize.height;
if (this._sizeMode === SizeMode.RAW) {
const size = this._spriteFrame.originalSize;
expectedW = size.width;
expectedH = size.height;
} else if (this._sizeMode === SizeMode.TRIMMED) {
const rect = this._spriteFrame.rect;
expectedW = rect.width;
expectedH = rect.height;
}

if (expectedW !== actualSize.width || expectedH !== actualSize.height) {
this._sizeMode = SizeMode.CUSTOM;
}
}
}
Expand Down Expand Up @@ -715,23 +717,24 @@ export class Sprite extends UIRenderer {
}

private _applyAtlas (spriteFrame: SpriteFrame | null): void {
if (EDITOR) {
if (spriteFrame) {
if (spriteFrame.atlasUuid.length > 0) {
if (!this.spriteAtlas || this.spriteAtlas.uuid !== spriteFrame.atlasUuid) {
cclegacy.assetManager.loadAny(spriteFrame.atlasUuid, (err: Error, asset: SpriteAtlas) => {
if (err) {
this.spriteAtlas = null;
error(err);
} else {
this.spriteAtlas = asset;
}
});
}
} else {
if (!EDITOR) return;

if (!spriteFrame) return;

if (spriteFrame.atlasUuid.length === 0) {
this.spriteAtlas = null;
return;
}

if (!this.spriteAtlas || this.spriteAtlas.uuid !== spriteFrame.atlasUuid) {
cclegacy.assetManager.loadAny(spriteFrame.atlasUuid, (err: Error, asset: SpriteAtlas) => {
if (err) {
this.spriteAtlas = null;
error(err);
} else {
this.spriteAtlas = asset;
}
}
});
}
}
}
Expand Down

0 comments on commit ba35e39

Please sign in to comment.