1 /* 2 * Copyright (C) 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package androidx.constraintlayout.motion.widget; 18 19 /** 20 * Container for holding swipe information 21 */ 22 public class OnSwipe { 23 private int mDragDirection = 0; 24 private int mTouchAnchorSide = 0; 25 private int mTouchAnchorId = MotionScene.UNSET; 26 private int mTouchRegionId = MotionScene.UNSET; 27 private int mLimitBoundsTo = MotionScene.UNSET; 28 private int mOnTouchUp = 0; 29 private int mRotationCenterId = MotionScene.UNSET; 30 private float mMaxVelocity = 4; 31 private float mMaxAcceleration = 1.2f; 32 private boolean mMoveWhenScrollAtTop = true; 33 private float mDragScale = 1f; 34 private int mFlags = 0; 35 private float mDragThreshold = 10; 36 private float mSpringDamping = Float.NaN; 37 private float mSpringMass = 1; 38 private float mSpringStiffness = Float.NaN; 39 private float mSpringStopThreshold = Float.NaN; 40 private int mSpringBoundary = 0; 41 private int mAutoCompleteMode = 0; 42 43 public static final int COMPLETE_MODE_CONTINUOUS_VELOCITY = 0; 44 public static final int COMPLETE_MODE_SPRING = 1; 45 46 public static final int SPRING_BOUNDARY_OVERSHOOT = 0; 47 public static final int SPRING_BOUNDARY_BOUNCESTART = 1; 48 public static final int SPRING_BOUNDARY_BOUNCEEND = 2; 49 public static final int SPRING_BOUNDARY_BOUNCEBOTH = 3; 50 public static final int DRAG_UP = 0; 51 public static final int DRAG_DOWN = 1; 52 public static final int DRAG_LEFT = 2; 53 public static final int DRAG_RIGHT = 3; 54 public static final int DRAG_START = 4; 55 public static final int DRAG_END = 5; 56 public static final int DRAG_CLOCKWISE = 6; 57 public static final int DRAG_ANTICLOCKWISE = 7; 58 59 public static final int FLAG_DISABLE_POST_SCROLL = 1; 60 public static final int FLAG_DISABLE_SCROLL = 2; 61 62 public static final int SIDE_TOP = 0; 63 public static final int SIDE_LEFT = 1; 64 public static final int SIDE_RIGHT = 2; 65 public static final int SIDE_BOTTOM = 3; 66 public static final int SIDE_MIDDLE = 4; 67 public static final int SIDE_START = 5; 68 public static final int SIDE_END = 6; 69 70 public static final int ON_UP_AUTOCOMPLETE = 0; 71 public static final int ON_UP_AUTOCOMPLETE_TO_START = 1; 72 public static final int ON_UP_AUTOCOMPLETE_TO_END = 2; 73 public static final int ON_UP_STOP = 3; 74 public static final int ON_UP_DECELERATE = 4; 75 public static final int ON_UP_DECELERATE_AND_COMPLETE = 5; 76 public static final int ON_UP_NEVER_TO_START = 6; 77 public static final int ON_UP_NEVER_TO_END = 7; 78 79 /** 80 * The id of the view who's movement is matched to your drag 81 * If not specified it will map to a linear movement across the width of the motionLayout 82 * 83 * @param side 84 * @return 85 */ setTouchAnchorId(int side)86 public OnSwipe setTouchAnchorId(int side) { 87 mTouchAnchorId = side; 88 return this; 89 } 90 getTouchAnchorId()91 public int getTouchAnchorId() { 92 return mTouchAnchorId; 93 } 94 95 /** 96 * This side of the view that matches the drag movement. 97 * Only meaning full if the object changes size during the movement. 98 * (rotation is not considered) 99 * 100 * @param side 101 * @return 102 */ setTouchAnchorSide(int side)103 public OnSwipe setTouchAnchorSide(int side) { 104 mTouchAnchorSide = side; 105 return this; 106 } 107 getTouchAnchorSide()108 public int getTouchAnchorSide() { 109 return mTouchAnchorSide; 110 } 111 112 /** 113 * The direction of the drag. 114 * 115 * @param dragDirection 116 * @return 117 */ setDragDirection(int dragDirection)118 public OnSwipe setDragDirection(int dragDirection) { 119 mDragDirection = dragDirection; 120 return this; 121 } 122 getDragDirection()123 public int getDragDirection() { 124 return mDragDirection; 125 } 126 127 /** 128 * The maximum velocity (Change in progress per second) animation can achieve 129 * 130 * @param maxVelocity 131 * @return 132 */ setMaxVelocity(int maxVelocity)133 public OnSwipe setMaxVelocity(int maxVelocity) { 134 mMaxVelocity = maxVelocity; 135 return this; 136 } 137 getMaxVelocity()138 public float getMaxVelocity() { 139 return mMaxVelocity; 140 } 141 142 /** 143 * The maximum acceleration and deceleration of the animation 144 * (Change in Change in progress per second) 145 * Faster makes the object seem lighter and quicker 146 * 147 * @param maxAcceleration 148 * @return 149 */ setMaxAcceleration(int maxAcceleration)150 public OnSwipe setMaxAcceleration(int maxAcceleration) { 151 mMaxAcceleration = maxAcceleration; 152 return this; 153 } 154 getMaxAcceleration()155 public float getMaxAcceleration() { 156 return mMaxAcceleration; 157 } 158 159 /** 160 * When collaborating with a NestedScrollView do you progress form 0-1 only 161 * when the scroll view is at the top. 162 * 163 * @param moveWhenScrollAtTop 164 * @return 165 */ setMoveWhenScrollAtTop(boolean moveWhenScrollAtTop)166 public OnSwipe setMoveWhenScrollAtTop(boolean moveWhenScrollAtTop) { 167 mMoveWhenScrollAtTop = moveWhenScrollAtTop; 168 return this; 169 } 170 getMoveWhenScrollAtTop()171 public boolean getMoveWhenScrollAtTop() { 172 return mMoveWhenScrollAtTop; 173 } 174 175 /** 176 * Normally 1 this can be tweaked to make the acceleration faster 177 * 178 * @param dragScale 179 * @return 180 */ setDragScale(int dragScale)181 public OnSwipe setDragScale(int dragScale) { 182 mDragScale = dragScale; 183 return this; 184 } 185 getDragScale()186 public float getDragScale() { 187 return mDragScale; 188 } 189 190 /** 191 * This sets the threshold before the animation is kicked off. 192 * It is important when have multi state animations the have some play before the 193 * System decides which animation to jump on. 194 * 195 * @param dragThreshold 196 * @return 197 */ setDragThreshold(int dragThreshold)198 public OnSwipe setDragThreshold(int dragThreshold) { 199 mDragThreshold = dragThreshold; 200 return this; 201 } 202 getDragThreshold()203 public float getDragThreshold() { 204 return mDragThreshold; 205 } 206 207 /** 208 * @param side 209 * @return 210 */ setTouchRegionId(int side)211 public OnSwipe setTouchRegionId(int side) { 212 mTouchRegionId = side; 213 return this; 214 } 215 getTouchRegionId()216 public int getTouchRegionId() { 217 return mTouchRegionId; 218 } 219 220 /** 221 * Configures what happens when the user releases on mouse up. 222 * One of: ON_UP_AUTOCOMPLETE, ON_UP_AUTOCOMPLETE_TO_START, ON_UP_AUTOCOMPLETE_TO_END, 223 * ON_UP_STOP, ON_UP_DECELERATE, ON_UP_DECELERATE_AND_COMPLETE 224 * 225 * @param mode default = ON_UP_AUTOCOMPLETE 226 * @return 227 */ setOnTouchUp(int mode)228 public OnSwipe setOnTouchUp(int mode) { 229 mOnTouchUp = mode; 230 return this; 231 } 232 getOnTouchUp()233 public int getOnTouchUp() { 234 return mOnTouchUp; 235 } 236 237 /** 238 * Various flag to control behaviours of nested scroll 239 * FLAG_DISABLE_POST_SCROLL = 1; 240 * FLAG_DISABLE_SCROLL = 2; 241 * 242 * @param flags 243 * @return 244 */ setNestedScrollFlags(int flags)245 public OnSwipe setNestedScrollFlags(int flags) { 246 mFlags = flags; 247 return this; 248 } 249 getNestedScrollFlags()250 public int getNestedScrollFlags() { 251 return mFlags; 252 } 253 254 /** 255 * Only allow touch actions to be initiated within this region 256 * 257 * @param id 258 * @return 259 */ setLimitBoundsTo(int id)260 public OnSwipe setLimitBoundsTo(int id) { 261 mLimitBoundsTo = id; 262 return this; 263 } 264 getLimitBoundsTo()265 public int getLimitBoundsTo() { 266 return mLimitBoundsTo; 267 } 268 269 /** 270 * The view to center the rotation about 271 * 272 * @param rotationCenterId 273 * @return this 274 */ setRotateCenter(int rotationCenterId)275 public OnSwipe setRotateCenter(int rotationCenterId) { 276 mRotationCenterId = rotationCenterId; 277 return this; 278 } 279 getRotationCenterId()280 public int getRotationCenterId() { 281 return mRotationCenterId; 282 } 283 284 getSpringDamping()285 public float getSpringDamping() { 286 return mSpringDamping; 287 } 288 289 /** 290 * Set the damping of the spring if using spring. 291 * c in "a = (-k*x-c*v)/m" equation for the acceleration of a spring 292 * 293 * @param springDamping 294 * @return this 295 */ setSpringDamping(float springDamping)296 public OnSwipe setSpringDamping(float springDamping) { 297 mSpringDamping = springDamping; 298 return this; 299 } 300 301 /** 302 * Get the mass of the spring. 303 * the m in "a = (-k*x-c*v)/m" equation for the acceleration of a spring 304 * 305 * @return 306 */ getSpringMass()307 public float getSpringMass() { 308 return mSpringMass; 309 } 310 311 /** 312 * Set the Mass of the spring if using spring. 313 * m in "a = (-k*x-c*v)/m" equation for the acceleration of a spring 314 * 315 * @param springMass 316 * @return this 317 */ setSpringMass(float springMass)318 public OnSwipe setSpringMass(float springMass) { 319 mSpringMass = springMass; 320 return this; 321 } 322 323 /** 324 * get the stiffness of the spring 325 * 326 * @return NaN if not set 327 */ getSpringStiffness()328 public float getSpringStiffness() { 329 return mSpringStiffness; 330 } 331 332 /** 333 * set the stiffness of the spring if using spring. 334 * If this is set the swipe will use a spring return system. 335 * If set to NaN it will revert to the norm system. 336 * K in "a = (-k*x-c*v)/m" equation for the acceleration of a spring 337 * 338 * @param springStiffness 339 * @return 340 */ setSpringStiffness(float springStiffness)341 public OnSwipe setSpringStiffness(float springStiffness) { 342 mSpringStiffness = springStiffness; 343 return this; 344 } 345 346 /** 347 * The threshold for spring motion to stop. 348 * 349 * @return 350 */ getSpringStopThreshold()351 public float getSpringStopThreshold() { 352 return mSpringStopThreshold; 353 } 354 355 /** 356 * set the threshold for spring motion to stop. 357 * This is in change in progress / second 358 * If the spring will never go above that threshold again it will stop. 359 * 360 * @param springStopThreshold 361 * @return 362 */ setSpringStopThreshold(float springStopThreshold)363 public OnSwipe setSpringStopThreshold(float springStopThreshold) { 364 mSpringStopThreshold = springStopThreshold; 365 return this; 366 } 367 368 /** 369 * The behaviour at the boundaries 0 and 1 370 * 371 * @return 372 */ getSpringBoundary()373 public int getSpringBoundary() { 374 return mSpringBoundary; 375 } 376 377 /** 378 * The behaviour at the boundaries 0 and 1. 379 * SPRING_BOUNDARY_OVERSHOOT = 0; 380 * SPRING_BOUNDARY_BOUNCE_START = 1; 381 * SPRING_BOUNDARY_BOUNCE_END = 2; 382 * SPRING_BOUNDARY_BOUNCE_BOTH = 3; 383 * 384 * @param springBoundary 385 * @return 386 */ setSpringBoundary(int springBoundary)387 public OnSwipe setSpringBoundary(int springBoundary) { 388 mSpringBoundary = springBoundary; 389 return this; 390 } 391 getAutoCompleteMode()392 public int getAutoCompleteMode() { 393 return mAutoCompleteMode; 394 } 395 396 397 /** 398 * sets the behaviour at the boundaries 0 and 1 399 * COMPLETE_MODE_CONTINUOUS_VELOCITY = 0; 400 * COMPLETE_MODE_SPRING = 1; 401 * 402 */ setAutoCompleteMode(int autoCompleteMode)403 public void setAutoCompleteMode(int autoCompleteMode) { 404 mAutoCompleteMode = autoCompleteMode; 405 } 406 407 } 408