This repository has been archived by the owner on Jul 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 604
/
ScaleController.java
155 lines (126 loc) · 4.62 KB
/
ScaleController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* Copyright 2018 Google LLC All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.ar.sceneform.ux;
import com.google.ar.sceneform.FrameTime;
import com.google.ar.sceneform.Node;
import com.google.ar.sceneform.math.MathHelper;
import com.google.ar.sceneform.math.Vector3;
/**
* Manipulates the Scale of a {@link BaseTransformableNode} using a Pinch {@link
* PinchGestureRecognizer}. Applies a tunable elastic bounce-back when scaling the {@link
* BaseTransformableNode} beyond the min/max scale.
*/
public class ScaleController extends BaseTransformationController<PinchGesture> {
public static final float DEFAULT_MIN_SCALE = 0.75f;
public static final float DEFAULT_MAX_SCALE = 1.75f;
public static final float DEFAULT_SENSITIVITY = 0.75f;
public static final float DEFAULT_ELASTICITY = 0.15f;
private float minScale = DEFAULT_MIN_SCALE;
private float maxScale = DEFAULT_MAX_SCALE;
private float sensitivity = DEFAULT_SENSITIVITY;
private float elasticity = DEFAULT_ELASTICITY;
private float currentScaleRatio;
private static final float ELASTIC_RATIO_LIMIT = 0.8f;
private static final float LERP_SPEED = 8.0f;
public ScaleController(
BaseTransformableNode transformableNode, PinchGestureRecognizer gestureRecognizer) {
super(transformableNode, gestureRecognizer);
}
public void setMinScale(float minScale) {
this.minScale = minScale;
}
public float getMinScale() {
return minScale;
}
public void setMaxScale(float maxScale) {
this.maxScale = maxScale;
}
public float getMaxScale() {
return maxScale;
}
public void setSensitivity(float sensitivity) {
this.sensitivity = sensitivity;
}
public float getSensitivity() {
return sensitivity;
}
public void setElasticity(float elasticity) {
this.elasticity = elasticity;
}
public float getElasticity() {
return elasticity;
}
@Override
public void onActivated(Node node) {
super.onActivated(node);
Vector3 scale = getTransformableNode().getLocalScale();
currentScaleRatio = (scale.x - minScale) / getScaleDelta();
}
@Override
public void onUpdated(Node node, FrameTime frameTime) {
if (isTransforming() || !isEnabled()) {
return;
}
float t = MathHelper.clamp(frameTime.getDeltaSeconds() * LERP_SPEED, 0, 1);
currentScaleRatio = MathHelper.lerp(currentScaleRatio, getClampedScaleRatio(), t);
float finalScaleValue = getFinalScale();
Vector3 finalScale = new Vector3(finalScaleValue, finalScaleValue, finalScaleValue);
getTransformableNode().setLocalScale(finalScale);
}
@Override
public boolean canStartTransformation(PinchGesture gesture) {
return getTransformableNode().isSelected();
}
@Override
public void onContinueTransformation(PinchGesture gesture) {
currentScaleRatio += gesture.gapDeltaInches() * sensitivity;
float finalScaleValue = getFinalScale();
Vector3 finalScale = new Vector3(finalScaleValue, finalScaleValue, finalScaleValue);
getTransformableNode().setLocalScale(finalScale);
if (currentScaleRatio < -ELASTIC_RATIO_LIMIT
|| currentScaleRatio > (1.0f + ELASTIC_RATIO_LIMIT)) {
gesture.cancel();
}
}
@Override
public void onEndTransformation(PinchGesture gesture) {}
private float getScaleDelta() {
float scaleDelta = maxScale - minScale;
if (scaleDelta <= 0.0f) {
throw new IllegalStateException("maxScale must be greater than minScale.");
}
return scaleDelta;
}
private float getClampedScaleRatio() {
return Math.min(1.0f, Math.max(0.0f, currentScaleRatio));
}
private float getFinalScale() {
float elasticScaleRatio = getClampedScaleRatio() + getElasticDelta();
float elasticScale = minScale + elasticScaleRatio * getScaleDelta();
return elasticScale;
}
private float getElasticDelta() {
float overRatio;
if (currentScaleRatio > 1.0f) {
overRatio = currentScaleRatio - 1.0f;
} else if (currentScaleRatio < 0.0f) {
overRatio = currentScaleRatio;
} else {
return 0.0f;
}
return (1.0f - (1.0f / ((Math.abs(overRatio) * elasticity) + 1.0f))) * Math.signum(overRatio);
}
}