@@ -8,6 +8,7 @@ import androidx.compose.ui.geometry.Size
88import androidx.compose.ui.input.pointer.PointerInputChange
99import androidx.compose.ui.input.pointer.util.VelocityTracker
1010import androidx.compose.ui.unit.IntSize
11+ import com.smarttoolfactory.image.util.coerceIn
1112import com.smarttoolfactory.image.util.getCropRect
1213import kotlinx.coroutines.coroutineScope
1314
@@ -31,15 +32,22 @@ open class EnhancedZoomState constructor(
3132 maxZoom : Float = 5f ,
3233 val flingGestureEnabled : Boolean = true ,
3334 val moveToBoundsEnabled : Boolean = true ,
34- override var zoomEnabled : Boolean = true ,
35- override var panEnabled : Boolean = true ,
36- override var rotationEnabled : Boolean = true ,
37- override var limitPan : Boolean = false
35+ zoomEnabled : Boolean = true ,
36+ panEnabled : Boolean = true ,
37+ rotationEnabled : Boolean = false ,
38+ limitPan : Boolean = false
3839) : ZoomState(
39- 1f , 1f , minZoom, maxZoom, zoomEnabled
40+ initialZoom = initialZoom,
41+ initialRotation = 1f ,
42+ minZoom = minZoom,
43+ maxZoom = maxZoom,
44+ zoomEnabled = zoomEnabled,
45+ panEnabled = panEnabled,
46+ rotationEnabled = rotationEnabled,
47+ limitPan = limitPan
4048) {
4149
42- var rectDraw = Rect (
50+ private val rectDraw = Rect (
4351 offset = Offset .Zero ,
4452 size = Size (containerSize.width.toFloat(), containerSize.height.toFloat())
4553 )
@@ -59,14 +67,18 @@ open class EnhancedZoomState constructor(
5967 return getBounds(containerSize)
6068 }
6169
62- // Touch gestures
70+ /*
71+ Touch Gesture Events
72+ */
6373 open suspend fun onDown (change : PointerInputChange ) {}
6474
6575 open suspend fun onMove (change : PointerInputChange ) {}
6676
6777 open suspend fun onUp (change : PointerInputChange ) {}
6878
69- // Transform Gestures
79+ /*
80+ Transform Gesture Events
81+ */
7082 internal open suspend fun onGestureStart (change : PointerInputChange ) {}
7183
7284 open suspend fun onGesture (
@@ -86,25 +98,28 @@ open class EnhancedZoomState constructor(
8698 )
8799
88100 // Fling Gesture
89- if (changes.size == 1 ) {
90- addPosition(
91- mainPointer.uptimeMillis,
92- mainPointer.position
93- )
101+ if (flingGestureEnabled) {
102+ if (changes.size == 1 ) {
103+ addPosition(mainPointer.uptimeMillis, mainPointer.position)
104+ }
94105 }
95106 }
96107
97- suspend fun onGestureEnd (onAnimationEnd : () -> Unit ) {
98- if (zoom > 1 ) {
108+ suspend fun onGestureEnd (onFinish : () -> Unit ) {
109+ if (flingGestureEnabled && zoom > 1 ) {
99110 fling()
100111 }
101- resetToValidBounds()
102- onAnimationEnd()
112+ if (moveToBoundsEnabled) {
113+ resetToValidBounds()
114+ }
115+ onFinish()
103116 }
104117
105118 // Double Tap
106119 suspend fun onDoubleTap (onAnimationEnd : () -> Unit ) {
107- resetTracking()
120+ if (flingGestureEnabled) {
121+ resetTracking()
122+ }
108123 resetWithAnimation()
109124 onAnimationEnd()
110125 }
@@ -115,18 +130,15 @@ open class EnhancedZoomState constructor(
115130 private suspend fun resetToValidBounds () {
116131 val zoom = zoom.coerceAtLeast(1f )
117132 val bounds = getBounds()
118-
119- val pan = Offset (
120- pan.x.coerceIn(- bounds.x, bounds.x),
121- pan.y.coerceIn(- bounds.y, bounds.y)
122- )
123-
133+ val pan = pan.coerceIn(- bounds.x.. bounds.x, - bounds.y.. bounds.y)
124134 resetWithAnimation(pan = pan, zoom = zoom)
125135 resetTracking()
126136 }
127137
128138
129- // Fling gesture
139+ /*
140+ Fling gesture
141+ */
130142 private fun addPosition (timeMillis : Long , position : Offset ) {
131143 velocityTracker.addPosition(
132144 timeMillis = timeMillis,
@@ -150,33 +162,6 @@ open class EnhancedZoomState constructor(
150162 velocityTracker.resetTracking()
151163 }
152164
153- override suspend fun updateZoomState (
154- size : IntSize ,
155- gesturePan : Offset ,
156- gestureZoom : Float ,
157- gestureRotate : Float ,
158- ) {
159- val zoom = (zoom * gestureZoom).coerceIn(zoomMin, zoomMax)
160- val rotation = if (rotationEnabled) {
161- rotation + gestureRotate
162- } else {
163- 0f
164- }
165-
166- if (panEnabled) {
167- val newOffset = pan + gesturePan.times(zoom)
168- snapPanTo(newOffset)
169- }
170-
171- if (zoomEnabled) {
172- snapZoomTo(zoom)
173- }
174-
175- if (rotationEnabled) {
176- snapRotationTo(rotation)
177- }
178- }
179-
180165 private fun calculateRectBounds (): Rect {
181166
182167 val width = containerSize.width
@@ -208,15 +193,6 @@ open class EnhancedZoomState constructor(
208193 zoom = zoom,
209194 rectSelection = rectDraw
210195 )
211-
212- println (
213- " 😃 EnhancedZoomState calculateRectBounds()\n " +
214- " offsetX: $offsetX , offsetY: $offsetY \n " +
215- " totalHeight: ${rect.bottom > imageSize.height} , offset: $offset \n " +
216- " containerSize: $containerSize , pan: $pan , zoom: $zoom \n " +
217- " rect: $rect , height: ${rect.height} "
218- )
219-
220196 return rect
221197 }
222198}
0 commit comments