From 351a7e5c82ecbdb6ebd70be65d63165473d66e55 Mon Sep 17 00:00:00 2001 From: JoneLau <32630749+JoneLau@users.noreply.github.com> Date: Thu, 21 Mar 2019 15:29:03 +0800 Subject: [PATCH] fix using spriteframe in atlas failed (#4073) --- cocos/3d/ui/components/label-component.ts | 14 +++++-- cocos/3d/ui/components/toggle-component.ts | 6 +-- .../ui/components/toggle-group-component.ts | 6 +-- cocos/3d/ui/components/widget-component.ts | 19 +++------- cocos/assets/CCSpriteFrame.ts | 38 +++++++------------ cocos/assets/sprite-atlas.ts | 32 +++++++++------- 6 files changed, 56 insertions(+), 59 deletions(-) diff --git a/cocos/3d/ui/components/label-component.ts b/cocos/3d/ui/components/label-component.ts index fd4469ae07..5ff856cf09 100644 --- a/cocos/3d/ui/components/label-component.ts +++ b/cocos/3d/ui/components/label-component.ts @@ -79,6 +79,8 @@ export enum VerticalTextAlignment { BOTTOM = 2, } +ccenum(VerticalTextAlignment); + /** * !#en Enum for Overflow. * !#zh Overflow 类型 @@ -169,7 +171,9 @@ export class LabelComponent extends UIRenderComponent { * !#zh 文本内容的水平对齐方式。 * @property {Label.HorizontalAlign} horizontalAlign */ - @property() + @property({ + type: HorizontalTextAlignment, + }) get horizontalAlign () { return this._horizontalAlign; } @@ -188,7 +192,9 @@ export class LabelComponent extends UIRenderComponent { * !#zh 文本内容的垂直对齐方式。 * @property {Label.VerticalAlign} VerticalTextAlignment */ - @property() + @property({ + type: VerticalTextAlignment, + }) get verticalAlign () { return this._verticalAlign; } @@ -277,7 +283,9 @@ export class LabelComponent extends UIRenderComponent { * !#zh 文字显示超出范围时的处理方式。 * @property {Overflow} overflow */ - @property() + @property({ + type: Overflow, + }) get overflow () { return this._overflow; } diff --git a/cocos/3d/ui/components/toggle-component.ts b/cocos/3d/ui/components/toggle-component.ts index 06d75da8cd..944ec5a1b1 100644 --- a/cocos/3d/ui/components/toggle-component.ts +++ b/cocos/3d/ui/components/toggle-component.ts @@ -27,7 +27,7 @@ import { EventHandler as ComponentEventHandler } from '../../../components/compo import { ccclass, executeInEditMode, executionOrder, menu, property } from '../../../core/data/class-decorator'; import { ButtonComponent} from './button-component'; import { SpriteComponent} from './sprite-component'; -import { ToggleGroupComponent} from './toggle-group-component'; +import { ToggleContainerComponent} from './toggle-group-component'; /** * !#en The toggle component is a CheckBox, when it used together with a ToggleGroup, it @@ -70,7 +70,7 @@ export class ToggleComponent extends ButtonComponent { * @property {ToggleGroup} toggleGroup */ @property({ - type: ToggleGroupComponent, + type: ToggleContainerComponent, }) get toggleGroup () { return this._toggleGroup; @@ -136,7 +136,7 @@ export class ToggleComponent extends ButtonComponent { @property private _isChecked: boolean = true; @property - private _toggleGroup: ToggleGroupComponent | null = null; + private _toggleGroup: ToggleContainerComponent | null = null; @property private _checkMark: SpriteComponent | null = null; diff --git a/cocos/3d/ui/components/toggle-group-component.ts b/cocos/3d/ui/components/toggle-group-component.ts index 8782e8befa..960121a994 100644 --- a/cocos/3d/ui/components/toggle-group-component.ts +++ b/cocos/3d/ui/components/toggle-group-component.ts @@ -37,11 +37,11 @@ import { ToggleComponent} from './toggle-component'; * @extends Component */ -@ccclass('cc.ToggleGroupComponent') +@ccclass('cc.ToggleContainerComponent') @executionOrder(100) @menu('UI/ToggleGroup') @executeInEditMode -export class ToggleGroupComponent extends Component { +export class ToggleContainerComponent extends Component { @property({ type: ComponentEventHandler, }) @@ -137,4 +137,4 @@ export class ToggleGroupComponent extends Component { } } -cc.ToggleGroupComponent = ToggleGroupComponent; +cc.ToggleContainerComponent = ToggleContainerComponent; diff --git a/cocos/3d/ui/components/widget-component.ts b/cocos/3d/ui/components/widget-component.ts index b5ecd8f88b..8f83c139aa 100644 --- a/cocos/3d/ui/components/widget-component.ts +++ b/cocos/3d/ui/components/widget-component.ts @@ -512,7 +512,6 @@ export class WidgetComponent extends Component { @property private _alignFlags = 0; - private _wasAlignOnce = false; @property private _target: Node | null = null; @property @@ -568,16 +567,6 @@ export class WidgetComponent extends Component { } protected onLoad () { - // TODO: - // if (this._wasAlignOnce) { - // // migrate for old version - // this.alignMode = this._wasAlignOnce ? AlignMode.ONCE : AlignMode.ALWAYS; - // this._wasAlignOnce = false; - // } - - if (this._alignMode === AlignMode.ONCE){ - this._wasAlignOnce = true; - } } protected onEnable () { @@ -604,6 +593,7 @@ export class WidgetComponent extends Component { this._originalWidth = this.node.width!; // test check conflict if (CC_EDITOR /*&& !cc.engine.isPlaying*/) { + // TODO: // _Scene.DetectConflict.checkConflict_Widget(this); } } @@ -613,13 +603,14 @@ export class WidgetComponent extends Component { // become stretch this._originalHeight = this.node.height!; // test check conflict - if (CC_EDITOR && !cc.engine.isPlaying) { + if (CC_EDITOR /*&& !cc.engine.isPlaying*/) { + // TODO: // _Scene.DetectConflict.checkConflict_Widget(this); } } } - if (CC_EDITOR && !cc.engine._isPlaying && this.node.parent) { + if (CC_EDITOR && this.node.parent) { // adjust the offsets to keep the size and position unchanged after alignment chagned cc._widgetManager.updateOffsetsToStayPut(this, flag); } @@ -641,6 +632,8 @@ export class WidgetComponent extends Component { } } +cc.WidgetComponent = WidgetComponent; + /** * !#en * When turned on, it will only be aligned once at the end of the onEnable frame, diff --git a/cocos/assets/CCSpriteFrame.ts b/cocos/assets/CCSpriteFrame.ts index c8d4524ed9..ea58edd9f9 100644 --- a/cocos/assets/CCSpriteFrame.ts +++ b/cocos/assets/CCSpriteFrame.ts @@ -121,9 +121,9 @@ export class SpriteFrame extends EventTargetFactory(Texture2D) { set insetTop (value) { this._capInsets[INSET_TOP] = value; - // if (this._texture) { - this._calculateSlicedUV(); - // } + if (this.image) { + this._calculateSlicedUV(); + } } /** @@ -139,9 +139,9 @@ export class SpriteFrame extends EventTargetFactory(Texture2D) { set insetBottom (value) { this._capInsets[INSET_BOTTOM] = value; - // if (this._texture) { - this._calculateSlicedUV(); - // } + if (this.image) { + this._calculateSlicedUV(); + } } /** @@ -157,9 +157,9 @@ export class SpriteFrame extends EventTargetFactory(Texture2D) { set insetLeft (value) { this._capInsets[INSET_LEFT] = value; - // if (this._texture) { - this._calculateSlicedUV(); - // } + if (this.image) { + this._calculateSlicedUV(); + } } /** @@ -175,9 +175,9 @@ export class SpriteFrame extends EventTargetFactory(Texture2D) { set insetRight (value) { this._capInsets[INSET_RIGHT] = value; - // if (this._texture) { - this._calculateSlicedUV(); - // } + if (this.image) { + this._calculateSlicedUV(); + } } get atlasUuid () { @@ -188,14 +188,6 @@ export class SpriteFrame extends EventTargetFactory(Texture2D) { this._atlasUuid = value; } - get atlasSize () { - return this._atlasSize; - } - - set atlasSize (value: Size) { - this._atlasSize = value; - } - public vertices: IVertices | null = null; public uv: number[] = []; @@ -216,12 +208,10 @@ export class SpriteFrame extends EventTargetFactory(Texture2D) { private _capInsets = [0, 0, 0, 0]; // store original info before packed to dynamic atlas - private _original = null; + // private _original = null; private _atlasUuid: string = ''; - private _atlasSize = new Size(); - /** * !#en * Constructor of SpriteFrame class. @@ -436,10 +426,10 @@ export class SpriteFrame extends EventTargetFactory(Texture2D) { * @method clone * @return {SpriteFrame} */ - public clone () { const cloneSprite = new SpriteFrame(); cloneSprite.name = this.name; + cloneSprite.atlasUuid = this.atlasUuid; cloneSprite.setOriginalSize(this._originalSize); cloneSprite.setRect(this._rect); const cap = this._capInsets; diff --git a/cocos/assets/sprite-atlas.ts b/cocos/assets/sprite-atlas.ts index f3c6c82a48..d7cbd88ae0 100644 --- a/cocos/assets/sprite-atlas.ts +++ b/cocos/assets/sprite-atlas.ts @@ -25,6 +25,7 @@ ****************************************************************************/ import { ccclass, property } from '../core/data/class-decorator'; +import * as js from '../core/utils/js'; import { Asset } from './asset'; import { SpriteFrame } from './CCSpriteFrame'; @@ -40,10 +41,14 @@ interface ISpriteAtlasSerializeData{ spriteFrames: string[]; } +interface ISpriteFrameList { + [key: string]: SpriteFrame | null; +} + @ccclass('cc.SpriteAtlas') export class SpriteAtlas extends Asset { @property - public spriteFrames: Map = new Map(); + public spriteFrames: ISpriteFrameList = js.createMap(); /** * Returns the texture of the sprite atlas @@ -51,10 +56,10 @@ export class SpriteAtlas extends Asset { * @returns {Texture2D} */ public getTexture () { - const values = this.spriteFrames.values; - if (values.length > 0) { - const spriteFrame = values[0]; - return spriteFrame && spriteFrame.image ? spriteFrame.image : null; + const keys = Object.keys(this.spriteFrames); + if (keys.length > 0) { + const spriteFrame = this.spriteFrames[keys[0]]; + return spriteFrame ? spriteFrame.image : null; } else { return null; @@ -85,8 +90,10 @@ export class SpriteAtlas extends Asset { */ public getSpriteFrames () { const frames: Array = []; - for (const spriteFrame of this.spriteFrames) { - frames.push(spriteFrame[1]); + const spriteFrames = this.spriteFrames; + + for (const key of Object.keys(spriteFrames)) { + frames.push(spriteFrames[key]); } return frames; @@ -94,9 +101,9 @@ export class SpriteAtlas extends Asset { public _serialize () { const frames: string[] = []; - for (const spriteFrame of this.spriteFrames) { - const id = spriteFrame[1] ? spriteFrame[1]._uuid : ''; - frames.push(spriteFrame[0]); + for (const key of Object.keys(this.spriteFrames)) { + const spriteFrame = this.spriteFrames[key]; + const id = spriteFrame ? spriteFrame._uuid : ''; frames.push(id); } @@ -110,10 +117,9 @@ export class SpriteAtlas extends Asset { const data = serializeData as ISpriteAtlasSerializeData; this._name = data.name; const frames = data.spriteFrames; - this.spriteFrames.clear(); + this.spriteFrames = js.createMap(); for (let i = 0; i < frames.length; i += 2) { - this.spriteFrames.set(frames[i], new SpriteFrame()); - handle.result.push(this.spriteFrames, `${i}`, frames[i + 1]); + handle.result.push(this.spriteFrames, frames[i], frames[i + 1]); } } }