Commit c83815d2 authored by rockyl's avatar rockyl

修复显示对象未设置尺寸与设置0的问题

parent 9566ced2
...@@ -706,7 +706,8 @@ export default class Container extends DisplayObject { ...@@ -706,7 +706,8 @@ export default class Container extends DisplayObject {
* @member {number} * @member {number}
*/ */
get width(): number { get width(): number {
return this._width || this.scale.x * this.getLocalBounds().width; let value = this._width;
return !value && value != 0 ? this.scale.x * this.getLocalBounds().width : value;
} }
set width(value: number) { set width(value: number) {
...@@ -718,10 +719,14 @@ export default class Container extends DisplayObject { ...@@ -718,10 +719,14 @@ export default class Container extends DisplayObject {
// this.scale.x = 1; // this.scale.x = 1;
// } // }
if (this._width !== value) { if (this._width !== value) {
if (!value && value != 0) {
this._width = undefined;
} else {
//子类有用,有_width,才需设置scaleX //子类有用,有_width,才需设置scaleX
this._width = value; this._width = value;
this._localBoundsSelf.width = value; this._localBoundsSelf.width = value;
//if (this.stage) this.stage.layoutInvalid = true; //if (this.stage) this.stage.layoutInvalid = true;
}
this.dispatchEvent(Event.RESIZE); this.dispatchEvent(Event.RESIZE);
} }
} }
...@@ -731,7 +736,8 @@ export default class Container extends DisplayObject { ...@@ -731,7 +736,8 @@ export default class Container extends DisplayObject {
* @member {number} * @member {number}
*/ */
get height(): number { get height(): number {
return this._height || this.scale.y * this.getLocalBounds().height; let value = this._height;
return !value && value != 0 ? this.scale.y * this.getLocalBounds().height : value;
} }
set height(value: number) { set height(value: number) {
...@@ -742,10 +748,14 @@ export default class Container extends DisplayObject { ...@@ -742,10 +748,14 @@ export default class Container extends DisplayObject {
// } else { // } else {
// this.scale.y = 1; // this.scale.y = 1;
// } // }
if (!value && value != 0) {
this._height = undefined;
} else {
if (this._height !== value) { if (this._height !== value) {
this._height = value; this._height = value;
this._localBoundsSelf.height = value; this._localBoundsSelf.height = value;
//if (this.stage) this.stage.layoutInvalid = true; //if (this.stage) this.stage.layoutInvalid = true;
}
this.dispatchEvent(Event.RESIZE); this.dispatchEvent(Event.RESIZE);
} }
} }
......
...@@ -94,9 +94,9 @@ export default class Sprite extends Container { ...@@ -94,9 +94,9 @@ export default class Sprite extends Container {
this._texture = null; this._texture = null;
this._width = 0; //this._width = 0;
this._height = 0; //this._height = 0;
this._tint = null; this._tint = null;
this._tintRGB = null; this._tintRGB = null;
...@@ -391,14 +391,22 @@ export default class Sprite extends Container { ...@@ -391,14 +391,22 @@ export default class Sprite extends Container {
* @member {number} * @member {number}
*/ */
get width() { get width() {
return Math.abs(this.scale.x) * this._texture.orig.width; let value = this._width;
return !value && value != 0 ? Math.abs(this.scale.x) * this._texture.orig.width : value;
} }
set width(value) { set width(value) {
if (this._width !== value) {
if (!value && value != 0) {
this.scale.x = 1
} else {
const s = sign(this.scale.x) || 1; const s = sign(this.scale.x) || 1;
this.scale.x = s * value / this._texture.orig.width; this.scale.x = s * value / this._texture.orig.width;
}
this._width = value; this._width = value;
this.dispatchEvent(Event.RESIZE);
}
} }
/** /**
...@@ -406,13 +414,23 @@ export default class Sprite extends Container { ...@@ -406,13 +414,23 @@ export default class Sprite extends Container {
* @member {number} * @member {number}
*/ */
get height() { get height() {
return Math.abs(this.scale.y) * this._texture.orig.height; let value = this._height;
return !value && value != 0 ? Math.abs(this.scale.y) * this._texture.orig.height : value;
} }
set height(value) { set height(value) {
if (!value && value != 0) {
this._height = undefined;
} else {
if (!value && value != 0) {
this.scale.y = 1
} else {
const s = sign(this.scale.y) || 1; const s = sign(this.scale.y) || 1;
this.scale.y = s * value / this._texture.orig.height; this.scale.y = s * value / this._texture.orig.height;
}
this._height = value; this._height = value;
this.dispatchEvent(Event.RESIZE);
}
} }
/** /**
......
...@@ -169,28 +169,42 @@ export class TextField extends Sprite { ...@@ -169,28 +169,42 @@ export class TextField extends Sprite {
// private _textHeight: number = 0; // private _textHeight: number = 0;
get width(): number { get width(): number {
if (this._width) return this._width; if (!this._width && this._width != 0) {
this.updateText(); this.updateText();
return this.scale.x * this.getLocalBounds().width; return this.scale.x * this.getLocalBounds().width;
} else {
return this._width;
}
} }
set width(value: number) { set width(value: number) {
if (this._width !== value) { if (this._width !== value) {
if (!value && value != 0) {
this._width = undefined;
} else {
this._width = value; this._width = value;
}
this.dirty = true; this.dirty = true;
this.dispatchEvent(Event.RESIZE); this.dispatchEvent(Event.RESIZE);
} }
} }
get height(): number { get height(): number {
if (this._height) return this._height; if (!this._height && this._height != 0) {
this.updateText(); this.updateText();
return this.scale.y * this.getLocalBounds().height; return this.scale.y * this.getLocalBounds().height;
} else {
return this._height;
}
} }
set height(value: number) { set height(value: number) {
if (this._height !== value) { if (this._height !== value) {
if (!value && value != 0) {
this._height = undefined;
} else {
this._height = value; this._height = value;
}
this.dirty = true; this.dirty = true;
this.dispatchEvent(Event.RESIZE); this.dispatchEvent(Event.RESIZE);
} }
...@@ -691,7 +705,7 @@ export class TextField extends Sprite { ...@@ -691,7 +705,7 @@ export class TextField extends Sprite {
s.realLines = realLines; s.realLines = realLines;
s._prepContext(ctx); s._prepContext(ctx);
let textWidth = s._width; let textWidth = !s._width && s._width != 0 ? 0 : s._width;
// let lineH = s._lineSpacing + s.size; // let lineH = s._lineSpacing + s.size;
//单行文本时 //单行文本时
if (isPureText && text.indexOf("\n") < 0 && s.lineType == TEXT_lINETYPE.SINGLE) { if (isPureText && text.indexOf("\n") < 0 && s.lineType == TEXT_lINETYPE.SINGLE) {
...@@ -719,7 +733,7 @@ export class TextField extends Sprite { ...@@ -719,7 +733,7 @@ export class TextField extends Sprite {
} }
} else { } else {
//textWidth取每行最大值,如果没设置过textWidth //textWidth取每行最大值,如果没设置过textWidth
const shouldMeasureTextWidth = !textWidth; const shouldMeasureTextWidth = !s._width && s._width != 0 ? true : false;
let index = 0; let index = 0;
for (let i = 0, l = hardLines.length; i < l; i++) { for (let i = 0, l = hardLines.length; i < l; i++) {
let str = hardLines[i]; let str = hardLines[i];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment