-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathZoomBasePlugin.kt
More file actions
59 lines (44 loc) · 2.06 KB
/
ZoomBasePlugin.kt
File metadata and controls
59 lines (44 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package co.stonephone.stonecamera.plugins
import android.annotation.SuppressLint
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import co.stonephone.stonecamera.StoneCameraViewModel
import co.stonephone.stonecamera.utils.selectCameraForStepZoomLevel
class ZoomBasePlugin : IPlugin {
override val id: String = "zoomBasePlugin"
override val name: String = "Zoom Base"
var currentZoom by mutableStateOf(1f)
var viewModel: StoneCameraViewModel? = null
@SuppressLint("ClickableViewAccessibility")
override fun initialize(viewModel: StoneCameraViewModel) {
this.viewModel = viewModel
currentZoom = 1f
}
fun setZoom(zoomFactor: Float) {
if (viewModel == null) return
else {
val cameras = viewModel!!.cameras
val facingCameras =
cameras.filter { it.lensFacing == viewModel!!.camera?.cameraInfo?.lensFacing }
val camera = viewModel!!.camera
// If difference in zoom is above threshold, we auto-cancel focus
// if (kotlin.math.abs(zoomFactor - oldZoom) > ZOOM_CANCEL_THRESHOLD) {
// cancelFocus("zoom changed by more than $ZOOM_CANCEL_THRESHOLD")
// }
val maxCamera = cameras.maxByOrNull { it.relativeZoom ?: 1f } ?: return
val maxRelativeZoom = (maxCamera.relativeZoom ?: 1f) * maxCamera.maxZoom
val minRelativeZoom =
cameras.minByOrNull { it.relativeZoom ?: 1f }?.relativeZoom ?: 1.0f
val newRelativeZoom = zoomFactor.coerceIn(minRelativeZoom, maxRelativeZoom)
currentZoom = newRelativeZoom
val (targetCamera, actualZoomRatio) = selectCameraForStepZoomLevel(
newRelativeZoom,
facingCameras
)
viewModel!!.setSelectedCamera(targetCamera.cameraId)
camera?.cameraControl?.setZoomRatio(actualZoomRatio)
}
}
override val settings: List<PluginSetting> = emptyList() // No settings for tap-to-focus yet
}