Skip to content

Commit

Permalink
[v3.8.6] Strip JSB relevant code in ui-opacity.ts for non-native plat…
Browse files Browse the repository at this point in the history
…forms
  • Loading branch information
dumganhar committed Jan 13, 2025
1 parent b299675 commit e54f325
Showing 1 changed file with 88 additions and 91 deletions.
179 changes: 88 additions & 91 deletions cocos/2d/components/ui-opacity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,80 @@ import { UIRenderer } from '../framework/ui-renderer';
import { Node } from '../../scene-graph';
import { NodeEventType } from '../../scene-graph/node-event';

/**
* @en
* Recursively sets localopacity.
*
* @zh
* 递归设置localopacity。
*
* @param node @en recursive node.
* @zh 递归的节点。
* @param dirty @en Is the color dirty.
* @zh color是否dirty。
* @param parentOpacity @en The parent node's opacity.
* @zh 父节点的opacity。
* @param stopRecursiveIfHasOpacity @en Stop recursion if UiOpacity component exists.
* @zh 如果存在UiOpacity组件则停止递归。
*/
function setEntityLocalOpacityDirtyRecursively (
node: Node,
dirty: boolean,
parentOpacity: number,
stopRecursiveIfHasOpacity: boolean,
): void {
if (!node.isValid) {
// Since children might be destroyed before the parent,
// we should add protecting condition when executing recursion downwards.
return;
}

const uiOp = node.getComponent(UIOpacity);
if (uiOp && stopRecursiveIfHasOpacity) {
// Because it's possible that UiOpacity components are handled by themselves (at onEnable or onDisable)
uiOp._parentOpacity = parentOpacity;
return;
}

// If the node has never been activated, then node._uiProps.uiComp won't be set.
// We need to check if the UIRenderer exists or not.
let render = node._uiProps.uiComp as UIRenderer | null;
if (!render) {
render = node.getComponent(UIRenderer);
}

if (render && render.color) { // exclude UIMeshRenderer which has not color
render.renderEntity.colorDirty = dirty;
if (uiOp) {
uiOp._parentOpacity = parentOpacity;
render.renderEntity.localOpacity = parentOpacity * uiOp.opacity / 255;
} else {
// there is a just UIRenderer but no UIOpacity on the node, we should just transport the parentOpacity to the node.
render.renderEntity.localOpacity = parentOpacity;
}
render.node._uiProps.localOpacity = render.renderEntity.localOpacity;
//No need for recursion here. Because it doesn't affect the capacity of the child nodes.
return;
}

if (uiOp) {
// there is a just UIOpacity but no UIRenderer on the node.
// we should transport the interrupt opacity downward
uiOp._parentOpacity = parentOpacity;
parentOpacity = parentOpacity * uiOp.opacity / 255;
}

const children = node.children;
for (let i = 0, len = children.length; i < len; ++i) {
setEntityLocalOpacityDirtyRecursively(
children[i],
dirty || (parentOpacity < 1),
parentOpacity,
stopRecursiveIfHasOpacity,
);
}
}

/**
* @en
* Set the UI transparency component.
Expand All @@ -56,10 +130,17 @@ export class UIOpacity extends Component {
*
* @zh
* 父节点的opacity。
*
* @engineInternal
* @mangle
*/
private _parentOpacity: number = 1.0;
public _parentOpacity: number = 1.0;

private _parentOpacityResetFlag: boolean = true;
/**
* @engineInternal
* @mangle
*/
public _parentOpacityResetFlag: boolean = true;

