Skip to content

Commit

Permalink
optimize: support Touch.clone() method and remove clone param
Browse files Browse the repository at this point in the history
  • Loading branch information
PPpro committed Sep 11, 2023
1 parent 8a16e74 commit a2a7258
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
8 changes: 4 additions & 4 deletions cocos/input/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,9 @@ export class Input {
* @param clone - Whether to clone touch object
* @returns
*/
public getTouch (touchID: number, clone?: boolean): Touch | undefined {
public getTouch (touchID: number): Touch | undefined {
const touch = touchManager._touchMap.get(touchID);
return touch ? (clone === false ? touch : touchManager._cloneTouch(touch)) : undefined;
return touch;
}

/**
Expand All @@ -263,8 +263,8 @@ export class Input {
* 获取当前 所有touch对象 的数组。
* @param clone - Whether to clone touch object
*/
public getAllTouches (clone?: boolean): Touch[] {
return touchManager.getAllTouches(clone);
public getAllTouches (): Touch[] {
return touchManager.getAllTouches();
}

/**
Expand Down
19 changes: 17 additions & 2 deletions cocos/input/types/touch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ export class Touch {

_vec2.set(this._point);
_vec2.subtract(this._prevPoint);
out.set(cclegacy.view.getScaleX(), cclegacy.view.getScaleY());
out.set(cclegacy.view.getScaleX() as number, cclegacy.view.getScaleY() as number);
Vec2.divide(out, _vec2, out);
return out;
}
Expand Down Expand Up @@ -261,7 +261,7 @@ export class Touch {
* @param x - x position of the touch point
* @param y - y position of the touch point
*/
public setTouchInfo (id = 0, x?: number, y?: number): void {
public setTouchInfo (id: number = 0, x: number = 0, y: number = 0): void {
this._prevPoint = this._point;
this._point = new Vec2(x || 0, y || 0);
this._id = id;
Expand Down Expand Up @@ -321,6 +321,21 @@ export class Touch {
}
this._lastModified = cclegacy.game.frameStartTime;
}

/**
* @zh Touch 对象的原始数据不应该被修改。如果你需要这么做,最好克隆一个新的对象。
* @en The original Touch object shouldn't be modified. If you need to, it's better to clone a new one.
*/
public clone (): Touch {
const touchID = this.getID();
this.getStartLocation(_vec2);
const clonedTouch = new Touch(_vec2.x, _vec2.y, touchID);
this.getLocation(_vec2);
clonedTouch.setPoint(_vec2.x, _vec2.y);
this.getPreviousLocation(_vec2);
clonedTouch.setPrevPoint(_vec2);
return clonedTouch;
}
}

cclegacy.Touch = Touch;
11 changes: 6 additions & 5 deletions pal/input/touch-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const tempVec2 = new Vec2();
class TouchManager {
/**
* A map from touch ID to touch object.
* @engineInternal
*/
public _touchMap: Map<number, Touch>;
private readonly _maxTouches = 8;
Expand All @@ -45,7 +46,7 @@ class TouchManager {
* @param touch
* @returns
*/
public _cloneTouch (touch: Touch): Touch {
private _cloneTouch (touch: Touch): Touch {
const touchID = touch.getID();
touch.getStartLocation(tempVec2);
const clonedTouch = new Touch(tempVec2.x, tempVec2.y, touchID);
Expand Down Expand Up @@ -100,25 +101,25 @@ class TouchManager {
* @param touchID
* @returns
*/
public getTouch (touchID: number, x: number, y: number, clone?: boolean): Touch | undefined {
public getTouch (touchID: number, x: number, y: number): Touch | undefined {
let touch = this._touchMap.get(touchID);
if (!touch) {
touch = this._createTouch(touchID, x, y);
} else {
this._updateTouch(touch, x, y);
}
return touch ? (clone === false ? touch : this._cloneTouch(touch)) : undefined;
return touch;
}

/**
* Get all the current touches objects.
* @returns
*/
public getAllTouches (clone?: boolean): Touch[] {
public getAllTouches (): Touch[] {
const touches: Touch[] = [];
this._touchMap.forEach((touch) => {
if (touch) {
touches.push(clone === false ? touch : this._cloneTouch(touch));
touches.push(touch);
}
});
return touches;
Expand Down

0 comments on commit a2a7258

Please sign in to comment.