Skip to content

Commit

Permalink
[v3.8.6] Optimize code size for input.ts, tiled-layer.ts, minigame pal (
Browse files Browse the repository at this point in the history
#18159)

* [v3.8.6] Optimize code size for input.ts and tiled-layer.ts
  • Loading branch information
dumganhar authored Jan 13, 2025
1 parent aa06696 commit e5aac8f
Show file tree
Hide file tree
Showing 5 changed files with 257 additions and 240 deletions.
110 changes: 58 additions & 52 deletions cocos/input/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
*/

import { EDITOR_NOT_IN_PREVIEW, NATIVE } from 'internal:constants';
import { AccelerometerInputSource, GamepadInputDevice, HMDInputDevice, HandheldInputDevice, HandleInputDevice, KeyboardInputSource, MouseInputSource, TouchInputSource } from 'pal/input';
import { AccelerometerInputSource, GamepadInputDevice, HMDInputDevice, HandheldInputDevice,
HandleInputDevice, KeyboardInputSource, MouseInputSource, TouchInputSource } from 'pal/input';
import { touchManager } from '../../pal/input/touch-manager';
import { EventTarget, error, sys } from '../core';
import { Event, EventAcceleration, EventGamepad, EventHandle, EventHandheld, EventHMD, EventKeyboard, EventMouse, EventTouch, Touch } from './types';
Expand Down Expand Up @@ -350,103 +351,108 @@ export class Input {
}

private _registerEvent (): void {
const self = this;
const touchInput = self._touchInput;
const mouseInput = self._mouseInput;
const keyboardInput = self._keyboardInput;
const handleInput = self._handleInput;
if (sys.hasFeature(sys.Feature.INPUT_TOUCH)) {
this._touchInput.on(InputEventType.TOUCH_START, (event): void => {
this._dispatchEventTouch(event);
touchInput.on(InputEventType.TOUCH_START, (event): void => {
self._dispatchEventTouch(event);
});
this._touchInput.on(InputEventType.TOUCH_MOVE, (event): void => {
this._dispatchEventTouch(event);
touchInput.on(InputEventType.TOUCH_MOVE, (event): void => {
self._dispatchEventTouch(event);
});
this._touchInput.on(InputEventType.TOUCH_END, (event): void => {
this._dispatchEventTouch(event);
touchInput.on(InputEventType.TOUCH_END, (event): void => {
self._dispatchEventTouch(event);
});
this._touchInput.on(InputEventType.TOUCH_CANCEL, (event): void => {
this._dispatchEventTouch(event);
touchInput.on(InputEventType.TOUCH_CANCEL, (event): void => {
self._dispatchEventTouch(event);
});
}

if (sys.hasFeature(sys.Feature.EVENT_MOUSE)) {
this._mouseInput.on(InputEventType.MOUSE_DOWN, (event): void => {
this._needSimulateTouchMoveEvent = true;
this._simulateEventTouch(event);
this._dispatchEventMouse(event);
mouseInput.on(InputEventType.MOUSE_DOWN, (event): void => {
self._needSimulateTouchMoveEvent = true;
self._simulateEventTouch(event);
self._dispatchEventMouse(event);
});
this._mouseInput.on(InputEventType.MOUSE_MOVE, (event): void => {
if (this._needSimulateTouchMoveEvent) {
this._simulateEventTouch(event);
mouseInput.on(InputEventType.MOUSE_MOVE, (event): void => {
if (self._needSimulateTouchMoveEvent) {
self._simulateEventTouch(event);
}
this._dispatchEventMouse(event);
self._dispatchEventMouse(event);
});
this._mouseInput.on(InputEventType.MOUSE_UP, (event): void => {
this._needSimulateTouchMoveEvent = false;
this._simulateEventTouch(event);
this._dispatchEventMouse(event);
mouseInput.on(InputEventType.MOUSE_UP, (event): void => {
self._needSimulateTouchMoveEvent = false;
self._simulateEventTouch(event);
self._dispatchEventMouse(event);
});
this._mouseInput.on(InputEventType.MOUSE_WHEEL, (event): void => {
this._dispatchEventMouse(event);
mouseInput.on(InputEventType.MOUSE_WHEEL, (event): void => {
self._dispatchEventMouse(event);
});
this._mouseInput.on(InputEventType.MOUSE_LEAVE, (event): void => {
this._dispatchEventMouse(event);
mouseInput.on(InputEventType.MOUSE_LEAVE, (event): void => {
self._dispatchEventMouse(event);
});
this._mouseInput.on(InputEventType.MOUSE_ENTER, (event): void => {
this._dispatchEventMouse(event);
mouseInput.on(InputEventType.MOUSE_ENTER, (event): void => {
self._dispatchEventMouse(event);
});
}

if (sys.hasFeature(sys.Feature.EVENT_KEYBOARD)) {
const eventKeyboardList = this._eventKeyboardList;
this._keyboardInput.on(InputEventType.KEY_DOWN, (event): void => {
this._dispatchOrPushEvent(event, eventKeyboardList);
const eventKeyboardList = self._eventKeyboardList;
keyboardInput.on(InputEventType.KEY_DOWN, (event): void => {
self._dispatchOrPushEvent(event, eventKeyboardList);
});
this._keyboardInput.on(InputEventType.KEY_PRESSING, (event): void => {
this._dispatchOrPushEvent(event, eventKeyboardList);
keyboardInput.on(InputEventType.KEY_PRESSING, (event): void => {
self._dispatchOrPushEvent(event, eventKeyboardList);
});
this._keyboardInput.on(InputEventType.KEY_UP, (event): void => {
this._dispatchOrPushEvent(event, eventKeyboardList);
keyboardInput.on(InputEventType.KEY_UP, (event): void => {
self._dispatchOrPushEvent(event, eventKeyboardList);
});
}

if (sys.hasFeature(sys.Feature.EVENT_ACCELEROMETER)) {
const eventAccelerationList = this._eventAccelerationList;
this._accelerometerInput.on(InputEventType.DEVICEMOTION, (event): void => {
this._dispatchOrPushEvent(event, eventAccelerationList);
const eventAccelerationList = self._eventAccelerationList;
self._accelerometerInput.on(InputEventType.DEVICEMOTION, (event): void => {
self._dispatchOrPushEvent(event, eventAccelerationList);
});
}

if (sys.hasFeature(sys.Feature.EVENT_GAMEPAD)) {
const eventGamepadList = this._eventGamepadList;
const eventGamepadList = self._eventGamepadList;
GamepadInputDevice._on(InputEventType.GAMEPAD_CHANGE, (event): void => {
this._dispatchOrPushEvent(event, eventGamepadList);
self._dispatchOrPushEvent(event, eventGamepadList);
});
GamepadInputDevice._on(InputEventType.GAMEPAD_INPUT, (event): void => {
this._dispatchOrPushEvent(event, eventGamepadList);
self._dispatchOrPushEvent(event, eventGamepadList);
});
GamepadInputDevice._on(InputEventType.HANDLE_POSE_INPUT, (event): void => {
this._dispatchOrPushEvent(event, eventGamepadList);
self._dispatchOrPushEvent(event, eventGamepadList);
});
}

if (sys.hasFeature(sys.Feature.EVENT_HANDLE)) {
const eventHandleList = this._eventHandleList;
this._handleInput._on(InputEventType.HANDLE_INPUT, (event): void => {
this._dispatchOrPushEvent(event, eventHandleList);
const eventHandleList = self._eventHandleList;
handleInput._on(InputEventType.HANDLE_INPUT, (event): void => {
self._dispatchOrPushEvent(event, eventHandleList);
});
this._handleInput._on(InputEventType.HANDLE_POSE_INPUT, (event): void => {
this._dispatchOrPushEvent(event, eventHandleList);
handleInput._on(InputEventType.HANDLE_POSE_INPUT, (event): void => {
self._dispatchOrPushEvent(event, eventHandleList);
});
}

if (sys.hasFeature(sys.Feature.EVENT_HMD)) {
const eventHMDList = this._eventHMDList;
this._hmdInput._on(InputEventType.HMD_POSE_INPUT, (event): void => {
this._dispatchOrPushEvent(event, eventHMDList);
const eventHMDList = self._eventHMDList;
self._hmdInput._on(InputEventType.HMD_POSE_INPUT, (event): void => {
self._dispatchOrPushEvent(event, eventHMDList);
});
}

if (sys.hasFeature(sys.Feature.EVENT_HANDHELD)) {
const eventHandheldList = this._eventHandheldList;
this._handheldInput._on(InputEventType.HANDHELD_POSE_INPUT, (event): void => {
this._dispatchOrPushEvent(event, eventHandheldList);
const eventHandheldList = self._eventHandheldList;
self._handheldInput._on(InputEventType.HANDHELD_POSE_INPUT, (event): void => {
self._dispatchOrPushEvent(event, eventHandheldList);
});
}
}
Expand Down
96 changes: 52 additions & 44 deletions cocos/tiledmap/tiled-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1341,81 +1341,89 @@ export class TiledLayer extends UIRenderer {
this._prepareToRender();
}

public init (layerInfo: TMXLayerInfo, mapInfo: TMXMapInfo, tilesets: TMXTilesetInfo[], textures: SpriteFrame[], texGrids: TiledTextureGrids): void {
this._cullingDirty = true;
this._layerInfo = layerInfo;
this._mapInfo = mapInfo;
public init (
layerInfo: TMXLayerInfo,
mapInfo: TMXMapInfo,
tilesets: TMXTilesetInfo[],
textures: SpriteFrame[],
texGrids: TiledTextureGrids,
): void {
const self = this;
self._cullingDirty = true;
self._layerInfo = layerInfo;
self._mapInfo = mapInfo;

const size = layerInfo.layerSize!;

// layerInfo
this._layerName = layerInfo.name;
this.tiles = layerInfo.tiles as unknown as any;
this._properties = layerInfo.properties;
this._layerSize = size;
this._minGID = layerInfo.minGID;
this._maxGID = layerInfo.maxGID;
this._opacity = layerInfo.opacity;
self._layerName = layerInfo.name;
self.tiles = layerInfo.tiles as unknown as any;
self._properties = layerInfo.properties;
self._layerSize = size;
self._minGID = layerInfo.minGID;
self._maxGID = layerInfo.maxGID;
self._opacity = layerInfo.opacity;

if (layerInfo.tintColor) {
this._tintColor = layerInfo.tintColor;
self._tintColor = layerInfo.tintColor;
// this.node.color = this._tintColor;
}

this.renderOrder = mapInfo.renderOrder;
this._staggerAxis = mapInfo.getStaggerAxis()!;
this._staggerIndex = mapInfo.getStaggerIndex()!;
this._hexSideLength = mapInfo.getHexSideLength();
this._animations = mapInfo.getTileAnimations();
self.renderOrder = mapInfo.renderOrder;
self._staggerAxis = mapInfo.getStaggerAxis()!;
self._staggerIndex = mapInfo.getStaggerIndex()!;
self._hexSideLength = mapInfo.getHexSideLength();
self._animations = mapInfo.getTileAnimations();

// tilesets
this._tilesets = tilesets;
self._tilesets = tilesets;
// textures
this._textures = textures;
self._textures = textures;
// grid texture
this.texGrids = texGrids;
self.texGrids = texGrids;

// mapInfo
this._layerOrientation = mapInfo.orientation;
this._mapTileSize = mapInfo.getTileSize();
self._layerOrientation = mapInfo.orientation;
self._mapTileSize = mapInfo.getTileSize();

const maptw = this._mapTileSize.width;
const mapth = this._mapTileSize.height;
const layerW = this._layerSize.width;
const layerH = this._layerSize.height;
const maptw = self._mapTileSize.width;
const mapth = self._mapTileSize.height;
const layerW = self._layerSize.width;
const layerH = self._layerSize.height;
const uiTransformComp = self.node._getUITransformComp()!;

if (this._layerOrientation === Orientation.HEX) {
if (self._layerOrientation === Orientation.HEX) {
let width = 0;
let height = 0;
const tileWidth = maptw & ~1;
const tileHeight = mapth & ~1;

this._odd_even = (this._staggerIndex === StaggerIndex.STAGGERINDEX_ODD) ? 1 : -1;
if (this._staggerAxis === StaggerAxis.STAGGERAXIS_X) {
this._diffX1 = (tileWidth - this._hexSideLength) / 2;
this._diffY1 = 0;
width = (this._diffX1 + this._hexSideLength) * layerW + this._diffX1;
self._odd_even = (self._staggerIndex === StaggerIndex.STAGGERINDEX_ODD) ? 1 : -1;
if (self._staggerAxis === StaggerAxis.STAGGERAXIS_X) {
self._diffX1 = (tileWidth - self._hexSideLength) / 2;
self._diffY1 = 0;
width = (self._diffX1 + self._hexSideLength) * layerW + self._diffX1;
height = (tileHeight * layerH) + tileHeight / 2;
} else {
this._diffX1 = 0;
this._diffY1 = (tileHeight - this._hexSideLength) / 2;
self._diffX1 = 0;
self._diffY1 = (tileHeight - self._hexSideLength) / 2;
width = (tileWidth * layerW) + tileWidth / 2;
height = (this._diffY1 + this._hexSideLength) * layerH + this._diffY1;
height = (self._diffY1 + self._hexSideLength) * layerH + self._diffY1;
}
this.node._getUITransformComp()!.setContentSize(width, height);
} else if (this._layerOrientation === Orientation.ISO) {
uiTransformComp.setContentSize(width, height);
} else if (self._layerOrientation === Orientation.ISO) {
const wh = layerW + layerH;
this.node._getUITransformComp()!.setContentSize(maptw * 0.5 * wh, mapth * 0.5 * wh);
uiTransformComp.setContentSize(maptw * 0.5 * wh, mapth * 0.5 * wh);
} else {
this.node._getUITransformComp()!.setContentSize(layerW * maptw, layerH * mapth);
uiTransformComp.setContentSize(layerW * maptw, layerH * mapth);
}

// offset (after layer orientation is set);
this._offset = new Vec2(layerInfo.offset.x, -layerInfo.offset.y);
this._useAutomaticVertexZ = false;
this._vertexZvalue = 0;
this._syncAnchorPoint();
this._prepareToRender();
self._offset = new Vec2(layerInfo.offset.x, -layerInfo.offset.y);
self._useAutomaticVertexZ = false;
self._vertexZvalue = 0;
self._syncAnchorPoint();
self._prepareToRender();
}

protected _prepareToRender (): void {
Expand Down
Loading

0 comments on commit e5aac8f

Please sign in to comment.