[go: nahoru, domu]

blob: e29af96617b15a5a13d97a0cf48f112ef073e116 [file] [log] [blame]
shepshapardb14d6432019-08-05 17:13:22 -07001/*
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 Mount54370f12020-05-06 16:03:20 -070017package androidx.ui.core.demos.gestures
shepshapardb14d6432019-08-05 17:13:22 -070018
Louis Pullen-Freilicha228adf2020-03-11 18:42:11 +000019import androidx.compose.Composable
shepshapardb14d6432019-08-05 17:13:22 -070020import androidx.compose.state
Adam Powell999a89b2020-03-11 09:08:07 -070021import androidx.ui.core.Alignment
Shep Shapard89316462020-03-03 17:00:52 -080022import androidx.ui.core.DensityAmbient
Adam Powell999a89b2020-03-11 09:08:07 -070023import androidx.ui.core.Modifier
shepshapardb14d6432019-08-05 17:13:22 -070024import androidx.ui.core.gesture.DragObserver
shepshapardb14d6432019-08-05 17:13:22 -070025import androidx.ui.core.gesture.ScaleObserver
Shep Shapard70a8cfe2020-03-30 16:17:26 -070026import androidx.ui.core.gesture.dragGestureFilter
Shep Shapard07c9e2c2020-05-21 16:44:25 -070027import androidx.ui.core.gesture.scaleGestureFilter
28import androidx.ui.core.gesture.tapGestureFilter
Louis Pullen-Freilichddda7be2020-07-17 18:28:12 +010029import androidx.compose.foundation.Box
30import androidx.compose.foundation.Text
Louis Pullen-Freilich623e4052020-07-19 20:24:03 +010031import androidx.compose.foundation.layout.Column
32import androidx.compose.foundation.layout.fillMaxSize
33import androidx.compose.foundation.layout.offset
34import androidx.compose.foundation.layout.preferredSize
35import androidx.compose.foundation.layout.wrapContentSize
Louis Pullen-Freilichf434a132020-07-22 14:19:24 +010036import androidx.compose.ui.geometry.Offset
George Mount842c8c12020-01-08 16:03:42 -080037import androidx.ui.unit.dp
shepshapardb14d6432019-08-05 17:13:22 -070038
39/**
Shep Shapardca62bbb2020-05-15 16:20:52 -070040 * Simple demo that shows off how [dragGestureFilter] and [scaleGestureFilter] automatically
shepshapardb14d6432019-08-05 17:13:22 -070041 * interoperate.
42 */
Louis Pullen-Freilicha228adf2020-03-11 18:42:11 +000043@Composable
Shep Shapardca62bbb2020-05-15 16:20:52 -070044fun DragAndScaleGestureFilterDemo() {
Shep Shapard89316462020-03-03 17:00:52 -080045 val size = state { 200.dp }
Nader Jawad6df06122020-06-03 15:27:08 -070046 val offset = state { Offset.Zero }
Louis Pullen-Freilicha228adf2020-03-11 18:42:11 +000047 val dragInScale = state { false }
shepshapardb14d6432019-08-05 17:13:22 -070048
Louis Pullen-Freilicha228adf2020-03-11 18:42:11 +000049 val scaleObserver = object : ScaleObserver {
50 override fun onScale(scaleFactor: Float) {
Shep Shapard89316462020-03-03 17:00:52 -080051 size.value *= scaleFactor
Louis Pullen-Freilicha228adf2020-03-11 18:42:11 +000052 }
53 }
54
55 val dragObserver = object : DragObserver {
Nader Jawad6df06122020-06-03 15:27:08 -070056 override fun onDrag(dragDistance: Offset): Offset {
Shep Shapard89316462020-03-03 17:00:52 -080057 offset.value += dragDistance
Louis Pullen-Freilicha228adf2020-03-11 18:42:11 +000058 return dragDistance
59 }
60 }
61
Nader Jawad6df06122020-06-03 15:27:08 -070062 val onRelease: (Offset) -> Unit = {
Louis Pullen-Freilicha228adf2020-03-11 18:42:11 +000063 dragInScale.value = !dragInScale.value
64 }
65
Shep Shapard89316462020-03-03 17:00:52 -080066 val gestures =
67 if (dragInScale.value) {
Shep Shapard70a8cfe2020-03-30 16:17:26 -070068 Modifier
69 .scaleGestureFilter(scaleObserver)
70 .dragGestureFilter(dragObserver)
71 .tapGestureFilter(onRelease)
Shep Shapard89316462020-03-03 17:00:52 -080072 } else {
Shep Shapard70a8cfe2020-03-30 16:17:26 -070073 Modifier
74 .dragGestureFilter(dragObserver)
75 .scaleGestureFilter(scaleObserver)
76 .tapGestureFilter(onRelease)
Louis Pullen-Freilicha228adf2020-03-11 18:42:11 +000077 }
Shep Shapard89316462020-03-03 17:00:52 -080078
Shep Shapard07c9e2c2020-05-21 16:44:25 -070079 val color =
80 if (dragInScale.value) {
81 Red
82 } else {
83 Blue
84 }
85
Shep Shapard89316462020-03-03 17:00:52 -080086 val (offsetX, offsetY) =
87 with(DensityAmbient.current) { offset.value.x.toDp() to offset.value.y.toDp() }
88
Shep Shapard07c9e2c2020-05-21 16:44:25 -070089 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 Shapard78f42892020-05-20 16:02:06 -070095 Modifier.fillMaxSize()
Shep Shapard07c9e2c2020-05-21 16:44:25 -070096 .wrapContentSize(Alignment.Center)
Shep Shapard78f42892020-05-20 16:02:06 -070097 .offset(offsetX, offsetY)
98 .preferredSize(size.value)
99 .plus(gestures),
Shep Shapard07c9e2c2020-05-21 16:44:25 -0700100 backgroundColor = color
101 )
102 }
Shep Shapard89316462020-03-03 17:00:52 -0800103}