Skip to content

Commit

Permalink
Add separate keybinds for hold/toggle and fix controlify compat
Browse files Browse the repository at this point in the history
  • Loading branch information
isXander committed May 25, 2024
1 parent 917d9b3 commit 6de0359
Show file tree
Hide file tree
Showing 13 changed files with 144 additions and 75 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
14 changes: 14 additions & 0 deletions changelogs/2.13.5.md
Original file line number Diff line number Diff line change
@@ -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
55 changes: 36 additions & 19 deletions src/main/kotlin/dev/isxander/zoomify/Zoomify.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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 <init>
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)
Expand All @@ -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
}

Expand Down Expand Up @@ -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(
Expand All @@ -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",
Expand Down
12 changes: 0 additions & 12 deletions src/main/kotlin/dev/isxander/zoomify/config/SettingsGuiFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -299,18 +299,6 @@ fun createSettingsGui(parent: Screen? = null): Screen {
category(ConfigCategory.createBuilder().apply {
name(Component.translatable(ZoomifySettings.CONTROLS))

option(Option.createBuilder<ZoomKeyBehaviour>().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<Boolean>().apply {
useSettxiName(ZoomifySettings::_keybindScrolling)
desc {
Expand Down
16 changes: 0 additions & 16 deletions src/main/kotlin/dev/isxander/zoomify/config/ZoomifySettings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
35 changes: 35 additions & 0 deletions src/main/kotlin/dev/isxander/zoomify/utils/KeySource.kt
Original file line number Diff line number Diff line change
@@ -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>) : 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) }
3 changes: 2 additions & 1 deletion src/main/resources/assets/zoomify/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"modmenu": "*"
},
"breaks": {
"optifabric": "*"
"optifabric": "*",
"controlify": "<2.0.0-beta.9"
}
}
2 changes: 1 addition & 1 deletion versions/1.20.1/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion versions/1.20.4/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion versions/1.20.6/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 6de0359

Please sign in to comment.