|
| 1 | +/* |
| 2 | + * Copyright 2025 Lambda |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +package com.lambda.module.modules.render |
| 19 | + |
| 20 | +import com.lambda.event.events.MouseEvent |
| 21 | +import com.lambda.event.events.RenderEvent |
| 22 | +import com.lambda.event.listener.SafeListener.Companion.listen |
| 23 | +import com.lambda.module.Module |
| 24 | +import com.lambda.module.tag.ModuleTag |
| 25 | +import com.lambda.util.NamedEnum |
| 26 | +import java.lang.Math.clamp |
| 27 | + |
| 28 | +object Zoom : Module( |
| 29 | + name = "Zoom", |
| 30 | + description = "Zooms the current view", |
| 31 | + tag = ModuleTag.RENDER, |
| 32 | +) { |
| 33 | + private var zoom by setting("Zoom", 2f, 1f..10f, 0.1f) |
| 34 | + private val style by setting("Style", ZoomStyle.EaseOut) |
| 35 | + private val animationDuration by setting("Animation Duration", 1f, 0.1f..10f, 0.1f) { style != ZoomStyle.Instant } |
| 36 | + private val scroll by setting("Scroll", true) |
| 37 | + private val persistentScroll by setting("Persistent Scroll", false) { scroll } |
| 38 | + private val sensitivity by setting("Sensitivity", 0.2f, 0.1f..1f, 0.1f) { scroll } |
| 39 | + @JvmStatic val smoothMovement by setting("Smooth Movement", false) |
| 40 | + |
| 41 | + private var extraZoom = 0f |
| 42 | + set(value) { |
| 43 | + field = value.coerceAtLeast(-zoom + 1) |
| 44 | + } |
| 45 | + @JvmStatic val targetZoom: Float |
| 46 | + get() = zoom + extraZoom |
| 47 | + |
| 48 | + @JvmStatic var currentZoom = 1f; private set |
| 49 | + private var lastZoomTime = 1L |
| 50 | + private val zoomProgress |
| 51 | + get() = clamp((System.currentTimeMillis() - lastZoomTime) / (animationDuration * 1000).toDouble(), 0.0, 1.0).toFloat() |
| 52 | + |
| 53 | + init { |
| 54 | + listen<MouseEvent.Scroll> { event -> |
| 55 | + val yDelta = event.delta.y.toFloat() |
| 56 | + val delta = (yDelta * sensitivity) + (((zoom + extraZoom) * sensitivity) * yDelta) |
| 57 | + if (persistentScroll) zoom += delta |
| 58 | + else extraZoom += delta |
| 59 | + updateZoomTime() |
| 60 | + event.cancel() |
| 61 | + } |
| 62 | + |
| 63 | + listen<RenderEvent.Render>(alwaysListen = true) { |
| 64 | + updateCurrentZoom() |
| 65 | + } |
| 66 | + |
| 67 | + onEnable { |
| 68 | + updateZoomTime() |
| 69 | + } |
| 70 | + |
| 71 | + onDisable { |
| 72 | + extraZoom = 0f |
| 73 | + updateZoomTime() |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private fun updateZoomTime() { |
| 78 | + lastZoomTime = System.currentTimeMillis() |
| 79 | + } |
| 80 | + |
| 81 | + @JvmStatic |
| 82 | + fun updateCurrentZoom() { |
| 83 | + val target = if (isEnabled) targetZoom else 1f |
| 84 | + currentZoom = style.apply(currentZoom, target, zoomProgress) |
| 85 | + } |
| 86 | + |
| 87 | + private enum class ZoomStyle( |
| 88 | + override val displayName: String, |
| 89 | + val apply: (Float, Float, Float) -> Float, |
| 90 | + ) : NamedEnum { |
| 91 | + Instant("Instant", { _, v, _ -> v }), |
| 92 | + EaseOut("Ease Out", { start, end, progress -> start + ((end - start) * (1f - ((1f - progress) * (1f - progress)))) }), |
| 93 | + EaseIn("Ease In", { start, end, progress -> start + ((end - start) * (progress * progress)) }) |
| 94 | + } |
| 95 | +} |
0 commit comments