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

reuse _tempVec2 and avoid creating new temple object #17782

Merged
merged 3 commits into from
Oct 30, 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
105 changes: 50 additions & 55 deletions cocos/ui/scroll-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ const eventMap = {
'scroll-began': 12,
};

const _moveDeltaOptions = {
anchor: v2(),
applyToHorizontal: false,
applyToVertical: false,
};

/**
* @en
* Enum for ScrollView event type.
Expand Down Expand Up @@ -486,11 +492,10 @@ export class ScrollView extends ViewGroup {
* ```
*/
public scrollToBottom (timeInSecond?: number, attenuated = true): void {
const moveDelta = this._calculateMovePercentDelta({
anchor: new Vec2(0, 0),
applyToHorizontal: false,
applyToVertical: true,
});
_moveDeltaOptions.anchor.set(0, 0);
_moveDeltaOptions.applyToHorizontal = false;
_moveDeltaOptions.applyToVertical = true;
const moveDelta = this._calculateMovePercentDelta(_moveDeltaOptions);

if (timeInSecond) {
this._startAutoScroll(moveDelta, timeInSecond, attenuated !== false);
Expand All @@ -516,11 +521,10 @@ export class ScrollView extends ViewGroup {
* ```
*/
public scrollToTop (timeInSecond?: number, attenuated = true): void {
const moveDelta = this._calculateMovePercentDelta({
anchor: new Vec2(0, 1),
applyToHorizontal: false,
applyToVertical: true,
});
_moveDeltaOptions.anchor.set(0, 1);
_moveDeltaOptions.applyToHorizontal = false;
_moveDeltaOptions.applyToVertical = true;
const moveDelta = this._calculateMovePercentDelta(_moveDeltaOptions);

if (timeInSecond) {
this._startAutoScroll(moveDelta, timeInSecond, attenuated !== false);
Expand All @@ -546,11 +550,10 @@ export class ScrollView extends ViewGroup {
* ```
*/
public scrollToLeft (timeInSecond?: number, attenuated = true): void {
const moveDelta = this._calculateMovePercentDelta({
anchor: new Vec2(0, 0),
applyToHorizontal: true,
applyToVertical: false,
});
_moveDeltaOptions.anchor.set(0, 0);
_moveDeltaOptions.applyToHorizontal = true;
_moveDeltaOptions.applyToVertical = false;
const moveDelta = this._calculateMovePercentDelta(_moveDeltaOptions);

if (timeInSecond) {
this._startAutoScroll(moveDelta, timeInSecond, attenuated !== false);
Expand All @@ -576,11 +579,10 @@ export class ScrollView extends ViewGroup {
* ```
*/
public scrollToRight (timeInSecond?: number, attenuated = true): void {
const moveDelta = this._calculateMovePercentDelta({
anchor: new Vec2(1, 0),
applyToHorizontal: true,
applyToVertical: false,
});
_moveDeltaOptions.anchor.set(1, 0);
_moveDeltaOptions.applyToHorizontal = true;
_moveDeltaOptions.applyToVertical = false;
const moveDelta = this._calculateMovePercentDelta(_moveDeltaOptions);

if (timeInSecond) {
this._startAutoScroll(moveDelta, timeInSecond, attenuated !== false);
Expand All @@ -606,11 +608,10 @@ export class ScrollView extends ViewGroup {
* ```
*/
public scrollToTopLeft (timeInSecond?: number, attenuated = true): void {
const moveDelta = this._calculateMovePercentDelta({
anchor: new Vec2(0, 1),
applyToHorizontal: true,
applyToVertical: true,
});
_moveDeltaOptions.anchor.set(0, 1);
_moveDeltaOptions.applyToHorizontal = true;
_moveDeltaOptions.applyToVertical = true;
const moveDelta = this._calculateMovePercentDelta(_moveDeltaOptions);

if (timeInSecond) {
this._startAutoScroll(moveDelta, timeInSecond, attenuated !== false);
Expand All @@ -636,11 +637,10 @@ export class ScrollView extends ViewGroup {
* ```
*/
public scrollToTopRight (timeInSecond?: number, attenuated = true): void {
const moveDelta = this._calculateMovePercentDelta({
anchor: new Vec2(1, 1),
applyToHorizontal: true,
applyToVertical: true,
});
_moveDeltaOptions.anchor.set(1, 1);
_moveDeltaOptions.applyToHorizontal = true;
_moveDeltaOptions.applyToVertical = true;
const moveDelta = this._calculateMovePercentDelta(_moveDeltaOptions);

if (timeInSecond) {
this._startAutoScroll(moveDelta, timeInSecond, attenuated !== false);
Expand All @@ -666,11 +666,10 @@ export class ScrollView extends ViewGroup {
* ```
*/
public scrollToBottomLeft (timeInSecond?: number, attenuated = true): void {
const moveDelta = this._calculateMovePercentDelta({
anchor: new Vec2(0, 0),
applyToHorizontal: true,
applyToVertical: true,
});
_moveDeltaOptions.anchor.set(0, 0);
_moveDeltaOptions.applyToHorizontal = true;
_moveDeltaOptions.applyToVertical = true;
const moveDelta = this._calculateMovePercentDelta(_moveDeltaOptions);

if (timeInSecond) {
this._startAutoScroll(moveDelta, timeInSecond, attenuated !== false);
Expand All @@ -696,11 +695,10 @@ export class ScrollView extends ViewGroup {
* ```
*/
public scrollToBottomRight (timeInSecond?: number, attenuated = true): void {
const moveDelta = this._calculateMovePercentDelta({
anchor: new Vec2(1, 0),
applyToHorizontal: true,
applyToVertical: true,
});
_moveDeltaOptions.anchor.set(1, 0);
_moveDeltaOptions.applyToHorizontal = true;
_moveDeltaOptions.applyToVertical = true;
const moveDelta = this._calculateMovePercentDelta(_moveDeltaOptions);

if (timeInSecond) {
this._startAutoScroll(moveDelta, timeInSecond, attenuated !== false);
Expand Down Expand Up @@ -806,11 +804,10 @@ export class ScrollView extends ViewGroup {
* ```
*/
public scrollToPercentHorizontal (percent: number, timeInSecond: number, attenuated: boolean): void {
const moveDelta = this._calculateMovePercentDelta({
anchor: new Vec2(percent, 0),
applyToHorizontal: true,
applyToVertical: false,
});
_moveDeltaOptions.anchor.set(percent, 0);
_moveDeltaOptions.applyToHorizontal = true;
_moveDeltaOptions.applyToVertical = false;
const moveDelta = this._calculateMovePercentDelta(_moveDeltaOptions);

if (timeInSecond) {
this._startAutoScroll(moveDelta, timeInSecond, attenuated !== false);
Expand Down Expand Up @@ -842,11 +839,10 @@ export class ScrollView extends ViewGroup {
* ```
*/
public scrollTo (anchor: Vec2, timeInSecond?: number, attenuated?: boolean): void {
const moveDelta = this._calculateMovePercentDelta({
anchor: new Vec2(anchor),
applyToHorizontal: true,
applyToVertical: true,
});
_moveDeltaOptions.anchor.set(anchor);
_moveDeltaOptions.applyToHorizontal = true;
_moveDeltaOptions.applyToVertical = true;
const moveDelta = this._calculateMovePercentDelta(_moveDeltaOptions);

if (timeInSecond) {
this._startAutoScroll(moveDelta, timeInSecond, attenuated);
Expand All @@ -873,11 +869,10 @@ export class ScrollView extends ViewGroup {
* ```
*/
public scrollToPercentVertical (percent: number, timeInSecond?: number, attenuated?: boolean): void {
const moveDelta = this._calculateMovePercentDelta({
anchor: new Vec2(0, percent),
applyToHorizontal: false,
applyToVertical: true,
});
_moveDeltaOptions.anchor.set(0, percent);
_moveDeltaOptions.applyToHorizontal = false;
_moveDeltaOptions.applyToVertical = true;
const moveDelta = this._calculateMovePercentDelta(_moveDeltaOptions);

if (timeInSecond) {
this._startAutoScroll(moveDelta, timeInSecond, attenuated);
Expand Down
Loading