shepshapard | b14d643 | 2019-08-05 17:13:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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 | |
George Mount | 54370f1 | 2020-05-06 16:03:20 -0700 | [diff] [blame] | 17 | package androidx.ui.core.demos.gestures |
shepshapard | b14d643 | 2019-08-05 17:13:22 -0700 | [diff] [blame] | 18 | |
Louis Pullen-Freilich | a228adf | 2020-03-11 18:42:11 +0000 | [diff] [blame] | 19 | import androidx.compose.Composable |
shepshapard | b14d643 | 2019-08-05 17:13:22 -0700 | [diff] [blame] | 20 | import androidx.compose.state |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 21 | import androidx.ui.core.Alignment |
Shep Shapard | 8931646 | 2020-03-03 17:00:52 -0800 | [diff] [blame] | 22 | import androidx.ui.core.DensityAmbient |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 23 | import androidx.ui.core.Modifier |
shepshapard | b14d643 | 2019-08-05 17:13:22 -0700 | [diff] [blame] | 24 | import androidx.ui.core.gesture.DragObserver |
shepshapard | b14d643 | 2019-08-05 17:13:22 -0700 | [diff] [blame] | 25 | import androidx.ui.core.gesture.ScaleObserver |
Shep Shapard | 70a8cfe | 2020-03-30 16:17:26 -0700 | [diff] [blame] | 26 | import androidx.ui.core.gesture.dragGestureFilter |
Shep Shapard | 07c9e2c | 2020-05-21 16:44:25 -0700 | [diff] [blame] | 27 | import androidx.ui.core.gesture.scaleGestureFilter |
| 28 | import androidx.ui.core.gesture.tapGestureFilter |
Louis Pullen-Freilich | ddda7be | 2020-07-17 18:28:12 +0100 | [diff] [blame] | 29 | import androidx.compose.foundation.Box |
| 30 | import androidx.compose.foundation.Text |
Louis Pullen-Freilich | 623e405 | 2020-07-19 20:24:03 +0100 | [diff] [blame] | 31 | import androidx.compose.foundation.layout.Column |
| 32 | import androidx.compose.foundation.layout.fillMaxSize |
| 33 | import androidx.compose.foundation.layout.offset |
| 34 | import androidx.compose.foundation.layout.preferredSize |
| 35 | import androidx.compose.foundation.layout.wrapContentSize |
Louis Pullen-Freilich | f434a13 | 2020-07-22 14:19:24 +0100 | [diff] [blame] | 36 | import androidx.compose.ui.geometry.Offset |
George Mount | 842c8c1 | 2020-01-08 16:03:42 -0800 | [diff] [blame] | 37 | import androidx.ui.unit.dp |
shepshapard | b14d643 | 2019-08-05 17:13:22 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
Shep Shapard | ca62bbb | 2020-05-15 16:20:52 -0700 | [diff] [blame] | 40 | * Simple demo that shows off how [dragGestureFilter] and [scaleGestureFilter] automatically |
shepshapard | b14d643 | 2019-08-05 17:13:22 -0700 | [diff] [blame] | 41 | * interoperate. |
| 42 | */ |
Louis Pullen-Freilich | a228adf | 2020-03-11 18:42:11 +0000 | [diff] [blame] | 43 | @Composable |
Shep Shapard | ca62bbb | 2020-05-15 16:20:52 -0700 | [diff] [blame] | 44 | fun DragAndScaleGestureFilterDemo() { |
Shep Shapard | 8931646 | 2020-03-03 17:00:52 -0800 | [diff] [blame] | 45 | val size = state { 200.dp } |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 46 | val offset = state { Offset.Zero } |
Louis Pullen-Freilich | a228adf | 2020-03-11 18:42:11 +0000 | [diff] [blame] | 47 | val dragInScale = state { false } |
shepshapard | b14d643 | 2019-08-05 17:13:22 -0700 | [diff] [blame] | 48 | |
Louis Pullen-Freilich | a228adf | 2020-03-11 18:42:11 +0000 | [diff] [blame] | 49 | val scaleObserver = object : ScaleObserver { |
| 50 | override fun onScale(scaleFactor: Float) { |
Shep Shapard | 8931646 | 2020-03-03 17:00:52 -0800 | [diff] [blame] | 51 | size.value *= scaleFactor |
Louis Pullen-Freilich | a228adf | 2020-03-11 18:42:11 +0000 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
| 55 | val dragObserver = object : DragObserver { |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 56 | override fun onDrag(dragDistance: Offset): Offset { |
Shep Shapard | 8931646 | 2020-03-03 17:00:52 -0800 | [diff] [blame] | 57 | offset.value += dragDistance |
Louis Pullen-Freilich | a228adf | 2020-03-11 18:42:11 +0000 | [diff] [blame] | 58 | return dragDistance |
| 59 | } |
| 60 | } |
| 61 | |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 62 | val onRelease: (Offset) -> Unit = { |
Louis Pullen-Freilich | a228adf | 2020-03-11 18:42:11 +0000 | [diff] [blame] | 63 | dragInScale.value = !dragInScale.value |
| 64 | } |
| 65 | |
Shep Shapard | 8931646 | 2020-03-03 17:00:52 -0800 | [diff] [blame] | 66 | val gestures = |
| 67 | if (dragInScale.value) { |
Shep Shapard | 70a8cfe | 2020-03-30 16:17:26 -0700 | [diff] [blame] | 68 | Modifier |
| 69 | .scaleGestureFilter(scaleObserver) |
| 70 | .dragGestureFilter(dragObserver) |
| 71 | .tapGestureFilter(onRelease) |
Shep Shapard | 8931646 | 2020-03-03 17:00:52 -0800 | [diff] [blame] | 72 | } else { |
Shep Shapard | 70a8cfe | 2020-03-30 16:17:26 -0700 | [diff] [blame] | 73 | Modifier |
| 74 | .dragGestureFilter(dragObserver) |
| 75 | .scaleGestureFilter(scaleObserver) |
| 76 | .tapGestureFilter(onRelease) |
Louis Pullen-Freilich | a228adf | 2020-03-11 18:42:11 +0000 | [diff] [blame] | 77 | } |
Shep Shapard | 8931646 | 2020-03-03 17:00:52 -0800 | [diff] [blame] | 78 | |
Shep Shapard | 07c9e2c | 2020-05-21 16:44:25 -0700 | [diff] [blame] | 79 | val color = |
| 80 | if (dragInScale.value) { |
| 81 | Red |
| 82 | } else { |
| 83 | Blue |
| 84 | } |
| 85 | |
Shep Shapard | 8931646 | 2020-03-03 17:00:52 -0800 | [diff] [blame] | 86 | val (offsetX, offsetY) = |
| 87 | with(DensityAmbient.current) { offset.value.x.toDp() to offset.value.y.toDp() } |
| 88 | |
Shep Shapard | 07c9e2c | 2020-05-21 16:44:25 -0700 | [diff] [blame] | 89 | Column { |
| 90 | Text("Demonstrates combining dragging with scaling.") |
| 91 | Text("Drag and scale around. Play with how slop works for both dragging and scaling. Tap" + |
| 92 | " on the box to flip the order of scaling and dragging. The behavior is always " + |
| 93 | "the same because the 2 gesture filters are completely orthogonal.") |
| 94 | Box( |
Shep Shapard | 78f4289 | 2020-05-20 16:02:06 -0700 | [diff] [blame] | 95 | Modifier.fillMaxSize() |
Shep Shapard | 07c9e2c | 2020-05-21 16:44:25 -0700 | [diff] [blame] | 96 | .wrapContentSize(Alignment.Center) |
Shep Shapard | 78f4289 | 2020-05-20 16:02:06 -0700 | [diff] [blame] | 97 | .offset(offsetX, offsetY) |
| 98 | .preferredSize(size.value) |
| 99 | .plus(gestures), |
Shep Shapard | 07c9e2c | 2020-05-21 16:44:25 -0700 | [diff] [blame] | 100 | backgroundColor = color |
| 101 | ) |
| 102 | } |
Shep Shapard | 8931646 | 2020-03-03 17:00:52 -0800 | [diff] [blame] | 103 | } |