Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

do not new object as possible #17809

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions cocos/ui/scroll-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,7 @@ export class ScrollView extends ViewGroup {
this._setContentPosition(position);
}

// Should not use _tempVec3 and also can not invoke any functions that use _tempVec3 as invoker may pass _tempVec3.
private _setContentPosition (position: Readonly<Vec3>): void {
if (!this._content) {
return;
Expand Down Expand Up @@ -1079,9 +1080,10 @@ export class ScrollView extends ViewGroup {
return;
}

const deltaMove = new Vec3();
const wheelPrecision = -0.1;
const scrollY = event.getScrollY();

const deltaMove = _tempVec3;
if (self.vertical) {
deltaMove.set(0, scrollY * wheelPrecision, 0);
} else if (self.horizontal) {
Expand Down Expand Up @@ -1242,9 +1244,11 @@ export class ScrollView extends ViewGroup {
}

protected _startInertiaScroll (touchMoveVelocity: Vec3): void {
const inertiaTotalMovement = new Vec3(touchMoveVelocity);
inertiaTotalMovement.multiplyScalar(MOVEMENT_FACTOR);
this._startAttenuatingAutoScroll(inertiaTotalMovement, touchMoveVelocity);
_tempVec3.set(touchMoveVelocity);
_tempVec3.multiplyScalar(MOVEMENT_FACTOR);

// this._startAttenuatingAutoScroll will clone _tempVec3, so can pass _tempVec3.
this._startAttenuatingAutoScroll(_tempVec3, touchMoveVelocity);
}

protected _calculateAttenuatedFactor (distance: number): number {
Expand Down Expand Up @@ -1332,7 +1336,8 @@ export class ScrollView extends ViewGroup {
if (totalTime <= 0 || totalTime >= 0.5) {
out.set(Vec3.ZERO);
} else {
let totalMovement = new Vec3();
let totalMovement = _tempVec3;
totalMovement.set(0, 0, 0);
totalMovement = this._touchMoveDisplacements.reduce((a, b) => {
a.add(b);
return a;
Expand All @@ -1358,6 +1363,9 @@ export class ScrollView extends ViewGroup {
_tempVec3.set(this._getContentPosition());
_tempVec3.add(adjustedMove);
_tempVec3.set(Math.round(_tempVec3.x * TOLERANCE) * EPSILON, Math.round(_tempVec3.y * TOLERANCE) * EPSILON, _tempVec3.z);

// Important: Pass a global variable _tempVec3 to other class member function is dangerous. As `this._setContentPosition`
// doesn't use _tempVec3 and it doesn't invoke any functions that use _tempVec3. So it is safe to pass _tempVec3 here.
this._setContentPosition(_tempVec3);
const outOfBoundary = this._getHowMuchOutOfBoundary();
_tempVec2.set(outOfBoundary.x, outOfBoundary.y);
Expand Down Expand Up @@ -1403,7 +1411,9 @@ export class ScrollView extends ViewGroup {
}

protected _getHowMuchOutOfBoundary (addition?: Vec3): Vec3 {
addition = addition || new Vec3();
if (!addition) {
addition = Vec3.ZERO;
}
if (addition.equals(Vec3.ZERO, EPSILON) && !this._outOfBoundaryAmountDirty) {
return this._outOfBoundaryAmount;
}
Expand Down Expand Up @@ -1494,6 +1504,9 @@ export class ScrollView extends ViewGroup {
if (_isOutOfBoundary) {
_tempVec3.set(this._getContentPosition());
_tempVec3.add(outOfBoundary);

// Important: Pass a global variable _tempVec3 to other class member function is dangerous. As `this._setContentPosition`
// doesn't use _tempVec3 and it doesn't invoke any functions that use _tempVec3. So it is safe to pass _tempVec3 here.
this._setContentPosition(_tempVec3);
this._updateScrollBar(Vec2.ZERO);
}
Expand Down Expand Up @@ -1750,7 +1763,7 @@ export class ScrollView extends ViewGroup {
const bounceBackStarted = this._startBounceBackIfNeeded();
if (!bounceBackStarted && this.inertia) {
const touchMoveVelocity = this._calculateTouchMoveVelocity();
if (!touchMoveVelocity.equals(_tempVec3, EPSILON) && this.brake < 1) {
if (!touchMoveVelocity.equals(Vec3.ZERO, EPSILON) && this.brake < 1) {
this._startInertiaScroll(touchMoveVelocity);
}
}
Expand Down Expand Up @@ -1999,9 +2012,10 @@ export class ScrollView extends ViewGroup {
return;
}

const deltaMove = new Vec3();
const wheelPrecision = -62.5;
const scrollY = event.y;

const deltaMove = _tempVec3;
if (self.vertical) {
deltaMove.set(0, scrollY * wheelPrecision, 0);
} else if (self.horizontal) {
Expand Down
Loading