/**
* @en
Expand All @@ -82,7 +163,9 @@ export class UIOpacity extends Component {
this._opacity = value;
this.node._uiProps.localOpacity = value / 255;

this.setEntityLocalOpacityDirtyRecursively(true);
if (JSB) {
setEntityLocalOpacityDirtyRecursively(this.node, true, this._parentOpacity, false);
}

if (EDITOR_NOT_IN_PREVIEW) {
setTimeout(() => {
Expand All @@ -91,92 +174,6 @@ export class UIOpacity extends Component {
}
}

private setEntityLocalOpacityDirtyRecursively (dirty: boolean): void {
if (JSB) {
// const render = this.node._uiProps.uiComp as UIRenderer;
// if (render) {
// render.setEntityOpacity(this.node._uiProps.localOpacity);
// }
// UIRenderer.setEntityColorDirtyRecursively(this.node, dirty);

UIOpacity.setEntityLocalOpacityDirtyRecursively(this.node, dirty, this._parentOpacity, false);
}
}

/**
* @en
* Recursively sets localopacity.
*
* @zh
* 递归设置localopacity。
*
* @param node @en recursive node.
* @zh 递归的节点。
* @param dirty @en Is the color dirty.
* @zh color是否dirty。
* @param parentOpacity @en The parent node's opacity.
* @zh 父节点的opacity。
* @param stopRecursiveIfHasOpacity @en Stop recursion if UiOpacity component exists.
* @zh 如果存在UiOpacity组件则停止递归。
*/
public static setEntityLocalOpacityDirtyRecursively (
node: Node,
dirty: boolean,
parentOpacity: number,
stopRecursiveIfHasOpacity: boolean,
): void {
if (!node.isValid) {
// Since children might be destroyed before the parent,
// we should add protecting condition when executing recursion downwards.
return;
}

const uiOp = node.getComponent(UIOpacity);
if (uiOp && stopRecursiveIfHasOpacity) {
// Because it's possible that UiOpacity components are handled by themselves (at onEnable or onDisable)
uiOp._parentOpacity = parentOpacity;
return;
}

// If the node has never been activated, then node._uiProps.uiComp won't be set.
// We need to check if the UIRenderer exists or not.
let render = node._uiProps.uiComp as UIRenderer | null;
if (!render) {
render = node.getComponent(UIRenderer);
}

if (render && render.color) { // exclude UIMeshRenderer which has not color
render.renderEntity.colorDirty = dirty;
if (uiOp) {
uiOp._parentOpacity = parentOpacity;
render.renderEntity.localOpacity = parentOpacity * uiOp.opacity / 255;
} else {
// there is a just UIRenderer but no UIOpacity on the node, we should just transport the parentOpacity to the node.
render.renderEntity.localOpacity = parentOpacity;
}
render.node._uiProps.localOpacity = render.renderEntity.localOpacity;
//No need for recursion here. Because it doesn't affect the capacity of the child nodes.
return;
}

if (uiOp) {
// there is a just UIOpacity but no UIRenderer on the node.
// we should transport the interrupt opacity downward
uiOp._parentOpacity = parentOpacity;
parentOpacity = parentOpacity * uiOp.opacity / 255;
}

const children = node.children;
for (let i = 0, len = children.length; i < len; ++i) {
UIOpacity.setEntityLocalOpacityDirtyRecursively(
children[i],
dirty || (parentOpacity < 1),
parentOpacity,
stopRecursiveIfHasOpacity,
);
}
}

@serializable
protected _opacity = 255;

Expand Down Expand Up @@ -207,7 +204,7 @@ export class UIOpacity extends Component {
} else {
this._parentOpacityResetFlag = true;
}
UIOpacity.setEntityLocalOpacityDirtyRecursively(this.node, true, opacity, false);
setEntityLocalOpacityDirtyRecursively(this.node, true, opacity, false);
}

protected _setEntityLocalOpacityRecursively (opacity: number): void {
Expand All @@ -224,7 +221,7 @@ export class UIOpacity extends Component {
}
// The current node is not recursive, only the child nodes are recursive.
for (const child of this.node.children) {
UIOpacity.setEntityLocalOpacityDirtyRecursively(child, true, opacity, true);
setEntityLocalOpacityDirtyRecursively(child, true, opacity, true);
}
}

Expand Down

0 comments on commit e54f325

Please sign in to comment.