@@ -4,19 +4,9 @@ import androidx.compose.foundation.gestures.detectTapGestures
44import androidx.compose.runtime.rememberCoroutineScope
55import androidx.compose.ui.Modifier
66import androidx.compose.ui.composed
7- import androidx.compose.ui.geometry.Offset
8- import androidx.compose.ui.geometry.Rect
9- import androidx.compose.ui.geometry.Size
10- import androidx.compose.ui.graphics.Color
11- import androidx.compose.ui.graphics.drawscope.DrawScope
12- import androidx.compose.ui.graphics.drawscope.Stroke
137import androidx.compose.ui.graphics.graphicsLayer
14- import androidx.compose.ui.input.pointer.PointerInputChange
158import androidx.compose.ui.input.pointer.pointerInput
16- import androidx.compose.ui.input.pointer.positionChange
17- import androidx.compose.ui.unit.dp
189import com.smarttoolfactory.gesture.detectTransformGestures
19- import com.smarttoolfactory.image.transform.TouchRegion
2010import com.smarttoolfactory.image.util.update
2111import kotlinx.coroutines.launch
2212
@@ -43,15 +33,14 @@ fun Modifier.enhancedZoom(
4333 },
4434 onGestureEnd = {
4535 coroutineScope.launch {
46- enhancedZoomState.onGestureEnd()
36+ enhancedZoomState.onGestureEnd {
37+ onUp(enhancedZoomState.enhancedZoomData)
38+ }
4739 }
48-
49- onUp(enhancedZoomState.enhancedZoomData)
5040 },
5141 onGesture = { centroid, pan, zoom, rotate, mainPointer, pointerList ->
5242
5343 coroutineScope.launch {
54-
5544 enhancedZoomState.onGesture(
5645 centroid,
5746 pan,
@@ -60,9 +49,9 @@ fun Modifier.enhancedZoom(
6049 mainPointer,
6150 pointerList
6251 )
63-
64- onMove(enhancedZoomState.enhancedZoomData)
6552 }
53+
54+ onMove(enhancedZoomState.enhancedZoomData)
6655 }
6756 )
6857 }
@@ -71,7 +60,9 @@ fun Modifier.enhancedZoom(
7160 detectTapGestures(
7261 onDoubleTap = {
7362 coroutineScope.launch {
74- enhancedZoomState.resetWithAnimation()
63+ enhancedZoomState.onDoubleTap {
64+ onUp(enhancedZoomState.enhancedZoomData)
65+ }
7566 }
7667 }
7768 )
@@ -98,169 +89,3 @@ fun Modifier.enhancedZoom(
9889 properties[" onUp" ] = onUp
9990 }
10091)
101-
102- internal fun moveIntoBounds (rectBounds : Rect , rectCurrent : Rect ): Rect {
103- var width = rectCurrent.width
104- var height = rectCurrent.height
105-
106-
107- if (width > rectBounds.width) {
108- width = rectBounds.width
109- }
110-
111- if (height > rectBounds.height) {
112- height = rectBounds.height
113- }
114-
115- var rect = Rect (offset = rectCurrent.topLeft, size = Size (width, height))
116-
117- if (rect.left < rectBounds.left) {
118- rect = rect.translate(rectBounds.left - rect.left, 0f )
119- }
120-
121- if (rect.top < rectBounds.top) {
122- rect = rect.translate(0f , rectBounds.top - rect.top)
123- }
124-
125- if (rect.right > rectBounds.right) {
126- rect = rect.translate(rectBounds.right - rect.right, 0f )
127- }
128-
129- if (rect.bottom > rectBounds.bottom) {
130- rect = rect.translate(0f , rectBounds.bottom - rect.bottom)
131- }
132-
133- return rect
134- }
135-
136- /* *
137- * Update draw rect based on user touch
138- */
139- fun updateDrawRect (
140- distanceToEdgeFromTouch : Offset ,
141- touchRegion : TouchRegion ,
142- minDimension : Float ,
143- rectTemp : Rect ,
144- rectDraw : Rect ,
145- change : PointerInputChange
146- ): Rect {
147-
148- val position = change.position
149- // Get screen coordinates from touch position inside composable
150- // and add how far it's from corner to not jump edge to user's touch position
151- val screenPositionX = position.x + distanceToEdgeFromTouch.x
152- val screenPositionY = position.y + distanceToEdgeFromTouch.y
153-
154- return when (touchRegion) {
155-
156- // Corners
157- TouchRegion .TopLeft -> {
158-
159- // Set position of top left while moving with top left handle and
160- // limit position to not intersect other handles
161- val left = screenPositionX.coerceAtMost(rectTemp.right - minDimension)
162- val top = screenPositionY.coerceAtMost(rectTemp.bottom - minDimension)
163- Rect (
164- left = left,
165- top = top,
166- right = rectTemp.right,
167- bottom = rectTemp.bottom
168- )
169- }
170-
171- TouchRegion .BottomLeft -> {
172-
173- // Set position of top left while moving with bottom left handle and
174- // limit position to not intersect other handles
175- val left = screenPositionX.coerceAtMost(rectTemp.right - minDimension)
176- val bottom = screenPositionY.coerceAtLeast(rectTemp.top + minDimension)
177- Rect (
178- left = left,
179- top = rectTemp.top,
180- right = rectTemp.right,
181- bottom = bottom,
182- )
183-
184- }
185-
186- TouchRegion .TopRight -> {
187-
188- // Set position of top left while moving with top right handle and
189- // limit position to not intersect other handles
190- val right = screenPositionX.coerceAtLeast(rectTemp.left + minDimension)
191- val top = screenPositionY.coerceAtMost(rectTemp.bottom - minDimension)
192-
193- Rect (
194- left = rectTemp.left,
195- top = top,
196- right = right,
197- bottom = rectTemp.bottom,
198- )
199-
200- }
201-
202- TouchRegion .BottomRight -> {
203-
204- // Set position of top left while moving with bottom right handle and
205- // limit position to not intersect other handles
206- val right = screenPositionX.coerceAtLeast(rectTemp.left + minDimension)
207- val bottom = screenPositionY.coerceAtLeast(rectTemp.top + minDimension)
208-
209- Rect (
210- left = rectTemp.left,
211- top = rectTemp.top,
212- right = right,
213- bottom = bottom
214- )
215- }
216-
217- TouchRegion .Inside -> {
218- val drag = change.positionChange()
219-
220- val scaledDragX = drag.x
221- val scaledDragY = drag.y
222-
223- rectDraw.translate(scaledDragX, scaledDragY)
224- }
225-
226- else -> rectDraw
227- }
228- }
229-
230-
231- fun DrawScope.drawGrid (rect : Rect , color : Color = Color .White ) {
232-
233- val width = rect.width
234- val height = rect.height
235- val gridWidth = width / 3
236- val gridHeight = height / 3
237-
238-
239- drawRect(
240- color = color,
241- topLeft = rect.topLeft,
242- size = rect.size,
243- style = Stroke (width = 2 .dp.toPx())
244- )
245-
246- // Horizontal lines
247- for (i in 1 .. 2 ) {
248- drawLine(
249- color = color,
250- start = Offset (rect.left, rect.top + i * gridHeight),
251- end = Offset (rect.right, rect.top + i * gridHeight),
252- strokeWidth = .7 .dp.toPx()
253- )
254- }
255-
256- // Vertical lines
257- for (i in 1 .. 2 ) {
258- drawLine(
259- color,
260- start = Offset (rect.left + i * gridWidth, rect.top),
261- end = Offset (rect.left + i * gridWidth, rect.bottom),
262- strokeWidth = .7 .dp.toPx()
263- )
264- }
265- }
266-
0 commit comments