@@ -34,7 +34,7 @@ import kotlinx.coroutines.launch
3434 */
3535@Composable
3636fun rememberEnhancedZoomState (
37- size : Size ,
37+ size : IntSize ,
3838 initialZoom : Float = 1f,
3939 initialRotation : Float = 0f,
4040 minZoom : Float = 1f,
@@ -80,7 +80,7 @@ fun rememberEnhancedZoomState(
8080 */
8181@Composable
8282fun rememberEnhancedZoomState (
83- size : Size ,
83+ size : IntSize ,
8484 initialZoom : Float = 1f,
8585 initialRotation : Float = 0f,
8686 minZoom : Float = 1f,
@@ -126,7 +126,7 @@ fun rememberEnhancedZoomState(
126126 */
127127@Composable
128128fun rememberEnhancedZoomState (
129- size : Size ,
129+ size : IntSize ,
130130 initialZoom : Float = 1f,
131131 initialRotation : Float = 0f,
132132 minZoom : Float = 1f,
@@ -164,7 +164,7 @@ fun rememberEnhancedZoomState(
164164 * @param rotationEnabled when set to true rotation is enabled
165165 */
166166open class EnhancedZoomState constructor(
167- var size : Size ,
167+ var size : IntSize ,
168168 initialZoom : Float = 1f ,
169169 initialRotation : Float = 0f ,
170170 minZoom : Float = .5f ,
@@ -181,7 +181,8 @@ open class EnhancedZoomState constructor(
181181 val isPanning = animatablePan.isRunning
182182 val isRotating = animatableRotation.isRunning
183183
184- var rectDraw = Rect (offset = Offset .Zero , size = size)
184+ var rectDraw =
185+ Rect (offset = Offset .Zero , size = Size (size.width.toFloat(), size.height.toFloat()))
185186 var rectImage = rectDraw.copy()
186187 var rectCrop = rectDraw
187188
@@ -196,6 +197,9 @@ open class EnhancedZoomState constructor(
196197 cropRect = rectCrop
197198 )
198199
200+ private fun getBounds (): Offset {
201+ return getBounds(size)
202+ }
199203
200204 // Touch gestures
201205 internal open suspend fun onDown (change : PointerInputChange ) = coroutineScope {
@@ -211,42 +215,90 @@ open class EnhancedZoomState constructor(
211215 }
212216
213217 // Transform Gestures
214- internal open suspend fun onGestureStart () = coroutineScope {
218+ internal open suspend fun onGestureStart (change : PointerInputChange ) = coroutineScope {
215219
216220 }
217221
218- internal open suspend fun onGesture () = coroutineScope {
222+ internal open suspend fun onGesture (
223+ centroid : Offset ,
224+ pan : Offset ,
225+ zoom : Float ,
226+ rotation : Float ,
227+ mainPointer : PointerInputChange ,
228+ changes : List <PointerInputChange >
229+ ) = coroutineScope {
230+
231+ updateZoomState(
232+ size = size,
233+ gestureZoom = zoom,
234+ gesturePan = pan,
235+ gestureRotate = rotation
236+ )
219237
238+ // Fling Gesture
239+ if (changes.size == 1 ) {
240+ addPosition(
241+ mainPointer.uptimeMillis,
242+ mainPointer.position
243+ )
244+ }
220245 }
221246
222247 internal suspend fun onGestureEnd () {
223- val velocity = velocityTracker.calculateVelocity ()
224- fling( Offset (velocity.x, velocity.y) )
248+ fling ()
249+ resetToValidBounds( )
225250 }
226251
227- internal fun addPosition (timeMillis : Long , position : Offset ) {
252+ private suspend fun resetToValidBounds () = coroutineScope {
253+
254+ val zoom = zoom.coerceAtLeast(1f )
255+ val bounds = getBounds()
256+
257+ val pan = Offset (
258+ pan.x.coerceIn(- bounds.x, bounds.x),
259+ pan.y.coerceIn(- bounds.y, bounds.y)
260+ )
261+
262+ resetWithAnimation(pan = pan,zoom = zoom)
263+ }
264+
265+
266+ // Fling gesture
267+ private fun addPosition (timeMillis : Long , position : Offset ) {
228268 velocityTracker.addPosition(
229269 timeMillis = timeMillis,
230270 position = position
231271 )
232272 }
233273
234274
235- private suspend fun fling (velocity : Offset ) = coroutineScope {
275+ private suspend fun fling () = coroutineScope {
276+ val velocityTracker = velocityTracker.calculateVelocity()
277+ val velocity = Offset (velocityTracker.x, velocityTracker.y)
278+
236279 launch {
237280 val animationResult = animatablePan.animateDecay(
238281 velocity,
239- exponentialDecay()
282+ exponentialDecay(),
283+ block = {
284+ // val pan = this.value
285+ // val bounds = getBounds()
286+ // if(pan.x < bounds.x || pan.y < bounds.y){
287+ // throw CancellationException()
288+ // }
289+
290+ }
240291 )
241292
293+
242294// if (!animationResult.endState.isRunning) {
243295// resetTracking()
244296// }
245297 }
246298 }
247299
248300
249- internal fun resetTracking () {
301+ private fun resetTracking () {
250302 velocityTracker.resetTracking()
251303 }
252304
0 commit comments