Skip to content

Commit a3f9215

Browse files
update EnhancedZoomModifier flags
1 parent 271b350 commit a3f9215

File tree

2 files changed

+46
-73
lines changed

2 files changed

+46
-73
lines changed

image/src/main/java/com/smarttoolfactory/image/zoom/EnhancedZoomStateImpl.kt

Lines changed: 37 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import androidx.compose.ui.geometry.Size
88
import androidx.compose.ui.input.pointer.PointerInputChange
99
import androidx.compose.ui.input.pointer.util.VelocityTracker
1010
import androidx.compose.ui.unit.IntSize
11+
import com.smarttoolfactory.image.util.coerceIn
1112
import com.smarttoolfactory.image.util.getCropRect
1213
import 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
}

image/src/main/java/com/smarttoolfactory/image/zoom/ZoomStateImpl.kt

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import kotlinx.coroutines.launch
2222
* @param rotationEnabled when set to true rotation is enabled
2323
*/
2424
@Immutable
25-
open class ZoomState internal constructor(
25+
open class ZoomState(
2626
initialZoom: Float = 1f,
2727
initialRotation: Float = 0f,
2828
minZoom: Float = 1f,
@@ -64,7 +64,8 @@ open class ZoomState internal constructor(
6464
val isRotating: Boolean
6565
get() = animatableRotation.isRunning
6666

67-
val isAnimationRunning = isZooming || isPanning || isRotating
67+
val isAnimationRunning: Boolean
68+
get() = isZooming || isPanning || isRotating
6869

6970
val zoomData: ZoomData
7071
get() = ZoomData(
@@ -73,42 +74,38 @@ open class ZoomState internal constructor(
7374
rotation = rotation
7475
)
7576

76-
open fun updateBounds(lowerBound: Offset?, upperBound: Offset?) {
77+
internal open fun updateBounds(lowerBound: Offset?, upperBound: Offset?) {
7778
animatablePan.updateBounds(lowerBound, upperBound)
7879
}
7980

80-
internal fun getBounds(size: IntSize): Offset {
81+
internal open fun getBounds(size: IntSize): Offset {
8182
val maxX = (size.width * (zoom - 1) / 2f).coerceAtLeast(0f)
8283
val maxY = (size.height * (zoom - 1) / 2f).coerceAtLeast(0f)
8384
return Offset(maxX, maxY)
8485
}
8586

86-
internal open suspend fun updateZoomState(
87+
open suspend fun updateZoomState(
8788
size: IntSize,
8889
gesturePan: Offset,
8990
gestureZoom: Float,
9091
gestureRotate: Float = 1f,
9192
) {
9293
val zoomChange = (zoom * gestureZoom).coerceIn(zoomMin, zoomMax)
94+
snapZoomTo(zoomChange)
9395
val rotationChange = if (rotationEnabled) {
9496
rotation + gestureRotate
9597
} else {
9698
0f
9799
}
98-
99-
snapZoomTo(zoomChange)
100100
snapRotationTo(rotationChange)
101101

102102
if (panEnabled) {
103-
var panChange = pan + gesturePan.times(zoom)
103+
val panChange = pan + gesturePan.times(zoom)
104104
val boundPan = limitPan && !rotationEnabled
105105

106106
if (boundPan) {
107107
val bound = getBounds(size)
108-
panChange = Offset(
109-
panChange.x.coerceIn(-bound.x, bound.x),
110-
panChange.y.coerceIn(-bound.y, bound.y)
111-
)
108+
updateBounds(bound.times(-1f), bound)
112109
}
113110
snapPanTo(panChange)
114111
}

0 commit comments

Comments
 (0)