From 6de035945166c3f5fb966c2b12a1858b40cc918a Mon Sep 17 00:00:00 2001 From: isxander Date: Sat, 25 May 2024 18:39:57 +0100 Subject: [PATCH] Add separate keybinds for hold/toggle and fix controlify compat --- build.gradle.kts | 2 +- changelogs/2.13.5.md | 14 +++++ .../kotlin/dev/isxander/zoomify/Zoomify.kt | 55 ++++++++++------ .../zoomify/config/SettingsGuiFactory.kt | 12 ---- .../zoomify/config/ZoomifySettings.kt | 16 ----- .../config/migrator/impl/OkZoomerMigrator.kt | 10 --- .../integrations/ControlifyIntegration.kt | 63 +++++++++++++++---- .../dev/isxander/zoomify/utils/KeySource.kt | 35 +++++++++++ .../resources/assets/zoomify/lang/en_us.json | 3 +- src/main/resources/fabric.mod.json | 3 +- versions/1.20.1/gradle.properties | 2 +- versions/1.20.4/gradle.properties | 2 +- versions/1.20.6/gradle.properties | 2 +- 13 files changed, 144 insertions(+), 75 deletions(-) create mode 100644 changelogs/2.13.5.md create mode 100644 src/main/kotlin/dev/isxander/zoomify/utils/KeySource.kt diff --git a/build.gradle.kts b/build.gradle.kts index c693471..5d1ec9f 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -18,7 +18,7 @@ val mcSemverVersion = stonecutter.current.version val mcDep = property("fmj.mcDep").toString() group = "dev.isxander" -val versionWithoutMC = "2.13.4" +val versionWithoutMC = "2.13.5" version = "$versionWithoutMC+${stonecutter.current.project}" val isAlpha = "alpha" in version.toString() diff --git a/changelogs/2.13.5.md b/changelogs/2.13.5.md new file mode 100644 index 0000000..a9485d5 --- /dev/null +++ b/changelogs/2.13.5.md @@ -0,0 +1,14 @@ +# Zoomify 2.13.5 + +Zoomify now targets the following Minecraft versions: + +- 1.20.1 +- 1.20.4 (also supports 1.20.3) +- 1.20.6 (also supports 1.20.5) + +Make sure you download the correct version for your Minecraft version! + +## Changes + +- Fix Controlify compatibility +- Remove zoom key behaviour option and instead have two separate keybinds diff --git a/src/main/kotlin/dev/isxander/zoomify/Zoomify.kt b/src/main/kotlin/dev/isxander/zoomify/Zoomify.kt index dabf143..a88b3e1 100644 --- a/src/main/kotlin/dev/isxander/zoomify/Zoomify.kt +++ b/src/main/kotlin/dev/isxander/zoomify/Zoomify.kt @@ -1,9 +1,12 @@ package dev.isxander.zoomify import com.mojang.blaze3d.platform.InputConstants +import dev.isxander.controlify.platform.client.PlatformClientUtil import dev.isxander.zoomify.config.* import dev.isxander.zoomify.config.migrator.Migrator import dev.isxander.zoomify.integrations.constrainModVersionIfLoaded +import dev.isxander.zoomify.utils.MultiKeySource +import dev.isxander.zoomify.utils.toKeySource import dev.isxander.zoomify.utils.toast import dev.isxander.zoomify.zoom.* import net.fabricmc.api.ClientModInitializer @@ -25,13 +28,32 @@ import org.slf4j.LoggerFactory object Zoomify : ClientModInitializer { val LOGGER = LoggerFactory.getLogger("Zoomify")!! - private val zoomKey = KeyMapping("zoomify.key.zoom", InputConstants.Type.KEYSYM, InputConstants.KEY_C, "zoomify.key.category") - private val secondaryZoomKey = KeyMapping("zoomify.key.zoom.secondary", InputConstants.Type.KEYSYM, InputConstants.KEY_F6, "zoomify.key.category") + val zoomHoldKey = MultiKeySource() + val zoomToggleKey = MultiKeySource() + val secondaryZoomKey = MultiKeySource() + + val zoomHoldKeyMapping = KeyMapping( + "zoomify.key.zoom", + InputConstants.KEY_C, + "zoomify.key.category" + ) + val zoomToggleKeyMapping = KeyMapping( + "zoomify.key.zoom_toggle", + -1, + "zoomify.key.category" + ) + val secondaryZoomKeyMapping = KeyMapping( + "zoomify.key.zoom.secondary", + InputConstants.KEY_F6, + "zoomify.key.category" + ) private val scrollZoomIn = KeyMapping("zoomify.key.zoom.in", -1, "zoomify.key.category") private val scrollZoomOut = KeyMapping("zoomify.key.zoom.out", -1, "zoomify.key.category") var zooming = false private set + private var zoomToggled = false + private val zoomHelper = ZoomHelper( TransitionInterpolator( ZoomifySettings::zoomInTransition, @@ -74,14 +96,13 @@ object Zoomify : ClientModInitializer { private var displayGui = false override fun onInitializeClient() { - // controlify compat only works on 2.x - constrainModVersionIfLoaded("controlify", "2.x.x") - // imports on ZoomifySettings - KeyBindingHelper.registerKeyBinding(zoomKey) - KeyBindingHelper.registerKeyBinding(secondaryZoomKey) + zoomHoldKey.addSource(zoomHoldKeyMapping.toKeySource(register = true)) + zoomToggleKey.addSource(zoomToggleKeyMapping.toKeySource(register = true)) + secondaryZoomKey.addSource(secondaryZoomKeyMapping.toKeySource(register = true)) + if (ZoomifySettings.keybindScrolling) { KeyBindingHelper.registerKeyBinding(scrollZoomIn) KeyBindingHelper.registerKeyBinding(scrollZoomOut) @@ -102,16 +123,12 @@ object Zoomify : ClientModInitializer { private fun tick(minecraft: Minecraft) { val prevZooming = zooming - when (ZoomifySettings.zoomKeyBehaviour) { - ZoomKeyBehaviour.HOLD -> zooming = zoomKey.isDown - ZoomKeyBehaviour.TOGGLE -> { - while (zoomKey.consumeClick()) { - zooming = !zooming - } - } + if (zoomToggleKey.justPressed) { + zoomToggled = !zoomToggled } + zooming = zoomHoldKey.isDown || zoomToggled - while (secondaryZoomKey.consumeClick()) { + if (secondaryZoomKey.justPressed) { secondaryZooming = !secondaryZooming } @@ -229,9 +246,9 @@ object Zoomify : ClientModInitializer { fun unbindConflicting(): Boolean { val minecraft = Minecraft.getInstance() - if (!zoomKey.isUnbound) { + if (!zoomHoldKeyMapping.isUnbound) { for (key in minecraft.options.keyMappings) { - if (key != zoomKey && key.equals(zoomKey)) { + if (key != zoomHoldKeyMapping && key.same(zoomHoldKeyMapping)) { minecraft.options.setKey(key, InputConstants.UNKNOWN) toast( @@ -253,10 +270,10 @@ object Zoomify : ClientModInitializer { private fun detectConflictingToast() { val minecraft = Minecraft.getInstance() - if (zoomKey.isUnbound) + if (zoomHoldKeyMapping.isUnbound) return - if (minecraft.options.keyMappings.any { it != zoomKey && it.equals(zoomKey) }) { + if (minecraft.options.keyMappings.any { it != zoomHoldKeyMapping && it.same(zoomHoldKeyMapping) }) { toast( Component.translatable("zoomify.toast.conflictingKeybind.title"), Component.translatable("zoomify.toast.conflictingKeybind.description", diff --git a/src/main/kotlin/dev/isxander/zoomify/config/SettingsGuiFactory.kt b/src/main/kotlin/dev/isxander/zoomify/config/SettingsGuiFactory.kt index 1becf52..e87cfb4 100644 --- a/src/main/kotlin/dev/isxander/zoomify/config/SettingsGuiFactory.kt +++ b/src/main/kotlin/dev/isxander/zoomify/config/SettingsGuiFactory.kt @@ -299,18 +299,6 @@ fun createSettingsGui(parent: Screen? = null): Screen { category(ConfigCategory.createBuilder().apply { name(Component.translatable(ZoomifySettings.CONTROLS)) - option(Option.createBuilder().apply { - useSettxiName(ZoomifySettings::zoomKeyBehaviour) - desc { - text(Component.translatable(ZoomifySettings::zoomKeyBehaviour.setting.description)) - } - bindSetting(ZoomifySettings::zoomKeyBehaviour) - controller { opt -> EnumControllerBuilder.create(opt).apply { - enumClass(ZoomKeyBehaviour::class.java) - formatSettxiEnum() - }} - }.build()) - option(Option.createBuilder().apply { useSettxiName(ZoomifySettings::_keybindScrolling) desc { diff --git a/src/main/kotlin/dev/isxander/zoomify/config/ZoomifySettings.kt b/src/main/kotlin/dev/isxander/zoomify/config/ZoomifySettings.kt index 76b8768..86f2d55 100644 --- a/src/main/kotlin/dev/isxander/zoomify/config/ZoomifySettings.kt +++ b/src/main/kotlin/dev/isxander/zoomify/config/ZoomifySettings.kt @@ -135,22 +135,6 @@ object ZoomifySettings : SettxiFileConfig( category = SCROLLING } - var zoomKeyBehaviour by enum(ZoomKeyBehaviour.HOLD) { - name = "zoomify.gui.zoomKeyBehaviour.name" - description = "zoomify.gui.zoomKeyBehaviour.description" - category = CONTROLS - migrator { type -> - if (type.primitive.isString) { - Zoomify.LOGGER.info("Migrating transition type from string to int") - PrimitiveType.of(ZoomKeyBehaviour.values().find { keyBehaviour -> - keyBehaviour.displayName.lowercase() - .replace(Regex("\\W+"), "_") - .trim { it == '_' || it.isWhitespace() } == type.primitive.string - }!!.ordinal).also { needsSaving = true } - } else type - } - } - var keybindScrolling = false private set diff --git a/src/main/kotlin/dev/isxander/zoomify/config/migrator/impl/OkZoomerMigrator.kt b/src/main/kotlin/dev/isxander/zoomify/config/migrator/impl/OkZoomerMigrator.kt index 5d2d693..ae56de2 100644 --- a/src/main/kotlin/dev/isxander/zoomify/config/migrator/impl/OkZoomerMigrator.kt +++ b/src/main/kotlin/dev/isxander/zoomify/config/migrator/impl/OkZoomerMigrator.kt @@ -50,16 +50,6 @@ object OkZoomerMigrator : Migrator { ZoomifySettings.relativeSensitivity = 0 } - when (okz.features.zoomMode) { - ZoomMode.HOLD -> - ZoomifySettings.zoomKeyBehaviour = ZoomKeyBehaviour.HOLD - ZoomMode.TOGGLE -> - ZoomifySettings.zoomKeyBehaviour = ZoomKeyBehaviour.TOGGLE - ZoomMode.PERSISTENT -> { - migration.error(Component.translatable("zoomify.migrate.okz.persistent")) - } - } - ZoomifySettings._keybindScrolling = okz.features.extraKeyBinds migration.requireRestart() diff --git a/src/main/kotlin/dev/isxander/zoomify/integrations/ControlifyIntegration.kt b/src/main/kotlin/dev/isxander/zoomify/integrations/ControlifyIntegration.kt index d56f631..9438516 100644 --- a/src/main/kotlin/dev/isxander/zoomify/integrations/ControlifyIntegration.kt +++ b/src/main/kotlin/dev/isxander/zoomify/integrations/ControlifyIntegration.kt @@ -1,31 +1,70 @@ package dev.isxander.zoomify.integrations import dev.isxander.controlify.api.ControlifyApi -import dev.isxander.controlify.api.bind.ControlifyBindingsApi +import dev.isxander.controlify.api.bind.ControlifyBindApi +import dev.isxander.controlify.api.bind.InputBinding +import dev.isxander.controlify.api.bind.InputBindingSupplier import dev.isxander.controlify.api.entrypoint.ControlifyEntrypoint import dev.isxander.controlify.api.event.ControlifyEvents -import dev.isxander.controlify.api.ingameinput.LookInputModifier -import dev.isxander.controlify.controller.ControllerEntity +import dev.isxander.controlify.bindings.BindContext +import dev.isxander.controlify.bindings.RadialIcons import dev.isxander.zoomify.Zoomify import dev.isxander.zoomify.config.ZoomifySettings +import dev.isxander.zoomify.utils.KeySource +import net.minecraft.network.chat.Component import net.minecraft.util.Mth +import net.minecraft.world.item.Items object ControlifyIntegration : ControlifyEntrypoint { + private val category = Component.translatable("zoomify.key.category") + + val ZOOM_HOLD = ControlifyBindApi.get().registerBinding { it.apply { + id("zoomify", "zoom_hold") + allowedContexts(BindContext.IN_GAME) + addKeyCorrelation(Zoomify.zoomHoldKeyMapping) + category(category) + name(Component.translatable(Zoomify.zoomHoldKeyMapping.name)) + } } + val ZOOM_TOGGLE = ControlifyBindApi.get().registerBinding { it.apply { + id("zoomify", "zoom_toggle") + allowedContexts(BindContext.IN_GAME) + addKeyCorrelation(Zoomify.zoomToggleKeyMapping) + radialCandidate(RadialIcons.getItem(Items.SPYGLASS)) + category(category) + name(Component.translatable(Zoomify.zoomToggleKeyMapping.name)) + } } + val SECONDARY_ZOOM = ControlifyBindApi.get().registerBinding { it.apply { + id("zoomify", "secondary_zoom") + allowedContexts(BindContext.IN_GAME) + addKeyCorrelation(Zoomify.secondaryZoomKeyMapping) + radialCandidate(RadialIcons.getItem(Items.SPYGLASS)) + category(category) + name(Component.translatable(Zoomify.secondaryZoomKeyMapping.name)) + } } + override fun onControlifyPreInit(controlify: ControlifyApi) { - ControlifyEvents.LOOK_INPUT_MODIFIER.register(SensitivityModifier) + ControlifyEvents.LOOK_INPUT_MODIFIER.register { event -> + event.lookInput.x /= Mth.lerp(ZoomifySettings.relativeSensitivity / 100.0, 1.0, Zoomify.previousZoomDivisor).toFloat() + event.lookInput.y /= Mth.lerp(ZoomifySettings.relativeSensitivity / 100.0, 1.0, Zoomify.previousZoomDivisor).toFloat() + } + + Zoomify.zoomHoldKey.addSource(InputBindingKeySource(ZOOM_HOLD)) + Zoomify.zoomToggleKey.addSource(InputBindingKeySource(ZOOM_TOGGLE)) + Zoomify.secondaryZoomKey.addSource(InputBindingKeySource(SECONDARY_ZOOM)) } override fun onControllersDiscovered(controlify: ControlifyApi) { } +} - object SensitivityModifier : LookInputModifier { - override fun modifyX(x: Float, controller: ControllerEntity): Float { - return x * Mth.lerp(ZoomifySettings.relativeSensitivity / 100.0, 1.0, Zoomify.previousZoomDivisor).toFloat() - } +private class InputBindingKeySource(private val bindingSupplier: InputBindingSupplier) : KeySource { + private val binding: InputBinding? + get() = ControlifyApi.get().currentController.orElse(null)?.let { bindingSupplier.on(it) } - override fun modifyY(y: Float, controller: ControllerEntity): Float { - return y * Mth.lerp(ZoomifySettings.relativeSensitivity / 100.0, 1.0, Zoomify.previousZoomDivisor).toFloat() - } - } + override val justPressed: Boolean + get() = binding?.justPressed() == true + + override val isDown: Boolean + get() = binding?.digitalNow() == true } diff --git a/src/main/kotlin/dev/isxander/zoomify/utils/KeySource.kt b/src/main/kotlin/dev/isxander/zoomify/utils/KeySource.kt new file mode 100644 index 0000000..b4c2b2d --- /dev/null +++ b/src/main/kotlin/dev/isxander/zoomify/utils/KeySource.kt @@ -0,0 +1,35 @@ +package dev.isxander.zoomify.utils + +import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper +import net.minecraft.client.KeyMapping + +interface KeySource { + val justPressed: Boolean + + val isDown: Boolean +} + +class MultiKeySource(sources: List) : KeySource { + private val sources = sources.toMutableList() + + constructor(vararg sources: KeySource) : this(sources.toList()) + constructor() : this(emptyList()) + + override val justPressed: Boolean + get() = sources.any { it.justPressed } + + override val isDown: Boolean + get() = sources.any { it.isDown } + + fun addSource(source: KeySource) { + sources.add(source) + } +} + +fun KeyMapping.toKeySource(register: Boolean = false) = object : KeySource { + override val justPressed: Boolean + get() = this@toKeySource.consumeClick() + + override val isDown: Boolean + get() = this@toKeySource.isDown +}.also { if (register) KeyBindingHelper.registerKeyBinding(this) } diff --git a/src/main/resources/assets/zoomify/lang/en_us.json b/src/main/resources/assets/zoomify/lang/en_us.json index 4b7d85c..2ec63c8 100644 --- a/src/main/resources/assets/zoomify/lang/en_us.json +++ b/src/main/resources/assets/zoomify/lang/en_us.json @@ -1,5 +1,6 @@ { - "zoomify.key.zoom": "Zoom", + "zoomify.key.zoom": "Zoom (Hold)", + "zoomify.key.zoom_toggle": "Zoom (Toggle)", "zoomify.key.zoom.secondary": "Secondary Zoom", "zoomify.key.zoom.in": "Scroll Zoom In", "zoomify.key.zoom.out": "Scroll Zoom Out", diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index bd0394b..0c72001 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -53,6 +53,7 @@ "modmenu": "*" }, "breaks": { - "optifabric": "*" + "optifabric": "*", + "controlify": "<2.0.0-beta.9" } } diff --git a/versions/1.20.1/gradle.properties b/versions/1.20.1/gradle.properties index 6c75698..fb01678 100644 --- a/versions/1.20.1/gradle.properties +++ b/versions/1.20.1/gradle.properties @@ -4,7 +4,7 @@ deps.parchment=1.20.1:2023.09.03 deps.fabricApi=0.92.0+1.20.1 deps.yacl=3.4.2+1.20.1-fabric deps.modMenu=7.2.2 -deps.controlify=2.0.0-beta.8+1.20.1 +deps.controlify=2.0.0-beta.9+1.20.1 java.version=17 diff --git a/versions/1.20.4/gradle.properties b/versions/1.20.4/gradle.properties index 289e6b8..b7d5d40 100644 --- a/versions/1.20.4/gradle.properties +++ b/versions/1.20.4/gradle.properties @@ -4,7 +4,7 @@ deps.parchment=1.20.4:2024.04.14 deps.fabricApi=0.96.11+1.20.4 deps.yacl=3.4.0+1.20.4-fabric deps.modMenu=9.0.0 -deps.controlify=2.0.0-beta.8+1.20.4 +deps.controlify=2.0.0-beta.9+1.20.4 java.version=17 diff --git a/versions/1.20.6/gradle.properties b/versions/1.20.6/gradle.properties index 6539535..8a9fd1c 100644 --- a/versions/1.20.6/gradle.properties +++ b/versions/1.20.6/gradle.properties @@ -4,7 +4,7 @@ deps.parchment=1.20.6:2024.05.01 deps.fabricApi=0.97.8+1.20.6 deps.yacl=3.4.2+1.20.5-fabric deps.modMenu=10.0.0-beta.1 -deps.controlify=2.0.0-beta.8+1.20.6 +deps.controlify=2.0.0-beta.9+1.20.6 java.version=21