Skip to content

Commit

Permalink
[v3.8.6] Refactor some code to reduce package size.
Browse files Browse the repository at this point in the history
  • Loading branch information
dumganhar committed Dec 20, 2024
1 parent ca29ce0 commit 0c65b99
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 72 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,5 @@ rules:
'@typescript-eslint/explicit-function-return-type': [error, {
allowIIFEs: true, # IIFEs are widely used, writing their signature twice is painful
}]

'@typescript-eslint/no-this-alias': off
74 changes: 40 additions & 34 deletions cocos/2d/components/sprite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -596,44 +596,49 @@ export class Sprite extends UIRenderer {
}

protected _flushAssembler (): void {
const assembler = Sprite.Assembler.getAssembler(this);
const self = this;
const assembler = Sprite.Assembler.getAssembler(self);

if (this._assembler !== assembler) {
this.destroyRenderData();
this._assembler = assembler;
if (self._assembler !== assembler) {
self.destroyRenderData();
self._assembler = assembler;
}

if (!this._renderData) {
if (this._assembler && this._assembler.createData) {
this._renderData = this._assembler.createData(this);
this._renderData!.material = this.getRenderMaterial(0);
this.markForUpdateRenderData();
if (this.spriteFrame) {
this._assembler.updateUVs(this);
if (!self._renderData) {
if (assembler && assembler.createData) {
const rd = self._renderData = assembler.createData(self);
rd.material = self.getRenderMaterial(0);
self.markForUpdateRenderData();
if (self.spriteFrame) {
assembler.updateUVs(self);
}
this._updateColor();
self._updateColor();
}
}

// Only Sliced type need update uv when sprite frame insets changed
if (this._spriteFrame) {
if (this._type === SpriteType.SLICED) {
this._spriteFrame.on(SpriteFrameEvent.UV_UPDATED, this._updateUVs, this);
const spriteFrame = self._spriteFrame;
if (spriteFrame) {
if (self._type === SpriteType.SLICED) {
spriteFrame.on(SpriteFrameEvent.UV_UPDATED, self._updateUVs, self);
} else {
this._spriteFrame.off(SpriteFrameEvent.UV_UPDATED, this._updateUVs, this);
spriteFrame.off(SpriteFrameEvent.UV_UPDATED, self._updateUVs, self);
}
}
}

private _applySpriteSize (): void {
if (this._spriteFrame) {
if (BUILD || !this._spriteFrame.isDefault) {
if (SizeMode.RAW === this._sizeMode) {
const size = this._spriteFrame.originalSize;
this.node._uiProps.uiTransformComp!.setContentSize(size);
} else if (SizeMode.TRIMMED === this._sizeMode) {
const rect = this._spriteFrame.rect;
this.node._uiProps.uiTransformComp!.setContentSize(rect.width, rect.height);
const self = this;
const spriteFrame = self._spriteFrame;
if (spriteFrame) {
if (BUILD || !spriteFrame.isDefault) {
const uiProps = self.node._uiProps;
if (SizeMode.RAW === self._sizeMode) {
const size = spriteFrame.originalSize;
uiProps.uiTransformComp!.setContentSize(size);
} else if (SizeMode.TRIMMED === self._sizeMode) {
const rect = spriteFrame.rect;
uiProps.uiTransformComp!.setContentSize(rect.width, rect.height);
}
}
}
Expand Down Expand Up @@ -685,10 +690,11 @@ export class Sprite extends UIRenderer {
}

private _applySpriteFrame (oldFrame: SpriteFrame | null): void {
const spriteFrame = this._spriteFrame;
const self = this;
const spriteFrame = self._spriteFrame;

if (oldFrame && this._type === SpriteType.SLICED) {
oldFrame.off(SpriteFrameEvent.UV_UPDATED, this._updateUVs, this);
if (oldFrame && self._type === SpriteType.SLICED) {
oldFrame.off(SpriteFrameEvent.UV_UPDATED, self._updateUVs, self);
}

let textureChanged = false;
Expand All @@ -697,23 +703,23 @@ export class Sprite extends UIRenderer {
textureChanged = true;
}
if (textureChanged) {
if (this.renderData) this.renderData.textureDirty = true;
if (self.renderData) self.renderData.textureDirty = true;
// texture type changed, set this._instanceMaterialType to default value
const oldIsRT = oldFrame ? oldFrame.texture instanceof RenderTexture : false;
const newIsRT = spriteFrame.texture instanceof RenderTexture;
if (oldIsRT !== newIsRT) {
this._instanceMaterialType = -1;
self._instanceMaterialType = -1;
}
this.changeMaterialForDefine();
self.changeMaterialForDefine();
}
this._applySpriteSize();
if (this._type === SpriteType.SLICED) {
spriteFrame.on(SpriteFrameEvent.UV_UPDATED, this._updateUVs, this);
self._applySpriteSize();
if (self._type === SpriteType.SLICED) {
spriteFrame.on(SpriteFrameEvent.UV_UPDATED, self._updateUVs, self);
}
}

if (EDITOR) {
this._applyAtlas(spriteFrame);
self._applyAtlas(spriteFrame);
}
}

Expand Down
4 changes: 2 additions & 2 deletions cocos/2d/event/pointer-event-dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,8 @@ class PointerEventDispatcher implements IEventDispatcher {
}
}

const priority1 = n1 ? n1.getSiblingIndex() : 0;
const priority2 = n2 ? n2.getSiblingIndex() : 0;
const priority1 = n1 ? n1.siblingIndex : 0;
const priority2 = n2 ? n2.siblingIndex : 0;

return ex ? priority1 - priority2 : priority2 - priority1;
}
Expand Down
30 changes: 18 additions & 12 deletions cocos/scene-graph/node-event-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -497,13 +497,14 @@ export class NodeEventProcessor {

private _handleMouseDown (event: EventMouse): boolean {
const node = this._node;
if (!node || !node._uiProps.uiTransformComp) {
const uiTransformComp = node._uiProps.uiTransformComp;
if (!node || !uiTransformComp) {
return false;
}

event.getLocation(pos);

if (node._uiProps.uiTransformComp.hitTest(pos, event.windowId)) {
if (uiTransformComp.hitTest(pos, event.windowId)) {
event.type = NodeEventType.MOUSE_DOWN;
event.bubbles = true;
node.dispatchEvent(event);
Expand All @@ -515,13 +516,14 @@ export class NodeEventProcessor {

private _handleMouseMove (event: EventMouse): boolean {
const node = this._node;
if (!node || !node._uiProps.uiTransformComp || this._isMouseLeaveWindow) {
const uiTransformComp = node._uiProps.uiTransformComp;
if (!node || !uiTransformComp || this._isMouseLeaveWindow) {
return false;
}

event.getLocation(pos);

const hit = node._uiProps.uiTransformComp.hitTest(pos, event.windowId);
const hit = uiTransformComp.hitTest(pos, event.windowId);
if (hit) {
if (!this.previousMouseIn) {
// Fix issue when hover node switched, previous hovered node won't get MOUSE_LEAVE notification
Expand Down Expand Up @@ -551,13 +553,14 @@ export class NodeEventProcessor {

private _handleMouseUp (event: EventMouse): boolean {
const node = this._node;
if (!node || !node._uiProps.uiTransformComp) {
const uiTransformComp = node._uiProps.uiTransformComp;
if (!node || !uiTransformComp) {
return false;
}

event.getLocation(pos);

if (node._uiProps.uiTransformComp.hitTest(pos, event.windowId)) {
if (uiTransformComp.hitTest(pos, event.windowId)) {
event.type = NodeEventType.MOUSE_UP;
event.bubbles = true;
node.dispatchEvent(event);
Expand All @@ -569,13 +572,14 @@ export class NodeEventProcessor {

private _handleMouseWheel (event: EventMouse): boolean {
const node = this._node;
if (!node || !node._uiProps.uiTransformComp) {
const uiTransformComp = node._uiProps.uiTransformComp;
if (!node || !uiTransformComp) {
return false;
}

event.getLocation(pos);

if (node._uiProps.uiTransformComp.hitTest(pos, event.windowId)) {
if (uiTransformComp.hitTest(pos, event.windowId)) {
event.type = NodeEventType.MOUSE_WHEEL;
event.bubbles = true;
node.dispatchEvent(event);
Expand Down Expand Up @@ -631,13 +635,14 @@ export class NodeEventProcessor {

private _handleTouchStart (event: EventTouch): boolean {
const node = this.node;
if (!node || !node._uiProps.uiTransformComp) {
const uiTransformComp = node._uiProps.uiTransformComp;
if (!node || !uiTransformComp) {
return false;
}

event.getLocation(pos);

if (node._uiProps.uiTransformComp.hitTest(pos, event.windowId)) {
if (uiTransformComp.hitTest(pos, event.windowId)) {
event.type = NodeEventType.TOUCH_START;
event.bubbles = true;
this._dispatchingTouch = event.touch;
Expand All @@ -663,13 +668,14 @@ export class NodeEventProcessor {

private _handleTouchEnd (event: EventTouch): void {
const node = this.node;
if (!node || !node._uiProps.uiTransformComp) {
const uiTransformComp = node._uiProps.uiTransformComp;
if (!node || !uiTransformComp) {
return;
}

event.getLocation(pos);

if (node._uiProps.uiTransformComp.hitTest(pos, event.windowId)) {
if (uiTransformComp.hitTest(pos, event.windowId)) {
event.type = NodeEventType.TOUCH_END;
} else {
event.type = NodeEventType.TOUCH_CANCEL;
Expand Down
30 changes: 17 additions & 13 deletions cocos/scene-graph/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1763,17 +1763,19 @@ export class Node extends CCObject implements ISchedulable, CustomSerializable {
*/
@editable
set layer (l: number) {
if (this._layer === l) {
const self = this;
if (self._layer === l) {
return;
}

this._layer = l;
self._layer = l;

if (this._uiProps && this._uiProps.uiComp) {
this._uiProps.uiComp.setNodeDirty();
this._uiProps.uiComp.markForUpdateRenderData();
const uiComp = self._uiProps && self._uiProps.uiComp;
if (uiComp) {
uiComp.setNodeDirty();
uiComp.markForUpdateRenderData();
}
this.emit(NodeEventType.LAYER_CHANGED, this._layer);
self.emit(NodeEventType.LAYER_CHANGED, self._layer);
}

get layer (): number {
Expand Down Expand Up @@ -1933,8 +1935,9 @@ export class Node extends CCObject implements ISchedulable, CustomSerializable {
* @deprecated since v3.5.0, this is an engine private interface that will be removed in the future.
*/
public _onPostActivated (active: boolean): void {
if (this._eventMask & ACTIVE_ON) {
this.emit(NodeEventType.ACTIVE_CHANGED, this, active);
const self = this;
if (self._eventMask & ACTIVE_ON) {
self.emit(NodeEventType.ACTIVE_CHANGED, self, active);
}

const eventProcessor = this._eventProcessor;
Expand All @@ -1957,12 +1960,13 @@ export class Node extends CCObject implements ISchedulable, CustomSerializable {

if (active) { // activated
// in case transform updated during deactivated period
this.invalidateChildren(TransformBit.TRS);
self.invalidateChildren(TransformBit.TRS);
// ALL Node renderData dirty flag will set on here
if (this._uiProps && this._uiProps.uiComp) {
this._uiProps.uiComp.setNodeDirty();
this._uiProps.uiComp.setTextureDirty(); // for dynamic atlas
this._uiProps.uiComp.markForUpdateRenderData();
const uiComp = self._uiProps && self._uiProps.uiComp;
if (uiComp) {
uiComp.setNodeDirty();
uiComp.setTextureDirty(); // for dynamic atlas
uiComp.markForUpdateRenderData();
}
}
}
Expand Down
26 changes: 15 additions & 11 deletions cocos/spine/skeleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1655,19 +1655,23 @@ export class Skeleton extends UIRenderer {
* @engineInternal
*/
public _updateColor (): void {
const a = this.node._uiProps.opacity;
// eslint-disable-next-line max-len
if (this._tempColor.r === this._color.r && this._tempColor.g === this._color.g && this._tempColor.b === this._color.b && this._tempColor.a === a) {
const self = this;
const uiProps = self.node._uiProps;
const a = uiProps.opacity;
const tempColor = self._tempColor;
const color = self._color;

if (tempColor.r === color.r && tempColor.g === color.g && tempColor.b === color.b && tempColor.a === a) {
return;
}
this.node._uiProps.colorDirty = true;
this._tempColor.r = this._color.r;
this._tempColor.g = this._color.g;
this._tempColor.b = this._color.b;
this._tempColor.a = a;
const r = this._color.r / 255.0;
const g = this._color.g / 255.0;
const b = this._color.b / 255.0;
uiProps.colorDirty = true;
tempColor.r = color.r;
tempColor.g = color.g;
tempColor.b = color.b;
tempColor.a = a;
const r = color.r / 255.0;
const g = color.g / 255.0;
const b = color.b / 255.0;
this._instance!.setColor(r, g, b, a);
}

Expand Down

0 comments on commit 0c65b99

Please sign in to comment.