fix widget not update in anchor changed (#4806)

This commit is contained in:
JoneLau
2019-07-04 10:12:57 +08:00
committed by Weiliang Wang
parent 247dee67b0
commit 0d2cabe1db
2 changed files with 20 additions and 9 deletions
+9 -7
View File
@@ -616,17 +616,19 @@ export class WidgetComponent extends Component {
this._recursiveDirty();
}
protected _registerTargetEvents (){
if (this._target){
this._target.on(SystemEventType.TRANSFORM_CHANGED, this._targetChangedOperation, this);
this._target.on(SystemEventType.SIZE_CHANGED, this._targetChangedOperation, this);
protected _registerTargetEvents () {
const target = this._target || this.node.parent;
if (target){
target.on(SystemEventType.TRANSFORM_CHANGED, this._targetChangedOperation, this);
target.on(SystemEventType.SIZE_CHANGED, this._targetChangedOperation, this);
}
}
protected _unregisterTargetEvents () {
if (this._target) {
this._target.off(SystemEventType.TRANSFORM_CHANGED, this._targetChangedOperation, this);
this._target.off(SystemEventType.SIZE_CHANGED, this._targetChangedOperation, this);
const target = this._target || this.node.parent;
if (target) {
target.off(SystemEventType.TRANSFORM_CHANGED, this._targetChangedOperation, this);
target.off(SystemEventType.SIZE_CHANGED, this._targetChangedOperation, this);
}
}
+11 -2
View File
@@ -178,6 +178,8 @@ function align (node: Node, widget: WidgetComponent) {
x = localRight + (anchorX - 1) * width;
}
}
widget._lastSize.width = width;
}
if (widget.alignFlags & AlignFlags.VERTICAL) {
@@ -236,6 +238,8 @@ function align (node: Node, widget: WidgetComponent) {
y = localTop + (anchorY - 1) * height;
}
}
widget._lastSize.height = height;
}
node.setPosition(x, y, _tempPos.z);
@@ -375,7 +379,6 @@ function adjustWidgetToAllowMovingInEditor (this: WidgetComponent, eventType: Sy
const oldPos = this._lastPos;
const delta = new Vec3(newPos);
delta.subtract(oldPos);
oldPos.set(newPos);
let target = self.node.parent!;
const inverseScale = new Vec3(1, 1, 1);
@@ -427,7 +430,6 @@ function adjustWidgetToAllowResizingInEditor (this: WidgetComponent/*, oldSize:
const newSize = self.node.getContentSize();
const oldSize = this._lastSize;
const delta = new Vec3(newSize.width - oldSize.width, newSize.height - oldSize.height, 0);
oldSize.set(newSize);
let target = self.node.parent!;
const inverseScale = new Vec3(1, 1, 1);
@@ -458,6 +460,11 @@ function adjustWidgetToAllowResizingInEditor (this: WidgetComponent/*, oldSize:
}
}
// 节点被父节点或者 target 的尺寸影响而重新更新
function adjustWidgetToAnchorChanged (this: WidgetComponent) {
this.setDirty();
}
const activeWidgets: WidgetComponent[] = [];
// updateAlignment from scene to node recursively
@@ -515,6 +522,7 @@ export const widgetManager = cc._widgetManager = {
}
widget.node.on(SystemEventType.TRANSFORM_CHANGED, adjustWidgetToAllowMovingInEditor, widget);
widget.node.on(SystemEventType.SIZE_CHANGED, adjustWidgetToAllowResizingInEditor, widget);
widget.node.on(SystemEventType.ANCHOR_CHANGED, adjustWidgetToAnchorChanged, widget);
// }
},
remove (widget: WidgetComponent) {
@@ -522,6 +530,7 @@ export const widgetManager = cc._widgetManager = {
// if (CC_EDITOR && !cc.engine.isPlaying) {
widget.node.off(SystemEventType.TRANSFORM_CHANGED, adjustWidgetToAllowMovingInEditor, widget);
widget.node.off(SystemEventType.SIZE_CHANGED, adjustWidgetToAllowResizingInEditor, widget);
widget.node.off(SystemEventType.ANCHOR_CHANGED, adjustWidgetToAnchorChanged, widget);
// }
},
onResized () {