Skip to content

Commit

Permalink
fix widget always mode bug (#8304)
Browse files Browse the repository at this point in the history
  • Loading branch information
LinYunMo committed Mar 1, 2021
1 parent 5d5a8fc commit d7f8389
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions cocos/ui/widget.ts
Expand Up @@ -34,7 +34,7 @@ import { EDITOR, DEV } from 'internal:constants';
import { Component } from '../core/components';
import { UITransform } from '../2d/framework/ui-transform';
import { Size, Vec3 } from '../core/math';
import { errorID, warnID } from '../core/platform/debug';
import { errorID } from '../core/platform/debug';
import { SystemEventType } from '../core/platform/event-manager/event-enum';
import { View } from '../core/platform/view';
import visibleRect from '../core/platform/visible-rect';
Expand Down Expand Up @@ -824,12 +824,16 @@ export class Widget extends Component {
if (this.node.getParent()) {
this._registerTargetEvents();
}
this._setDirtyByMode();
}

protected _registerEvent () {
if (EDITOR && !legacyCC.GAME_VIEW) {
this.node.on(SystemEventType.TRANSFORM_CHANGED, this._adjustWidgetToAllowMovingInEditor, this);
this.node.on(SystemEventType.SIZE_CHANGED, this._adjustWidgetToAllowResizingInEditor, this);
} else {
this.node.on(SystemEventType.TRANSFORM_CHANGED, this._setDirtyByMode, this);
this.node.on(SystemEventType.SIZE_CHANGED, this._setDirtyByMode, this);
}
this.node.on(SystemEventType.ANCHOR_CHANGED, this._adjustWidgetToAnchorChanged, this);
this.node.on(SystemEventType.PARENT_CHANGED, this._adjustTargetToParentChanged, this);
Expand All @@ -839,6 +843,9 @@ export class Widget extends Component {
if (EDITOR && !legacyCC.GAME_VIEW) {
this.node.off(SystemEventType.TRANSFORM_CHANGED, this._adjustWidgetToAllowMovingInEditor, this);
this.node.off(SystemEventType.SIZE_CHANGED, this._adjustWidgetToAllowResizingInEditor, this);
} else {
this.node.off(SystemEventType.TRANSFORM_CHANGED, this._setDirtyByMode, this);
this.node.off(SystemEventType.SIZE_CHANGED, this._setDirtyByMode, this);
}
this.node.off(SystemEventType.ANCHOR_CHANGED, this._adjustWidgetToAnchorChanged, this);
}
Expand Down Expand Up @@ -877,29 +884,29 @@ export class Widget extends Component {
const target = this._target || this.node.parent;
if (target) {
if (target.getComponent(UITransform)) {
target.on(SystemEventType.TRANSFORM_CHANGED, this._targetChangedOperation, this);
target.on(SystemEventType.SIZE_CHANGED, this._targetChangedOperation, this);
target.on(SystemEventType.TRANSFORM_CHANGED, this._setDirtyByMode, this);
target.on(SystemEventType.SIZE_CHANGED, this._setDirtyByMode, this);
}
}
}

protected _unregisterTargetEvents () {
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);
target.off(SystemEventType.TRANSFORM_CHANGED, this._setDirtyByMode, this);
target.off(SystemEventType.SIZE_CHANGED, this._setDirtyByMode, this);
}
}

protected _unregisterOldParentEvents (oldParent: Node) {
const target = this._target || oldParent;
if (target) {
target.off(SystemEventType.TRANSFORM_CHANGED, this._targetChangedOperation, this);
target.off(SystemEventType.SIZE_CHANGED, this._targetChangedOperation, this);
target.off(SystemEventType.TRANSFORM_CHANGED, this._setDirtyByMode, this);
target.off(SystemEventType.SIZE_CHANGED, this._setDirtyByMode, this);
}
}

protected _targetChangedOperation () {
protected _setDirtyByMode () {
if (this.alignMode === AlignMode.ALWAYS) {
this._recursiveDirty();
}
Expand Down

0 comments on commit d7f8389

Please sign in to comment.