Skip to content

Commit

Permalink
[v3.8.6] Optimize code size: Add an internal method ( Node.getUITrans…
Browse files Browse the repository at this point in the history
…formComp ) which could be mangled to avoid invoking the code `._uiProps.uiTransformComp` that could not be mangled. (#18165)

* [v3.8.6] Optimize code size: Add an internal Node.getUITransformComponent which could be mangled to avoid invoking the code `._uiProps.uiTransformComp` that could not be mangled.
  • Loading branch information
dumganhar authored Jan 13, 2025
1 parent 86b314f commit 67a0736
Show file tree
Hide file tree
Showing 36 changed files with 166 additions and 156 deletions.
2 changes: 1 addition & 1 deletion cocos/2d/assembler/label/bmfontUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export const bmfontUtils = {

if (comp.renderData.vertDirty) {
_comp = comp;
_uiTrans = _comp.node._uiProps.uiTransformComp!;
_uiTrans = _comp.node._getUITransformComp()!;
const renderData = comp.renderData;

const processing = TextProcessing.instance;
Expand Down
2 changes: 1 addition & 1 deletion cocos/2d/assembler/label/ttf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export const ttf: IAssembler = {
if (!renderData) {
return;
}
const uiTrans = comp.node._uiProps.uiTransformComp!;
const uiTrans = comp.node._getUITransformComp()!;
const width = uiTrans.width;
const height = uiTrans.height;
const appX = uiTrans.anchorX * width;
Expand Down
2 changes: 1 addition & 1 deletion cocos/2d/assembler/label/ttfUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export const ttfUtils = {
if (!comp.renderData) { return; }

if (comp.renderData.vertDirty) {
const trans = comp.node._uiProps.uiTransformComp!;
const trans = comp.node._getUITransformComp()!;
const processing = TextProcessing.instance;
const style = comp.textStyle;
const layout = comp.textLayout;
Expand Down
2 changes: 1 addition & 1 deletion cocos/2d/assembler/sprite/bar-filled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export const barFilled: IAssembler = {
updateVertexData (sprite: Sprite, fillStart: number, fillEnd: number) {
const renderData: RenderData | null = sprite.renderData;
const dataList: IRenderData[] = renderData!.data;
const uiTrans = sprite.node._uiProps.uiTransformComp!;
const uiTrans = sprite.node._getUITransformComp()!;
const width = uiTrans.width;
const height = uiTrans.height;
const appX = uiTrans.anchorX * width;
Expand Down
2 changes: 1 addition & 1 deletion cocos/2d/assembler/sprite/radial-filled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ function _calcIntersectedPoints (
}

function _calculateVertices (sprite: Sprite): void {
const uiTrans = sprite.node._uiProps.uiTransformComp!;
const uiTrans = sprite.node._getUITransformComp()!;
const width = uiTrans.width;
const height = uiTrans.height;
const appX = uiTrans.anchorX * width;
Expand Down
10 changes: 5 additions & 5 deletions cocos/2d/assembler/sprite/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ const QUAD_INDICES = Uint16Array.from([0, 1, 2, 1, 3, 2]);
* 可通过 `UI.simple` 获取该组装器。
*/
export const simple: IAssembler = {
createData(sprite: Sprite) {
createData (sprite: Sprite) {
const renderData = sprite.requestRenderData();
renderData.dataLength = 4;
renderData.resize(4, 6);
renderData.chunk.setIndexBuffer(QUAD_INDICES);
return renderData;
},

updateRenderData(sprite: Sprite) {
updateRenderData (sprite: Sprite) {
const frame = sprite.spriteFrame;

dynamicAtlasManager.packToDynamicAtlas(sprite, frame);
Expand Down Expand Up @@ -95,7 +95,7 @@ export const simple: IAssembler = {
}
},

fillBuffers(sprite: Sprite, renderer: IBatcher) {
fillBuffers (sprite: Sprite, renderer: IBatcher) {
if (sprite === null) {
return;
}
Expand Down Expand Up @@ -138,13 +138,13 @@ export const simple: IAssembler = {
// renderer.switchBufferAccessor().appendIndices(chunk);
},

updateVertexData(sprite: Sprite) {
updateVertexData (sprite: Sprite) {
const renderData: RenderData | null = sprite.renderData;
if (!renderData) {
return;
}

const uiTrans = sprite.node._uiProps.uiTransformComp!;
const uiTrans = sprite.node._getUITransformComp()!;
const dataList: IRenderData[] = renderData.data;
const cw = uiTrans.width;
const ch = uiTrans.height;
Expand Down
2 changes: 1 addition & 1 deletion cocos/2d/assembler/sprite/sliced.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export const sliced: IAssembler = {
const renderData = sprite.renderData;
if (!renderData) return;
const dataList: IRenderData[] = renderData.data;
const uiTrans = sprite.node._uiProps.uiTransformComp!;
const uiTrans = sprite.node._getUITransformComp()!;
const width = uiTrans.width;
const height = uiTrans.height;
const appX = uiTrans.anchorX * width;
Expand Down
4 changes: 2 additions & 2 deletions cocos/2d/assembler/sprite/tiled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export const tiled: IAssembler = {
return;
}

const uiTrans = sprite.node._uiProps.uiTransformComp!;
const uiTrans = sprite.node._getUITransformComp()!;
const contentWidth = Math.abs(uiTrans.width);
const contentHeight = Math.abs(uiTrans.height);

Expand Down Expand Up @@ -226,7 +226,7 @@ export const tiled: IAssembler = {
},

updateVerts (sprite: Sprite, sizableWidth: number, sizableHeight: number, row: number, col: number) {
const uiTrans = sprite.node._uiProps.uiTransformComp!;
const uiTrans = sprite.node._getUITransformComp()!;
const renderData = sprite.renderData;
if (!renderData) return;
const dataList: IRenderData[] = renderData.data;
Expand Down
4 changes: 2 additions & 2 deletions cocos/2d/components/mask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ export class Mask extends Component {
* @param worldPt @en point in World Space. @zh 世界空间中的点击点。
*/
public isHit (worldPt: Vec2): boolean {
const uiTrans = this.node._uiProps.uiTransformComp!;
const uiTrans = this.node._getUITransformComp()!;
const size = uiTrans.contentSize;
const w = size.width;
const h = size.height;
Expand Down Expand Up @@ -412,7 +412,7 @@ export class Mask extends Component {
return;
}

const uiTrans = this.node._uiProps.uiTransformComp!;
const uiTrans = this.node._getUITransformComp()!;
const graphics = this._graphics;
// Share render data with graphics content
graphics.clear();
Expand Down
26 changes: 13 additions & 13 deletions cocos/2d/components/rich-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ function getSegmentByPool (type: string, content: string | SpriteFrame): ISegmen
seg.comp.underlineHeight = 2;
}
node.setPosition(0, 0, 0);
const trans = node._uiProps.uiTransformComp!;
const trans = node._getUITransformComp()!;
trans.setAnchorPoint(0.5, 0.5);

seg.node = node;
Expand Down Expand Up @@ -758,7 +758,7 @@ export class RichText extends Component {
}
label.styleIndex = styleIndex;
this._applyTextAttribute(label);
const size = label.node._uiProps.uiTransformComp!.contentSize;
const size = label.node._getUITransformComp()!.contentSize;
Vec2.set(out, size.x, size.y);
return out;
}
Expand Down Expand Up @@ -855,7 +855,7 @@ export class RichText extends Component {

labelSegment.styleIndex = styleIndex;
labelSegment.lineCount = this._lineCount;
labelSegment.node._uiProps.uiTransformComp!.setAnchorPoint(0, 0);
labelSegment.node._getUITransformComp()!.setAnchorPoint(0, 0);
labelSegment.node.layer = this.node.layer;
this.node.insertChild(labelSegment.node, this._labelChildrenNum++);
this._applyTextAttribute(labelSegment);
Expand Down Expand Up @@ -901,7 +901,7 @@ this._measureText(styleIndex) as unknown as (s: string) => number,
for (let k = 0; k < fragments.length; ++k) {
const splitString = fragments[k];
labelSegment = this._addLabelSegment(splitString, styleIndex);
const labelSize = labelSegment.node._uiProps.uiTransformComp!.contentSize;
const labelSize = labelSegment.node._getUITransformComp()!.contentSize;
this._lineOffsetX += labelSize.width;
if (fragments.length > 1 && k < fragments.length - 1) {
this._updateLineInfo();
Expand Down Expand Up @@ -984,13 +984,13 @@ this._measureText(styleIndex) as unknown as (s: string) => number,
const sprite = segment.comp;
switch (style.imageAlign) {
case 'top':
segment.node._uiProps.uiTransformComp!.setAnchorPoint(0, 1);
segment.node._getUITransformComp()!.setAnchorPoint(0, 1);
break;
case 'center':
segment.node._uiProps.uiTransformComp!.setAnchorPoint(0, 0.5);
segment.node._getUITransformComp()!.setAnchorPoint(0, 0.5);
break;
default:
segment.node._uiProps.uiTransformComp!.setAnchorPoint(0, 0);
segment.node._getUITransformComp()!.setAnchorPoint(0, 0);
break;
}

Expand Down Expand Up @@ -1033,7 +1033,7 @@ this._measureText(styleIndex) as unknown as (s: string) => number,
this._labelWidth = this._lineOffsetX;
}
}
segment.node._uiProps.uiTransformComp!.setContentSize(spriteWidth, spriteHeight);
segment.node._getUITransformComp()!.setContentSize(spriteWidth, spriteHeight);
segment.lineCount = this._lineCount;

segment.clickHandler = '';
Expand Down Expand Up @@ -1126,7 +1126,7 @@ this._measureText(styleIndex) as unknown as (s: string) => number,
} else {
label = this._addLabelSegment(labelString, i);

this._lineOffsetX += label.node._uiProps.uiTransformComp!.width;
this._lineOffsetX += label.node._getUITransformComp()!.width;
if (this._lineOffsetX > this._labelWidth) {
this._labelWidth = this._lineOffsetX;
}
Expand All @@ -1147,7 +1147,7 @@ this._measureText(styleIndex) as unknown as (s: string) => number,
this._labelHeight = (this._lineCount + BASELINE_RATIO) * this._lineHeight;

// trigger "size-changed" event
this.node._uiProps.uiTransformComp!.setContentSize(this._labelWidth, this._labelHeight);
this.node._getUITransformComp()!.setContentSize(this._labelWidth, this._labelHeight);

this._updateRichTextPosition();
this._layoutDirty = false;
Expand Down Expand Up @@ -1176,7 +1176,7 @@ this._measureText(styleIndex) as unknown as (s: string) => number,
let nextTokenX = 0;
let nextLineIndex = 1;
const totalLineCount = this._lineCount;
const trans = this.node._uiProps.uiTransformComp!;
const trans = this.node._getUITransformComp()!;
const anchorX = trans.anchorX;
const anchorY = trans.anchorY;
for (let i = 0; i < this._segments.length; ++i) {
Expand Down Expand Up @@ -1209,7 +1209,7 @@ this._measureText(styleIndex) as unknown as (s: string) => number,
);

if (lineCount === nextLineIndex) {
nextTokenX += segment.node._uiProps.uiTransformComp!.width;
nextTokenX += segment.node._getUITransformComp()!.width;
}

const sprite = segment.node.getComponent(Sprite);
Expand All @@ -1218,7 +1218,7 @@ this._measureText(styleIndex) as unknown as (s: string) => number,
// adjust img align (from <img align=top|center|bottom>)
const lineHeightSet = this._lineHeight;
const lineHeightReal = this._lineHeight * (1 + BASELINE_RATIO); // single line node height
switch (segment.node._uiProps.uiTransformComp!.anchorY) {
switch (segment.node._getUITransformComp()!.anchorY) {
case 1:
position.y += (lineHeightSet + ((lineHeightReal - lineHeightSet) / 2));
break;
Expand Down
2 changes: 1 addition & 1 deletion cocos/2d/components/sprite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ export class Sprite extends UIRenderer {
}

if (this._spriteFrame) {
const actualSize = this.node._uiProps.uiTransformComp!.contentSize;
const actualSize = this.node._getUITransformComp()!.contentSize;
let expectedW = actualSize.width;
let expectedH = actualSize.height;
if (this._sizeMode === SizeMode.RAW) {
Expand Down
2 changes: 1 addition & 1 deletion cocos/2d/components/ui-mesh-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export class UIMeshRenderer extends Component {
}

public onLoad (): void {
if (!this.node._uiProps.uiTransformComp) {
if (!this.node._getUITransformComp()) {
this.node.addComponent('cc.UITransform');
}

Expand Down
6 changes: 3 additions & 3 deletions cocos/2d/event/pointer-event-dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ class PointerEventDispatcher implements IEventDispatcher {
const pointerEventProcessor = pointerEventProcessorList[i];
const node = pointerEventProcessor.node;
if (node._uiProps) {
const trans = node._uiProps.uiTransformComp;
const trans = node._getUITransformComp();
pointerEventProcessor.cachedCameraPriority = trans!.cameraPriority;
}
}
Expand All @@ -227,9 +227,9 @@ class PointerEventDispatcher implements IEventDispatcher {
private _sortByPriority (p1: NodeEventProcessor, p2: NodeEventProcessor): number {
const node1: Node = p1.node;
const node2: Node = p2.node;
if (!p2 || !node2 || !node2.activeInHierarchy || !node2._uiProps.uiTransformComp) {
if (!p2 || !node2 || !node2.activeInHierarchy || !node2._getUITransformComp()) {
return -1;
} else if (!p1 || !node1 || !node1.activeInHierarchy || !node1._uiProps.uiTransformComp) {
} else if (!p1 || !node1 || !node1.activeInHierarchy || !node1._getUITransformComp()) {
return 1;
}

Expand Down
2 changes: 1 addition & 1 deletion cocos/2d/framework/canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export class Canvas extends RenderRoot2D {
// TODO: support paddings of locked widget
this.node.getPosition(this._pos);
const nodeSize = view.getDesignResolutionSize();
const trans = this.node._uiProps.uiTransformComp!;
const trans = this.node._getUITransformComp()!;

let scaleX = this.node.scale.x;
let anchorX = trans.anchorX;
Expand Down
6 changes: 3 additions & 3 deletions cocos/2d/framework/ui-transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -777,12 +777,12 @@ export class UITransform extends Component {
}
}

private static _sortChildrenSibling (node): void {
private static _sortChildrenSibling (node: Node): void {
const siblings = node.children;
if (siblings) {
siblings.sort((a: Node, b: Node): number => {
const aComp = a._uiProps.uiTransformComp;
const bComp = b._uiProps.uiTransformComp;
const aComp = a._getUITransformComp();
const bComp = b._getUITransformComp();
const ca = aComp ? aComp._priority : 0;
const cb = bComp ? bComp._priority : 0;
const diff = ca - cb;
Expand Down
2 changes: 1 addition & 1 deletion cocos/dragon-bones/ArmatureDisplay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1089,7 +1089,7 @@ export class ArmatureDisplay extends UIRenderer {
if (this._armature) {
const armatureData = this._armature.armatureData;
const aabb = armatureData.aabb;
this.node._uiProps.uiTransformComp!.setContentSize(aabb.width, aabb.height);
this.node._getUITransformComp()!.setContentSize(aabb.width, aabb.height);
}
this.attachUtil.init(this);

Expand Down
4 changes: 2 additions & 2 deletions cocos/dragon-bones/CCArmatureDisplay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export class CCArmatureDisplay extends DisplayData implements IEventDispatcher {
convertToWorldSpace (point: Vec3): Vec3 | undefined {
const newPos = this.convertToRootSpace(point);
const ccNode = this.getRootNode();
return ccNode?._uiProps.uiTransformComp?.convertToWorldSpaceAR(newPos);
return ccNode?._getUITransformComp()?.convertToWorldSpaceAR(newPos);
}
/**
* @en Get the node of root ArmatureDisplay component in.
Expand Down Expand Up @@ -185,7 +185,7 @@ export class CCArmatureDisplay extends DisplayData implements IEventDispatcher {
this.shouldAdvanced = !!on;
}

hasDBEventListener (type): boolean {
hasDBEventListener (type: string): boolean {
return this._eventTarget.hasEventListener(type);
}

Expand Down
Loading

0 comments on commit 67a0736

Please sign in to comment.